Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. class LogicGate:  # Clase llamada LogicGate
  2.     def __init__(self,n):  # Constructor de la clase LogicGate
  3.         self.name = n  # Variable label obtiene el valor por parametro de n
  4.         self.output = None  # Variable Output obtiene el valor nulo
  5.  
  6.     def getName(self):  # Metodo de la clase LogicGate llamado getLabel
  7.         return self.name  # Devuelve de label(getter)
  8.  
  9.     def getOutput(self):  # Metodo de la clase LogicGate llamado geOutput
  10.         self.output = self.performGateLogic()  # Variable Output obtiene el valor return del metodo performGateLogic
  11.         return self.output  # Devuelve el valor obtenido
  12.  
  13. class BinaryGate(LogicGate):  # Clase BinaryGate hereda LogicGate
  14.     def __init__(self,n):  # Constructor de BinaryGate con parametro n
  15.         LogicGate.__init__(self,n)  # Declara el constructor de la clase LogicGate
  16.         self.pinA = None  # Variable pinA valor nulo
  17.         self.pinB = None  # Variable pinB valor nulo
  18.  
  19.     def getPinA(self):  # Metodo de la clase BinaryGate llamado getPinA
  20.         if self.pinA == None:
  21.             return int(input("Enter Pin A input for gate "+self.getName()+"-->"))  #  Cambia el tipo de dato a entero + obtiene el valor del usuario +
  22.                                                                                  #  Utiliza Inherits para llamar el metodo getLabel de LogicGate
  23.         else:
  24.             return self.pinA.getFrom().getOutput()
  25.  
  26.     def getPinB(self):  # Metodo de la clase BinaryGate llamado getPinB
  27.         if self.pinB == None:
  28.             return int(input("Enter Pin B input for gate "+self.getName()+"-->"))  # Cambia el tipo de dato a entero + obtiene el valor del usuario
  29.         else:
  30.             return self.pinB.getFrom().getOutput()
  31.  
  32.     def setNextPin(self,source):  # Metodo llamado setNextPin con source por parametro
  33.         if self.pinA == None:  # If PinA es nulo
  34.             self.pinA = source  # Se le otorga el valor a PinA
  35.         else:
  36.             if self.pinB == None:
  37.                 self.pinB = source
  38.             else:
  39.                 print("Cannot Connect: NO EMPTY PINS on this gate")
  40.  
  41.  
  42. class AndGate(BinaryGate):  # Clase llamada AndGate hereda BinaryGate
  43.     def __init__(self,n):  # Constructor de la clase con this y n por parametro
  44.         BinaryGate.__init__(self,n)
  45.  
  46.     def performGateLogic(self):  # Metodo performGateLogic de la clase
  47.         a = self.getPinA()  # a obtiene el valor devuelto de getPinA clase heredada
  48.         b = self.getPinB()
  49.         if a==1 and b==1:  # Condicion para ejecutar codigo si a & b son 1
  50.             return 1  # Devuelve
  51.         else:
  52.             return 0
  53.  
  54. class OrGate(BinaryGate):
  55.     def __init__(self,n):
  56.         BinaryGate.__init__(self,n)
  57.  
  58.     def performGateLogic(self):
  59.         a = self.getPinA()
  60.         b = self.getPinB()
  61.         if a ==1 or b==1:
  62.             return 1
  63.         else:
  64.             return 0
  65.  
  66. class UnaryGate(LogicGate):  # Clase llamada UnaryGate hereda LogicGate
  67.     def __init__(self,n):  # Constructor de la clase UnaryGate
  68.         LogicGate.__init__(self,n)  # Declara constructor de LogicGate
  69.         self.pin = None  # Pin nulo
  70.  
  71.     def getPin(self):  # Metodo llamado getPin
  72.         if self.pin == None:
  73.             return int(input("Enter Pin input for gate "+self.getName()+"-->"))  # Devuelve en tipo entero el input del usuario y llama al metodo
  74.                                                                                # getLabel heredado de LogicGate
  75.         else:
  76.             return self.pin.getFrom().getOutput()
  77.  
  78.     def setNextPin(self,source):
  79.         if self.pin == None:
  80.             self.pin = source
  81.         else:
  82.             print("Cannot Connect: NO EMPTY PINS on this gate")
  83.  
  84.  
  85. class NotGate(UnaryGate):  # Clase llamada NotGate
  86.     def __init__(self,n):  # Constructor
  87.         UnaryGate.__init__(self,n)
  88.        
  89.     def performGateLogic(self):  # Metodo llamado performGateLogic
  90.         if self.getPin():
  91.             return 0
  92.         else:
  93.             return 1
  94.  
  95.  
  96. class Connector:  # Clase llamada conector
  97.     def __init__(self, fgate, tgate):  # Constructor de la clase & contiene dos parametros
  98.         self.fromgate = fgate  # Otorga el valor del parametro a la variable
  99.         self.togate = tgate
  100.         tgate.setNextPin(self)
  101.  
  102.     def getFrom(self):  # Metodo Getter
  103.         return self.fromgate
  104.  
  105.     def getTo(self):  # Metodo Getter
  106.         return self.togate
  107.  
  108.  
  109. def main():
  110.     gateOne = AndGate("gateOne")
  111.     # Se declara un objeto de la clase AndGate esta hereda BinaryGate y luego es heredada por la SuperClass LogicGate
  112.     # pasando el valor de n(parametro) por todas las clases. Luego para calcularlo e imprimirlo es llamado con print el metodo getOutput().
  113.     # performGateLogic() este metodo tiene dos variables a = int(input) al igual que b luego es evaluado si ambos son 1 return 1 else 0.
  114.     print(gateOne.getOutput())
  115.     gateTwo = OrGate("gateTwo")
  116.     # Para el OrGate es exactamente igual que AndGate ya que ambas utilizan la clase BinaryGate
  117.     # se ejectuta de la misma manera.
  118.     gateThree = NotGate("gateThree")
  119.     # NotGate utiliza la clase UnaryGate y esta hereda a LogicGate lleva por parametro el string y lo guarda en LigicGate Class Name.
  120.     # Termina ciendo ejecutada igualmente con la unica diferencia que devuelve lo contrario de AndGate.
  121.     connectorOne = Connector(gateOne,gateTwo)
  122.     print("")
  123.     # Connector es una clase utilizada para cambiar a un tipo de boolean diferente tiene dos variables en su constructor y son igualadas a los
  124.     # objetos dados por parametro. Luego se ejecuta el metodo setNextPin() que es parte de los objetos enviados por parametro.
  125.  
  126. main()