Skip to content

Commit

Permalink
Added %S to format()
Browse files Browse the repository at this point in the history
  • Loading branch information
progval committed Oct 16, 2010
1 parent db2324f commit e556fbd
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/utils/str.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ def timestamp(t):
t = time.time()
return time.ctime(t)

_formatRe = re.compile('%((?:\d+)?\.\d+f|[bfhiLnpqrstu%])')
_formatRe = re.compile('%((?:\d+)?\.\d+f|[bfhiLnpqrsStu%])')
def format(s, *args, **kwargs):
"""w00t.
Expand All @@ -377,6 +377,7 @@ def format(s, *args, **kwargs):
p: pluralize (takes a string)
q: quoted (takes a string)
n: nItems (takes a 2-tuple of (n, item) or a 3-tuple of (n, between, item))
S: returns a human-readable size (takes an int)
t: time, formatted (takes an int)
u: url, wrapped in braces (this should be configurable at some point)
"""
Expand Down Expand Up @@ -425,6 +426,15 @@ def sub(match):
return nItems(t[0], t[2], between=t[1])
else:
raise ValueError, 'Invalid value for %%n in format: %s' % t
elif char == 'S':
t = args.pop()
if not isinstance(t, (int, long)):
raise ValueError, 'Invalid value for %%S in format: %s' % t
for suffix in ['B','KB','MB','GB','TB']:
if t < 1024:
return "%i%s" % (t, suffix)
t /= 1024

elif char == 't':
return timestamp(args.pop())
elif char == 'u':
Expand Down

0 comments on commit e556fbd

Please sign in to comment.