Changeset 181

Show
Ignore:
Timestamp:
Wed Jan 18 11:36:36 2006
Author:
drew
Message:

-added xxml (alpha) and binder mods

Files:

Legend:

Unmodified
Added
Removed
Modified
  • branches/utils/utiltests.py

    r177 r181  
    5 5 import xix.utils.timetool  
    6 6 import xix.utils.diff  
      7 import xix.utils.binder  
    7 8 from tests import doctests  
    8 9  
     
    14 15     xix.utils.timetool,  
    15 16     xix.utils.diff,  
      17     xix.utils.binder,  
    16 18     doctests.utils.config,  
    17 19     doctests.utils.python  
  • branches/utils/xix/utils/timetool.py

    r177 r181  
    3 3 $Id$  
    4 4 """  
    5   import time, os  
    6 5 from datetime import datetime  
    7 6 from time import mktime, strptime  
  • branches/utils/xix/utils/diff.py

    r177 r181  
    8 8 __version__ = '$Revision$'[11:-2]  
    9 9  
    10   from xix.utils.interfaces import IDiff  
      10 from xix.utils.interfaces import IDiff, IDiffFactory  
    10 10 from xix.utils.comp.interface import implements  
    11 11  
     
    30 30  
    31 31  
      32 class SequentialDiffFactory:  
      33     """Generates Diffs between source and target objects, considering sequence  
      34     of appearance.  
      35     """  
      36      
      37     implements(IDiffFactory)  
      38  
      39     def diff(self, source, target):  
      40         """Example:  
      41  
      42         # >>> l1 = [1,2,3,4,5]  
      43         # >>> l2 = [3,4,5,1,2]  
      44         # >>> diffFactory = SequentialDiffFactory()  
      45         # >>> d = diffFactory.diff(source, target)  
      46         # >>> d.same  
      47         # [3, 4, 5]  
      48         # >>> d.missing  
      49         # [1, 2]  
      50         # >>> d.added  
      51         # [1, 2]  
      52         """  
      53         raise NotImplementedError, 'diff has not been implemented yet'  
      54          
      55          
      56  
      57 class NonSequentialDiffFactory:  
      58     """Generates Diffs between source and target objects, without consideration  
      59     to sequence of appearces of objects.  
      60     """  
      61  
      62     implements(IDiffFactory)  
      63  
      64     def diff(self, source, target):  
      65         """Example usage:  
      66  
      67         >>> l1 = [1, 2, 3, 4, 5]  
      68         >>> l2 = [3, 4, 7, 1, 8, 9]  
      69         >>> diffFactory = NonSequentialDiffFactory()  
      70         >>> d = diffFactory.diff(l1, l2)  
      71         >>> d.same  
      72         (1, 3, 4)  
      73         >>> d.missing  
      74         (2, 5)  
      75         >>> d.added  
      76         (7, 8, 9)  
      77         """  
      78         added = tuple([elm for elm in target if elm not in source])  
      79         missing = tuple([elm for elm in source if elm not in target])  
      80         same = tuple([elm for elm in source if elm in target])  
      81         return AbstractDiff(same, missing, added)  
      82  
      83 class DictionaryDiffFactory:  
      84     """Return diff of two dictionaries.  Entries in same, added and missing  
      85     lists are key, value pairs. (k,v)  
      86     """  
      87  
      88     implements(IDiffFactory)  
      89  
      90     keyValueDiffFactory = NonSequentialDiffFactory()  
      91  
      92     def diff(self, source, target):  
      93         """Example Usage:  
      94  
      95         >>> d1 = {'a':1, 'b':2, 'c':3}  
      96         >>> d2 = {'b':4, 'c':3, 'a':2}  
      97         >>> diffFactory = DictionaryDiffFactory()  
      98         >>> d = diffFactory.diff(d1, d2)  
      99         >>> len(d.same)  
      100         1  
      101         >>> len(d.missing)  
      102         2  
      103         >>> len(d.added)  
      104         2  
      105         >>> d.same  
      106         (('c', 3),)  
      107         >>> [int(elm in d.missing) for elm in (('a',1), ('b',2))]  
      108         [1, 1]  
      109         >>> [int(elm in d.added) for elm in (('a',2), ('b',4))]  
      110         [1, 1]  
      111         """  
      112         return self.keyValueDiffFactory.diff(source.items(), target.items())  
      113  
      114  
    32 115 class PrettyDiff:  
    33 116     """A pair of sequences alligned representing a Diff.  The first sequence  
  • branches/utils/xix/utils/interfaces.py

    r177 r181  
    193 193             sequences""")  
    194 194  
      195 class IDiffFactory(Interface):  
      196     """DiffFactory instances generate IDiff providers given source and target  
      197     objects to compare.  
      198     """  
    195 199  
      200     def diff(source, target, **kwargs):  
      201         """Generate IDiff provider given source and target objects and optional  
      202         keyword arguments.  
      203         """  
      204  
      205 ###############################################################################  
      206 # Binding interfaces  
      207 ###############################################################################  
      208  
      209 class IBindingRequest(Interface):  
      210         """IBindingRequest providers represent request for binding that  
      211         has a name to bind and creator to bind and initialize component when needed.  
      212         """  
      213  
      214         name = Attribute("""name to bind instances to.""")  
      215          
      216         creator = Attribute("""creator callable to return object bindings""")  
      217          
      218         shared = Attribute("""shared flag - True => share single or """ \  
      219                 """bundled instances.""")  
      220          
      221         bundlect = Attribute(  
      222                 """Bundle count if shared - default is 1 for """ \  
      223                 """singleton sharing""")  
      224          
      225         maxcount = Attribute("""Max instantiation count if not shared""")  
      226          
      227         initargs = Attribute(  
      228                 """Initialization positional args to creator""")  
      229          
      230         initkwargs = Attribute(  
      231                 """Initialization key-word args to creator""")  
      232          
      233         stack = Attribute(  
      234                 """Rotating bindings stack""")  
      235          
      236         createdct = Attribute(  
      237                 """Number of bindings created""")  
      238  
      239    
      240 class IBindingFactory(Interface):  
      241     """IBindingFactory providers provide method 'bind` which can be  
      242     called with an IBindingRequest to get an object instance.  
      243     """  
      244  
      245     def bind(bindingRequest):  
      246         """Given a bindingRequest, return an object instance or  
      247         raise an Exception if binding cannot be created.  
      248  
      249         @param bindingRequest: IBindingRequest provider  
      250         @raises Exception: if binding cannot be created.  
      251         """