Skip to content

Commit

Permalink
Allow physical constants in extract expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Feb 22, 2018
1 parent 5805b3e commit adc3029
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
19 changes: 11 additions & 8 deletions doc/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,9 @@ to date.
gain2 = 0.5 -- gain of second integrator
Vmax = 1 -- quantizer maximum input voltage
Vmin = -1 -- quantizer minimum input voltage
-- levels = 16 -- quantizer output levels
# levels = 16 -- quantizer output levels
levels = 4 -- quantizer output levels
Tstop = 1/Fin "s" -- how many input periods to simulate
Tstop = 1/Fin "s" -- simulation stop time
Tstart = -0.5/Fin "s" -- initial transient interval (discarded)
The values given above are used in the simulation, no further modification
Expand All @@ -477,12 +477,6 @@ to date.
from math import sin, tau
from inform import display, error, os_error
parameters = Quantity.extract(__doc__, ignore_nonconforming_lines=True)
globals().update(parameters)
display('Simulation parameters:')
for v in parameters.values():
display(' ', v.render(show_label='f'))
class Integrator:
def __init__(self, gain=1):
self.state = 0
Expand Down Expand Up @@ -515,6 +509,15 @@ to date.
return self.amp*sin(self.omega*t)
# read simulation parameters and load into module namespace
parameters = Quantity.extract(__doc__)
globals().update(parameters)
# display the simulation parameters
display('Simulation parameters:')
for v in parameters.values():
display(' ', v.render(show_label='f'))
# instantiate components
integrator1 = Integrator(gain1)
integrator2 = Integrator(gain2)
Expand Down
5 changes: 3 additions & 2 deletions doc/user.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1380,8 +1380,9 @@ a version that displays the name and description by default.
Kvco = 9.07 GHz/V -- Gain of VCO
With :meth:`quantiphy.Quantity.extract()` the values of quantities can be given
as a expression that contains previously defined quantities. This requires that
you set the *evaluate* argument to True. For example:
as a expression that contains previously defined quantities (or :ref:`physical
constants <physical-constants> or select mathematical constants (pi, tau, π, or
τ). This feature is disabled if the *evaluate* argument is False. For example:

.. code-block:: python

Expand Down
20 changes: 10 additions & 10 deletions quantiphy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"""

# License {{{1
# Copyright (C) 2016 Kenneth S. Kundert
# Copyright (C) 2016-2018 Kenneth S. Kundert
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -524,13 +524,11 @@ def add_constant(value, alias=None, unit_systems=None):
}

# These constants are available to expressions in extract strings.
# Be aware that this dictionary is passed as globals to eval, so it gets
# polluted with things like __builtins__.
CONSTANTS = {
'pi': math.pi,
'π': math.pi,
'tau': 2*math.pi,
'τ': 2*math.pi,
'tau': getattr(math, 'tau', 2*math.pi),
'τ': getattr(math, 'tau', 2*math.pi),
}


Expand Down Expand Up @@ -1835,13 +1833,14 @@ def extract(cls, text, evaluate=True, ignore_nonconforming_lines=True):
// comment
:arg str evaluate:
When true extract attempt to evaluate a value it does not recognize
When true extract attempts to evaluate a value it does not recognize
as a number. In this case the expression for the value may be
followed by a string surrounded by double quotes, which is taken as
the units. For example: Tstop = 5/Fin "s". The expressions may only
contain number literals, quantities defined previously in the same set of
definitions, and the constants pi and tau (2*pi), which may be named
π or τ. The units should not include a scale factor.
contain number literals, quantities defined previously in the same
set of definitions, physical quantities, and the constants pi and
tau (2*pi), which may be named π or τ. The units should not include
a scale factor.
:arg str ignore_nonconforming_lines:
When true extract will ignore all lines that are not matched by
Expand Down Expand Up @@ -1908,7 +1907,8 @@ def extract(cls, text, evaluate=True, ignore_nonconforming_lines=True):
value = ' '.join(components[:-1])
else:
units = ''
value = eval(value, CONSTANTS, quantities)
predefined = ChainMap(quantities, _active_constants, CONSTANTS)
value = eval(value, None, predefined)
quantity = cls(value, units=units, name=qname, desc=desc)
else:
raise
Expand Down

0 comments on commit adc3029

Please sign in to comment.