def from_json(cls, rec_json): """Create the solar access recipe from json. { "id": "solar_access", "type": "gridbased", "location": null, // a honeybee location - see below "hoys": [], // list of hours of the year "surfaces": [], // list of honeybee surfaces "analysis_grids": [] // list of analysis grids "sun_vectors": [] // list of sun vectors if location is not provided } """ raise NotImplementedError() hoys = rec_json["hoys"] if 'sun_vectors' not in rec_json or not rec_json['sun_vectors']: # create sun vectors from location inputs loc = Location.from_json(rec_json['location']) sp = Sunpath.from_location(loc) suns = (sp.calculate_sun_from_hoy(hoy) for hoy in hoys) sun_vectors = tuple(s.sun_vector for s in suns if s.is_during_day) else: sun_vectors = rec_json['sun_vectors'] analysis_grids = \ tuple(AnalysisGrid.from_json(ag) for ag in rec_json["analysis_grids"]) hb_objects = tuple( HBSurface.from_json(srf) for srf in rec_json["surfaces"]) return cls(sun_vectors, hoys, analysis_grids, 1, hb_objects)
def from_json(cls, rec_json): """Create the solar access recipe from json. { "id": "solar_access", "type": "gridbased", "location": null, // a honeybee location - see below "hoys": [], // list of hours of the year "surfaces": [], // list of honeybee surfaces "analysis_grids": [] // list of analysis grids "sun_vectors": [] // list of sun vectors if location is not provided } """ hoys = rec_json["hoys"] if 'sun_vectors' not in rec_json or not rec_json['sun_vectors']: # create sun vectors from location inputs loc = Location.from_json(rec_json['location']) sp = Sunpath.from_location(loc) suns = (sp.calculate_sun_from_hoy(hoy) for hoy in hoys) sun_vectors = tuple(s.sun_vector for s in suns if s.is_during_day) else: sun_vectors = rec_json['sun_vectors'] analysis_grids = \ tuple(AnalysisGrid.from_json(ag) for ag in rec_json["analysis_grids"]) hb_objects = tuple(HBSurface.from_json(srf) for srf in rec_json["surfaces"]) return cls(sun_vectors, hoys, analysis_grids, 1, hb_objects)
def from_json(cls, loc_json): """Create sky form json. { "location": {}, // honeybee (or actually ladybug location schema) "day": 1, // an integer between 1-31 "month": 1, // an integer between 1-12 "hour": 12.0, // a float number between 0-23 "north": 0, // degree for north if not Y axis "sky_type": 0 // A number between 0-5 -- 0: sunny sky } location schema { "city": "", "latitude": 0, "longitude": 0, "time_zone": 0, "elevation": 0 } """ data = loc_json location = Location.from_json(data["location"]) return cls(location, month=data["month"], day=data["day"], hour=data["hour"], north=data["north"], sky_type=data["sky_type"])
def from_json(cls, loc_json): """Create sky form json. { "location": {}, // honeybee (or actually ladybug location schema) "day": 1, // an integer between 1-31 "month": 1, // an integer between 1-12 "hour": 12.0, // a float number between 0-23 "north": 0, // degree for north if not Y axis "sky_type": 0 // A number between 0-5 -- 0: sunny sky } location schema { "city": "", "latitude": 0, "longitude": 0, "time_zone": 0, "elevation": 0 } """ data = loc_json location = Location.from_json(data["location"]) return cls(location, month=data["month"], day=data["day"], hour=data["hour"], north=data["north"], sky_type=data["sky_type"])
def test_json_methods(self): """Test JSON serialization functions""" city = 'Tehran' country = 'Iran' latitude = 36 longitude = 34 time_zone = 3.5 elevation = 54 loc = Location(city=city, country=country, latitude=latitude, longitude=longitude, time_zone=time_zone, elevation=elevation) assert loc.to_json() == { "city": city, "country": country, "latitude": latitude, "longitude": longitude, "time_zone": time_zone, "elevation": elevation } loc_from_json = Location.from_json(loc.to_json()) assert loc_from_json.city == city assert loc_from_json.latitude == latitude assert loc_from_json.longitude == longitude assert loc_from_json.time_zone == time_zone assert loc_from_json.elevation == elevation assert loc_from_json.country == country
def test_json_methods(self): """Test JSON serialization functions""" city = 'Tehran' country = 'Iran' latitude = 36 longitude = 34 time_zone = 3.5 elevation = 54 loc = Location(city=city, country=country, latitude=latitude, longitude=longitude, time_zone=time_zone, elevation=elevation) assert loc.to_json() == {"city": city, "country": country, "latitude": latitude, "longitude": longitude, "time_zone": time_zone, "elevation": elevation} loc_from_json = Location.from_json(loc.to_json()) assert loc_from_json.city == city assert loc_from_json.latitude == latitude assert loc_from_json.longitude == longitude assert loc_from_json.time_zone == time_zone assert loc_from_json.elevation == elevation assert loc_from_json.country == country
def from_json(cls, inp): """Create a SunMatrix from a dictionary.""" keys = ('location', 'solar_values', 'sun_up_hours', 'hoys') for key in keys: assert key in inp, '%s is missing from input dictionary' % key location = Location.from_json(inp['location']) solar_values = inp['solar_values'] sun_up_hours = inp['sun_up_hours'] hoys = inp['hoys'] return cls(location, solar_values, sun_up_hours, hoys)