473,396 Members | 2,113 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,396 software developers and data experts.

Function metadata (like Java annotations) in Python

Hi,

I'm trying to attach some attributes to functions and methods, similar
to Java annotations and .NET attributes.
I also want to use a convenient decorator for it, something along the
lines of

@attr(name="xander", age=10)
def foo():
...

Assigning attributes to the function will work, as will assigning keys
and values to a dictionary in an attribute. But if there are more
decorators in the way, this could fail:

@onedec
@attr(...)
@twodec
def foo():
...

Given 'foo' now, how do I find the attributes?

Assigning to a global attribute registry (some interpreter-global
dictionary), although less desirable, might have been acceptable, but
then how do I identify the function after it's been wrapped in more
decorators?

Also, it may be nice to have the following work as well:

@attr(name="Xander")
@attr(age=10)
@somedec
@attr(hobby="knitting")
def foo():
...
Any thoughts? Am I rehashing something old that my search skills didn't
uncover?

Thanks!
Ori.

Sep 10 '06 #1
8 2769
oripel:

Maybe this is a silly suggestion, the docstring is already overloaded,
but it may be used for this too:

def foo():
"""
...
...
@ATTR name="Xander"
@ATTR age=10
@ATTR hobby="knitting"
"""
...

(Or somethins similar without the @). Later you can retrive the
attributes from the docstring, for example using a verbose RE like:
r"\s* @ATTR \s+ (\w*) \s* = \s* (.*)"
And you use it to create a dict of attributes.
The decorators you apply to foo() must keep its docstring too.

Bye,
bearophile

Sep 10 '06 #2
oripel wrote:
I'm trying to attach some attributes to functions and methods, similar
to Java annotations and .NET attributes.
...
Assigning attributes to the function will work, as will assigning keys
and values to a dictionary in an attribute. But if there are more
decorators in the way, this could fail:

@onedec
@attr(...)
@twodec
def foo():
...

Given 'foo' now, how do I find the attributes?
...
Any thoughts? Am I rehashing something old that my search skills didn't
uncover?
There are past discussions about this; google for "python-dev decorator
metadata". For example:
http://thread.gmane.org/gmane.comp.p...06/focus=77507
Robert Brewer
System Architect
Amor Ministries
fu******@amor.org

Sep 10 '06 #3
oripel wrote:
Hi,

I'm trying to attach some attributes to functions and methods, similar
to Java annotations and .NET attributes.
I also want to use a convenient decorator for it, something along the
lines of

@attr(name="xander", age=10)
def foo():
...

Assigning attributes to the function will work, as will assigning keys
and values to a dictionary in an attribute. But if there are more
decorators in the way, this could fail:

@onedec
@attr(...)
@twodec
def foo():
...

Given 'foo' now, how do I find the attributes?

Assigning to a global attribute registry (some interpreter-global
dictionary), although less desirable, might have been acceptable, but
then how do I identify the function after it's been wrapped in more
decorators?

Also, it may be nice to have the following work as well:

@attr(name="Xander")
@attr(age=10)
@somedec
@attr(hobby="knitting")
def foo():
...
Any thoughts? Am I rehashing something old that my search skills didn't
uncover?

Thanks!
Ori.
I wrote up my investigation into function attributes and decorators on
my blog:

http://paddy3118.blogspot.com/2006/0...ttributes.html
http://paddy3118.blogspot.com/2006/0...signed-by.html

What do you think?

- Paddy.

Sep 10 '06 #4
Thanks bearophile,

I prefer not to use docstrings for metadata.

1. Not interfering with the other accepted docstring uses may be
difficult (doctests, epydoc)

2. It's impractical for attributes generated by code:

@attr(reference_profile_stats=pstats.Stats("foo.pr ofile"))
def foo():
...

Regards,
Ori.

be************@lycos.com wrote:
oripel:

Maybe this is a silly suggestion, the docstring is already overloaded,
but it may be used for this too:

def foo():
"""
...
...
@ATTR name="Xander"
@ATTR age=10
@ATTR hobby="knitting"
"""
...

(Or somethins similar without the @). Later you can retrive the
attributes from the docstring, for example using a verbose RE like:
r"\s* @ATTR \s+ (\w*) \s* = \s* (.*)"
And you use it to create a dict of attributes.
The decorators you apply to foo() must keep its docstring too.

