Skip to main content

A declarative Python library for building computation DAGs

Project description

formularity-dag

A declarative Python framework for building computation/formula DAGs (Directed Acyclic Graphs).

Installation

pip install formularity-dag

# With JSON serialization support
pip install formularity-dag[json]

Quick Start

from formularity_dag import InputPlaceholder, ContextPlaceholder, compute, formula_dataclass

# Define a dataclass for structured data
@formula_dataclass
class TaxConfig:
    rate: float = 0.1
    name: str = "Sales Tax"

# Declare inputs
price = InputPlaceholder("price", float, "Item price", default_val=10.0)
quantity = InputPlaceholder("quantity", int, "Number of items", default_val=1)

# Declare context (environment parameters)
tax_config = ContextPlaceholder("tax_config", TaxConfig, "Tax configuration", default_val=TaxConfig())

# Define computations - dependencies are automatically wired by parameter names
@compute
def subtotal(price, quantity):
    return price * quantity

@compute
def tax_amount(subtotal, tax_config):
    return subtotal * tax_config.rate

@compute(is_output=True)
def total(subtotal, tax_amount):
    return subtotal + tax_amount

Evaluating the Graph

# The graph is automatically created in the module
graph = __compute_graph__

# Evaluate with inputs
result = graph.evaluate({
    "price": 25.0,
    "quantity": 3,
    "tax_config": {"rate": 0.08, "name": "State Tax"}
})

print(result['outputs'])  # {'total': 81.0}
print(result['state'])    # {'subtotal': 75.0, 'tax_amount': 6.0, 'total': 81.0}

Features

  • Declarative syntax: Define inputs and computations, let the framework handle wiring
  • Automatic dependency resolution: Parameter names match node names automatically
  • Type hints support: Full support for Python type annotations
  • Dataclass integration: First-class support for dataclasses with optional JSON serialization
  • Input/Context separation: Distinguish between user inputs and environment configuration
  • Output marking: Tag specific nodes as final outputs

API Reference

Placeholders

  • InputPlaceholder(name, dtype, description, default_val, is_required) - Declare an input node
  • ContextPlaceholder(name, dtype, description, default_val, is_required) - Declare a context node

Decorators

  • @compute - Register a computation node
  • @compute(is_output=True) - Register as an output node
  • @compute(mapping={'param': 'node_name'}) - Custom parameter-to-node mapping
  • @formula_dataclass - Combined @dataclass + @dataclass_json decorator

Utilities

  • is_dataclass_type(dtype) - Check if a type is a dataclass
  • dataclass_to_dict(obj) - Convert dataclass instance to dict
  • dict_to_dataclass(data, dtype) - Convert dict to dataclass instance

License

MIT

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

formularity_dag-0.1.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

formularity_dag-0.1.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: formularity_dag-0.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for formularity_dag-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6dce0efdd4aead1c15cbe6543420b33247f7412568a167157e743f70a2cbc8b6
MD5 36ca43e2933a4582ba0325ab656764c3
BLAKE2b-256 4bdacc859a7ce135257233a5fefd83e797c3ef07b8adbfbe7d6bf1cbeb0ba930

See more details on using hashes here.

Provenance

The following attestation bundles were made for formularity_dag-0.1.0.tar.gz:

Publisher: publish-dag.yml on soyvv/formularity

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for formularity_dag-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 396f88ddcccae84a29a983afc9e5c7452340ded4c9e9996913fa95cce2993092
MD5 3c2c325fc8b26e7de0ed97d7510bd97e
BLAKE2b-256 608a664ade6ad01ac5aadb89c6b88f2d039bf9c235cb87374fbece08f6c6d90e

See more details on using hashes here.

Provenance

The following attestation bundles were made for formularity_dag-0.1.0-py3-none-any.whl:

Publisher: publish-dag.yml on soyvv/formularity

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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