Skip to content

Instantly share code, notes, and snippets.

@ChadSki
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChadSki/550668f632c215168e36 to your computer and use it in GitHub Desktop.
Save ChadSki/550668f632c215168e36 to your computer and use it in GitHub Desktop.
Nested Dictionary Comprehensions
def class_from_xml(struct_layout):
"""Define a new class based on the given struct layout defined in xml.
"""
# { field_name => ObservableField_constructor_params }
field_params = {
field.attrib['name']: {
'field_type': field.tag,
'offset': int(field.attrib.get('offset'), 0),
'length': int(field.attrib.get('length', '0'), 0),
'reverse': field.attrib.get('reverse', 'false') == 'true',
'maxlength': int(field.attrib.get('maxlength', '0'), 0),
'options': { opt.attrib['value']: opt.attrib['name'] for opt in field.iter('option') },
'reflexive_class': class_from_xml(field) if field.tag == 'reflexive' else None,
}
for field in struct_layout
}
class Foo(object):
def __init__(self, extra_param):
for name in field_params:
setattr(self, name, make_observable_field(extra_param, field_params[name]))
return Foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment