How to use the pint.UnitRegistry function in Pint

To help you get started, we’ve selected a few Pint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pymedusa / Medusa / ext / pint / testsuite / test_issues.py View on Github external
def test_issue54(self):
        ureg = UnitRegistry()
        self.assertEqual((1*ureg.km/ureg.m + 1).magnitude, 1001)
github hgrecco / pint / pint / testsuite / test_issues.py View on Github external
def test_issue170(self):
        Q_ = UnitRegistry().Quantity
        q = Q_("1 kHz") / Q_("100 Hz")
        iq = int(q)
        self.assertEqual(iq, 10)
        self.assertIsInstance(iq, int)
github suavecode / SUAVE / trunk / SUAVE / Plugins / pint / testsuite / test_issues.py View on Github external
def test_issue66b(self):
        ureg = UnitRegistry()
        self.assertEqual(ureg.get_base_units(ureg.degK.units),
                         (None, UnitsContainer({'degK': 1})))
        self.assertEqual(ureg.get_base_units(ureg.degC.units),
                         (None, UnitsContainer({'degK': 1})))
github hgrecco / pint / pint / testsuite / test_application_registry.py View on Github external
def setUp(self):
        super().setUp()
        self.ureg_bak = get_application_registry()
        self.ureg1 = UnitRegistry(None)
        self.ureg1.define("foo = [dim1]")
        self.ureg1.define("bar = foo / 2")
        self.ureg2 = UnitRegistry(None)
        self.ureg2.define("foo = [dim2]")
        self.ureg2.define("bar = foo / 3")
github softwareunderground / subsurface / subsurface / units.py View on Github external
# -*- coding: utf-8 -*-
"""
Unit implementation using :mod:`pint`.
"""
import pint
import pint.unit

UndefinedUnitError = pint.UndefinedUnitError
DimensionalityError = pint.DimensionalityError

units = pint.UnitRegistry()

# Units not in the standard pint syntax
units.define('degrees_north = degree = degrees_N = degreesN = degree_north = degree_N = degreeN')
units.define('degrees_east = degree = degrees_E = degreesE = degree_east = degree_E = degreeE')
units.define('us_feet = usft = us_ft = 1200/3937 meters')
units.define('us_barrel = us_bbl = usbbl = 158,987294928 liter')
units.define('barrel = imperial_barrel = international_barrel = imp_bbl = bbl = 159,113159869818 liter')
units.define('bpd = bpod = barrel / day')
units.define('cf = cubic_feet = feet * feet * feet')
units.define('Mcfpd = Mcf / day')
units.define('boe = barrels_of_oil_equivalent = 6 Mcf')
units.define('BTU = british_thermal_unit = 1055.05585262 Joule')
units.define('percent = 0.01*count = %')
units.define('permille = 0.001*count = %')

del pint
github cropsinsilico / yggdrasil / cis_interface / units.py View on Github external
import numpy as np
from cis_interface import backwards
import unyt
import pint
_ureg_unyt = unyt.UnitRegistry()
_ureg_pint = pint.UnitRegistry()
_ureg_pint.define('micro_mole = 1e-6 * mole = uMol = umol')
_use_unyt = True
if _use_unyt:
    _unit_quantity = unyt.array.unyt_quantity
    _unit_array = unyt.array.unyt_array
else:
    _unit_quantity = _ureg_pint.Quantity
    _unit_array = _ureg_pint.Quantity


