Skip to content

Instantly share code, notes, and snippets.

@jdriordan
Created March 22, 2017 23:11
Show Gist options
  • Select an option

  • Save jdriordan/7209af6160bfe345eff66b996ffb2be9 to your computer and use it in GitHub Desktop.

Select an option

Save jdriordan/7209af6160bfe345eff66b996ffb2be9 to your computer and use it in GitHub Desktop.
import pint
units = pint.UnitRegistry()
def make_constant(name):
'''Make a dimensional quanitity from a CODATA value using pint and scipy'''
from warnings import warn
from scipy.constants import physical_constants
if not (name in physical_constants):
matches = scipy.constants.find(name)
if matches==[] :
raise LookupError("Constant not found, use scipy.constant.find(name) \n or see https://docs.scipy.org/doc/scipy/reference/constants.html")
else:
warn("Assuming \"{}\" means \"{}\".".format(name,matches[0]))
name = matches[0]
value, unit, error = physical_constants[name]
return value*units[unit]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment