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