def runMethodWithNestedLayers5(self, label):
     timer = Timer()
     self.resetCounter()
     consumedTime = 0.0
     rangeList = range(16) # Speed Up
     size = INITSIZE
     while consumedTime < TARGETTIME and size < MAXSIZE:
         timer.resetTimer()
         timer.startTimer()
         for i in xrange(size):
             for j in rangeList:
                 with activelayer(l01):
                     self.m1()
                     with activelayer(l02):
                         self.m2()
                         with activelayer(l03):
                             self.m3()
                             with activelayer(l04):
                                 self.m4()
                                 with activelayer(l05):
                                     self.m5()
         timer.stopTimer()
         consumedTime = timer.readTimer()
         timer.addOpsToTimer(16*size)
         size *= 2
     print "%s\t%s" % (label, timer.printPerfTimer())
 def testMultipleActivation(self):
     self.assertEqual(self.greeting.__str__(), "Hello")
     with activelayer(detailsLayer):
         self.assertEqual(self.greeting.__str__(), "Hello from Potsdam in 2008")
         with activelayer(detailsLayer):
             self.assertEqual(self.greeting.__str__(), "Hello from Potsdam in 2008")
             with activelayer(whoLayer):
                 self.assertEqual(self.greeting.__str__(), "Hello from Potsdam in 2008 World")
 def testInheritance(self):
     greetings = GermanGreeting("Hallo", "Welt", "Potsdam", 2008)
     self.assertEqual(greetings.__str__(), "Hallo")
     with activelayer(whoLayer):
         self.assertEqual(greetings.__str__(), "Hallo Welt Aus: Welt")
     with activelayer(detailsLayer):
         self.assertEqual(greetings.__str__(), "German: Hallo from Potsdam in 2008")
         with activelayer(whoLayer):
             self.assertEqual(greetings.__str__(), "German: Hallo from Potsdam in 2008 Welt Aus: Welt")
 def testFunctions(self):
     self.assertEqual(answerFunction("Hello World"), "Hello World")
     with activelayer(whoLayer):
         self.assertEqual(answerFunction("Hello World"), "answerFunction: Hello World")
     with activelayer(detailsLayer):
         self.assertEqual(answerFunction("Hello World"), "Hello World (Normal Python Module Function)")
         with activelayer(whoLayer):
             self.assertEqual(answerFunction("Hello World"), "answerFunction: Hello World (Normal Python Module Function)")
     self.assertEqual(answerFunction("Hello World"), "Hello World")
 def testStaticMethods(self):
     self.assertEqual(Address.staticAddress("Test Address"), "Address: Test Address")
     self.assertEqual(Address("city", "street", 123).staticAddress("Test Address"), "Address: Test Address")
     with activelayer(detailsLayer):
         self.assertEqual(Address.staticAddress("Test Address"), "Address: Test Address More Details")
     with activelayer(whoLayer):
         self.assertEqual(Address.staticAddress("Test Address"), "Address: Test Address After")
         with activelayer(detailsLayer):
             self.assertEqual(Address("city", "street", 123).staticAddress("Test Address"), "Address: Test Address More Details After")
 def testCrossCutLayer(self):
     self.assertEqual(self.greeting.__str__(), "Hello")
     with activelayer(detailsLayer):
         self.assertEqual(self.greeting.__str__(), "Hello from Potsdam in 2008")
         self.greeting.setYear(1999)
         self.assertEqual(self.greeting.__str__(), "Hello from Potsdam in 500")
     self.assertEqual(self.greeting.year, 500)
     self.assertEqual(self.address.__str__(), "Potsdam")
     self.greeting.setYear(2008)
     self.assertEqual(self.address.__str__(), "Potsdam")
     with activelayer(detailsLayer):
         self.assertEqual(self.greeting.__str__(), "Hello from Potsdam in 2008")
         self.assertEqual(self.address.__str__(), "Saarmunder Str. 9 Potsdam 14478")
 def testWithDoubleLayer(self):
     self.assertEqual(self.greeting.__str__(), "Hello")
     with activelayer(detailsLayer):
         self.assertEqual(self.greeting.__str__(), "Hello from Potsdam in 2008")
         with activelayer(whoLayer):
             self.assertEqual(self.greeting.__str__(), "Hello from Potsdam in 2008 World")
             with inactivelayer(whoLayer):
                 self.assertEqual(self.greeting.__str__(), "Hello from Potsdam in 2008")
         self.assertEqual(self.greeting.__str__(), "Hello from Potsdam in 2008")
     with activelayers(detailsLayer, whoLayer):
         self.assertEqual(self.greeting.__str__(), "Hello from Potsdam in 2008 World")
     with activelayers(whoLayer, detailsLayer):
         self.assertEqual(self.greeting.__str__(), "Hello World from Potsdam in 2008")
     self.assertEqual(self.greeting.__str__(), "Hello")
 def testLateMethodBinding(self):
     germanGreet = GermanGreeting("Hallo", "Welt", "Potsdam", 2008)
     self.assertRaises(AttributeError, getattr, germanGreet, "hallo")
     GermanGreeting.hallo = hallo
     self.assertEqual(germanGreet.hallo("Hallo"), "Hallo")
     with activelayer(detailsLayer):
         self.assertEqual(germanGreet.hallo("Hallo"), "Deutsch: Hallo")
 def testYearLayer(self):
     self.assertEqual(self.greeting.year, 2008)
     self.greeting.setYear(1999)
     self.assertEqual(self.greeting.year, 1999)
     with activelayer(yearLayer):
         self.greeting.setYear(1998)
     self.assertEqual(self.greeting.year, 2000)
     self.greeting.setYear(1998)
     self.assertEqual(self.greeting.year, 1998)
Exemple #10
0
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()

with activelayer(b_layer):
    with activelayer(a_layer):
        test()

with activelayers(a_layer, b_layer):
    test()

with activelayers(b_layer, a_layer):
    test()
#!/usr/bin/python2

from contextpy import base, around, activelayer, layer, proceed

employerLayer = layer("Employer")

class Person(object):
    def __init__(self, name, employer):
        self.name = name
        self.employer = employer

    @base
    def getDetails(self):
        return self.name

    @around(employerLayer)
    def getDetails(self):
        return proceed() + "\n" + self.employer

person = Person("Michael Perscheid" , "HPI")

print person.getDetails()

with activelayer(employerLayer):
    print person.getDetails()
 def testWithSingleLayer(self):
     self.assertEqual(self.greeting.__str__(), "Hello")
     with activelayer(whoLayer):
         self.assertEqual(self.greeting.__str__(), "Hello World")
     self.assertEqual(self.greeting.__str__(), "Hello")
#!/usr/bin/python2

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

layer_a = layer("A")


class TestClass:
    @base
    @classmethod
    def test_method(cls):
        print "Classmethod base."

    @around(layer_a)
    @classmethod
    def test_method(cls):
        print "Classmethod around."


TestClass.test_method()

with activelayer(layer_a):
    TestClass.test_method()
#!/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)