Last active
August 29, 2015 14:01
-
-
Save ChadSki/550668f632c215168e36 to your computer and use it in GitHub Desktop.
Nested Dictionary Comprehensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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