diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2015-04-30 12:10:08 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2015-04-30 12:10:08 +1200 |
commit | 4dce7ee074c242f5b6530ff64879875d98c1d255 (patch) | |
tree | 4f242c627cfb2e122faaa4a6cf4ff10932eeb0ad /netlib/utils.py | |
parent | 80860229209b4c6eb8384e1bca3cabdbe062fe6e (diff) | |
download | mitmproxy-4dce7ee074c242f5b6530ff64879875d98c1d255.tar.gz mitmproxy-4dce7ee074c242f5b6530ff64879875d98c1d255.tar.bz2 mitmproxy-4dce7ee074c242f5b6530ff64879875d98c1d255.zip |
websockets: more compact and legible human_readable
Diffstat (limited to 'netlib/utils.py')
-rw-r--r-- | netlib/utils.py | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/netlib/utils.py b/netlib/utils.py index 905d948f..7e539977 100644 --- a/netlib/utils.py +++ b/netlib/utils.py @@ -70,11 +70,12 @@ def getbit(byte, offset): class BiDi: """ A wee utility class for keeping bi-directional mappings, like field - constants in protocols: + constants in protocols. Names are attributes on the object, dict-like + access maps values to names: CONST = BiDi(a=1, b=2) assert CONST.a == 1 - assert CONST[1] == "a" + assert CONST.get_name(1) == "a" """ def __init__(self, **kwargs): self.names = kwargs @@ -89,5 +90,21 @@ class BiDi: return self.names[k] raise AttributeError("No such attribute: %s", k) - def __getitem__(self, k): - return self.values[k] + def get_name(self, n, default=None): + return self.values.get(n, default) + + +def pretty_size(size): + suffixes = [ + ("B", 2**10), + ("kB", 2**20), + ("MB", 2**30), + ] + for suf, lim in suffixes: + if size >= lim: + continue + else: + x = round(size/float(lim/2**10), 2) + if x == int(x): + x = int(x) + return str(x) + suf |