def test_bad_dcids(self, post_mock): """ Calling get_triples with dcids that do not exist returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Call get_triples where one dcid does not exist triples_1 = dc.get_triples(['geoId/06085', 'dc/MadDcid']) self.assertDictEqual( triples_1, { 'geoId/06085': [ ('geoId/06085', 'name', 'Santa Clara County'), ('geoId/0649670', 'containedInPlace', 'geoId/06085'), ('geoId/06085', 'containedInPlace', 'geoId/06'), ], 'dc/MadDcid': [] }) # Call get_triples where both dcids do not exist triples_1 = dc.get_triples(['dc/MadDcid', 'dc/MadderDcid']) self.assertDictEqual(triples_1, { 'dc/MadDcid': [], 'dc/MadderDcid': [] })
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 main(): # Set the dcid to be that of Santa Clara County. dcids = ['geoId/06085', 'dc/p/zsb968m3v1f97'] # Print all incoming and outgoing properties from Santa Clara County. print('Property Labels for Santa Clara County') in_labels = dc.get_property_labels(dcids) out_labels = dc.get_property_labels(dcids, out=False) print('> Printing properties for {}'.format(dcids)) print('> Incoming properties: {}'.format(in_labels)) print('> Outgoing properties: {}'.format(out_labels)) # Print all property values for "containedInPlace" for Santa Clara County. print('Property Values for "containedInPlace" of Santa Clara County') prop_vals = dc.get_property_values( dcids, 'containedInPlace', out=False, value_type='City') print('> Cities contained in {}'.format(dcids)) for dcid in dcids: for city_dcid in prop_vals[dcid]: print(' - {}'.format(city_dcid)) # Print the first 10 triples associated with Santa Clara County print('Triples for Santa Clara County') triples = dc.get_triples(dcids) for dcid in dcids: print('> Triples for {}'.format(dcid)) for s, p, o in triples[dcid][:5]: print(' - ("{}", {}, "{}")'.format(s, p, o))
def test_no_dcids(self, post_mock): """ Calling get_triples with no dcids returns empty results. """ # Set the API key dc.set_api_key('TEST-API-KEY') # Call get_triples with no dcids triples_1 = dc.get_triples([]) self.assertDictEqual(triples_1, {})
def test_multiple_dcids(self, urlopen_mock): """ Calling get_triples with proper dcids returns valid results. """ # Call get_triples triples = dc.get_triples(['geoId/06085', 'geoId/24031']) self.assertDictEqual(triples, { 'geoId/06085': [ ('geoId/06085', 'name', 'Santa Clara County'), ('geoId/0649670', 'containedInPlace', 'geoId/06085'), ('geoId/06085', 'containedInPlace', 'geoId/06'), ], 'geoId/24031': [ ('geoId/24031', 'name', 'Montgomery County'), ('geoId/2467675', 'containedInPlace', 'geoId/24031'), ('geoId/24031', 'containedInPlace', 'geoId/24'), ] })
def main(): # Set the dcid to be that of Santa Clara County. dcids = ['geoId/06085'] # Print all incoming and outgoing properties from Santa Clara County. utils._print_header('Property Labels for Santa Clara County') in_labels = dc.get_property_labels(dcids) out_labels = dc.get_property_labels(dcids, out=False) print('> Printing properties for {}'.format(dcids)) print('> Incoming properties: {}'.format(in_labels)) print('> Outgoing properties: {}'.format(out_labels)) # Print all property values for "containedInPlace" for Santa Clara County. utils._print_header( 'Property Values for "containedInPlace" of Santa Clara County') prop_vals = dc.get_property_values(dcids, 'containedInPlace', out=False, value_type='City') print('> Cities contained in {}'.format(dcids)) for dcid in dcids: for city_dcid in prop_vals[dcid]: print(' - {}'.format(city_dcid)) # Print the first 10 triples associated with Santa Clara County utils._print_header('Triples for Santa Clara County') triples = dc.get_triples(dcids) for dcid in dcids: print('> Triples for {}'.format(dcid)) for s, p, o in triples[dcid][:5]: print(' - ("{}", {}, "{}")'.format(s, p, o)) # get_property_values can be easily used to populate Pandas DataFrames. First # create a DataFrame with some data. utils._print_header('Initialize the DataFrame') pd_frame = pd.DataFrame({'county': ['geoId/06085', 'geoId/24031']}) print(pd_frame) # Get the names for the given counties. utils._print_header('Get County Names') pd_frame['county_name'] = dc.get_property_values(pd_frame['county'], 'name') print(pd_frame) # Get the cities contained in these counties. utils._print_header('Get Contained Cities') pd_frame['city'] = dc.get_property_values(pd_frame['county'], 'containedInPlace', out=False, value_type='City') print(pd_frame) # To expand on a column with get_property_values, the data frame has to be # flattened first. Clients can use flatten_frame to do this. utils._print_header('Flatten the Frame') pd_frame = dc.flatten_frame(pd_frame) print(pd_frame) # Get the names for each city. utils._print_header('Get City Names') pd_frame['city_name'] = dc.get_property_values(pd_frame['city'], 'name') print(pd_frame) # Format the final frame. utils._print_header('The Final Frame') pd_frame = dc.flatten_frame(pd_frame) print(pd_frame)
def test_no_dcids(self, urlopen_mock): """ Calling get_triples with no dcids returns empty results. """ # Call get_triples with no dcids triples_1 = dc.get_triples([]) self.assertDictEqual(triples_1, {})