demorganizer is a Python-based framework for constructing, evaluating, and visualizing Boolean algebra expressions. It leverages object-oriented principles to represent logical expressions as a tree structure, allowing for intuitive construction using Python's operator overloading and generating truth tables.
- Expression Tree: Build complex Boolean expressions using
Variable,Constant,UnaryOperation, andBinaryOperationclasses. - Operator Overloading: Use standard Python logical operators (
&,|,^,~) to intuitively combine expressions. - Expression Evaluation: Evaluate expressions by providing a dictionary of variable bindings.
- Truth Table Generation: Generate comprehensive truth tables for any given expression, beautifully formatted using the
tabulatelibrary.
The project is organized into two main components:
algebramodule: Contains the core classes for defining and manipulating Boolean expressions.formatmodule: Provides utilities for presenting expressions, specifically for generating truth tables.
-
Clone the repository:
git clone https://github.com/othonhugo/demorganizer.git cd demorganizer -
Install dependencies: This project requires the
tabulatelibrary for truth table formatting.pip install -r requirements.txt
You can define variables and constants, then combine them using Python's logical operators.
from algebra import Variable, Constant, TRUE, FALSE
# Define variables
A = Variable("A")
B = Variable("B")
C = Variable("C")
# Construct your expressions
expr1 = (A & B) | ~C
expr2 = A ^ (B | FALSE)
# Example with direct boolean literals (automatically converted to Constants)
expr3 = (A | (B & True)) | ~FalseUse the evaluate() method on an expression, passing a dictionary of variable bindings.
from algebra import Variable
A = Variable("A")
B = Variable("B")
expr = A & B
# Evaluate with specific values for A and B
bindings_11 = {"A": True, "B": True}
bindings_12 = {"A": True, "B": False}
print(expr.evaluate(bindings_11)) # True
print(expr.evaluate(bindings_12)) # FalseThe create_truth_table function from the format module will generate a string representation of the truth table.
from algebra import Variable
from format import create_truth_table
A = Variable("A")
B = Variable("B")
C = Variable("C")
expr = (A & B) | ~C
print(create_truth_table(expr))This will output:
A B C ((A & B) | (~C))
--- --- --- ------------------
1 1 1 1
1 1 0 1
1 0 1 0
1 0 0 1
0 1 1 0
0 1 0 1
0 0 1 0
0 0 0 1