Shared formula evaluator library for FastAPI apps. Synchronously evaluates string-template formulas with field-type-aware substitution and pluggable function registry.
Project description
Formula Core Package
A shared formula evaluator library for FastAPI applications that synchronously evaluates string-template formulas with field-type-aware substitution.
Features
- 56 built-in functions across Conditional, Math, Logical, String, Array, Date/Time, and Constant categories
- Field-type-aware token resolution (dropdown/radio/checkbox label mapping, association reference following, date/datetime coercion, file metadata)
- SEQUENCE() preprocessing via a Mongo
counterscollection for atomic counters - Per-call field + record caching to avoid redundant DB lookups
- Standardized exceptions:
MissingFieldErrorandFormulaEvaluationError
Installation
pip install abs-formula-core
or with Poetry:
poetry add abs-formula-core
Usage
from motor.motor_asyncio import AsyncIOMotorDatabase
from abs_formula_core import FormulaEvaluatorService, MissingFieldError
evaluator = FormulaEvaluatorService(db=cosmos_db)
formula = [
"MULTIPLY(IF(",
{"is_field": True, "field": "<dropdown_field_id>"},
" == \"Yes\", MULTIPLY(",
{"is_field": True, "field": "<number_field_id>"},
", 6), 24))",
]
try:
value = await evaluator.evaluate(formula, record, entity_id="<entity_id>")
except MissingFieldError as e:
# Field referenced by formula has no value on the record
print(e.field_id)
Formula token shape
List[Union[
str, # literal expression fragment
{"is_field": True, "field": "<field_id>", # record field reference
"reference_field": "<inner_field_id>"?}, # optional association follow
]]
Strings are concatenated verbatim into the expression. Field-reference dicts are resolved against the record:
- dropdown / radio / checkbox UUIDs are replaced with their option labels via the field definition.
- association values are followed when
reference_fieldis set: the referenced record is loaded and its inner field value substituted. - date / datetime / time / time_range / date_range are coerced into
datetimeobjects for date-function compatibility. - file fields return their stored metadata shape.
- number / text / boolean values are formatted as expression literals.
Supported functions
56 functions across categories: Conditional (IF, SWITCH), Math (SUM, COUNT, AVG, MIN, MAX, ROUND, ABS, DIVIDE, MULTIPLY, MINUS, MOD, POWER, SQRT, LOG), Logical (AND, OR, NOT, XOR, EXACT), String (CONCATENATE, UPPER, LOWER, LEN, LEFT, RIGHT, MID, TRIM, REPLACE, REPT, SEARCH, SUBSTITUTE, TEXT, IN, STR, VALUE), Array (APPEND, EXTEND, REMOVE), Date/Time (TODAY, NOW, DATE, DATEVALUE, DATEADD, DATEDIFF, ADD_DAYS, SUBTRACT_DAYS, WORKDAY, WORKDAYS, DAYS, MONTH, YEAR, DAY, HOUR, MINUTE, SECOND, HOURS_DIFF, MINUTES_DIFF, ADD_MINUTES, SUBTRACT_MINUTES, FORMAT_DATE, IOSWEEKNUM, WEEKNUM), Constants (PI, TRUE, FALSE).
Plus SEQUENCE() preprocessing via the counters Mongo collection.
Errors
MissingFieldError(field_id)— raised when a record field referenced by the formula has no value. Callers should surface a 422 with the field id.FormulaEvaluationError(message, expression?)— raised when a formula fails to evaluate for non-missing-field reasons (malformed expression, division by zero, simpleeval rejection).
Project details
Release history Release notifications | RSS feed
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 abs_formula_core-0.1.1.tar.gz.
File metadata
- Download URL: abs_formula_core-0.1.1.tar.gz
- Upload date:
- Size: 10.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eee7052f659b9028b449aa31eba77d68f6b2ccb984449546116324804f9dfe96
|
|
| MD5 |
a2de60d4dd099f0f9a4752224d3ddd6f
|
|
| BLAKE2b-256 |
52f3b76e8c058cc4b2bddb091d384f1700272101b3d0cd794f7140d136b6f3b0
|
File details
Details for the file abs_formula_core-0.1.1-py3-none-any.whl.
File metadata
- Download URL: abs_formula_core-0.1.1-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/24.5.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
094f1141fda07e6f24cbb3a41c08bbbe3305f5043c65a64bef22e239377dcfc3
|
|
| MD5 |
e6d3b4b7d5fff1281763d90e479e60ff
|
|
| BLAKE2b-256 |
9b62ef538346b2842105363d38ae2ecc920a8d18b6ff404fd9dc956afb9434f1
|