Skip to main content

a Unit converter based on graph network and classes to operate with units.

Project description

unyts

After culminating a project for a class from MITx courses, I saw the opportunity to use a digraph network to build a Unit converter able to convert from any units to any units without the need to populate a huge but finite table of possible conversions. Powered by the Breadth First Search, or BFS, algorithm to search through the network, this converter can find conversions from a particular Unit (or ratio of units) to any other Unit (or ratio) as long as a path connecting them exists.

This package is under development and is regularly updated. Back compatibility is intended to be maintained when possible.

What Contains This Package

  • It is loaded with the 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 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 on the same system.
  • It provides classes of unit useful powered with arithmetic and logic operations to intrinsically consider Unit conversions when making calculations.

How To Use It

To install it from the PyPI repository:
pip install unyts

To use the converter:

from unyts import convert
convert(value, sourceUnits, unitsToConvertTo) where:

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

To refer to the unit class:

from unyts import Unit

  • refering to Unit class is not intended to be used to create Unit instances, but to allow checking if other objects are instances 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.

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

For further examples:

The Jupyter notebook unyts_demo intends to be a guide on how to use this converter and units classes.

Requisites

  • NumPy

To install this package:

pip install unyts

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-0.4.8.tar.gz (29.7 kB view hashes)

Uploaded Source

Built Distribution

unyts-0.4.8-py3-none-any.whl (38.9 kB view hashes)

Uploaded Python 3

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