Skip to main content

Simple library with simple parser which parses simple expressions.

Project description


Simple expressions parser.


Installing via pip:

$ pip install simpleparser



To simply parse something, use Parser.parse method. With Parser you can describe different parsers. There is ready-made Defaults.parser.

from simpleparser import Defaults
expr = "a + b / 2"
parsed = Defaults.parser.parse(expr)
parsed(a=2, b=4) # returns 4.0

Parser.parse returns callable ParsedExpression object.

Custom parsers

Parser can be easily configured. It is described by a set of operators and constants types (such as numbers or booleans). See the Operator and ConstantType documentation below.

from simpleparser import Parser, Defaults
my_parser = Parser(
    operators=[, Defaults.minus, Defaults.mul, Defaults.div], # using some operators from `Defaults` here
    constants=[Defaults.integers_decimal, Defaults.float_point]

Here my_parser parses simple math expressions with 4 basic operators and decimal numbers, for example 1 + 2.0 * a.


Creating a custom operator:

from simpleparser import Operator, Parser
in_op = Operator(
    func=lambda el, container: el in container,
# creating new parser which uses this operator
new_parser = Parser([in_op], [])
el_in_set_checker = new_parser.parse("a in A")
el_in_set_checker(a=1, A={1, 2, 3}) # True
el_in_set_checker(a=0, A={1, 2, 3}) # False


Describes a type of constants which can be used in expression. Here is the definition of Defaults.boolean:

from simpleparser import ConstantType
boolean = ConstantType(r"(True|False|true|false)$", lambda s: True if s in {"True", "true"} else False)

Parser with this constant type replaces words which are matched by the boolean's regular expression to True or False.

parser = Parser([],[boolean])
parser.parse("True")() # True
parser.parse("false")() # False

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simpleparser-0.1.3.tar.gz (5.8 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page