Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

retrack

A business rules engine

Package version Code style: black Semantic Versions License

Installation

pip install retrack

Usage

import retrack

rule = retrack.from_json("rule.json")

result = await rule.execute(your_data_df)

Creating a rule/model

A rule is a set of conditions and actions that are executed when the conditions are met. The conditions are evaluated using the data passed to the runner. The actions are executed when the conditions are met.

Each rule is composed of many nodes. To see each node type, check the nodes folder.

To create a rule, you need to create a JSON file with the following structure:

{
  "nodes": {
		"node id": {
			"id": "node id",
			"data": {},
			"inputs": {},
			"outputs": {},
			"name": "node name",
		},
    // ... more nodes
  }
}

The nodes key is a dictionary of nodes. Each node has the following properties:

  • id: The node id. This is used to reference the node in the inputs and outputs properties.
  • data: The node data. This is used as a metadata for the node.
  • inputs: The node inputs. This is used to reference the node inputs.
  • outputs: The node outputs. This is used to reference the node outputs.
  • name: The node name. This is used to define the node type.

The inputs and outputs properties are dictionaries of node connections. Each connection has the following properties:

  • node: The node id that is connected to the current node.
  • input: The input name of the connection that is connected to the current node. This is only used in the inputs property.
  • output: The output name of the connection that is connected to the current node. This is only used in the outputs property.

To see some examples, check the examples folder.

Creating a custom node

To create a custom node, you need to create a class that inherits from the BaseNode class. Each node is a pydantic model, so you can use pydantic features to create your custom node. To see the available features, check the pydantic documentation.

To create a custom node you need to define the inputs and outputs of the node. To do this, you need to define the inputs and outputs class attributes. Let's see an example of a custom node that has two inputs, sum them and return the result:

import retrack
import pydantic
import pandas as pd
import typing


class SumInputsModel(pydantic.BaseModel):
    input_value_0: retrack.InputConnectionModel
    input_value_1: retrack.InputConnectionModel


class SumOutputsModel(pydantic.BaseModel):
    output_value: retrack.OutputConnectionModel


class SumNode(retrack.BaseNode):
    inputs: SumInputsModel
    outputs: SumOutputsModel

    async def run(self, input_value_0: pd.Series,
        input_value_1: pd.Series,
    ) -> typing.Dict[str, pd.Series]:
        output_value = input_value_0.astype(float) + input_value_1.astype(float)
        return {
            "output_value": output_value,
        }

After creating the custom node, you need to register it in the nodes registry and pass the registry to the parser. Let's see an example:

import retrack

# Register the custom node
custom_registry = retrack.nodes_registry()
custom_registry.register("sum", SumNode)

rule = retrack.from_json("rule.json", nodes_registry=custom_registry)

Contributing

Contributions are welcome! Please read the contributing guidelines first.

Metadata

Release files for retrack 3.9.0a0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for retrack 3.9.0a0
File Size Uploaded Provenance
retrack-3.9.0a0.tar.gz 28.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for retrack 3.9.0a0
File Interpreter ABI Platform
retrack-3.9.0a0-py3-none-any.whl Python 3 none any Details

Total release size: 74.2 kB

Release files / retrack-3.9.0a0.tar.gz

Download URL retrack-3.9.0a0.tar.gz
Size 28.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4b5d4428fa8c9bbad177c155c123bf1f208d791371b3f979ee7e0ce8aefa062d
BLAKE2b-256 checksum
How to use checksums
6dfe3e2803d5fe1dddfe9216c1f2a0775ab79c5f2bb07406efa3ae0a9eacb272
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.12.14 Linux/6.17.0-1022-azure

Release files / retrack-3.9.0a0-py3-none-any.whl

Download URL retrack-3.9.0a0-py3-none-any.whl
Size 45.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b6537529d18852c3c34d35c52d938ffeca2622f5933bc07e501aa83fa0696e7
BLAKE2b-256 checksum
How to use checksums
e08976bd33b63377d9e95f64b32de108c8cb6bbdab28a93caa545edd524b82df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.12.14 Linux/6.17.0-1022-azure

Security for retrack 3.9.0a0

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.

Release history Release notifications | RSS feed

This release
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page