Changeset 191

Show
Ignore:
Timestamp:
06/24/02 10:09:50 (6 years ago)
Author:
sholloway
Message:

*** empty log message ***

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/RBFoundation/RBFoundation/Acquisition.py

    r186 r191  
    4040 
    4141class AcquisitionMixin(object): 
     42    """Allows for dynamic pseudo inheritence from a list of 'acquirable' objects. 
     43    Use sparingly, as the acquisition (both spanish and otherwise ;) is an expesive 
     44    propsition.  Works great for a subject/observer proxy, where you want notifications 
     45    when values are changed.""" 
     46    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     47    #~ Constants / Variables / Etc.  
     48    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     49 
    4250    __AcquirableObjects = [] 
    4351 
     52    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     53    #~ Public Methods  
     54    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     55 
    4456    def AddAcquirable(self, obj): 
     57        """Adds obj to the acquisition list, allowing for pseudo inheritance.""" 
    4558        if not self.__AcquirableObjects: 
    46             self.__AcquirableObjects= [] 
     59            self.__AcquirableObjects = [] 
    4760        self.__AcquirableObjects.append(obj) 
     61 
     62    def RemoveAcquirable(self, obj): 
     63        """Removes obj from the acquisition list.""" 
     64        self.__AcquirableObjects.remove(obj) 
    4865         
     66    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     67    #~ Special  
     68    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     69 
    4970    def __getattribute__(self, name): 
     71        """Returns attribute value from self if exists, or from an aquirable object.""" 
    5072        try: 
    51             return object.__getattribute__(self,name) 
     73            return super(AcquisitionMixin, self).__getattribute__(name) 
    5274        except AttributeError: 
    53             lst = [x for x in self.__AcquirableObjects if hasattr(x, name)] 
    54             assert len(lst) in [0,1] 
    55             if lst: 
    56                 result = getattr(lst[0], name, id(None)) 
     75            for x in self.__AcquirableObjects: 
     76                result = getattr(x, name, id(None)) 
    5777                if result != id(None):  
    5878                    return result 
     
    6080 
    6181    def __setattr__(self, name, value): 
     82        """Sets attribute to value on self if exists, or on an aquirable object.""" 
    6283        try: 
    63             object.__getattribute__(self,name) 
     84            super(AcquisitionMixin, self).__getattribute__(name) 
    6485        except AttributeError: 
    65             lst = [x for x in self.__AcquirableObjects if hasattr(x, name)] 
    66             assert len(lst) in [0,1] 
    67             if lst: return setattr(lst[0], name, value) 
    68         return object.__setattr__(self,name,value) 
     86            for x in self.__AcquirableObjects: 
     87                if hasattr(x, name): 
     88                    return setattr(x, name, value) 
     89        return super(AcquisitionMixin, self).__setattr__(name,value) 
    6990 
    7091    def __delattr__(self, name): 
     92        """Removes attribute from self if exists, or from an aquirable object.""" 
    7193        try: 
    72             object.__getattribute__(self,name) 
     94            super(AcquisitionMixin, self).__getattribute__(name) 
    7395        except AttributeError: 
    74             lst = [x for x in self.__AcquirableObjects if hasattr(x, name)] 
    75             assert len(lst) in [0,1] 
    76             if lst: return delattr(lst[0], name) 
    77         return object.__delattr__(self,name) 
     96            for x in self.__AcquirableObjects: 
     97                if hasattr(x, name): 
     98                    return delattr(x, name) 
     99        return super(AcquisitionMixin, self).__delattr__(name) 
    78100 
    79101#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     
    89111    a.AddAcquirable(temp()) 
    90112 
    91     assert a.v == 1 
    92     assert a.u == 2 
    93     assert a.u != a.v 
    94     a.u = 'new value' 
    95     assert a.u == 'new value' 
    96     a.new_val = 'a new value!' 
    97     assert a.new_val == 'a new value!' 
     113    if 1: 
     114        assert a.v == 1 
     115        assert a.u == 2 
     116        assert a.u != a.v 
     117        a.u = 'new value' 
     118        assert a.u == 'new value' 
     119        a.new_val = 'a new value!' 
     120        assert a.new_val == 'a new value!' 
     121 
     122        import time 
     123        count = 1e4 
     124        start = time.clock() 
     125        for i in xrange(count): me = i + temp.v 
     126        pereach = (time.clock() - start) / count 
     127        print "Normal:", pereach, "per", count 
     128 
     129        start = time.clock() 
     130        for i in xrange(count): me = i + a.v 
     131        pereach2 = (time.clock() - start) / count 
     132        print "Acquisition:", pereach2, "per", count 
     133 
     134        print "Acquisition is", pereach2/pereach, "times slower"  
    98135    print "Test complete." 
    99136 
  • trunk/RBFoundation/RBFoundation/AttributedDict.py

    r164 r191  
    102102    except AttributeError: 
    103103        pass 
     104 
     105    import time 
     106    class Normal(object): 
     107        answer = 42**2 
     108    normal = Normal() 
     109    count = 1e4 
     110    start = time.clock() 
     111    for i in xrange(count): me = i + normal.answer 
     112    pereach = (time.clock() - start) / count 
     113    print "Normal:", pereach, "per", count 
     114 
     115    start = time.clock() 
     116    for i in xrange(count): me = i + d.answer 
     117    pereach2 = (time.clock() - start) / count 
     118    print "Attributed Dict:", pereach2, "per", count 
     119 
     120    print "Attributed Dict is", pereach2/pereach, "times slower"  
  • trunk/RBJabber/RBJabber/SubjectObserver/ProxySubject.py

    r190 r191  
    4343#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    4444 
    45 class ProxySubjectTriggerMixin(object, Acquisition.AcquisitionMixin): 
    46     __subject = None 
    47     def __init__(self, subject): 
    48         self.__subject = subject 
    49         self.AddAcquirable(self.__subject) 
     45class ProxySubjectTriggerMixin(object): 
     46    _subject = None 
     47 
     48    def __init__(self, subject=None): 
     49        self.SetSubject(subject) 
     50 
     51    def SetSubject(self, subject): 
     52        self._subject = subject 
    5053 
    5154    def __setattr__(self, name, value): 
    5255        result = self.__super.__setattr__(name, value) 
    53         if name != '_ProxySubjectTriggerMixin__subject'
    54             self.__subject.UpdateObserversEx({name: value}) 
     56        if name != '_subject' and self._subject
     57            self._subject.UpdateObserversEx({name: value}) 
    5558        return result 
    5659 
    5760    def __delattr__(self, name): 
    5861        result = self.__super.__delattr__(name) 
    59         if name != '_ProxySubjectTriggerMixin__subject'
    60             self.__subject.UpdateObserversEx({name: None}) 
     62        if name != '_subject' and self._subject
     63            self._subject.UpdateObserversEx({name: None}) 
    6164        return result 
    6265 
     
    6568#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    6669 
    67 class ProxySubject(ProxySubjectTriggerMixin): 
    68     def __init__(self): 
    69         ProxySubjectTriggerMixin.__init__(self, Subject.Subject()
     70class ProxySubjectAcquisitionMixin(ProxySubjectTriggerMixin, Acquisition.AcquisitionMixin): 
     71    def __init__(self, subject=None): 
     72        ProxySubjectTriggerMixin.__init__(self, subject
    7073        Acquisition.AcquisitionMixin.__init__(self) 
    7174 
    72 class ProxyCategorySubject(ProxySubjectTriggerMixin): 
    73     def __init__(self): 
    74         ProxySubjectTriggerMixin.__init__(self, CategorySubject.CategorySubject()) 
    75         Acquisition.AcquisitionMixin.__init__(self) 
     75    def SetSubject(self, subject): 
     76        if self._subject: self.RemoveAcquirable(self._subject) 
     77        super(ProxySubjectAcquisitionMixin, self).SetSubject(subject) 
     78        if self._subject: self.AddAcquirable(self._subject) 
     79 
     80#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     81 
     82class ProxySubject(ProxySubjectAcquisitionMixin): 
     83    def __init__(self, subject=None): 
     84        ProxySubjectAcquisitionMixin.__init__(self, subject or Subject.Subject()) 
     85 
     86#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     87 
     88class ProxyCategorySubject(ProxySubjectAcquisitionMixin): 
     89    def __init__(self, subject=None): 
     90        ProxySubjectAcquisitionMixin.__init__(self, subject or CategorySubject.CategorySubject()) 
    7691 
    7792#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~