This class is the primary medium for data transfer. Objects of this class can be connected to any digital object.
>>> from BinPy import *
>>> conn = Connector(1) #Initializing connector with initial state = 1
>>> conn.state
1
>>> gate = OR(0, 1)
>>> conn.tap(gate, 'output') #Tapping the connector
Bases: BinPy.Gates.gates.MIGATES
This class implements AND gate
>>> from BinPy import *
>>> gate = AND(0, 1)
>>> gate.output()
0
>>> gate.setInputs(1, 1, 1, 1)
>>> gate.output()
1
>>> conn = Connector()
>>> gate.setOutput(conn)
>>> gate2 = AND(conn, 1)
>>> gate2.output()
1
Base Class implementing all common functions used by Logic Gates
Returns a string representation of a gate, where gate_name is the class name For example, for an AND gate with two inputs the resulting string would be: ‘AND Gate; Output: 0; Inputs: [0, 1];’
This method is used to add input to a gate. It requires an index and a value/connector object to add an input to the gate.
Bases: BinPy.Gates.gates.GATES
This class makes GATES compatible with multiple inputs.
Bases: BinPy.Gates.gates.MIGATES
This class implements NAND gate
>>> from BinPy import *
>>> gate = NAND(0, 1)
>>> gate.output()
1
Bases: BinPy.Gates.gates.MIGATES
This class implements NOR gate
>>> from BinPy import *
>>> gate = NOR(0, 1)
>>> gate.output()
0
Bases: BinPy.Gates.gates.GATES
This class implements NOT gate
>>> from BinPy import *
>>> gate = NOT(0)
>>> gate.output()
1
>>> conn = Connector()
>>> gate.setOutput(conn)
>>> gate2 = AND(conn, 1)
>>> gate2.output()
1
Bases: BinPy.Gates.gates.MIGATES
This class implements OR gate
>>> from BinPy import *
>>> gate = OR(0, 1)
>>> gate.output()
1
>>> gate.setInputs(0, 0, 0, 0)
>>> gate.output()
0
>>> conn = Connector()
>>> gate.setOutput(conn)
>>> gate2 = AND(conn, 1)
>>> gate2.output()
0
Bases: BinPy.Gates.gates.MIGATES
This class implements XNOR gate
>>> from BinPy import *
>>> gate = XNOR(0, 1)
>>> gate.output()
0
>>> gate.setInputs(1, 0, 1, 0)
>>> gate.output()
1
>>> conn = Connector()
>>> gate.setOutput(conn)
>>> gate2 = AND(conn, 1)
>>> gate2.output()
1
Bases: BinPy.Gates.gates.MIGATES
This class implements XOR gate
>>> from BinPy import *
>>> gate = XOR(0, 1)
>>> gate.output()
1
>>> gate.setInputs(1, 0, 1, 0)
>>> gate.output()
0
>>> conn = Connector()
>>> gate.setOutput(conn)
>>> gate2 = AND(conn, 1)
>>> gate2.output()
0
This class helps to keep the cycle history of a circuit by registering occurrences of a digital element. The class has a dictionary that stores an instance of CycleHistValue for each key element.
Get the repetition index for the given element
Keyword arguments: element – A digital element in the dictionary
This class represents the value in the dictionary of the CycleHist class. It has the index of the element and if it has been repeated or not.
Check if the element for which this instance is associated is repeated or not.
This class is a tree representation of a digital element, such as a gate, and its inputs. The class uses the backtrack() function which follows the element and tracks the inputs, and inputs of inputs, and so on, thus constructing the backtrack tree.
The tree construction has the possibility to not follow cycles so the final output is simpler.
The printTree() function can be used to print the Tree in a readable way. The following examples show two use cases, one of which shows what happens if cycles are not being followed.
>>> g1 = AND(True, False)
>>> g2 = AND(True, False)
>>> g3 = AND(g1, g2)
>>> tree = Tree(g3, 2)
>>> tree.backtrack()
>>> tree.printTree()
|- AND Gate; Output: 0; Inputs: [0, 0];
|- AND Gate; Output: 0; Inputs: [True, False];
|- True
|- False
|- AND Gate; Output: 0; Inputs: [True, False];
|- True
|- False
If the algorithm was executed to not follow cycles, the output will have marks indicating repetitions. In the following example the elements marked with [0] are the same and have no sons to avoid repetitive output. The same for the elements with [1].
>>> c1 = Connector(True)
>>> c2 = Connector(True)
>>> g1 = AND(True, c1)
>>> g2 = AND(c2, False)
>>> g3 = AND(g1, g2)
>>> g4 = AND(g3, True)
>>> g3.setOutput(c1)
>>> g4.setOutput(c2)
|- [1] AND Gate; Output: 0; Inputs: [0, True];
|- [0] AND Gate; Output: 0; Inputs: [0, 0];
|- AND Gate; Output: 0; Inputs: [True, 0];
|- True
|- Connector; State: 0
|- [0] AND Gate; Output: 0; Inputs: [0, 0];
|- AND Gate; Output: 0; Inputs: [0, False];
|- Connector; State: 0
|- [1] AND Gate; Output: 0; Inputs: [0, True];
|- False
|- True
Constructs the backtrack hierarchy of the tree up to self.depth.
Keyword arguments: hist – An instance of CycleHist. A class which maintains the passed
tracked if backtrack is not following cycles. Should only be used internally.
This function prints the tree in a readable way. The way a gate, or a mux or any other digital element gets represented depends on it’s __str__() implementation.
Keyword arguments: space – Number of spaces which are going to be printed in each
recursive step. Should only be used internally. (default 0)