Skip to main content

SimpleParser

Simple expressions parser.

Installing

Installing via pip:

$ pip install simpleparser

Usage

Parser

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.plus, 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.

Operator

Creating a custom operator:

from simpleparser import Operator, Parser
in_op = Operator(
    name="in",
    operator_type=2,
    func=lambda el, container: el in container,
    signs=("\u2208",),
    priority=3,
)
# 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

ConstantType

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

Metadata

Release files for simpleparser 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simpleparser 0.1.3
File Size Uploaded
simpleparser-0.1.3.tar.gz 5.8 kB Details

Release files / simpleparser-0.1.3.tar.gz

Download URL simpleparser-0.1.3.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6f1af6ec9ff50034a424829d95bd42bb41f91256555f406d1ab7e1b0c42b0d36
BLAKE2b-256 checksum
How to use checksums
67ab2092b918938c5baf14c93b0fcdc50517c5654fd78ea647024b6f8a6a79e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Release history Release notifications | RSS feed

This release

0.1.3 This release

1 release file

0.1.2

1 release file

0.1.1

1 release 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