On Wed, Jun 30, 2010 at 9:52 PM, Mary Morris <marris1...@gmail.com> wrote: > I need help with the following task for my new job: > The code we're using is python, which I have never worked with before. > Our AMS source code relies heavily on decorators.Things look something like > this: > @decomaker(argA, argB, ...) > def func(arg1, arg2, ...): > pass > which is the same as > func = decomaker(argA, argB, ...)(func) > and > @dec2 > @dec1 > def func(arg1, arg2, ...): > pass > func = dec2(dec1(func)) > Or when implemented the second example looks like: > def dec1(func): > def new_func(arg1, arg2, ...): > ... do something... > ret = func(arg1, arg 2, ...) > ... do more things... > return ret > return new_func > > My first task is that there is an issue with the name new_func. When the > program crashes, that is what shows up in the logs-which doesn't help debug > anything. I need to find out every decorator and make sure it has a > descriptive name. I was thinking I could write a simple script which would > parse through all of the source files and find decorators-maybe by looking > for the @ symbol? Then I could manually check to make sure it has a good > name. I was thinking I could copy the searching code from find_imports.py > (up to the startswith() calls) and print the list of decorators found and > which files that they're in. I have never worked with python before so I > definitely need help with this task-any suggestions or examples that would > be helpful?
The functools library has a function, called wraps, that makes sure any decorated function maintains its original name and docstring. You use it inside your decorator functions, like so: from functools import wraps def decorator(func): @wraps(func) def new_func(*args, **kwargs): #do some stuff return new_func @decorator def wrapped_func(a, b): """this docstring will be saved, and if the function crashes, it will show up as 'wrapped_func' in your tracebacks""" # do some other stuff HTH, Hugo _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor