Rohan Singh's Weblog

Writing about software, data science, and things I learn along the way.

Fire

python

Command Line Interface using python-fire

Fire provides a truly simple way to get started with command line applications. It is python friendly and can expose any python object as a command line interface

To get started pip install fire

Docs: read the docs

Simple Use Case 1

Expose a function in your module as CLI

   1    import fire
   2   
   3    def human_population(year=2021):
   4      if year == 2021:
   5        return 8000000000
   6   
   7      return "unknown"
   8   
   9    if __name__ == "__main__":
  10      fire.Fire(human_population)

Output

python module-example.py
8000000000

Simple Use Case 2

Expore your class as CLI

   1    import fire
   2   
   3    database = {
   4      "United States": {
   5         "GDP": "20 Trillion",
   6         "pop": 330000000
   7       },
   8      "India": {
   9         "GDP": "5 Trillion",
  10         "pop": 1500000000
  11       },
  12   
  13    }
  14   
  15   
  16    class Census:
  17      """This class handles the census for the UN"""
  18   
  19      def __init__(self):
  20        self.countries = database
  21   
  22      def info(self, country, attribute):
  23        res = self.countries[country.title()].get(attribute)
  24        return res
  25   
  26   
  27    if __name__ == "__main__":
  28      """
  29   │   use this as key based interface to the dict
  30   │   outputs
  31   │   $python component-example.py India
  32   │   GDP: 5 Trillion
  33   │   pop: 1500000000
  34   │   """
  35      # fire.Fire(database)
  36   
  37      """
  38   │   Expose the class as an Interface
  39   │   """
  40      fire.Fire(Census)

Output

python component-example.py info "united states" GDP
20 Trillion

← Back to TIL