meeting this Thursday -- Yes we can! (Bob the Builder)

2 views
Skip to first unread message

Greg Saunders

unread,
Jun 9, 2010, 12:24:48 AM6/9/10
to pythoncalgary
Meeting is on for this coming Thursday.

Thursday April 8, 7pm to 9pm (ish) 

The Arusha Centre 
Old Y Building 
223 12 Avenue SW 
Suite 106 
Calgary, Alberta 

I'm interested in decorators ... just started using them myself this year ... not sure why it took me so long.

So bring along either your curiosity and or examples of python decorators.

There will be a door prize for the best contribution. Of course, email your examples to the list if you can't be there on Thursday ... you'll still qualify for the door prize (yet to be announced ... could be "up to" a $100 value ... like my DSL is suppose to be "up to" 6 mb down).

:)

Ciao,
Greg

leepro

unread,
Jun 10, 2010, 11:29:47 AM6/10/10
to calgary python user group
This is my usage of decorators in Python to measure an elapsed time of
a method running in a class.
It's very convenience to compare several algorithms.

--DongWoo

-------

def Timer(f):
def measureTime(self, *args, **kargs):
print "Start..."
start = time.time()
ret = f(self, *args, **kargs)
elapsed = time.time()-start
print "End..... %.2f sec" % elapsed
return ret
return measureTime

class A(BASE):

def __init__(self, a, b, c):

@Timer
def run(self):
......

class B(BASE):

def __init__(self, a, b, c, d):

@Timer
def run(self):
......

Jerry Seutter

unread,
Jun 10, 2010, 12:44:24 PM6/10/10
to python...@googlegroups.com
This triggered something in my brain.  I use a similar decorator to use the python profiler.  I can never remember what the syntax for using the profiler is, so putting it in a decorator turns it into a one-liner.

2010/6/10 leepro <lee...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "calgary python user group" group.
To post to this group, send email to python...@googlegroups.com.
To unsubscribe from this group, send email to pythoncalgar...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pythoncalgary?hl=en.


Greg Saunders

unread,
Jun 10, 2010, 1:10:22 PM6/10/10
to python...@googlegroups.com
My foray into decorators have been limited to class attributes.

--------------------------------------------------------------------
decorator_example.py

import types

class My_obj(object):
    def __init__(self):
        self.__data = 'abc'

    @property
    def data(self):
        return self.__data

    @data.setter
    def data(self, data=None):
        if type(data) == types.StringType:
            self.__data = data
        else:
            raise TypeError('"data" is not a string')

    @data.deleter
    def data(self):
        self.__data = ''


>>> import decorator_example
>>> my_obj = decorator_example.My_obj()
>>> my_obj.data
'abc'
>>> my_obj.data = 'def'
>>> my_obj.data
'def'
>>> my_obj.data = 123

Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    my_obj.data = 123
  File "/home/greg/projects/proman/decorator_example.py", line 16, in data
    raise TypeError('"data" is not a string')
TypeError: "data" is not a string
>>> 
>>> my_obj.data
'def'
>>> del my_obj.data
>>> my_obj.data
''

David Glass

unread,
Jun 10, 2010, 8:18:39 PM6/10/10
to python...@googlegroups.com
My main use of decorators has been with generators/iterators. I often use a fancier version (i.e. with various options and defaults) of the following as a convenience wrapper for the csv module.
 
from contextlib import contextmanager
import csv
 
@contextmanager
def csv_rows(file_name):
    with open(file_name) as f:
        reader = csv.reader(f)
        yield reader
 
#usage
with csv_rows("C:/t/test.csv") as rows:
    for row in rows:
        print row
 
File is presumably closed when the with statement is exited.
Also, I've played with decorators that wrap generators and change them into collection-returning functions. Non-working example:
 
@collector_deco(collection_type=list)
def my_valuesf():
   # loops etc.
   yield x
   # etc.
 
The idea is to collect the results of an iterator/generator rather than explicitly doing something like.
 
def my_values():
    result = []
    # loops etc.
    result.append(x)
    # etc.
    return result
 
Potentially handy when you haven't yet decided if you actually want lazy execution or not, and you want to make changing your decision super-easy. I can't say I've really put this one to use too much.
Reply all
Reply to author
Forward
0 new messages