Skip to content

Instantly share code, notes, and snippets.

@Arachnid
Created May 14, 2009 11:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arachnid/111622 to your computer and use it in GitHub Desktop.
Save Arachnid/111622 to your computer and use it in GitHub Desktop.
import logging
from google.appengine.ext import db
class KindIterator(object):
def __init__(self, kind, filters=None, batch_size=100):
self.kind = kind
if filters:
self.filters = filters
else:
self.filters = []
self.batch_size = batch_size
self.last_key = None
self.current_iter = iter([])
self.total_fetched = 0
self.eof = False
def _get_query(self):
q = self.kind.all();
for prop, value in self.filters:
q.filter("%s =" % prop, value)
if self.last_key:
q.filter("__key__ >", self.last_key)
q.order("__key__")
return q
def __iter__(self):
return self
def next(self):
try:
return self.current_iter.next()
except StopIteration:
if self.eof:
raise StopIteration()
logging.info("Fetching results %d-%d for kind '%s'", self.total_fetched,
self.total_fetched + self.batch_size, self.kind.kind())
while True:
try:
q = self._get_query()
results = q.fetch(self.batch_size)
break
except db.Timeout:
print "Query timed out, retrying"
if not results:
raise StopIteration()
if len(results) < self.batch_size:
self.eof = True
self.total_fetched += len(results)
self.last_key = results[-1].key()
self.current_iter = iter(results)
return self.current_iter.next()
#!/usr/bin/python
import code
import getpass
import logging
import sys
logging.basicConfig(level=logging.INFO)
base_path = "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine"
sys.path.append(base_path)
sys.path.append(base_path + "/lib/yaml/lib")
sys.path.append(base_path + "/lib/webob")
sys.path.append(base_path + "/lib/django")
from google.appengine.ext.remote_api import remote_api_stub
from google.appengine.ext import db
def auth_func():
return raw_input('Username:'), getpass.getpass('Password:')
if len(sys.argv) < 2:
print "Usage: %s app_id [host]" % (sys.argv[0],)
app_id = sys.argv[1]
if len(sys.argv) > 2:
host = sys.argv[2]
else:
host = '%s.appspot.com' % app_id
remote_api_stub.ConfigureRemoteDatastore(app_id, '/remote_api', auth_func, host)
code.interact('App Engine interactive console for %s' % (app_id,), None, locals())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment