7 Returns the exact value of floor(log2(n)).
8 No floating point calculations are used.
9 Requires positive integer type.
12 return n.bit_length() - 1
16 Returns the exact value of ceil(log2(n)).
17 No floating point calculations are used.
18 Requires positive integer type.
22 return log2floor(n-1) + 1
26 Returns the exact value of ceil(x // y).
27 No floating point calculations are used.
28 Requires positive integer types. The result
29 is undefined if at least one of the inputs
37 def save_private_key(filename, key):
38 with open(filename, "wb") as f:
39 f.write(base64.b64encode(key))
41 def load_private_key(filename):
42 with open(filename, "rb") as f:
43 return base64.b64decode(f.read())
45 from fractions import Fraction
47 class MemorySize(object):
50 to_bytes['b'] = lambda x: Fraction(x,8)
51 to_bytes['B'] = lambda x: Fraction(x,1)
52 to_bytes['KB'] = lambda x: Fraction(1000*x,1)
53 to_bytes['MB'] = lambda x: Fraction((1000**2)*x,1)
54 to_bytes['GB'] = lambda x: Fraction((1000**3)*x,1)
55 to_bytes['TB'] = lambda x: Fraction((1000**4)*x,1)
56 to_bytes['KiB'] = lambda x: Fraction(1024*x,1)
57 to_bytes['MiB'] = lambda x: Fraction((1024**2)*x,1)
58 to_bytes['GiB'] = lambda x: Fraction((1024**3)*x,1)
59 to_bytes['TiB'] = lambda x: Fraction((1024**4)*x,1)
61 def __init__(self, size, unit='B'):
63 self.numbytes = MemorySize.to_bytes[unit](Fraction.from_float(size))
67 return "%.3f b" % (self.b)
69 return "%.3f B" % (self.B)
71 return "%.3f KB" % (self.KB)
73 return "%.3f MB" % (self.MB)
75 return "%.3f GB" % (self.GB)
76 return "%.3f TB" % (self.TB)
79 def b(self): return self.numbytes*8
81 def B(self): return self.numbytes
84 def KB(self): return self.B/1000
86 def MB(self): return self.KB/1000
88 def GB(self): return self.MB/1000
90 def TB(self): return self.GB/1000
93 def KiB(self): return self.B/1024
95 def MiB(self): return self.KiB/1024
97 def GiB(self): return self.MiB/1024
99 def TiB(self): return self.GiB/1024
101 def chunkiter(objs, n=100):
103 Chunk an iterator of unknown size. The optional
104 keyword 'n' sets the chunk size (default 100).
111 while len(chunk) < n:
112 chunk.append(six.next(objs))
114 except StopIteration: