Smart, Pythonic, and dynamic: automatic unit selection and conversions made intuitive.
Project description
ByteSize: A Simple Library for Byte Size Operations
ByteSize takes the pain out of data-size conversions—efficiently handle metric/binary units, block alignment, and more, all from a single, Pythonic interface.
ByteSize is a Python library that simplifies operations with file sizes, offering dynamic unit conversions, string parsing, formatting, and more.
Features
- Parse human-readable size strings (e.g.,
"10MB","1.5GiB") into raw bytes. - Convert between metric (e.g.,
MB) and binary units (e.g.,MiB). - Arithmetic operations while preserving byte units.
- Block-aligned size calculations.
- User-friendly formatting with customizable precision.
- No dependencies, lightweight, and easy to use.
Installation
Clone the repository and install the package:
pip install pybytesize
Quickstart Guide
Creating a ByteSize Object
Create a ByteSize object from integers or human-readable strings.
By default, string representation will find the most suitable (binary) unit.
>>> from bytesize import ByteSize
>>> size = ByteSize(1_048_576) # From an integer bytes
>>> print(size)
1.00 MiB
>>> size = ByteSize("1_073_741_824MB") # From a string
>>> print(size)
1.00 PiB
Unit Conversion
Access size in different units dynamically.
>>> size1 = ByteSize(1_073_741_824)
>>> print(size1.MB) # Metric:
1.073741824
>>> print(size1.MiB) # Binary
1.00
Advanced Usage
Block Alignment
Calculate the apparent size with block alignment.
>>> size = ByteSize(123_456_789)
>>> aligned_size = size.apparent_size(4096)
>>> print(aligned_size.bytes)
123457536
Arithmetic with Sizes
Perform addition, subtraction, multiplication, and division.
>>> size3 = ByteSize("1GB") + ByteSize("512MB")
>>> print(size3) # '1.50 GiB'
1.50 GiB
>>> size4 = ByteSize("1TB") - ByteSize("500GB")
>>> print(size4) # '0.50 TiB'
0.50 TiB
Formatting Sizes
Customize formatting for specific units or precision.
>>> size = ByteSize(123_456_789)
>>> print(f"{size:.2f:MB}") # '123.46 MB'
123.46 MB
>>> print(f"{size:.2f:GiB}") # '0.11 GiB'
0.11 GiB
Project details
Release history Release notifications | RSS feed
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 pybytesize-0.8.1.tar.gz.
File metadata
- Download URL: pybytesize-0.8.1.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cc72178fc1a30aec86bba875a928e6d2fa4a5856a7d919f570280f1a03fe9e31
|
|
| MD5 |
2db92a05f99423d93ef1572511a7491f
|
|
| BLAKE2b-256 |
12ac72eeab5ece596ad6c03215bcff8e3a2fbf4e317a8af4ad7661a4c6265bd5
|
Provenance
The following attestation bundles were made for pybytesize-0.8.1.tar.gz:
Publisher:
main.yml on jjjermiah/ByteSize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pybytesize-0.8.1.tar.gz -
Subject digest:
cc72178fc1a30aec86bba875a928e6d2fa4a5856a7d919f570280f1a03fe9e31 - Sigstore transparency entry: 190979937
- Sigstore integration time:
-
Permalink:
jjjermiah/ByteSize@4cf20299fb67c84b6cb5ea08db72b0c5fb6c5b39 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jjjermiah
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@4cf20299fb67c84b6cb5ea08db72b0c5fb6c5b39 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pybytesize-0.8.1-py3-none-any.whl.
File metadata
- Download URL: pybytesize-0.8.1-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae182fa37a8f9e70e9cc0557ed1ff611b28079dbad6311a8634e778bc80ee99f
|
|
| MD5 |
92326d7d26e6a075bf2a4f4849284ebf
|
|
| BLAKE2b-256 |
05e8108d641e8cc7f1a2bb8745132ba433569dfff6301cac77a0942e2f7f1ae1
|
Provenance
The following attestation bundles were made for pybytesize-0.8.1-py3-none-any.whl:
Publisher:
main.yml on jjjermiah/ByteSize
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pybytesize-0.8.1-py3-none-any.whl -
Subject digest:
ae182fa37a8f9e70e9cc0557ed1ff611b28079dbad6311a8634e778bc80ee99f - Sigstore transparency entry: 190979941
- Sigstore integration time:
-
Permalink:
jjjermiah/ByteSize@4cf20299fb67c84b6cb5ea08db72b0c5fb6c5b39 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/jjjermiah
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@4cf20299fb67c84b6cb5ea08db72b0c5fb6c5b39 -
Trigger Event:
push
-
Statement type: