I recently tried to put together a few widgets to share with you the common box of Python Utility. This box is very powerful, including but not limited to: one line of command to extract the climax of music, one line of command to recognize faces, one line of command to classify text… Wait, stay tuned!
However, the principle of making such a tool box is not complicated. It is to pass the custom parameters that the user needs to use directly into Python through commands. The user does not need to care about the code, nor does he need to understand the code. However, when making the tool box, I found such a problem that the command line parameter passing tool is not enough.
For example, the following simple example of argparse allows Python commands to take a -n or –number number at runtime and pass that number into the Double of Calculator to compute in a single line:
#! /usr/bin/python
# -*- coding: utf-8 -*-
import argparse
class Calculator(object):
"""
简单算算数.
"""
def double(self, number):
print(2 * number)
def main():
parse = argparse.ArgumentParser()
parse.add_argument("-n"."--number".type=float.help="Used to pass in the value of number")
args = parse.parse_args()
Calculator().double(args.number)
if __name__ == '__main__':
main()
Copy the code
Use:
$python1. Py - number 2, 4.0Copy the code
If I have many methods, don’t I have to define many parameters? If I have a lot of class methods, don’t I have to call them all? The engineers at Google had the same idea I did: simplify it! They developed a module called Fire. This module solves my problem perfectly and simplifies a lot of code, so read on.
1. Prepare
Before you start, you need to make sure you have Python installed on your computer. If not, please visit this article: Super Detailed Python Installation Guide to install it.
After installing Python, open Cmd(start, Run, Cmd) in Windows, and Terminal(command+ space enter Terminal) in apple.
Go to the folder we just downloaded and enter the following command to install the required modules:
pip install fire
Copy the code
If Successfully Installed Fire is displayed, the installation is successful.
2. The headphones
This package is much more comfortable to use than Argparse. For example, let’s simplify the example at the beginning of this article:
import fire
class Calculator(object):
"""
简单算算数.
"""
def double(self, number):
print(2 * number)
if __name__ == '__main__':
fire.Fire(Calculator)
Copy the code
$python 1.py double 10
20
Copy the code
How simple! Do you understand? It can take the parameters as the target function of the Fire specified object, pass in the values of the function, and finally compute the result and output it.
That is, no matter how many “tools” you have, you can put its calling function in a class and call it with the function name as an argument. In this way, we can save a lot of repetitive operations on parse.add_argument.
3. Simpler gameplay
What, there’s a simpler way to play it? Xiaobian can not brag not draft ah. Am I so good as to be a braggart:
**import** fire
english = 'Hello World'
chinese = 'Hello world'
fire.Fire()
Copy the code
$python 1.py english
Hello World
Copy the code
$pythonPy Chinese hello, worldCopy the code
That is, it can be used to call variables directly, without arguments, if your function produces results that end up on variables.
It can also be used directly with functions, such as sorting numbers:
import fire
def order_by_value(*items):
"""Sort by number size"""
sorted_items = sorted(items, key=lambda item: item)
return sorted_items
if __name__ == '__main__':
fire.Fire(order_by_value)
Copy the code
$ python 1.py 20 30 1
1
20
30
Copy the code
Too comfortable, I can make ten tools a day with this tool (no nesting dolls), and I hope you can apply it to the tools you often need to use, and share your open source project in the comments section, a line of command to get the results you want, isn’t it sweet?
So that’s the end of our article, if you’d like to see our Python tutorial today, please stay tuned and give us a thumbs up/check out if it was helpful. If you have any questions, please leave them in the comments below and we’ll be patient to answer them!
Python Dict.com Is more than a dictatorial model
What does a one-line command do? Google engineers do this