- Version:
- 0.1.0
- Released:
2020-03-06
A companion to QuantiPhy, quantiphy_eval evaluates strings that contains a simple algebraic expression that involves quantities and returns a quantity. For example:
>>> from quantiphy_eval import evaluate
>>> average = evaluate('($1.2M + $1.3M)/2', '$')
>>> print(f'{average}')
$1.25M
>>> f_avg = evaluate('(122.317MHz + 129.349MHz)/2', 'Hz')
>>> print(f'{f_avg}')
125.83 MHz
With QuantiPhy the units do not survive operations, so you can specify the resolved units using the second argument.
QuantiPhy Eval provides three constants built-in: pi, tau, and e. However, you can pass in additional constants:
>>> from quantiphy import Quantity
>>> my_constants = dict(
... k = Quantity('k'),
... q = Quantity('q'),
... T = Quantity('25°C', scale='K'),
... )
>>> vt = evaluate('k*T/q', 'V', constants=my_constants)
>>> print(vt)
25.693 mV
QuantiPhy Eval provides the following functions built-in: sin, cos, tan, sqrt, abs, trunc, round and sgn. However, you can pass in additional functions:
>>> 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
>>> my_functions = dict(median=median)
>>> median_price = evaluate('median($636122, $749151, $706781)', '$', functions=my_functions)
>>> print(median_price.fixed(show_commas=True))
$706,781
Finally, QuantiPhy Eval provides rm_commas function for removing commas from an expression. Using it would prevent you from using multi-argument functions, however it would allow you to have commas in your numbers:
>>> from quantiphy_eval import evaluate, rm_commas
>>> average_price = evaluate(rm_commas('($636,122 + $749,151 + $706,781)/3'), '$')
>>> print(average_price.fixed(show_commas=True, prec=2, strip_zeros=False))
$697,351.33
Releases
- Latest development release:
- Version: 0.1.0Released: 2020-03-06
- 0.1 (2019-09-03):
Add support for user-defined constants and functions.
add rm_commas function.
- 0.0 (2020-02-14):
Initial version.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file quantiphy_eval-0.1.0.tar.gz.
File metadata
- Download URL: quantiphy_eval-0.1.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3d650f37f2783db31cbd23c7b50b642242d3d8b740838ce98aa1cfb00ace39f8
|
|
| MD5 |
be39ca82ad91850297650db8a591948d
|
|
| BLAKE2b-256 |
d7523286f97087a2ce78e69ba36b60ac1f629c3711604762ca32430738b16b23
|