truth table: Nullary and Unary Operations


preview

These are the tests I have at the end of the chapter

truth_table/tests/test_nullary_unary.py
 1import src.truth_table
 2import unittest
 3
 4
 5class TestNullaryOperations(unittest.TestCase):
 6
 7    def test_logical_true(self):
 8        self.assertTrue(
 9            src.truth_table.logical_true()
10        )
11
12    def test_logical_false(self):
13        self.assertFalse(
14            src.truth_table.logical_false()
15        )
16
17
18class TestUnaryOperations(unittest.TestCase):
19
20    def test_logical_identity(self):
21        self.assertTrue(
22            src.truth_table.logical_identity(True)
23        )
24        self.assertFalse(
25            src.truth_table.logical_identity(False)
26        )
27
28    def test_logical_negation_aka_not(self):
29        self.assertFalse(
30            src.truth_table.logical_negation(True)
31        )
32        self.assertTrue(
33            src.truth_table.logical_negation(False)
34        )
35
36
37# Exceptions seen
38# AssertionError
39# AttributeError
40# TypeError

questions about Nullary and Unary Operations

Questions to think about as I go through the chapter


requirements

truth table


Nullary Operations

There are 2 Nullary operations - Logical True and Logical False. They do not take input and always return False or True.


test_logical_true


RED: make it fail


  • I go to the other terminal

  • I use mv to change the name of test_truth_table.py to test_nullary_unary.py

    mv tests/test_truth_table.py tests/test_nullary_unary.py
    
  • I open test_nullary_unary.py from the tests folder

  • I add an import statement at the top of test_nullary_unary.py

    1import src.truth_table
    2import unittest
    
  • I change the name of the class from TestTruthTable to TestNullaryOperations

    1import src.truth_table
    2import unittest
    3
    4
    5class TestNullaryOperations(unittest.TestCase):
    6
    7    def test_failure(self):
    
  • I change test_failure to test_logical_true

     5class TestNullaryOperations(unittest.TestCase):
     6
     7    def test_logical_true(self):
     8        self.assertTrue(
     9            src.truth_table.logical_true()
    10        )
    11
    12
    13# Exceptions seen
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'logical_true'
    

    because I have not added a definition for logical_true to truth_table.py in the src folder.


GREEN: make it pass


  • I add AttributeError to the list of Exceptions seen, in test_nullary_unary.py

    13# Exceptions seen
    14# AssertionError
    15# AttributeError
    
  • I open truth_table/__init__.py from the src folder

  • I remove all the text in the file then add a function to truth_table.py

    1def logical_true():
    2    return None
    

    the terminal is my friend, and shows AssertionError

    AssertionError: None is not true
    

    because the function returns None and the assertion expects True. Using substitution since I can treat a call to a function as the object it returns

    assertTrue(src.truth_table.logical_true())
    assertTrue(None                          )
    assertTrue(bool(None)                    )
    assertTrue(False                         )
    
  • I change None to True in the return statement

    1def logical_true():
    2    # return None
    3    return True
    

    the test passes.

  • I remove the commented line

    1def logical_true():
    2    return True
    
  • I open a new terminal, then add tests/test_nullary_unary.py to git for tracking

    git add tests/test_nullary_unary.py
    
  • I add a git commit message

    git commit -am 'add logical_true'
    

logical_true always returns True because it is True.


test_logical_false


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a test for logical_false to test_nullary_unary.py

     7    def test_logical_true(self):
     8        self.assertTrue(
     9            src.truth_table.logical_true()
    10        )
    11
    12    def test_logical_false(self):
    13        self.assertFalse(
    14            src.truth_table.logical_false()
    15        )
    16
    17
    18# Exceptions seen
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'logical_false'.
                    Did you mean: 'logical_true'?
    

    because I have not added a definition for logical_false to truth_table.py, I only added one for logical_true.


