Skip to content

Commit

Permalink
Add __str__ methods for gate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesjiang52 committed Apr 1, 2019
1 parent c4b1922 commit 3470610
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 0 deletions.
39 changes: 39 additions & 0 deletions bitwise/gate/AND.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def __init__(self, input_1, input_2, output):
else:
self.output.value = 0

self.input_1 = input_1
self.input_2 = input_2
self.output = output

def _update_input_1(self, value):
if ((value == 1) and (self.input_2.value == 1)):
self.output.value = 1
Expand All @@ -44,6 +48,13 @@ def _update_input_2(self, value):
else:
self.output.value = 0

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_


class ANDGate3:
"""Construct a new three-input AND gate.
Expand All @@ -59,6 +70,19 @@ def __init__(self, input_1, input_2, input_3, output):
ANDGate2(input_1, input_2, wire_1)
ANDGate2(input_3, wire_1, output)

self.input_1 = input_1
self.input_2 = input_2
self.input_3 = input_3
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "input_3: " + str(self.input_3.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_


class ANDGate4:
"""Construct a new four-input AND gate.
Expand All @@ -76,3 +100,18 @@ def __init__(self, input_1, input_2, input_3, input_4, output):
ANDGate2(input_1, input_2, wire_1)
ANDGate2(input_3, input_4, wire_2)
ANDGate2(wire_1, wire_2, output)

self.input_1 = input_1
self.input_2 = input_2
self.input_3 = input_3
self.input_4 = input_4
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "input_3: " + str(self.input_3.value) + "\n"
str_ += "input_4: " + str(self.input_4.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_
9 changes: 9 additions & 0 deletions bitwise/gate/BUF.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@ def __init__(self, input_1, output):
else:
self.output.value = 0

self.input = input_1
self.output = output

def _update_input_1(self, value):
if value == 1:
self.output.value = 1
else:
self.output.value = 0

def __str__(self):
str_ = ""
str_ += "input: " + str(self.input.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_
11 changes: 11 additions & 0 deletions bitwise/gate/IMPLY.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ def __init__(self, input_1, input_2, output):
wire_1 = Wire()
NOT.NOTGate(input_1, wire_1)
OR.ORGate2(wire_1, input_2, output)

self.input_1 = input_1
self.input_2 = input_2
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_
39 changes: 39 additions & 0 deletions bitwise/gate/NAND.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ def __init__(self, input_1, input_2, output):
AND.ANDGate2(input_1, input_2, wire_1)
NOT.NOTGate(wire_1, output)

self.input_1 = input_1
self.input_2 = input_2
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_


class NANDGate3:
"""Construct a new three-input NAND gate.
Expand All @@ -40,6 +51,19 @@ def __init__(self, input_1, input_2, input_3, output):
AND.ANDGate3(input_1, input_2, input_3, wire_1)
NOT.NOTGate(wire_1, output)

self.input_1 = input_1
self.input_2 = input_2
self.input_3 = input_3
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "input_3: " + str(self.input_3.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_


class NANDGate4:
"""Construct a new four-input NAND gate.
Expand All @@ -55,3 +79,18 @@ def __init__(self, input_1, input_2, input_3, input_4, output):
wire_1 = Wire()
AND.ANDGate4(input_1, input_2, input_3, input_4, wire_1)
NOT.NOTGate(wire_1, output)

self.input_1 = input_1
self.input_2 = input_2
self.input_3 = input_3
self.input_4 = input_4
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "input_3: " + str(self.input_3.value) + "\n"
str_ += "input_4: " + str(self.input_4.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_
39 changes: 39 additions & 0 deletions bitwise/gate/NOR.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ def __init__(self, input_1, input_2, output):
OR.ORGate2(input_1, input_2, wire_1)
NOT.NOTGate(wire_1, output)

self.input_1 = input_1
self.input_2 = input_2
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_


class NORGate3:
"""Construct a new three-input NOR gate.
Expand All @@ -40,6 +51,19 @@ def __init__(self, input_1, input_2, input_3, output):
OR.ORGate3(input_1, input_2, input_3, wire_1)
NOT.NOTGate(wire_1, output)

self.input_1 = input_1
self.input_2 = input_2
self.input_3 = input_3
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "input_3: " + str(self.input_3.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_


class NORGate4:
"""Construct a new four-input NOR gate.
Expand All @@ -55,3 +79,18 @@ def __init__(self, input_1, input_2, input_3, input_4, output):
wire_1 = Wire()
OR.ORGate4(input_1, input_2, input_3, input_4, wire_1)
NOT.NOTGate(wire_1, output)

self.input_1 = input_1
self.input_2 = input_2
self.input_3 = input_3
self.input_4 = input_4
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "input_3: " + str(self.input_3.value) + "\n"
str_ += "input_4: " + str(self.input_4.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_
9 changes: 9 additions & 0 deletions bitwise/gate/NOT.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@ def __init__(self, input_1, output):
else:
self.output.value = 1

self.input = input_1
self.output = output

def _update_input_1(self, value):
if value == 1:
self.output.value = 0
else:
self.output.value = 1

def __str__(self):
str_ = ""
str_ += "input: " + str(self.input.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_
39 changes: 39 additions & 0 deletions bitwise/gate/OR.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def __init__(self, input_1, input_2, output):
else:
self.output.value = 0

self.input_1 = input_1
self.input_2 = input_2
self.output = output

def _update_input_1(self, value):
if ((value == 1) or (self.input_2.value == 1)):
self.output.value = 1
Expand All @@ -44,6 +48,13 @@ def _update_input_2(self, value):
else:
self.output.value = 0

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_


class ORGate3:
"""Construct a new three-input OR gate.
Expand All @@ -59,6 +70,19 @@ def __init__(self, input_1, input_2, input_3, output):
ORGate2(input_1, input_2, wire_1)
ORGate2(input_3, wire_1, output)

self.input_1 = input_1
self.input_2 = input_2
self.input_3 = input_3
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "input_3: " + str(self.input_3.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_


class ORGate4:
"""Construct a new four-input OR gate.
Expand All @@ -76,3 +100,18 @@ def __init__(self, input_1, input_2, input_3, input_4, output):
ORGate2(input_1, input_2, wire_1)
ORGate2(input_3, input_4, wire_2)
ORGate2(wire_1, wire_2, output)

self.input_1 = input_1
self.input_2 = input_2
self.input_3 = input_3
self.input_4 = input_4
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "input_3: " + str(self.input_3.value) + "\n"
str_ += "input_4: " + str(self.input_4.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_
11 changes: 11 additions & 0 deletions bitwise/gate/XNOR.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ def __init__(self, input_1, input_2, output):
AND.ANDGate2(input_1, input_2, wire_3)
AND.ANDGate2(wire_1, wire_2, wire_4)
OR.ORGate2(wire_3, wire_4, output)

self.input_1 = input_1
self.input_2 = input_2
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_
11 changes: 11 additions & 0 deletions bitwise/gate/XOR.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,14 @@ def __init__(self, input_1, input_2, output):
AND.ANDGate2(input_1, wire_2, wire_3)
AND.ANDGate2(input_2, wire_1, wire_4)
OR.ORGate2(wire_3, wire_4, output)

self.input_1 = input_1
self.input_2 = input_2
self.output = output

def __str__(self):
str_ = ""
str_ += "input_1: " + str(self.input_1.value) + "\n"
str_ += "input_2: " + str(self.input_2.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_

0 comments on commit 3470610

Please sign in to comment.