A quick look at Python Fire.
It's kind of cool,it simplify it down bye using some magic.
I want to start with the class and use it in a normal way.
Use method object "obj" and call method double.
Now the same using fire.
So we can use the class in same way as in first example.
Using a function.
Other stuff eg
From command line:
It's kind of cool,it simplify it down bye using some magic.
I want to start with the class and use it in a normal way.
class Calculator: """A simple calculator class.""" def double(self, number): return 2 * number if __name__ == '__main__': obj = Calculator() print(obj.double(5)) print(obj.double(4.6)) print(obj.double('hello'))
Output:10
9.2
hellohello
So the normal way,instance of the class and assigns to variable obj.Use method object "obj" and call method double.
Now the same using fire.
# fire_class.py import fire class Calculator: """A simple calculator class.""" def double(self, number): return 2 * number if __name__ == '__main__': fire.Fire(Calculator)From command line:
Output:λ python fire_class.py double 5
10
λ python fire_class.py double 4.6
9.2
λ python fire_class.py double hello
hellohello
λ python fire_class.py -- --help
Type: type
String form: <class '__main__.Calculator'>
File: c:\python36\web_scrape\fire_class.py
Line: 4
Docstring: A simple calculator class.
Usage: fire_class.py
So file name fire_class.py
is acting like the method object "obj".So we can use the class in same way as in first example.
Using a function.
# fire_func.py import fire def hello(name): return f'Hello {name}!☂' if __name__ == '__main__': fire.Fire(hello)From command line:
Output:λ python fire_func.py outside
Hello outside!☂
So will *args
work so we can give many argument in # fire_arg.py import fire def hello(*args): return f'Hello {args}!☂' if __name__ == '__main__': fire.Fire(hello)From command line:
Output:λ python fire_arg.py i am from outside and want in
Hello ('i', 'am', 'from', 'outside', 'and', 'want', 'in')!☂
Other stuff eg
--interactive
From command line:
Output:λ python fire_class.py -- --interactive
Fire is starting a Python REPL with the following objects:
Modules: fire
Objects: Calculator, component, fire_class.py, result, trace
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 5.3.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
So it start a IPython interactive shell.