Bye,
bearophile
Sep 10 '06 #5
Thanks!

Now I see it's accepted to assume nice decorators that update __dict__.
At least until __decorates__ or something similar is added...

fumanchu wrote:
oripel wrote:
I'm trying to attach some attributes to functions and methods, similar
to Java annotations and .NET attributes.
...
Assigning attributes to the function will work, as will assigning keys
and values to a dictionary in an attribute. But if there are more
decorators in the way, this could fail:

@onedec
@attr(...)
@twodec
def foo():
...

Given 'foo' now, how do I find the attributes?
...
Any thoughts? Am I rehashing something old that my search skills didn't
uncover?

There are past discussions about this; google for "python-dev decorator
metadata". For example:
http://thread.gmane.org/gmane.comp.p...06/focus=77507
Robert Brewer
System Architect
Amor Ministries
fu******@amor.org
Sep 10 '06 #6
Thanks Paddy - you're showing normal use of function attributes.
They're still hidden when wrapped by an uncooperative decorator.

Paddy wrote:
oripel wrote:
Hi,

I'm trying to attach some attributes to functions and methods, similar
to Java annotations and .NET attributes.
I also want to use a convenient decorator for it, something along the
lines of

@attr(name="xander", age=10)
def foo():
...

Assigning attributes to the function will work, as will assigning keys
and values to a dictionary in an attribute. But if there are more
decorators in the way, this could fail:

@onedec
@attr(...)
@twodec
def foo():
...

Given 'foo' now, how do I find the attributes?

Assigning to a global attribute registry (some interpreter-global
dictionary), although less desirable, might have been acceptable, but
then how do I identify the function after it's been wrapped in more
decorators?

Also, it may be nice to have the following work as well:

@attr(name="Xander")
@attr(age=10)
@somedec
@attr(hobby="knitting")
def foo():
...
Any thoughts? Am I rehashing something old that my search skills didn't
uncover?

Thanks!
Ori.

I wrote up my investigation into function attributes and decorators on
my blog:

http://paddy3118.blogspot.com/2006/0...ttributes.html
http://paddy3118.blogspot.com/2006/0...signed-by.html

What do you think?

- Paddy.
Sep 10 '06 #7
oripel wrote:
Thanks Paddy - you're showing normal use of function attributes.
They're still hidden when wrapped by an uncooperative decorator.
The decorator module may be helpful in defining cooperative decorators:
http://www.phyast.pitt.edu/~micheles...mentation.html

George

Sep 10 '06 #8
Thanks,
In Python 2.5 there are also functools.wraps and
functools.update_wrapper:
http://docs.python.org/dev/whatsnew/pep-309.html

George Sakkis wrote:
oripel wrote:
Thanks Paddy - you're showing normal use of function attributes.
They're still hidden when wrapped by an uncooperative decorator.

The decorator module may be helpful in defining cooperative decorators:
http://www.phyast.pitt.edu/~micheles...mentation.html

George
Sep 10 '06 #9

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

Similar topics

3
by: Philipp Lenssen | last post by:
I want to do the following (and I suspect the easiest way to be a regular expression that uses a user-function): Given the string... -------- <p>Hello</p> <p class="bla">World</p> ...
4
by: Doug Holton | last post by:
First let me say please see the wiki page about python decorators if you haven't already: http://www.python.org/cgi-bin/moinmoin/PythonDecorators I propose (and others have) that built-in...
4
by: Elmo Mäntynen | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm interested in various metadata extraction/prosessing/distribution/something tools(including ways of differentiating between files, eg hashing...
14
by: Java and Swing | last post by:
static PyObject *wrap_doStuff(PyObject *self, PyObject *args) { // this will store the result in a Python object PyObject *finalResult; // get arguments from Python char *result = 0; char *in=...
1
by: Jonathan Gibbs | last post by:
I'm very new to xml, and struggling a bit.. I want to use an .xsd file passed to a windows application to define a dataset's schema, and also (if possible) pass other metadata associated with...
2
by: EP | last post by:
I'm looking for a method by which to access Windows files metadata and have not been able to find anything in the standard modules or via Google - what is the standard approach? Shamefully I...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
1
by: Thomas W | last post by:
I know this is slightly off-topic, but since Python is hot at Google and several key members from the python community works there, I was hoping to get some help on this subject. I want to...
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.