SoFunction
Updated on 2025-03-06

Tutorial on using python library fire

1. Introduction

Currently, the Python command line parsing library used include: argparse (comed with python), click (written by Flask author)

But compared with the fire library, it is not "simple" enough to use. A line of fire code can export functions to a command line window.

Example of argparse usage

# arg_test.py

import argparse

def counter(file_type=None):
    return {file_type: 100}

# Initialize parserparser = ()
# Define parametersparser.add_argument("-f", "--file", help="Statistics specified file type")
#Analysisargs = parser.parse_args()
print(counter())

run:

$ python arg_test.py -f python
{'python': 100}

Click usage example

# 

import click

@()
@("-f", "--file", help="Statistics specified file type")
def counter(file=None):
    ({file: 100})

if __name__ == '__main__':
    print(counter())

run:

$ python  -f python
{u'python': 100}

2. Fire installation

# pip install fire# conda install fire -c conda-forge# Source code installation: git gets the code, execute: python install

3. Fire usage example

()Call () directly in the program, without modifying the objective function, fire will export all objects in the current module to the command line

# 

import fire

def foo(name):
    return 'foo {name}!'.format(name=name)

def bar(name):
    return "bar {name}".format(name=name)

if __name__ == '__main__':
    ()

run:

# Call method: python [file name] [function name] [parameter]
# The function name is followed by the parameter value$ python  foo hello
foo hello!
# You can also specify the parameter name first and then follow the parameter value.$ python  bar --name hello
bar hello!

(<function>)You can specify that a function is exported to the command line

import fire

def foo(name):
    return 'foo {name}!'.format(name=name)

if __name__ == '__main__':
    (foo)

run:

# Call method: python [function name] [parameter]$ python  hello
foo hello!

When Fire receives the function foo as a parameter, only the foo function is loaded into the command line. At this time, there is no need to specify the function name when running in the command line, just specify the parameters.

(<dict>)

Fire can not only receive functions, but also receive dictionary objects as parameters. You can configure those functions in the dictionary that need to be exported to the command line.

For example, there are 3 functions: addition, subtraction, and multiplication. We can select 2 of them to export to the command line:

import fire

def add(x, y):
    return x + y

def multiply(x, y):
    return x * y

def subtract(x, y):
    return x - y

if __name__ == '__main__':
    ({
        'add': add,
        'subtract': subtract,
    })

run:

$ python  add 1 4
5
$ python  subtract 1 4
-3
$ python  multiply 1 4
# multiply will report an error because it is not exported

(<object>)You can also receive instance objects of the class

import fire

class Calculator(object):

  def add(self, x, y):
    return x + y

  def multiply(self, x, y):
    return x * y

if __name__ == '__main__':
  calculator = Calculator()
  (calculator)

run:

$ python  add 10 20
30
$ python  multiply 10 20
200

This is the end of this article about the use of python library fire. For more related python fire content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!