jsonextended.units.core module

jsonextended.units.core.apply_unitschema(data, uschema, as_quantity=True, raise_outerr=False, convert_base=False, use_wildcards=False, list_of_dicts=False)[source]

apply the unit schema to the data

Parameters:
  • data (dict) –
  • uschema (dict) – units schema to apply
  • as_quantity (bool) – if true, return values as pint.Quantity objects
  • raise_outerr (bool) – raise error if a unit cannot be found in the outschema
  • convert_to_base (bool) – rescale units to base units
  • use_wildcards (bool) – if true, can use * (matches everything) and ? (matches any single character)
  • list_of_dicts (bool) – treat list of dicts as additional branches

Examples

>>> from pprint import pprint
>>> data = {'energy':1,'x':[1,2],'other':{'y':[4,5]},'y':[4,5],'meta':None}
>>> uschema =   {'energy':'eV','x':'nm','other':{'y':'m'},'y':'cm'}
>>> data_units = apply_unitschema(data,uschema)
>>> pprint(data_units)
{'energy': <Quantity(1, 'electron_volt')>,
 'meta': None,
 'other': {'y': <Quantity([4 5], 'meter')>},
 'x': <Quantity([1 2], 'nanometer')>,
 'y': <Quantity([4 5], 'centimeter')>}
>>> newschema = {'energy':'kJ','other':{'y':'nm'},'y':'m'}
>>> new_data = apply_unitschema(data_units,newschema)
>>> str(new_data["energy"])
'1.60217653e-22 kilojoule'
>>> new_data["other"]["y"].magnitude.round(3).tolist(), str(new_data["other"]["y"].units)
([4000000000.0, 5000000000.0], 'nanometer')
>>> old_data = apply_unitschema(new_data,uschema,as_quantity=False)
>>> old_data["energy"]
1.0
>>> old_data["other"]["y"].round(3).tolist()
[4.0, 5.0]
jsonextended.units.core.combine_quantities(data, units='units', magnitude='magnitude', list_of_dicts=False)[source]

combine <unit,magnitude> pairs into pint.Quantity objects

Parameters:
  • data (dict) –
  • units (str) – name of units key
  • magnitude (str) – name of magnitude key
  • list_of_dicts (bool) – treat list of dicts as additional branches

Examples

>>> from pprint import pprint
>>> sdata = {'energy': {'magnitude': 1.602e-22, 'units': 'kilojoule'},
...          'meta': None,
...          'other': {'y': {'magnitude': [4, 5, 6], 'units': 'nanometer'}},
...          'x': {'magnitude': [1, 2, 3], 'units': 'nanometer'},
...          'y': {'magnitude': [8,9,10], 'units': 'meter'}}
...
>>> combined_data = combine_quantities(sdata)
>>> pprint(combined_data)
{'energy': <Quantity(1.602e-22, 'kilojoule')>,
 'meta': None,
 'other': {'y': <Quantity([4 5 6], 'nanometer')>},
 'x': <Quantity([1 2 3], 'nanometer')>,
 'y': <Quantity([ 8  9 10], 'meter')>}
jsonextended.units.core.get_in_units(value, units)[source]

get a value in the required units

jsonextended.units.core.split_quantities(data, units='units', magnitude='magnitude', list_of_dicts=False)[source]

split pint.Quantity objects into <unit,magnitude> pairs

Parameters:
  • data (dict) –
  • units (str) – name for units key
  • magnitude (str) – name for magnitude key
  • list_of_dicts (bool) – treat list of dicts as additional branches

Examples

>>> from pprint import pprint
>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> Q = ureg.Quantity
>>> qdata = {'energy': Q(1.602e-22, 'kilojoule'),
...          'meta': None,
...          'other': {'y': Q([4,5,6], 'nanometer')},
...          'x': Q([1,2,3], 'nanometer'),
...          'y': Q([8,9,10], 'meter')}
...
>>> split_data = split_quantities(qdata)
>>> pprint(split_data)
{'energy': {'magnitude': 1.602e-22, 'units': 'kilojoule'},
 'meta': None,
 'other': {'y': {'magnitude': array([4, 5, 6]), 'units': 'nanometer'}},
 'x': {'magnitude': array([1, 2, 3]), 'units': 'nanometer'},
 'y': {'magnitude': array([ 8,  9, 10]), 'units': 'meter'}}