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. 8 2723
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
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
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.
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
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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>
...
|
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...
|
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...
|
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=...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |