app-engine-ndb

GAE/P: Transaction safety with API calls

偶尔善良 提交于 2019-12-08 16:41:26
问题 Suppose you use a transaction to process a Stripe payment and update a user entity: @ndb.transactional def process_payment(user_key, amount): user = user_key.get() user.stripe_payment(amount) # API call to Stripe user.balance += amount user.put() It is possible that the Stripe API call succeeds but that the put fails because of contention. The user would then be charged, but his account wouldn't reflect the payment. You could pull the Stripe API call out of the transaction and do the

python backward paging

六月ゝ 毕业季﹏ 提交于 2019-12-08 08:49:17
问题 i have tried to make a backward paging, based on the example of google appengine documentation NDB Query Cursor my question is focused on this example: # Set up. q = Bar.query() q_forward = q.order(Bar.key) q_reverse = q.order(-Bar.key) # Fetch a page going forward. bars, cursor, more = q_forward.fetch_page(10) # Fetch the same page going backward. rev_cursor = cursor.reversed() bars1, cursor1, more1 = q_reverse.fetch_page(10, start_cursor=rev_cursor) based on this example i create my own

How do I prevent application calling datastore_v3.next() when calling get_multi?

≡放荡痞女 提交于 2019-12-08 08:17:42
问题 I'm running a keys_only query, which fetches 20 results. result_keys, cursor, more = ActivityIndex.query(cls.followers == key)\ .order(-cls.date_created)\ .fetch_page(num_results, start_cursor = cursor, keys_only=True) I then get the parents of the activityIndex objects: keys = [] for k in result_keys: for pair in k.parent().pairs(): keys.append(ndb.Key(pairs=[pair])) activities_related = ndb.get_multi(keys) I thought this would be quick, because I was getting a batch of objects by key.

What is the best practice to populate a StructuredProperty through the ndb.Model constructor?

℡╲_俬逩灬. 提交于 2019-12-08 08:12:28
问题 I looked into the ndb GitHub sample code, but I couldn't find any example which shows on how to create a ndb entity with a constructor that contains a StructuredProperty . Here is the GitHub example. What if I want to initialize a Contact entity with a list of phone numbers and this list of phone number is not a list of PhoneNumber objects. Instead it is a list of Python dictionaries. So, given the following Model classes: class PhoneNumber(ndb.Model): """A model representing a phone number."

ndb get & get_or_insert how to use ? (alway raise Exception)

此生再无相见时 提交于 2019-12-08 06:46:09
问题 I write code as below from google.appengine.ext import ndb __metaclass__ = type class UserSession(ndb.Model): session = ndb.BlobProperty() class KV: @staticmethod def get(id): r = ndb.Key(UserSession, int(id)).get() if r: return r.session @staticmethod def set(id, value): return UserSession.get_or_insert(int(id), session=value) @staticmethod def delete(id): ndb.Key(UserSession, int(id)).delete() where I write id = 1 key = ndb.Key(UserSession, int(id)) UserSession.get_or_insert(key, session=1)

TransactionFailedError on GAE when no transaction

北城以北 提交于 2019-12-08 05:55:27
问题 I got this error: TransactionFailedError: too much contention on these datastore entities. please try again. Even though I'm not doing any transactions. The line of my code that causes the error is ndb.put_multi(entity_list) # entity_list is a list of 100 entities This error doesn't happen often so it isn't a big deal, but I'm curious why I get this error. Any ideas? Here is most of the traceback: Traceback (most recent call last): ... File "/base/data/home/runtimes/python27_experiment

validator for repeated ndb.StructuredProperty fails to fire

三世轮回 提交于 2019-12-08 01:10:28
问题 Here is my ndb Model from google.appengine.ext import ndb from mainsite.rainbow.models.CFCSocialUser import CFCSocialUser class CFCSocialGroup(ndb.Model): def remove_duplicate(self, value): raise Exception("Duplicate user detected") name = ndb.StringProperty(required=True) created_on = ndb.DateTimeProperty(auto_now_add=True) updated_on = ndb.DateTimeProperty(auto_now=True) created_by = ndb.StructuredProperty(CFCSocialUser) members = ndb.StructuredProperty(CFCSocialUser, repeated=True,

ndb Models are not saved in memcache when using MapReduce

别来无恙 提交于 2019-12-07 21:07:31
问题 I've created two MapReduce Pipelines for uploading CSVs files to create Categories and Products in bulk. Each product is gets tied to a Category through a KeyProperty. The Category and Product models are built on ndb.Model, so based on the documentation, I would think they'd be automatically cached in Memcache when retrieved from the Datastore. I've run these scripts on the server to upload 30 categories and, afterward, 3000 products. All the data appears in the Datastore as expected. However

Are ndb cached read ops still counted as datastore read ops for billing purposes?

微笑、不失礼 提交于 2019-12-07 18:09:53
问题 From NDB Caching: NDB manages caches for you. There are two caching levels: an in-context cache and a gateway to App Engine's standard caching service, memcache. Both caches are enabled by default for all entity types, but can be configured to suit advanced needs. My app doesn't make any ndb caching configuration change, so it must be using the defaults - both caching levels enabled. I'm running some tests on my staging environment (a separate, dedicated GAE project) where I can totally

multiple filters vs OR , ndb query

泪湿孤枕 提交于 2019-12-07 15:09:52
问题 What is the difference between these queries: With consequent filters: qry1 = Account.query() # Retrieve all Account entitites qry2 = qry1.filter(Account.userid >= 40) # Filter on userid >= 40 qry3 = qry2.filter(Account.userid < 50) # Filter on userid < 50 as well Using ndb.OR: qry = Article.query(ndb.OR(Account.userid >= 40, Account.userid < 50)) Using ndb.AND: qry = Article.query(ndb.AND(Account.userid >= 40, Account.userid < 50)) 回答1: The first query does an AND. Thus, only the entities