Python units and measurements
Project description
unitmeasure: Swift units and measurement in Python
unitmeasure is a simple Python library that tries to implement the features of Swift's Units and Measurement framework.
With unitmeasure, you can label numeric quantities with physical dimensions and convert between related units.
Contributing
For anyone looking to contribute, you can look at doing any of the following:
- Submit bugs and feature requests
- Review any documentation and create pull requests for anything from typos to new content.
Installation
unitmeasure can be installed from PyPI:
pip install unitmeasure
Overview
Base available classes
The basic available classes should match the classes found in swift.
Unit
- represents a unit that has asymbol
to describe it.Dimension
- subclass ofUnit
and has multiple child classes defining different dimensions.Measurement
- Immutable. wraps a number with a unit. Supports comparison operators, addition and subtraction with another measurement, and multiplication and division with a scalar value.
Supported Units
All supported units can be found in the units sub directory. Any unit class can be imported directly from unitmeasure.
Examples
for more detailed usage see unit tests.
Create a measurement
import unitmeasure
dur = unitmeasure.Measurement(value=2,
unit=unitmeasure.UnitDuration.minutes)
print(dur)
Convert a measurement
import unitmeasure
dur = unitmeasure.Measurement(value=2,
unit=unitmeasure.UnitDuration.minutes)
dur.convert(unitmeasure.UnitDuration.seconds)
print(dur)
Convert a measurement and return new object
import unitmeasure
minutes = unitmeasure.Measurement(value=2,
unit=unitmeasure.UnitDuration.minutes)
seconds = minutes.converted(unitmeasure.UnitDuration.seconds)
print(minutes)
print(seconds)
add measurements
import unitmeasure
minutes = unitmeasure.Measurement(value=2,
unit=unitmeasure.UnitDuration.minutes)
seconds = minutes.converted(unitmeasure.UnitDuration.seconds)
# adding measurements will automagically convert the measurement into its base unit.
print(minutes + seconds)
Create a custom Unit
import unitmeasure
customLengthUnit = unitmeasure.UnitLength(symbol="FLARB", coefficient=2.0)
customLength = Measurement(value=1, unit=customLengthUnit)
meters = customLength.converted(to: unitmeasure.UnitLength.meters)
Converting to and from JSON
NOTE: JSON struct should match swift's so it should be possible to import json data to/from swift.
import unitmeasure
seconds = unitmeasure.Measurement(value=2,
unit=unitmeasure.UnitDuration.seconds)
with open("measurement.json", "w") as f:
f.write(unitmeasure.to_json(seconds))
with open("measurement.json", "r") as f:
# if a dimension is not specified you will end up with
# a measurement that has a basic unit (only a symbol)
loaded_measurement = unitmeasure.from_json(f.read(), dimension=unitmeasure.UnitDuration)
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
Built Distribution
File details
Details for the file unitmeasure-1.0.0.tar.gz
.
File metadata
- Download URL: unitmeasure-1.0.0.tar.gz
- Upload date:
- Size: 14.1 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.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7fd3f93785f3fdbc10897f80772cc8644bd32dc77e164d2e749ef8344c54a0e6 |
|
MD5 | 19b892182b6464502d7aa88902c91a8c |
|
BLAKE2b-256 | 99eefe14afcc92813d6930947a8e519cc800072725b743f3204d4813544a79ef |
File details
Details for the file unitmeasure-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: unitmeasure-1.0.0-py3-none-any.whl
- Upload date:
- Size: 23.1 kB
- Tags: Python 3
- 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.9.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dff1ecb687fd376442d7fc11c001cb5b0821570aa297d1c6e43b4975c874f8c1 |
|
MD5 | db4b201427476531f3f877b004b2e2c4 |
|
BLAKE2b-256 | 3681f19ce6313da72ca28f7dd4ed70937c21f829e30fbb71ddc767ee2496b7bf |