Skip to main content

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

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.

NOT gate

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.

AND gate

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.

NAND gate

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.

OR gate

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.

XOR gate

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.

NOR gate

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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

PyCircTools-0.0.3-py3-none-any.whl (19.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page