Skip to content

Commit

Permalink
Merge pull request #15 from LovetheFrogs/2-to-1-mux
Browse files Browse the repository at this point in the history
2 to 1 mux
  • Loading branch information
LovetheFrogs authored Jun 21, 2022
2 parents d0d40c3 + 2d3e3e0 commit 3f11f37
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 1 deletion.
24 changes: 24 additions & 0 deletions CTools/Exceptions/MultiplexersExceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
PyCircTools Multiplexer module Exceptions.
"""


class MultiplexerException(Exception):
"""
MultiplexerException is the parent Exception class for all PyCircTools.Multiplexers related Exceptions
"""
def __init__(self, msg="Multiplexer Exception"):
self.msg = msg
super().__init__(self.msg)


class NonExistingInput(MultiplexerException):
"""
NonExistingInput is raised when a Multiplexer/Demultiplexer doesn't have the input asked for.
"""
def __init__(self, requestedInput=None):
if requestedInput is not None:
self.msg = "Input " + str(requestedInput) + " does not exist."
else:
self.msg = "Input requested does not exist."
super().__init__(self.msg)
1 change: 1 addition & 0 deletions CTools/Exceptions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

from CTools.Exceptions.CircuitToolsExceptions import *
from CTools.Exceptions.LogicGateExceptions import *
from CTools.Exceptions.MultiplexersExceptions import *
93 changes: 93 additions & 0 deletions CTools/Multiplexers/Mux2to1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from CTools.Exceptions.MultiplexersExceptions import NonExistingInput
from CTools.Exceptions.CircuitToolsExceptions import NotTruthValue


class Mux2to1:
"""
2 to 1 Multiplexer. Takes one Set input.
"""
def __init__(self):
"""
Mux2to1 class constructor method.
"""
self.input = [False, False]
self.set = False
self.output = self.__calculate_output()

def get_input(self, num):
"""
Method get_input is used to get the value of input[num]
:param num: Number of the input you want to get, either 0 or 1.
:type num: int
:raises NonExistingInput: raised when a Multiplexer/Demultiplexer doesn't have the input asked for.
:return: Value of the desired input (num).
:rtype: bool
"""
if num >= len(self.input) or num < 0:
raise NonExistingInput(num)

return self.input[num]

def get_set(self):
"""
Method get_set gets the value of the set control signal.
:return: Value of the set control signal.
:rtype: bool
"""
return self.set

def get_output(self):
"""
Method get_output gets the output of the 2 to 1 Multiplexer.
:return: Value of the output
:rtype: bool
"""
return self.output

def set_input(self, num, value):
"""
Method set_input sets a certain input to the desired value, either True or False.
:param num: Number of the input selected.
:type num: int
:param value: Desired value of the input.
:type value: bool
:raises NonExistingInput: raised when a Multiplexer/Demultiplexer doesn't have the input asked for.
:raises NotTruthValue: Raised when value's type is not bool.
"""
if num >= len(self.input) or num < 0:
raise NonExistingInput(num)

if type(value) is not bool:
raise NotTruthValue

self.input[num] = value
self.output = self.__calculate_output()
return self

def set_set(self, value):
"""
Method set_set sets the value of the set control signal to the desired truth value.
:param value: Desired value of the set control signal.
:type value: bool
:raises NotTruthValue: Raised when value's type is not bool.
"""
if type(value) is not bool:
raise NotTruthValue

self.set = value
self.output = self.__calculate_output()
return self

def __calculate_output(self):
"""
Private method __calculate_output calculates the output signal using the 2-to-1 mux formula.
:return: Value of the output signal.
:rtype: bool
"""
return (self.input[0] and not(self.set)) or (self.input[1] and self.set)
7 changes: 7 additions & 0 deletions CTools/Multiplexers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
PyCircTools Multiplexers package.
This package includes some Multiplexers and Demultiplexers (from 1 to 2 to 1 to 16)
"""

from CTools.Multiplexers.Mux2to1 import *
1 change: 1 addition & 0 deletions CTools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

from CTools.Exceptions import *
from CTools.LogicGates import *
from CTools.Multiplexers import *
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
setup(
name='PyCircTools',
packages=find_packages(include=['Exceptions', 'LogicGates']),
version='0.0.1',
version='0.0.3',
description='CTools, a python circuit library.',
author='LovetheFrogs',
license='GPL-3.0',
Expand Down
File renamed without changes.
45 changes: 45 additions & 0 deletions tests/Multiplexers_tests/test_Mux2to1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from CTools import NotTruthValue, NonExistingInput
from CTools.Multiplexers import Mux2to1


def test_Mux2to1():
errors = []
mux = Mux2to1()

if mux.get_output():
errors.append("Base get output test not passed!")

if mux.set_set(True).get_output():
errors.append("False, False with set to True test not passed!")

if mux.set_input(1, True).set_set(False).get_output():
errors.append("False, True with set to False test not passed!")

if not mux.set_set(True).get_output():
errors.append("False, True with set to True test not passed!")

if not mux.set_input(0, True).set_input(1, False).set_set(False).get_output():
errors.append("True, False with set to False test not passed!")

if mux.set_set(True).get_output():
errors.append("True, False with set to True test not passed!")

if not mux.set_input(1, True).set_set(False).get_output():
errors.append("True, True with set to False test not passed!")

if not mux.set_set(True).get_output():
errors.append("True, True with set to True test not passed!")

try:
mux.set_set(1)
errors.append("Set to not truth test not passed!")
except NotTruthValue:
pass

try:
mux.set_input(-1, True)
errors.append("Input to not existing input test not passed!")
except NonExistingInput:
pass

assert not errors, "errors occured:\n{}".format("\n".join(errors))

0 comments on commit 3f11f37

Please sign in to comment.