BinPy.Gates package

Submodules

BinPy.Gates.connector module

class BinPy.Gates.connector.Connector(state=None)[source]

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
  • tap
  • untap
  • isInputof
  • isOutputof
  • trigger
isInputof(element)[source]
isOutputof(element)[source]
tap(element, mode)[source]
trigger()[source]
untap(element, mode)[source]

BinPy.Gates.gates module

Contains

  • GATES(Base class for all the gates)
  • MIGATES(Base class for multiple input gates inherits GATES)
  • AND
  • OR
  • NOT
  • XOR
  • XNOR
  • NAND
  • NOR
class BinPy.Gates.gates.AND(*inputs)[source]

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
trigger()[source]
class BinPy.Gates.gates.GATES(inputs)[source]

Base Class implementing all common functions used by Logic Gates

buildStr(gate_name)[source]

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];’

getInputStates()[source]

This method returns the input states of the gate

output()[source]

This methods returns the output of the gate.

setInput(index, value)[source]

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.

setInputs(*inputs)[source]

This method sets multiple inputs of the gate at a time. You can also use setInput() multiple times with different index to add multiple inputs to the gate.

setOutput(connector)[source]

This method sets the output of the gate. It connects the passed connector to its output.

class BinPy.Gates.gates.MIGATES(*inputs)[source]

Bases: BinPy.Gates.gates.GATES

This class makes GATES compatible with multiple inputs.

class BinPy.Gates.gates.NAND(*inputs)[source]

Bases: BinPy.Gates.gates.MIGATES

This class implements NAND gate

>>> from BinPy import *
>>> gate = NAND(0, 1)
>>> gate.output()
1
trigger()[source]
class BinPy.Gates.gates.NOR(*inputs)[source]

Bases: BinPy.Gates.gates.MIGATES

This class implements NOR gate

>>> from BinPy import *
>>> gate = NOR(0, 1)
>>> gate.output()
0
trigger()[source]
class BinPy.Gates.gates.NOT(*inputs)[source]

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
setInput(value)[source]
setInputs(*inputs)[source]
trigger()[source]
class BinPy.Gates.gates.OR(*inputs)[source]

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
trigger()[source]
class BinPy.Gates.gates.XNOR(*inputs)[source]

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
trigger()[source]
class BinPy.Gates.gates.XOR(*inputs)[source]

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
trigger()[source]

BinPy.Gates.tree module

Contains

  • Tree
  • CycleHist
  • CycleHistValue
class BinPy.Gates.tree.CycleHist[source]

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.

getIndex(element)[source]

Get the repetition index for the given element

Keyword arguments: element – A digital element in the dictionary

isRepeated(element)[source]

Check if the given element is repeating or not

Keyword arguments: element – The element that is being check if it is repeated or not.

regOccurrence(element)[source]

Register an occurrence for an element. If the element has been seen before, mark that element has a repeating element.

Keyword arguments: element – Any digital element to be added to the dictionary.

class BinPy.Gates.tree.CycleHistValue[source]

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.

getIndex()[source]

Get index of the element of this instance.

isRepeated()[source]

Check if the element for which this instance is associated is repeated or not.

setIndex(index)[source]

Set the index of the element for which this instance is associated.

Keyword arguments: index – The index in question.

setRepeated()[source]

Set is the element of this instance is repeated or not.

class BinPy.Gates.tree.Tree(element, depth=0, cycles=True)[source]

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
backtrack(hist=None)[source]

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.
printSpaces(space)[source]
printTree(space=0)[source]

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)
printTuple(tree_node, space=0)[source]
resetTree()[source]
setDepth(val)[source]

Sets depth until which the tree is constructed.

val – New depth.

Module contents

Table Of Contents

Previous topic

BinPy.Combinational package

Next topic

BinPy.Operations package

This Page