Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions demo/dataviewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def get(self):
results, cursor, more = query.fetch_page(page_size,
start_cursor=cursor)
except Exception, err:
params['error'] = '%s error: %s.%s: %s' % (prefix,
params['error'] = '{0!s} error: {1!s}.{2!s}: {3!s}'.format(prefix,
err.__class__.__module__,
err.__class__.__name__,
err)
Expand All @@ -104,7 +104,7 @@ def get(self):
data.append('<tr>')
columns = ['__key__'] + sorted(columns)
for col in columns:
data.append(' <th>%s</th>' % cgi.escape(col))
data.append(' <th>{0!s}</th>'.format(cgi.escape(col)))
data.append('</tr>')
data.append('</thead>')
data.append('<tbody>')
Expand All @@ -114,17 +114,17 @@ def get(self):
if col not in row:
data.append(' <td></td>')
else:
data.append(' <td>%s</td>' % cgi.escape(row[col]))
data.append(' <td>{0!s}</td>'.format(cgi.escape(row[col])))
data.append('</tr>')
data.append('</tbody>')
data.append('</table>')
params['data'] = '\n '.join(data)
if more:
next = ('<a href=/dataviewer?%s>Next</a>' %
next = ('<a href=/dataviewer?{0!s}>Next</a>'.format(
urllib.urlencode([('query', query_string),
('cursor', cursor.to_websafe_string()),
('page', page_size),
]))
])))
params['next'] = next
self.response.out.write(FORM % params)

Expand Down
3 changes: 1 addition & 2 deletions demo/fibo.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def get(self):
memo_type = ''
ans = yield fibonacci(num)
t1 = time.time()
self.response.out.write('%sfibonacci(%d) == %d # computed in %.3f\n' %
(memo_type, num, ans, t1 - t0))
self.response.out.write('{0!s}fibonacci({1:d}) == {2:d} # computed in {3:.3f}\n'.format(memo_type, num, ans, t1 - t0))


