Skip to main content

Infix pre-parser/expression validator for numexpr

Project description

Infix pre-parser for numexpr

This is an infix parser for the basic syntax outlined in the numexpr user guide. It may not correctly parse every supported numexpr expression, but it does offer strict parsing of a subset of numexpr syntax.

It is defined using the pyparsing package

Not supported at this time/known issues

  • String constants are not yet supported, and will not be sucessfully parsed
  • the axis keyword in numexpr sum and prod will not be sucessfully parsed

It can be used as numexpr pre-parser/validator:

  • ensure string expressions are strictly infix expressions
  • get detailed error feedback for parsing errors via pyparsing
  • assert only function names that are supported in numexpr
  • assert identifiers are valid python identifiers
  • assert that only the numexpr unary and binary operators are used

constant values supported are:

  • floating point with scientific and complex notation
  • integers

Installation

pip install numexpr_preparser

Example usage:

Using as a pre-parser, to reject expressions that are not strictly infix notation with the documented numexpr syntax

import numexpr
from numexpr_preparser import parser as preparser

def numexpr_safe_evaluate(ex: str, *args, **kwargs):
    parse_success, results = preparser.get_parser().run_tests(
        ex, print_results=False
    )
    if not parse_success:
        raise ValueError(results[0][1])
    return numexpr.evaluate(ex, *args, **kwargs)

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

numexpr_preparser-1.0.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file numexpr_preparser-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for numexpr_preparser-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ada2f1c73ecfe2213078e813284e298e18447013e5820e141493c9339cd72bdf
MD5 fc1d38c51e46eb6d4c3ead9328dd82ca
BLAKE2b-256 f79a2d4166919f8897711b5cc5b808dd6af2f7919cd4ce82e03ddb90abccb7d1

See more details on using hashes here.

Supported by

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