Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Click Click
#4
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.
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 Think
# 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.
Reply


Messages In This Thread
Click Click - by snippsat - Mar-07-2017, 12:04 AM
RE: Click Click - by wavic - Mar-07-2017, 06:17 AM
RE: Click Click - by snippsat - Mar-07-2017, 12:34 PM
RE: Click Click - by snippsat - Mar-10-2017, 07:52 PM
RE: Click Click - by GamingAgent - Apr-15-2017, 09:00 AM
RE: Click Click - by wavic - Apr-16-2017, 07:28 AM
RE: Click Click - by snippsat - Apr-16-2017, 12:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  wxpython single click/double click recognition Larz60+ 1 6,197 Jul-23-2017, 06:49 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020