Zurück tutorials.de > Programming > CGI, Perl, Python, Ruby

 
 
Hallo und herzlich willkommen! Tutorials.de ist eine Hilfe-Community mit dem Motto User helfen Usern. Als Gast verfügst Du über Schreibrechte in unseren Foren und Blogs. Du kannst dich aber gerne auch kostenlos registrieren und Teil unserer Gemeinschaft werden! Viel Spaß & Erfolg bei der Vermehrung deines Wissens :-)

Themen: 242.975 | Beiträge: 1.352.293 | Mitglieder: 169.418 (Stand 28.01.10) | Fragen zur Nutzung von Tutorials.de? Nutzungsregeln | Kontaktformular | Impressum

Jubiläums-Countdown 23.02 23.03 23.04 23.05 23.06 23.07 23.08 23.09


Einladung zum C++ für Einsteiger-Workshop
  AntwortAntworten (über Gastzugang)    
  AntwortAntworten (über Gastzugang)    
 
Themen-Optionen Ansicht
Alt 14.05.07, 13:16   #1 (permalink)
 
Benutzerbild von Thomas Darimont tutorials.de Administrator 
 
Registriert seit: Jun 2002
Ort: Saarbrücken (Saarland)
Beiträge: 9.149
Renommee-Modifikator: 61
Thomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende ZukunftThomas Darimont hat eine strahlende Zukunft

Dynamic Proxy in Python

Hallo,

hier mal ein kleines Beispiel wie man sich ganz einfach mit Python dynamic Proxies bauen kann.

Schaut mal hier:
python Code:
  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
#@author: Thomas.Darimont
import inspect
 
class Interface(object):
    pass
 
class IService(Interface):
    def businessOperation(self,arguments):
        """some business operation"""
        pass
 
class IBubu(Interface):
    def someAction(self,arguments):
        """xxxx"""
        pass
 
class ServiceImpl(IService,IBubu):
    def businessOperation(self,arguments):
        print str(arguments)
        return str(arguments).upper()
   
    def someAction(self,arguments):
        print "xxx"+str(arguments)
        return str(arguments)[0]
 
class IInvocationHandler(Interface):
    def invoke(self,proxy,target,methodName,args):
        """invoke..."""
        pass
   
class LoggingInvocationHandler(IInvocationHandler):
    def invoke(self,proxy,target,methodName,args):
        print "before %s" % methodName
        result = getattr(target,methodName)(args)
        print "after %s" % methodName
        return result
   
class InvokableMethod:
    def __init__(self,proxy,invocationTarget,methodName):
        self.methodName = methodName
        self.proxy = proxy
        self.invocationTarget = invocationTarget
   
    def __call__(self,args):
        #print "self=%s, args=%s" % (self,args)
        #return getattr(self.invocationTarget,self.methodName)(args)
        return self.proxy.invocationHandler.invoke(self.proxy,self.invocationTarget,self.methodName,args)
       
 
class Proxy(object):
   
    def __init__(self):
        pass
   
    @staticmethod
    def createProxy(invocationTarget,interfaces,invocationHandler):
        #print "interfaces: %s" % interfaces
        proxy = Proxy()
        proxy.invocationTarget = invocationTarget
        proxy.invocationHandler = invocationHandler
        proxy.interfaces = interfaces
        proxy.interfaceMethodMap = {}
        for interface in interfaces:
           #print "interfaces to implement %s->%s" % (interface,dir(interface))
           for key in dir(interface):
               #print "checking: %s" % key
               if inspect.ismethod(getattr(interface,key)):
                   #print "adding: %s" % key
                   if not key in proxy.interfaceMethodMap:
                       proxy.interfaceMethodMap[interface] = {key:getattr(interface,key)}
                   else:
                       proxy.interfaceMethodMap[interface][key]=getattr(interface,key)
        return proxy
       
    def __getattr__(self, name):
        for interface in self.interfaces:
            if name in self.interfaceMethodMap[interface]:
                #print "candidate: %s " % candidate.__name__
                return InvokableMethod(self,self.invocationTarget,self.interfaceMethodMap[interface][name].__name__)
           
        return None
   
       
 
print ServiceImpl().businessOperation("bubu")
 
serviceProxy = Proxy.createProxy(ServiceImpl(), [IService(),IBubu()],LoggingInvocationHandler())
 
print serviceProxy.interfaces
 
print serviceProxy.businessOperation("test")
print "xxxxxxxxxxxxxxxxxxxxx"
print serviceProxy.someAction("test")

Ausgabe:
Code:
bubu
BUBU
[<__main__.IService object at 0x00A87E90>, <__main__.IBubu object at 0x00A87F90>]
before businessOperation
test
after businessOperation
TEST
xxxxxxxxxxxxxxxxxxxxx
before someAction
xxxtest
after someAction
t
Gruß Tom
__________________
Java rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
  Thomas Darimont ist offline  
 
 
 
Lesezeichen:


Themen-Optionen
Ansicht
Ähnliche Themen
 
Thema Autor Forum Antworten Letzter Beitrag
Dynamic Proxy unter .Net Thomas Darimont .NET Application und Service Design 3 10.07.08 17:10
[Python] Video tutorial zu dem Python Webframework TurboGears (20 Min ) Thomas Darimont CGI, Perl, Python, Ruby 0 07.12.06 13:28
Dynamic DNS kAmBeR Hosting & Webserver 3 25.03.04 14:36
dynamic login/logout quidnovi PHP 8 13.02.04 23:05
NewUntil - Dynamic News gbrems Javascript & Ajax 1 12.02.04 14:02
» Tools
 
tutorials.de-Tools tutorial.de-Suchfeld tutorial.de-Widget tutorial.de-RSS-Feed tutorial.de-Banner
» Neue Links
 
Hits: 126
»
JHT's Planetary...
(Cinema 4D-Objekte)
Hits: 257
»
Tageslicht ohne GI
(Cinema 4D-Tutorials)
Hits: 143
»
Puzzle
(Cinema 4D-Tutorials)
Hits: 96
»
Lacreme
(Cinema 4D-Tutorials)
Hits: 186
»
Liquid Light
(Cinema 4D-Tutorials)
» Aktuelle Umfrage
 
Bist du mit der Geschwindigkeit der Tutorials.de-Website zufrieden?
Ja, es putzt mir glatt den Staub vom Bildschirm! - 78,74%
137 Stimmen
Nein, ich denke da muss noch nachgebessert werden... - 21,26%
37 Stimmen
Stimmen gesamt: 174
Du darfst bei dieser Umfrage nicht abstimmen.

 

Alle Zeitangaben in WEZ +1. Es ist jetzt 05:28 Uhr.


Powered by vBulletin® Version 3.8.4 (Deutsch) & vBadvanced CMPS v.3.2.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.5.0 RC2 ©2010, Crawlability, Inc.
Alle Rechte vorbehalten ©2000 - 2010 tutorials.de
Design by Mark, CSS by Maik & Sven Mintel
Seite generiert in 0,26446 Sekunden mit 26 queries