diff --git a/src/stats.py b/src/stats.py --- a/src/stats.py +++ b/src/stats.py @@ -1,48 +1,55 @@ class Stats: def __init__(self): - self.received=0 - self.sent=0 - self.exchangedNodes=0 - self.transferredBlocks=0 + self.received = 0 + self.sent = 0 + self.exchangedNodes = 0 + self.transferredBlocks = 0 -stats=Stats() +stats = Stats() def logReceived(data): - stats.received+=len(data) + stats.received += len(data) def logSent(data): - stats.sent+=len(data) + stats.sent += len(data) def logExchangedNode(k=1): - stats.exchangedNodes+=k + stats.exchangedNodes += k def logTransferredBlock(k=1): - stats.transferredBlocks+=k + stats.transferredBlocks += k def reset(): global stats - stats=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) +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 + 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]) + 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])