A unified storage unit library for Python with cross-platform file size support
Project description
Features
- 📦 Comprehensive Unit Support: Binary (KiB, MiB, GiB...), decimal (KB, MB, GB...), and bit units
- 🧮 Smart Arithmetic Support: Same-unit operations preserve units (1 GB + 2 GB = 3 GB), mixed units convert automatically
- 🎯 Configurable Precision: Eliminate scientific notation with configurable decimal precision (no more 1.23e-05 GB!)
- 📝 Flexible String Parsing: Case-insensitive parsing with support for various formats and separators
- 🔗 Cross-Platform File Operations: Get file and directory sizes using
pathlibwith platform-specific optimizations - ⚡ Platform-Specific Optimizations: Windows, Linux, and macOS-specific implementations for better performance
- 🔒 Type Safety: Complete type annotations for better IDE support and code safety
- 🎯 Zero Dependencies: Uses only Python standard library
Installation
pip install filesizelib
Quick Start
from filesizelib import Storage, StorageUnit, FileSizeLib
# Create storage values
storage1 = Storage(1, StorageUnit.KIB)
storage2 = Storage(512, StorageUnit.BYTES)
# FileSizeLib is an alias for Storage - both are functionally identical
filesize = FileSizeLib(1024, StorageUnit.BYTES)
print(storage1 == filesize) # True
# Smart arithmetic - same units preserve unit, mixed units convert to bytes
same_unit_total = Storage(1, StorageUnit.GB) + Storage(2, StorageUnit.GB) # 3 GB (preserved!)
mixed_unit_total = storage1 + storage2 # 1536 BYTES (converted)
doubled = storage1 * 2 # 2 KIB
# Comparisons
print(storage1 > storage2) # True
print(storage1 == Storage(1024, StorageUnit.BYTES)) # True
# Unit conversion
print(storage1.convert_to_bytes()) # 1024.0
print(storage2.convert_to(StorageUnit.KIB)) # 0.5 KIB
# String parsing (flexible formats)
size1 = Storage.parse("1.5 MB") # 1.5 MB
size2 = Storage.parse("1,024 KiB") # 1024.0 KIB (comma as decimal separator)
size3 = Storage.parse("500GB") # 500.0 GB (no space)
size4 = Storage.parse("2.5 tb") # 2.5 TB (case insensitive)
size5 = Storage.parse("1024") # 1024.0 BYTES (defaults to bytes)
# Auto-scaling for human-readable output
large_size = Storage(1536000000, StorageUnit.BYTES)
print(large_size.auto_scale()) # 1.43 GIB (binary) or 1.536 GB (decimal)
# Decimal precision control (eliminates scientific notation)
Storage.set_decimal_precision(5) # Configure precision globally
small_value = Storage(9.872019291e-05, StorageUnit.GIB)
print(f"Precise: {small_value}") # 0.0001 GIB (no scientific notation!)
# File operations
file_size = Storage.get_size_from_path("/path/to/file.txt")
dir_size = Storage.get_size_from_path("/path/to/directory")
print(f"Directory size: {dir_size.auto_scale()}")
Supported Units
Binary Units (Base 1024)
BYTES(1 byte)KIB(1,024 bytes)MIB(1,048,576 bytes)GIB,TIB,PIB,EIB,ZIB,YIB
Decimal Units (Base 1000)
KB(1,000 bytes)MB(1,000,000 bytes)GB(1,000,000,000 bytes)TB,PB,EB,ZB,YB
Bit Units
BITS(1/8 byte)KILOBITS,MEGABITS,GIGABITS,TERABITS,PETABITS,EXABITS,ZETTABITS,YOTTABITS
Advanced Usage
Platform-Specific Optimizations
from filesizelib import Storage
# Automatically detect and use platform-specific optimizations
platform_storage = Storage.get_platform_storage()
size = platform_storage.get_size_from_path("/large/directory")
# Platform info
info = platform_storage.get_platform_info()
print(f"Platform: {info['platform']}")
print(f"Optimizations: {info.get('api_optimization', 'none')}")
String Parsing Features
The parse() method supports various input formats:
from filesizelib import Storage, StorageUnit
# Case insensitive
Storage.parse("1.5 mb") # Works
Storage.parse("1.5 MB") # Works
Storage.parse("1.5 Mb") # Works
# Decimal separators
Storage.parse("1.5 MB") # Dot separator
Storage.parse("1,5 MB") # Comma separator
# Spacing
Storage.parse("1.5 MB") # With space
Storage.parse("1.5MB") # Without space
# Default unit
Storage.parse("1024") # Defaults to bytes
Storage.parse("1024", StorageUnit.KIB) # Custom default
# Comprehensive unit aliases
Storage.parse("1 kilobyte") # Full name
Storage.parse("1 kb") # Abbreviation
Storage.parse("1 k") # Single letter
Error Handling
from filesizelib import Storage, StorageUnit
# Invalid values raise appropriate exceptions
try:
Storage(-1, StorageUnit.BYTES) # Raises ValueError
except ValueError as e:
print(f"Error: {e}")
try:
Storage.parse("invalid") # Raises ValueError
except ValueError as e:
print(f"Parsing error: {e}")
try:
storage = Storage(1, StorageUnit.KIB)
result = storage / 0 # Raises ZeroDivisionError
except ZeroDivisionError as e:
print(f"Division error: {e}")
Real-World Examples
Download Time Calculator
FileSizeLib is the same thing as Storage, so you can use either interchangeably.
from filesizelib import FileSizeLib
# File sizes
movie = FileSizeLib.parse("1.4 GB")
song = FileSizeLib.parse("4.5 MB")
# Network speeds (in bits per second)
broadband = FileSizeLib.parse("50 Megabits") # 50 Mbps
fiber = FileSizeLib.parse("1 Gigabit") # 1 Gbps
# Calculate download times
movie_time_broadband = movie / broadband # seconds
movie_time_fiber = movie / fiber # seconds
print(f"Movie download time:")
print(f" Broadband (50 Mbps): {movie_time_broadband:.1f} seconds")
print(f" Fiber (1 Gbps): {movie_time_fiber:.1f} seconds")
Storage Capacity Planning
from filesizelib import FileSizeLib
# Calculate total storage needs
photos = FileSizeLib.parse("2.8 MiB") * 2000 # 2000 photos
music = FileSizeLib.parse("4.5 MB") * 500 # 500 songs
videos = FileSizeLib.parse("1.2 GB") * 50 # 50 videos
documents = FileSizeLib.parse("250 KB") * 1000 # 1000 documents
total_needed = photos + music + videos + documents
print(f"Total storage needed: {total_needed.auto_scale()}")
# Available storage
available = FileSizeLib.parse("500 GB")
remaining = available - total_needed
print(f"Remaining space: {remaining.auto_scale()}")
Development
Running Tests
# Run basic functionality tests
python test_basic_functionality.py
Building and Installation
please install uv first
# Install development dependencies
uv sync
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file filesizelib-0.6.14.tar.gz.
File metadata
- Download URL: filesizelib-0.6.14.tar.gz
- Upload date:
- Size: 109.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
85edf21810041dcddd99f9ea1413b866c9386a9f73680c92708202e9a40357a4
|
|
| MD5 |
5c17fa9824e6568c7949f28483a56e9a
|
|
| BLAKE2b-256 |
231e9dce7a57c7db20fb57018b0dff357db90b2ef55243f633bab0aa85cfc8d0
|
Provenance
The following attestation bundles were made for filesizelib-0.6.14.tar.gz:
Publisher:
publish.yml on 271374667/filesizelib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
filesizelib-0.6.14.tar.gz -
Subject digest:
85edf21810041dcddd99f9ea1413b866c9386a9f73680c92708202e9a40357a4 - Sigstore transparency entry: 301795997
- Sigstore integration time:
-
Permalink:
271374667/filesizelib@6c19dd82cb844ca68eebaed17de5f29ccfdf5899 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/271374667
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6c19dd82cb844ca68eebaed17de5f29ccfdf5899 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file filesizelib-0.6.14-py3-none-any.whl.
File metadata
- Download URL: filesizelib-0.6.14-py3-none-any.whl
- Upload date:
- Size: 17.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcbb94433206e29c21640283142f53ed8944039efca706e4fff6c83d54d141fd
|
|
| MD5 |
171f02e93dd9253747fa992200846837
|
|
| BLAKE2b-256 |
c2b476c2ac5f90a7ed14a75a70ef7f2afc1a0c582a54740b074115db86d0a8a7
|
Provenance
The following attestation bundles were made for filesizelib-0.6.14-py3-none-any.whl:
Publisher:
publish.yml on 271374667/filesizelib
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
filesizelib-0.6.14-py3-none-any.whl -
Subject digest:
bcbb94433206e29c21640283142f53ed8944039efca706e4fff6c83d54d141fd - Sigstore transparency entry: 301796005
- Sigstore integration time:
-
Permalink:
271374667/filesizelib@6c19dd82cb844ca68eebaed17de5f29ccfdf5899 -
Branch / Tag:
refs/heads/master - Owner: https://github.com/271374667
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@6c19dd82cb844ca68eebaed17de5f29ccfdf5899 -
Trigger Event:
workflow_dispatch
-
Statement type: