Skip to content

Instantly share code, notes, and snippets.

@jgmel
Created March 4, 2026 14:34
Show Gist options
  • Select an option

  • Save jgmel/1ce840d656c0e1908ad201b3669fe99b to your computer and use it in GitHub Desktop.

Select an option

Save jgmel/1ce840d656c0e1908ad201b3669fe99b to your computer and use it in GitHub Desktop.
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