Skip to content

Commit

Permalink
Merge pull request #33 from jamesjiang52/feature_str
Browse files Browse the repository at this point in the history
Added __str__ methods to wire and gate classes
  • Loading branch information
jamesjiang52 committed Apr 8, 2019
2 parents f430a78 + 3470610 commit 18c7399
Show file tree
Hide file tree
Showing 38 changed files with 379 additions and 32 deletions.
39 changes: 39 additions & 0 deletions bitwise/gate/AND.py
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_
11 changes: 10 additions & 1 deletion bitwise/gate/BUF.py
Expand Up @@ -12,7 +12,7 @@ class Buffer:
"""Construct a new Buffer.
Args:
input_1: An object of type Wire. The input to the buffer.
input: An object of type Wire. The input to the buffer.
output: An object of type Wire. The output of the buffer.
"""
def __init__(self, input_1, output):
Expand All @@ -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
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
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
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_
11 changes: 10 additions & 1 deletion bitwise/gate/NOT.py
Expand Up @@ -12,7 +12,7 @@ class NOTGate:
"""Construct a new NOT gate.
Args:
input_1: An object of type Wire. The input to the NOT gate.
input: An object of type Wire. The input to the NOT gate.
output: An object of type Wire. The output of the NOT gate.
"""
def __init__(self, input_1, output):
Expand All @@ -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
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
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
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_
3 changes: 3 additions & 0 deletions bitwise/wire/CLK.py
Expand Up @@ -44,3 +44,6 @@ def _oscillate(self):

def start(self):
_thread.start_new_thread(self._oscillate, ())

def __str__(self):
return str(self._value)
13 changes: 10 additions & 3 deletions bitwise/wire/TRI.py
Expand Up @@ -9,9 +9,9 @@ class TristateBuffer:
Args:
enable: An object of type Wire.
input_: An object of type Wire.
output: An object of type Wire. Takes on the value of input_ if enable
has value 1. Otherwise, value is independent of input_.
input: An object of type Wire.
output: An object of type Wire. Takes on the value of input if enable
has value 1. Otherwise, value is independent of input.
"""
def __init__(self, enable, input_, output):
self.input_ = input_
Expand All @@ -37,3 +37,10 @@ def _update_enable(self, value):
self.output.value = self.input_.value
else:
pass

def __str__(self):
str_ = ""
str_ += "enable: " + str(self.enable.value) + "\n"
str_ += "input: " + str(self.input_.value) + "\n"
str_ += "output: " + str(self.output.value)
return str_

0 comments on commit 18c7399

Please sign in to comment.