Skip to main content

an unit converter based on graph network and classes to operate with units.

Project description

Run tests PyPI version PyPI versions PyPI license

unyts

I identified the opportunity to use a directed graph (digraph) network to build a unit converter capable of converting between any two units without the need to populate an endless table of predefined conversions. Powered by the Breadth-First Search (BFS) algorithm to traverse the network, the converter determines conversion paths from a given unit (or ratio of units) to any other compatible unit (or ratio), provided that a connecting path exists.

This package is currently under active development and is regularly updated. Backward compatibility will be maintained whenever possible.

What do this package contains:

  • It is loaded with a network of units preloaded for distances, area, volume, mass and time conversions defined for SI and Imperial systems according to the definition of each Unit, i.e.: 1_foot = 12_inches.
  • Prefixes applied to the basic units, like in SI units k to m to make km, are loaded as a network of conversion paths allowing the algorithm to apply the prefix to any other unit in the same system.
  • It provides a class Unit powered with arithmetic and logic operations to intrinsically consider unit conversions when making calculations.

How to use this package:

This package is intended to be used in three ways:

  • Calling the function units() to define instances of the Units class, that holds values associated to units, or, quantities.
  • As unit converter with the function convert() to explicitly make conversion of numeric variables and instances.
  • As unit converter for single numerical values, using the GUI.

A comprehensive user manual is also available here: UNYTS_User_Manual.

To launch the GUI

From an OS terminal or console, execute python -m unyts

drawing

In the interface, simply type in the units to convert and the value to be converted and click the convert button or hit Enter key.

To use the units converter function convert():

from unyts import convert
convert(value, from_units, to_units)
where:

  • value is a number (int, float, numpy.array, etc)
  • from_units is a string defining the units of value (i.e.: 'ft')
  • to_units is a string representing the units to convert value (i.e.: 'km')

To create instances of the Unit class using the units() function:

from unyts import units
variable = units(value, units)

  • value is a number (int, float, numpy.array, etc)
  • units is a string defining the units of value (i.e.: 'ft')

Then simply operate with the Unit instances or the variables related to them:
In: units(6, 'in') + units(1, 'ft')
Out: 18_in

Referring to the Unit class:

from unyts import Unit

  • The Unit class is not intended to be used to create Unit instances, but to allow checking if other object isinstance of Unit: i.e.: isinstance(variable, Unit)
  • In order to create instances of Unit it is convenient to use the units() function as it will return the appropriate Unit subclass.

Uses examples:

For further examples of use, the following Jupyter notebook unyts_demo intends to be a guide on how to use this converter and units classes.

To install this package:

Install it from the pypi.org repository:
pip install unyts
or upgrade to the latest version:
pip install --upgrade unyts !

Optional requisites:

The main functionalities are purely Python powered and does not require any other package to work but, if present, some commonly known packages are used to improve the operability of unyts:

  • NumPy to be able to deal with iterables not of nparray type, like (list of values)
  • Pandas to be able to recognize Series and DataFrames
  • cloudpickle to be able to save internal dictionaries and network to cache file, for faster loading
  • openpyxl if willing to export the units network to a pandas DataFrame

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

unyts-1.0.2.tar.gz (285.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unyts-1.0.2-py3-none-any.whl (281.7 kB view details)

Uploaded Python 3

File details

Details for the file unyts-1.0.2.tar.gz.

File metadata

  • Download URL: unyts-1.0.2.tar.gz
  • Upload date:
  • Size: 285.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for unyts-1.0.2.tar.gz
Algorithm Hash digest
SHA256 38194ae1d5a25d77016f8924453e029e6643178c961d918805a2f6b065bf37db
MD5 8fd05b1cc74d9dc4ab7114679e45bd5e
BLAKE2b-256 3af733c3ab4710adab2332c031fd4ed89f4b02bde3d33d076011fc12babc7aa0

See more details on using hashes here.

File details

Details for the file unyts-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: unyts-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 281.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for unyts-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 93a0058464d11627a8a854ffef4dc7ba6eceeb975ef1ece8eff20e13cd1236f1
MD5 d1259f28f77f92e89514f3516529e870
BLAKE2b-256 7ee85c20f720732c55ea6d33ee6e4607530cfb9f8b91d3150ff312c6fe18286f

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