def from_ngsi_dict(data: dict) -> Optional['SlfPoint']: """ Build an SlfPoint from an NGSI 'geo:point' value. :param data: a dictionary containing the required NGSI attributes. :return: the corresponding SlfPoint or ``None`` on a parse error. """ if maybe_value(data, 'type') == SlfPoint.ngsi_type(): return SlfPoint.from_ngsi_coords(maybe_value(data, 'value'))
def query_result_name_value_pairs(result: dict) -> dict: """ Extract the result set returned by the ``/v2/entities/{entityId}`` endpoint using the same format as that of ``entity_name_value_pairs``. """ eid = {'entityId': maybe_value(result, 'entityId')} attrs_array = maybe_value(result, 'attributes') attrs_array = attrs_array if attrs_array else [] attrs = {maybe_value(a, 'attrName'): maybe_value(a, 'values') for a in attrs_array} return merge_dicts(eid, attrs)
def time_index_priority_list(headers: dict, notification: dict) -> datetime: """ Returns the next possible time_index value using the strategy described in the function select_time_index_value. """ custom_attribute = maybe_value(headers, TIME_INDEX_HEADER_NAME) # Custom time index attribute yield to_datetime(_attribute(notification, custom_attribute)) # The most recent custom time index metadata yield latest_from_str_rep(_iter_metadata(notification, custom_attribute)) # TimeInstant attribute yield to_datetime(_attribute(notification, "TimeInstant")) # The most recent TimeInstant metadata yield latest_from_str_rep(_iter_metadata(notification, "TimeInstant")) # timestamp attribute yield to_datetime(_attribute(notification, "timestamp")) # The most recent timestamp metadata yield latest_from_str_rep(_iter_metadata(notification, "timestamp")) # dateModified attribute yield to_datetime(_attribute(notification, "dateModified")) # The most recent dateModified metadata yield latest_from_str_rep(_iter_metadata(notification, "dateModified"))
def _from_ngsi_dict(data: dict, slf_type) -> Optional['SlfGeometry']: if maybe_value(data, 'type') == slf_type.ngsi_type(): try: ps = [SlfPoint.from_ngsi_coords(p) for p in data['value']] if all(ps): return slf_type(ps) except (TypeError, KeyError, ValueError): return None
def is_ngsi_slf_attr(data: dict) -> bool: """ Does the given NGSI attribute have an SLF type? :param data: the NGSI attribute to test. :return: true for yes, false for no. """ return maybe_value(data, 'type') in [ SlfPoint.ngsi_type(), SlfLine.ngsi_type(), SlfPolygon.ngsi_type(), SlfBox.ngsi_type() ]
def entity_name_value_pairs(entity: dict) -> dict: """ Transform an NGSI entity ``e`` into the format:: { entityId: e[id] attr1: [ e[attr1][value] ] ... attrN: [ e[attrN][value] ] } """ eid = {'entityId': entity['id']} attr_names = {k for k in entity.keys()} - {'id', 'type'} attrs = {k: [maybe_value(entity, k, 'value')] for k in attr_names} return merge_dicts(eid, attrs)
def _meta_attribute(notification: dict, attr_name: str, meta_name: str) \ -> MaybeString: return maybe_value(notification, attr_name, 'metadata', meta_name, 'value')
def _attribute(notification: dict, attr_name: str) -> MaybeString: return maybe_value(notification, attr_name, 'value')
def _meta_date_modified_attribute(notification: dict, attr_name: str) \ -> MaybeString: return maybe_value(notification, attr_name, 'metadata', 'dateModified', 'value')
def _date_modified_attribute(notification: dict) -> MaybeString: return maybe_value(notification, 'dateModified', 'value')
def _meta_time_instant_attribute(notification: dict, attr_name: str) \ -> MaybeString: return maybe_value(notification, attr_name, 'metadata', 'TimeInstant', 'value')
def _time_instant_attribute(notification: dict) -> MaybeString: return maybe_value(notification, 'TimeInstant', 'value')
def _custom_time_index_attribute(headers: dict, notification: dict) \ -> MaybeString: attr_name = maybe_value(headers, TIME_INDEX_HEADER_NAME) if attr_name: return maybe_value(notification, attr_name, 'value') return None
def assert_inserted_entity(actual_row, original_entity): assert actual_row['a_number'] == \ maybe_value(original_entity, 'a_number', 'value') assert actual_row['an_attr'] == \ maybe_value(original_entity, 'an_attr', 'value') assert actual_row[ORIGINAL_ENTITY_COL] is None
def _json_ld_meta_attribute(notification: dict, attr_name: str, meta_name: str) -> MaybeString: return maybe_value(notification, attr_name, meta_name)