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 = ValueAccessor("age", lambda student: student['age'])
interest = ValueAccessor("interest", lambda student: student['interest'])
gender = ValueAccessor("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.
Release files for dtree-python 1.0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dtree-python-1.0.5.tar.gz | 5.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dtree_python-1.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.3 kB
Release files / dtree-python-1.0.5.tar.gz
| Download URL | dtree-python-1.0.5.tar.gz |
|---|---|
| Size | 5.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
95b3ab756b5fd8d1d18d48145943c82975898e2b4d39056ed45425c49b6c5760
|
|
BLAKE2b-256 checksum How to use checksums |
1edd0ce9ca38a1c371a01d619ba7fba25133c73ef48817f5f6d49d95521f0839
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / dtree_python-1.0.5-py3-none-any.whl
| Download URL | dtree_python-1.0.5-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e0bf05eae925c64ac71038d86e3befb085e731850c5c7f17978dc780ba66a267
|
|
BLAKE2b-256 checksum How to use checksums |
a30cd55ba101112a5801d8657d9a09b1cc1224c6ecad14ecffafc9651b218b38
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|