Skip to main content

A simple, pythonic library for working with and humanizing file sizes.

Project description

📄
sizelib

sizelib is a lightweight, type-safe Python Library for working with and humanizing file sizes. It offers clean, type-preserving size helpers (supporting both `int` and `float`) and loop-based human readable string conversions.

🔗 PyPI Package  |  🆔 pip install sizelib or uv add sizelib


🐍 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)

print(MAX_UPLOAD_SIZE)           # Output: 10485760
print(type(MAX_UPLOAD_SIZE))     # Output: <class 'int'>

Humanize Byte Sizes

from sizelib import humanize

# Default binary formatting (base 2 / 1024)
print(humanize(MAX_UPLOAD_SIZE))  # Output: 10 MiB
print(humanize(CACHE_LIMIT))      # Output: 2 GiB

# Decimal values are formatted up to 2 decimal places
TOTAL = CACHE_LIMIT + size.mib(500)
print(humanize(TOTAL))            # Output: 2.49 GiB

# Decimal formatting (base 10 / 1000)
print(humanize(USER_QUOTA, base=10))  # Output: 50 GB

Exception Handling

from sizelib import FileTooLarge, size, humanize

file_size = size.mib(12)

try:
    if file_size > MAX_UPLOAD_SIZE:
        raise FileTooLarge(f"File size exceeds limit of {humanize(MAX_UPLOAD_SIZE)}")
except FileTooLarge as e:
    print(e)
    # Output: File size exceeds limit of 10 MiB

Features

FEATURE DESCRIPTION
📏 Unit Helpers Standardized functions for all major divisions (kb, mb, gb, tb, kib, mib, gib, tib)
🧪 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
🛡️ Structured Errors Exposes custom SizeLibError and FileTooLarge exceptions for validation
Ultra Minimalism Zero external dependencies with an optimized, lightweight iteration algorithm

🏗️ System Architecture

# COMPONENT DESCRIPTION STACK
1️⃣ Sizelib Size The math factor constants and unit calculation helper functions Python
2️⃣ Sizelib Format The humanization formatting module for readable string units Python

Made with 📄 by Saptarshi Roy

Project details


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.1.0.tar.gz (4.3 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.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sizelib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 513f44e446c340a5eada544bf9ad956294ed5519637ef163c447e51de706455a
MD5 58a7cb9af570d3c3bc11a64890a4fa01
BLAKE2b-256 948fc865a456e1d694b71e116902503a1d8ef43c030d05ff5a2cab479acb626b

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sizelib-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24cd5e416bbe96543686f4c901f57b58085e9c09d34ce923efa6b9dcb7aee22c
MD5 21e7ad537799eb514116a72b8f167a0b
BLAKE2b-256 685c79af23fadd8cbba06daf827d641654b215bf1100aaf61134ab276df0b1cd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page