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.

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.0.tar.gz (51.3 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.0-py3-none-any.whl (41.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dsiunits-3.0.0.tar.gz
  • Upload date:
  • Size: 51.3 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.0.tar.gz
Algorithm Hash digest
SHA256 c4beed2c04a2ea0c7fe623668986e3b572d0bc1e91f3dc89c7eab57bffee4815
MD5 ae23853c57504faf9cf91dcd12f3b451
BLAKE2b-256 dd73a3deb008f9e8636ded51953783307444cdd96f27c0155e2072ebbc8fe121

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dsiunits-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 41.0 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7929a724e5b4c5c69a13d78e79e4af7d8ce8411f7df2c472eefa52ac384b51c9
MD5 2111e229fb73dcd10867cad726115275
BLAKE2b-256 f73e005ebded41452ea4255617491efac7215fa83a549dd4dcfea6738bbd7189

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