Skip to main content
Author:

Ken Kundert

Version:
0.3.0
Released:

2020-08-12

A companion to QuantiPhy, quantiphy_eval evaluates strings containing simple algebraic expressions that involve quantities. It 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

QuantiPhy Eval is used in networth to 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.

With QuantiPhy the units do not survive operations, so you can specify the resolved units using the second argument. In fact, the second argument is passed to QuantiPhy as the model, which allows you to give the return value a name and description along with units, as demonstrated in the next example.

By default QuantiPhy Eval provides 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 voltage

Or, you can use evaluate to assign values to names directly, QuantiPhy Eval remembers these values between calls to evaluate:

>>> f_0 = evaluate('f₀ = 1MHz')
>>> omega_0 = evaluate('ω₀ = τ*f₀', 'rads/s')
>>> print(omega_0.render(show_label='f'))
ω₀ = 6.2832 Mrads/s

Similarly, QuantiPhy Eval provides 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,781

initialize takes three arguments, variables, functions and quantity. Both arguments and functions take dictionaries that overwrite any previously saved values. quantity takes a quantiphy Quantity class. The return value of evaluate will be an object of this class.

rm_commas 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 commas rm_commas also 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,781

Finally, QuantiPhy Eval supports 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.33

Releases

Latest development release:
Version: 0.3.0
Released: 2020-08-12
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.

  • split evaluate into two: evaluate and initialize.

0.2 (2020-03-06):
  • rm_commas now converts semicolons to commas

  • support comments

0.1 (2020-03-05):
  • 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

quantiphy_eval-0.3.0.tar.gz (39.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

quantiphy_eval-0.3.0-py3-none-any.whl (50.6 kB view details)

Uploaded Python 3

File details

Details for the file quantiphy_eval-0.3.0.tar.gz.

File metadata

  • Download URL: quantiphy_eval-0.3.0.tar.gz
  • Upload date:
  • Size: 39.9 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.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.7

File hashes

Hashes for quantiphy_eval-0.3.0.tar.gz
Algorithm Hash digest
SHA256 39dcd5d6679f3fac9b20ef65b8d845062c4a50185fc096b270add7bdff0a4011
MD5 5393e694232222714780d9d73720e9ee
BLAKE2b-256 f0445eb39177a985e7400324c4597eded36c6ca98e47bae723c3bf5cd494c7c3

See more details on using hashes here.

File details

Details for the file quantiphy_eval-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: quantiphy_eval-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 50.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.7

File hashes

Hashes for quantiphy_eval-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb4b30bee27473c4a336d6ebe839aecefd7f624afd07042cd475be62ea536316
MD5 206e72935de2e96f14483264fe307f3f
BLAKE2b-256 05af712219023293f23f7f94027353bbc9c47df8aded4453a654da7df2399cb7

See more details on using hashes here.

Release history Release notifications | RSS feed

0.5.0

2 files

This release

0.3.0 This release

2 files

0.2.0

1 file

0.1.0

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page