From: Sparky on 15 Jul 2010 15:37 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
From: John Krukoff on 15 Jul 2010 16:26 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 <jkrukoff(a)ltgc.com> Land Title Guarantee Company
From: Sparky on 15 Jul 2010 18:30 On Jul 15, 2:26 pm, John Krukoff <jkruk...(a)ltgc.com> 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 <jkruk...(a)ltgc.com> > Land Title Guarantee Company Sorry about that and thanks for pointing out my mistake there. Sam
From: Chris Rebert on 16 Jul 2010 05:22 On Thu, Jul 15, 2010 at 12:37 PM, Sparky <samnsparky(a)gmail.com> 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
From: Christian Heimes on 16 Jul 2010 05:57 > def to_JSON(self): > returnDict = {} > for member in filter(someMethod, inspect.getmembers(self)): > returnDict[member[0]] = member[1] > return json.dumps(returnDict) By the way you don't need filter here. The getmembers() function has a filter functions. It's called 'predicate'. Try inspect.getmembers(self, someMethod). http://docs.python.org/library/inspect.html#inspect.getmembers
|
Pages: 1 Prev: Code generator and visitor pattern Next: [OFF TOPIC] How to Learn Lambda Calculus: A Guide |