Show More
@@ -1,18 +1,17 | |||||
|
1 | """This is version 0.7 of Philip J. Eby's simplegeneric module | |||
|
2 | (http://pypi.python.org/pypi/simplegeneric) | |||
|
3 | """ | |||
|
4 | ||||
1 | #Name: simplegeneric |
|
5 | #Name: simplegeneric | |
2 |
#Version: 0. |
|
6 | #Version: 0.7 | |
3 | #Summary: Simple generic functions (similar to Python's own len(), pickle.dump(), etc.) |
|
7 | #Summary: Simple generic functions (similar to Python's own len(), pickle.dump(), etc.) | |
4 |
#Home-page: http:// |
|
8 | #Home-page: http://pypi.python.org/pypi/simplegeneric | |
5 | #Author: Phillip J. Eby |
|
9 | #Author: Phillip J. Eby | |
6 | #Author-email: peak@eby-sarna.com |
|
10 | #Author-email: peak@eby-sarna.com | |
7 | #License: PSF or ZPL |
|
11 | #License: PSF or ZPL | |
8 |
|
12 | |||
9 | # This is version 0.6 of Philip J. Eby's simplegeneric module |
|
|||
10 | # (http://cheeseshop.python.org/pypi/simplegeneric) patched to work |
|
|||
11 | # with Python 2.3 (which doesn't support assigning to __name__) |
|
|||
12 |
|
||||
13 | __all__ = ["generic"] |
|
13 | __all__ = ["generic"] | |
14 |
|
14 | |||
15 |
|
||||
16 | from types import ClassType, InstanceType |
|
15 | from types import ClassType, InstanceType | |
17 | classtypes = type, ClassType |
|
16 | classtypes = type, ClassType | |
18 |
|
17 | |||
@@ -33,35 +32,36 def generic(func): | |||||
33 | _by_type = {object: func, InstanceType: _by_class} |
|
32 | _by_type = {object: func, InstanceType: _by_class} | |
34 | _gbt = _by_type.get |
|
33 | _gbt = _by_type.get | |
35 |
|
34 | |||
36 | def when_type(t): |
|
35 | def when_type(*types): | |
37 |
"""Decorator to add a method that will be called for t |
|
36 | """Decorator to add a method that will be called for the given types""" | |
38 |
|
|
37 | for t in types: | |
39 | raise TypeError( |
|
38 | if not isinstance(t, classtypes): | |
40 | "%r is not a type or class" % (t,) |
|
|||
41 | ) |
|
|||
42 | def decorate(f): |
|
|||
43 | if _by_type.setdefault(t,f) is not f: |
|
|||
44 | raise TypeError( |
|
39 | raise TypeError( | |
45 |
"%r |
|
40 | "%r is not a type or class" % (t,) | |
46 | ) |
|
41 | ) | |
|
42 | def decorate(f): | |||
|
43 | for t in types: | |||
|
44 | if _by_type.setdefault(t,f) is not f: | |||
|
45 | raise TypeError( | |||
|
46 | "%r already has method for type %r" % (func, t) | |||
|
47 | ) | |||
47 | return f |
|
48 | return f | |
48 | return decorate |
|
49 | return decorate | |
49 |
|
50 | |||
50 |
|
51 | |||
51 |
|
52 | |||
52 |
|
53 | |||
53 |
|
||||
54 |
|
||||
55 | _by_object = {} |
|
54 | _by_object = {} | |
56 | _gbo = _by_object.get |
|
55 | _gbo = _by_object.get | |
57 |
|
56 | |||
58 | def when_object(o): |
|
57 | def when_object(*obs): | |
59 |
"""Decorator to add a method t |
|
58 | """Decorator to add a method to be called for the given object(s)""" | |
60 | def decorate(f): |
|
59 | def decorate(f): | |
61 | if _by_object.setdefault(id(o), (o,f))[1] is not f: |
|
60 | for o in obs: | |
62 | raise TypeError( |
|
61 | if _by_object.setdefault(id(o), (o,f))[1] is not f: | |
63 | "%r already has method for object %r" % (func, o) |
|
62 | raise TypeError( | |
64 | ) |
|
63 | "%r already has method for object %r" % (func, o) | |
|
64 | ) | |||
65 | return f |
|
65 | return f | |
66 | return decorate |
|
66 | return decorate | |
67 |
|
67 | |||
@@ -78,10 +78,7 def generic(func): | |||||
78 | else: |
|
78 | else: | |
79 | return f[1](*args, **kw) |
|
79 | return f[1](*args, **kw) | |
80 |
|
80 | |||
81 | try: |
|
81 | dispatch.__name__ = func.__name__ | |
82 | dispatch.__name__ = func.__name__ |
|
|||
83 | except TypeError: |
|
|||
84 | pass |
|
|||
85 | dispatch.__dict__ = func.__dict__.copy() |
|
82 | dispatch.__dict__ = func.__dict__.copy() | |
86 | dispatch.__doc__ = func.__doc__ |
|
83 | dispatch.__doc__ = func.__doc__ | |
87 | dispatch.__module__ = func.__module__ |
|
84 | dispatch.__module__ = func.__module__ | |
@@ -94,46 +91,9 def generic(func): | |||||
94 | return dispatch |
|
91 | return dispatch | |
95 |
|
92 | |||
96 |
|
93 | |||
97 |
|
||||
98 |
|
||||
99 | def test_suite(): |
|
94 | def test_suite(): | |
100 | import doctest |
|
95 | import doctest | |
101 | return doctest.DocFileSuite( |
|
96 | return doctest.DocFileSuite( | |
102 | 'README.txt', |
|
97 | 'README.txt', | |
103 | optionflags=doctest.ELLIPSIS|doctest.REPORT_ONLY_FIRST_FAILURE, |
|
98 | optionflags=doctest.ELLIPSIS|doctest.REPORT_ONLY_FIRST_FAILURE, | |
104 | ) |
|
99 | ) | |
105 |
|
||||
106 |
|
||||
107 |
|
||||
108 |
|
||||
109 |
|
||||
110 |
|
||||
111 |
|
||||
112 |
|
||||
113 |
|
||||
114 |
|
||||
115 |
|
||||
116 |
|
||||
117 |
|
||||
118 |
|
||||
119 |
|
||||
120 |
|
||||
121 |
|
||||
122 |
|
||||
123 |
|
||||
124 |
|
||||
125 |
|
||||
126 |
|
||||
127 |
|
||||
128 |
|
||||
129 |
|
||||
130 |
|
||||
131 |
|
||||
132 |
|
||||
133 |
|
||||
134 |
|
||||
135 |
|
||||
136 |
|
||||
137 |
|
||||
138 |
|
||||
139 |
|
General Comments 0
You need to be logged in to leave comments.
Login now