Package: quantiphy-eval
Author:Ken KundertVersion:0.5.0Released:2022-09-02A companion toQuantiPhy,quantiphy_evalevaluates strings containing simple algebraic expressions that involve
quantities. It returns a quantity.  For example:>>> from quantiphy_eval import evaluate

>>> avg_price = evaluate('($1.2M + $1.3M)/2', '$')
>>> print(avg_price)
$1.25M

>>> avg_freq = evaluate('(122.317MHz + 129.349MHz)/2', 'Hz')
>>> print(avg_freq)
125.83 MHzQuantiPhy Evalis used innetworthto allow you to give your estimated values using expressions that include
numbers that have units, SI scale factors, and commas.  That allows you the
convenience of copy-and-pasting your numbers from websites without being forced
to reformat them.WithQuantiPhythe units do not survive operations, so you can specify the
resolved units using the second argument.  In fact, the second argument is
passed toQuantiPhyas themodel,
which allows you to give the return value a name and description along with
units, as demonstrated in the next example.By defaultQuantiPhy Evalprovides no built-in constants.
However, you can add your own constants:>>> from quantiphy import Quantity
>>> from quantiphy_eval import evaluate, initialize
>>> import math

>>> my_constants = dict(
...     k = Quantity('k'),
...     q = Quantity('q'),
...     T = Quantity('25°C', scale='K'),
...     π = Quantity(math.pi),
...     τ = Quantity(math.tau),
... )
>>> initialize(variables=my_constants)

>>> Vt = evaluate('k*T/q', 'Vt V thermal voltage')
>>> print(Vt.render(show_label='f'))
Vt = 25.693 mV — thermal voltageAlternatively, you can specify the model directly in the text passed toevaluate. Simply append it in the form of a double-quoted string:>>> Vt = evaluate('k*T/q "Vt V thermal voltage"')
>>> print(Vt.render(show_label='f'))
Vt = 25.693 mV — thermal voltageYou can also useevaluateto assign values to names directly,QuantiPhy Evalremembers these values between calls toevaluate:>>> f_0 = evaluate('f₀ = 1MHz')
>>> omega_0 = evaluate('ω₀ = τ*f₀ "rads/s"')
>>> print(omega_0.render(show_label=True))
ω₀ = 6.2832 Mrads/sSimilarly,QuantiPhy Evalprovides no built-in functions by default, but you
can add any you need:>>> def median(*args):
...    args = sorted(args)
...    l = len(args)
...    m = l//2
...    if l % 2:
...        return args[m]
...    return (args[m] + args[m-1])/2

>>> initialize(functions = dict(median=median))
>>> median_price = evaluate('median($636122, $749151, $706781)', '$')
>>> print(median_price.fixed(show_commas=True))
$706,781initializetakes three arguments,variables,functionsandquantity.
Bothargumentsandfunctionstake dictionaries that overwrite any previously
saved values.quantitytakes aquantiphyQuantityclass. The return value
ofevaluatewill be an object of this class.rm_commasis a function for removing commas from an expression. This is used
if your number contain commas. Simply stripping the commas it would prevent you
from using multi-argument functions.  However after removing the commasrm_commasalso converts semicolons to commas.  So the previous example could
be rewritten as:>>> from quantiphy_eval import evaluate, rm_commas

>>> median_price = evaluate(
...     rm_commas('median($636,122; $749,151; $706,781)'),
...     '$',
... )
>>> print(median_price.fixed(show_commas=True))
$706,781QuantiPhy Evalsupports comments. A#and anything that follows it to the
end of the line is ignored:>>> average_price = evaluate(
...     rm_commas('''
...         median(
...             $636,122 +   # Zillow
...             $749,151 +   # Redfin
...             $706,781     # Trulia
...         )/3
...     '''),
...     '$'
... )
>>> print(average_price.fixed(show_commas=True, prec=2, strip_zeros=False))
$697,351.33Finally,QuantiPhy Evalusesinform.Errorfor error reporting:>>> from inform import Error

>>> try:
...     Vt = evaluate('kT/q', 'V')
...     print(Vt)
... except Error as e:
...     print(str(e))
kT: variable unknown.ReleasesLatest development release:Version: 0.5.0Released: 2022-09-020.5 (2022-09-02):refactor the project structureprovideqeexample, a simple calculator0.4 (2021-01-27):Add ability to explicitly specify units (or model) in evaluated string.0.3 (2020-08-12):complete re-write, parser now implemented with ply rather than pyparsing.all built-in constants and functions have been removed.splitevaluateinto two:evaluateandinitialize.0.2 (2020-03-06):rm_commasnow converts semicolons to commassupport comments0.1 (2020-03-05):Add support for user-defined constants and functions.addrm_commasfunction.0.0 (2020-02-14):Initial version.