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

Discussion in 'Python' started by Sakesun Roykiattisak, Aug 5, 2004.

  1. 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 ?
     
    Sakesun Roykiattisak, Aug 5, 2004
    #1
    1. Advertising

  2. 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.
     
    Christopher T King, Aug 5, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    6
    Views:
    4,355
    Steven T. Hatton
    Apr 12, 2004
  2. Paul McGuire
    Replies:
    25
    Views:
    619
    Eli Stevens (WG.c)
    Aug 25, 2004
  3. Robert Brewer
    Replies:
    5
    Views:
    260
    Michael Sparks
    Aug 22, 2004
  4. Robert Brewer
    Replies:
    13
    Views:
    471
    Anthony Baxter
    Aug 26, 2004
  5. laredotornado
    Replies:
    4
    Views:
    1,447
    Daniel Pitts
    Sep 23, 2011
Loading...

Share This Page