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

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

Issue 2432203003: [Predator] Run predator. (Closed)
Patch Set: . Created 4 years, 1 month 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
1 # Copyright 2016 The Chromium Authors. All rights reserved. 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 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Fetches entities and iterate over and process them.""" 5 """Fetches entities and iterate over and process them."""
6 6
7 import os 7 import os
8 8
9 import remote_api 9 import remote_api
10 10
11 _DEFAULT_BATCH_SIZE = 1000 11 _DEFAULT_BATCH_SIZE = 1000
12 12
13 13
14 def ProjectEntity(entity, fields): 14 def ProjectEntity(entity, fields):
15 """Projects fields from entity. Returns dict.""" 15 """Projects fields from entity. Returns dict."""
16 entity_info = {} 16 entity_info = {}
17 for field in fields: 17 for field in fields:
18 if hasattr(entity, field): 18 if hasattr(entity, field):
19 entity_info[field] = getattr(entity, field) 19 entity_info[field] = getattr(entity, field)
20 else: 20 else:
21 entity_info[field] = None 21 entity_info[field] = None
22 entity_info['id'] = entity.key.id() 22 entity_info['id'] = entity.key.urlsafe()
23 return entity_info 23 return entity_info
24 24
25 25
26 def Iterate(query, 26 def Iterate(query,
27 fields,
28 app_id, 27 app_id,
28 fields=None,
29 filter_func=None, 29 filter_func=None,
30 batch_size=_DEFAULT_BATCH_SIZE, 30 batch_size=_DEFAULT_BATCH_SIZE,
31 batch_run=False): 31 batch_run=False):
32 """Iterates entities queried by query. 32 """Iterates entities queried by query.
33 33
34 Args: 34 Args:
35 query (ndb.Query): The query to fetch entities. 35 query (ndb.Query): The query to fetch entities.
36 app_id (str): App engine app id.
36 fields (list): Field names of an entity to be projected to a dict. 37 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 If a given field name is not available, it is set to None.
38 'id' is always added by default as an integer. 39 return complete entities.
wrengr 2016/11/01 22:07:52 What do you mean "return complete entities." ?
Sharu Jiang 2016/11/11 23:10:26 I mean model instead of a projected dict.
39 app_id (str): App engine app id.
40 filter_func (function): A function that does in memory filtering. 40 filter_func (function): A function that does in memory filtering.
41 batch_size (int): The number of entities to query at one time. 41 batch_size (int): The number of entities to query at one time.
42 batch_run (bool): If True, iterate batches of entities, if 42 batch_run (bool): If True, iterate batches of entities, if
43 False, iterate each entity. 43 False, iterate each entity.
44 44
45 An exmaple is available in crash_printer/print_crash.py. 45 An exmaple is available in crash_printer/print_crash.py.
46 """ 46 """
47 remote_api.EnableRemoteApi(app_id) 47 remote_api.EnableRemoteApi(app_id)
48 48
49 cursor = None 49 cursor = None
50 while True: 50 while True:
51 entities, next_cursor, more = query.fetch_page(batch_size, 51 entities, next_cursor, more = query.fetch_page(batch_size,
52 start_cursor=cursor) 52 start_cursor=cursor)
53 if not more and not entities: 53 if not more and not entities:
54 break 54 break
55 55
56 if filter_func: 56 if filter_func:
57 entities = filter_func(entities) 57 entities = filter_func(entities)
58 58
59 entities = [ProjectEntity(entity, fields) for entity in entities] 59 if fields:
60 entities = [ProjectEntity(entity, fields) for entity in entities]
61
60 if batch_run: 62 if batch_run:
61 yield entities 63 yield entities
62 else: 64 else:
63 for entity in entities: 65 for entity in entities:
64 yield entity 66 yield entity
65 67
66 if not more: 68 if not more:
67 break 69 break
68 70
69 cursor = next_cursor 71 cursor = next_cursor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698