home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1787 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.2 KB  |  92 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'generic']
  6. from types import ClassType, InstanceType
  7. classtypes = (type, ClassType)
  8.  
  9. def generic(func):
  10.     _sentinel = object()
  11.     
  12.     def _by_class(*args, **kw):
  13.         cls = args[0].__class__
  14.         for t in type(cls.__name__, (cls, object), { }).__mro__:
  15.             f = _gbt(t, _sentinel)
  16.             if f is not _sentinel:
  17.                 return f(*args, **kw)
  18.         else:
  19.             return func(*args, **kw)
  20.         return f is not _sentinel
  21.  
  22.     _by_type = {
  23.         object: func,
  24.         InstanceType: _by_class }
  25.     _gbt = _by_type.get
  26.     
  27.     def when_type(t):
  28.         if not isinstance(t, classtypes):
  29.             raise TypeError('%r is not a type or class' % (t,))
  30.         isinstance(t, classtypes)
  31.         
  32.         def decorate(f):
  33.             if _by_type.setdefault(t, f) is not f:
  34.                 raise TypeError('%r already has method for type %r' % (func, t))
  35.             _by_type.setdefault(t, f) is not f
  36.             return f
  37.  
  38.         return decorate
  39.  
  40.     _by_object = { }
  41.     _gbo = _by_object.get
  42.     
  43.     def when_object(o):
  44.         
  45.         def decorate(f):
  46.             if _by_object.setdefault(id(o), (o, f))[1] is not f:
  47.                 raise TypeError('%r already has method for object %r' % (func, o))
  48.             _by_object.setdefault(id(o), (o, f))[1] is not f
  49.             return f
  50.  
  51.         return decorate
  52.  
  53.     
  54.     def dispatch(*args, **kw):
  55.         f = _gbo(id(args[0]), _sentinel)
  56.         if f is _sentinel:
  57.             for t in type(args[0]).__mro__:
  58.                 f = _gbt(t, _sentinel)
  59.                 if f is not _sentinel:
  60.                     return f(*args, **kw)
  61.             else:
  62.                 return func(*args, **kw)
  63.         f is not _sentinel
  64.         return f[1](*args, **kw)
  65.  
  66.     
  67.     try:
  68.         dispatch.__name__ = func.__name__
  69.     except TypeError:
  70.         (None, None, None, (None, (None, (None, None, None))))
  71.         (None, None, None, (None, (None, (None, None, None))))
  72.     except:
  73.         (None, None, None, (None, (None, (None, None, None))))
  74.  
  75.     dispatch.__dict__ = func.__dict__.copy()
  76.     dispatch.__doc__ = func.__doc__
  77.     dispatch.__module__ = func.__module__
  78.     dispatch.when_type = when_type
  79.     dispatch.when_object = when_object
  80.     dispatch.default = func
  81.     
  82.     dispatch.has_object = lambda o: id(o) in _by_object
  83.     
  84.     dispatch.has_type = lambda t: t in _by_type
  85.     return dispatch
  86.  
  87.  
  88. def test_suite():
  89.     import doctest
  90.     return doctest.DocFileSuite('README.txt', optionflags = doctest.ELLIPSIS | doctest.REPORT_ONLY_FIRST_FAILURE)
  91.  
  92.