Skip to content

Instantly share code, notes, and snippets.

@xlarsx
Created March 4, 2011 20:01

Revisions

  1. xlarsx created this gist Mar 4, 2011.
    52 changes: 52 additions & 0 deletions UsoPythonWith.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    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