core#

tt4human.core.to_bool(v: str) bool[source]#

Convert a “bool” like string to a boolean value.

exception tt4human.core.InvalidCaseError[source]#
class tt4human.core.TruthTable(headers: List[str], rows: List[list])[source]#

Truth Table is a lookup table that can return a boolean value by given a set of conditions. A set of conditions is called a case.

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.

Below is the truth table:

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
Parameters:
  • headers – the last column is the “target”, others are “conditions”.

  • rows – list of “cases”, for each row, the last item is the “target”, others are “conditions”.

property conditions: List[str]#

Condition types.

property sorted_conditions: List[str]#

Sorted condition types.

property dict_view: Dict[str, Iterable]#

A dict view of the truth table.

Example:

{
    'weather': ('is_sunny', 'is_sunny', 'is_sunny', 'not_sunny', 'not_sunny', 'not_sunny'),
    'get_up': ('before_10', '10_to_2', 'after_2', 'before_10', '10_to_2', 'after_2'),
    'go_out': (True, True, False, False, False, False)
}
property lookup: Dict[str, bool]#

Return a dict to map the conditions to the result.

Sample lookup:

{
    'before_10____is_sunny': True,
    '10_to_2____is_sunny': True,
    'after_2____is_sunny': False,
    'before_10____not_sunny': False,
    '10_to_2____not_sunny': False,
    'after_2____not_sunny': False
}
classmethod new(headers: List[str], rows: List[list])[source]#

Create a new truth table.

Parameters:
  • headers – the last column is the “target”, others are “conditions”.

  • rows – list of “cases”, for each row, the last item is the “target”, others are “conditions”.

classmethod from_csv(path, sep: str = '\t')[source]#

Read truth table data from a CSV file. The first row is the types of condition, and the last column has to be the name of the result.

The value in the last column can be any “bool” liked value, such as “true”, “false”, “t”, “f”, “yes”, “no”, “y”, “n”, “1”, “0”, etc.

Parameters:
  • path – path-like object, path to the CSV file.

  • sep – str, separator of the CSV file, by default we use tab, because TSV can be copied into Excel / GoogleSheet easily.

classmethod from_pandas_df(df: pd.DataFrame)[source]#

Create a truth table from a pandas DataFrame.

evaluate(case: Dict[str, str]) bool[source]#

Evaluate a case and return the result. A case is a key value pair that the key is the condition type, and the value is the condition value.

Parameters:

case – example, {"weather": "is_sunny", "get_up": "before_10"}

generate_module(dir_path: Path, module_name: str, overwrite: bool = False)[source]#

Human usually create a truth table in Excel or GoogleSheet. This function can generate a Python module from a truth table that can be imported and used in your code.

Parameters:
  • truth_table – TruthTable, the truth table to be converted.

  • dir_path – Path, path to the directory to save the generated module.

  • module_name – str, name of the generated Python module.

  • overwrite – bool, if True, overwrite the existing module.

tt4human.core.generate_initial_csv(conditions: Dict[str, List[str]], flag_name: str, path: Path, sep: str = '\t', default_flag: str = '0', overwrite: bool = False)[source]#

Generate an initial CSV file from a dict of conditions. So human can copy it into Excel or GoogleSheet and fill the truth table.

Parameters:
  • conditions – dict, key is the condition type, value is a list of condition values.

  • flag_name – str, name of the flag column.

  • path – path-like object, path to the CSV file.

  • sep – str, separator of the CSV file, by default we use tab, because TSV can be copied into Excel / GoogleSheet easily.

  • overwrite – bool, if True, overwrite the existing CSV file.