home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / util / primitives / functional.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  6.8 KB  |  198 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from cStringIO import StringIO
  5.  
  6. class chained_files(object):
  7.     
  8.     def __init__(self, fileobjs):
  9.         self._bytecount = 0
  10.         self.fileobjs = []
  11.         for obj in fileobjs:
  12.             if not hasattr(obj, 'read') and isinstance(obj, str):
  13.                 self.fileobjs.append(StringIO(obj))
  14.             else:
  15.                 self.fileobjs.append(obj)
  16.             if not hasattr(self.fileobjs[-1], 'read'):
  17.                 raise TypeError('chained_files makes a .read object out of .read objects only. got a %r', obj)
  18.             hasattr(self.fileobjs[-1], 'read')
  19.             if hasattr(obj, 'tell'):
  20.                 self._bytecount += obj.tell()
  21.                 continue
  22.             self
  23.             print '%s object %r has no attribute tell' % (type(obj), obj)
  24.         
  25.         self.obj = self.fileobjs.pop(0)
  26.  
  27.     
  28.     def read(self, blocksize = -1):
  29.         if blocksize < 0:
  30.             val = ''.join((lambda .0: for obj in .0:
  31. obj.read())([
  32.                 self.obj] + self.fileobjs))
  33.             self._bytecount += len(val)
  34.             return val
  35.         chunk = StringIO()
  36.         chunkstr = blocksize < 0 if self.obj is not None else ''
  37.         chunksize = len(chunkstr)
  38.         chunk.write(chunkstr)
  39.         diff = blocksize - chunksize
  40.         while diff and self.obj is not None:
  41.             subchunk = self.obj.read(diff)
  42.             if not subchunk:
  43.                 if self.fileobjs:
  44.                     self.obj = self.fileobjs.pop(0)
  45.                 else:
  46.                     
  47.                     self.read = lambda self: ''
  48.                     self.obj = None
  49.             
  50.             chunk.write(subchunk)
  51.             chunksize += len(subchunk)
  52.             diff = blocksize - chunksize
  53.         val = chunk.getvalue()
  54.         self._bytecount += len(val)
  55.         return val
  56.  
  57.     
  58.     def tell(self):
  59.         return self._bytecount
  60.  
  61.  
  62.  
  63. class AttrChain(object):
  64.     __name = None
  65.     __target = None
  66.     
  67.     def __init__(self, name = None, target = None):
  68.         self._AttrChain__name = name
  69.         self._AttrChain__target = target
  70.  
  71.     
  72.     def __getattr__(self, attr):
  73.         if attr == '_getAttributeNames':
  74.             return False
  75.         return attr == '_getAttributeNames'(AttrChain if self._AttrChain__name is not None else '' + attr, self._AttrChain__get_target())
  76.  
  77.     
  78.     def __call__(self, *a, **k):
  79.         return self._AttrChain__get_target()(self._AttrChain__name, *a, **k)
  80.  
  81.     
  82.     def __get_target(self):
  83.         if self._AttrChain__target is not None:
  84.             return self._AttrChain__target
  85.         
  86.         try:
  87.             return super(AttrChain, self).__call__
  88.         except AttributeError:
  89.             self._AttrChain__target is not None
  90.             self._AttrChain__target is not None
  91.             if getattr(type(self), '__call__') != AttrChain.__call__:
  92.                 return self.__call__
  93.         except:
  94.             getattr(type(self), '__call__') != AttrChain.__call__
  95.  
  96.         raise AttributeError('%s object has no target, no __call__ and no super class with __call__' % type(self).__name__)
  97.  
  98.     
  99.     def __repr__(self):
  100.         return '<AttrChain ' + str(self._AttrChain__name) + '>'
  101.  
  102.  
  103.  
  104. class ObjectList(list):
  105.     
  106.     def __init__(self, *a, **k):
  107.         self.__dict__['strict'] = k.pop('strict', True)
  108.         list.__init__(self, *a, **k)
  109.  
  110.     
  111.     def __setattr__(self, attr, val):
  112.         for o in self:
  113.             setattr(o, attr, val)
  114.         
  115.  
  116.     
  117.     def __getattr__(self, attr):
  118.         
  119.         try:
  120.             return list.__getattr__(self, attr)
  121.         except AttributeError:
  122.             
  123.             try:
  124.                 return self.__dict__[attr]
  125.             except KeyError:
  126.                 if self.__dict__.get('strict', True):
  127.                     default = sentinel
  128.                 else:
  129.                     
  130.                     default = lambda *a, **k: pass
  131.                 res = (ObjectList,)((lambda .0: for x in .0:
  132. getattr(x, attr, sentinel))(self))
  133.                 if self.__dict__.get('strict', True) and sentinel in res:
  134.                     raise AttributeError('Not all objects in %r have attribute %r' % (self, attr))
  135.                 sentinel in res
  136.                 
  137.                 try:
  138.                     res = FunctionList(res)
  139.                 except AssertionError:
  140.                     pass
  141.  
  142.                 return res
  143.             
  144.  
  145.             None<EXCEPTION MATCH>KeyError
  146.  
  147.  
  148.     
  149.     def __repr__(self):
  150.         return '<%s: %r>' % (type(self).__name__, list.__repr__(self))
  151.  
  152.  
  153.  
  154. class FunctionList(ObjectList):
  155.     
  156.     def __init__(self, *a, **k):
  157.         ObjectList.__init__(self, *a, **k)
  158.         if not all((lambda .0: for x in .0:
  159. callable(x))(self)):
  160.             raise AssertionError
  161.         all((lambda .0: for x in .0:
  162. callable(x))(self))
  163.  
  164.     
  165.     def __call__(self, *a, **k):
  166.         return [ f(*a, **k) for f in self ]
  167.  
  168.  
  169.  
  170. def compose(funcs):
  171.     
  172.     def composed(res):
  173.         for f in funcs:
  174.             res = f(res)
  175.         
  176.         return res
  177.  
  178.     return composed
  179.  
  180.  
  181. def chain(*iterables):
  182.     for it in iterables:
  183.         for element in it:
  184.             yield element
  185.         
  186.     
  187.  
  188.  
  189. def main():
  190.     chained = chained_files((lambda .0: for s in .0:
  191. StringIO(s))('one two three'.split()))
  192.     print chained.read(8)
  193.     print chained.read(8)
  194.  
  195. if __name__ == '__main__':
  196.     main()
  197.  
  198.