summaryrefslogtreecommitdiff
path: root/src/leap/bitmask/vpn/_human.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/leap/bitmask/vpn/_human.py')
-rw-r--r--src/leap/bitmask/vpn/_human.py24
1 files changed, 11 insertions, 13 deletions
diff --git a/src/leap/bitmask/vpn/_human.py b/src/leap/bitmask/vpn/_human.py
index 1b2ba64c..803d3656 100644
--- a/src/leap/bitmask/vpn/_human.py
+++ b/src/leap/bitmask/vpn/_human.py
@@ -1,6 +1,3 @@
-## {{{ http://code.activestate.com/recipes/578019/ (r15)
-#!/usr/bin/env python
-
"""
Bytes-to-human / human-to-bytes converter.
Based on: http://goo.gl/kTQMs
@@ -12,14 +9,15 @@ License: MIT
# see: http://goo.gl/kTQMs
SYMBOLS = {
- 'customary' : ('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'),
- 'customary_ext' : ('byte', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa',
- 'zetta', 'iotta'),
- 'iec' : ('Bi', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'),
- 'iec_ext' : ('byte', 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi',
- 'zebi', 'yobi'),
+ 'customary': ('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'),
+ 'customary_ext': ('byte', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa',
+ 'zetta', 'iotta'),
+ 'iec': ('Bi', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'),
+ 'iec_ext': ('byte', 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi',
+ 'zebi', 'yobi'),
}
+
def bytes2human(n, format='%(value).1f %(symbol)s', symbols='customary'):
"""
Convert n bytes into a human readable string based on format.
@@ -63,13 +61,14 @@ def bytes2human(n, format='%(value).1f %(symbol)s', symbols='customary'):
symbols = SYMBOLS[symbols]
prefix = {}
for i, s in enumerate(symbols[1:]):
- prefix[s] = 1 << (i+1)*10
+ prefix[s] = 1 << (i + 1) * 10
for symbol in reversed(symbols[1:]):
if n >= prefix[symbol]:
value = float(n) / prefix[symbol]
return format % locals()
return format % dict(symbol=symbols[0], value=n)
+
def human2bytes(s):
"""
Attempts to guess the string format based on default symbols
@@ -115,8 +114,7 @@ def human2bytes(s):
letter = letter.upper()
else:
raise ValueError("can't interpret %r" % init)
- prefix = {sset[0]:1}
+ prefix = {sset[0]: 1}
for i, s in enumerate(sset[1:]):
- prefix[s] = 1 << (i+1)*10
+ prefix[s] = 1 << (i + 1) * 10
return int(num * prefix[letter])
-