Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Unlock the full potential of AI with Building LLMs for Productionβ€”our 470+ page guide to mastering LLMs with practical projects and expert insights!

Publication

Python Fire
Programming

Python Fire

Last Updated on January 7, 2023 by Editorial Team

Author(s): Bala Priya C

Programming

Understanding Fire- a command-line interface generation library

Photo by Maxwell Nelson onΒ Unsplash

This blog post summarizes the use cases of Python Fire, explained by David Bieber, a Software Engineer at Google Brain in a webinar organized by Women Who Code PythonΒ Track

Hello, PythonΒ Fire!

Fire is a Python library that can create a Command-line Interface from absolutely any Python object, created for the purpose of unifying scripting, testing, and operations for complex systems. It’s named β€œFire” as it instantly fires off (executes) our command. It can be of great help in the following workflows:

  • Creating Command-line interfaces
  • Developing and Debugging PythonΒ code
  • Exploring third-party PythonΒ projects

Let’s install fire and look at the generic pattern to use fire on the Python programs/functions that weΒ create.

pip install fire
import fire
fire.Fire(function_name)

Creating CLIs

Let’s call fire on our very own β€œHello World!” program

import fire
def hello(name="world"):
return "Hello " + name + "!"
if __name__== '__main__':
fire.Fire(hello)

Fire now creates a CLI for our program as shownΒ below.

> hello            # Hello world!
> hello @My_Name # Hello @My_Name!
> hello --name=WWC # Hello WWC!

What does calling FireΒ do?

Fire consumes the arguments and uses themΒ to

  • Instantiate aΒ class
  • Call aΒ function
  • Get a member orΒ property

And, it repeats the process until all the arguments have been exhausted. After completion, the resulting component is serialized in a human-readable way.

To reiterate, we can call fire.Fire with any Python class, object, function, module, dict and soΒ on!

Using multipleΒ commands

Let’s see a simple Calculator example, that contains methods add and multiply which each takes two arguments and computes sum and product respectively.

import fire
class Calculator(object):
def add(self,x,y):
return x + y
def multiply(self,x,y):
return x * y
if __name__== '__main__':
fire.Fire(Calculator)

As expected Fire creates a CLI from which we should be able to run multiple commands including inspecting the class, add and multiply functions.

> calculator add 1 2 # 3
> calculator multiply 3 4 # 12
> calculator
Usage: calculator add
calculator multiply

Using Hierarchical commands

In the following code snippet, we have a Tools class, which when instantiated would in turn instantiate the Calculator and WidgetΒ classes.

import fire
class Tools(object):
def __init__(self):
self.calculator = Calculator()
self.widget = Widget()
if __name__== '__main__':
fire.Fire(Tools)
# The CLI that Fire generates looks allows us to access the respective classes and class methods 
> tools calculator add 1 2 # 3
> tools calculator multiply 3 4 # 12
> tools
Usage: tools calculator
tools widget

Debugging and Development

Python Fire library is indeed very helpful in debugging and development where we can just go ahead and call fire.Fire( ) on any Python script without any argument and use in the command-line.

There’s also an option to use the β€˜interactive’ flag which as shown below, imports useful objects and allows us to directly use them from the IPython shell without having to import modules, function callsΒ etc.

> calc -- --interactive
Fire is starting Python REPL with the following objects:
Modules: fire
Objects: Calculator, calc, component, result, trace
IPython -- An enhanced Interactive Python
[1]: calc.add(1,2)
[2]: calc.multiply(3,4)

Exploring third-party PythonΒ projects

As with above-explained examples, we might as well call fire.Fire() on third party modules whose source code we may not necessarily have access to. In the latest release of fire, we can directly call python -m fire from the command-line

Let’s walk through an example with the image processing library PIL. Suppose we have a Python script imagefire.py with the following lines and we want to open a giraffe.png image and perform some operations on theΒ image.

from PIL import Image
import fire
fire.Fire(Image)
> imagefire.py open giraffe.png - convert L - resize 100,100 - save giraffe_out.png

The above code opens the image, converts it to grayscale, resizes it to the specified size, and then saves the resulting image under the name giraffe_out.png. The recent release of fire allows us to do the same all the moreΒ easily!

# NEW release of Fire
> python -m fire PIL.Image open giraffe

References

If you’re looking to delve deeper into Python Fire, the following are some of the resources

  1. Python FireΒ Guide
  2. GitHub repo

The recording of the webinar can be found onΒ YouTube


Python Fire was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.

Published via Towards AI

Feedback ↓