Skip to main content

Convert bytes to human-readable file sizes and back.

Project description

philiprehberger-filesize

Tests PyPI version Last updated

Convert bytes to human-readable file sizes and back.

Installation

pip install philiprehberger-filesize

Usage

from philiprehberger_filesize import humanize, parse, is_larger_than

humanize(1536)                  # "1.5 KB"
humanize(1073741824)            # "1.0 GB"
humanize(1024, binary=True)     # "1.0 KiB"

parse("1.5 GB")                 # 1500000000
parse("1 KiB")                  # 1024

is_larger_than(5000000, "1 MB") # True

Convert to a specific unit

to_unit(size, unit) returns a float in the requested unit. Useful for arithmetic when a formatted string would not do.

from philiprehberger_filesize import to_unit

to_unit(1500, "KB")         # 1.5
to_unit(1024 ** 2, "MiB")   # 1.0
to_unit(2_500_000, "MB")    # 2.5

Size constants

Exported integer constants for use as multipliers in code:

from philiprehberger_filesize import KB, MB, GB, MIB, GIB, humanize

threshold = 5 * MB
humanize(threshold)             # "5.0 MB"
humanize(2 * GIB, binary=True)  # "2.0 GiB"

Available: BYTES, KB, MB, GB, TB, KIB, MIB, GIB, TIB.

Summing and comparing sizes

total(*sizes) accepts any mix of integer byte counts and human-readable strings, parses the strings via parse(), and returns the sum. compare(a, b) returns -1, 0, or 1 after parsing each operand — useful for sorting mixed int/str size values.

from philiprehberger_filesize import total, compare

total(1024, "1 KB", "2 MB")     # 2_001_024
total()                         # 0

compare(100, 200)               # -1
compare("1 KiB", 1024)          # 0
compare("1 MB", "1 KB")         # 1

sizes = ["10 KB", "1 MB", 500]
sorted(sizes, key=lambda s: compare(s, 0))
# [500, '10 KB', '1 MB']

API

Function / Class Description
humanize(size, binary=False, precision=1) Bytes to human string
format_bytes(size, binary=False, precision=2) Alias with precision=2
parse(text) Human string to bytes
to_unit(size, unit) Convert bytes to a specific unit, returning a float
is_larger_than(size, threshold) Compare size to human string
total(*sizes) Sum mixed int/string sizes into bytes
compare(a, b) Return -1/0/1 after parsing both operands
BYTES, KB, MB, GB, TB SI multiplier constants (1000-based)
KIB, MIB, GIB, TIB Binary multiplier constants (1024-based)

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

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

philiprehberger_filesize-0.3.0.tar.gz (177.7 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_filesize-0.3.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_filesize-0.3.0.tar.gz.

File metadata

  • Download URL: philiprehberger_filesize-0.3.0.tar.gz
  • Upload date:
  • Size: 177.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_filesize-0.3.0.tar.gz
Algorithm Hash digest
SHA256 ebb3e1cc5e450fc2e788a37f8542ba97528f6e32f0872105c965fc6c5d764c28
MD5 7b4787b14b27c7b6f1f1dd5a88104408
BLAKE2b-256 28aca75138471228f6fb925a512469ca957a2984f77c831a59bf7ee3768552bf

See more details on using hashes here.

File details

Details for the file philiprehberger_filesize-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_filesize-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 acf16619ab4f541189deaa8c3804a959b79baeb06516776e29c76a068fd2064a
MD5 90278c0fd74e28630e3ae63ac4612477
BLAKE2b-256 7863d5594e61cbade1197d2c722d5f11181657c256ced156ef020d6a517175be

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