def sanity_check(sit_1, sit_2): '''Performs a sanity check before two Situations can be compared''' if sit_1.area != sit_2.area: raise EnergysystemParseError('Areas do not match') if not sit_1.context: raise EnergysystemParseError('Context scenario is needed to measure relative change') if sit_1.context['end_year'] != sit_2.year: raise EnergysystemParseError('Context scenario does not match compared situation')
def from_string(cls, esdl_string): ''' Create a new EnergySystemHandler based on an EnergySystem esdl_string (using UTF-8 encoding) ''' try: handler = cls() handler.load_from_string(esdl_string) return handler except Exception as exception: raise EnergysystemParseError('ESDL could not be parsed') from exception
def as_situation(self): ''' Calculates the inputs and wraps them in a Situation''' self.calculate() try: year = self.energy_system.es.instance[0].date.date.year except AttributeError as exc: raise EnergysystemParseError( 'Date instance was missing in the ESDL') from exc return Situation(self.inputs, self.area, year)
def set_asset_generator(self): """ Get all instances of asset type and set the generator. """ try: self.asset_generator = self.energy_system.get_all_instances_of_type_by_name( self.props['asset']) except AttributeError as att: raise EnergysystemParseError( f'We currently do not support the asset {str(att).split()[-1]}' ) from att
def __ensure_valid_props(self): '''Raises an ESPError if power and fullLoadHours are not included in the props''' if all(a in self.props['attr_set'] for a in ['power', 'fullLoadHours']): return print(self.props['asset']) raise EnergysystemParseError( f'Props do not contain "power" or "fullLoadHours" in attribute set: {self.props["attr_set"]}' )
def from_string(cls, esdl_string): ''' Create a new EnergySystemHandler based on an EnergySystem esdl_string (using UTF-8 encoding) ''' try: handler = EnergySystemHandler() handler.resource = handler.rset.create_resource( StringURI('loadfromstring', esdl_string) ) handler.resource.load() handler.es = handler.resource.contents[0] return handler except Exception as exception: raise EnergysystemParseError('ESDL could not be parsed') from exception
def set_asset_generator(self): """ Set the generator to assets with type and carrier. """ try: self.asset_generator = self.energy_system.get_all_instances_of_type_and_carrier( self.props['asset'], self.props['carrier'] ) except AttributeError as att: raise EnergysystemParseError( f'We currently do not support the asset {str(att).split()[-1]}' ) from att
def set_asset_generator(self): """ Get all instances with given sector and carrier and set the generator. """ try: self.asset_generator = self.energy_system.get_all_instances_of_type_carrier_and_sector( self.props['asset'], self.props['sector'], self.props['carrier'] ) except AttributeError as att: print(att) raise EnergysystemParseError( f'We currently do not support the asset {str(att).split()[-1]}' ) from att
def __init__(self, energy_system, props, *args, **kwargs): ''' Assumes props to be one item with 'power' ''' if not 'sector' in props: raise EnergysystemParseError(f'No sector was given in the config for {props["input"]}') super().__init__(energy_system, props, *args, **kwargs)
def test_with_humanized_message_with_existing_key(): errors = [list(etm_error_messages.keys())[0]] err = EnergysystemParseError.with_humanized_message(errors, 404) assert err.message == etm_error_messages[errors[0]] assert err.status_code == 404
def test_with_humanized_message_with_non_existing_key(): err = EnergysystemParseError.with_humanized_message(['hi', 'you']) assert err.message == 'hi' assert err.status_code == 422