GREEN: make it pass


  • I add a function to truth_table.py

    1def logical_true():
    2    return True
    3
    4
    5def logical_false():
    6    return True
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the function returns True and the assertion expects False.

  • I change True to False in the return statement

    5def logical_false():
    6    # return True
    7    return False
    

    the test passes.

  • I remove the commented line

    1def logical_true():
    2    return True
    3
    4
    5def logical_false():
    6    return False
    
  • I add a git commit message in the other terminal

    git commit -am 'add logical_false'
    

logical_false always returns False because it is False.

The two Nullary Operations do not take input

return

operation

True

logical_true

False

logical_false


Unary Operations

There are 2 unary operations: Logical Identity and Logical Negation, they each take one input and return False or True.


test_logical_identity


RED: make it fail


  • I go back to the terminal where the tests are running

  • I add a new TestCase for Unary Operations with a test for logical_identity when it gets True as input, in test_nullary_unary.py

    input

    output

    True

    True

    12    def test_logical_false(self):
    13        self.assertFalse(
    14            src.truth_table.logical_false()
    15        )
    16
    17
    18class TestUnaryOperations(unittest.TestCase):
    19
    20    def test_logical_identity(self):
    21        self.assertTrue(
    22            src.truth_table.logical_identity(True)
    23        )
    24
    25
    26# Exceptions seen
    

    the terminal is my friend, and shows AttributeError

    AttributeError: module 'src.truth_table'
                    has no attribute 'logical_identity'
    

    because I need to add a definition for logical_identity to truth_table.py


GREEN: make it pass


  • I add a function named logical_identity to truth_table.py

     5def logical_false():
     6    return False
     7
     8
     9def logical_identity():
    10    return False
    

    the terminal is my friend, and shows TypeError

    TypeError: logical_identity() takes
               0 positional arguments but 1 was given
    

    because the test called the logical_identity function with one argument and the definition does not allow any arguments (the parentheses are empty).

  • I add TypeError to the list of Exceptions seen, in test_nullary_unary.py

    26# Exceptions seen
    27# AssertionError
    28# AttributeError
    29# TypeError
    
  • I add a name in parentheses so that logical_identity can take calls with one input, in truth_table.py

     9# def logical_identity():
    10def logical_identity(the_input):
    11    return False
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the function returns False and the assertion expects True.

  • I change the return statement to give the test what it wants

     9# def logical_identity():
    10def logical_identity(the_input):
    11    # return False
    12    return True
    

    the test passes.


REFACTOR: make it better


  • I add another assertion to test_logical_identity in test_nullary_unary.py for the case when it gets False as input

    input

    output

    False

    False

    20    def test_logical_identity(self):
    21        self.assertTrue(
    22            src.truth_table.logical_identity(True)
    23        )
    24        self.assertFalse(
    25            src.truth_table.logical_identity(False)
    26        )
    27
    28
    29# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the function returns True and the assertion expects False.

  • I change the return statement of logical_identity in truth_table.py

     9# def logical_identity():
    10def logical_identity(the_input):
    11    return False
    12    # return True
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the assertion that was passing before, expects True and the function now returns False.

  • I change the return statement of the function

     9# def logical_identity():
    10def logical_identity(the_input):
    11    # return False
    12    # return True
    13    return the_input
    

    the test passes because the logical_identity function returns its input as output

    logical_identity(the_input) -> the_input
    
  • I remove the commented lines

     5def logical_false():
     6    return False
     7
     8
     9def logical_identity(the_input):
    10    return the_input
    
  • I add a git commit message in the other terminal

    git commit -am 'add logical_identity'
    

When the logical_identity function is called

  • it returns True, if the input is True

  • it returns False, if the input is False

  • it returns the input as output

Tip

the_input is a name, I can use any name.

input

output

True

True

False

False


test_logical_negation


RED: make it fail


I add a test for logical_negation with an assertion for when it gets True as input, to test_nullary_unary.py

input

output

True

False

