diff options
author | Kali Kaneko (leap communications) <kali@leap.se> | 2017-02-04 23:09:27 +0100 |
---|---|---|
committer | Kali Kaneko (leap communications) <kali@leap.se> | 2017-02-23 00:40:38 +0100 |
commit | 7f07ffa13eaa51419af6f019bf9b36b298274485 (patch) | |
tree | 3f6c2ed42e9160ed1ff437f2744cd38440a5b870 /src/leap/bitmask/vpn/_human.py | |
parent | 229d672dbb68c5e25612f744595febc60ef6e3cb (diff) |
[feature] add up/down traffic in status
Diffstat (limited to 'src/leap/bitmask/vpn/_human.py')
-rw-r--r-- | src/leap/bitmask/vpn/_human.py | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/src/leap/bitmask/vpn/_human.py b/src/leap/bitmask/vpn/_human.py index 1b2ba64..803d365 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]) - |