Courses‎ > ‎Software Development 2‎ > ‎Dyrland-Weaver‎ > ‎Work‎ > ‎

Work 12: Decorating with style!

posted Apr 27, 2017, 6:23 AM by JonAlf Dyrland-Weaver   [ updated Apr 27, 2017, 11:32 AM ]
Python decorators are used when we want to apply the same process to multiple functions.
This can be useful for developing a logging framework to help developing and debugging code. Consider the following two kinds of logging:
  1. Printing out the name of a function and its arguments each time it runs.
  2. Printing out the amount of time a function takes to run.
In order to do this, you need to be able to do the following (in addition to creating closures and decorators)
  1. Get time information
    • python has a time module, after importing you can do the following:
      • time.time() will give you epoch time
  2. Pass arguments from a function variable (f) of an outer function (wrapper) to an inner function (inner)
    • *arg represents arguments passed to a function, look at the following:
      • def wrapper( f ):
           def inner( *arg ):
               return f( *arg )
           return inner


        closure = wrapper(foo)
        closure( -2, 3, 'hello' ) #will run foo with the arguments -2, 3, 'hello' through wrapper
  3. Get the name of a function
    • if f is a variable that represents a function f.func_name will return the name of the function.
  4. Before you go onto the rest of this assignment, please try these things out
Your mission is to create the structure for 2 decorators, one to log the name and arguments of a function and one to log the execution time.

What would this all look like?
  • If you had a standard recursive fibonacci function, putting a name and argument logger (by creating the appropriate closure apparatus and using it as a decorator) on it would result in output like the following:
    • $ python fibtest.py 
      fib: (2,)                                                                                                                                                  
      fib: (1,)                                                                                                                                                  
      fib: (3,)                                                                                                                                                  
      fib: (2,)                                                                                                                                                  
      fib: (4,)                                                                                                                                                  
      fib: (2,)                                                                                                                                                  
      fib: (1,)                                                                                                                                                  
      fib: (3,)                                                                                                                                                  
      fib: (5,)                                                                     
      5          
  • If you had a different function, and created an execution time logger, you might see output like this:
    • $ python timetest.py
      execution time: 4.87653708458

Submit this in the workshop under decorators
Comments