truth table: test binary operations with dictionaries
I want to use a generator in the truth table project for binary operations
preview
This is the test I have at the end of the chapter
1import itertools
2import src.truth_table
3import unittest
4
5
6class TestBinaryOperations(unittest.TestCase):
7
8 def test_binary_operations(self):
9 for first, second in itertools.product(
10 (False, True), repeat=2
11 ):
12 for operation, expectation in {
13 src.truth_table.contradiction: False,
14 src.truth_table.logical_conjunction: (
15 (first, second) == (True, True)
16 ),
17 src.truth_table.project_second: second,
18 src.truth_table.converse_non_implication: (
19 (first, second) == (False, True)
20 ),
21 src.truth_table.negate_first: not first,
22 src.truth_table.logical_nand: (
23 (first, second) != (True, True)
24 ),
25 src.truth_table.tautology: True,
26 src.truth_table.logical_disjunction: (
27 (first, second) != (False, False)
28 ),
29 src.truth_table.exclusive_disjunction: (
30 first != second
31 ),
32 src.truth_table.material_non_implication: (
33 (first, second) == (True, False)
34 ),
35 src.truth_table.project_first: first,
36 src.truth_table.converse_implication: (
37 (first, second) != (False, True)
38 ),
39 src.truth_table.negate_second: not second,
40 src.truth_table.logical_nor: (
41 (first, second) == (False, False)
42 ),
43 src.truth_table.logical_equality: (
44 first == second
45 ),
46 src.truth_table.material_implication: (
47 (first, second) != (True, False)
48 ),
49 }:
50 with self.subTest(
51 operation.__name__,
52 first=first,
53 second=second
54 ):
55 self.assertEqual(
56 operation(first, second),
57 expectation
58 )
59
60
61# Exceptions seen
62# AttributeError
63# TypeError
64# AssertionError
65# SyntaxError
open the project
I open a terminal
I change directory to the truth_table folder in the
pumping_pythonfolderI open
test_binary.pyfrom thetestsfolderI use pytest-watcher to run the tests automatically
test_binary_operations with dictionaries
RED: make it fail
I change the nested for loops in test_binary_operations to a generator expression
7 def test_binary_operations(self):
8 booleans = (False, True)
9 for first, second, operation, expectation in (
10 (first, second, operation, expectation)
11 for first in booleans
12 for second in booleans
13 for operation, expectation in (
14 (src.truth_table.contradiction, False),
15 (
16 src.truth_table.logical_conjunction,
17 (first, second) == (True, True)
18 ),
19 (src.truth_table.project_second, second),
20 (
21 src.truth_table.converse_non_implication,
22 (first, second) == (False, True)
23 ),
24 (src.truth_table.negate_first, not first),
25 (
26 src.truth_table.logical_nand,
27 (first, second) != (True, True)
28 ),
29 (src.truth_table.tautology, True),
30 (
31 src.truth_table.logical_disjunction,
32 (first, second) != (False, False)
33 ),
34 (
35 src.truth_table.exclusive_disjunction,
36 first != second
37 ),
38 (
39 src.truth_table.material_non_implication,
40 (first, second) == (True, False)
41 ),
42 (src.truth_table.project_first, first),
43 (
44 src.truth_table.converse_implication,
45 (first, second) != (False, True)
46 ),
47 (src.truth_table.negate_second, not second),
48 (
49 src.truth_table.logical_nor,
50 (first, second) == (False, False)
51 ),
52 (
53 src.truth_table.logical_equality,
54 first == second
55 ),
56 (
57 src.truth_table.material_implication,
58 (first, second) != (True, False)
59 ),
60 )
61 ):
62 with self.subTest(
63 operation.__name__,
64 first=first,
65 second=second
66 ):
67 raise Exception
68 self.assertEqual(
69 operation(first, second),
70 expectation
71 )
72
73
74# Exceptions seen
the terminal is my friend, and shows Exception for sixty four sub tests.
GREEN: make it pass
I remove the Exception
62 with self.subTest(
63 operation.__name__,
64 first=first,
65 second=second
66 ):
67 self.assertEqual(
68 operation(first, second),
69 expectation
70 )
71
72
73# Exceptions seen
the test is green again.
REFACTOR: make it better
I use itertools.product to make the statement simpler
7 def test_binary_operations(self): 8 # booleans = (False, True) 9 # for first, second, operation, expectation in ( 10 # (first, second, operation, expectation) 11 # for first in booleans 12 # for second in booleans 13 for first, second in itertools.product( 14 (False, True), repeat=2 15 ): 16 for operation, expectation in ( 17 (src.truth_table.contradiction, False), 18 ( 19 src.truth_table.logical_conjunction, 20 (first, second) == (True, True) 21 ),59 ( 60 src.truth_table.material_implication, 61 (first, second) != (True, False) 62 ), 63 ): 64 with self.subTest( 65 operation.__name__, 66 first=first, 67 second=second 68 ): 69 self.assertEqual( 70 operation(first, second), 71 expectation 72 ) 73 74 75# Exceptions seenI add an import statement for the itertools library
the test is green again.
I remove the commented lines
I add a git commit message
refactor with __getattribute__
I use the full path for each of the binary operations. Python objects have a method I can use to remove the repetition of src.truth_table by passing the name of the attribute or method.
RED: make it fail
I add a call to the getattribute method
60 with self.subTest(
61 operation.__name__,
62 first=first,
63 second=second
64 ):
65 self.assertEqual(
66 src.truth_table.__getattribute__(
67 operation
68 )(first, second),
69 expectation
70 )
71
72
73# Exceptions seen
GREEN: make it pass
I change the functions to just the names as strings
8 def test_binary_operations(self): 9 for first, second in itertools.product( 10 (False, True), repeat=2 11 ): 12 for operation, expectation in ( 13 ('contradiction', False), 14 ( 15 'logical_conjunction', 16 (first, second) == (True, True) 17 ), 18 ('project_second', second), 19 ( 20 'converse_non_implication', 21 (first, second) == (False, True) 22 ),23 ('negate_first', not first), 24 ( 25 'logical_nand', 26 (first, second) != (True, True) 27 ), 28 ('tautology', True), 29 ( 30 'logical_disjunction', 31 (first, second) != (False, False) 32 ),33 ( 34 'exclusive_disjunction', 35 first != second 36 ), 37 ( 38 'material_non_implication', 39 (first, second) == (True, False) 40 ), 41 ('project_first', first), 42 ( 43 'converse_implication', 44 (first, second) != (False, True) 45 ),46 ('negate_second', not second), 47 ( 48 'logical_nor', 49 (first, second) == (False, False) 50 ), 51 ( 52 'logical_equality', 53 first == second 54 ), 55 ( 56 'material_implication', 57 (first, second) != (True, False) 58 ), 59 ): 60 with self.subTest( 61 operation.__name__, 62 first=first, 63 second=second 64 ):the terminal is my friend, and shows AttributeError
I change
operation.__name__because I changed the operations from the functions to their names which are strings60 with self.subTest( 61 operation, 62 first=first, second=second 63 ): 64 self.assertEqual( 65 src.truth_table.__getattribute__( 66 operation 67 )(first, second), 68 expectation 69 ) 70 71 72# Exceptions seenthe test is green again.
I add a git commit message
close the project
I close
tests/test_binary.pyI click in the terminal where the tests are running
I use q on the keyboard to leave the tests
I change directory to the parent of truth table: projects
review
I can use a generator expression or itertools.product for nested for loops.
code from the chapter
Do you want to see all the CODE I typed in this chapter?
what is next?
rate pumping python
If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too.