#!/usr/bin/python2 from contextpy import (base, around, activelayer, activelayers, globalActivateLayer, globalDeactivateLayer, layer, proceed) a_layer = layer("LayerA") b_layer = layer("LayerB") @base def test(): 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)
#!/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()
#!/usr/bin/python2 from contextpy import ( base, around, activelayer, activelayers, globalActivateLayer, globalDeactivateLayer, layer, proceed) a_layer = layer("LayerA") @base def test(): print "Base\n" @around(a_layer) def test(): print "Layer A" proceed() globalActivateLayer(a_layer) globalActivateLayer(a_layer) test()
from __future__ import with_statement from contextpy import layer, proceed, activelayer, activelayers, inactivelayer, after, around, before, base, globalActivateLayer, globalDeactivateLayer import contextpy import time import gc INITSIZE = 200000 # was 10000 MAXSIZE = 1000000000 TARGETTIME = 5.0 l01 = layer("layer 1") l02 = layer("layer 2") l03 = layer("layer 3") l04 = layer("layer 4") l05 = layer("layer 5") l06 = layer("layer 6") l07 = layer("layer 7") l08 = layer("layer 8") l09 = layer("layer 9") l10 = layer("layer 10") # l11 = layer("layer 11") # l12 = layer("layer 12") # l13 = layer("layer 13") # l14 = layer("layer 14") class ContextPyMethodBench2(object): counter_00 = 0 counter_01 = 0 counter_02 = 0 counter_03 = 0
from __future__ import with_statement from contextpy import layer, proceed, activelayer, activelayers, inactivelayer, after, around, before, base, globalActivateLayer, globalDeactivateLayer import unittest whoLayer = layer("whoLayer") detailsLayer = layer("DetailsLayer") yearLayer = layer("YearLayer") class Greeting(object): def __init__(self, greets, who, fromPlace, year): self.greets = greets self.who = who self.fromPlace = fromPlace self.year = year def __str__(self): return self.greets @around(whoLayer) def __str__(self): return " ".join((proceed(), self.who)) @after(detailsLayer) def __str__(self, *args, **kwargs): return " ".join((kwargs["__result__"], "from", self.fromPlace, "in", str(self.year))) @around(detailsLayer) def setYear(self, value): self.year = 500
#!/usr/bin/python2 from contextpy import (layer, base, around, proceed, globalActivateLayer, globalDeactivateLayer, activelayers, inactivelayer, inactivelayers) from datetime import datetime time_layer = layer("Time") location_layer = layer("Location") class Appointment: def __init__(self, datetime=datetime.now(), location="Anywhere"): self.datetime = datetime self.location = location @base def __str__(self): return self.datetime.strftime(format="%Y-%m-%d") @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()
def test_layerstack_unique(): l1 = contextpy.layer("l1") l2 = contextpy.layer("l2") s1 = contextpy.layerstack([l1, l2]) s2 = contextpy.layerstack([l1, l2]) assert s1 is s2
#!/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)
from contextpy import layer import simplejson as json, re from bbcode import Parser v1 = layer("Version 1") v2 = layer("Version 2") v3 = layer("Version 3") is_anonymous = layer("Anonymous") class Message(object): login = layer("Login") chat = layer("Chat") layer = None __method_to_layer = { "login": login, "chat": chat, } __version_to_layer = { "v1": v1, "v2": v2, "v3": v3, } def __init__(self, payload = None, data = None, method = None, version = "v2"): super(Message, self).__init__()