def has_units(obj):
    r"""Determine if a Python object has associated units.

    Args:
        obj (object): Object to be tested for units.
github zlatko-minev / pyEPR / pyEPR / toolbox_circuits.py View on Github external
# Python 2.7 and 3 compatibility
from __future__ import division, print_function, absolute_import
import numpy as np
import pandas as pd
#from collections import OrderedDict

from numpy import sqrt
from .toolbox import ϕ0, fluxQ, Planck, hbar, e_el, pi, ħ, elementary_charge, π,\
    divide_diagonal_by_2
#from scipy.constants import hbar, Planck, e as e_el, epsilon_0, pi


from pyEPR.toolbox_circuits import *
import pint
ureg = pint.UnitRegistry()
ureg.enable_contexts('spectroscopy')
ureg.ϕ0 = ϕ0
#_c_jj = pint.Context('jj')
#_c_jj.add_transformation('([length] ** 2 * [mass] / [current] ** 2 / [time] ** 2)', '[current]', lambda ureg, x, **kw: ureg.ϕ0 / x)
#ureg.add_context(_c_jj)
#ureg.enable_contexts('jj')

class Convert(object):
    '''
        Static container class for conversions of units and variables.

        TEST CONVERSION:
        ```python
            from pyEPR.toolbox_circuits import Convert

            Lj_nH, Cs_fF = 11, 60
github DFO-Ocean-Navigator / Ocean-Data-Map-Project / plotting / sound.py View on Github external
def load_data(self):
        super(SoundSpeedPlotter, self).load_data()

        self.pressure = [seawater.pres(self.temperature_depths[idx], ll[0])
                         for idx, ll in enumerate(self.points)]

        ureg = pint.UnitRegistry()
        try:
            u = ureg.parse_units(self.variable_units[0].lower())
        except:
            u = ureg.dimensionless

        if u == ureg.boltzmann_constant:
            u = ureg.kelvin

        if u == ureg.kelvin:
            temperature_c = ureg.Quantity(
                self.temperature, u).to(ureg.celsius).magnitude
        else:
            temperature_c = self.temperature

        self.sspeed = seawater.svel(
            self.salinity, temperature_c, self.pressure
github MolSSI / QCElemental / qcelemental / physical_constants / ureg.py View on Github external
def build_units_registry(context):
    """Builds a pint UnitRegistry based on a given PhysicalConstantsContext.

    Parameters
    ----------
    context : PhysicalConstantsContext
        The context to use for the values.
    """
    import pint

    phys_const = context.raw_codata
    ureg = pint.UnitRegistry(on_redefinition="ignore")

    # Explicitly update relevant 2014 codata

    # Definitions
    ureg.define("avogadro_constant = {} / mol = N_A".format(phys_const["avogadro constant"]["value"]))
    ureg.define("boltzmann_constant = {} * joule / kelvin".format(phys_const["boltzmann constant"]["value"]))
    ureg.define("speed_of_light = {} * meter / second".format(phys_const["speed of light in vacuum"]["value"]))
    ureg.define(
        "hartree_inverse_meter = {} / hartree / m".format(phys_const["hartree-inverse meter relationship"]["value"])
    )

    # Energy
    ureg.define("hartree = {} * joule = E_h = hartree_energy".format(phys_const["hartree energy"]["value"]))
    ureg.define("electron_volt = {} * J = eV".format(phys_const["electron volt-joule relationship"]["value"]))

    # Constants
github openstack / horizon / horizon / utils / units.py View on Github external
# Mapping of units from Ceilometer to Pint
INFORMATION_UNITS = (
    ('B', 'byte'),
    ('KB', 'Kibyte'),
    ('MB', 'Mibyte'),
    ('GB', 'Gibyte'),
    ('TB', 'Tibyte'),
    ('PB', 'Pibyte'),
    ('EB', 'Eibyte'),
)

TIME_UNITS = ('ns', 's', 'min', 'hr', 'day', 'week', 'month', 'year')


ureg = pint.UnitRegistry()


def is_supported(unit):
    """Returns a bool indicating whether the unit specified is supported."""
    return unit in functions.get_keys(INFORMATION_UNITS) + TIME_UNITS


def is_larger(unit_1, unit_2):
    """Returns a boolean indicating whether unit_1 is larger than unit_2.

    E.g:

    >>> is_larger('KB', 'B')
    True
    >>> is_larger('min', 'day')
    False