Skip to main content

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 a symbol to describe it.
  • Dimension - subclass of Unit 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unitmeasure-1.0.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

unitmeasure-1.0.0-py3-none-any.whl (23.1 kB view details)

Uploaded Python 3

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

Hashes for unitmeasure-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7fd3f93785f3fdbc10897f80772cc8644bd32dc77e164d2e749ef8344c54a0e6
MD5 19b892182b6464502d7aa88902c91a8c
BLAKE2b-256 99eefe14afcc92813d6930947a8e519cc800072725b743f3204d4813544a79ef

See more details on using hashes here.

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

Hashes for unitmeasure-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dff1ecb687fd376442d7fc11c001cb5b0821570aa297d1c6e43b4975c874f8c1
MD5 db4b201427476531f3f877b004b2e2c4
BLAKE2b-256 3681f19ce6313da72ca28f7dd4ed70937c21f829e30fbb71ddc767ee2496b7bf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page