def __init__(self): self._db = None for member in dir(self): #these checks help make sure we are wrapping normal exported methods #we dont want to wrap stuf like __init__ or already instanciated methods if str(member)[0].isalpha(): real = getattr(self, member) if callable(real): aspects.wrap_around(real, dbConnectionWrapper) return
def __init__(self, *args, **keyw): generic.update.__init__(self, *args, **keyw) #Choose which update class to run based on ProductID d = db.db() d.Connect() ret = d.Command('''SELECT * FROM BuildCfg.products WHERE id=%d''' % (int(self.ProductID))) d.Close() p = ret[0] if p['version'] >= "5.0": self._run_class = apt.update(self.ProductID) else: self._run_class = mvl.update(self.ProductID) #wrap only the export methods by generic for member in dir(self): if str(member)[0].isalpha(): real = getattr(self, member) if callable(real): aspects.wrap_around(real, dispatchWrapper) return
import urllib import aspects import timeout_advice import sys import time if len(sys.argv) < 2: print "Usage: httpget URL [URL ...]" aspects.wrap_around(urllib.URLopener.open, timeout_advice.create_timeout_advice(1, 1)) for url in sys.argv[1:]: start_time = time.time() ufile = urllib.urlopen(url) elapsed_time = time.time() - start_time if ufile: print ufile.read() ufile.close sys.stderr.write(url + " arrived in " + str(elapsed_time) + " seconds\n") else: sys.stderr.write(url + " could not be retrieved, tried " + str(elapsed_time) + " seconds\n")
def foo(self): print "B2.foo" def bar(self): print "B2.bar" class B3: def bar(self): print "B3.bar" class D1(B1, B2): pass class DD1(D1, B3): pass wrap_around(B1.foo, tracer_advice) wrap_around(B2.foo, tracer_advice) wrap_around(B2.bar, tracer_advice) wrap_around(B3.bar, tracer_advice) wrap_around(DD1.bar, tracer_advice) o = DD1() o.foo() o.bar()
class B1: def foo(self): print "B1.foo" class B2: def foo(self): print "B2.foo" def bar(self): print "B2.bar" class B3: def bar(self): print "B3.bar" class D1(B1,B2): pass class DD1(D1,B3): pass wrap_around( B1.foo, tracer_advice ) wrap_around( B2.foo, tracer_advice ) wrap_around( B2.bar, tracer_advice ) wrap_around( B3.bar, tracer_advice ) wrap_around( DD1.bar, tracer_advice ) o = DD1() o.foo() o.bar()
class b: pass class d(b): pass class c: def m(self,integer,string): print "c.m called with",integer,string def n(self,object): print "c.n called with object",object def x(self, arg1, opt1=None, opt2=None): print "c.x called with ", arg1, opt1, opt2 c.__argtypes={ 'm':[[int,long],[str]], 'n':[[b]], 'x':[[int], [int, type(None)], [int, type(None)]] } wrap_around(c.m,argtypecheck) wrap_around(c.n,argtypecheck) wrap_around(c.x,argtypecheck) o=c() o.m(2L,"a") o.n( d() ) o.x(1) o.x(1, 2) o.x(1, 2, 3) o.x(1, opt2=3)
import urllib import aspects import timeout_advice import sys import time if len(sys.argv)<2: print "Usage: httpget URL [URL ...]" aspects.wrap_around( urllib.URLopener.open, timeout_advice.create_timeout_advice(1,1) ) for url in sys.argv[1:]: start_time = time.time() ufile = urllib.urlopen(url) elapsed_time = time.time() - start_time if ufile: print ufile.read() ufile.close sys.stderr.write(url + " arrived in " + str(elapsed_time) + " seconds\n") else: sys.stderr.write(url + " could not be retrieved, tried " + str(elapsed_time) + " seconds\n")
return self.__proceed(args[0]+100) def adv2(self,*args,**keyw): return self.__proceed(keyw['num']+400) o=C() test,verdict="using wrap_count when no wraps",FAIL try: if aspects.wrap_count(C.foo1)==0: verdict=PASS finally: print verdict,test test,verdict="wrap_around a public method, keyword argument",FAIL try: n=aspects.wrap_around(C.foo1,adv2) if o.foo1(num=0)==401 and n==0: verdict=PASS finally: print verdict,test test,verdict="using wrap_count when one wrap",FAIL try: if aspects.wrap_count(C.foo1)==1: verdict=PASS else: print "wrap_count returned",aspects.wrap_count(C.foo1) finally: print verdict,test test,verdict="wrap_around a private method (called by a public method)",FAIL try: aspects.wrap_around(C._C__foo0,adv1) if o.foo1(num=0)==501: verdict=PASS
class Example: def __init__( self ): self.all_waits = 0 def wait( self, time_to_wait, throw_exception = 0 ): myname = "Example.wait("+str(time_to_wait)+","+str(throw_exception)+")" print myname, "falls sleep." time.sleep( time_to_wait ) self.all_waits = self.all_waits + time_to_wait if throw_exception: print "Suddenly", myname, "raises exception" raise myname else: print myname, "wakes up. Total sleep:",self.all_waits return self.all_waits e = Example() adv = timeout_advice.create_timeout_advice( timeout=1.0, catch_exceptions=1 ) aspects.wrap_around( Example.wait, adv ) t = time.time() print e.wait( time_to_wait=1.6, throw_exception=1 ) print e.wait(0.2) print e.wait(0.3) print e.wait(0.4) print time.time()-t
class c: def m(self, integer, string): print "c.m called with", integer, string def n(self, object): print "c.n called with object", object def x(self, arg1, opt1=None, opt2=None): print "c.x called with ", arg1, opt1, opt2 c.__argtypes = { 'm': [[int, long], [str]], 'n': [[b]], 'x': [[int], [int, type(None)], [int, type(None)]] } wrap_around(c.m, argtypecheck) wrap_around(c.n, argtypecheck) wrap_around(c.x, argtypecheck) o = c() o.m(2L, "a") o.n(d()) o.x(1) o.x(1, 2) o.x(1, 2, 3) o.x(1, opt2=3)
def adv2(self, *args, **keyw): return self.__proceed(keyw['num'] + 400) o = C() test, verdict = "using wrap_count when no wraps", FAIL try: if aspects.wrap_count(C.foo1) == 0: verdict = PASS finally: print verdict, test test, verdict = "wrap_around a public method, keyword argument", FAIL try: n = aspects.wrap_around(C.foo1, adv2) if o.foo1(num=0) == 401 and n == 0: verdict = PASS finally: print verdict, test test, verdict = "using wrap_count when one wrap", FAIL try: if aspects.wrap_count(C.foo1) == 1: verdict = PASS else: print "wrap_count returned", aspects.wrap_count(C.foo1) finally: print verdict, test test, verdict = "wrap_around a private method (called by a public method)", FAIL try: aspects.wrap_around(C._C__foo0, adv1) if o.foo1(num=0) == 501: verdict = PASS
class Example: def __init__(self): self.all_waits = 0 def wait(self, time_to_wait, throw_exception=0): myname = "Example.wait(" + str(time_to_wait) + "," + str( throw_exception) + ")" print myname, "falls sleep." time.sleep(time_to_wait) self.all_waits = self.all_waits + time_to_wait if throw_exception: print "Suddenly", myname, "raises exception" raise myname else: print myname, "wakes up. Total sleep:", self.all_waits return self.all_waits e = Example() adv = timeout_advice.create_timeout_advice(timeout=1.0, catch_exceptions=1) aspects.wrap_around(Example.wait, adv) t = time.time() print e.wait(time_to_wait=1.6, throw_exception=1) print e.wait(0.2) print e.wait(0.3) print e.wait(0.4) print time.time() - t