Use of the Python Fire Library

Source: Internet
Author: User

I. Introduction

Fire is a tool used in Python to generate command line Interfaces, CLIs, without any additional work, requiring only fire from the main module. Fire (), it will automatically convert your code to Cli,fire () parameters can say any Python object

two. Installation
Pip Install fire
three. UsageInstance 1 single function: Python module name parameter 1 parameter 2

Create a new test_fire.py module in the common directory

Import Kindle def Add (A, b):     = A + b    return  countif__name__'__main__ ' : Fire    . Fire (ADD)

When no parameters are taken, the Python test_fire.py will display the Help information

The fire is displayed in the Help message. Fire () parameter type (function), file path, document string, parameter usage and other information

The result of the add parameter operation is as follows:

Note: The two method calls, one is direct with the argument, one is--formal parameter argument form, for convenience, this article takes the first kind of

Instance 2 multiple functions: Python module name Function name parameter
Import Kindle def Add (A, b):     = A + b    return  countdef  Sub (A, b):    = A- b     return result if __name__ ' __main__ ' : Fire    . Fire ()

Plus the result of the parameter operation:

Instance 3 Class (object) Multiple functions: Python module name Function name parameter
ImportKindleclassCalculator (object):defAdd (self, A, b): Count= A +breturnCountdefSub (self, A, b): Result= A-breturnresultif __name__=='__main__': Fire. Fire (Calculator) #这里用类名Calculator或者类的实例化对象Calculator () The result is the same. 

Add parameter Run Result:

Note:

1. Fire is used by default - as a parameter delimiter, so if you want to pass in a similar parameter at the command line 2017-04-22 , then the program will not receive the parameters 2017-04-22 , you need --separator to use to change the delimiter

2. fireAutomatically distinguishes the type of parameters you pass in the command line, such as automatically recognized, automatically recognized, and automatically recognized 20170422 int hello str ‘(1,2)‘ tuple ‘{"name": "Alan Lee"}‘ dict . But what if you want to pass in a string type 20170422 ? Then you need to write: ‘"20170422"‘ or "‘20170422‘" \"20170422\" , anyway, just add an escape, because the command line will eat your quotes by default

Reference Articles

70332074

Https://mp.weixin.qq.com/s/lMOieL_RFRHsh5OW4G4axg

Use of the Python Fire Library

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.