20    def test_logical_identity(self):
21        self.assertTrue(
22            src.truth_table.logical_identity(True)
23        )
24        self.assertFalse(
25            src.truth_table.logical_identity(False)
26        )
27
28    def test_logical_negation(self):
29        self.assertFalse(
30            src.truth_table.logical_negation(True)
31        )
32
33
34# Exceptions seen

the terminal is my friend, and shows AttributeError

AttributeError: module 'src.truth_table'
                has no attribute 'logical_negation'

there is no definition for logical_negation in truth_table.py


GREEN: make it pass


  • I add logical_negation to truth_table.py

     9def logical_identity(the_input):
    10    return the_input
    11
    12
    13def logical_negation(the_input):
    14    return the_input
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    because the function returned True and the assertion expects False.

  • I change the return statement to give the test what it wants

    13def logical_negation(the_input):
    14    # return the_input
    15    return False
    

    the test passes.


REFACTOR: make it better


  • I add another assertion to test_logical_negation for when logical_negation gets False as input, in test_nullary_unary.py

    input

    output

    False

    True

    28    def test_logical_negation(self):
    29        self.assertFalse(
    30            src.truth_table.logical_negation(True)
    31        )
    32        self.assertTrue(
    33            src.truth_table.logical_negation(False)
    34        )
    35
    36
    37# Exceptions seen
    

    the terminal is my friend, and shows AssertionError

    AssertionError: False is not true
    

    because the function returns False and the new assertion expects True.

  • I change the return statement of the logical_negation function in truth_table.py

    13def logical_negation(the_input):
    14    # return the_input
    15    # return False
    16    return True
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    the test fails for the assertion that passed before, because the function now returns True and that assertion expects False.

  • I make the function return its input again

    13def logical_negation(the_input):
    14    return the_input
    15    # return False
    16    # return True
    

    the terminal is my friend, and shows AssertionError

    AssertionError: True is not false
    

    this means the expectation of the test is that the logical_negation function

    • returns True if the input is False

    • returns False if the input is True

    • returns the opposite of the input it gets


how to return the opposite of a boolean


I can use the not keyword to return the opposite of the boolean after it.

  • I add not to the return statement

    13def logical_negation(the_input):
    14    # return the_input
    15    # return False
    16    # return True
    17    return not the_input
    

    the test passes.

  • I remove the commented lines

    13def logical_negation(the_input):
    14    return not the_input
    
  • I add to the name of the test as a note, in test_nullary_unary.py

    18class TestUnaryOperations(unittest.TestCase):
    19
    20    def test_logical_identity(self):
    21        self.assertTrue(
    22            src.truth_table.logical_identity(True)
    23        )
    24        self.assertFalse(
    25            src.truth_table.logical_identity(False)
    26        )
    27
    28    def test_logical_negation_aka_not(self):
    29        self.assertFalse(
    30            src.truth_table.logical_negation(True)
    31        )
    32        self.assertTrue(
    33            src.truth_table.logical_negation(False)
    34        )
    35
    36
    37# Exceptions seen
    
  • I add a git commit message in the other terminal

    git commit -am 'add logical_negation'
    

logical_negation also known as not returns the opposite of its input

input

output

True

False

False

True


close the project

  • I close test_nullary_unary.py and truth_table.py

  • I click in the terminal where the tests are running

  • I use q on the keyboard to leave the tests. The terminal goes back to the command line.

  • I change directory to the parent of truth_table

    cd ..
    

    the terminal shows

    .../pumping_python
    

    I am back in the pumping_python directory.


review

I ran these tests for Nullary Operations which take no input

and for Unary operations which take one input

the truth table for Unary Operations is

return

True

False

operation

the_input

True

False

logical_identity

not the_input

False

True

logical_negation (NOT)

How many questions can you answer after going through this chapter?


code from the chapter

Do you want to see all the CODE I typed for Nullary and Unary Operations?


what is next?

Would you like to test binary operations? They only take two inputs.


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.