Skip to content

Instantly share code, notes, and snippets.

@xlarsx
Created March 4, 2011 20:01
Show Gist options
  • Save xlarsx/855613 to your computer and use it in GitHub Desktop.
Save xlarsx/855613 to your computer and use it in GitHub Desktop.
class Canvas(object):
def __init__(self, lock):
self.lock = lock
def __enter__(self):
ContenedorCanvas.estableceObjeto(self)
return self
def __exit__(self, type, value, tb):
ContenedorCanvas.terminaUsarObjeto(self)
def obtenTexto(self):
return self.lock
class Scatter(object):
def __init__(self, num):
self.canvas = Canvas("Canvas: " + str(num))
class ContenedorCanvas(object):
pilaObjetos = []
@staticmethod
def estableceObjeto(objeto):
if objeto not in ContenedorCanvas.pilaObjetos:
ContenedorCanvas.pilaObjetos.append(objeto)
@staticmethod
def terminaUsarObjeto(objeto):
if objeto in ContenedorCanvas.pilaObjetos:
ContenedorCanvas.pilaObjetos.remove(objeto)
@staticmethod
def obtenUltimoElemento():
return ContenedorCanvas.pilaObjetos[-1] if len(ContenedorCanvas.pilaObjetos) > 0 else None
class Rectangle(object):
def __init__(self):
ultimoElemento = ContenedorCanvas.obtenUltimoElemento()
if ultimoElemento is not None:
print "Dibujando rectangulo en %s" % ultimoElemento.obtenTexto()
node = Scatter(1)
node2 = Scatter(2)
with node.canvas:
Rectangle() # Solicitud de dibujar rectangulo en Canvas 1
with node2.canvas:
Rectangle() # Solicitud de dibujar rectangulo en Canvas 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment