Skip to main content

The unit handler

Project description

To use, import the Units class. I like to shorthand it as _, after the style of TI calculators:

>>> from unties import Units as _

And you instantly have access to a ton of units and constants

Examples:

## Convert ‘ft’ to ‘inch’

_.ft.units_of(_.inch) #=> ‘12.000000000000002 * (0.0254 * _.m)’

## Convert 12.5 ‘ft’ to ‘inch’

(11.5 * _.ft).units_of(_.inch) #=> ‘138.00000000000003 * (0.0254 * _.m)’

# As you can see fro the examples, the decimals are not perfectly exact

## Each unit_group does not have to have the same dimensions:

(_.m/_.s).units_of(_.inch) #=> ‘39.37007874015748 * _.s**-1 * (0.0254 * _.m)’

But this isn’t always very useful:

(_.m**2).units_of(_.inch**3) #=> ‘61023.74409473229 * _.m**-1 * (1.6387064e-05 * _.m**3)’

## Multiple units have to be grouped:

(_.inch*_.fur).units_of(_.m**2) #=> ‘5.1096672 * (1.0 * _.m**2)’

# or else: _.inch*_.fur.units_of(_.m**2) #=> Exception: AttributeError: ‘str’ object has no attribute ‘copy’

# This is because methods have higher priority than the <*> operator

Project details


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