From 314155ea7f16bccc6a7659efe18d13fd6d821483 Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Wed, 22 Jun 2022 01:26:15 +0200 Subject: [PATCH 1/2] Added implementation for 2-to-1 multiplexer --- CTools/Exceptions/MultiplexersExceptions.py | 24 ++++++ CTools/Exceptions/__init__.py | 1 + CTools/Modules/__init__.py | 0 CTools/Multiplexers/Mux2to1.py | 92 +++++++++++++++++++++ CTools/Multiplexers/__init__.py | 7 ++ CTools/__init__.py | 1 + setup.py | 2 +- 7 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 CTools/Exceptions/MultiplexersExceptions.py delete mode 100644 CTools/Modules/__init__.py create mode 100644 CTools/Multiplexers/Mux2to1.py create mode 100644 CTools/Multiplexers/__init__.py diff --git a/CTools/Exceptions/MultiplexersExceptions.py b/CTools/Exceptions/MultiplexersExceptions.py new file mode 100644 index 0000000..620e939 --- /dev/null +++ b/CTools/Exceptions/MultiplexersExceptions.py @@ -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 " + requestedInput + " does not exist." + else: + self.msg = "Input requested does not exist." + super().__init__(self.msg) diff --git a/CTools/Exceptions/__init__.py b/CTools/Exceptions/__init__.py index 76b9bc5..2e7c0d3 100644 --- a/CTools/Exceptions/__init__.py +++ b/CTools/Exceptions/__init__.py @@ -4,3 +4,4 @@ from CTools.Exceptions.CircuitToolsExceptions import * from CTools.Exceptions.LogicGateExceptions import * +from CTools.Exceptions.MultiplexersExceptions import * diff --git a/CTools/Modules/__init__.py b/CTools/Modules/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/CTools/Multiplexers/Mux2to1.py b/CTools/Multiplexers/Mux2to1.py new file mode 100644 index 0000000..29065d3 --- /dev/null +++ b/CTools/Multiplexers/Mux2to1.py @@ -0,0 +1,92 @@ +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 + 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) diff --git a/CTools/Multiplexers/__init__.py b/CTools/Multiplexers/__init__.py new file mode 100644 index 0000000..38604cd --- /dev/null +++ b/CTools/Multiplexers/__init__.py @@ -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 * diff --git a/CTools/__init__.py b/CTools/__init__.py index e326bfd..520d9e2 100644 --- a/CTools/__init__.py +++ b/CTools/__init__.py @@ -5,3 +5,4 @@ from CTools.Exceptions import * from CTools.LogicGates import * +from CTools.Multiplexers import * diff --git a/setup.py b/setup.py index 266dcb6..4061783 100644 --- a/setup.py +++ b/setup.py @@ -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', From 2d3e3e032d25b481f707694347b328868bf313ef Mon Sep 17 00:00:00 2001 From: LovetheFrogs <102818341+LovetheFrogs@users.noreply.github.com> Date: Wed, 22 Jun 2022 01:53:35 +0200 Subject: [PATCH 2/2] Tested Mux2to1 Tested Mux2to1 module and fixed the following errorrs: MultiplexExceptions.NonExistingInput; fixed trying to concatenate an integer to a String. Mux2to1; output not updated when changing set value. --- CTools/Exceptions/MultiplexersExceptions.py | 2 +- CTools/Multiplexers/Mux2to1.py | 1 + tests/Multiplexers_tests/__init__.py | 0 tests/Multiplexers_tests/test_Mux2to1.py | 45 +++++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 tests/Multiplexers_tests/__init__.py create mode 100644 tests/Multiplexers_tests/test_Mux2to1.py diff --git a/CTools/Exceptions/MultiplexersExceptions.py b/CTools/Exceptions/MultiplexersExceptions.py index 620e939..15bb830 100644 --- a/CTools/Exceptions/MultiplexersExceptions.py +++ b/CTools/Exceptions/MultiplexersExceptions.py @@ -18,7 +18,7 @@ class NonExistingInput(MultiplexerException): """ def __init__(self, requestedInput=None): if requestedInput is not None: - self.msg = "Input " + requestedInput + " does not exist." + self.msg = "Input " + str(requestedInput) + " does not exist." else: self.msg = "Input requested does not exist." super().__init__(self.msg) diff --git a/CTools/Multiplexers/Mux2to1.py b/CTools/Multiplexers/Mux2to1.py index 29065d3..ee8c05e 100644 --- a/CTools/Multiplexers/Mux2to1.py +++ b/CTools/Multiplexers/Mux2to1.py @@ -80,6 +80,7 @@ def set_set(self, value): raise NotTruthValue self.set = value + self.output = self.__calculate_output() return self def __calculate_output(self): diff --git a/tests/Multiplexers_tests/__init__.py b/tests/Multiplexers_tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/Multiplexers_tests/test_Mux2to1.py b/tests/Multiplexers_tests/test_Mux2to1.py new file mode 100644 index 0000000..0139c3e --- /dev/null +++ b/tests/Multiplexers_tests/test_Mux2to1.py @@ -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))