Skip to main content

📄
Sizelib

sizelib is a lightweight, type-safe Python Library for working with and humanizing file sizes, time durations, frequencies, and data transfer rates. It offers clean, type-preserving unit helpers (supporting both int and float) and loop-based human-readable string conversions.

PyPI Version GitHub Release PyPI Downloads GitHub License


✳️ SDK

Installation

pip install sizelib
# or
uv add sizelib

Size Helper Functions

from sizelib import size

# Define constraints using binary (base 2 / 1024) or decimal (base 10 / 1000) helper methods
MAX_UPLOAD_SIZE = size.mib(10)  # 10 MiB (10485760 bytes)
CACHE_LIMIT = size.gib(2)  # 2 GiB (2147483648 bytes)
USER_QUOTA = size.gb(50)  # 50 GB (50000000000 bytes)

# Variables, expressions, and type preservation (int/float) are supported
limit = 2
custom_limit = size.gib(limit)  # 2 GiB (2147483648 bytes)
print(type(size.mib(10)))  # Output: <class 'int'>
print(type(size.kb(1.5)))  # Output: <class 'float'>

Humanize Byte Sizes (human_size)

from sizelib import human_size, size

# Default binary formatting (base 2 / 1024)
print(human_size(10485760))  # Output: 10 MiB
print(human_size(size.gib(2.5)))  # Output: 2.50 GiB

# Decimal formatting (base 10 / 1000)
print(human_size(size.gb(50), base=10))  # Output: 50 GB

Time Helper Functions

from sizelib import time

# Define time constraints cleanly in seconds (supports ms, s, m, h, d, w, m28..m31, y, ly)
TIMEOUT = time.s(30)  # 30 s
CACHE_TTL = time.m(15)  # 900 s
TOKEN_EXPIRY = time.h(2)  # 7200 s
FEB_NON_LEAP = time.m28(1)  # 2419200 s (28 days)

# Variables, expressions, and type preservation (int/float) are supported
limit = 5
custom_timeout = time.m(limit)  # 300 s
print(type(time.s(30)))  # Output: <class 'int'>
print(type(time.s(1.5)))  # Output: <class 'float'>

Humanize Times (human_time)

from sizelib import human_time, time

# Duration formatting across ms, s, m, h, d, w
print(human_time(0.005))  # Output: 5 ms
print(human_time(time.s(45)))  # Output: 45 s
print(human_time(time.m(90)))  # Output: 1.50 h

# Auto-rounds and formats fractional durations up to 2 decimal places
print(human_time(time.h(2.5)))  # Output: 2.50 h

Frequency Helper Functions

from sizelib import freq

# Define frequency constraints cleanly in Hz (supports hz, khz, mhz, ghz, thz, phz, ehz, zhz, yhz)
CPU_BASE_CLOCK = freq.ghz(3.2)  # 3.2 GHz (3200000000.0 Hz)
RAM_SPEED = freq.mhz(3200)  # 3200 MHz (3200000000 Hz)
AUDIO_SAMPLE_RATE = freq.khz(44.1)  # 44.1 kHz (44100.0 Hz)

# Variables, expressions, and type preservation (int/float) are supported
multiplier = 4.5
custom_clock = freq.ghz(multiplier)  # 4.5 GHz (4500000000.0 Hz)
print(type(freq.mhz(800)))  # Output: <class 'int'>
print(type(freq.ghz(4.8)))  # Output: <class 'float'>

Humanize Frequencies (human_freq)

from sizelib import freq, human_freq

# Frequency formatting across Hz through YHz
print(human_freq(44100))  # Output: 44.10 kHz
print(human_freq(freq.mhz(800)))  # Output: 800 MHz
print(human_freq(freq.ghz(3.2)))  # Output: 3.20 GHz
print(human_freq(freq.thz(1.5)))  # Output: 1.50 THz

Rate Helper Functions

from sizelib import rate

# Define bandwidth and bitrate constraints cleanly (supports b_s, kib_s..yib_s, kb_s..yb_s, bps, kibps..yibps, kbps..ybps)
NETWORK_BANDWIDTH = rate.mbps(100)  # 100 Mbps (100000000 bits/s)
BUFFER_RATE = rate.mibps(50)  # 50 Mibps (52428800 bits/s)
INTERNET_SPEED = rate.gbps(1)  # 1 Gbps (1000000000 bits/s)
DOWNLOAD_SPEED = rate.mb_s(12.5)  # 12.5 MB/s (12500000 bytes/s)
DISK_WRITE_RATE = rate.mib_s(500)  # 500 MiB/s (524288000 bytes/s)

`Humanize Rates (human_rate)`

```python
from sizelib import human_rate, rate

# Default binary formatting (base 2 / 1024)
print(human_rate(10485760))                         # Output: 10 MiB/s
print(human_rate(rate.gib_s(2.5)))                  # Output: 2.50 GiB/s

# Decimal formatting (base 10 / 1000)
print(human_rate(20000000000, base=10))             # Output: 20 GB/s
print(human_rate(rate.kb_s(5), base=10))            # Output: 5 KB/s

# Decimal bit rate formatting (bits=True)
print(human_rate(rate.mbps(100), bits=True))        # Output: 100 Mbps
print(human_rate(rate.gbps(1.5), bits=True))        # Output: 1.50 Gbps

# Binary bit rate formatting (bits=True, base=2)
print(human_rate(rate.mibps(2), base=2, bits=True)) # Output: 2 Mibps

✳️ Features

FEATURE DESCRIPTION
Size Helpers Standardized functions for all major divisions (kb, mb, gb, tb, pb, eb, zb, yb, kib, mib, gib, tib, pib, eib, zib, yib)
Time Helpers Clean scaling for duration units (ms, s, m, h, d, w, m28, m29, m30, m31, y, ly)
Frequency Helpers Clean scaling for frequency units (hz, khz, mhz, ghz, thz, phz, ehz, zhz, yhz)
Rate Helpers Clean scaling for data transfer rate units (b_s, kib_s..yib_s, kb_s..yb_s, bps, kibps..yibps, kbps..ybps)
Type Preservation Dynamically maintains input types (returns int/floats accordingly)
Custom Bases Support for both binary (base=2 / 1024) and decimal (base=10 / 1000) formats
Ultra Minimalism Zero external dependencies with an optimized, lightweight iteration algorithm

✳️ Architecture

# COMPONENT DESCRIPTION STACK
1️⃣ Sizelib SDK Python library for size, time, frequency & rate calculations and humanization Python
2️⃣ Sizelib Docs Official documentation & web interface Next.js, Tailwind, Vercel

Made with 📄 by Saptarshi Roy

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sizelib-0.4.0.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sizelib-0.4.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file sizelib-0.4.0.tar.gz.

File metadata

  • Download URL: sizelib-0.4.0.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for sizelib-0.4.0.tar.gz
Algorithm Hash digest
SHA256 8c64d0cbeb6950c25534c8aacba9a8bdb5ab5fd4f9d767079f7401bcd98a5c72
MD5 db8ea7d16c9c6465e285d363648fefe9
BLAKE2b-256 1654ca55632ae175c7d68acfe6c042cc4e82a19f3eb52ab42361758eca6f8a66

See more details on using hashes here.

File details

Details for the file sizelib-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: sizelib-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for sizelib-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b62f37aaeaf20662d19bcc6d8c922ae49d68e16fe3449d93a8316ecdc950fa35
MD5 27b622289382099409aaeac3fbdc16b9
BLAKE2b-256 48297d680de659d6749a09cb5493d4499406a88a30847c17d54823721201eeac

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 files

0.3.0

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page