Skip to main content

Lexpr: A simple logical expressions parser

Lexpr is a simple package containing a logical expressions parser developed using a Lark grammar.

The expressions may contain:

  • entity identifiers
  • the binary operators | (or), & (and)
  • the unary operator ! (not).
  • balanced pairs of round parentheses

Installation

The package can be installed using pip install lexpr.

Usage

A parser is created and used for parsing the text, as in the following example:

import lexpr
lp = lexpr.Parser()
lp.parse("(G1 & G2) | !G3")
#
# output:
#
#  Tree(
#    Token('RULE', 'start'),
#    [Tree(Token('RULE', 'entity'),
#      [Tree(Token('RULE', 'or_expr'),
#        [Tree(Token('RULE', 'entity'),
#          [Tree(Token('RULE', 'enclosed_expr'),
#            [Tree(Token('RULE', 'entity'),
#              [Tree(Token('RULE', 'and_expr'),
#                [Tree(Token('RULE', 'entity'), [Token('IDENTIFIER', 'G1')]),
#                 Tree(Token('RULE', 'entity'), [Token('IDENTIFIER', 'G2')])])]
#            )]
#          )]
#        ), Tree(Token('RULE', 'entity'),
#             [Tree(Token('RULE', 'not_expr'),
#               [Tree(Token('RULE', 'entity'), [Token('IDENTIFIER', 'G3')])]
#             )]
#           )]
#      )]
#    )]
#  )

In case of an invalid string is passed to the parser, an exception is raised:

import lexpr
lp = lexpr.Parser()
lp.parse("G1 &")
# raises LexprParserError, unbalanced expression
lp.parse("G1 & G$")
# raises LexprParserError, invalid character in identifier

Implementation

The grammar is contained in the file lexpr/data/lexpr.g. The parser is in the module lexpr/parser.py. Errors raised by the module are defined in lexpr/error.py and are instances of the class LexprError or its subclasses.

History

The package has been developed to support the parsing of the EGC format, for expressing expectations about the contents of prokaryotic genomes. In this format, groups of organisms can be combined using logical expressions of the form parsed by this package. The main implementation of the format is based on TextFormats, which, however does not support non-regular, indefinetly nested expressions, such as the logical expressions parsed here. Thus the parsing of this expressions has been developed separately in this package.

Acknowledgements

This package has been created in context of the DFG project GO 3192/1-1 “Automated characterization of microbial genomes and metagenomes by collection and verification of association rules”. The funders had no role in study design, data collection and analysis.

Release files for lexpr 0.1

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

Source distribution (sdist)

Source distribution for lexpr 0.1
File Size Uploaded
lexpr-0.1.tar.gz 4.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lexpr 0.1
File Interpreter ABI Platform
lexpr-0.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.4 kB

Release files / lexpr-0.1.tar.gz

Download URL lexpr-0.1.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4a2cf447634bd9f27c03b0c45352d132a020854ffa481d3a465f36cfae0717d3
BLAKE2b-256 checksum
How to use checksums
e17034a6a0736d5233b164922f8e9f78d11d8b4f433e30c4124bfd849e0dedd7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.12

Release files / lexpr-0.1-py3-none-any.whl

Download URL lexpr-0.1-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0941a73301fb808b3b41718b4ecd4a9cd258e36766795a26f831e869a189157b
BLAKE2b-256 checksum
How to use checksums
d72c90dc3c45c423a20f5af3f6b3415b0d1b087bfc00698c82a96f605ad2754c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.12

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

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