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.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

retrack-3.8.0a0.tar.gz (27.6 kB view details)

Uploaded Source

Built Distribution

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

retrack-3.8.0a0-py3-none-any.whl (43.4 kB view details)

Uploaded Python 3

File details

Details for the file retrack-3.8.0a0.tar.gz.

File metadata

  • Download URL: retrack-3.8.0a0.tar.gz
  • Upload date:
  • Size: 27.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.12.14 Linux/6.17.0-1022-azure

File hashes

Hashes for retrack-3.8.0a0.tar.gz
Algorithm Hash digest
SHA256 39ca55a47a9e3e1c93a52e8a6d18b30138fa28fbc34b940a324dc9be944394ef
MD5 6cc38497a3b2a3439c1b0deb9aed9582
BLAKE2b-256 94e344cfc9b70fe0cb259ac99ee8e9c5bf158567cd837317263d2103fc70f448

See more details on using hashes here.

File details

Details for the file retrack-3.8.0a0-py3-none-any.whl.

File metadata

  • Download URL: retrack-3.8.0a0-py3-none-any.whl
  • Upload date:
  • Size: 43.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.12.14 Linux/6.17.0-1022-azure

File hashes

Hashes for retrack-3.8.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5806f38473c416b3a4d0b13e0e164b90abaa10a607446e47971159a8da010ce
MD5 f00830c6403aaa6e7b97c0f0254071f4
BLAKE2b-256 2150b54bd1425916d6d91a10b840ad53d164d31179ca8e885adf1d4c56760d0d

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

3.8.0a0 This release

2 files

3.7.0

2 files

3.6.0

2 files

3.5.0

2 files

3.4.1

2 files

3.4.0

2 files

3.3.5

2 files

3.3.4

2 files

3.3.3

2 files

3.3.2

2 files

3.3.1

2 files

3.3.0

2 files

3.2.0

2 files

3.1.0

2 files

3.0.0

2 files

2.11.3

2 files

2.11.2

2 files

2.11.1

2 files

2.11.0

2 files

2.10.2

2 files

2.10.1

2 files

2.10.0

2 files

2.9.0

2 files

2.8.0

2 files

2.7.1

2 files

2.7.0

2 files

2.6.0

2 files

2.5.1

2 files

2.5.0

2 files

2.4.0

2 files

2.3.2

2 files

2.3.1

2 files

2.3.0

1 file

2.2.0

2 files

2.1.0

2 files

2.0.1

2 files

2.0.0

2 files

1.1.0

2 files

1.0.1

2 files

1.0.0

2 files

0.11.1

2 files

0.11.0

2 files

0.10.0

2 files

0.9.1

2 files

0.9.0

2 files

0.8.1

2 files

0.8.0

2 files

0.7.0

2 files

0.6.0

2 files

0.5.0

2 files

0.4.1

2 files

0.3.0

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page