Skip to content

Instantly share code, notes, and snippets.

@alpha-beta-soup
Last active May 3, 2016 23:47
Show Gist options
  • Save alpha-beta-soup/0350c21c054d3bc0f23d8ddd8c857ed1 to your computer and use it in GitHub Desktop.
Save alpha-beta-soup/0350c21c054d3bc0f23d8ddd8c857ed1 to your computer and use it in GitHub Desktop.
import pint
ur = pint.UnitRegistry()
Q = ur.Quantity
def convert(array, source, target):
'''
Convert a list in one unit to a list in another, using Pint.
Simple but saves me from writing this repeatedly in ipython.
<source> and <target> must be Pint units, e.g.
>>> ur = pint.UnitRegistry()
>>> convert([1, 2], ur.inch, ur.meter)
[<Quantity(0.0254, 'meter')>, <Quantity(0.0508, 'meter')>]
'''
return [Q(x, source).to(target) for x in array]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment