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 Units

A Python library for parsing, validating, and performing mathematical operations on SI units using the D-SI (Digital SI) and BIPM-RP (SI Reference Point) syntax.

The library allows:

  • Unit Parsing & Validation: Parse units from D-SI and BIPM-RP notation strings
  • Mathematical Operations: Perform multiplication, division, and power operations on units while preserving dimensional consistency
  • Multiple Output Formats: Convert units to LaTeX, UTF-8, and SIRP representations
  • Scale Factor Calculations: Calculate conversion factors between compatible units
  • Base Unit Conversions: Convert complex units to their base SI unit representation

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

Installation

pip install dsiUnits

Requirements: Python 3.9+

Quick Start

from dsi_unit import DsiUnit

# Create units from D-SI notation
meter = DsiUnit(r"\metre")
second = DsiUnit(r"\second")
kilogram = DsiUnit(r"\kilogram")

# Or from BIPM-RP notation
velocity = DsiUnit("m.s-1")  # meters per second

Usage Examples

Basic Unit Operations

from dsi_unit import DsiUnit

# Mathematical operations
velocity = DsiUnit(r"\metre") / DsiUnit(r"\second")
print(velocity)  # \metre\per\second

acceleration = velocity / DsiUnit(r"\second")
print(acceleration)  # \metre\per\second\tothe{2}

force = DsiUnit(r"\kilogram") * acceleration
print(force)  # \kilogram\metre\per\second\tothe{2}

# Power operations
area = DsiUnit(r"\metre") ** 2
print(area)  # \metre\tothe{2}

Unit Conversions

# Check if units are scalable (convertible)
joule = DsiUnit(r"\joule")
watt_second = DsiUnit(r"\watt") * DsiUnit(r"\second")
print(joule.is_scalable(watt_second))  # True

# Get scale factor between compatible units
km = DsiUnit(r"\kilo\metre")
m = DsiUnit(r"\metre")
scale = km.get_scale_factor(m)  # 0.001 (1 km = 1000 m, so 1 km * 0.001 = 1 m)

# Convert to base units
newton = DsiUnit(r"\newton")
base = newton.to_base_unit_tree()
print(base)  # \kilogram\metre\per\second\tothe{2}

Output Formats

unit = DsiUnit(r"\kilo\metre\tothe{2}\per\second")

# LaTeX representation
print(unit.to_latex())  # $$\frac{\mathrm{k}\mathrm{m}^{2}}{\mathrm{s}}$$

# UTF-8 representation
print(unit.to_utf8())  # km²/s

# SIRP (SI Reference Point) representation
print(unit.to_sirp())  # km2.s-1

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

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.1.0.tar.gz (53.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.1.0-py3-none-any.whl (42.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: dsiunits-3.1.0.tar.gz
  • Upload date:
  • Size: 53.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.1.0.tar.gz
Algorithm Hash digest
SHA256 a8844d24687c870a25e6d930eeb68867175135232820fcadf1bf8c6b737aeeaa
MD5 73ae9a559d6a35b920cd1568eb8b5932
BLAKE2b-256 4750699a6416759fc4176c7f84000ad31a3a9ec799604233fd1e35d0097a44c2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: dsiunits-3.1.0-py3-none-any.whl
  • Upload date:
  • Size: 42.7 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9640c10776435b301210e758293e1241b3228aa5a3593d29c22df8661cdc40e9
MD5 1e6afb51a4d96c655207101aa20d9786
BLAKE2b-256 2258220bbd4b33365153e9285ce1dd9bcc77eef9c3fbd8666cc350d91e2c3d24

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