Created
March 4, 2026 14:34
-
-
Save jgmel/1ce840d656c0e1908ad201b3669fe99b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from dataclasses import dataclass | |
| from enum import Enum | |
| @dataclass(frozen=True) | |
| class Scale: | |
| power: int | |
| suffix: str | |
| class UnitSystem(Enum): | |
| IEC = 1024 # KiB, MiB... | |
| SI = 1000 # kB, MB... | |
| class Unit(Enum): | |
| B = Scale(0, "B") | |
| KB = Scale(1, "KB") | |
| MB = Scale(2, "MB") | |
| GB = Scale(3, "GB") | |
| TB = Scale(4, "TB") | |
| PB = Scale(5, "PB") | |
| EB = Scale(6, "EB") | |
| ZB = Scale(7, "ZB") | |
| @property | |
| def power(self) -> int: | |
| return self.value.power | |
| @property | |
| def suffix(self) -> str: | |
| return self.value.suffix | |
| def factor(self, system: UnitSystem = UnitSystem.IEC) -> int: | |
| return int(system.value ** self.power) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment