def test_rows(self, urlopen): """ Sending a valid query returns the correct response. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Create the SPARQL query query_string = (''' SELECT ?name ?dcid WHERE { ?a typeOf Place . ?a name ?name . ?a dcid ("geoId/06" "geoId/21" "geoId/24") . ?a dcid ?dcid } ''') selector = lambda row: row['?name'] != 'California' # Issue the query results = dc.query(query_string) selected_results = dc.query(query_string, select=selector) # Execute the query and iterate through the results. for idx, row in enumerate(results): if idx == 0: self.assertDictEqual(row, { '?name': 'California', '?dcid': 'geoId/06' }) if idx == 1: self.assertDictEqual(row, { '?name': 'Kentucky', '?dcid': 'geoId/21' }) if idx == 2: self.assertDictEqual(row, { '?name': 'Maryland', '?dcid': 'geoId/24' }) # Verify that the select function works. for idx, row in enumerate(selected_results): if idx == 0: self.assertDictEqual(row, { '?name': 'Kentucky', '?dcid': 'geoId/21' }) if idx == 1: self.assertDictEqual(row, { '?name': 'Maryland', '?dcid': 'geoId/24' })
def main(): response = make_response({'error' : f'Please specify url query like this: ...url...?desired_method=args'}) print("Got request args:") print(request.args) # The requestor would like to perform a SPARQL query if (request.args and 'sparql' in request.args) : sparql_query = request.args.get('sparql') print("Got query:") print(sparql_query) response = make_response(dc.query(sparql_query)) # The requestor would like to request all triples associated with given dcids elif (request.args and 'triples' in request.args) : dcids = json.loads(request.args.get('triples')) print("Got dcids: ") print(dcids) #limit = dc.utils._MAX_LIMIT #if ('limit' in request.args) : # limit = request.args.get('limit') #print(f"limiting to: {limit}") response = make_response(dc.get_triples(dcids)) # test endpoint for debugging elif request.args and 'test' in request.args : response = make_response({'message' : f'Test acknowledged and received data: {request.args.get("test")}' }) else: pass # return_msg already set return response
def test_query_w_api_key(self, urlopen): """ Handles row-less response. """ # Set the API key dc.set_api_key('make_sure_I_am_replaced') dc.set_api_key(_TEST_API_KEY) # Issue a dummy SPARQL query that tells the mock to expect a key self.assertEqual(dc.query(_SPARQL_W_KEY), [])
def test_no_rows(self, urlopen): """ Handles row-less response. """ # Create a SPARQL query query_string = (''' SELECT ?name ?dcid WHERE { ?a typeOf Place . ?a name ?name . ?a dcid ("geoId/DNE") . ?a dcid ?dcid } ''') # Issue the query self.assertEqual(dc.query(query_string), [])
def main(): # Create a SPARQL query querying for the name of some states query = (''' SELECT ?name ?dcid WHERE { ?a typeOf Place . ?a name ?name . ?a dcid ("geoId/06" "geoId/21" "geoId/24") . ?a dcid ?dcid } ''') print('> Issuing query.\n{}'.format(query)) # Iterate through all the rows in the results. print('> Printing results.\n') for row in dc.query(query_string=query): print(' {}'.format(row))
def write_place_url(place_type): logging.info(place_type) sparql = ''' SELECT ?dcid WHERE {{ ?a typeOf {} . ?a dcid ?dcid }} Order By ASC(?dcid) '''.format(place_type) try: data = dc.query(sparql) except Exception: logging.exception('Got an error while query %s', place_type) return index = 0 for places in chunks(data, LINE_LIMIT): file_name = '{}{}.{}.txt'.format(SAVE_PATH, place_type, index) with open(file_name, 'w') as f: logging.info('writing to file: %s', file_name) for p in places: f.write(SITE_PREFIX + p['?dcid'] + '\n') index += 1 time.sleep(10)
st.write(duplicated_region_codes) ################################################################################ ##### Query datacommons to get dcids ##### ################################################################################ query_str = ''' SELECT ?name ?dcid ?isoCode WHERE { ?a typeOf Place . ?a name ?name . ?a isoCode ?isoCode . ?a dcid ?dcid } ''' results = dc.query(query_str) iso_level_2 = list( filter(lambda elem: elem['?dcid'].startswith('iso'), results)) iso_level_2_df = pd.DataFrame(iso_level_2) iso_level_2_df = iso_level_2_df.rename( columns={ '?name': 'datacommons_name', '?dcid': 'datacommons_id', '?isoCode': 'region_code' }) # Filter out dcids that aren't ISO-3166-2 codes # (These appear to be freebase ids from wikidata) iso_level_2_df['real_iso'] = iso_level_2_df.apply( lambda x: '/m/' not in x['region_code'], axis=1)
################################################################################ ##### Query datacommons to get dcids ##### ################################################################################ datacommons_query_str = ''' SELECT ?name ?dcid ?geoId ?wikidataId WHERE { ?a typeOf County . ?a name ?name . ?a geoId ?geoId . ?a wikidataId ?wikidataId . ?a dcid ?dcid } ''' dc_results = dc.query(datacommons_query_str) dc_df = pd.DataFrame(dc_results) dc_df = dc_df.rename( columns={ '?name': 'datacommons_name', '?dcid': 'datacommons_id', '?wikidataId': 'wikidata_id', '?geoId': 'fips_code' }) dc_df = dc_df.drop_duplicates() # New York County has two wikidata ids in datacommons: # Q500416 (correct) # Q11299 (incorrect - this is Manhattan not the fips county) dc_df = dc_df[dc_df['wikidata_id'] != 'Q11299']
import datacommons as dc print("Running query...") query_str = ''' SELECT ?name ?dcid WHERE { ?a typeOf Place . ?a name ?name . ?a dcid ("geoId/06" "geoId/21" "geoId/24") . ?a dcid ?dcid } ''' result = dc.query(query_str) print("Done:") for r in result: print(r)
def test_query_no_api_key(self, urlopen): del os.environ[utils._ENV_VAR_API_KEY] # Issue a dummy SPARQL query that tells the mock to not expect a key self.assertEqual(dc.query(_SPARQL_NO_KEY), [])