PyCircTools, a python circuit library.
Project description
PyCircTools
PyCircTools is a python package which contains tools to build circuits using python 3. It is a work in progress, and will be updated frequently to add more modules.
Table of contents
- 1. Installing PyCircTools and importing it
- 2. Logic Gates Module
- 3. Multiplexers Module
- Exceptions
- About
Installing PyCircTools and importing it.
To install PyCircTools, use pip by calling the following command:
pip install PyCircTools
Importing it is as easy as it gets, just insert the line
from PyCircTools.*subpackage* import *modules*
at the start of your code, and substitute subpackage with the package you want, and modules with the modules you want to import to your project.
Logic Gates Module
Here is an in depth explanation for the PyCircTools.LogicGates module. LogicGates adds implementation for common logic gates used in circuit design. It allows to create gates with as many inputs as the user decides (except for the NOT gate). For further reference about each of the gates, check its section in this README.
NOT gate
The NOT gate is a simple logic gate which simply inverts the input. It implements the logical negation (¬) and has one input and one output. The NOT gate symbol and truth table is shown below.
Attributes
The NOT gate has the following attributes:
Name | Description | Type |
---|---|---|
input | Input of the gate | bool |
output | Output of the gate | bool |
Constructor
The constructor of the NOT gate has the following format:
Not()
Which doesn't take any parameters and returns a NOT gate with its Input set to True and has its output calculated by the not gate calculate output method)Methods
The NOT gate has the following methods:
-
get_input(): Gets the input of the gate. It returns a bool containing the requested value.
-
get_output(): Gets the output of the gate. It returns a bool containing the requested value.
-
set_input(bool value): Sets the input of the gate to the truth value 'value', which is passed as a parameter.
- __calculate_output(): Private method which calculates the output of the gate. This output is:
not(input)
AND gate
The AND gate is a logic gate with two or more inputs that implements the logical conjunction (^). It's output is True only when all the inputs are True. If any of them is set to False, the output will then be False. Below you can find the AND truth table.
Attributes
The AND gate has the following attributes:
Name | Description | Type |
---|---|---|
input | List of inputs of the gate | List[bool] |
output | Output of the gate | bool |
numOfInputs | Number of inputs of the gate | int |
Constructor
The constructor of the AND gate has the following format:
And(int inputNumber)
Which takes the inputNumber parameter, an integer set to two by default which can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the and gate calculate output method and numOfInputs will take the same value as the parameter inputNumber.Methods
- get_input(int num): Gets the value of the input num. It returns the boolean value in Output[num]
- get_output(): Gets the output of the gate. It returns a bool containing the requested value.
- get_numOfInputs(): Gets the number of inputs of the gate. It returns an int containing the requested value.
- set_input(int num, bool value): Sets the input 'num' to the truth value 'value'. Both of them are passed as parameters to the method.
- add_input(): Adds a new input to the gate, which defaults to False and updates both output and numOfInputs.
- remove_input(): Removes the last input and updates both output and numOfInputs.
- __calculate_output(): Private method which calculates the output of the gate. This output is:
(input_n and input_n+1)
for all the inputs of the gate.
NAND gate
The NAND gate is a logic gate with two or more inputs which produces a False output only when all of its inputs are True. Its output is True in any other case. In other words, its output is calculated by negating the conjunction of all inputs of the gate. Below you can find the NAND truth table.
Attributes
The NAND gate has the following attributes:
Name | Description | Type |
---|---|---|
input | List of inputs of the gate | List[bool] |
output | Output of the gate | bool |
numOfInputs | Number of inputs of the gate | int |
Constructor
The constructor of the NAND gate has the following format:
Nand(int inputNumber)
Which takes the inputNumber parameter, an integer set to two by default which can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the nand gate calculate output method and numOfInputs will take the same value as the parameter inputNumber.Methods
- get_input(int num): Gets the value of the input num. It returns the boolean value in Output[num]
- get_output(): Gets the output of the gate. It returns a bool containing the requested value.
- get_numOfInputs(): Gets the number of inputs of the gate. It returns an int containing the requested value.
- set_input(int num, bool value): Sets the input 'num' to the truth value 'value'. Both of them are passed as parameters to the method.
- add_input(): Adds a new input to the gate, which defaults to False and updates both output and numOfInputs.
- remove_input(): Removes the last input and updates both output and numOfInputs.
- __calculate_output(): Private method which calculates the output of the gate. This output is
not(input_n and input_n+1)
for all the inputs of the gate.
OR gate
The OR gate is a logic gate with two or more inputs which implements the logical disjunction (∨). Its output is True if any of the inputs is True, and False only when all the gate's inputs are set to False. Below you can find the OR gate truth table.
Attributes
The OR gate has the following attributes:
Name | Description | Type |
---|---|---|
input | List of inputs of the gate | List[bool] |
output | Output of the gate | bool |
numOfInputs | Number of inputs of the gate | int |
Constructor
The constructor of the OR gate has the following format:
Or(int inputNumber)
Which takes the inputNumber parameter, an integer set to two by default which can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the or gate calculate output method and numOfInputs will take the same value as the parameter inputNumber.Methods
- get_input(int num): Gets the value of the input num. It returns the boolean value in Output[num]
- get_output(): Gets the output of the gate. It returns a bool containing the requested value.
- get_numOfInputs(): Gets the number of inputs of the gate. It returns an int containing the requested value.
- set_input(int num, bool value): Sets the input 'num' to the truth value 'value'. Both of them are passed as parameters to the method.
- add_input(): Adds a new input to the gate, which defaults to False and updates both output and numOfInputs.
- remove_input(): Removes the last input and updates both output and numOfInputs.
- __calculate_output(): Private method which calculates the output of the gate. This output is
(input_n or input_n+1)
for all the inputs of the gate.
XOR gate
The XOR gateis a logic gate with two or more inputs whose output is True when the number of True inputs is odd. In any other case, the output value is False. Below you can find the OR gate truth table.
Attributes
The XOR gate has the following attributes:
Name | Description | Type |
---|---|---|
input | List of inputs of the gate | List[bool] |
output | Output of the gate | bool |
numOfInputs | Number of inputs of the gate | int |
Constructor
The constructor of the XOR gate has the following format:
Xor(int inputNumber)
Which takes the inputNumber parameter, an integer set to two by default which can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the xor gate calculate output method and numOfInputs will take the same value as the parameter inputNumber.Methods
- get_input(int num): Gets the value of the input num. It returns the boolean value in Output[num]
- get_output(): Gets the output of the gate. It returns a bool containing the requested value.
- get_numOfInputs(): Gets the number of inputs of the gate. It returns an int containing the requested value.
- set_input(int num, bool value): Sets the input 'num' to the truth value 'value'. Both of them are passed as parameters to the method.
- add_input(): Adds a new input to the gate, which defaults to False and updates both output and numOfInputs.
- remove_input(): Removes the last input and updates both output and numOfInputs.
- __calculate_output(): Private method which calculates the output of the gate. This output is
[(input_n and not(input_n+1)) or (not(input_n) and input_n+1)]
for all the inputs of the gate.
NOR gate
The NOR gate is a logic gate which can take two or more inputs. Its output is True only when all the inputs are False. If any of the inputs is True, the output will be False. In other words, its output is calculated by negating the disjunction of all inputs of the gate. Below you can find the NOR gate truth table.
Attributes
The NOR gate has the following attributes:
Name | Description | Type |
---|---|---|
input | List of inputs of the gate | List[bool] |
output | Output of the gate | bool |
numOfInputs | Number of inputs of the gate | int |
Constructor
The constructor of the NOR gate has the following format:
Nor(int inputNumber)
Which takes the inputNumber parameter, an integer set to two by default which can be changed to have more inputs to the gate. The Input will be initialized to a list of False values, containing as many values as inputs specified, Output will be calculated by the Nor gate calculate output method and numOfInputs will take the same value as the parameter inputNumber.Methods
- get_input(int num): Gets the value of the input num. It returns the boolean value in Output[num]
- get_output(): Gets the output of the gate. It returns a bool containing the requested value.
- get_numOfInputs(): Gets the number of inputs of the gate. It returns an int containing the requested value.
- set_input(int num, bool value): Sets the input 'num' to the truth value 'value'. Both of them are passed as parameters to the method.
- add_input(): Adds a new input to the gate, which defaults to False and updates both output and numOfInputs.
- remove_input(): Removes the last input and updates both output and numOfInputs.
- __calculate_output(): Private method which calculates the output of the gate. This output is
not(input_n or input_n+1)
for all the inputs of the gate.
Multiplexers Module
Here is an in depth explanation for the PyCircTools.Multiplexers module. Multiplexers adds implementation for multiplexers ranging from 2-to-1 to 16-to-1. For further reference about each of the multiplexers, check its section in this README.
2-to-1 Multiplexer
The 2-to-1 Multiplexer adds the implementation for this circuit element. Below you will find a picture depicting this mux and its equation.
Attributes
The 2-to-1 Multiplexer has the following attributes:
Name | Description | Type |
---|---|---|
input | Input of the multiplexer | list[bool] |
set | Set control signals of the multiplexer | bool |
output | Output of the multiplexer | bool |
Note that for this multiplexer, the number of inputs is two and the number of control signals is one.
Constructor
The constructor of the 2-to-1 Multiplexer has the following format:
Mux2to1()
Which doesn't take any parameters and returns a 2-to-1 Multiplexer with its two inputs set to False, its set control signal set to False and output calculated by the 2-to-1 Mux gate calculate output methodMethods
The 2-to-1 Multiplexer has the following methods:
-
get_input(int num): Gets the value of the input num. It returns the boolean value in input[num].
-
get_set(): Gets the value of the set control signal. It returns a bool with the requested value.
-
get_output(): Gets the output of the mux. It returns a bool containing the requested value.
-
set_input(int num, bool value): Sets the input[num] of the mux to the truth value 'value', which is passed as a parameter.
-
set_set(bool value): Sets the value of the set control signal to 'value'.
- __calculate_output(): Private method which calculates the output of the mux. This output is showed in 2-to-1 Multiplexer chapter.
4-to-1 Multiplexer
The 4-to-1 Multiplexer adds the implementation for this circuit element. Below you will find a picture depicting this mux and its equation.
Attributes
The 4-to-1 Multiplexer has the following attributes:
Name | Description | Type |
---|---|---|
input | Input of the multiplexer | list[bool] |
set | Set control signals of the multiplexer | list[bool] |
output | Output of the multiplexer | bool |
Note that for this multiplexer, the number of inputs is four and the number of control signals is two.
Constructor
The constructor of the 4-to-1 Multiplexer has the following format:
Mux4to1()
Which doesn't take any parameters and returns a 4-to-1 Multiplexer with its four inputs set to False, its set control signals set to False and output calculated by the 4-to-1 Mux gate calculate output methodMethods
The 4-to-1 Multiplexer has the following methods:
-
get_input(int num): Gets the value of the input num. It returns the boolean value in input[num].
-
get_set(int setNum): Gets the value of the set control signal setNum. It returns a bool with the requested value.
-
get_output(): Gets the output of the mux. It returns a bool containing the requested value.
-
set_input(int num, bool value): Sets the input[num] of the mux to the truth value 'value', which is passed as a parameter.
-
set_set(int setNum, bool value): Sets the value of the set control signal set[setNum] to 'value'. Both are passed as parameters.
- __calculate_output(): Private method which calculates the output of the mux. This output is showed in 4-to-1 Multiplexer chapter.
8-to-1 Multiplexer
The 8-to-1 Multiplexer adds the implementation for this circuit element. It has 8 inputs and the output value is selected by the input number in the set signal.
Attributes
The 8-to-1 Multiplexer has the following attributes:
Name | Description | Type |
---|---|---|
input | Input of the multiplexer | list[bool] |
set | Set control signals of the multiplexer | list[bool] |
output | Output of the multiplexer | bool |
Note that for this multiplexer, the number of inputs is eight and the number of control signals is three.
Constructor
The constructor of the 8-to-1 Multiplexer has the following format:
Mux8to1()
Which doesn't take any parameters and returns an 8-to-1 Multiplexer with its four inputs set to False, its set control signals set to False and output calculated by the 8-to-1 Mux gate calculate output methodMethods
The 8-to-1 Multiplexer has the following methods:
-
get_input(int num): Gets the value of the input num. It returns the boolean value in input[num].
-
get_set(int setNum): Gets the value of the set control signal setNum. It returns a bool with the requested value.
-
get_output(): Gets the output of the mux. It returns a bool containing the requested value.
-
set_input(int num, bool value): Sets the input[num] of the mux to the truth value 'value', which is passed as a parameter.
-
set_set(int setNum, bool value): Sets the value of the set control signal set[setNum] to 'value'. Both are passed as parameters.
- __calculate_output(): Private method which calculates the output of the mux. This output is showed in 8-to-1 Multiplexer chapter.
16-to-1 Multiplexer
The 16-to-1 Multiplexer adds the implementation for this circuit element. It has 16 inputs and the output value is selected by the input number in the set signal.
Attributes
The 16-to-1 Multiplexer has the following attributes:
Name | Description | Type |
---|---|---|
input | Input of the multiplexer | list[bool] |
set | Set control signals of the multiplexer | list[bool] |
output | Output of the multiplexer | bool |
Note that for this multiplexer, the number of inputs is sixteen and the number of control signals is four.
Constructor
The constructor of the 8-to-1 Multiplexer has the following format:
Mux16to1()
Which doesn't take any parameters and returns a 16-to-1 Multiplexer with its sixteen inputs set to False, its set control signals set to False and output calculated by the 16-to-1 Mux gate calculate output methodMethods
The 16-to-1 Multiplexer has the following methods:
-
get_input(int num): Gets the value of the input num. It returns the boolean value in input[num].
-
get_set(int setNum): Gets the value of the set control signal setNum. It returns a bool with the requested value.
-
get_output(): Gets the output of the mux. It returns a bool containing the requested value.
-
set_input(int num, bool value): Sets the input[num] of the mux to the truth value 'value', which is passed as a parameter.
-
set_set(int setNum, bool value): Sets the value of the set control signal set[setNum] to 'value'. Both are passed as parameters.
- __calculate_output(): Private method which calculates the output of the mux. This output is showed in 16-to-1 Multiplexer chapter.
Exceptions
CTools implements the following Exceptions. They are divided depending on which module they are intended to use.
PyCircTools Exceptions
These are the general Exceptions used all over the PyCircTools library. An explanation of each one follows below.
NotTruthValue Exception
NotTruthValue is an exception that raises when an input which is expected to be a truth value (either True or False) is of another data type.
Logic gates Exceptions
These are the exceptions used in the PyCircTools.LogicGates module. An explanation of each one follows below.
NonPositiveInput
NonPositiveInput is raised when the number of inputs for a Logic Gate is lower than 1, as this is the minimum number of inputs of any logic gate.
NotAnInput
NotAnInput is raised when the selected input does not exist.
Multiplexers Exceptions
These are the exceptions used in the PyCircTools.Multiplexers module. An explanation of each one follows below.
NonExistingInput
NonExistingInput is raised when a Multiplexer/Demultiplexer doesn't have the input asked for. It can take an int as an input. This input 'requestedInput' is the number which caused the exception.
NonExistingControlSignal
NonExistingControlSignal is raised when a Multiplexer/Demultiplexer doesn't have the set control signal asked for.
About
PyCircTools is software developed by LovetheFrogs and licensed under GPL-3.0 license.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for PyCircTools-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61cf2c96897e047562d493ca0b9f568846155428b91972e40b079009b90ff32b |
|
MD5 | 263874c3f1c0b3aadc632994f55efeea |
|
BLAKE2b-256 | 40dee95cfedad610796cf6484fef48ded4429e31ef5a3f0d755ae3c60e2bc30c |