Skip to main content

No project description provided

Project description

ruler

A rule dsl language parser in Python

ruler DSL

This mini-language is partially inspired by LISP. A rule is represented by a an s-expression.

Syntax & Structure

(namespace.function_name "some_arguments" "more_arguments_if_applicable")

A rule is usually consist of a function name, and a list of (sometimes optional) arguments. Function names are often namespaced (e.g. "boolean.and", "condition.equal" etc.) and usually only recognized if placed in the first elemnt.

Unless otherwise specified, a rule can be inserted as an argument to another rule, for example a boolean.and rule.

(boolean.and (condition.equal (basic.field "fieldA") "X"),
              condition.equal (basic.field "fieldB") "Y")

Parsing and computing result

In order to parse the rule, just call genruler.parse. The result is a function where you can put in a context object in order for it to compute a result.

import genruler

rule = genruler.parse('(condition.Equal (basic.Field "fieldA") "X")')
context = {"fieldA": "X"}
rule(context) // should return true

Usage

Installation

I probably should submit this to PyPI so it is pip-installable

Ruler API overview

Array functions

Some array related functions.

array.length

(array.length $argument)

Returns the length of a given array $argument. For example,

import genruler

rule = genruler.parse('(array.length (basic.Field "fieldA"))')
context = {"fieldA": [1, 2, 3]}
rule(context) // should return 3

Basic functions

Some random functions that don't fit anywhere else goes here

basic.context

(basic.Context $context_sub, $rule)

Temporarily change the context to $context_sub, and perform $rule with $context_sub as the new context

  • $context_sub (required): A struct, or a rule to extract a new struct w.r.t. the original context
  • $rule (required): the rule to be applied w.r.t. $context_sub

An example:

rule = ruler.parse('(basic.Context (basic.field, 'sub')
                                   (basic.field, 'foo'))')

context = {"sub": {"foo": "bar"}}
rule(context) # returns context['sub']['foo'], which is 'bar'

Basic.field

(basic.field $key $default)

Returns a field value from context when called.

  • $key (required): is a key in the context.
  • $default (optional): is a default value to be returned when context[key] does not exist.

basic.value

(basic.value $value)

Returns a value, regardless what is in the context

  • $value (required): a value to return. MAY NOT be a sub-rule

Boolean operators

Usually used to chain condition rules (see next section) together

boolean.and_

(boolean.and_ $argument1 $argument2 ...)

Returns True if all arguments returns True, or False otherwise.

boolean.contradiction

(boolean.contradiction)

Always returns a False, a shorthand for

(basic.Value false)

boolean.not_

(boolean.not_ $argument)

Returns the result of negation done to $argument.

boolean.or_

(boolean.or $argument2 $argument2)

Returns True if any of the arguments is True, or False otherwise.

boolean.tautology

(boolean.tautology)

Returns True regardless

Condition rules

Usually returns either true or false

condition.equal

(condition.equal $alpha $beta)

Returns True if and only if $alpha is equivalent to $beta.

condition.gt

(condition.gt $alpha $beta)

Returns True if and only if $alpha is greater than $beta.

condition.ge

(condition.ge $alpha $beta)

Returns True if and only if $alpha is greater than or equal to $beta.

condition.in_

(condition.In $alpha $values)

Returns True if $alpha is in $values

condition.is_none

(condition.is_none $alpha)

Returns True if $alpha is None

condition.is_true

(condition.Is_True $alpha)

Returns True if $alpha is True

condition.lt

(condition.less_than $alpha $beta)

Returns True if and only if $alpha is less than $beta.

condition.le

["condition.Less_Than_Equal", $alpha, $beta]

Returns True if and only if $alpha is less than or equal to $beta.

String operations

Some basic string operations

string.concat

(string.Concat $link $argument1 $argument2 ...)

Concatenate arguments by $link

string.concat_fields

(string.concat_fields $link $key1 $key2 ...)

A short hand for

(string.concat $link (string.Field $key1) (string.field $key2) ...)

Note: $key1, $key2 etc.

string.lower

(string.lower $value)

Change $value to lowercase

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

genruler-0.1.1.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

genruler-0.1.1-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

Details for the file genruler-0.1.1.tar.gz.

File metadata

  • Download URL: genruler-0.1.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.11.0-8-generic

File hashes

Hashes for genruler-0.1.1.tar.gz
Algorithm Hash digest
SHA256 66ab630d4fa5ffdc247463f062cb44f3e65562a00fa9bdeed29a3c55645f1100
MD5 db52b8b2d3b2bc9cc0cb5d3e55d6f6b0
BLAKE2b-256 c60a17e14226eb18c8ac1fa94b4302dda20f0fe0d00c990c525a4963d3c6e9a1

See more details on using hashes here.

File details

Details for the file genruler-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: genruler-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/6.11.0-8-generic

File hashes

Hashes for genruler-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 79d3d75b4cbd2ffceb1f22611ea412be3bb84f06057d8c2dfb4fd6d79bc396bd
MD5 daafe3121baf60e5e183c7f4df93efa1
BLAKE2b-256 111ea76e939f0f627fe4079a689ec7053d5a35d79a34cd02ce0f132330c028ac

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