Skip to content

Commit

Permalink
bytes formatting simplify (Chia-Network#6021)
Browse files Browse the repository at this point in the history
* init

* Add dash in README

* add more test cases

* whitespace around ==

* explicit return

* add another large test case
  • Loading branch information
Nikolaj-K authored May 26, 2021
1 parent ee35d85 commit 0b31248
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
Chia is a modern cryptocurrency built from scratch, designed to be efficient, decentralized, and secure. Here are some of the features and benefits:
* [Proof of space and time](https://docs.google.com/document/d/1tmRIb7lgi4QfKkNaxuKOBHRmwbVlGL4f7EsBDr_5xZE/edit) based consensus which allows anyone to farm with commodity hardware
* Very easy to use full node and farmer GUI and cli (thousands of nodes active on mainnet)
* Simplified UTXO based transaction model, with small on chain state
* Lisp-style turing complete functional [programming language](https://chialisp.com/) for money related use cases
* Simplified UTXO based transaction model, with small on-chain state
* Lisp-style Turing-complete functional [programming language](https://chialisp.com/) for money related use cases
* BLS keys and aggregate signatures (only one signature per block)
* [Pooling protocol](https://www.chia.net/2020/11/10/pools-in-chia.html) (in development) that allows farmers to have control of making blocks
* Support for light clients with fast, objective syncing
Expand Down
19 changes: 9 additions & 10 deletions chia/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ def format_bytes(bytes: int) -> str:
if not isinstance(bytes, int) or bytes < 0:
return "Invalid"

labels = {0: "MiB", 1: "GiB", 2: "TiB", 3: "PiB", 4: "EiB", 5: "ZiB", 6: "YiB"}

base = 1024
value = bytes / base ** 2
key = 0
while value >= base and key < 6:
value /= base
key += 1

return f"{value:.3f} {labels[key]}"
LABELS = ("MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB")
BASE = 1024
value = bytes / BASE
for label in LABELS:
value /= BASE
if value < BASE:
return f"{value:.3f} {label}"

return f"{value:.3f} {LABELS[-1]}"


def format_minutes(minutes: int) -> str:
Expand Down
3 changes: 3 additions & 0 deletions tests/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ async def test_format_bytes(self):
assert format_bytes(-1024) == "Invalid"
assert format_bytes(0) == "0.000 MiB"
assert format_bytes(1024) == "0.001 MiB"
assert format_bytes(1024 ** 2 - 1000) == "0.999 MiB"
assert format_bytes(1024 ** 2) == "1.000 MiB"
assert format_bytes(1024 ** 3) == "1.000 GiB"
assert format_bytes(1024 ** 4) == "1.000 TiB"
Expand All @@ -20,6 +21,8 @@ async def test_format_bytes(self):
assert format_bytes(1024 ** 7) == "1.000 ZiB"
assert format_bytes(1024 ** 8) == "1.000 YiB"
assert format_bytes(1024 ** 9) == "1024.000 YiB"
assert format_bytes(1024 ** 10) == "1048576.000 YiB"
assert format_bytes(1024 ** 20).endswith("YiB")

@pytest.mark.asyncio
async def test_format_minutes(self):
Expand Down

0 comments on commit 0b31248

Please sign in to comment.