Gate

class LogicGate:

    def __init__(self,n: str) -> None:
        self.label = n
        self.output = None

    def getLabel(self) -> str:
        return self.label

    def getOutput(self):
        self.output = self.performGateLogic() # not implement yet
        return self.output
        
class BinaryGate(LogicGate):

    def __init__(self,n: str) -> None:
        LogicGate.__init__(self,n)

        self.pinA = None
        self.pinB = None

    def getPinA(self) -> int:
        return int(input("Enter Pin A input for gate "+ self.getLabel()+"-->"))

    def getPinB(self) -> int:
        return int(input("Enter Pin B input for gate "+ self.getLabel()+"-->"))
    
class UnaryGate(LogicGate):

    def __init__(self,n: str) -> None:
        LogicGate.__init__(self,n)

        self.pin = None

    def getPin(self) -> int:
        return int(input("Enter Pin input for gate "+ self.getLabel()+"-->"))
        
class AndGate(BinaryGate):

    def __init__(self,n: str) -> None:
        super(AndGate,self).__init__(n)

    def performGateLogic(self) -> int:

        a = self.getPinA()
        b = self.getPinB()
        if a==1 and b==1:
            return 1
        else:
            return 0
        
class OrGate(BinaryGate):

    def __init__(self,n: str) -> None:
        super(OrGate,self).__init__(n)

    def performGateLogic(self) -> int:

        a = self.getPinA()
        b = self.getPinB()
        if a==1 or b==1:
            return 1
        else:
            return 0
        
class NotGate(UnaryGate):

    def __init__(self,n: str) -> None:
        super(NotGate,self).__init__(n)

    def performGateLogic(self) -> int:

        a = self.getPin()
        if a==1:
            return 0
        else:
            return 1
        
g1 = AndGate('G1')
g1.getOutput()

g2 = OrGate('G2')
g2.getOutput()

g3 = NotGate('G3')
g3.getOutput()
        
Reference
  • Problem Solving with Algorithms and Data Structures using Python