skip to navigation
skip to content

Not Logged In

cexprtk 0.1.0

Mathematical expression parser: cython wrapper around the 'C++ Mathematical Expression Toolkit Library'

Latest Version: 0.1.1

cexprtk: mathematical expression parsing and evaluation in python

cexprtk is a cython wrapper around "`C++ Mathematical Expression  Toolkit Library (ExprTk) <ExprTk_>`_ "  by Arash Partow. Using ``cexprtk`` a powerful mathematical expression engine can be incorporated into your python project.

.. contents::
        :depth: 1
        :backlinks: none


The latest version of ``cexprtk`` can be installed using `pip`_ :

.. code-block:: bash

        $ pip install cexprtk

.. note:: Installation requires a compatible C++ compiler to be installed.


The ``cexprtk`` module contains two functions check_expression_ and evaluate_expression_; the first can be used validate a mathematical formula before calculating its value using the latter.

Example: Evaluate a simple equation

The following shows how the arithmetic expression ``(5+5) * 23`` can be evaluated:

.. code-block:: python

        >>> import cexprtk
        >>> cexprtk.evaluate_expression("(5+5) * 23", {})

Example: Using Variables

Variables can be used within expressions by passing a dictionary to the evaluate_expression_ function. This maps variable names to their values. The expression from the previous example can be re-calculated using variable values:

.. code-block:: python

        >>> import cexprtk
        >>> cexprtk.evaluate_expression("(A+B) * C", {"A" : 5, "B" : 5, "C" : 23})

API Reference

For information about expressions supported by ``cexprtk`` please refer to the original C++ `ExprTK`_ documentation:

.. _check_expression :

**check_expression** (*expression*)

        Check that expression can be parsed. If successful do nothing, if unsuccessful raise ParseException.


                **expression** (*str*) Formula to be evaluated

        **Raises** ``ParseException``: If expression is invalid.

.. _evaluate_expression :

**evaluate_expression** (*expression*, *variables*)

        Evaluate a mathematical formula using the exprtk library and return result.

        For more information about supported functions and syntax see the
        exprtk C++ library website


                **expression** (*str*) Expression to be evaluated.

                **variables** (*dict*) Dictionary containing variable name, variable value pairs to be used in expression.

        **Returns** (*float*): Evaluated expression

        **Raises** ``ParseException``: if **expression** is invalid.


Cython wrapper by Michael Rushton (, although most credit should go to Arash Partow for creating the underlying ExprTK_ library.


``cexprtk`` is released under the same terms as the ExprTK_ library the `Common Public License Version 1.0`_ (CPL).


.. _ExprTK:
.. _pip:
.. _Common Public License Version 1.0:
File Type Py Version Uploaded on Size
cexprtk-0.1.0.tar.gz (md5) Source 2013-12-30 93KB
  • Downloads (All Versions):
  • 2 downloads in the last day
  • 221 downloads in the last week
  • 726 downloads in the last month