Skip to content
Utility library for representing storage capacity units in Python
Python Makefile
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
capacity
tests
.coveragerc
.gitignore
.travis.yml
LICENSE
Makefile
README.md
setup.py
tox.ini

README.md

Build Status   Downloads   Version

Overview

Capacity is a package helping you express capacity (or data size) units as Pythonic objects, and manipulate them in a useful, intuitive manner.

Usage

Capacity units can be created by using the built-in capacity objects:

>>> from capacity import *
>>> size = 2 * GiB

The above creates a capacity unit that represents 2GiB (2 * (10243) bytes). Capacity already comes pre-loaded with KiB, MiB,GiB,TiB,EiBfor binary units, andKB,MB,GB,TB,EBfor decimal units. You even havebyte` to denote a single byte.

Capacity objects can be added, multiplied, divided and more:

>>> size // GiB
2

>>> ((size * 2) // 3 * 12) % (13 * byte)
9*byte

>>> (2.51 * GiB) // GiB
2

>>> abs(10*byte - 1500*GiB)
1610612735990*byte

You can round units up or down to the nearest boundary:

>>> (2.5 * GiB).roundup(GiB)
3*GiB
>>> (2.5 * GiB).rounddown(GiB)
2*GiB

Formatting is easy:

>>> size = 1234567 * byte
>>> "In megabytes, we have {0:MB}".format(size)
'In megabytes, we have 1'

>>> "We have {0:MB!}".format(size) # include unit in output
'We have 1MB'

You can easily parse textual representations of sizes:

>>> Capacity("20GB")
20*GB
>>> Capacity("20GiB")
20*GiB

You can even manipulate units which are not whole bytes:

>>> byte / 2
4*bit

License

BSD3 (See LICENSE)

Something went wrong with that request. Please try again.