CTools, a python circuit library.
Project description
CTools
CTools is a python package wich 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 CTools and importing it
- 2. Logic Gates Module
- Exceptions
- About
Installing CTools and importing it.
To install CTools, use pip by calling the following command:
pip install CTools
Importing it is as easy as it gets, just insert the line
from CTools.*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 CTools.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 it's section in this README.
NOT gate
The NOT gate is a simple logic gate wich 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()
Wich 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', wich is passed as a parameter.
- __calculate_output(): Private method wich 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 of the inputs are True. If any of them is set to False, the output willthen 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)
Wich takes the inputNumber parameter, an integer set to two by default wich 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, wich 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 wich calculates the output of the gate. This output is:
(input_n and input_n+1)
for all of the inputs of the gate.
NAND gate
The NAND gate is a logic gate with two or more inputs wich 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)
Wich takes the inputNumber parameter, an integer set to two by default wich 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, wich 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 wich calculates the output of the gate. This output is
not(input_n and input_n+1)
for all of the inputs of the gate.
OR gate
The OR gate is a logic gate with two or more inputs wich implements the logical disjunction (∨). It's output is True if any of the inputs is True, and False only when all of 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)
Wich takes the inputNumber parameter, an integer set to two by default wich 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, wich 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 wich calculates the output of the gate. This output is
(input_n or input_n+1)
for all of 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)
Wich takes the inputNumber parameter, an integer set to two by default wich 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, wich 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 wich 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 of the inputs of the gate.
NOR gate
The NOR gate is a logic gate wich can take two or more inputs. Its output is True only when all of 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)
Wich takes the inputNumber parameter, an integer set to two by default wich 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, wich 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 wich calculates the output of the gate. This output is
not(input_n or input_n+1)
for all of the inputs of the gate.
Exceptions
CTools implements the following Exceptions. They are divided depending on wich module they are intended to use.
CTools Exceptions
These are the general Exceptions used all over the CTools 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 CTools.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.
About
CTools 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.0.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c927c28a951f69769719e2975c81a119dd2b77319a5a3a5bf82f7c2a5b810bb0 |
|
MD5 | ef98ecfd98ef587a03de58d34b558b7c |
|
BLAKE2b-256 | dd49a00078fec4b5516597ca7c333f772115219038bdf770fab90493743eb348 |