2011-10-22 07:38:52 +08:00
|
|
|
import copy
|
2011-06-01 23:30:06 +08:00
|
|
|
import operator
|
2013-03-07 08:09:36 +08:00
|
|
|
from functools import wraps
|
2012-05-08 01:38:14 +08:00
|
|
|
import sys
|
2008-02-25 14:02:35 +08:00
|
|
|
|
2012-07-20 20:48:51 +08:00
|
|
|
from django.utils import six
|
2013-05-21 20:41:39 +08:00
|
|
|
from django.utils.six.moves import copyreg
|
2013-05-21 13:17:56 +08:00
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
2012-12-15 21:22:22 +08:00
|
|
|
# You can't trivially replace this with `functools.partial` because this binds
|
|
|
|
# to classes and returns bound instances, whereas functools.partial (on
|
|
|
|
# CPython) is a type and its instances don't bind.
|
2006-09-08 02:26:00 +08:00
|
|
|
def curry(_curried_func, *args, **kwargs):
|
2005-08-02 05:29:52 +08:00
|
|
|
def _curried(*moreargs, **morekwargs):
|
2013-03-07 08:09:36 +08:00
|
|
|
return _curried_func(*(args + moreargs), **dict(kwargs, **morekwargs))
|
2005-08-02 05:29:52 +08:00
|
|
|
return _curried
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
2007-07-08 08:39:32 +08:00
|
|
|
def memoize(func, cache, num_args):
|
2007-06-23 13:40:28 +08:00
|
|
|
"""
|
|
|
|
Wrap a function so that results for any argument tuple are stored in
|
|
|
|
'cache'. Note that the args to the function must be usable as dictionary
|
|
|
|
keys.
|
2007-07-08 08:39:32 +08:00
|
|
|
|
|
|
|
Only the first num_args are considered when creating the key.
|
2007-06-23 13:40:28 +08:00
|
|
|
"""
|
2011-05-02 00:46:02 +08:00
|
|
|
@wraps(func)
|
2007-06-23 13:40:28 +08:00
|
|
|
def wrapper(*args):
|
2007-07-08 08:39:32 +08:00
|
|
|
mem_args = args[:num_args]
|
|
|
|
if mem_args in cache:
|
|
|
|
return cache[mem_args]
|
2007-06-23 13:40:28 +08:00
|
|
|
result = func(*args)
|
2007-07-08 08:39:32 +08:00
|
|
|
cache[mem_args] = result
|
2007-06-23 13:40:28 +08:00
|
|
|
return result
|
2011-05-02 00:46:02 +08:00
|
|
|
return wrapper
|
2007-06-23 13:40:28 +08:00
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
2011-09-30 18:41:25 +08:00
|
|
|
class cached_property(object):
|
|
|
|
"""
|
2012-12-15 21:22:22 +08:00
|
|
|
Decorator that converts a method with a single self argument into a
|
|
|
|
property cached on the instance.
|
2011-09-30 18:41:25 +08:00
|
|
|
"""
|
|
|
|
def __init__(self, func):
|
|
|
|
self.func = func
|
|
|
|
|
2013-02-24 06:20:00 +08:00
|
|
|
def __get__(self, instance, type=None):
|
|
|
|
if instance is None:
|
|
|
|
return self
|
2011-09-30 18:41:25 +08:00
|
|
|
res = instance.__dict__[self.func.__name__] = self.func(instance)
|
|
|
|
return res
|
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
class Promise(object):
|
2005-11-21 18:41:54 +08:00
|
|
|
"""
|
|
|
|
This is just a base class for the proxy class created in
|
|
|
|
the closure of the lazy function. It can be used to recognize
|
|
|
|
promises in code.
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def lazy(func, *resultclasses):
|
|
|
|
"""
|
2005-11-07 06:22:02 +08:00
|
|
|
Turns any callable into a lazy evaluated callable. You need to give result
|
|
|
|
classes or types -- at least one is needed so that the automatic forcing of
|
|
|
|
the lazy evaluation code is triggered. Results are not memoized; the
|
|
|
|
function is evaluated on every access.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
2010-03-27 23:54:31 +08:00
|
|
|
|
2012-05-18 20:52:24 +08:00
|
|
|
@total_ordering
|
2005-11-21 18:41:54 +08:00
|
|
|
class __proxy__(Promise):
|
2008-07-28 08:45:04 +08:00
|
|
|
"""
|
|
|
|
Encapsulate a function call and act as a proxy for methods that are
|
|
|
|
called on the result of that function. The function is not evaluated
|
|
|
|
until one of the methods on the result is called.
|
|
|
|
"""
|
|
|
|
__dispatch = None
|
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def __init__(self, args, kw):
|
2006-05-02 09:31:56 +08:00
|
|
|
self.__args = args
|
|
|
|
self.__kw = kw
|
2008-07-28 08:45:04 +08:00
|
|
|
if self.__dispatch is None:
|
|
|
|
self.__prepare_class__()
|
|
|
|
|
2010-04-19 20:40:46 +08:00
|
|
|
def __reduce__(self):
|
|
|
|
return (
|
|
|
|
_lazy_proxy_unpickle,
|
2012-01-09 03:16:33 +08:00
|
|
|
(func, self.__args, self.__kw) + resultclasses
|
2010-04-19 20:40:46 +08:00
|
|
|
)
|
2010-03-27 23:54:31 +08:00
|
|
|
|
2008-07-28 08:45:04 +08:00
|
|
|
def __prepare_class__(cls):
|
|
|
|
cls.__dispatch = {}
|
2006-05-02 09:31:56 +08:00
|
|
|
for resultclass in resultclasses:
|
2008-07-28 08:45:04 +08:00
|
|
|
cls.__dispatch[resultclass] = {}
|
2011-05-05 08:09:51 +08:00
|
|
|
for type_ in reversed(resultclass.mro()):
|
|
|
|
for (k, v) in type_.__dict__.items():
|
2013-03-07 08:09:36 +08:00
|
|
|
# All __promise__ return the same wrapper method, but
|
|
|
|
# they also do setup, inserting the method into the
|
|
|
|
# dispatch dict.
|
2011-05-05 08:09:51 +08:00
|
|
|
meth = cls.__promise__(resultclass, k, v)
|
|
|
|
if hasattr(cls, k):
|
|
|
|
continue
|
|
|
|
setattr(cls, k, meth)
|
2012-08-04 21:55:53 +08:00
|
|
|
cls._delegate_bytes = bytes in resultclasses
|
|
|
|
cls._delegate_text = six.text_type in resultclasses
|
|
|
|
assert not (cls._delegate_bytes and cls._delegate_text), "Cannot call lazy() with both bytes and text return types."
|
|
|
|
if cls._delegate_text:
|
|
|
|
if six.PY3:
|
|
|
|
cls.__str__ = cls.__text_cast
|
|
|
|
else:
|
|
|
|
cls.__unicode__ = cls.__text_cast
|
|
|
|
elif cls._delegate_bytes:
|
|
|
|
if six.PY3:
|
|
|
|
cls.__bytes__ = cls.__bytes_cast
|
|
|
|
else:
|
|
|
|
cls.__str__ = cls.__bytes_cast
|
2008-07-28 08:45:04 +08:00
|
|
|
__prepare_class__ = classmethod(__prepare_class__)
|
|
|
|
|
2012-01-09 03:16:33 +08:00
|
|
|
def __promise__(cls, klass, funcname, method):
|
2013-03-07 08:09:36 +08:00
|
|
|
# Builds a wrapper around some magic method and registers that
|
|
|
|
# magic method for the given type and method name.
|
2008-07-28 08:45:04 +08:00
|
|
|
def __wrapper__(self, *args, **kw):
|
2005-11-07 06:22:02 +08:00
|
|
|
# Automatically triggers the evaluation of a lazy value and
|
|
|
|
# applies the given magic method of the result type.
|
2012-01-09 03:16:33 +08:00
|
|
|
res = func(*self.__args, **self.__kw)
|
2008-07-28 08:45:04 +08:00
|
|
|
for t in type(res).mro():
|
|
|
|
if t in self.__dispatch:
|
|
|
|
return self.__dispatch[t][funcname](res, *args, **kw)
|
|
|
|
raise TypeError("Lazy object returned unexpected type.")
|
|
|
|
|
|
|
|
if klass not in cls.__dispatch:
|
|
|
|
cls.__dispatch[klass] = {}
|
2012-01-09 03:16:33 +08:00
|
|
|
cls.__dispatch[klass][funcname] = method
|
2005-11-04 12:59:46 +08:00
|
|
|
return __wrapper__
|
2008-07-28 08:45:04 +08:00
|
|
|
__promise__ = classmethod(__promise__)
|
2005-11-07 06:22:02 +08:00
|
|
|
|
2012-08-04 21:55:53 +08:00
|
|
|
def __text_cast(self):
|
2012-01-09 03:16:33 +08:00
|
|
|
return func(*self.__args, **self.__kw)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2012-08-04 21:55:53 +08:00
|
|
|
def __bytes_cast(self):
|
|
|
|
return bytes(func(*self.__args, **self.__kw))
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2012-05-18 20:52:24 +08:00
|
|
|
def __cast(self):
|
2012-08-04 21:55:53 +08:00
|
|
|
if self._delegate_bytes:
|
|
|
|
return self.__bytes_cast()
|
|
|
|
elif self._delegate_text:
|
|
|
|
return self.__text_cast()
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
else:
|
2012-05-18 20:52:24 +08:00
|
|
|
return func(*self.__args, **self.__kw)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, Promise):
|
|
|
|
other = other.__cast()
|
|
|
|
return self.__cast() == other
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
if isinstance(other, Promise):
|
|
|
|
other = other.__cast()
|
|
|
|
return self.__cast() < other
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2013-02-26 05:53:08 +08:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.__cast())
|
2012-08-04 21:55:53 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def __mod__(self, rhs):
|
2012-08-04 21:55:53 +08:00
|
|
|
if self._delegate_bytes and not six.PY3:
|
|
|
|
return bytes(self) % rhs
|
|
|
|
elif self._delegate_text:
|
2012-07-20 20:48:51 +08:00
|
|
|
return six.text_type(self) % rhs
|
2013-01-31 03:28:16 +08:00
|
|
|
return self.__cast() % rhs
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2007-09-15 18:57:03 +08:00
|
|
|
def __deepcopy__(self, memo):
|
2007-09-15 19:13:21 +08:00
|
|
|
# Instances of this class are effectively immutable. It's just a
|
|
|
|
# collection of functions. So we don't need to do anything
|
|
|
|
# complicated for copying.
|
|
|
|
memo[id(self)] = self
|
|
|
|
return self
|
2007-09-15 18:57:03 +08:00
|
|
|
|
2011-05-02 00:46:02 +08:00
|
|
|
@wraps(func)
|
2005-11-04 12:59:46 +08:00
|
|
|
def __wrapper__(*args, **kw):
|
2005-11-07 06:22:02 +08:00
|
|
|
# Creates the proxy object, instead of the actual value.
|
2005-11-04 12:59:46 +08:00
|
|
|
return __proxy__(args, kw)
|
|
|
|
|
2011-05-02 00:46:02 +08:00
|
|
|
return __wrapper__
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
2010-04-19 20:40:46 +08:00
|
|
|
def _lazy_proxy_unpickle(func, args, kwargs, *resultclasses):
|
|
|
|
return lazy(func, *resultclasses)(*args, **kwargs)
|
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def allow_lazy(func, *resultclasses):
|
|
|
|
"""
|
|
|
|
A decorator that allows a function to be called with one or more lazy
|
|
|
|
arguments. If none of the args are lazy, the function is evaluated
|
|
|
|
immediately, otherwise a __proxy__ is returned that will evaluate the
|
|
|
|
function when needed.
|
|
|
|
"""
|
2011-05-02 00:46:02 +08:00
|
|
|
@wraps(func)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def wrapper(*args, **kwargs):
|
2012-07-21 03:14:27 +08:00
|
|
|
for arg in list(args) + list(six.itervalues(kwargs)):
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
if isinstance(arg, Promise):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return lazy(func, *resultclasses)(*args, **kwargs)
|
2011-05-02 00:46:02 +08:00
|
|
|
return wrapper
|
2009-03-02 12:48:22 +08:00
|
|
|
|
2011-06-01 23:30:06 +08:00
|
|
|
empty = object()
|
2013-03-07 08:09:36 +08:00
|
|
|
|
|
|
|
|
2011-06-01 23:30:06 +08:00
|
|
|
def new_method_proxy(func):
|
|
|
|
def inner(self, *args):
|
|
|
|
if self._wrapped is empty:
|
|
|
|
self._setup()
|
|
|
|
return func(self._wrapped, *args)
|
|
|
|
return inner
|
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
2009-03-02 12:48:22 +08:00
|
|
|
class LazyObject(object):
|
|
|
|
"""
|
|
|
|
A wrapper for another class that can be used to delay instantiation of the
|
|
|
|
wrapped class.
|
|
|
|
|
2009-10-15 22:12:34 +08:00
|
|
|
By subclassing, you have the opportunity to intercept and alter the
|
|
|
|
instantiation. If you don't need to do that, use SimpleLazyObject.
|
2009-03-02 12:48:22 +08:00
|
|
|
"""
|
2013-03-18 18:22:43 +08:00
|
|
|
|
|
|
|
# Avoid infinite recursion when tracing __init__ (#19456).
|
|
|
|
_wrapped = None
|
|
|
|
|
2009-03-02 12:48:22 +08:00
|
|
|
def __init__(self):
|
2011-06-01 23:30:06 +08:00
|
|
|
self._wrapped = empty
|
2009-03-02 12:48:22 +08:00
|
|
|
|
2011-06-01 23:30:06 +08:00
|
|
|
__getattr__ = new_method_proxy(getattr)
|
2009-03-02 12:48:22 +08:00
|
|
|
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
if name == "_wrapped":
|
|
|
|
# Assign to __dict__ to avoid infinite __setattr__ loops.
|
|
|
|
self.__dict__["_wrapped"] = value
|
|
|
|
else:
|
2011-06-01 23:30:06 +08:00
|
|
|
if self._wrapped is empty:
|
2009-03-02 12:48:22 +08:00
|
|
|
self._setup()
|
|
|
|
setattr(self._wrapped, name, value)
|
|
|
|
|
2009-12-13 01:51:51 +08:00
|
|
|
def __delattr__(self, name):
|
|
|
|
if name == "_wrapped":
|
|
|
|
raise TypeError("can't delete _wrapped.")
|
2011-06-01 23:30:06 +08:00
|
|
|
if self._wrapped is empty:
|
2009-12-13 01:51:51 +08:00
|
|
|
self._setup()
|
|
|
|
delattr(self._wrapped, name)
|
|
|
|
|
2009-03-02 12:48:22 +08:00
|
|
|
def _setup(self):
|
|
|
|
"""
|
|
|
|
Must be implemented by subclasses to initialise the wrapped object.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
2013-03-18 18:09:29 +08:00
|
|
|
# Introspection support
|
2011-06-01 23:30:06 +08:00
|
|
|
__dir__ = new_method_proxy(dir)
|
2009-10-20 05:48:06 +08:00
|
|
|
|
2013-03-18 18:09:29 +08:00
|
|
|
# Dictionary methods support
|
|
|
|
@new_method_proxy
|
|
|
|
def __getitem__(self, key):
|
|
|
|
return self[key]
|
|
|
|
|
|
|
|
@new_method_proxy
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
self[key] = value
|
|
|
|
|
|
|
|
@new_method_proxy
|
|
|
|
def __delitem__(self, key):
|
|
|
|
del self[key]
|
|
|
|
|
2009-10-15 22:12:34 +08:00
|
|
|
|
2012-08-04 21:55:53 +08:00
|
|
|
# Workaround for http://bugs.python.org/issue12370
|
|
|
|
_super = super
|
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
2009-10-15 22:12:34 +08:00
|
|
|
class SimpleLazyObject(LazyObject):
|
|
|
|
"""
|
|
|
|
A lazy object initialised from any function.
|
|
|
|
|
|
|
|
Designed for compound objects of unknown type. For builtins or objects of
|
|
|
|
known type, use django.utils.functional.lazy.
|
|
|
|
"""
|
|
|
|
def __init__(self, func):
|
|
|
|
"""
|
|
|
|
Pass in a callable that returns the object to be wrapped.
|
2009-10-20 05:13:22 +08:00
|
|
|
|
|
|
|
If copies are made of the resulting SimpleLazyObject, which can happen
|
|
|
|
in various circumstances within Django, then you must ensure that the
|
|
|
|
callable can be safely run more than once and will return the same
|
|
|
|
value.
|
2009-10-15 22:12:34 +08:00
|
|
|
"""
|
|
|
|
self.__dict__['_setupfunc'] = func
|
2012-08-04 21:55:53 +08:00
|
|
|
_super(SimpleLazyObject, self).__init__()
|
2009-10-15 22:12:34 +08:00
|
|
|
|
2011-06-01 23:30:06 +08:00
|
|
|
def _setup(self):
|
|
|
|
self._wrapped = self._setupfunc()
|
2009-10-15 22:12:34 +08:00
|
|
|
|
2012-08-04 21:55:53 +08:00
|
|
|
if six.PY3:
|
|
|
|
__bytes__ = new_method_proxy(bytes)
|
|
|
|
__str__ = new_method_proxy(str)
|
|
|
|
else:
|
|
|
|
__str__ = new_method_proxy(str)
|
|
|
|
__unicode__ = new_method_proxy(unicode)
|
2009-10-20 22:11:08 +08:00
|
|
|
|
2009-10-20 05:13:22 +08:00
|
|
|
def __deepcopy__(self, memo):
|
2011-06-01 23:30:06 +08:00
|
|
|
if self._wrapped is empty:
|
2009-10-20 22:11:08 +08:00
|
|
|
# We have to use SimpleLazyObject, not self.__class__, because the
|
|
|
|
# latter is proxied.
|
|
|
|
result = SimpleLazyObject(self._setupfunc)
|
2009-10-20 05:13:22 +08:00
|
|
|
memo[id(self)] = result
|
|
|
|
return result
|
|
|
|
else:
|
2011-03-28 10:11:19 +08:00
|
|
|
return copy.deepcopy(self._wrapped, memo)
|
2009-10-20 05:13:22 +08:00
|
|
|
|
2011-12-15 10:33:14 +08:00
|
|
|
# Because we have messed with __class__ below, we confuse pickle as to what
|
|
|
|
# class we are pickling. It also appears to stop __reduce__ from being
|
|
|
|
# called. So, we define __getstate__ in a way that cooperates with the way
|
2013-03-07 08:09:36 +08:00
|
|
|
# that pickle interprets this class. This fails when the wrapped class is
|
|
|
|
# a builtin, but it is better than nothing.
|
2011-12-15 10:33:14 +08:00
|
|
|
def __getstate__(self):
|
|
|
|
if self._wrapped is empty:
|
|
|
|
self._setup()
|
|
|
|
return self._wrapped.__dict__
|
|
|
|
|
2013-05-21 13:17:56 +08:00
|
|
|
# Python 3.3 will call __reduce__ when pickling; this method is needed
|
|
|
|
# to serialize and deserialize correctly.
|
2012-09-29 03:57:38 +08:00
|
|
|
@classmethod
|
|
|
|
def __newobj__(cls, *args):
|
|
|
|
return cls.__new__(cls, *args)
|
|
|
|
|
2013-05-21 13:17:56 +08:00
|
|
|
def __reduce_ex__(self, proto):
|
|
|
|
if proto >= 2:
|
|
|
|
# On Py3, since the default protocol is 3, pickle uses the
|
|
|
|
# ``__newobj__`` method (& more efficient opcodes) for writing.
|
|
|
|
return (self.__newobj__, (self.__class__,), self.__getstate__())
|
|
|
|
else:
|
|
|
|
# On Py2, the default protocol is 0 (for back-compat) & the above
|
|
|
|
# code fails miserably (see regression test). Instead, we return
|
|
|
|
# exactly what's returned if there's no ``__reduce__`` method at
|
|
|
|
# all.
|
|
|
|
return (copyreg._reconstructor, (self.__class__, object, None), self.__getstate__())
|
2012-09-29 03:57:38 +08:00
|
|
|
|
2013-03-07 08:00:05 +08:00
|
|
|
# Return a meaningful representation of the lazy object for debugging
|
|
|
|
# without evaluating the wrapped object.
|
|
|
|
def __repr__(self):
|
|
|
|
if self._wrapped is empty:
|
|
|
|
repr_attr = self._setupfunc
|
|
|
|
else:
|
|
|
|
repr_attr = self._wrapped
|
|
|
|
return '<SimpleLazyObject: %r>' % repr_attr
|
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
# Need to pretend to be the wrapped class, for the sake of objects that
|
|
|
|
# care about this (especially in equality tests)
|
2011-06-01 23:30:06 +08:00
|
|
|
__class__ = property(new_method_proxy(operator.attrgetter("__class__")))
|
|
|
|
__eq__ = new_method_proxy(operator.eq)
|
2013-04-20 01:58:29 +08:00
|
|
|
__ne__ = new_method_proxy(operator.ne)
|
2011-06-01 23:30:06 +08:00
|
|
|
__hash__ = new_method_proxy(hash)
|
2012-08-04 21:55:53 +08:00
|
|
|
__bool__ = new_method_proxy(bool) # Python 3
|
|
|
|
__nonzero__ = __bool__ # Python 2
|
2011-06-01 21:47:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
class lazy_property(property):
|
|
|
|
"""
|
|
|
|
A property that works with subclasses by wrapping the decorated
|
|
|
|
functions of the base class.
|
|
|
|
"""
|
|
|
|
def __new__(cls, fget=None, fset=None, fdel=None, doc=None):
|
|
|
|
if fget is not None:
|
|
|
|
@wraps(fget)
|
|
|
|
def fget(instance, instance_type=None, name=fget.__name__):
|
|
|
|
return getattr(instance, name)()
|
|
|
|
if fset is not None:
|
|
|
|
@wraps(fset)
|
|
|
|
def fset(instance, value, name=fset.__name__):
|
|
|
|
return getattr(instance, name)(value)
|
|
|
|
if fdel is not None:
|
|
|
|
@wraps(fdel)
|
|
|
|
def fdel(instance, name=fdel.__name__):
|
|
|
|
return getattr(instance, name)()
|
2011-09-10 03:22:28 +08:00
|
|
|
return property(fget, fset, fdel, doc)
|
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
2011-09-10 03:22:28 +08:00
|
|
|
def partition(predicate, values):
|
|
|
|
"""
|
|
|
|
Splits the values into two sets, based on the return value of the function
|
|
|
|
(True/False). e.g.:
|
|
|
|
|
2012-08-05 06:56:43 +08:00
|
|
|
>>> partition(lambda x: x > 3, range(5))
|
|
|
|
[0, 1, 2, 3], [4]
|
2011-09-10 03:22:28 +08:00
|
|
|
"""
|
|
|
|
results = ([], [])
|
|
|
|
for item in values:
|
|
|
|
results[predicate(item)].append(item)
|
2011-09-30 18:41:25 +08:00
|
|
|
return results
|
2012-05-08 01:08:20 +08:00
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
if sys.version_info >= (2, 7, 2):
|
2012-05-08 01:08:20 +08:00
|
|
|
from functools import total_ordering
|
2012-05-08 01:38:14 +08:00
|
|
|
else:
|
2013-07-14 01:29:11 +08:00
|
|
|
# For Python < 2.7.2. total_ordering in versions prior to 2.7.2 is buggy.
|
|
|
|
# See http://bugs.python.org/issue10042 for details. For these versions use
|
2012-05-08 01:38:14 +08:00
|
|
|
# code borrowed from Python 2.7.3.
|
2012-05-08 01:08:20 +08:00
|
|
|
def total_ordering(cls):
|
|
|
|
"""Class decorator that fills in missing ordering methods"""
|
|
|
|
convert = {
|
|
|
|
'__lt__': [('__gt__', lambda self, other: not (self < other or self == other)),
|
|
|
|
('__le__', lambda self, other: self < other or self == other),
|
|
|
|
('__ge__', lambda self, other: not self < other)],
|
|
|
|
'__le__': [('__ge__', lambda self, other: not self <= other or self == other),
|
|
|
|
('__lt__', lambda self, other: self <= other and not self == other),
|
|
|
|
('__gt__', lambda self, other: not self <= other)],
|
|
|
|
'__gt__': [('__lt__', lambda self, other: not (self > other or self == other)),
|
|
|
|
('__ge__', lambda self, other: self > other or self == other),
|
|
|
|
('__le__', lambda self, other: not self > other)],
|
|
|
|
'__ge__': [('__le__', lambda self, other: (not self >= other) or self == other),
|
|
|
|
('__gt__', lambda self, other: self >= other and not self == other),
|
|
|
|
('__lt__', lambda self, other: not self >= other)]
|
|
|
|
}
|
|
|
|
roots = set(dir(cls)) & set(convert)
|
|
|
|
if not roots:
|
|
|
|
raise ValueError('must define at least one ordering operation: < > <= >=')
|
|
|
|
root = max(roots) # prefer __lt__ to __le__ to __gt__ to __ge__
|
|
|
|
for opname, opfunc in convert[root]:
|
|
|
|
if opname not in roots:
|
|
|
|
opfunc.__name__ = opname
|
|
|
|
opfunc.__doc__ = getattr(int, opname).__doc__
|
|
|
|
setattr(cls, opname, opfunc)
|
|
|
|
return cls
|