def test_wd_quantity(self): dt = wdi_core.WDQuantity(value='34', prop_nr='P43') dt_json = dt.get_json_representation() if not dt_json['mainsnak']['datatype'] == 'quantity': raise value = dt_json['mainsnak']['datavalue'] if not value['value']['amount'] == '+34': raise if not value['value']['unit'] == '1': raise dt2 = wdi_core.WDQuantity(value='34', prop_nr='P43', upper_bound='35', lower_bound='33') value = dt2.get_json_representation()['mainsnak']['datavalue'] if not value['value']['amount'] == '+34': raise if not value['value']['unit'] == '1': raise if not value['value']['upperBound'] == '+35': raise if not value['value']['lowerBound'] == '+33': raise
def update_all_settlements(config: Configuration): login = login_with_credentials(config.credentials_path) ref_time, ref_url, path = find_latest_processed_file_info( config.matched_tables_path, config.data) ref = wdi_core.WDUrl(prop_nr="P854", value=ref_url, is_reference=True) # publisher = wdi_core.WDItemID(value=login.consumer_key, prop_nr="P123", is_reference=True) qualifiers = create_qualifiers(ref_time) error_logs = [] data = pd.DataFrame(pd.read_csv(path)) for _, row in data.iterrows(): settlement_qid: str = row['settlement'] population: str = row['permanent_population'] prop = wdi_core.WDQuantity(prop_nr='P1082', value=population, qualifiers=qualifiers, references=[[ref]]) try: update_item(login, settlement_qid, [prop]) except BaseException: error_logs.append(settlement_qid) print("An error occurred for item : " + settlement_qid) if len(error_logs) > 0: print("Summarizing failures for specific IDs") for error in error_logs: print("Error for : " + error)
def create_wdquantity(content, upper_bound=None, lower_bound=None, **kwargs) -> wdi_core.WDQuantity: """ Create a WDQantity object from a number value and its bounds. """ return wdi_core.WDQuantity(content, upper_bound=upper_bound, lower_bound=lower_bound, **kwargs)
def test_new_item_creation(self): data = [ wdi_core.WDString(value='test', prop_nr='P1'), wdi_core.WDString(value='test1', prop_nr='P2'), wdi_core.WDMath("xxx", prop_nr="P3"), wdi_core.WDExternalID("xxx", prop_nr="P4"), wdi_core.WDItemID("Q123", prop_nr="P5"), wdi_core.WDTime('+%Y-%m-%dT%H:%M:%SZ', "P6"), wdi_core.WDUrl("http://www.google.com", "P7"), wdi_core.WDMonolingualText("xxx", prop_nr="P8"), wdi_core.WDQuantity(5, prop_nr="P9"), wdi_core.WDQuantity(5, upper_bound=9, lower_bound=2, prop_nr="P10"), wdi_core.WDCommonsMedia("xxx", prop_nr="P11"), wdi_core.WDGlobeCoordinate(1.2345, 1.2345, 12, prop_nr="P12"), wdi_core.WDGeoShape("xxx", prop_nr="P13"), wdi_core.WDProperty("P123", "P14") ] core_props = set(["P{}".format(x) for x in range(20)]) for d in data: item = wdi_core.WDItemEngine(item_name='dae', domain="szadf", data=[d], core_props=core_props) assert item.get_wd_json_representation() item = wdi_core.WDItemEngine(item_name='dae', domain="szadf", data=[d], core_props=set()) assert item.get_wd_json_representation() item = wdi_core.WDItemEngine(item_name='dae', domain="szadf", data=data, core_props=core_props) assert item.get_wd_json_representation() item = wdi_core.WDItemEngine(item_name='dae', domain="szadf", data=data, core_props=set()) assert item.get_wd_json_representation()
def create_qualifier(incidence): q = [] if incidence: q.append( wdi_core.WDQuantity(incidence, PROPS['incidence'], is_qualifier=True, unit="http://www.wikidata.org/entity/" + ITEMS['percentage'])) pass return q
def update_item(login, settlement_qid, population): ref = wdi_core.WDUrl(prop_nr="P854", value="https://www.grao.bg/tna/t41nm-15-06-2020_2.txt", is_reference=True) determination_method = wdi_core.WDItemID(value='Q90878157', prop_nr="P459", is_qualifier=True) point_in_time = wdi_core.WDTime(time='+2020-06-15T00:00:00Z', prop_nr='P585', is_qualifier=True) # publisher = wdi_core.WDItemID(value=login.consumer_key, prop_nr="P123", is_reference=True) qualifiers = [] qualifiers.append(point_in_time) qualifiers.append(determination_method) data = [] prop = wdi_core.WDQuantity(prop_nr='P1082', value=population, qualifiers=qualifiers, references=[[ref]]) data.append(prop) item = wdi_core.WDItemEngine(wd_item_id=settlement_qid, data=data) item.write(login, False) time.sleep(15)
def create_qty_qualifier(self, wdresult, value, quantity): """ Function to create the quantity qualifier to respresent stoichiometry :param wdresult: results from a wikidata query for a list of items :param value: list of the items :param quantity: list of the quantity for each item (indexed as in value) :return: a wikidata qualifier statement for quantity """ this_item = '\"' + wdresult[self.property]['value'] + '\"' if this_item in value: this_index = value.index(this_item) if this_index < len(quantity): qualifier = wdi_core.WDQuantity(int(quantity[this_index]), prop_nr='P1114', is_qualifier=True) return qualifier return []
def test_quantity_bounds(): result = datatype2wdiobject[f"{XSD_BASE}positiveInteger"](50, prop_nr=-1) expected = wdi_core.WDQuantity(value=50, lower_bound=1, prop_nr=-1) assert result == expected
def test_quantity(): result = datatype2wdiobject[f"{XSD_BASE}integer"](-125, prop_nr=-1) expected = wdi_core.WDQuantity(value=-125, prop_nr=-1) assert result == expected