A tiny type-safe expression parser in Python
Project description
Espresso ☕
A tiny, type-safe expression parser in python.
This documentation is a WIP
Rationale
In certain cases, there is need for a safe way for the back-end to call functions provided by the client, espresso addresses this through a type-safety post-processing setup to ensure the function always receives the type it was defined with.
Example
import espresso
import random
import string
# An interface between the interpreter frontend and backend
ctx = espresso.Context()
# Namespaces are automatically resolved. In this case, within the space
# random, integer would be defined. Functionally this is syntactic sugar,
# the namespaces are simply nested dictionaries.
ctx.define_function(
random.randint, ["random", "integer"], ["int", "int"]
)
# Variadic typing is also supported. The method will be passed an array of
# all the arguments (of a singular type).
ctx.define_function(
random.choice, ["random", "oneOf"], ["*any"]
)
ctx.define_function(
random.choice, ["random", "oneOfInteger"], ["*int"]
)
ctx.define_function(
random.choice, ["random", "oneOfString"], ["*str"]
)
def random_string(length=32):
return "".join(
random.choice(string.ascii_letters + string.digits) for i in range(length)
)
# Optional typing, will pass None if not provided
ctx.define_function(
random_string, ["random", "string"], ["str?"]
)
ctx.eval("random.integer(0, 10)")
Integration and Usage
To see how espresso is integrated into a production SaaS, you can see the YAML Resolver which defines a bunch of functions and handles errors.
- id: random.string(32)
- first_name: person.firstName
- last_name: person.lastName
- age: random.integer(0, 32)
- plan: choices.oneOf("beta", "pro", "free")
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python-espresso-1.8.4.tar.gz.
File metadata
- Download URL: python-espresso-1.8.4.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e56648fa744598d1f342f9cb23606d2376620f4862cf7752564e82994daf641
|
|
| MD5 |
95b62b5bfb338d91e96db085d9a98f92
|
|
| BLAKE2b-256 |
f70a450af44f210fab7ab4ab880ed5b9254511d1f8580624a1e29837db2f65fb
|
File details
Details for the file python_espresso-1.8.4-py3-none-any.whl.
File metadata
- Download URL: python_espresso-1.8.4-py3-none-any.whl
- Upload date:
- Size: 9.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
551a915ddebe7c386a1aa958d9d6e020e62ac71a7ad7858f3ee8fce63f85f6b4
|
|
| MD5 |
e9cc4411fb25fa2ab28bc1d6d232963d
|
|
| BLAKE2b-256 |
8275ddfbf37203a774fab1c141c4140523ab78d692ef72a045e935a034a00518
|