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 checken weather teh can be converted into each other with scalar multiplication

Javascript version

The folder dsiUnits-js contains an Javascript version of this libary that can be used for a D-SI-Unit-Input Element with autosugestion. And a dsiUnit CLass that supports parsing and rendering (HTML converion) of the dsi Units.

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:

  • toLatex(): returns the Latex representation of the unit
  • toUTF8(): returns the UTF8 representation of the unit
  • isScalablyEqualTo(other): checks whether the unit is equal to another unit with scalar multiplication
  • toSIRP(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

  • toBaseUnitTree(): returns the base unit tree of the unit

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

  • sortTree(): sorts the base unit tree of the unit

from dsiUnits import dsiUnit

unit = dsiUnit('\metre\second\tothe{-1}')
latexStr=unit.toLatex()
print(latexStr)
from dsiUnits import dsiUnit

mps = dsiUnit(r'\metre\second\tothe{-1}')
kmh = dsiUnit(r'\kilo\metre\per\hour')
scaleFactor, baseUnit = mps.isScalablyEqualTo(kmh)
print("The unit "+str(mps)+" is equal to "+str(kmh)+" with a factor of "+scaleFactor+" and base unit "+str(baseUnit))

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: dsiunits-2.5.2.tar.gz
  • Upload date:
  • Size: 51.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dsiunits-2.5.2.tar.gz
Algorithm Hash digest
SHA256 08de106253daf4b20c8ac1b76c3652aaf952049c5b7e4c2187ee3cc128339ef3
MD5 37d6980bc4528761aadde258fa3d68f3
BLAKE2b-256 0c0779d0be102905cfe357215b9afb245db6253effdabd99d8604715f7d10320

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dsiunits-2.5.2-py3-none-any.whl
  • Upload date:
  • Size: 42.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for dsiunits-2.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 899a611d31895482f734de5d000a0cca71d9635ff07b925b8a8d76989481258f
MD5 397ed64aac886b707ad2dcf135a95f01
BLAKE2b-256 b858b1ab788748fac15af7c01745b05e6d19c37e07244f6e2273cd6b548652c6

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