Skip to content

Commit

Permalink
adding constants support
Browse files Browse the repository at this point in the history
  • Loading branch information
dprada committed Nov 8, 2023
1 parent d41da8e commit d4e2c64
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pyunitwizard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
from ._version import __version__

# Add imports here
from .constants import constants as _constants
from .constants import constants_synonyms as _constants_synonyms
from .main import unit, quantity, get_form, is_quantity, is_unit
from .main import get_value, get_unit, get_value_and_unit, change_value
from .main import convert
from .main import get_standard_units, standardize, get_dimensionality
from .main import concatenate, stack, hstack, vstack
from .main import are_compatible, are_equal, are_close, check
from .main import get_constant, show_constants
from . import configure
from . import kernel as _kernel

Expand Down
2 changes: 1 addition & 1 deletion pyunitwizard/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.9.1+6.g394f7f0.dirty"
__version__ = "0.9.3+0.gd41da8e.dirty"
5 changes: 5 additions & 0 deletions pyunitwizard/configure/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pyunitwizard._private.forms import digest_form
from pyunitwizard._private.lists_and_tuples import is_list_or_tuple
from pyunitwizard.main import convert, get_dimensionality
from pyunitwizard import _constants, _constants_synonyms
import numpy as np
from importlib.util import find_spec
from typing import List, Dict, Union
Expand Down Expand Up @@ -227,3 +228,7 @@ def set_standard_units(standard_units: List[str]) -> None:
if candidate_array[jj]>0:
already[jj]=1

def add_constant(constant_name, value, unit) -> None:

_constants[constant_name]=[value, unit]
pass
42 changes: 41 additions & 1 deletion pyunitwizard/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import numpy as np
from typing import Any, Dict, Optional, Union, Tuple


def get_form(quantity_or_unit: QuantityOrUnit) -> str:
""" Returns the form of a quantity as a string.
Expand Down Expand Up @@ -994,3 +993,44 @@ def vstack(sequence, to_unit=None, to_form=None, type_value='tuple', standardize
else:
raise ValueError

def get_constant(constant_name: str,
to_unit: Optional[str]=None,
to_form: Optional[str]=None,
standardized: Optional[bool]=False)-> QuantityLike:

from pyunitwizard import _constants
from pyunitwizard import _constants_synonyms

if constant_name in _constants_synonyms:
constant_name = _constants_synonyms[constant_name]

try:

value, unit = _constants[constant_name]
output = quantity(value, unit, form=to_form, standardized=standardized)
if to_unit is not None:
output = convert(output, to_unit=to_unit)

return output

except:

raise ValueError

def show_constants()-> dict:

from pyunitwizard import _constants
from pyunitwizard import _constants_synonyms

output = {}

for constant_name, constant_value in _constants.items():
names = [constant_name]
value = f'{constant_value[0]} {constant_value[1]}'
for ii, jj in _constants_synonyms.items():
if jj==constant_name:
names.append(ii)
output[tuple(names)]=value

return output

0 comments on commit d4e2c64

Please sign in to comment.