Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: appengine/findit/util_scripts/iterator.py

Issue 2391823006: [Findit] Add iterator and crash_iterator for delta test (Closed)
Patch Set: Fix nits. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright 2016 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """Fetches entities and iterate over and process them."""
6
7 import os
8
9 import remote_api
10
11 _DEFAULT_BATCH_SIZE = 1000
12
13
14 def ProjectEntity(entity, fields):
15 """Projects fields from entity. Returns dict."""
16 entity_info = {}
17 for field in fields:
18 if hasattr(entity, field):
19 entity_info[field] = getattr(entity, field)
20 else:
21 entity_info[field] = None
22 entity_info['id'] = entity.key.id()
23 return entity_info
24
25
26 def Iterate(query,
27 fields,
28 filter_func=None,
29 batch_size=_DEFAULT_BATCH_SIZE,
30 batch_run=False):
31 """Iterates entities queried by query.
32
33 Args:
34 query (ndb.Query): The query to fetch entities.
35 fields (list): Field names of an entity to be projected to a dict.
36 If a given field name is not available, it is set to None.
37 'id' is always added by default as an integer.
38 filter_func (function): A function that does in memory filtering.
39 batch_size (int): The number of entities to query at one time.
40 batch_run (bool): If True, iterate batches of entities, if
41 False, iterate each entity.
42
43 An exmaple is available in crash_printer/print_crash.py.
44 """
45 remote_api.EnableRemoteApi(app_id=os.getenv('APP_ID'))
stgao 2016/10/10 23:39:40 It seems better to handle the app_id from user cod
Sharu Jiang 2016/10/12 00:52:11 Done.
46
47 cursor = None
48 while True:
49 entities, next_cursor, more = query.fetch_page(batch_size,
50 start_cursor=cursor)
51
52 if not more:
53 break
stgao 2016/10/10 23:39:40 Why break here? What if `entities` is not empty?
Sharu Jiang 2016/10/12 00:52:11 https://cloud.google.com/appengine/docs/python/ndb
stgao 2016/10/13 06:38:51 My question is not whether to break "if not more"
Sharu Jiang 2016/10/19 20:12:44 Oops, done.
54
55 if filter_func:
56 entities = filter_func(entities)
57
58 entities = [ProjectEntity(entity, fields) for entity in entities]
59
60 if batch_run:
61 yield entities
62 else:
63 for entity in entities:
64 yield entity
65
66 cursor = next_cursor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698