Skip to main content

This is a Python module for handling the SI units as objects in Python, parsing them from strings and converting them to Latex and Unicode, as well as performing math operations and calculating scale factors.

Project description

D-SI Parser

This library converts D-SI unit strings to Latex. And is able to perform math operations *, / and power with the D-SI units as well as checked weather teh can be converted into each other with scalar multiplication.

Javascript version

The Javascript version of this library has moved to https://gitlab1.ptb.de/digitaldynamicmeasurement/dsiunits-js/

Installation

pip install dsiUnits

Usage

The Constructor DsiUnit(str) will parse the string and create a DsiUnit object. BIMP-SI-RP strings are also supported and will be converted to D-SI units. The DsiUnit object has the following methods:

  • to_latex(): returns the Latex representation of the unit.
  • to_utf8(): returns the UTF8 representation of the unit.
  • is_scalably_equal_to(other): checks whether the unit is equal to another unit with scalar multiplication.
  • to_sirp(pid=False): returns the SIRP representation of the unit. If pid is true the PID as URL is returned.

And following magic functions:

  • __mul__(other): "*" multiplies the unit with another unit or a scalar

  • __truediv__(other): "/" divides the unit by another unit or a scalar

  • __pow__(other): "**" raises the unit to the power of another unit or a scalar

  • __eq__(other): "==" checks whether the unit is equal to another unit

  • __str__: "str()" returns the string representation of the unit

  • __repr__: returns the string representation of the unit

  • to_base_unit_tree(): returns the base unit tree of the unit.

  • reduce_fraction(): reduces the fraction of the unit by resolving all \per and combining same units by exponent addition.

  • sort_tree(): sorts the base unit tree of the unit.

from dsi_unit import DsiUnit

unit = DsiUnit(r'\metre\second\tothe{-1}')
latexStr = unit.to_latex()
print(latexStr)
from dsi_unit import DsiUnit

mps = DsiUnit(r'\metre\second\tothe{-1}')
kmh = DsiUnit(r'\kilo\metre\per\hour')
scale_factor, base_unit = mps.is_scalably_equal_to(kmh)
print(
    f"The unit {mps} is equal to {kmh} with a factor of {scale_factor} and base unit {base_unit}"
)

For more usage examples see the Example Notebook, as well as the pytest file.

D-SI unit regex

This project also generates a RegEx that can be used to test whether a string conforms to the syntax of D-SI units. You can find the up-to-date text files here: https://gitlab1.ptb.de/digitaldynamicmeasurement/dsiUnits/-/jobs/artifacts/main/browse?job=generate_regex.

More details on the RegEx generator, including documentation of the different variants, can be found in the doc/ folder: English, German

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

dsiunits-3.0.2.tar.gz (52.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dsiunits-3.0.2-py3-none-any.whl (41.9 kB view details)

Uploaded Python 3

File details

Details for the file dsiunits-3.0.2.tar.gz.

File metadata

  • Download URL: dsiunits-3.0.2.tar.gz
  • Upload date:
  • Size: 52.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for dsiunits-3.0.2.tar.gz
Algorithm Hash digest
SHA256 f5f9ce841cc773f72b08729847ea313df475e9693315943d5c40cfcf3e511972
MD5 b73511f667c7154500741bcd58b30101
BLAKE2b-256 558a4f623aa91b0bf9b58f546523ba5f45d2588835ad69b6541ca7efe0541a48

See more details on using hashes here.

File details

Details for the file dsiunits-3.0.2-py3-none-any.whl.

File metadata

  • Download URL: dsiunits-3.0.2-py3-none-any.whl
  • Upload date:
  • Size: 41.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for dsiunits-3.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f867138be94e4fde3e5b571d2a3241400cc3d7d067108bdf0b8d61b9cc46ee44
MD5 17bda8dfcfb70530a27c157a577401b9
BLAKE2b-256 09b5fe62ef24e99fd029f96643e9f646fdaca4dcc1cc26747feaa99a21dd9325

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page