]>
Commit | Line | Data |
---|---|---|
3257aa99 DM |
1 | """functools.py - Tools for working with functions and callable objects\r |
2 | """\r | |
3 | # Python module wrapper for _functools C module\r | |
4 | # to allow utilities written in Python to be added\r | |
5 | # to the functools module.\r | |
6 | # Written by Nick Coghlan <ncoghlan at gmail.com>\r | |
7 | # Copyright (C) 2006 Python Software Foundation.\r | |
8 | # See C source code for _functools credits/copyright\r | |
9 | \r | |
10 | from _functools import partial, reduce\r | |
11 | \r | |
12 | # update_wrapper() and wraps() are tools to help write\r | |
13 | # wrapper functions that can handle naive introspection\r | |
14 | \r | |
15 | WRAPPER_ASSIGNMENTS = ('__module__', '__name__', '__doc__')\r | |
16 | WRAPPER_UPDATES = ('__dict__',)\r | |
17 | def update_wrapper(wrapper,\r | |
18 | wrapped,\r | |
19 | assigned = WRAPPER_ASSIGNMENTS,\r | |
20 | updated = WRAPPER_UPDATES):\r | |
21 | """Update a wrapper function to look like the wrapped function\r | |
22 | \r | |
23 | wrapper is the function to be updated\r | |
24 | wrapped is the original function\r | |
25 | assigned is a tuple naming the attributes assigned directly\r | |
26 | from the wrapped function to the wrapper function (defaults to\r | |
27 | functools.WRAPPER_ASSIGNMENTS)\r | |
28 | updated is a tuple naming the attributes of the wrapper that\r | |
29 | are updated with the corresponding attribute from the wrapped\r | |
30 | function (defaults to functools.WRAPPER_UPDATES)\r | |
31 | """\r | |
32 | for attr in assigned:\r | |
33 | setattr(wrapper, attr, getattr(wrapped, attr))\r | |
34 | for attr in updated:\r | |
35 | getattr(wrapper, attr).update(getattr(wrapped, attr, {}))\r | |
36 | # Return the wrapper so this can be used as a decorator via partial()\r | |
37 | return wrapper\r | |
38 | \r | |
39 | def wraps(wrapped,\r | |
40 | assigned = WRAPPER_ASSIGNMENTS,\r | |
41 | updated = WRAPPER_UPDATES):\r | |
42 | """Decorator factory to apply update_wrapper() to a wrapper function\r | |
43 | \r | |
44 | Returns a decorator that invokes update_wrapper() with the decorated\r | |
45 | function as the wrapper argument and the arguments to wraps() as the\r | |
46 | remaining arguments. Default arguments are as for update_wrapper().\r | |
47 | This is a convenience function to simplify applying partial() to\r | |
48 | update_wrapper().\r | |
49 | """\r | |
50 | return partial(update_wrapper, wrapped=wrapped,\r | |
51 | assigned=assigned, updated=updated)\r | |
52 | \r | |
53 | def total_ordering(cls):\r | |
54 | """Class decorator that fills in missing ordering methods"""\r | |
55 | convert = {\r | |
56 | '__lt__': [('__gt__', lambda self, other: not (self < other or self == other)),\r | |
57 | ('__le__', lambda self, other: self < other or self == other),\r | |
58 | ('__ge__', lambda self, other: not self < other)],\r | |
59 | '__le__': [('__ge__', lambda self, other: not self <= other or self == other),\r | |
60 | ('__lt__', lambda self, other: self <= other and not self == other),\r | |
61 | ('__gt__', lambda self, other: not self <= other)],\r | |
62 | '__gt__': [('__lt__', lambda self, other: not (self > other or self == other)),\r | |
63 | ('__ge__', lambda self, other: self > other or self == other),\r | |
64 | ('__le__', lambda self, other: not self > other)],\r | |
65 | '__ge__': [('__le__', lambda self, other: (not self >= other) or self == other),\r | |
66 | ('__gt__', lambda self, other: self >= other and not self == other),\r | |
67 | ('__lt__', lambda self, other: not self >= other)]\r | |
68 | }\r | |
69 | roots = set(dir(cls)) & set(convert)\r | |
70 | if not roots:\r | |
71 | raise ValueError('must define at least one ordering operation: < > <= >=')\r | |
72 | root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__\r | |
73 | for opname, opfunc in convert[root]:\r | |
74 | if opname not in roots:\r | |
75 | opfunc.__name__ = opname\r | |
76 | opfunc.__doc__ = getattr(int, opname).__doc__\r | |
77 | setattr(cls, opname, opfunc)\r | |
78 | return cls\r | |
79 | \r | |
80 | def cmp_to_key(mycmp):\r | |
81 | """Convert a cmp= function into a key= function"""\r | |
82 | class K(object):\r | |
83 | __slots__ = ['obj']\r | |
84 | def __init__(self, obj, *args):\r | |
85 | self.obj = obj\r | |
86 | def __lt__(self, other):\r | |
87 | return mycmp(self.obj, other.obj) < 0\r | |
88 | def __gt__(self, other):\r | |
89 | return mycmp(self.obj, other.obj) > 0\r | |
90 | def __eq__(self, other):\r | |
91 | return mycmp(self.obj, other.obj) == 0\r | |
92 | def __le__(self, other):\r | |
93 | return mycmp(self.obj, other.obj) <= 0\r | |
94 | def __ge__(self, other):\r | |
95 | return mycmp(self.obj, other.obj) >= 0\r | |
96 | def __ne__(self, other):\r | |
97 | return mycmp(self.obj, other.obj) != 0\r | |
98 | def __hash__(self):\r | |
99 | raise TypeError('hash not implemented')\r | |
100 | return K\r |