def testGlobalActivation(self):
        self.assertEqual(self.greeting.__str__(), "Hello")
        globalActivateLayer(whoLayer)
        self.assertEqual(self.greeting.__str__(), "Hello World")
        globalActivateLayer(detailsLayer)
        self.assertEqual(self.greeting.__str__(), "Hello World from Potsdam in 2008")
        globalDeactivateLayer(whoLayer)
        self.assertEqual(self.greeting.__str__(), "Hello from Potsdam in 2008")
        globalDeactivateLayer(detailsLayer)
        self.assertEqual(self.greeting.__str__(), "Hello")

        # Test Exception Handling
        globalActivateLayer(whoLayer)
        self.assertRaises(ValueError, globalActivateLayer, whoLayer)
        globalDeactivateLayer(whoLayer)
        self.assertRaises(ValueError, globalDeactivateLayer, whoLayer)
Beispiel #2
0
    print "Base\n"


@around(a_layer)
def test():
    print "Layer A"
    proceed()


@around(b_layer)
def test():
    print "Layer B"
    proceed()


globalActivateLayer(a_layer)
globalActivateLayer(b_layer)
test()
globalDeactivateLayer(b_layer)
globalDeactivateLayer(a_layer)

globalActivateLayer(b_layer)
globalActivateLayer(a_layer)
test()
globalDeactivateLayer(a_layer)
globalDeactivateLayer(b_layer)

with activelayer(a_layer):
    with activelayer(b_layer):
        test()
    @around(time_layer)
    def __str__(self):
        return proceed() + self.datetime.strftime(format=", %H:%M")

    @around(location_layer)
    def __str__(self):
        return proceed() + ", " + self.location


appointment = Appointment()

print "Global Layer Activation:"

print appointment
globalActivateLayer(time_layer)
print appointment
globalActivateLayer(location_layer)
print appointment
globalDeactivateLayer(time_layer)
print appointment
globalDeactivateLayer(location_layer)
print appointment

print "---"
print "Thread-Local Layer Activation:"

with activelayers(time_layer, location_layer):
    print appointment
    with inactivelayer(time_layer):
        print appointment
#!/usr/bin/python2

from contextpy import (
  base, around, activelayer, inactivelayer, globalActivateLayer, layer, proceed)

test_a_layer = layer("TestA")
test_b_layer = layer("TestB")

@base
def test():
    print "Base\n"

@around(test_a_layer)
def test():
    print "Test A"
    proceed()
@around(test_b_layer)
def test():
    print "Test B"
    proceed()
    
globalActivateLayer(test_a_layer)

with activelayer(test_b_layer):
    test() # A and B active (A from global, B from local stack)

with activelayer(test_a_layer):
    test() # A active twice (global and local stack)

with inactivelayer(test_a_layer):
    test() # A active anyway (from global stack)