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

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

Uploaded Python 3

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