Permalink
Browse files

Update bundled simplegeneric to version 0.7.

  • Loading branch information...
1 parent c14b594 commit bcd1047799d83d4f29d734a62ddec4c6dc72287b @takluyver committed Aug 6, 2011
Showing with 25 additions and 65 deletions.
  1. +25 −65 IPython/external/simplegeneric/_simplegeneric.py
@@ -1,18 +1,17 @@
+"""This is version 0.7 of Philip J. Eby's simplegeneric module
+(http://pypi.python.org/pypi/simplegeneric)
+"""
+
#Name: simplegeneric
-#Version: 0.6
+#Version: 0.7
#Summary: Simple generic functions (similar to Python's own len(), pickle.dump(), etc.)
-#Home-page: http://cheeseshop.python.org/pypi/simplegeneric
+#Home-page: http://pypi.python.org/pypi/simplegeneric
#Author: Phillip J. Eby
#Author-email: peak@eby-sarna.com
#License: PSF or ZPL
-# This is version 0.6 of Philip J. Eby's simplegeneric module
-# (http://cheeseshop.python.org/pypi/simplegeneric) patched to work
-# with Python 2.3 (which doesn't support assigning to __name__)
-
__all__ = ["generic"]
-
from types import ClassType, InstanceType
classtypes = type, ClassType
@@ -33,35 +32,36 @@ def _by_class(*args, **kw):
_by_type = {object: func, InstanceType: _by_class}
_gbt = _by_type.get
- def when_type(t):
- """Decorator to add a method that will be called for type `t`"""
- if not isinstance(t, classtypes):
- raise TypeError(
- "%r is not a type or class" % (t,)
- )
- def decorate(f):
- if _by_type.setdefault(t,f) is not f:
+ def when_type(*types):
+ """Decorator to add a method that will be called for the given types"""
+ for t in types:
+ if not isinstance(t, classtypes):
raise TypeError(
- "%r already has method for type %r" % (func, t)
+ "%r is not a type or class" % (t,)
)
+ def decorate(f):
+ for t in types:
+ if _by_type.setdefault(t,f) is not f:
+ raise TypeError(
+ "%r already has method for type %r" % (func, t)
+ )
return f
return decorate
-
-
_by_object = {}
_gbo = _by_object.get
- def when_object(o):
- """Decorator to add a method that will be called for object `o`"""
+ def when_object(*obs):
+ """Decorator to add a method to be called for the given object(s)"""
def decorate(f):
- if _by_object.setdefault(id(o), (o,f))[1] is not f:
- raise TypeError(
- "%r already has method for object %r" % (func, o)
- )
+ for o in obs:
+ if _by_object.setdefault(id(o), (o,f))[1] is not f:
+ raise TypeError(
+ "%r already has method for object %r" % (func, o)
+ )
return f
return decorate
@@ -78,10 +78,7 @@ def dispatch(*args, **kw):
else:
return f[1](*args, **kw)
- try:
- dispatch.__name__ = func.__name__
- except TypeError:
- pass
+ dispatch.__name__ = func.__name__
dispatch.__dict__ = func.__dict__.copy()
dispatch.__doc__ = func.__doc__
dispatch.__module__ = func.__module__
@@ -94,46 +91,9 @@ def dispatch(*args, **kw):
return dispatch
-
-
def test_suite():
import doctest
return doctest.DocFileSuite(
'README.txt',
optionflags=doctest.ELLIPSIS|doctest.REPORT_ONLY_FIRST_FAILURE,
)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-

0 comments on commit bcd1047

Please sign in to comment.