A basic python package that converts numerical strings with units to base units
Project description
SI-Base python package
A basic python package that converts numerical strings with units to base units.
Examples:
from sibase import Value, Unit
value = Value('3e5 nm/ps') # 3e+08 m/s
# value is instance of float with value if 3e8
float_value = value.to('km/s') # 300000.0
# shorthand method:
float_value = value @ 'km/s'
# Converting with units:
float_value = Unit('km/s').convert(3e8)
# shorthand methods:
float_value = 3e8 @ Unit('km/s')
float_value = Unit('km/s') @ 3e8
# Operations
# Note that this only compares numerical values, not units
Value('50 km') > '1e6 mm' # True
Value('50 km') < '1e8 mm' # True
Value('50 /km') == '50 km^-1' # True
Value('50 km') + '50 km' # 1e5 m (returns Value object, keeps units from LHS)
Value('50 km') - '1e4 m' # 4e4 m (returns Value object, keeps units from LHS)
Value('50 km') * '100 m' # 5e6 m^2 (returns Value object, unit updated)
Value('1 km') / '50 m' # 20 (returns Value object, this case unitless)
Value('1 km') / '50 s' # 20 m/s (returns Value object, units updated)
# Invert operation returns string with original units
~Value('50 km') # 50 km
Value('50 km').original() # 50 km
# Reduces same unit and prefix values, useful for multiply/divide operations
Value('17 ps/nm/km/km').original(simplify=True) # 17 ps/nm/km^2
Superscripts are enabled by default
from sibase import Value, Unit
Unit.USE_SUBSCRIPTS = False # Disable superscripts for units
Value('300mm^2/ps') # 3e+08 m²s⁻¹
Supports converting units with powers such as:
from sibase import Value
Value('-21 ps^2/km') # -2.1e-26 s^2/m
Value('17 ps/nm/km') # 1.7e-05 s/m^2
# decibels are treated as special unit for now, probably will be changed in future
Value('0.2 dB/km') # 2e-4 /m
Numpy, probably could also applied for other libraries
from sibase import to_base, Unit
import numpy as np
np_array = np.array([...])
# Convert from nonstandard:
si_array = to_base(np_array, 'km/s')
# Convert to other units
not_base_array = Unit('km/s').convert(si_array)
# shorthand method (will not work other way around for numpy arrays):
not_base_array = Unit('km/s') @ si_array
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
sibase-0.1.7.tar.gz
(6.6 kB
view details)
File details
Details for the file sibase-0.1.7.tar.gz
.
File metadata
- Download URL: sibase-0.1.7.tar.gz
- Upload date:
- Size: 6.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9fc43e9863212f82cd780e0e79f972204f4b52e1f08d909c0c632ea9bae217bb |
|
MD5 | 711e613744049faaeb486b545655912b |
|
BLAKE2b-256 | a87ea53fed94cb7e631c80224ce999688508252d2d8920499873b646450386ed |