modeval
Modeval (or Modular Eval) is a modular and secure string evaluation library that can be used to create custom parsers or interpreters.
Install with: pip install modeval
Basic Use
If performance is not really a concern, you can use the meval() function.
from modeval import meval
# Evalute string. Spaces are automatically removed.
print( meval('1 * (2-3)') )
Creating a parser class is more efficient for crunching lots of expressions with the same ruleset.
from modeval import Parser
# Create a new parser with the default ruleset.
p = Parser()
# Evalute string. Spaces are automatically removed.
print( p.eval('1 * (2-3)') )
Rulesets
The Parser class will use a basic mathematical ruleset if no ruleset is specified. Use the default ruleset as a guide on how to make custom ones.
⚠️ Warning: You cannot change the ruleset of a parser once it has been initialized. Create a new parser instead.
from modeval import Parser, Ruleset, meval
import operator # (standard library)
default_ruleset = Ruleset()
# Notice the order of the array follows order of operations.
default_ruleset.operators = [
[('^', operator.pow), ('**', operator.pow)],
[('*', operator.mul), ('/', operator.truediv)],
[('+', operator.add), ('-', operator.sub)]
]
p = Parser(ruleset = default_ruleset)
meval('1+1', ruleset=default_ruleset) # Rulesets can also be supplied to meval()
The second item in the tuple must be the function that will be called when the operator is used. The attached method must have two inputs in the correct order (L + R is parsed as add(L, R)).
Modeval also supports functions like sin(), but they are not included in the default ruleset. However, they are included in scientific_ruleset which can be imported and used. Adding custom functions works as follows:
from modeval import Parser, Ruleset
import math # (standard library)
custom_ruleset = Ruleset()
# Function order does not matter, so an extra layer of grouping is not needed.
custom_ruleset.functions = [
('sin', math.sin),
('cos', math.cos),
('tan', math.tan)
]
p = Parser(ruleset = custom_ruleset)
# You can now use "sin(...)" in the input string for eval().
Speaking of sin(), what about pi? Modeval also supports custom variables. They can be set like this:
from modeval import Parser, Ruleset
import math # (standard library)
custom_ruleset = Ruleset()
custom_ruleset.variables = [
('pi', math.pi) # Keep in mind this needs to be a value and not a reference to function.
]
p = Parser(ruleset = custom_ruleset)
# Now you can use "pi" just like any other number.
Utilities
The function parse_parentheses() is also available for use. It takes one string as an input and will output a nested array depending on the parenthesis grouping in the input string.
For example: parse_parentheses('ab(c(d))') ==> ['a', 'b', ['c', ['d']]]
Metadata
Release files for modeval 1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| modeval-1.5.tar.gz | 5.2 kB | Without provenance | Details for modeval-1.5.tar.gz |
Release files / modeval-1.5.tar.gz
| Download URL | modeval-1.5.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2c137f1e15d199d8f5c51017d44287ea545947c7cdab831265377306ff57c72a
|
|
BLAKE2b-256 checksum How to use checksums |
e3e0940fad8d38bb3cc464cf7fe52a0d51a02c3409bc780d59436776b2285eb6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
|
Security for modeval 1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.