Added a %r handler (repr, just like normal string formatting).

This commit is contained in:
Jeremy Fincher 2005-02-02 13:09:14 +00:00
parent 9d4291322a
commit dbd9e9f16c

View File

@ -338,7 +338,7 @@ def timestamp(t):
t = time.time() t = time.time()
return time.ctime(t) return time.ctime(t)
_formatRe = re.compile('%(\.\d+f|[bfhiLnpqstu%])') _formatRe = re.compile('%(\.\d+f|[bfhiLnpqrstu%])')
def format(s, *args, **kwargs): def format(s, *args, **kwargs):
"""w00t. """w00t.
@ -346,6 +346,7 @@ def format(s, *args, **kwargs):
i: integer i: integer
s: string s: string
f: float f: float
r: repr
b: form of the verb 'to be' (takes an int) b: form of the verb 'to be' (takes an int)
h: form of the verb 'to have' (takes an int) h: form of the verb 'to have' (takes an int)
L: commaAndify (takes a list of strings or a tuple of ([strings], and)) L: commaAndify (takes a list of strings or a tuple of ([strings], and))
@ -376,9 +377,11 @@ def format(s, *args, **kwargs):
return commaAndify(t) return commaAndify(t)
elif isinstance(t, tuple) and len(t) == 2: elif isinstance(t, tuple) and len(t) == 2:
if not isinstance(t[0], list): if not isinstance(t[0], list):
raise ValueError, 'Invalid list for %%L in format: %s' % t raise ValueError, \
'Invalid list for %%L in format: %s' % t
if not isinstance(t[1], basestring): if not isinstance(t[1], basestring):
raise ValueError, 'Invalid string for %%L in format: %s' % t raise ValueError, \
'Invalid string for %%L in format: %s' % t
return commaAndify(t[0], And=t[1]) return commaAndify(t[0], And=t[1])
else: else:
raise ValueError, 'Invalid value for %%L in format: %s' % t raise ValueError, 'Invalid value for %%L in format: %s' % t
@ -386,6 +389,8 @@ def format(s, *args, **kwargs):
return pluralize(args.pop()) return pluralize(args.pop())
elif char == 'q': elif char == 'q':
return quoted(args.pop()) return quoted(args.pop())
elif char == 'r':
return repr(args.pop())
elif char == 'n': elif char == 'n':
t = args.pop() t = args.pop()
if not isinstance(t, (tuple, list)): if not isinstance(t, (tuple, list)):