Skip to main content

stringunitconverter

PyPI version coverage License: MIT downloads

Goal

Get the multiplication factor required to convert from one unit to another, with the units being given as strings. In short: make it easy to convert between units.

Get it

Install with pip using pip install stringunitconverter. For more information, please see the PyPI page.

Main concepts

  • Use strings to specify units (e.g. 'kN').
  • Allow quasi all mathematical combinations of units (e.g. 'N/m^2'). Math execution order rules should be respected.
  • Keep code short (~100 LOC) and unit-independent and put all unit-related data in a data file (JSON). It should be easy to add units.
  • Focus lies on engineering applications. Currency conversion etc. is not supported.
  • Working concept:
    1. Keep a data file with a multiplier for each individual unit to its SI base unit(s). For example: ft (to m): 0.3048, mmHg (to Pa): 0.00750.
    2. Use this datafile to get the multiplier for each given side to base SI. This is done by substituting each prefix and unit in the string with the appropriate numerical value. The equation, containing only mathematical operations and numbers, is then evaluated, returning the multiplier.
    3. The multiplier for the conversion follows from dividing the two derived multipliers.

Limits and notes

  • Dimensional correctness is not checked.
  • Conversions are always multiplicative. Therefore, conversions between coordinate systems with a different origin are not (yet) supported. For example, between °C and K. (For this reason, °C and °F are deliberately unsupported; use K.) Conversions between Hz and s are therefore also not possible, as this would require inverting the value.
  • SI style conventions are followed. Some notes:
    • A multiplication is defined using a space or a multiplication dot ·. The asterisk * (Python multiplication symbol) is also supported
    • Units not separated by a space or multiplication dot (e.g. 'Nm' for 'N·m') are not in line with SI style conventions and are not supported by this parser.
  • Power superscripts ² and ³ are supported, for example mm².

Supported prefixes and units

Prefixes: y to Y.
Units:

  • SI: g, N, s, Pa, bar, A, K, mol, Hz, J, W, C, V, Ohm, S, F, L, rad
  • Imperial: oz, psi, mi, ft, in, mil, mph
  • Nautical: (NM, nmi), (kn, kt)
  • other: mmHg, gf, (°, deg, degree)
  • also supported: %

Functions and examples

def add_unit(unit: str, multiplier: str)

def add_json(filename: str, add_to_existing=False)

def multiplier(a: str, b: str) -> float)

def get_factor(a: str, unsafe=False) -> float)

def unit_to_factor_string(a: str) -> str

For documentation of these functions: please see their help strings.

Two usage examples:

>>> import stringunitconverter as suc
>>>
>>> suc.multiplier('N/m^2', 'kN/cm^2')
10.0
>>> suc.multiplier('1/kPa', '1/(N/m^2)')
0.001

Contribution and development

The Git remote repository is hosted on GitLab.

Testing

Tests are in the directory tests in the root directory. To run the tests, navigate to the root directory and run pytest.

This requires a local copy of the repo. Note that the tests will be run on the stringunitconverter Python package. This package may or may not point to the code in the local copy of the repo, depending on how the package was installed.

To get this repo locally

  1. Clone the repo. The directory with the project will be located in the current working directory of the terminal.
    $ git clone https://gitlab.com/abaeyens/stringunitconverter.git
    
  2. Navigate to the created directory.
    $ cd stringunitconverter
    

Contact

Have a question, suggestion, ... ? Your feedback on this project is always appreciated! Send an e-mail to mail@arnebaeyens.com.

It is also possible to use the GitLab issues webpage.

Metadata

Release files for stringunitconverter 0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stringunitconverter 0.4
File Size Uploaded Provenance Actions
stringunitconverter-0.4.tar.gz 9.7 kB Without provenance Details for stringunitconverter-0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for stringunitconverter 0.4
File Interpreter ABI Platform Actions
stringunitconverter-0.4-py3-none-any.whl Python 3 none any Details for stringunitconverter-0.4-py3-none-any.whl

Total release size: 17.5 kB

Release files / stringunitconverter-0.4.tar.gz

Download URL stringunitconverter-0.4.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
ff85dc967e8aee97826ea45bb3557070a63d7863b0e0b43b9d42e48d847edb9d
BLAKE2b-256 checksum
How to use checksums
78dc61287aeee822097dbbf7b26ffaf3d3bc23bafd4fec44139f04251a805492
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.10

Release files / stringunitconverter-0.4-py3-none-any.whl

Download URL stringunitconverter-0.4-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c30ea855106bd119868a33b18e47d51a404085b3b618a271a8b8b4b097aee346
BLAKE2b-256 checksum
How to use checksums
a896a0f30ac5ca5d1725dfe43aa4cd11a84b9c7dd1edc0ec5db9ffeabd0e893d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.10

Security for stringunitconverter 0.4

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page