Skip to content

Instantly share code, notes, and snippets.

@marioidival
Last active August 29, 2015 14:26
Show Gist options
  • Select an option

  • Save marioidival/4f60f097299950e836a2 to your computer and use it in GitHub Desktop.

Select an option

Save marioidival/4f60f097299950e836a2 to your computer and use it in GitHub Desktop.
from zope import interface
from zope import component
class ISocket(interface.Interface):
pins = interface.Attribute("""pins of socket""")
class ISocket2(ISocket):
def connect_socket_2_pins():
"""Connect in socket"""
class ISocket3(ISocket):
def connect_socket_3_pins():
"""Connect in socket"""
@interface.implementer(ISocket2)
class Socket2:
pins = 2
def connect_socket_2_pins(self):
print("Socket connect with {} pins".format(self.pins))
@interface.implementer(ISocket3)
class Socket3:
pins = 3
def connect_socket_3_pins(self):
print("Socket connect with {} pins".format(self.pins))
@interface.implementer(ISocket2)
class Adapter3to2:
component.adapts(ISocket3)
pins = 2
def __init__(self, socket):
self.context = socket
def connect_socket_2_pins(self):
print("Use socket of 3 pins")
self.context.connect_socket_3_pins()
app = component.getGlobalSiteManager()
app.registerUtility(Socket2(), ISocket, "socket_usa")
app.registerUtility(Socket3(), ISocket, "socket_br")
app.registerAdapter(Adapter3to2, (ISocket3,), ISocket2)
sock2 = app.getUtility(ISocket, "socket_usa")
sock3 = app.getUtility(ISocket, "socket_br")
adapter = app.getAdapter(sock3, ISocket2)
sock2.connect_socket_2_pins()
sock3.connect_socket_3_pins()
adapter.connect_socket_2_pins()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment