def test_all_value_types(self): key = Config.CLIENT.key("TestPanObject", 1234) entity = datastore.Entity(key=key) entity["timestamp"] = datetime.datetime(2014, 9, 9, tzinfo=UTC) key_stored = Config.CLIENT.key("SavedKey", "right-here") entity["key"] = key_stored entity["truthy"] = True entity["float"] = 2.718281828 entity["int"] = 3735928559 entity["words"] = u"foo" entity["blob"] = b"seekretz" entity_stored = datastore.Entity(key=key_stored) entity_stored["hi"] = "bye" entity["nested"] = entity_stored entity["items"] = [1, 2, 3] entity["geo"] = GeoPoint(1.0, 2.0) entity["nothing_here"] = None # Store the entity. self.case_entities_to_delete.append(entity) Config.CLIENT.put(entity) # Check the original and retrieved are the the same. retrieved_entity = Config.CLIENT.get(entity.key) self.assertEqual(retrieved_entity, entity)
def test_all_value_types(self): key = Config.CLIENT.key('TestPanObject', 1234) entity = datastore.Entity(key=key) entity['timestamp'] = datetime.datetime(2014, 9, 9, tzinfo=UTC) key_stored = Config.CLIENT.key('SavedKey', 'right-here') entity['key'] = key_stored entity['truthy'] = True entity['float'] = 2.718281828 entity['int'] = 3735928559 entity['words'] = u'foo' entity['blob'] = b'seekretz' entity_stored = datastore.Entity(key=key_stored) entity_stored['hi'] = 'bye' entity['nested'] = entity_stored entity['items'] = [1, 2, 3] entity['geo'] = GeoPoint(1.0, 2.0) entity['nothing_here'] = None # Store the entity. self.case_entities_to_delete.append(entity) Config.CLIENT.put(entity) # Check the original and retrieved are the the same. retrieved_entity = Config.CLIENT.get(entity.key) self.assertEqual(retrieved_entity, entity)
def convert_coord_list_to_geopoints(coordinate_list): '''Convert list of coordinate lists like those returned by Planet API for feature geometries to GeoPoints objects usable by DataStore ''' geopoints = [] for lon, lat in coordinate_list: geopoints.append(GeoPoint(lat, lon)) return geopoints
def test_geo_point(self): from google.type import latlng_pb2 from google.cloud.datastore.helpers import GeoPoint pb = self._makePB() lat = 9.11 lng = 3.337 geo_pt = GeoPoint(latitude=lat, longitude=lng) geo_pt_pb = latlng_pb2.LatLng(latitude=lat, longitude=lng) self._call_fut(pb, geo_pt) self.assertEqual(pb.geo_point_value, geo_pt_pb)
def test_geo_point(self): from google.type import latlng_pb2 from google.cloud.datastore.helpers import GeoPoint lat = 42.42 lng = 99.0007 geo_pt = GeoPoint(latitude=lat, longitude=lng) geo_pt_pb = latlng_pb2.LatLng(latitude=lat, longitude=lng) name, value = self._call_fut(geo_pt) self.assertEqual(name, 'geo_point_value') self.assertEqual(value, geo_pt_pb)
def request_scene_data(session, scenes, item_type): '''Request the scene data for scenes dictionary and merge results ''' scene_data = [] total = len(scenes) for i, scene_id in enumerate(scenes): print('Downloading scene_id: {}; {} of {}... '.format( scene_id, i + 1, total), end='', flush=True) url = 'https://api.planet.com/data/v1/item-types/{}/items/{}'.format( item_type, scene_id) # TODO: implement unsuccessful request handling # response = session.get(url).json() response = session.get(url) rjson = response.json() # unsuccessful request if response.status_code != 200: item = { 'id': scene_id, 'status_msg': '{}: {}'.format(response.status_code, rjson['general'][0]['message']), 'labels': scenes[scene_id] } else: item = rjson item['status_msg'] = str(response.status_code) item['labels'] = scenes[ scene_id] # add labels parsed from filename # NOTE: assumes we'll only ever get one set of coordinates item['geometry']['coordinates'] = [ GeoPoint(*coords[::-1]) for coords in rjson['geometry']['coordinates'][0] ] scene_data.append(item) sleep(5) print('Done.') # break # NOTE: testing only return scene_data
def testEntityToClientEntity(self): # Test conversion from Beam type to client type. k = Key(['kind', 1234], project=self._PROJECT) kc = k.to_client_key() exclude_from_indexes = ('datetime', 'key') e = Entity(k, exclude_from_indexes=exclude_from_indexes) properties = { 'datetime': datetime.datetime.utcnow(), 'key_ref': Key(['kind2', 1235]), 'bool': True, 'float': 1.21, 'int': 1337, 'unicode': 'text', 'bytes': b'bytes', 'geopoint': GeoPoint(0.123, 0.456), 'none': None, 'list': [1, 2, 3], 'entity': Entity(Key(['kind', 111])), 'dict': { 'property': 5 }, } e.set_properties(properties) ec = e.to_client_entity() self.assertEqual(kc, ec.key) self.assertSetEqual(set(exclude_from_indexes), ec.exclude_from_indexes) self.assertEqual('kind', ec.kind) self.assertEqual(1234, ec.id) for name, unconverted in properties.items(): converted = ec[name] if name == 'key_ref': self.assertNotIsInstance(converted, Key) self._assert_keys_equal(unconverted, converted, self._PROJECT) elif name == 'entity': self.assertNotIsInstance(converted, Entity) self.assertNotIsInstance(converted.key, Key) self._assert_keys_equal(unconverted.key, converted.key, self._PROJECT) else: self.assertEqual(unconverted, converted) # Test reverse conversion. entity_from_client_entity = Entity.from_client_entity(ec) self.assertEqual(e, entity_from_client_entity)
def insert(self, capitalId, received_json): # print 'retriving key' key = self.ds.key(self.kind, capitalId) # print 'getting entity' entity = datastore.Entity(key) print 'filling entity' entity['name'] = received_json['name'] entity['countryCode'] = received_json['countryCode'] entity['country'] = received_json['country'] entity['id'] = received_json['id'] entity['location'] = GeoPoint(received_json['location']['latitude'], received_json['location']['longitude']) entity['continent'] = received_json['continent'] print entity #print 'put...' return self.ds.put(entity)
def add_therapist(name, lat, lon, address, description, image, rating): client = datastore.Client() key = client.key('Therapist') therapist = datastore.Entity(key) location = GeoPoint(lat, lon) therapist.update({ 'id': key.id, 'name': name, 'lat': lat, 'lon': lon, 'address': address, 'description': description, 'image': image, 'rating': rating }) client.put(therapist) return therapist.key
def parse_blob_names(file_names): '''Parse scene and label metadata from a list of filenames. # TODO: format labels as probas and include class list ''' print('Parsing file names... ', end='', flush=True) scenes = {} for file_name in file_names: direct, name = file_name.rsplit('/', maxsplit=1) # currently unused label, id, coords = name.split('__') lon, lat = coords[:-4].split('_') # slice to ignore file extension scenes[id] = scenes.get(id, list()) scenes[id].append({ # 'dir': direct, # 'file_name': name, 'label': label, 'geopoint': GeoPoint(float(lat), float(lon)) }) print('Done.') return scenes
'key7': [], 'key8': { 'a': { 'b': { 'c': [] } } }, 'key9': True, 'key10': False, 'key11': 11.123 }, 'timestamp_key': dt, 'geo_point_key': GeoPoint(-20.2, +160.5), 'null_key': None } geo_point_value = latlng_pb2.LatLng(latitude=-20.2, longitude=+160.5) EXAMPLE_DICT_DEFAULT_VALUES = { 'bool_key': False, 'string_key': u'', 'int32_key': 0, 'int64_key': 0, 'double_key': 0.0, 'float_key': 0.0, 'enum_key': example_pb2.ExampleEnumModel.ENUM0, 'bool_key': False, 'bytes_key': b'',