urls = [
Expand Down
10 changes: 5 additions & 5 deletions demo/guestbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ def get(self):
greetings = Greeting.QueryBook(ancestor_key)

for greeting in greetings:
self.response.out.write('<blockquote>%s</blockquote>' %
cgi.escape(greeting.content))
self.response.out.write('<blockquote>{0!s}</blockquote>'.format(
cgi.escape(greeting.content)))

self.response.out.write("""
<form action="/sign?%s" method="post">
<form action="/sign?{0!s}" method="post">
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
<hr>
<form>Guestbook name: <input value="%s" name="guestbook_name">
<form>Guestbook name: <input value="{1!s}" name="guestbook_name">
<input type="submit" value="switch"></form>
</body>
</html>""" % (urllib.urlencode({'guestbook_name': guestbook_name}),
</html>""".format(urllib.urlencode({'guestbook_name': guestbook_name}),
cgi.escape(guestbook_name)))


Expand Down
6 changes: 3 additions & 3 deletions demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,11 @@ def _hp_callback(self, message):
yield summary.put_async()
hover = ''
if summary.title:
hover = ' title="%s"' % summary.title
hover = ' title="{0!s}"'.format(summary.title)
escbody = (cgi.escape(pre) +
'<a%s href="%s">' % (hover, cgi.escape(url)) +
'<a{0!s} href="{1!s}">'.format(hover, cgi.escape(url)) +
cgi.escape(url) + '</a>' + cgi.escape(post))
text = '%s - %s - %s<br>' % (cgi.escape(nickname),
text = '{0!s} - {1!s} - {2!s}<br>'.format(cgi.escape(nickname),
time.ctime(message.when or 0),
escbody)
if message.when is None:
Expand Down
8 changes: 4 additions & 4 deletions demo/shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def shell():
project_id = id_resolver.resolve_project_id(application_id)

banner = """ndb shell
Python %s
Project: %s
Python {0!s}
Project: {1!s}
The ndb module is already imported.
""" % (sys.version, project_id)
""".format(sys.version, project_id)

imports = {
'ndb': ndb,
Expand All @@ -53,7 +53,7 @@ def shell():
# set up the environment
os.environ['SERVER_SOFTWARE'] = 'Development (ndb_shell)/0.1'

sys.ps1 = '%s> ' % project_id
sys.ps1 = '{0!s}> '.format(project_id)
if readline is not None:
# set up readline
readline.parse_and_bind('tab: complete')
Expand Down
12 changes: 6 additions & 6 deletions ndb/blobstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def get(cls, blob_key, **ctx_options):
def get_async(cls, blob_key, **ctx_options):
"""Async version of get()."""
if not isinstance(blob_key, (BlobKey, basestring)):
raise TypeError('Expected blob key, got %r' % (blob_key,))
raise TypeError('Expected blob key, got {0!r}'.format(blob_key))
if 'parent' in ctx_options:
raise TypeError('Parent is not supported')
return cls.get_by_id_async(str(blob_key), **ctx_options)
Expand All @@ -206,7 +206,7 @@ def get_multi_async(cls, blob_keys, **ctx_options):
"""Async version of get_multi()."""
for blob_key in blob_keys:
if not isinstance(blob_key, (BlobKey, basestring)):
raise TypeError('Expected blob key, got %r' % (blob_key,))
raise TypeError('Expected blob key, got {0!r}'.format(blob_key))
if 'parent' in ctx_options:
raise TypeError('Parent is not supported')
blob_key_strs = map(str, blob_keys)
Expand Down Expand Up @@ -273,7 +273,7 @@ def delete(blob_key, **options):
def delete_async(blob_key, **options):
"""Async version of delete()."""
if not isinstance(blob_key, (basestring, BlobKey)):
raise TypeError('Expected blob key, got %r' % (blob_key,))
raise TypeError('Expected blob key, got {0!r}'.format(blob_key))
rpc = blobstore.create_rpc(**options)
yield blobstore.delete_async(blob_key, rpc=rpc)

Expand All @@ -293,7 +293,7 @@ def delete_multi(blob_keys, **options):
def delete_multi_async(blob_keys, **options):
"""Async version of delete_multi()."""
if isinstance(blob_keys, (basestring, BlobKey)):
raise TypeError('Expected a list, got %r' % (blob_key,))
raise TypeError('Expected a list, got {0!r}'.format(blob_key))
rpc = blobstore.create_rpc(**options)
yield blobstore.delete_async(blob_keys, rpc=rpc)

Expand Down Expand Up @@ -366,7 +366,7 @@ def get_value(dct, name):
value = dct.get(name, None)
if value is None:
raise BlobInfoParseError(
'Field %s has no %s.' % (field_name, name))
'Field {0!s} has no {1!s}.'.format(field_name, name))
return value

filename = get_value(field_storage.disposition_options, 'filename')
Expand All @@ -384,7 +384,7 @@ def get_value(dct, name):
size = int(size)
except (TypeError, ValueError):
raise BlobInfoParseError(
'%s is not a valid value for %s size.' % (size, field_name))
'{0!s} is not a valid value for {1!s} size.'.format(size, field_name))

try:
creation = blobstore._parse_creation(creation_string, field_name)
Expand Down
61 changes: 30 additions & 31 deletions ndb/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,42 +53,42 @@ class ContextOptions(datastore_rpc.Configuration):
def use_cache(value):
if not isinstance(value, bool):
raise datastore_errors.BadArgumentError(
'use_cache should be a bool (%r)' % (value,))
'use_cache should be a bool ({0!r})'.format(value))
return value

@datastore_rpc.ConfigOption
def use_memcache(value):
if not isinstance(value, bool):
raise datastore_errors.BadArgumentError(
'use_memcache should be a bool (%r)' % (value,))
'use_memcache should be a bool ({0!r})'.format(value))
return value

@datastore_rpc.ConfigOption
def use_datastore(value):
if not isinstance(value, bool):
raise datastore_errors.BadArgumentError(
'use_datastore should be a bool (%r)' % (value,))
'use_datastore should be a bool ({0!r})'.format(value))
return value

@datastore_rpc.ConfigOption
def memcache_timeout(value):
if not isinstance(value, (int, long)):
raise datastore_errors.BadArgumentError(
'memcache_timeout should be an integer (%r)' % (value,))
'memcache_timeout should be an integer ({0!r})'.format(value))
return value

@datastore_rpc.ConfigOption
def max_memcache_items(value):
if not isinstance(value, (int, long)):
raise datastore_errors.BadArgumentError(
'max_memcache_items should be an integer (%r)' % (value,))
'max_memcache_items should be an integer ({0!r})'.format(value))
return value

@datastore_rpc.ConfigOption
def memcache_deadline(value):
if not isinstance(value, (int, long)):
raise datastore_errors.BadArgumentError(
'memcache_deadline should be an integer (%r)' % (value,))
'memcache_deadline should be an integer ({0!r})'.format(value))
return value


