def testEntityIsCanonical(self): self.assertTrue( Dimension.is_entity_canonical( self.entities['canonical_type_appended'])) self.assertFalse( Dimension.is_entity_canonical( self.entities['noncanonical_type_appended'])) # This entity has had a type of `None` appended, thus it returns false. reporting_type_none = copy.copy(self.entities['reporting']) reporting_type_none.type = None self.assertFalse(Dimension.is_entity_canonical(reporting_type_none))
def retrieve_reporting_translations( *, proposed_entities: DeserializedFile, solution_entities: DeserializedFile) -> TranslationsDict: """ Retrieves proposed and solution translations for all matched reporting entities. Args: matches: List of `cloud_device_id`s which have corresponding proposed and solution entities proposed_entities: Dictionary of proposed entity names and `EntityInstance`s solution_entities: Dictionary of solution entity names and `EntityInstance`s Returns: Dictionary with `cloud_device_id`s as keys and values which are dictionaries containing lists of translations for the device, keyed under the file type """ translations = {} for solution_entity in solution_entities.values(): if solution_entity.cloud_device_id is None: continue # as this is not a reporting device if not Dimension.is_entity_canonical(solution_entity): continue # as noncanonical entities are skipped cloud_device_id = solution_entity.cloud_device_id def find_matches(cdid: str) -> List[Any]: """Find the matching proposal via cloud_device_id comparison. Args: cdid: cloud device id string. Returns: List of matching entities. """ matches = [ proposed_entity for proposed_entity in proposed_entities.values() if proposed_entity.cloud_device_id == cdid ] return matches proposed_entity = find_matches(cloud_device_id)[0] if find_matches( cloud_device_id) else {} def aggregate_translations(entity) -> List[Any]: """Isolate translation of an entity pairing.""" if getattr(entity, 'translation', None): return list(entity.translation.items()) return [] translations[cloud_device_id] = { f'{PROPOSED}': aggregate_translations(proposed_entity), f'{SOLUTION}': aggregate_translations(solution_entity) } return translations