Skip to main content

Flow-based process just got Pythonic

dtree is a library for processing flow-based logical system with complicated chain of if-else blocks just like handling a flowchart.

Example

Here is a quick example to get a feeling of dtree. Given information of a student like

student = {
    'age': 15,
    'interest': 'reading',
    'gender': 'female',
}

figure out what gift to give with the following logics:

+++root:
|      +++age < 12:
|      |      +++interest = sports:
|      |      |      ---gender = female --> give note
|      |      |      ---ELSE --> give football
|      |      ---ELSE --> give book
|      +++age >= 15:
|      |      ---interest = writing --> give note
|      |      ---ELSE --> give book
|      +++ELSE:
|      |      ---gender = male --> give football
|      |      ---ELSE --> give book
from dtree import *

student = {
    'age': 15,
    'interest': 'reading',
    'gender': 'female',
}

age = ValueGetter("age", lambda student: student['age'])
interest = ValueGetter("interest", lambda student: student['interest'])
gender = ValueGetter("gender", lambda student: student['gender'])

is_male = gender.eq("male")
is_female = gender.eq("female")


def give(item):
    print("give %s" % item)


give_book = ToAction(lambda student: give("book"), "give book")
give_football = ToAction(lambda student: give("football"), "give football")
give_note = ToAction(lambda student: give("note"), "give note")

rule = DTree(Node(
    (age.lt(12), Node(
        (interest.eq("sports"), Node(
            (is_female, give_note),
            (else_, give_football),
        )),
        (else_, give_book),
    )),
    (age.ge(15), Node(
        (interest.eq("writing"), give_note),
        (else_, give_book),
    )),
    (else_, Node(
        (is_male, give_football),
        (else_, give_book),
    )),
))

rule.run(student)  # give book

Installation

Use pip <http://pip-installer.org>_ or easy_install::

pip install dtree-python

Alternatively, you can just drop dtree.py file into your project—it is self-contained.

  • dtree is tested with Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7 and PyPy.

Download files

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

Source Distribution

dtree-python-1.0.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distributions

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

dtree_python-1.0.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

dtree_python-1.0.0-py2-none-any.whl (4.7 kB view details)

Uploaded Python 2

File details

Details for the file dtree-python-1.0.0.tar.gz.

File metadata

  • Download URL: dtree-python-1.0.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.7.0

File hashes

Hashes for dtree-python-1.0.0.tar.gz
Algorithm Hash digest
SHA256 82e98b68e8488d628712135a2008ad2ae9b199dfa8f4cbb6b5c9079bd3c5d89a
MD5 4e804007f2a9338dda48f3bc8a637d65
BLAKE2b-256 17bc5b8866e07953c9a7bc01d1b299746d202c434703cc1ea57758353aefefb3

See more details on using hashes here.

File details

Details for the file dtree_python-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: dtree_python-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.7.0

File hashes

Hashes for dtree_python-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4874cd636911e53526ddd09d4ab7217438eb4d3c36642a6d345c569fa41c98ba
MD5 e2d238dcaeb57d5b8c5d168ff0f29b38
BLAKE2b-256 cf2553228111801f0495715dc490640d40636956d30a5851ff0c3420c7e546b8

See more details on using hashes here.

File details

Details for the file dtree_python-1.0.0-py2-none-any.whl.

File metadata

  • Download URL: dtree_python-1.0.0-py2-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.40.1 CPython/3.7.0

File hashes

Hashes for dtree_python-1.0.0-py2-none-any.whl
Algorithm Hash digest
SHA256 676b02f33634967809e0b8117a04cb5addcb13ddbe905c7a2b48af8eb8e58731
MD5 5c41da8e99a6966bc646e8529b33d799
BLAKE2b-256 501942fb7d640db1587270cc658ab1a5a89464e3f2ba2adaa46ca9f5ad254b21

See more details on using hashes here.

Supported by

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