[PATCH 1 of 2] util: generalize bytecount to unitcount
Bryan O'Sullivan
bos at serpentine.com
Thu Feb 21 21:33:01 UTC 2013
# HG changeset patch
# User Bryan O'Sullivan <bryano at fb.com>
# Date 1361482359 28800
# Node ID 19d3300883ce1407d0b527549a82f25d3f182873
# Parent 5779b305bf3a9192823252b8caf2240124238706
util: generalize bytecount to unitcount
This gives us a function we can reuse to count units of other kinds.
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -1268,7 +1268,18 @@ def ellipsis(text, maxlength=400):
except (UnicodeDecodeError, UnicodeEncodeError):
return _ellipsis(text, maxlength)[0]
-_byteunits = (
+def unitcount(*unittable):
+ '''return a count of some quantity formatted as a readable string'''
+
+ def go(count):
+ for multiplier, divisor, format in unittable:
+ if count >= divisor * multiplier:
+ return format % (count / float(divisor))
+ return unittable[-1][2] % count
+
+ return go
+
+bytecount = unitcount(
(100, 1 << 30, _('%.0f GB')),
(10, 1 << 30, _('%.1f GB')),
(1, 1 << 30, _('%.2f GB')),
@@ -1281,14 +1292,6 @@ def ellipsis(text, maxlength=400):
(1, 1, _('%.0f bytes')),
)
-def bytecount(nbytes):
- '''return byte count formatted as readable string, with units'''
-
- for multiplier, divisor, format in _byteunits:
- if nbytes >= divisor * multiplier:
- return format % (nbytes / float(divisor))
- return _byteunits[-1][2] % nbytes
-
def uirepr(s):
# Avoid double backslash in Windows path repr()
return repr(s).replace('\\\\', '\\')
More information about the Mercurial-devel
mailing list