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

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

Issue 2391823006: [Findit] Add iterator and crash_iterator for delta test (Closed)
Patch Set: Update doc strings. 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 app_id,
29 filter_func=None,
30 batch_size=_DEFAULT_BATCH_SIZE,
31 batch_run=False):
32 """Iterates entities queried by query.
33
34 Args:
35 query (ndb.Query): The query to fetch entities.
36 fields (list): Field names of an entity to be projected to a dict.
37 If a given field name is not available, it is set to None.
38 'id' is always added by default as an integer.
39 app_id (str): App engine app id.
40 filter_func (function): A function that does in memory filtering.
41 batch_size (int): The number of entities to query at one time.
42 batch_run (bool): If True, iterate batches of entities, if
43 False, iterate each entity.
44
45 An exmaple is available in crash_printer/print_crash.py.
46 """
47 remote_api.EnableRemoteApi(app_id)
48
49 cursor = None
50 while True:
51 entities, next_cursor, more = query.fetch_page(batch_size,
52 start_cursor=cursor)
53 if not more and not entities:
54 break
55
56 if filter_func:
57 entities = filter_func(entities)
58
59 entities = [ProjectEntity(entity, fields) for entity in entities]
60 if batch_run:
61 yield entities
62 else:
63 for entity in entities:
64 yield entity
65
66 if not more:
67 break
68
69 cursor = next_cursor
OLDNEW
« no previous file with comments | « appengine/findit/util_scripts/crash_queries/crash_printer/print_crash.py ('k') | appengine/findit/util_scripts/remote_api.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698