Expand Down Expand Up @@ -122,8 +122,7 @@ def _make_ctx_options(ctx_options, config_cls=ContextOptions):
translation = _OPTION_TRANSLATIONS.get(key)
if translation:
if translation in ctx_options:
raise ValueError('Cannot specify %s and %s at the same time' %
(key, translation))
raise ValueError('Cannot specify {0!s} and {1!s} at the same time'.format(key, translation))
ctx_options[translation] = ctx_options.pop(key)
return config_cls(**ctx_options)

Expand Down Expand Up @@ -176,7 +175,7 @@ def __init__(self, todo_tasklet, limit):
self._cache = {} # Cache of in-flight todo_tasklet futures.

def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self._todo_tasklet.__name__)
return '{0!s}({1!s})'.format(self.__class__.__name__, self._todo_tasklet.__name__)

def run_queue(self, options, todo):
"""Actually run the _todo_tasklet."""
Expand Down Expand Up @@ -208,7 +207,7 @@ def add(self, arg, options=None):
An instance of future, representing the result of running
_todo_tasklet without batching.
"""
fut = tasklets.Future('%s.add(%s, %s)' % (self, arg, options))
fut = tasklets.Future('{0!s}.add({1!s}, {2!s})'.format(self, arg, options))
todo = self._queues.get(options)
if todo is None:
utils.logging_debug('AutoBatcher(%s): creating new queue for %r',
Expand Down Expand Up @@ -979,7 +978,7 @@ def transaction(self, callback, **ctx_options):
'Context without non-transactional ancestor')
else:
raise datastore_errors.BadArgumentError(
'Invalid propagation value (%s).' % (propagation,))
'Invalid propagation value ({0!s}).'.format(propagation))

app = TransactionOptions.app(options) or key_module._DefaultAppId()
# Note: zero retries means try it once.
Expand Down Expand Up @@ -1176,9 +1175,9 @@ def memcache_get(self, key, for_cas=False, namespace=None, use_cache=False,
memcache, or None.
"""
if not isinstance(key, basestring):
raise TypeError('key must be a string; received %r' % key)
raise TypeError('key must be a string; received {0!r}'.format(key))
if not isinstance(for_cas, bool):
raise TypeError('for_cas must be a bool; received %r' % for_cas)
raise TypeError('for_cas must be a bool; received {0!r}'.format(for_cas))
if namespace is None:
namespace = namespace_manager.get_namespace()
options = (for_cas, namespace, deadline)
Expand All @@ -1197,9 +1196,9 @@ def memcache_gets(self, key, namespace=None, use_cache=False, deadline=None):
def memcache_set(self, key, value, time=0, namespace=None, use_cache=False,
deadline=None):
if not isinstance(key, basestring):
raise TypeError('key must be a string; received %r' % key)
raise TypeError('key must be a string; received {0!r}'.format(key))
if not isinstance(time, (int, long)):
raise TypeError('time must be a number; received %r' % time)
raise TypeError('time must be a number; received {0!r}'.format(time))
if namespace is None:
namespace = namespace_manager.get_namespace()
options = ('set', time, namespace, deadline)
Expand All @@ -1211,52 +1210,52 @@ def memcache_set(self, key, value, time=0, namespace=None, use_cache=False,

def memcache_add(self, key, value, time=0, namespace=None, deadline=None):
if not isinstance(key, basestring):
raise TypeError('key must be a string; received %r' % key)
raise TypeError('key must be a string; received {0!r}'.format(key))
if not isinstance(time, (int, long)):
raise TypeError('time must be a number; received %r' % time)
raise TypeError('time must be a number; received {0!r}'.format(time))
if namespace is None:
namespace = namespace_manager.get_namespace()
return self._memcache_set_batcher.add((key, value),
('add', time, namespace, deadline))

def memcache_replace(self, key, value, time=0, namespace=None, deadline=None):
if not isinstance(key, basestring):
raise TypeError('key must be a string; received %r' % key)
raise TypeError('key must be a string; received {0!r}'.format(key))
if not isinstance(time, (int, long)):
raise TypeError('time must be a number; received %r' % time)
raise TypeError('time must be a number; received {0!r}'.format(time))
if namespace is None:
namespace = namespace_manager.get_namespace()
options = ('replace', time, namespace, deadline)
return self._memcache_set_batcher.add((key, value), options)

def memcache_cas(self, key, value, time=0, namespace=None, deadline=None):
if not isinstance(key, basestring):
raise TypeError('key must be a string; received %r' % key)
raise TypeError('key must be a string; received {0!r}'.format(key))
if not isinstance(time, (int, long)):
raise TypeError('time must be a number; received %r' % time)
raise TypeError('time must be a number; received {0!r}'.format(time))
if namespace is None:
namespace = namespace_manager.get_namespace()
return self._memcache_set_batcher.add((key, value),
('cas', time, namespace, deadline))

def memcache_delete(self, key, seconds=0, namespace=None, deadline=None):
if not isinstance(key, basestring):
raise TypeError('key must be a string; received %r' % key)
raise TypeError('key must be a string; received {0!r}'.format(key))
if not isinstance(seconds, (int, long)):
raise TypeError('seconds must be a number; received %r' % seconds)
raise TypeError('seconds must be a number; received {0!r}'.format(seconds))
if namespace is None:
namespace = namespace_manager.get_namespace()
return self._memcache_del_batcher.add(key, (seconds, namespace, deadline))

def memcache_incr(self, key, delta=1, initial_value=None, namespace=None,
deadline=None):
if not isinstance(key, basestring):
raise TypeError('key must be a string; received %r' % key)
raise TypeError('key must be a string; received {0!r}'.format(key))
if not isinstance(delta, (int, long)):
raise TypeError('delta must be a number; received %r' % delta)
raise TypeError('delta must be a number; received {0!r}'.format(delta))
if initial_value is not None and not isinstance(initial_value, (int, long)):
raise TypeError('initial_value must be a number or None; received %r' %
initial_value)
raise TypeError('initial_value must be a number or None; received {0!r}'.format(
initial_value))
if namespace is None:
namespace = namespace_manager.get_namespace()
return self._memcache_off_batcher.add((key, delta),
Expand All @@ -1265,12 +1264,12 @@ def memcache_incr(self, key, delta=1, initial_value=None, namespace=None,
def memcache_decr(self, key, delta=1, initial_value=None, namespace=None,
deadline=None):
if not isinstance(key, basestring):
raise TypeError('key must be a string; received %r' % key)
raise TypeError('key must be a string; received {0!r}'.format(key))
if not isinstance(delta, (int, long)):
raise TypeError('delta must be a number; received %r' % delta)
raise TypeError('delta must be a number; received {0!r}'.format(delta))
if initial_value is not None and not isinstance(initial_value, (int, long)):
raise TypeError('initial_value must be a number or None; received %r' %
initial_value)
raise TypeError('initial_value must be a number or None; received {0!r}'.format(
initial_value))
if namespace is None:
namespace = namespace_manager.get_namespace()
return self._memcache_off_batcher.add((key, -delta),
Expand Down
6 changes: 3 additions & 3 deletions ndb/context_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ def run():
def testUrlFetch(self):
self.testbed.init_urlfetch_stub()
host, port = self.start_test_server()
fut = self.ctx.urlfetch('http://%s:%d' % (host, port))
fut = self.ctx.urlfetch('http://{0!s}:{1:d}'.format(host, port))
result = fut.get_result()
self.assertEqual(result.status_code, 200)
self.assertTrue(isinstance(result.content, str))
Expand Down Expand Up @@ -881,7 +881,7 @@ class EmptyModel(model.Model):
get_fut = self.ctx.get(key)
ent = get_fut.get_result()
self.assertTrue(ent is not None,
'Memcache delete did block memcache set %r' % ent)
'Memcache delete did block memcache set {0!r}'.format(ent))

def testMemcacheAPI(self):
self.ExpectErrors()
Expand Down Expand Up @@ -1068,7 +1068,7 @@ class Employee(model.Model):
ks = self.ctx._memcache_prefix + k.urlsafe()
v = memcache.get(ks)
self.assertTrue(isinstance(v, str),
'Expected instanceof "str", got "%s"' % type(v))
'Expected instanceof "str", got "{0!s}"'.format(type(v)))

def testCorruptMemcache(self):
# Check that corrupt memcache entries silently fail.
Expand Down
3 changes: 1 addition & 2 deletions ndb/eventloop.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ def run0(self):
# Yes, wait_any() may return None even for a non-empty argument.
# But no, it won't ever return an RPC not in its argument.
if rpc not in self.rpcs:
raise RuntimeError('rpc %r was not given to wait_any as a choice %r' %
(rpc, self.rpcs))
raise RuntimeError('rpc {0!r} was not given to wait_any as a choice {1!r}'.format(rpc, self.rpcs))
callback, args, kwds = self.rpcs[rpc]
del self.rpcs[rpc]
if callback is not None:
Expand Down
Loading