Functions for decomposing nonnegative integers according to various unit scales and also parsing support for values written in scales.
Latest release 20250528:
- Big refactor so that we can just str() the result of a decompose.
- New Decomposed, UnitStep, UnitScale classes.
Presupplied scales:
BINARY_BYTES_SCALE: Binary units of (B)ytes, KiB, MiB, GiB etc.DECIMAL_BYTES_SCALE: Decimal units of (B)ytes, KB, MB, GB etc.DECIMAL_SCALE: Unit suffixes K, M, G etc.TIME_SCALE: Units of (s)econds, (m)inutes, (h)ours, (d)ays and (w)eeks.UNSCALED_SCALE: no units
Short summary:
decimal: Decompose a nonnegative integerninto human decimal counts (kilo etc).decompose: Decompose a nonnegative integerninto counts by unit fromscale. Returns aDecomposedlist of(modulus,UnitStep)in order from smallest unit upward.Decomposed: A list of(modulus,UnitStep)2-tuples representing a decomposed value.geek: Decompose a nonnegative integerninto geek bytes sizes (kibibytes etc).geek_bytes: Decompose a nonnegative integerninto geek bytes sizes (kibibytes etc).human: Decompose a nonnegative integerninto human decimal counts (kilo etc).human_bytes: Decompose a nonnegative integerninto human bytes sizes (kilobytes etc).human_time: Decompose a nonnegative integerninto human time (hours etc).multiparse: Parse an integer followed by an optional scale and return computed value. Returns the parsed value and the new offset.parse: Parse an integer followed by an optional scale and return computed value. Returns the parsed value and the new offset. This is a shim forUnitScale.get_term(s,offset).transcribe: Transcribe a nonnegative integernagainstscale. This is just a shim forUnitScale.transcribe, itself a shim forDecomposed.__str__.transcribe_bytes_decompose: Transcribe a nonnegative integernagainstDECIMAL_BYTES_SCALE.transcribe_bytes_geek: Transcribe a nonnegative integernagainstBINARY_BYTES_SCALE.transcribe_time: Transcribe a nonnegative integernagainstTIME_SCALE.UnitScale: A representation of a unit scale as a list of unit terms and scale factors.UnitStep: A scale step for a unit scale. The lastUnitStepin a scale should havefactor=0.
Module contents:
-
decimal(n): Decompose a nonnegative integerninto human decimal counts (kilo etc). -
decompose(n, scale: cs.units.UnitScale) -> cs.units.Decomposed: Decompose a nonnegative integerninto counts by unit fromscale. Returns aDecomposedlist of(modulus,UnitStep)in order from smallest unit upward.Parameters:
n: a nonnegative integer.scale: aUnitScale(or promotable)
-
ClassDecomposed(builtins.list)``: A list of(modulus,UnitStep)2-tuples representing a decomposed value.
Decomposed.__float__(self) -> float:
The total value as a float.
Decomposed.__int__(self) -> int:
The total value as an int.
Decomposed.__str__(self, max_parts=None, *, skip_zero=False, sep='', no_pad=False) -> str:
Transcribe a nonnegative integer n against scale.
Parameters:
max_parts: the maximum number of components to transcribe.skip_zero: omit components of value 0.sep: separator between words, default:''.
Decomposed.sum(self):
Return the total value represented by this Decomposed.
-
geek(n): Decompose a nonnegative integerninto geek bytes sizes (kibibytes etc). -
geek_bytes(n): Decompose a nonnegative integerninto geek bytes sizes (kibibytes etc). -
human(n): Decompose a nonnegative integerninto human decimal counts (kilo etc). -
human_bytes(n): Decompose a nonnegative integerninto human bytes sizes (kilobytes etc). -
human_time(n): Decompose a nonnegative integerninto human time (hours etc). -
multiparse(s, scales, offset=0): Parse an integer followed by an optional scale and return computed value. Returns the parsed value and the new offset.Parameters:
s: the string to parse.scales: an iterable of scale arrays of (factor, unit_name).offset: starting position for parse.
-
parse(s: str, scale: cs.units.UnitScale, offset=0): Parse an integer followed by an optional scale and return computed value. Returns the parsed value and the new offset. This is a shim forUnitScale.get_term(s,offset).Parameters:
s: the string to parse.scale: a scale array of (factor, unit_name).offset: starting position for parse.
-
transcribe(n, scale: cs.units.UnitScale, max_parts=None, *, skip_zero=False, sep='', no_pad=False): Transcribe a nonnegative integernagainstscale. This is just a shim forUnitScale.transcribe, itself a shim forDecomposed.__str__.Parameters:
n: a nonnegative integer.scale: aUnitScale(or promotable)max_parts: the maximum number of components to transcribe.skip_zero: omit components of value 0.sep: separator between words, default:''.
-
transcribe_bytes_decompose(n, max_parts=1, **kw): Transcribe a nonnegative integernagainstDECIMAL_BYTES_SCALE. -
transcribe_bytes_geek(n, max_parts=1, **kw): Transcribe a nonnegative integernagainstBINARY_BYTES_SCALE. -
transcribe_time(n, max_parts=3, **kw): Transcribe a nonnegative integernagainstTIME_SCALE. -
ClassUnitScale(cs.deco.Promotable)``: A representation of a unit scale as a list of unit terms and scale factors.
UnitScale.decompose(self, n) -> cs.units.Decomposed:
Decompose a nonnegative integer n into counts by unit.
Returns a Decomposed list of (modulus,UnitStep) in order from smallest unit upward.
Parameters:
n: a nonnegative integer.scale: a sequence ofUnitStepor(factor,unit[,max_width])where factor is the size factor to the following scale item andunitis the designator of the unit.
UnitScale.from_str(scale_name: str):
Return a UnitScale from its name.
UnitScale.get_term(self, s: str, offset=0) -> Tuple[int, int]:
Parse a decimal values possibly followed by a unit name from s at offset.
Return a (value,offset) 2-tuple.
Parameters:
s: the string to parse.scale: a scale array of (factor, unit_name).offset: starting position for parse.
UnitScale.transcribe(self, n, max_parts=None, *, skip_zero=False, sep='', no_pad=False):
Transcribe n according to this scale.
Parameters are passed to Decomposed.__str__.
ClassUnitStep``: A scale step for a unit scale. The lastUnitStepin a scale should have `factor=0`.
UnitStep.__iter__(self):
for unpacking assignment
Release Log
Release 20250528:
- Big refactor so that we can just str() the result of a decompose.
- New Decomposed, UnitStep, UnitScale classes.
Release 20220311: Naming update: rename human* to decompose* to clarify that these decompose a number into a sequence of values on a scale.
Release 20210809: Update parse() for new structure in the scale definitions.
Release 20210717: transcribe: pad units by default, produces less jitter in dynamic displays like progress bars.
Release 20201025: transcribe: use str(count_i), not str(count) if count == count_i.
Release 20200718: Use str.isalpha to recognise "unit" words instead of string.ascii_letters (uses new cs.lex.get_chars "callable gochars" mode).
Release 20200626: transcribe: use "%.1f" to format the count if it is not == int(count) ==> a float-with-fraction.
Release 20200613: New UNSCALED_SCALE for no units.
Release 20190220: Dependency fix.
Release 20181228: Initial PyPI release.
Metadata
Release files for cs-units 20250528
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cs_units-20250528.tar.gz | 5.7 kB | Without provenance | Details for cs_units-20250528.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cs_units-20250528-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for cs_units-20250528-py2.py3-none-any.whl |
Total release size: 12.5 kB
Release files / cs_units-20250528.tar.gz
| Download URL | cs_units-20250528.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
179602f6e4ea4d59c1aaa9308f99a29b6aaac0fe461e2ec02c309d076d2ab7f8
|
|
BLAKE2b-256 checksum How to use checksums |
451e17e69cbd7c7fcc32e5fa76c793b2bc15d37b79fda5021489a9bd71ac6a31
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Release files / cs_units-20250528-py2.py3-none-any.whl
| Download URL | cs_units-20250528-py2.py3-none-any.whl |
|---|---|
| Size | 6.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
6c807c1495ac23801ebf87bfe7ce4756d3b4f332c8fd116a6d2c40e1411cde8b
|
|
BLAKE2b-256 checksum How to use checksums |
aeaa68a764957fc2f6589449b523c3171eb630ebf426cb8abfdfead627da044d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.1
|
Security for cs-units 20250528
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.