def test_to_from_dict(): """Test the to/from dict of ContextShade objects.""" tree_canopy_geo1 = Face3D.from_regular_polygon(6, 6, Plane(o=Point3D(5, -10, 6))) tree_canopy_geo2 = Face3D.from_regular_polygon(6, 2, Plane(o=Point3D(-5, -10, 3))) tree_canopy = ContextShade('Tree_Canopy', [tree_canopy_geo1, tree_canopy_geo2]) context_dict = tree_canopy.to_dict() new_context = ContextShade.from_dict(context_dict) assert isinstance(new_context, ContextShade) assert new_context.to_dict() == context_dict
def test_to_from_dict(): """Test the Building to_dict and from_dict methods.""" context = custom_context() context_dict = context.to_dict() assert 'uwg' in context_dict['properties'] assert 'is_vegetation' in context_dict['properties']['uwg'] new_context = ContextShade.from_dict(context_dict) assert new_context.properties.uwg.is_vegetation == \ context.properties.uwg.is_vegetation
def dict_to_object(dragonfly_dict, raise_exception=True): """Re-serialize a dictionary of almost any object within dragonfly. This includes any Model, Building, Story, Room2D, WindowParameter, ShadingParameter, and boundary conditions. Args: dragonfly_dict: A dictionary of any Dragonfly object. Note that this should be a non-abridged dictionary to be valid. raise_exception: Boolean to note whether an excpetion should be raised if the object is not identified as a part of dragonfly. Default: True. Returns: A Python object derived from the input dragonfly_dict. """ try: # get the type key from the dictionary obj_type = dragonfly_dict['type'] except KeyError: raise ValueError('Dragonfly dictionary lacks required "type" key.') if obj_type == 'Model': return Model.from_dict(dragonfly_dict) elif obj_type == 'Building': return Building.from_dict(dragonfly_dict) elif obj_type == 'Story': return Story.from_dict(dragonfly_dict) elif obj_type == 'Room2D': return Room2D.from_dict(dragonfly_dict) elif obj_type == 'ContextShade': return ContextShade.from_dict(dragonfly_dict) elif hasattr(dfw, obj_type): win_class = getattr(dfw, obj_type) return win_class.from_dict(dragonfly_dict) elif hasattr(dfs, obj_type): shd_class = getattr(dfs, obj_type) return shd_class.from_dict(dragonfly_dict) elif hasattr(hbc, obj_type): bc_class = getattr(hbc, obj_type) return bc_class.from_dict(dragonfly_dict) elif raise_exception: raise ValueError( '{} is not a recognized dragonfly object'.format(obj_type))
def test_from_dict(): """Test the Story from_dict method with energy properties.""" tree_canopy_geo1 = Face3D.from_regular_polygon(6, 6, Plane(o=Point3D(5, -10, 6))) tree_canopy_geo2 = Face3D.from_regular_polygon( 6, 2, Plane(o=Point3D(-5, -10, 3))) tree_canopy = ContextShade('TreeCanopy', [tree_canopy_geo1, tree_canopy_geo2]) bright_leaves = ShadeConstruction('Bright Light Leaves', 0.5, 0.5, True) tree_trans = ScheduleRuleset.from_constant_value('Tree Transmittance', 0.5, schedule_types.fractional) tree_canopy.properties.energy.construction = bright_leaves tree_canopy.properties.energy.transmittance_schedule = tree_trans sd = tree_canopy.to_dict() new_shd = ContextShade.from_dict(sd) assert new_shd.properties.energy.construction == bright_leaves assert new_shd.properties.energy.transmittance_schedule == tree_trans assert new_shd.to_dict() == sd