class WikibaseItem: def __init__(self, entity_id, item=None): # Import Entity locally to prevent circular imports from wikidata.entity import Entity self.entity_id = entity_id if item: self.item = item else: self.item = Entity(entity_id) @staticmethod def parse(data_value_dict): entity_id = "Q%s" % data_value_dict["value"]["numeric-id"] return WikibaseItem(entity_id) def __str__(self): return str(self.item.terms()) def compare_to(self, counterpart_values): terms = self.item.terms() if terms: if len(self.item.terms().intersection(counterpart_values)) > 0: return True else: return False else: return True
def test_entity_load_redirected_entity(fx_client: Client, fx_redirected_entity: Entity): canonical_id = EntityId('Q3571994') alternate_id = EntityId('Q16231742') assert fx_redirected_entity.id == alternate_id fx_redirected_entity.load() assert fx_redirected_entity.id == canonical_id
def __init__(self, entity_id, item=None): # Import Entity locally to prevent circular imports from wikidata.entity import Entity self.entity_id = entity_id if item: self.item = item else: self.item = Entity(entity_id)
def test_entity_mapping(fx_client: Client, fx_loaded_entity: Entity): occupation = fx_client.get(EntityId('P106')) musicbrainz_id = fx_client.get(EntityId('P434')) singer = fx_client.get(EntityId('Q177220')) instagram_username = fx_client.get(EntityId('P2003')) assert len(fx_loaded_entity) == 13 expected_ids = { 'P19', 'P21', 'P27', 'P31', 'P106', 'P136', 'P345', 'P434', 'P569', 'P646', 'P1303', 'P1728', 'P1953' } expected = {fx_client.get(EntityId(pid)) for pid in expected_ids} assert set(fx_loaded_entity) == expected assert musicbrainz_id in fx_loaded_entity assert (fx_loaded_entity[musicbrainz_id] == fx_loaded_entity.get(musicbrainz_id) == fx_loaded_entity.get(musicbrainz_id, ...) == '3eb63662-a02c-4d2d-9544-845cd92fd4e7') assert (fx_loaded_entity.getlist(musicbrainz_id) == ['3eb63662-a02c-4d2d-9544-845cd92fd4e7']) assert occupation in fx_loaded_entity assert (fx_loaded_entity[occupation] == fx_loaded_entity.get(occupation) == fx_loaded_entity.get(occupation, ...) == singer) assert (fx_loaded_entity.getlist(occupation) == [singer, fx_client.get(EntityId('Q753110'))]) assert instagram_username not in fx_loaded_entity with raises(KeyError): fx_loaded_entity[instagram_username] assert fx_loaded_entity.get(instagram_username) is None assert fx_loaded_entity.get(instagram_username, ...) is ... assert fx_loaded_entity.getlist(instagram_username) == [] assert (dict(fx_loaded_entity.iterlists()) == dict(fx_loaded_entity.lists()) == {p: fx_loaded_entity.getlist(p) for p in expected}) def sorted_list(v: Iterable) -> list: return list(sorted(v, key=str)) assert (sorted_list(fx_loaded_entity.iterlistvalues()) == sorted_list(fx_loaded_entity.listvalues()) == sorted_list(fx_loaded_entity.getlist(p) for p in expected))
def test_entity_load(fx_unloaded_entity: Entity): fx_unloaded_entity.load() with (ENTITY_FIXTURES_PATH / 'Q1299.json').open('r') as f: assert fx_unloaded_entity.data == json.load(f)['entities']['Q1299']
def parse(entities_dict, language): entities = {} for entity_dict in entities_dict.values(): entity = Entity.parse(entity_dict, language) entities[entity.entity_id] = entity return entities