Best Pythonic Approach to Annotation/Metadata?

Discussion in 'Python' started by Sparky, Jul 15, 2010.

  1. Sparky

    Sparky Guest

    Hello Python community!

    I am building a JSON-RPC web application that uses quite a few models.
    I would like to return JSON encoded object information and I need a
    system to indicate which properties should be returned when the object
    is translated to a JSON encoded string. Unfortunately, this
    application runs on top of Google's App Engine and, thus, private
    attributes are not an option. Also, a preliminary search leads me to
    believe that there are no real established ways to annotate variables.
    Ideally I want to do something like:

    def to_JSON(self):
    returnDict = {}
    for member in filter(someMethod, inspect.getmembers(self)):
    returnDict[member[0]] = member[1]
    return json.dumps(returnDict)

    I recognize that two solutions would be to 1) include a prefix like
    "public_" before variable names or 2) have a list/tuple of attributes
    that should be transmitted, simply using the "in" operator. However,
    both these options seem like a pretty ungraceful way to do it. Does
    anyone else have an idea? Are there any established methods to apply
    metadata / annotations to variables in Python or do you believe one of
    the above is a good "pythonic" way to solve this problem? I am using
    2.6.

    Thanks,
    Sam
    Sparky, Jul 15, 2010
    #1
    1. Advertising

  2. Sparky

    John Krukoff Guest

    On Thu, 2010-07-15 at 12:37 -0700, Sparky wrote:
    <snip>
    > the above is a good "pythonic" way to solve this problem? I am using
    > 2.6.


    Hopefully a helpful correction, but if you're running on google app
    engine, you're using python 2.5 on the google side irrespective of what
    you're running for development.

    --
    John Krukoff <>
    Land Title Guarantee Company
    John Krukoff, Jul 15, 2010
    #2
    1. Advertising

  3. Sparky

    Sparky Guest

    On Jul 15, 2:26 pm, John Krukoff <> wrote:
    > On Thu, 2010-07-15 at 12:37 -0700, Sparky wrote:
    >
    > <snip>
    >
    > > the above is a good "pythonic" way to solve this problem? I am using
    > > 2.6.

    >
    > Hopefully a helpful correction, but if you're running on google app
    > engine, you're using python 2.5 on the google side irrespective of what
    > you're running for development.
    >
    > --
    > John Krukoff <>
    > Land Title Guarantee Company


    Sorry about that and thanks for pointing out my mistake there.

    Sam
    Sparky, Jul 15, 2010
    #3
  4. Sparky

    Chris Rebert Guest

    On Thu, Jul 15, 2010 at 12:37 PM, Sparky <> wrote:
    > Hello Python community!
    >
    > I am building a JSON-RPC web application that uses quite a few models.
    > I would like to return JSON encoded object information and I need a
    > system to indicate which properties should be returned when the object
    > is translated to a JSON encoded string. Unfortunately, this
    > application runs on top of Google's App Engine and, thus, private
    > attributes are not an option. Also, a preliminary search leads me to
    > believe that there are no real established ways to annotate variables.
    > Ideally I want to do something like:
    >
    > def to_JSON(self):
    >        returnDict = {}
    >        for member in filter(someMethod, inspect.getmembers(self)):
    >                returnDict[member[0]] = member[1]
    >        return json.dumps(returnDict)
    >
    > I recognize that two solutions would be to 1) include a prefix like
    > "public_" before variable names or 2) have a list/tuple of attributes
    > that should be transmitted, simply using the "in" operator. However,
    > both these options seem like a pretty ungraceful way to do it. Does
    > anyone else have an idea? Are there any established methods to apply
    > metadata / annotations to variables in Python or do you believe one of
    > the above is a good "pythonic" way to solve this problem?


    Those are about as Pythonic as you're going to get; I for one find the
    prefix (or similar) solution rather neat (Good luck pulling it off in
    a less dynamic language!), but option (2) is probably better in your
    particular case.
    Remember that at class-definition-time, Python has no idea what
    instance variables an object will have, so nothing exists for you to
    annotate; hence we're left with solutions of the forms you've given.
    You /could/ do something involving decorators and property()s, but
    that would be more verbose, more unnecessarily complicated, and less
    Pythonic.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, Jul 16, 2010
    #4
    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. Brett Selleck

    Schema Metadata not a Metadata Schema

    Brett Selleck, Sep 4, 2003, in forum: XML
    Replies:
    1
    Views:
    397
    Andy Dingley
    Sep 4, 2003
  2. Timo Virkkala

    The pythonic approach

    Timo Virkkala, Sep 15, 2004, in forum: Python
    Replies:
    7
    Views:
    293
    Peter Hansen
    Sep 15, 2004
  3. rh0dium
    Replies:
    1
    Views:
    240
    George Sakkis
    Jan 18, 2006
  4. Carl J. Van Arsdall
    Replies:
    4
    Views:
    488
    Bruno Desthuilliers
    Feb 7, 2006
  5. hiwa
    Replies:
    2
    Views:
    311
Loading...

Share This Page