def to_entity(line): fields = line.split( ',') #id,president,startYear,endYear,party,homeState,dateOfBirth id = int(fields[0]) key = Key([kind, id]) entity = Entity(key) president = fields[1] names = president.split(' ') entity.set_properties({ 'id': id, 'firstName': names[0], 'lastName': names[1], 'startYear': int(fields[2]), 'endYear': int(fields[3]), 'party': fields[4], 'homeState': fields[5], 'dateOfBirth': datetime.strptime(fields[6], '%Y-%m-%d') }) return entity
def testEntityFromClientEntity(self): k = Key(['kind', 1234], project=self._PROJECT) exclude_from_indexes = ('efi1', 'efi2') e = Entity(k, exclude_from_indexes=exclude_from_indexes) e.set_properties({'efi1': 'value', 'property': 'value'}) efc = Entity.from_client_entity(e.to_client_entity()) self.assertEqual(e, efc)
def make_entity(self, content): """Create entity from given string.""" key = Key([self._kind, hashlib.sha1(content.encode('utf-8')).hexdigest()], parent=self._parent_key) entity = Entity(key) entity.set_properties({'content': str(content)}) return entity
def process(self, element): key = Key(['natality-guid', element['guid']]) entity = Entity(key) entity.set_properties({ 'weight': element['weight'], 'time': element['time'] }) yield entity
def make_entity(self, content): ancestor_key = Key([self._kind, self._ancestor], self._namespace, self._project) # Namespace and project are inherited from parent key. key = Key([self._kind, str(uuid.uuid4())], parent=ancestor_key) entity = Entity(key) entity.set_properties({'content': content}) return entity
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 testEntityToClientEntity(self): k = Key(['kind', 1234], project=self._PROJECT) kc = k.to_client_key() exclude_from_indexes = ('efi1', 'efi2') e = Entity(k, exclude_from_indexes=exclude_from_indexes) e.set_properties({'efi1': 'value', 'property': 'value'}) 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)
def to_entity(content): key = Key([kind, str(uuid.uuid4())]) entity = Entity(key) entity.set_properties({'content': content}) return entity