def make_key(table_name, objid): """Create an object key for storage.""" key = datastore.Key() path = key.path_element.add() path.kind = table_name path.name = str(objid) return key
def testIncompleteKey(self): key = datastore.Key() add_key_path(key, 'Foo') self.assertEquals(1, len(key.path)) self.assertEquals('Foo', key.path[0].kind) self.assertEquals(0, key.path[0].id) self.assertEquals('', key.path[0].name)
def testSetKeyPath(self): key = datastore.Key() add_key_path(key, 'Foo', 1, 'Bar', 'bar') self.assertEquals(2, len(key.path)) self.assertEquals('Foo', key.path[0].kind) self.assertEquals(1, key.path[0].id) self.assertEquals('Bar', key.path[1].kind) self.assertEquals('bar', key.path[1].name)
def get_key(id): key = googledatastore.Key() path = key.path_element.add() path.kind = cls.__name__ if isinstance(id, basestring): path.name = id else: path.id = id return key
def clouddatastore_client(): try: logFormatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') root = logging.getLogger() root.setLevel(logging.INFO) ch = logging.StreamHandler(sys.stdout) ch.setLevel(logging.INFO) ch.setFormatter(logFormatter) root.addHandler(ch) logging.getLogger('oauth2service.client').setLevel(logging.INFO) logging.getLogger('apiclient.discovery').setLevel(logging.INFO) credentials = GoogleCredentials.get_application_default() http = httplib2.Http() http = credentials.authorize(http) credentials.refresh(http) credentials.access_token = 'foo' print credentials.access_token datastore.set_options(project_id='p0', credentials=credentials) req = datastore.LookupRequest() key = datastore.Key() path = key.path.add() path.kind = 'Employee' path.name = 'aguadypoogznoqofmgmy' req.keys.extend([key]) resp = datastore.lookup(req) if resp.found: entity = resp.found[0].entity print (str(entity)) for prop in entity.properties: print 'Lookup: ' + str(prop) else: print 'entity not found; initialize entity and insert..' req = datastore.CommitRequest() req.mode = datastore.CommitRequest.NON_TRANSACTIONAL employee = req.mutations.add().insert path = employee.key.path.add() path.kind = 'Employee' path.name = 'aguadypoogznoqofmgmy' res = datastore.commit(req) print res except HttpError as err: print 'Error:', pprint.pprint(err.content) except AccessTokenRefreshError: print ("Credentials have been revoked or expired, please re-run" "the application to re-authorize")
def __get_key(self): key = googledatastore.Key() path = key.path_element.add() path.kind = type(self).__name__ id = self._get_id() if isinstance(id, basestring): path.name = id else: path.id = id return key
def testPropertyValues(self): property_dict = collections.OrderedDict( a_string=u'a', a_blob='b', a_boolean=True, a_integer=1L, a_double=1.0, a_timestamp_microseconds=datetime.datetime.now(), a_key=datastore.Key(), a_entity=datastore.Entity(), many_integer=[1L, 2L, 3L]) entity = datastore.Entity() add_properties(entity, property_dict) d = dict((key, get_value(value)) for key, value in entity.properties.items()) self.maxDiff = None self.assertDictEqual(d, property_dict)
def testPropertyValues(self): blob_key = datastore.Value() blob_key.blob_key_value = 'blob-key' properties = collections.OrderedDict( a_string=u'a', a_blob='b', a_boolean=True, a_integer=1, a_double=1.0, a_timestamp_microseconds=datetime.datetime.now(), a_key=datastore.Key(), a_entity=datastore.Entity(), a_blob_key=blob_key, many_integer=[1, 2, 3]) entity = datastore.Entity() add_property_values(entity, **properties) d = get_property_values(entity) self.assertDictEqual(d, properties)
def testPropertyValues(self): blob_key = datastore.Value() blob_key.blob_key_value = 'blob-key' property_dict = collections.OrderedDict( a_string=u'a', a_blob='b', a_boolean=True, a_integer=1, a_long=2L, a_double=1.0, a_timestamp_microseconds=datetime.datetime.now(), a_key=datastore.Key(), a_entity=datastore.Entity(), a_blob_key=blob_key, many_integer=[1, 2, 3]) entity = datastore.Entity() helper.add_properties(entity, property_dict) d = dict((prop.name, helper.get_value(prop.value)) for prop in entity.property) self.assertDictEqual(d, property_dict)
def cbquery(self, camid, field): camkey = datastore.Key() path = camkey.path_element.add() path.kind = 'PSCams' path.name = camid req = datastore.LookupRequest() req.key.extend([camkey]) resp = datastore.lookup(req) if resp.found: pscam = resp.found[0].entity password_property = datastore.Property() password_property.name = field for prop in pscam.property: if prop.name == field: val = prop.value.string_value result = [camid, val] else: result = [camid, field] return result
def key(self): return add_key_path(datastore.Key(), *self.key_path)
def checkDB(self, gitkit_user): req = datastore.BeginTransactionRequest() resp = datastore.begin_transaction(req) tx = resp.transaction req = datastore.LookupRequest() key = datastore.Key() path = key.path_element.add() path.kind = 'PSUsers' path.name = gitkit_user.user_id req.key.extend([key]) req.read_options.transaction = tx resp = datastore.lookup(req) req = datastore.CommitRequest() req.transaction = tx if resp.missing: user = req.mutation.insert.add() user.key.CopyFrom(key) userid_property = user.property.add() userid_property.name = 'userid' userid_property.value.string_value = gitkit_user.user_id display_name_property = user.property.add() display_name_property.name = 'display_name' display_name_property.value.string_value = gitkit_user.name photo_url_property = user.property.add() photo_url_property.name = 'photo_url' if gitkit_user.photo_url: photo_url_property.value.string_value = gitkit_user.photo_url else: photo_url_property.value.string_value = "/images/home/slider/slide1/cloud1.png" email_property = user.property.add() email_property.name = 'email' email_property.value.string_value = gitkit_user.email last_login_property = user.property.add() last_login_property.name = 'last_login' last_login_property.value.timestamp_microseconds_value = long( time.time() * 1e6) elif resp.found: user = resp.found[0].entity last_login_property = datastore.Property() last_login_property.name = 'last_login' for prop in user.property: if prop.name == 'last_login': prop.value.timestamp_microseconds_value = long( time.time() * 1e6) req.mutation.update.extend([user]) resp = datastore.commit(req) return None
def testInvalidKey(self): key = datastore.Key() self.assertRaises(TypeError, add_key_path, key, 'Foo', 1.0)
# if platform.system() != 'Windows' and platform.system() != 'Darwin': # Set the dataset from the command line parameters. datastore.set_options(dataset="daily-stock-forecast") #Save each symbol into the datastore for i in np.arange(len(symbols)): if rank[symbols[i]] <= 100000: try: req = datastore.CommitRequest() req.mode = datastore.CommitRequest.NON_TRANSACTIONAL entity = req.mutation.insert_auto_id.add() # Create a new entity key. key = datastore.Key() # Set the entity key with only one `path_element`: no parent. path = key.path_element.add() path.kind = 'Forecast' # Copy the entity key. entity.key.CopyFrom(key) # - a dateTimeValue 64bit integer: `date` prop = entity.property.add() prop.name = 'date' prop.value.timestamp_microseconds_value = long( tt.mktime(dayToPredict.timetuple()) * 1e6) #prop.value.timestamp_microseconds_value = long(tt.time() * 1e6)
def main(): # Set dataset id from command line argument. if len(sys.argv) < 2: print 'Usage: adams.py <DATASET_ID>' sys.exit(1) # Set the dataset from the command line parameters. datastore.set_options(dataset=sys.argv[1]) try: # Create a RPC request to begin a new transaction. req = datastore.BeginTransactionRequest() # Execute the RPC synchronously. resp = datastore.begin_transaction(req) # Get the transaction handle from the response. tx = resp.transaction # Create a RPC request to get entities by key. req = datastore.LookupRequest() # Create a new entity key. key = datastore.Key() # Set the entity key with only one `path_element`: no parent. path = key.path_element.add() path.kind = 'Trivia' path.name = 'hgtg' # Add one key to the lookup request. req.key.extend([key]) # Set the transaction, so we get a consistent snapshot of the # entity at the time the transaction started. req.read_options.transaction = tx # Execute the RPC and get the response. resp = datastore.lookup(req) # Create a RPC request to commit the transaction. req = datastore.CommitRequest() # Set the transaction to commit. req.transaction = tx if resp.found: # Get the entity from the response if found. entity = resp.found[0].entity else: # If no entity was found, insert a new one in the commit request mutation. entity = req.mutation.insert.add() # Copy the entity key. entity.key.CopyFrom(key) # Add two entity properties: # - a utf-8 string: `question` prop = entity.property.add() prop.name = 'question' prop.value.string_value = 'Meaning of life?' # - a 64bit integer: `answer` prop = entity.property.add() prop.name = 'answer' prop.value.integer_value = 42 # Execute the Commit RPC synchronously and ignore the response: # Apply the insert mutation if the entity was not found and close # the transaction. datastore.commit(req) props = get_property_dict(entity) # Get question property value. question = props['question'].string_value # Get answer property value. answer = props['answer'].integer_value # Print the question and read one line from stdin. print question result = raw_input('> ') if result == str(answer): print ('fascinating, extraordinary and, ' 'when you think hard about it, completely obvious.') else: print "Don't Panic!" except datastore.RPCError as e: # RPCError is raised if any error happened during a RPC. # It includes the `method` called and the `reason` of the # failure as well as the original `HTTPResponse` object. logging.error('Error while doing datastore operation') logging.error('RPCError: %(method)s %(reason)s', {'method': e.method, 'reason': e.reason}) logging.error('HTTPError: %(status)s %(reason)s', {'status': e.response.status, 'reason': e.response.reason}) return