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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: appengine/findit/util_scripts/iterator.py
diff --git a/appengine/findit/util_scripts/iterator.py b/appengine/findit/util_scripts/iterator.py
new file mode 100644
index 0000000000000000000000000000000000000000..c57809a0244c6e4141658b1699cb0400c8fbc059
--- /dev/null
+++ b/appengine/findit/util_scripts/iterator.py
@@ -0,0 +1,69 @@
+# Copyright 2016 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Fetches entities and iterate over and process them."""
+
+import os
+
+import remote_api
+
+_DEFAULT_BATCH_SIZE = 1000
+
+
+def ProjectEntity(entity, fields):
+ """Projects fields from entity. Returns dict."""
+ entity_info = {}
+ for field in fields:
+ if hasattr(entity, field):
+ entity_info[field] = getattr(entity, field)
+ else:
+ entity_info[field] = None
+ entity_info['id'] = entity.key.id()
+ return entity_info
+
+
+def Iterate(query,
+ fields,
+ app_id,
+ filter_func=None,
+ batch_size=_DEFAULT_BATCH_SIZE,
+ batch_run=False):
+ """Iterates entities queried by query.
+
+ Args:
+ query (ndb.Query): The query to fetch entities.
+ fields (list): Field names of an entity to be projected to a dict.
+ If a given field name is not available, it is set to None.
+ 'id' is always added by default as an integer.
+ app_id (str): App engine app id.
+ filter_func (function): A function that does in memory filtering.
+ batch_size (int): The number of entities to query at one time.
+ batch_run (bool): If True, iterate batches of entities, if
+ False, iterate each entity.
+
+ An exmaple is available in crash_printer/print_crash.py.
+ """
+ remote_api.EnableRemoteApi(app_id)
+
+ cursor = None
+ while True:
+ entities, next_cursor, more = query.fetch_page(batch_size,
+ start_cursor=cursor)
+ if not more and not entities:
+ break
+
+ if filter_func:
+ entities = filter_func(entities)
+
+ entities = [ProjectEntity(entity, fields) for entity in entities]
+ if batch_run:
+ yield entities
+ else:
+ for entity in entities:
+ yield entity
+
+ if not more:
+ break
+
+ cursor = next_cursor
« 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