computing with and displaying bytes
Project description
Bytesize
Bytesize is a module for handling computation with sizes expressed in bytes. Its principle feature is a Size class from which can be constructed Size objects which represent a precise and finite quantity of bytes. Various arithmetic operations are defined for Size objects.
Its sole purpose is the representation of real quantities of memory on real machines. For that reason, it does not allow powers of bytes, imprecise quantities of bytes, or non-finite quantities of bytes. In order that the usual laws of arithmetic can be maintained, it does allow fractional quantities of bytes.
Practical Computing with Bytes
When computing with bytes, the numeric value can be viewed as a logical, rather than a physical, quantity. That is, unlike, e.g., mass or length, which are quantities which must be measured with a measuring instrument which has some built-in imprecision, the number of bytes of memory in RAM, or on a disk, is a quantity that is not measured, but is known precisely. Consequently, computations such as addition of two Sizes, and conversion between different magnitudes of bytes, i.e., from MiB to GiB, must be done precisely. The underlying implementation must therefore use a precise representation of the number of bytes. Floating point numbers, which are frequently the preferred type for the representation of physical quantities, are disallowed by this requirement.
Operations
This module does not accomodate multi-dimensionality of byte quantities. Consequently, multiplying one Size object by another Size object will cause an error to be raised, since bytes^2 is not representable by the module. For most uses any operation which would yield a multi-dimensional quantity of bytes is not useful. There are no plans to adapt this package so that it can accomodate multi-dimensionality of bytes.
Numerous computations with bytes are nonsensical. For example, 2 raised to a power which is some number of bytes, is a meaningless computation. All such operations cause an error to be raised.
The result type of operations is a Size, where appropriate, or a subtype of Rational, where a numeric value is appropriate.
Floating Point Numbers
It is not possible to use floating point numbers in computations with Sizes. Where a fractional quantity is desired, use Decimal objects instead of floats. Thus, Size(0) * 1.2 raises an exception, but Size(0) * Decimal(“1.2”) is acceptable.
Displaying Sizes
Sizes are displayed according to a specified configuration. In the default configuration, Sizes are displayed using binary rather than SI prefixes or names, regardless of the value. For example, 1000 bytes is not displayed as 1KB (1 kilobyte), but as some number of bytes or KiB (kibibytes).
The detailed representation of Sizes uses a precise decimal representation that includes the repeating portion, if any.
Representing Units
The size module supplies a set of named prefixes for both SI and binary units, for all non-fractional prefixes. Fractional prefixes are not defined.
Constructing Sizes Programatically
New Size objects can be constructed from Size objects, numeric values, e.g., int or Decimal, or strings which represent such numeric values. strings may be used to represent fractional quantities, e.g., “1.2”, but floats are disallowed.
The constructor takes an optional units specifier, which defaults to bytes for all numeric values, and to None for Size objects. The type of the unit specifier is a named prefix supplied by the size module or a Size object.
Errors
All errors raised by bytesize operations are subtypes of the SizeError class.
Alternative Packages
If you are interested in computing in Python with physical, rather than logical, quantities, you should consult the pint package: http://pint.readthedocs.org.
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
File details
Details for the file bytesize-0.4.tar.gz
.
File metadata
- Download URL: bytesize-0.4.tar.gz
- Upload date:
- Size: 48.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9dfbd5cca080327f4c85c1237f7ec0509187bb197478c9523c07ad55fc5529d |
|
MD5 | 0301154aa272c558cf16dad091a925d5 |
|
BLAKE2b-256 | 7e4c68ce9e514213f91a40468bf83bd929c1b09bde73fee55fd6604d2703b749 |