def __iter__(self): """ Implement iterator functionality This iterator functionality returns tuples such that the data contained in the object can be converted into a dictionary. All `PhysicalProperty` attributes appear once and only once during the iteration. The order of these atttibutes are not guaranteed. The values corresponding to an attribute are returned as floats; their unit is defined by the default unit of the corresponding `PhysicalProperty`. Additionally, this iteration returns the following attribute: * `__class__`: The object's class returned by `type(self)`. """ # Crappy implmentation -- should use iterators/iterables. # Construct a list of tuples to return. physical_prop_names = find_PhysicalProperty(self) physical_prop_vals = [ getattr(self, prop).value for prop in physical_prop_names ] attribs = zip(physical_prop_names, physical_prop_vals) ext_attribs = [( "__class__", type(self), )] return itertools.chain(ext_attribs, attribs)
def _to_dict(self): """ Return a dictionary representation of the current object. """ physical_prop_names = find_PhysicalProperty(self) physical_prop_vals = [getattr(self, prop) for prop in physical_prop_names] return dict(zip(physical_prop_names, physical_prop_vals))
def test_iteration_returns_all_physicalproperties(self): """ All PhysicalProperty attributes should appear once and only once during the iteration """ # Use sets to compare results physical_properties = set(find_PhysicalProperty(self.el)) iteration_keys = set([itm[0] for itm in iter(self.el)]) self.assertTrue(physical_properties.issubset(iteration_keys))
def __iter__(self): """ Implement iterator functionality This iterator functionality returns tuples such that the data contained in the object can be converted into a dictionary. All `PhysicalProperty` attributes appear once and only once during the iteration. The order of these atttibutes are not guaranteed. The values corresponding to an attribute are returned as floats; their unit is defined by the default unit of the corresponding `PhysicalProperty`. Additionally, this iteration returns the following attribute: * `__class__`: The object's class returned by `type(self)`. """ # Crappy implmentation -- should use iterators/iterables. # Construct a list of tuples to return. physical_prop_names = find_PhysicalProperty(self) physical_prop_vals = [getattr(self, prop).value for prop in physical_prop_names] attribs = zip(physical_prop_names, physical_prop_vals) ext_attribs = [("__class__", type(self),)] return itertools.chain(ext_attribs, attribs)
def __init__(self, params): for attr in find_PhysicalProperty(self): setattr(self, attr, params[attr])