Quick Start#

What is Truth Table#

A Truth Table is like a cheat sheet that helps us figure out if something is true or false based on certain “conditions”. We call a specific combination of conditions a “case.” The outcome, whether it’s true or false, is what we call the “target.”

Imagine you’re trying to decide whether or not you should go out. Two things could affect your decision: the weather and what time you wake up. So, in this situation, the weather and your wake-up time are the “conditions”. When you combine these conditions, like if it’s sunny and you woke up early, that combination is a “case.” And the big question of whether you should go out or not is your “target.” The Truth Table helps us organize all these cases and their outcomes to make decisions easier.

For example, we have two types of conditions: weather and get_up (when you get up). And we want to determine if you will go out. weather has two possible values: is_sunny and not_sunny. get_up has three possible values: before_10, 10_to_2, after_2.

[1]:
import pandas as pd

df = pd.DataFrame(
    [
        ("is_sunny", "before_10", 1),
        ("is_sunny", "10_to_2", 1),
        ("is_sunny", "after_2", 0),
        ("not_sunny", "before_10", 0),
        ("not_sunny", "10_to_2", 0),
        ("not_sunny", "after_2", 0),
    ],
    columns=["weather", "get_up", "go_out"],
)
df
[1]:
weather get_up go_out
0 is_sunny before_10 1
1 is_sunny 10_to_2 1
2 is_sunny after_2 0
3 not_sunny before_10 0
4 not_sunny 10_to_2 0
5 not_sunny after_2 0

Create a Truth Table And Evaluate Case#

A truth table is structured with headers and rows. Think of the headers as the columns in a data table. The final column is called the “target”, while the preceding columns are known as “conditions”. The rows represent a collection of “cases”. In each row, the last element is the “target”, which is a boolean value, and the preceding elements are the “conditions”.

  1. You can create truth table from scratch. Then you can use the TruthTable.evaluate() method to get the “target”.

[2]:
from tt4human.api import TruthTable

# create Truth Table
tt = TruthTable.new(
    headers=["weather", "get_up", "go_out"],
    rows=[
        ("is_sunny", "before_10", True),
        ("is_sunny", "10_to_2", True),
        ("is_sunny", "after_2", False),
        ("not_sunny", "before_10", False),
        ("not_sunny", "10_to_2", False),
        ("not_sunny", "after_2", False),
    ],
)
tt.evaluate(case=dict(weather="is_sunny", get_up="before_10"))
[2]:
True
  1. You can also load Truth Table from a CSV file. The last column can be any “boolean-liked” value like ["true", "false", "t", "f", "yes", "no", "y", "n", "1", "0", ""].

[3]:
# content of the CSV file
from pathlib import Path

p = Path("go_out.tsv")
print(p.read_text())
weather get_up  go_out
is_sunny        before_10       1
is_sunny        10_to_2 1
is_sunny        after_2 0
not_sunny       before_10       0
not_sunny       10_to_2 0
not_sunny       after_2 0
[4]:
# load Truth Table from csv file
tt = TruthTable.from_csv(path="go_out.tsv", sep="\t")
tt.evaluate(case=dict(weather="is_sunny", get_up="10_to_2"))
[4]:
True
  1. You can also load Truth Table from a Pandas dataframe.

[5]:
# load Truth Table from pandas Dataframe
df = pd.DataFrame(
    [
        ("is_sunny", "before_10", 1),
        ("is_sunny", "10_to_2", 1),
        ("is_sunny", "after_2", 0),
        ("not_sunny", "before_10", 0),
        ("not_sunny", "10_to_2", 0),
        ("not_sunny", "after_2", 0),
    ],
    columns=["weather", "get_up", "go_out"],
)
tt = TruthTable.from_pandas_df(df)
tt.evaluate(case=dict(weather="is_sunny", get_up="after_2"))
[5]:
False

Edit Truth Table in Excel and Use it in Your Code#

Typically, we manage truth tables using tools like Excel or Google Sheets, making it easy for humans to edit them. However, keeping these tables in sync with your code can be quite cumbersome. That’s where tt4human comes in handy. It can automatically generate a Python module based on the provided TruthTable, which you can seamlessly incorporate into your code. It provides an enum class for each condition and a TruthTable object that you can import into your code, simplifying the process of working with truth tables and ensuring consistency between your code and the table.

[6]:
tt.generate_module(
    dir_path=Path.cwd(),
    module_name="do_you_go_out",
    overwrite=True,
)
# it automatically creates the ``do_you_go_out.py`` module
path_python_module = Path("do_you_go_out.py")
print(path_python_module.read_text())
# -*- coding: utf-8 -*-

"""
this module is generated by https://pypi.org/project/tt4human = 0.3.1
"""

from pathlib import Path
from tt4human.api import BetterStrEnum, TruthTable


class WeatherEnum(BetterStrEnum):
    is_sunny = "is_sunny"
    not_sunny = "not_sunny"


class GetUpEnum(BetterStrEnum):
    before_10 = "before_10"
    _10_to_2 = "10_to_2"
    after_2 = "after_2"


truth_table = TruthTable.from_csv(
    path=Path(__file__).absolute().parent.joinpath("do_you_go_out.tsv"),
)

if __name__ == "__main__":
    assert truth_table.evaluate(case={'weather': 'is_sunny', 'get_up': 'before_10'}) is True
[7]:
# it also create a snapshot of your Truth Table data in ``do_you_go_out.tsv``
path_tsv = Path("do_you_go_out.tsv")
print(path_tsv.read_text())
weather get_up  go_out
is_sunny        before_10       True
is_sunny        10_to_2 True
is_sunny        after_2 False
not_sunny       before_10       False
not_sunny       10_to_2 False
not_sunny       after_2 False

[ ]: