Files
@ 5c80ca07f00c
Branch filter:
Location: Morevna/src/stats.py - annotation
5c80ca07f00c
948 B
text/x-python
reformatted whitespace with more respect for PEP-8
41ea9614ce8c 3b755a58a8b5 5c80ca07f00c 5c80ca07f00c 5c80ca07f00c 5c80ca07f00c 3b755a58a8b5 5c80ca07f00c 41ea9614ce8c 41ea9614ce8c 41ea9614ce8c 5c80ca07f00c 41ea9614ce8c 41ea9614ce8c 41ea9614ce8c 5c80ca07f00c 41ea9614ce8c 41ea9614ce8c 5813971dbecc 5c80ca07f00c 41ea9614ce8c 41ea9614ce8c 87a9ced6e7b5 5c80ca07f00c 3b755a58a8b5 3b755a58a8b5 3b755a58a8b5 3b755a58a8b5 5c80ca07f00c 41ea9614ce8c 41ea9614ce8c 41ea9614ce8c d76b98d421ae d76b98d421ae d76b98d421ae 5c80ca07f00c 5c80ca07f00c 5c80ca07f00c 5c80ca07f00c 5c80ca07f00c 5c80ca07f00c 5c80ca07f00c 5c80ca07f00c d76b98d421ae d76b98d421ae d76b98d421ae 5c80ca07f00c 5c80ca07f00c d76b98d421ae 5c80ca07f00c 5c80ca07f00c 5c80ca07f00c 5c80ca07f00c 5c80ca07f00c 5c80ca07f00c | class Stats:
def __init__(self):
self.received = 0
self.sent = 0
self.exchangedNodes = 0
self.transferredBlocks = 0
stats = Stats()
def logReceived(data):
stats.received += len(data)
def logSent(data):
stats.sent += len(data)
def logExchangedNode(k=1):
stats.exchangedNodes += k
def logTransferredBlock(k=1):
stats.transferredBlocks += k
def reset():
global stats
stats = Stats()
def report():
return """received {rf} ({r:,} B)
sent {sf} ({s:,} B)
exchanged {nodes:,} hash tree nodes
transferred {blocks:,} blocks""".format(
rf=formatBytes(stats.received),
r=stats.received,
sf=formatBytes(stats.sent),
s=stats.sent,
nodes=stats.exchangedNodes,
blocks=stats.transferredBlocks
)
def formatBytes(x):
exts = ["B", "kiB", "MiB", "GiB", "TiB", "PiB"]
i = 0
while x>1024:
x /= 1024
i += 1
if x>=100: x = round(x)
elif x>=10: x = round(x, 1)
else: x = round(x, 2)
return "{0} {1}".format(x, exts[i])
|