Skip to main content

A business rules engine

Project description

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 = 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

    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.

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

retrack-2.10.2a1.tar.gz (20.8 kB view details)

Uploaded Source

Built Distribution

retrack-2.10.2a1-py3-none-any.whl (33.6 kB view details)

Uploaded Python 3

File details

Details for the file retrack-2.10.2a1.tar.gz.

File metadata

  • Download URL: retrack-2.10.2a1.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.12.7 Linux/6.5.0-1025-azure

File hashes

Hashes for retrack-2.10.2a1.tar.gz
Algorithm Hash digest
SHA256 426e23bae897a59b2722f13126a7b5f5ea6fff88c4f338ab1d89b22edb465fc0
MD5 118645a698db9d902979e2c56da4780a
BLAKE2b-256 3efff6cc2024c0483874116f3435f744fc1c1a5ea164149141c77e2d68401c6a

See more details on using hashes here.

File details

Details for the file retrack-2.10.2a1-py3-none-any.whl.

File metadata

  • Download URL: retrack-2.10.2a1-py3-none-any.whl
  • Upload date:
  • Size: 33.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.12.7 Linux/6.5.0-1025-azure

File hashes

Hashes for retrack-2.10.2a1-py3-none-any.whl
Algorithm Hash digest
SHA256 d9b461051e0556aa65ee7a6aaa2b5dd2919f6d67dec65bcb2f57816b6965c2a6
MD5 ed9415324357d0009cfe581670e0a94f
BLAKE2b-256 4f2a9c5169253a0c488532232d37ceefe324abb80aa975ce1c1030aa31d29ec1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page