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

Can decorator syntax do this ? (annotated results' names)


I have developed a framework to ease my database programming job, called
"DescribedData Framework"
which use inspection on function/method parameter-name to simplify my code.
for example:

--------
connection = dbi.connect('database-connection-string')
cursor = connection.cursor()
cursor.execute('select TITLE, NAME, AGE from PEOPLE')

descdata = DescribedData(cursor.description, cursor.fetchall())

def show(TITLE, NAME, AGE): print '%s %s %s' % (TITLE, NAME, AGE)

# print all records
print 'Show all data'
descdata.perform(show)

# filter
def teenage(AGE): return AGE <= 19
fdata = FilteredData(teenage, descdata)
print 'Show teenages'
fdata.perform(show)

#sort
def compare_age(AGE1, AGE2): return cmp(AGE1, AGE2)
sdata = SortedData(compare_age, fdata)
print 'Show sorted teenages'
sdata.perform(show)
--------
FilteredData & SortedData are handy decorators for DescribedData
But when I implement more kind of decorators like "JoinedData",
parameter-name inspection is no longer sufficient,
function's *result-name* also have to be inspected.
Currently, I use class instead of function, like this:

--------
class ExtraNameJoiner(object):
def join(self, NAME): return NAME.upper(), NAME.lower(), NAME.capitalize()
def get_joining_description(self): return 'UPPER_NAME', 'LOWER_NAME',
'CAPITALIZED_NAME'

joiner = ExtrasNameJoiner()
jdata = JoinedData(joiner, sdata) # join extra fields
def show_extra(NAME, UPPER_NAME, LOWER_NAME, CAPITALIZED_NAME):
print '%s %s %s %s' % (NAME, UPPER_NAME, LOWER_NAME, CAPITALIZED_NAME)
jdata.perform(show_extra)
--------
The question is: Can the upcomming decorator-syntax be applied in this
situation ?

Can I do something like this :

--------
@resultnames('UPPER_NAME', 'LOWER_NAME', 'CAPITALIZED_NAME')
def join_extra_name(NAME):
return NAME.upper(), NAME.lower(), NAME.capitalize()

jdata = JoinedData(join_extra_name, sdata)
--------
I stopped follow decorator-syntax discussion for a while, because it
seems everlasting.
But now look like it's settle, so I wonder how far can I go with this
new feature ?

Jul 18 '05 #1
1 1269
On Thu, 5 Aug 2004, Sakesun Roykiattisak wrote:
I stopped follow decorator-syntax discussion for a while, because it
seems everlasting.
But now look like it's settle, so I wonder how far can I go with this
new feature ?


Oh, you don't know how wrong you are ;) All Guido's allowance of the @
syntax into 2.4a2 did was reignite debate, with no end in sight (unless he
pushes it through to 2.4 final, something I sincerely hope he doesn't).
If I were you, I'd hold off on using decorators until they're in a stable
branch, since there's a good chance they might disappear in 2.4a3.

To answer your question, decorators allow for pretty much anything you can
define a function to do; the code in your example will work, provided you
provide a suitable definition for resultnames(): it must return a function
that accepts a function and returns a (mangled version of that) function.

Jul 18 '05 #2

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

Similar topics

5
by: barnesc | last post by:
I'm revising my vote. Thanks for the responses. I vote against: class foo: @synchronized @types("o,i,i") @author('Chris King')
24
by: Steven Bethard | last post by:
I think one of the biggest reasons we're having such problems coming to any agreement on decorator syntax is that each proposal makes a number of syntax decisions, not just one. For decorators, I...
7
by: Steven Bethard | last post by:
So here's the state of the decorator debate as I see it: *** Location GvR pretty strongly wants decorators before the function: ...
6
by: Michele Simionato | last post by:
could ildg wrote: > I think decorator is a function which return a function, is this right? > e.g. The decorator below if from http://www.python.org/peps/pep-0318.html#id1. > > def...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.