473,289 Members | 1,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

Finding decorators in a file

Probably a bit of weird question. I realise decorators shouldn't be
executed until the function they are defined with are called, but is
there anyway for me to find all the decorates declared in a file when
I import it? Or perhaps anyway to find the decorators by loading the
file by other methods (with out simply parsing it by hand).

Basically what I'm looking for is a way to, given a python file, look
through that file and find all the decorators and the associated
functions, that includes any arguments that the decorator has.

Sorry if this has been asked before but I've googled no end and come
up with nothing so far.

Oct 27 '07 #1
2 1926
On Behalf Of Andrew West
Basically what I'm looking for is a way to, given a python file, look
through that file and find all the decorators and the associated
functions, that includes any arguments that the decorator has.
The inspect module has a function called "findsource"

import inspect
import my_module

lines, line_num = inspect.findsource(my_module)

decorated_lines = [num
for num, line in enumerate(lines)
if line.strip().startswith("@")]

Probably a little more complicated than that -- like what if a function has
two decorators? -- but I think the basic idea will work.

Regards,
Ryan Ginstrom

Oct 27 '07 #2
On Oct 27, 3:14 am, Andrew West <andreww...@gmail.comwrote:
Probably a bit of weird question. I realise decorators shouldn't be
executed until the function they are defined with are called, but is
there anyway for me to find all the decorates declared in a file when
I import it? Or perhaps anyway to find the decorators by loading the
file by other methods (with out simply parsing it by hand).
There is no need for having *parser Angst* in Python. Once you
understand the syntactical structure of the language it's easy to move
on. I show you how to deal with it using EasyExtend for grepping parse
trees ( which is much easier than using regexps for that purpose ).
Alternatively you can use the compiler package. It hides some low
level stuff being present in EE on this level. You need to decide how
you want to reduce mental overhead and achieve simplicity that "fits
your brain".

EasyExtend is available at www.fiber-space.de

Here is a small tutorial:

# purpose -- seeking for decorators and decorated functions in Python
2.5 stdlibs contextlib.py

import inspect
import parser
import symbol
import token

# just import contextlib, keep source and create a parse tree

import contextlib
src = inspect.getsource(contextlib) # keep source
cst = parser.suite(src).tolist() # create concrete syntax tree as
a nested list

# so far its all standard lib stuff...
# ... now something new:

from EasyExtend.csttools import find_node, find_all

functions = find_all(cst, symbol.funcdef)

# You need to know how these nodes are structured to get more
information out of them
# This can be looked up in EasyExtend/Grammar which contains a copy of
Pythons Grammar or
# in the same file in Pythons source distribution. Here are the
relevant rules:

# decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
# decorators: decorator+
# funcdef: [decorators] 'def' NAME parameters ':' suite

# We see immediately that it's not all that simple. Decorators can be
methods and the first name
# in dotted-name can be the name of an object. For demonstration
purposes we simplify our
# assumptions and suppose to have plain function decorators

func_info = {}

for f in functions:

decorator_info = set() # store decorator information here

decorators = find_node(f, symbol.decorators) # we do *not* want
all decorators
# since there might
be those related to
# a closure that is
handled separately
if decorators:
decos = find_all(decorators, symbol.decorator)
for deco in decos:
# token.NAME has structure [1, 'name', line_no]
deco_name = find_node(deco, token.NAME)[1]
args = find_node(deco, symbol.arglist) # ... we
analyze args later
decorator_info.add((deco_name, args))

f_names = find_all(f, token.NAME, level = 1) # set level
information otherwise you
# get all names
defined anywhere in
# funcdef
func_name = f_names[1][1] # the first name is 'def', the
second one the func_name
func_info[func_name] = decorator_info

# Note: you can move from syntax tree representation straightforward
back to a textual
# representation. This requires not much work but the following
import is nevertheless
# conceptual overhead I do not try to explain in detail here:

from EasyExtend.fibers.zero.fiber import unparse # zero represents
*Python* in the larger
# context of
EasyExtend applications
# unparse
transforms a cst of a particular
# grammar back to
source code

for func_name, deco_info in func_info.items():
for (deco_name, deco_args) in deco_info:
print func_name, deco_name, (unparse(deco_args) if deco_args
else "()")

# I get the rather unspectacular result
# nested contextmanager ()
# You might verify this manually...
Oct 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
4
by: RebelGeekz | last post by:
Just my humble opinion: def bar(low,high): meta: accepts(int,int) returns(float) #more code Use a metadata section, no need to introduce new messy symbols, or mangling our beloved visual...
8
by: Paul Morrow | last post by:
I like many am not wild about the <at> operator. I also don't think that the decorator syntax should be so directly attached to the method, since what we're trying to do is to say something about...
2
by: Guido van Rossum | last post by:
Robert and Python-dev, I've read the J2 proposal up and down several times, pondered all the issues, and slept on it for a night, and I still don't like it enough to accept it. The only reason...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
9
by: Bengt Richter | last post by:
;-) We have @deco def foo(): pass as sugar (unless there's an uncaught exception in the decorator) for def foo(): pass foo = deco(foo) The binding of a class name is similar, and class...
5
by: John Perks and Sarah Mount | last post by:
When handling resources in Python, where the scope of the resource is known, there seem to be two schools of thought: (1) Explicit: f = open(fname) try: # ... finally: f.close()
0
by: Robert Brewer | last post by:
We're trying to get CherryPy 2.1 RC 1 out the door, but setup.py is giving us some problems. In our test suite, we want to test a decorator that we provide. Of course, decorators won't work in...
23
by: Chance Ginger | last post by:
If I define a decorator like: def t(x) : def I(x) : return x return I and use it like: @t(X) def foo(a) :
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.