Skip to main content

Shared formula evaluator for GovAssist services. Synchronously evaluates string-template formulas authored in the form formula-field UI, with full field-handler parity with automation-builder-service.

Project description

abs-formula-core

Shared formula evaluator for GovAssist services.

Synchronously evaluates string-template formulas authored in the form formula-field UI. Mirrors automation-builder-service's evaluation engine function-for-function so payment formulas, record-number formulas, and any other formula-typed field produce identical results across services.

Install

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_field is set: the referenced record is loaded and its inner field value substituted.
  • date / datetime / time / time_range / date_range are coerced into datetime objects 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.

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

abs_formula_core-0.1.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

abs_formula_core-0.1.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file abs_formula_core-0.1.0.tar.gz.

File metadata

  • Download URL: abs_formula_core-0.1.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.13.5 Darwin/24.5.0

File hashes

Hashes for abs_formula_core-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f659f8ce6bfab6d81976192d1a25b434bab96563185b46ad26442ea8d546b591
MD5 a71ca03cf7e20de3fc0db6c9dcb2770f
BLAKE2b-256 7b63b1e775206495f15177ff1a443cacfc76754825a99b0ecc2deaf5e23a440e

See more details on using hashes here.

File details

Details for the file abs_formula_core-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: abs_formula_core-0.1.0-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

Hashes for abs_formula_core-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 233629f9daca739fbbcc91d7e015dc46f83747dc9e2200151774e93eb8bce845
MD5 2db16ee21eac8c2d4b2a24c39cb1b51f
BLAKE2b-256 d1170ae94668067a2705babec30a6bf74feffc6bbf122fbe0acd3e6efe38ce34

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