.. start-badges
| |docs| |gh_actions| |codecov|
| |pypi| |supported-versions| |supported-implementations|
.. |docs| image:: https://img.shields.io/readthedocs/prefixed.svg?style=plastic&logo=read-the-docs
:target: https://prefixed.readthedocs.org
:alt: Documentation Status
.. |gh_actions| image:: https://img.shields.io/github/actions/workflow/status/Rockhopper-Technologies/prefixed/tests.yml?event=push&logo=github-actions&style=plastic
:target: https://github.com/Rockhopper-Technologies/prefixed/actions/workflows/tests.yml
:alt: GitHub Actions Status
.. |travis| image:: https://img.shields.io/travis/com/Rockhopper-Technologies/prefixed.svg?style=plastic&logo=travis
:target: https://travis-ci.com/Rockhopper-Technologies/prefixed
:alt: Travis-CI Build Status
.. |codecov| image:: https://img.shields.io/codecov/c/github/Rockhopper-Technologies/prefixed.svg?style=plastic&logo=codecov
:target: https://codecov.io/gh/Rockhopper-Technologies/prefixed
:alt: Coverage Status
.. |pypi| image:: https://img.shields.io/pypi/v/prefixed.svg?style=plastic&logo=pypi
:alt: PyPI Package latest release
:target: https://pypi.python.org/pypi/prefixed
.. |supported-versions| image:: https://img.shields.io/pypi/pyversions/prefixed.svg?style=plastic&logo=pypi
:alt: Supported versions
:target: https://pypi.python.org/pypi/prefixed
.. |supported-implementations| image:: https://img.shields.io/pypi/implementation/prefixed.svg?style=plastic&logo=pypi
:alt: Supported implementations
:target: https://pypi.python.org/pypi/prefixed
.. end-badges
Overview
========
Prefixed provides an alternative implementation of the built-in float_ which supports
formatted output with `SI (decimal)`_ and `IEC (binary)`_ prefixes.
.. code-block:: python
>>> from prefixed import Float
>>> f'{Float(3250):.2h}'
'3.25k'
>>> '{:.2h}s'.format(Float(.00001534))
'15.34μs'
>>> '{:.2k}B'.format(Float(42467328))
'40.50MiB'
>>> f'{Float(2048):.2m}B'
'2.00KB'
Because `prefixed.Float`_ inherits from the built-in float_, it behaves
exactly the same in most cases.
When a math operation is performed with another real number type
(float_, int_), the result will be a `prefixed.Float`_ instance.
Presentation Types
^^^^^^^^^^^^^^^^^^
Additional presentation types ``'h'``, ``'H'``, ``'k'``, ``'K'``,
``'m'``, and ``'M'`` are supported for f-strings and `format()`_.
+---------+-------------------------------------------------------------------+
| Type | Meaning |
+=========+===================================================================+
| ``'h'`` | SI format. Outputs the number with closest divisible SI prefix. |
| | (k, M, G, ...) |
+---------+-------------------------------------------------------------------+
| ``'H'`` | Same as ``'h'`` with precision indicating significant digits. |
+---------+-------------------------------------------------------------------+
| ``'k'`` | IEC Format. Outputs the number with closest divisible IEC prefix. |
| | (Ki, Mi, Gi, ...) |
+---------+-------------------------------------------------------------------+
| ``'K'`` | Same as ``'k'`` with precision indicating significant digits. |
+---------+-------------------------------------------------------------------+
| ``'m'`` | Short IEC Format. Same as ``'k'`` but only a single character. |
| | (K, M, G, ...) |
+---------+-------------------------------------------------------------------+
| ``'M'`` | Same as ``'m'`` with precision indicating significant digits. |
+---------+-------------------------------------------------------------------+
| | |
+---------+-------------------------------------------------------------------+
| ``'j'`` | Alias for ``'k'`` - DEPRECATED |
+---------+-------------------------------------------------------------------+
| ``'J'`` | Alias for ``'m'`` - DEPRECATED |
+---------+-------------------------------------------------------------------+
String Initialization
^^^^^^^^^^^^^^^^^^^^^
When initializing from strings, SI and IEC prefixes are honored
.. code-block:: python
>>> Float('2k')
Float(2000.0)
>>> Float('2Ki')
Float(2048.0)
Additional Flags
^^^^^^^^^^^^^^^^
An additional format flag '!' is available which adds a space before the prefix
.. code-block:: python
>>> f'{Float(3250):!.2h}'
'3.25 k'
Significant Digits
^^^^^^^^^^^^^^^^^^
When the ``'H'``, ``'K``, or ``'M'`` presentation types are used, precision is treated as
the number of `significant digits`_ to include. Standard rounding will occur for the final digit.
.. code-block:: python
>>> f'{Float(1246):.3h}'
'1.246k'
>>> f'{Float(1246):.3H}'
'1.25k'
By default, trailing zeros are removed.
.. code-block:: python
>>> f'{Float(1000):.3H}'
'1k'
To preserve trailing zeros, include the ``'#'`` flag.
.. code-block:: python
>>> f'{Float(1000):#.3H}'
'1.00k'
Adjustable Thresholds
^^^^^^^^^^^^^^^^^^^^^
An additional field, margin, can be specified which lowers or raises the threshold for
for each prefix by the given percentage.
Margin is specified before precision with the syntax ``%[-]digit+``.
.. code-block:: python
>>> f'{Float(950):.2h}'
'950.00'
>>> f'{Float(950):%-5.2h}'
'0.95k'
>>> f'{Float(1000):%5.2h}'
'1000.00'
>>> f'{Float(1050):%5.2h}'
'1.05k'
.. _SI (decimal): https://en.wikipedia.org/wiki/Metric_prefix
.. _IEC (binary): https://en.wikipedia.org/wiki/Binary_prefix
.. _signifigant digits: https://en.wikipedia.org/wiki/Significant_figures
Supported Prefixes
==================
SI (Decimal) Prefixes
^^^^^^^^^^^^^^^^^^^^^
+--------+--------+----------+
| Prefix | Name | Base |
+========+========+==========+
| Q | Quetta | |10^30| |
+--------+--------+----------+
| R | Ronna | |10^27| |
+--------+--------+----------+
| Y | Yotta | |10^24| |
+--------+--------+----------+
| Z | Zetta | |10^21| |
+--------+--------+----------+
| E | Exa | |10^18| |
+--------+--------+----------+
| P | Peta | |10^15| |
+--------+--------+----------+
| T | Tera | |10^12| |
+--------+--------+----------+
| G | Giga | |10^9| |
+--------+--------+----------+
| M | Mega | |10^6| |
+--------+--------+----------+
| k | Kilo | |10^3| |
+--------+--------+----------+
| m | Milli | |10^-3| |
+--------+--------+----------+
| μ | Micro | |10^-6| |
+--------+--------+----------+
| n | Nano | |10^-9| |
+--------+--------+----------+
| p | Pico | |10^-12| |
+--------+--------+----------+
| f | Femto | |10^-15| |
+--------+--------+----------+
| a | Atto | |10^-18| |
+--------+--------+----------+
| z | Zepto | |10^-21| |
+--------+--------+----------+
| y | Yocto | |10^-24| |
+--------+--------+----------+
| r | Ronto | |10^-27| |
+--------+--------+----------+
| q | Quecto | |10^-30| |
+--------+--------+----------+
IEC (Binary) Prefixes
^^^^^^^^^^^^^^^^^^^^^
+--------+------+--------+
| Prefix | Name | Base |
+========+======+========+
| Y | Yobi | |2^80| |
+--------+------+--------+
| Z | Zebi | |2^70| |
+--------+------+--------+
| E | Exbi | |2^60| |
+--------+------+--------+
| P | Pedi | |2^50| |
+--------+------+--------+
| T | Tebi | |2^40| |
+--------+------+--------+
| G | Gibi | |2^30| |
+--------+------+--------+
| M | Mebi | |2^20| |
+--------+------+--------+
| K | Kibi | |2^10| |
+--------+------+--------+
.. _SI (decimal): https://en.wikipedia.org/wiki/Metric_prefix
.. _IEC (binary): https://en.wikipedia.org/wiki/Binary_prefix
.. _float: https://docs.python.org/3/library/functions.html#float
.. _int: https://docs.python.org/3/library/functions.html#int
.. _prefixed.Float: https://prefixed.readthedocs.io/en/stable/api.html#prefixed.Float
.. _format(): https://docs.python.org/3/library/functions.html#format
.. |10^30| replace:: 10\ :sup:`30`\
.. |10^27| replace:: 10\ :sup:`27`\
.. |10^24| replace:: 10\ :sup:`24`\
.. |10^21| replace:: 10\ :sup:`21`\
.. |10^18| replace:: 10\ :sup:`18`\
.. |10^15| replace:: 10\ :sup:`15`\
.. |10^12| replace:: 10\ :sup:`12`\
.. |10^9| replace:: 10\ :sup:`9`\
.. |10^6| replace:: 10\ :sup:`6`\
.. |10^3| replace:: 10\ :sup:`3`\
.. |10^-3| replace:: 10\ :sup:`-3`\
.. |10^-6| replace:: 10\ :sup:`-6`\
.. |10^-9| replace:: 10\ :sup:`-9`\
.. |10^-12| replace:: 10\ :sup:`-12`\
.. |10^-15| replace:: 10\ :sup:`-15`\
.. |10^-18| replace:: 10\ :sup:`-18`\
.. |10^-21| replace:: 10\ :sup:`-21`\
.. |10^-24| replace:: 10\ :sup:`-24`\
.. |10^-27| replace:: 10\ :sup:`-27`\
.. |10^-30| replace:: 10\ :sup:`-30`\
.. |2^80| replace:: 2\ :sup:`80`\
.. |2^70| replace:: 2\ :sup:`70`\
.. |2^60| replace:: 2\ :sup:`60`\
.. |2^50| replace:: 2\ :sup:`50`\
.. |2^40| replace:: 2\ :sup:`40`\
.. |2^30| replace:: 2\ :sup:`30`\
.. |2^20| replace:: 2\ :sup:`20`\
.. |2^10| replace:: 2\ :sup:`10`\
Raw data
{
"_id": null,
"home_page": "https://github.com/Rockhopper-Technologies/prefixed",
"name": "prefixed",
"maintainer": "Avram Lubkin",
"docs_url": null,
"requires_python": null,
"maintainer_email": "avylove@rockhopper.net",
"keywords": "si iec prefix nist",
"author": "Avram Lubkin",
"author_email": "avylove@rockhopper.net",
"download_url": "https://files.pythonhosted.org/packages/30/c9/0ef2519396b400387fc870efbd775f613410b4e55b8e7d24fd13f0b475b8/prefixed-0.8.0.tar.gz",
"platform": null,
"description": ".. start-badges\n\n| |docs| |gh_actions| |codecov|\n| |pypi| |supported-versions| |supported-implementations|\n\n.. |docs| image:: https://img.shields.io/readthedocs/prefixed.svg?style=plastic&logo=read-the-docs\n :target: https://prefixed.readthedocs.org\n :alt: Documentation Status\n\n.. |gh_actions| image:: https://img.shields.io/github/actions/workflow/status/Rockhopper-Technologies/prefixed/tests.yml?event=push&logo=github-actions&style=plastic\n :target: https://github.com/Rockhopper-Technologies/prefixed/actions/workflows/tests.yml\n :alt: GitHub Actions Status\n\n.. |travis| image:: https://img.shields.io/travis/com/Rockhopper-Technologies/prefixed.svg?style=plastic&logo=travis\n :target: https://travis-ci.com/Rockhopper-Technologies/prefixed\n :alt: Travis-CI Build Status\n\n.. |codecov| image:: https://img.shields.io/codecov/c/github/Rockhopper-Technologies/prefixed.svg?style=plastic&logo=codecov\n :target: https://codecov.io/gh/Rockhopper-Technologies/prefixed\n :alt: Coverage Status\n\n.. |pypi| image:: https://img.shields.io/pypi/v/prefixed.svg?style=plastic&logo=pypi\n :alt: PyPI Package latest release\n :target: https://pypi.python.org/pypi/prefixed\n\n.. |supported-versions| image:: https://img.shields.io/pypi/pyversions/prefixed.svg?style=plastic&logo=pypi\n :alt: Supported versions\n :target: https://pypi.python.org/pypi/prefixed\n\n.. |supported-implementations| image:: https://img.shields.io/pypi/implementation/prefixed.svg?style=plastic&logo=pypi\n :alt: Supported implementations\n :target: https://pypi.python.org/pypi/prefixed\n\n.. end-badges\n\n\nOverview\n========\n\nPrefixed provides an alternative implementation of the built-in float_ which supports\nformatted output with `SI (decimal)`_ and `IEC (binary)`_ prefixes.\n\n.. code-block:: python\n\n >>> from prefixed import Float\n\n >>> f'{Float(3250):.2h}'\n '3.25k'\n\n >>> '{:.2h}s'.format(Float(.00001534))\n '15.34\u03bcs'\n\n >>> '{:.2k}B'.format(Float(42467328))\n '40.50MiB'\n\n >>> f'{Float(2048):.2m}B'\n '2.00KB'\n\nBecause `prefixed.Float`_ inherits from the built-in float_, it behaves\nexactly the same in most cases.\n\nWhen a math operation is performed with another real number type\n(float_, int_), the result will be a `prefixed.Float`_ instance.\n\n\nPresentation Types\n^^^^^^^^^^^^^^^^^^\n\nAdditional presentation types ``'h'``, ``'H'``, ``'k'``, ``'K'``,\n``'m'``, and ``'M'`` are supported for f-strings and `format()`_.\n\n+---------+-------------------------------------------------------------------+\n| Type | Meaning |\n+=========+===================================================================+\n| ``'h'`` | SI format. Outputs the number with closest divisible SI prefix. |\n| | (k, M, G, ...) |\n+---------+-------------------------------------------------------------------+\n| ``'H'`` | Same as ``'h'`` with precision indicating significant digits. |\n+---------+-------------------------------------------------------------------+\n| ``'k'`` | IEC Format. Outputs the number with closest divisible IEC prefix. |\n| | (Ki, Mi, Gi, ...) |\n+---------+-------------------------------------------------------------------+\n| ``'K'`` | Same as ``'k'`` with precision indicating significant digits. |\n+---------+-------------------------------------------------------------------+\n| ``'m'`` | Short IEC Format. Same as ``'k'`` but only a single character. |\n| | (K, M, G, ...) |\n+---------+-------------------------------------------------------------------+\n| ``'M'`` | Same as ``'m'`` with precision indicating significant digits. |\n+---------+-------------------------------------------------------------------+\n| | |\n+---------+-------------------------------------------------------------------+\n| ``'j'`` | Alias for ``'k'`` - DEPRECATED |\n+---------+-------------------------------------------------------------------+\n| ``'J'`` | Alias for ``'m'`` - DEPRECATED |\n+---------+-------------------------------------------------------------------+\n\n\nString Initialization\n^^^^^^^^^^^^^^^^^^^^^\n\nWhen initializing from strings, SI and IEC prefixes are honored\n\n.. code-block:: python\n\n >>> Float('2k')\n Float(2000.0)\n\n >>> Float('2Ki')\n Float(2048.0)\n\n\nAdditional Flags\n^^^^^^^^^^^^^^^^\n\nAn additional format flag '!' is available which adds a space before the prefix\n\n.. code-block:: python\n\n >>> f'{Float(3250):!.2h}'\n '3.25 k'\n\n\nSignificant Digits\n^^^^^^^^^^^^^^^^^^\n\nWhen the ``'H'``, ``'K``, or ``'M'`` presentation types are used, precision is treated as\nthe number of `significant digits`_ to include. Standard rounding will occur for the final digit.\n\n.. code-block:: python\n\n >>> f'{Float(1246):.3h}'\n '1.246k'\n\n >>> f'{Float(1246):.3H}'\n '1.25k'\n\nBy default, trailing zeros are removed.\n\n.. code-block:: python\n\n >>> f'{Float(1000):.3H}'\n '1k'\n\nTo preserve trailing zeros, include the ``'#'`` flag.\n\n.. code-block:: python\n\n >>> f'{Float(1000):#.3H}'\n '1.00k'\n\n\nAdjustable Thresholds\n^^^^^^^^^^^^^^^^^^^^^\n\nAn additional field, margin, can be specified which lowers or raises the threshold for\nfor each prefix by the given percentage.\nMargin is specified before precision with the syntax ``%[-]digit+``.\n\n.. code-block:: python\n\n >>> f'{Float(950):.2h}'\n '950.00'\n\n >>> f'{Float(950):%-5.2h}'\n '0.95k'\n\n >>> f'{Float(1000):%5.2h}'\n '1000.00'\n\n >>> f'{Float(1050):%5.2h}'\n '1.05k'\n\n\n.. _SI (decimal): https://en.wikipedia.org/wiki/Metric_prefix\n.. _IEC (binary): https://en.wikipedia.org/wiki/Binary_prefix\n.. _signifigant digits: https://en.wikipedia.org/wiki/Significant_figures\n\n\nSupported Prefixes\n==================\n\nSI (Decimal) Prefixes\n^^^^^^^^^^^^^^^^^^^^^\n\n+--------+--------+----------+\n| Prefix | Name | Base |\n+========+========+==========+\n| Q | Quetta | |10^30| |\n+--------+--------+----------+\n| R | Ronna | |10^27| |\n+--------+--------+----------+\n| Y | Yotta | |10^24| |\n+--------+--------+----------+\n| Z | Zetta | |10^21| |\n+--------+--------+----------+\n| E | Exa | |10^18| |\n+--------+--------+----------+\n| P | Peta | |10^15| |\n+--------+--------+----------+\n| T | Tera | |10^12| |\n+--------+--------+----------+\n| G | Giga | |10^9| |\n+--------+--------+----------+\n| M | Mega | |10^6| |\n+--------+--------+----------+\n| k | Kilo | |10^3| |\n+--------+--------+----------+\n| m | Milli | |10^-3| |\n+--------+--------+----------+\n| \u03bc | Micro | |10^-6| |\n+--------+--------+----------+\n| n | Nano | |10^-9| |\n+--------+--------+----------+\n| p | Pico | |10^-12| |\n+--------+--------+----------+\n| f | Femto | |10^-15| |\n+--------+--------+----------+\n| a | Atto | |10^-18| |\n+--------+--------+----------+\n| z | Zepto | |10^-21| |\n+--------+--------+----------+\n| y | Yocto | |10^-24| |\n+--------+--------+----------+\n| r | Ronto | |10^-27| |\n+--------+--------+----------+\n| q | Quecto | |10^-30| |\n+--------+--------+----------+\n\nIEC (Binary) Prefixes\n^^^^^^^^^^^^^^^^^^^^^\n\n+--------+------+--------+\n| Prefix | Name | Base |\n+========+======+========+\n| Y | Yobi | |2^80| |\n+--------+------+--------+\n| Z | Zebi | |2^70| |\n+--------+------+--------+\n| E | Exbi | |2^60| |\n+--------+------+--------+\n| P | Pedi | |2^50| |\n+--------+------+--------+\n| T | Tebi | |2^40| |\n+--------+------+--------+\n| G | Gibi | |2^30| |\n+--------+------+--------+\n| M | Mebi | |2^20| |\n+--------+------+--------+\n| K | Kibi | |2^10| |\n+--------+------+--------+\n\n.. _SI (decimal): https://en.wikipedia.org/wiki/Metric_prefix\n.. _IEC (binary): https://en.wikipedia.org/wiki/Binary_prefix\n.. _float: https://docs.python.org/3/library/functions.html#float\n.. _int: https://docs.python.org/3/library/functions.html#int\n.. _prefixed.Float: https://prefixed.readthedocs.io/en/stable/api.html#prefixed.Float\n.. _format(): https://docs.python.org/3/library/functions.html#format\n\n.. |10^30| replace:: 10\\ :sup:`30`\\\n.. |10^27| replace:: 10\\ :sup:`27`\\\n.. |10^24| replace:: 10\\ :sup:`24`\\\n.. |10^21| replace:: 10\\ :sup:`21`\\\n.. |10^18| replace:: 10\\ :sup:`18`\\\n.. |10^15| replace:: 10\\ :sup:`15`\\\n.. |10^12| replace:: 10\\ :sup:`12`\\\n.. |10^9| replace:: 10\\ :sup:`9`\\\n.. |10^6| replace:: 10\\ :sup:`6`\\\n.. |10^3| replace:: 10\\ :sup:`3`\\\n.. |10^-3| replace:: 10\\ :sup:`-3`\\\n.. |10^-6| replace:: 10\\ :sup:`-6`\\\n.. |10^-9| replace:: 10\\ :sup:`-9`\\\n.. |10^-12| replace:: 10\\ :sup:`-12`\\\n.. |10^-15| replace:: 10\\ :sup:`-15`\\\n.. |10^-18| replace:: 10\\ :sup:`-18`\\\n.. |10^-21| replace:: 10\\ :sup:`-21`\\\n.. |10^-24| replace:: 10\\ :sup:`-24`\\\n.. |10^-27| replace:: 10\\ :sup:`-27`\\\n.. |10^-30| replace:: 10\\ :sup:`-30`\\\n\n.. |2^80| replace:: 2\\ :sup:`80`\\\n.. |2^70| replace:: 2\\ :sup:`70`\\\n.. |2^60| replace:: 2\\ :sup:`60`\\\n.. |2^50| replace:: 2\\ :sup:`50`\\\n.. |2^40| replace:: 2\\ :sup:`40`\\\n.. |2^30| replace:: 2\\ :sup:`30`\\\n.. |2^20| replace:: 2\\ :sup:`20`\\\n.. |2^10| replace:: 2\\ :sup:`10`\\\n",
"bugtrack_url": null,
"license": "MPLv2.0",
"summary": "Prefixed alternative numeric library",
"version": "0.8.0",
"project_urls": {
"Documentation": "https://prefixed.readthedocs.io",
"Homepage": "https://github.com/Rockhopper-Technologies/prefixed"
},
"split_keywords": [
"si",
"iec",
"prefix",
"nist"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "5a1065bc09b3012728eac710e6e5676b0b69af7a6fd1404ace03637f7b14e678",
"md5": "da0e17b43543e38f295c26bd5858e954",
"sha256": "d78fe17bbed8b2e8eb07ed9e611961c96338b4135b45fdd2bd6a3113f5a9f9cf"
},
"downloads": -1,
"filename": "prefixed-0.8.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "da0e17b43543e38f295c26bd5858e954",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 13193,
"upload_time": "2024-08-09T12:49:33",
"upload_time_iso_8601": "2024-08-09T12:49:33.607785Z",
"url": "https://files.pythonhosted.org/packages/5a/10/65bc09b3012728eac710e6e5676b0b69af7a6fd1404ace03637f7b14e678/prefixed-0.8.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "30c90ef2519396b400387fc870efbd775f613410b4e55b8e7d24fd13f0b475b8",
"md5": "8d1dfa6a4a8d6c8ece3c8630c54a8343",
"sha256": "135b93d4d18a2ca49243cc5b62f7a02969759e170135cd0121759f4a666cd369"
},
"downloads": -1,
"filename": "prefixed-0.8.0.tar.gz",
"has_sig": false,
"md5_digest": "8d1dfa6a4a8d6c8ece3c8630c54a8343",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 97039,
"upload_time": "2024-08-09T12:49:35",
"upload_time_iso_8601": "2024-08-09T12:49:35.079352Z",
"url": "https://files.pythonhosted.org/packages/30/c9/0ef2519396b400387fc870efbd775f613410b4e55b8e7d24fd13f0b475b8/prefixed-0.8.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-09 12:49:35",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Rockhopper-Technologies",
"github_project": "prefixed",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "prefixed"
}