Best Pythonic Approach to Annotation/Metadata?

Sparky samnsparky at gmail.com
Thu Jul 15 15:37:26 EDT 2010


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



More information about the Python-list mailing list