How to use asteval - 10 common examples

To help you get started, we’ve selected a few asteval examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github newville / asteval / tests / test_1.py View on Github external
def test_namefinder(self):
        'test namefinder'
        ast = self.interp.parse('x+y+cos(z)')
        nf = NameFinder()
        nf.generic_visit(ast)
        self.assertTrue('x' in nf.names)
        self.assertTrue('y' in nf.names)
        self.assertTrue('z' in nf.names)
        self.assertTrue('cos' in nf.names)
github newville / asteval / tests / test_1.py View on Github external
def test_namefinder(self):
        'test namefinder'
        ast = self.interp.parse('x+y+cos(z)')
        nf = NameFinder()
        nf.generic_visit(ast)
        self.assertTrue('x' in nf.names)
        self.assertTrue('y' in nf.names)
        self.assertTrue('z' in nf.names)
        self.assertTrue('cos' in nf.names)
github newville / asteval / tests / test_asteval.py View on Github external
def test_namefinder(self):
        """test namefinder"""
        p = self.interp.parse('x+y+cos(z)')
        nf = NameFinder()
        nf.generic_visit(p)
        self.assertTrue('x' in nf.names)
        self.assertTrue('y' in nf.names)
        self.assertTrue('z' in nf.names)
        self.assertTrue('cos' in nf.names)
github newville / asteval / tests / test_asteval.py View on Github external
def test_namefinder(self):
        """test namefinder"""
        p = self.interp.parse('x+y+cos(z)')
        nf = NameFinder()
        nf.generic_visit(p)
        self.assertTrue('x' in nf.names)
        self.assertTrue('y' in nf.names)
        self.assertTrue('z' in nf.names)
        self.assertTrue('cos' in nf.names)
github newville / asteval / tests / test_asteval.py View on Github external
def setUp(self):
        self.interp = Interpreter()
        self.symtable = self.interp.symtable
        self.set_stdout()
        self.set_stderr()
github newville / asteval / tests / test_asteval.py View on Github external
def test_partial_exception(self):
        sym_table = make_symbol_table(sqrt=partial(math.sqrt))

        aeval = Interpreter(symtable=sym_table)

        assert aeval("sqrt(4)") == 2

        # Calling sqrt(-1) should raise a ValueError. When the interpreter
        # encounters an exception, it attempts to form an error string that
        # uses the function's __name__ attribute. Partials don't have a
        # __name__ attribute, so we want to make sure that an AttributeError is
        # not raised.
    
        result = aeval("sqrt(-1)")
        assert aeval.error.pop().exc == ValueError
github newville / asteval / tests / test_asteval.py View on Github external
"test making and using a custom symbol table"

        if HAS_NUMPY:
            def cosd(x):
                "cos with angle in degrees"
                return np.cos(np.radians(x))

            def sind(x):
                "sin with angle in degrees"
                return np.sin(np.radians(x))

            def tand(x):
                "tan with angle in degrees"
                return np.tan(np.radians(x))

            sym_table = make_symbol_table(cosd=cosd, sind=sind, tand=tand)

            aeval = Interpreter(symtable=sym_table)
            aeval("x1 = sind(30)")
            aeval("x2 = cosd(30)")
            aeval("x3 = tand(45)")

            x1 = aeval.symtable['x1']
            x2 = aeval.symtable['x2']
            x3 = aeval.symtable['x3']

            assert_allclose(x1, 0.50,     rtol=0.001)
            assert_allclose(x2, 0.866025, rtol=0.001)
            assert_allclose(x3, 1.00,     rtol=0.001)
github pjamesjoyce / lcopt / lcopt / bw2_parameter_utils.py View on Github external
def get_symbols(expression):
    interpreter = asteval.Interpreter()
    nf = asteval.NameFinder()
    nf.generic_visit(interpreter.parse(expression))
    return set(nf.names).difference(EXISTING_SYMBOLS)
github newville / asteval / doc / conf.py View on Github external
#source_encoding = 'utf-8'

# The master toctree document.
master_doc = 'index'

# General information about the project.
project = u'asteval'
copyright = u'{}, Matthew Newville, The University of Chicago'.format(date.today().year)

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
import asteval
release = asteval.__version__.split('+', 1)[0]


# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None

# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'

# List of documents that shouldn't be included in the build.
#unused_docs = []

# List of directories, relative to source directory, that shouldn't be searched
github newville / asteval / asteval / astutils.py View on Github external
def get_ast_names(astnode):
    """Return symbol Names from an AST node."""
    finder = NameFinder()
    finder.generic_visit(astnode)
    return finder.names