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.1.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.1-py3-none-any.whl (41.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dsiunits-3.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 9c39d752948d4bc1ea529a8e29d983ccc1590c41884b04bdfde57dc5fb1fa10a
MD5 ce68f29eda4ba04839c87a0a83a5ca76
BLAKE2b-256 bf1b6dc79c349cc969ba4e51f4629d41f7258325a4150bfa11e1a220aaaaf06a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dsiunits-3.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a1929dc4ef1e98e9cd63cfc594966080475bafcd3046dd133a09a42e0a27fff5
MD5 da0537251855e863db4a3a350c7104c3
BLAKE2b-256 8fd4b45e296002e44ba28a7194f0f0e7172869faa2b17a18a83e2c298de0ea8d

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