Skip to content

Instantly share code, notes, and snippets.

@eevans
Created September 2, 2010 21:14
Show Gist options
  • Save eevans/562964 to your computer and use it in GitHub Desktop.
Save eevans/562964 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from simplejson import loads
schema = loads(open('cassandra.avpr').read())
print """
class MissingFieldException(Exception):
pass
"""
for record in schema['types']:
if not record['type'] == 'record':
continue
print "class %s(dict):" % record['name']
print " def __init__(self, **kwargs):"
for field in record['fields']:
if not isinstance(field['type'], list):
print " if not kwargs.has_key('%s'):" % field['name']
print " raise MissingFieldException('%s')" % field['name']
print " self['%s'] = kwargs.get('%s', None)" % (field['name'], field['name'])
print
print " def __setattr__(self, name, value):"
print " self[name] = value"
print
print " def __getattr__(self, name):"
print " return self[name]"
print
# vi:ai sw=4 ts=4 et
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment