Registry / serialization / prefixed

prefixed

JSON →
library0.9.0pypypi✓ verified 26d ago

Prefixed provides an alternative implementation of the built-in `float` which supports formatted output with SI (decimal) and IEC (binary) prefixes. The library is actively maintained with regular updates addressing bug fixes, new SI prefixes, and improvements to string parsing and formatting behavior.

pip install prefixed
INSTALL
IMPORT
SIG · PREFIXED
P
prefixed
serializationpythonv0.9.0
Install
1.7s avg
Import
10ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.006s · 17.8MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.006s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Float
✓ from prefixed import Float

Initialize `prefixed.Float` objects and format them using f-strings with SI ('h', 'H') or IEC ('k', 'K', 'm', 'M') prefixes. Precision can indicate significant digits for 'H', 'K', 'M' types. String initialization also recognizes SI and IEC prefixes.

from prefixed import Float # Basic SI formatting value_si = Float(3250) print(f'{value_si:.2h}') # IEC formatting with bytes unit value_iec = Float(42467328) print(f'{value_iec:.2k}B') # Significant digits with SI value_sig_dig = Float(1246) print(f'{value_sig_dig:.3H}') # Initializing from strings with prefixes from_string_k = Float('2k') from_string_Ki = Float('2Ki') print(f'{from_string_k=}') print(f'{from_string_Ki=}')
Debug
Known issues
breakingThe behavior of the `!` format flag changed in version 0.6.0. Previously, `!` would drop the space after the number if no prefix was added. It now consistently adds a space after the number, even if no prefix is present. A new `!!` flag was introduced for the old behavior (dropping the space if no prefix is added).
fix
Review usage of `!` flag. If you relied on the space being dropped when no prefix was present, switch to using `!!`. Otherwise, no action is needed, as `!` now consistently adds a space.
affects: 0.6.0+
deprecatedThe format specifiers 'j' (IEC) and 'J' (IEC short) were deprecated in version 0.4.0. They have been replaced by 'k' and 'm' respectively. While 'j' and 'J' still work, their use is discouraged and may be removed in future versions.
fix
Replace 'j' with 'k' and 'J' with 'm' in your format strings.
affects: 0.4.0+
gotchaInput string parsing became stricter in version 0.3.2. It no longer accepts arbitrary characters after the prefix and now specifically allows a single space between the value and the prefix. If your application relied on less strict parsing for `Float` initialization from strings, it might break.
fix
Ensure input strings adhere to the format of a number followed by an optional single space, then an optional prefix (e.g., '10 k', '500m', '2Ki').
affects: 0.3.2+
gotchaPrior to version 0.8.0, rounding was not correctly accounted for when determining the appropriate prefix, leading to inaccurate results (e.g., `9.999999999e-10` formatting as `1000p` instead of `1n`). This was corrected in 0.8.0.
fix
Upgrade to version 0.8.0 or later to ensure correct rounding behavior during prefix determination.
affects: <0.8.0
gotchaAs of version 0.9.0, the micro symbol ('µ', U+00B5) is accepted as a valid prefix when creating `prefixed.Float` objects from strings. However, the output will still consistently use the lowercase Greek letter mu ('μ', U+03BC).
fix
Be aware of this input/output distinction if your application processes or expects specific Unicode representations of the micro prefix.
affects: 0.9.0+
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'prefixed'
The 'prefixed' library is not installed in your Python environment.
fix
Install the library using pip: `pip install prefixed`
AttributeError: 'prefixed.Float' object has no attribute 'replace'
You are attempting to call a string method like `replace()` on a `prefixed.Float` object, which is a subclass of `float` and does not have string manipulation methods directly.
fix
Convert the `prefixed.Float` object to a string first using `str()` before applying string methods, or use f-string formatting to control its string representation. For example: `str(my_prefixed_float).replace('k', 'Kilo')` or `f'{my_prefixed_float:.2h}'.replace('k', 'Kilo')`
ValueError: could not convert string to prefixed.Float: 'invalid_string_input'
The string provided to the `prefixed.Float` constructor or parsing function contains characters or a format that the library cannot interpret as a valid number with an optional SI/IEC prefix.
fix
Ensure the input string represents a valid number with an optional, correctly formatted prefix (e.g., '1.2k', '5MiB', '1000', '2.5G'). Pre-process the string to remove any unexpected characters if necessary. Example: `Float('1.2k')` or `Float('5MiB')`
TypeError: unsupported operand type(s) for *: 'prefixed.Float' and 'some_other_type'
You are attempting an arithmetic operation between a `prefixed.Float` object and an incompatible type that does not support the operation, such as a non-numeric custom object or potentially a `decimal.Decimal` without explicit conversion.
fix
Ensure that operations involving `prefixed.Float` are performed with standard numeric types (`int`, `float`) or other `prefixed.Float` instances. If operating with a special numeric type like `decimal.Decimal`, convert one of the operands to be compatible, e.g., `prefixed.Float(my_decimal_value)` or `float(my_prefixed_float) * my_decimal_value`.
Upgrade
Version history
0.9.0latest on PyPI · released Sep 2, 2024
Audit
Dependencies
pythonrequiredCore runtime dependency; the library is implemented in Python.
Agent activity
7 hits · last 30 days
node
6
Resources
prefixed — pip install prefixed · libregistry