2011-10-22 07:38:52 +08:00
|
|
|
import copy
|
2017-10-13 09:02:04 +08:00
|
|
|
import itertools
|
2011-06-01 23:30:06 +08:00
|
|
|
import operator
|
2015-06-15 22:10:40 +08:00
|
|
|
from functools import total_ordering, wraps
|
2008-02-25 14:02:35 +08:00
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class cached_property:
|
2011-09-30 18:41:25 +08:00
|
|
|
"""
|
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.
|
2014-05-24 18:29:31 +08:00
|
|
|
|
2018-06-07 21:03:45 +08:00
|
|
|
A cached property can be made out of an existing method:
|
|
|
|
(e.g. ``url = cached_property(get_absolute_url)``).
|
2019-01-18 23:04:29 +08:00
|
|
|
The optional ``name`` argument is obsolete as of Python 3.6 and will be
|
|
|
|
deprecated in Django 4.0 (#30127).
|
2011-09-30 18:41:25 +08:00
|
|
|
"""
|
2018-06-07 21:03:45 +08:00
|
|
|
name = None
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def func(instance):
|
|
|
|
raise TypeError(
|
|
|
|
'Cannot use cached_property instance without calling '
|
|
|
|
'__set_name__() on it.'
|
|
|
|
)
|
|
|
|
|
2014-05-24 18:29:31 +08:00
|
|
|
def __init__(self, func, name=None):
|
2019-01-18 23:04:29 +08:00
|
|
|
self.real_func = func
|
2014-10-20 06:34:35 +08:00
|
|
|
self.__doc__ = getattr(func, '__doc__')
|
2018-06-07 21:03:45 +08:00
|
|
|
|
|
|
|
def __set_name__(self, owner, name):
|
|
|
|
if self.name is None:
|
|
|
|
self.name = name
|
|
|
|
self.func = self.real_func
|
|
|
|
elif name != self.name:
|
|
|
|
raise TypeError(
|
|
|
|
"Cannot assign the same cached_property to two different names "
|
|
|
|
"(%r and %r)." % (self.name, name)
|
|
|
|
)
|
2011-09-30 18:41:25 +08:00
|
|
|
|
2015-10-26 23:31:16 +08:00
|
|
|
def __get__(self, instance, cls=None):
|
2017-05-15 21:05:42 +08:00
|
|
|
"""
|
2017-05-15 21:25:14 +08:00
|
|
|
Call the function and put the return value in instance.__dict__ so that
|
|
|
|
subsequent attribute access on the instance returns the cached value
|
|
|
|
instead of calling cached_property.__get__().
|
2017-05-15 21:05:42 +08:00
|
|
|
"""
|
2013-02-24 06:20:00 +08:00
|
|
|
if instance is None:
|
|
|
|
return self
|
2014-05-24 18:29:31 +08:00
|
|
|
res = instance.__dict__[self.name] = self.func(instance)
|
2011-09-30 18:41:25 +08:00
|
|
|
return res
|
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
2019-10-19 03:00:34 +08:00
|
|
|
class classproperty:
|
|
|
|
def __init__(self, method=None):
|
|
|
|
self.fget = method
|
|
|
|
|
|
|
|
def __get__(self, instance, cls=None):
|
|
|
|
return self.fget(cls)
|
|
|
|
|
|
|
|
def getter(self, method):
|
|
|
|
self.fget = method
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Promise:
|
2005-11-21 18:41:54 +08:00
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Base class for the proxy class created in the closure of the lazy function.
|
|
|
|
It's used to recognize promises in code.
|
2005-11-21 18:41:54 +08:00
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2013-03-07 08:09:36 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def lazy(func, *resultclasses):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Turn any callable into a lazy evaluated callable. result classes or types
|
|
|
|
is required -- at least one is needed so that the automatic forcing of
|
2005-11-07 06:22:02 +08:00
|
|
|
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.
|
|
|
|
"""
|
2014-08-23 00:31:26 +08:00
|
|
|
__prepared = False
|
2008-07-28 08:45:04 +08:00
|
|
|
|
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
|
2014-08-23 00:31:26 +08:00
|
|
|
if not self.__prepared:
|
2008-07-28 08:45:04 +08:00
|
|
|
self.__prepare_class__()
|
2019-05-03 18:46:21 +08:00
|
|
|
self.__class__.__prepared = True
|
2008-07-28 08:45:04 +08:00
|
|
|
|
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
|
|
|
|
2016-06-05 07:13:00 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return repr(self.__cast())
|
|
|
|
|
2013-11-20 10:02:30 +08:00
|
|
|
@classmethod
|
2008-07-28 08:45:04 +08:00
|
|
|
def __prepare_class__(cls):
|
2006-05-02 09:31:56 +08:00
|
|
|
for resultclass in resultclasses:
|
2014-08-23 00:31:26 +08:00
|
|
|
for type_ in resultclass.mro():
|
2017-05-28 07:08:46 +08:00
|
|
|
for method_name in type_.__dict__:
|
2014-08-23 00:31:26 +08:00
|
|
|
# All __promise__ return the same wrapper method, they
|
|
|
|
# look up the correct implementation when called.
|
|
|
|
if hasattr(cls, method_name):
|
2011-05-05 08:09:51 +08:00
|
|
|
continue
|
2014-08-23 00:31:26 +08:00
|
|
|
meth = cls.__promise__(method_name)
|
|
|
|
setattr(cls, method_name, meth)
|
2012-08-04 21:55:53 +08:00
|
|
|
cls._delegate_bytes = bytes in resultclasses
|
2016-12-29 23:27:49 +08:00
|
|
|
cls._delegate_text = str in resultclasses
|
2014-09-04 20:15:09 +08:00
|
|
|
assert not (cls._delegate_bytes and cls._delegate_text), (
|
|
|
|
"Cannot call lazy() with both bytes and text return types.")
|
2012-08-04 21:55:53 +08:00
|
|
|
if cls._delegate_text:
|
2016-12-01 18:38:01 +08:00
|
|
|
cls.__str__ = cls.__text_cast
|
2012-08-04 21:55:53 +08:00
|
|
|
elif cls._delegate_bytes:
|
2016-12-01 18:38:01 +08:00
|
|
|
cls.__bytes__ = cls.__bytes_cast
|
2008-07-28 08:45:04 +08:00
|
|
|
|
2013-11-20 10:02:30 +08:00
|
|
|
@classmethod
|
2014-08-23 00:31:26 +08:00
|
|
|
def __promise__(cls, method_name):
|
|
|
|
# Builds a wrapper around some magic method
|
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)
|
2014-08-23 00:31:26 +08:00
|
|
|
return getattr(res, method_name)(*args, **kw)
|
2005-11-04 12:59:46 +08:00
|
|
|
return __wrapper__
|
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
|
|
|
|
2015-01-31 23:05:16 +08:00
|
|
|
def __bytes_cast_encoded(self):
|
2017-02-08 01:05:47 +08:00
|
|
|
return func(*self.__args, **self.__kw).encode()
|
2015-01-31 23:05:16 +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)
|
|
|
|
|
2015-06-20 20:00:55 +08:00
|
|
|
def __str__(self):
|
|
|
|
# object defines __str__(), so __prepare_class__() won't overload
|
|
|
|
# a __str__() method from the proxied class.
|
|
|
|
return str(self.__cast())
|
|
|
|
|
2012-05-18 20:52:24 +08:00
|
|
|
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):
|
2016-12-01 18:38:01 +08:00
|
|
|
if self._delegate_text:
|
2016-12-29 23:27:49 +08:00
|
|
|
return str(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
|
|
|
|
2015-11-07 21:30:20 +08:00
|
|
|
def lazystr(text):
|
|
|
|
"""
|
|
|
|
Shortcut for the common case of a lazy callable that returns str.
|
|
|
|
"""
|
2017-03-04 22:47:49 +08:00
|
|
|
return lazy(str, str)(text)
|
2015-11-07 21:30:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
def keep_lazy(*resultclasses):
|
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
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2015-11-07 21:30:20 +08:00
|
|
|
if not resultclasses:
|
|
|
|
raise TypeError("You must pass at least one argument to keep_lazy().")
|
2015-02-09 02:14:30 +08:00
|
|
|
|
2015-11-07 21:30:20 +08:00
|
|
|
def decorator(func):
|
|
|
|
lazy_func = lazy(func, *resultclasses)
|
|
|
|
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
2017-12-27 02:44:12 +08:00
|
|
|
if any(isinstance(arg, Promise) for arg in itertools.chain(args, kwargs.values())):
|
|
|
|
return lazy_func(*args, **kwargs)
|
|
|
|
return func(*args, **kwargs)
|
2015-11-07 21:30:20 +08:00
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def keep_lazy_text(func):
|
|
|
|
"""
|
|
|
|
A decorator for functions that accept lazy arguments and return text.
|
|
|
|
"""
|
2016-12-29 23:27:49 +08:00
|
|
|
return keep_lazy(str)(func)
|
2009-03-02 12:48:22 +08:00
|
|
|
|
2016-11-13 01:11:23 +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
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class LazyObject:
|
2009-03-02 12:48:22 +08:00
|
|
|
"""
|
|
|
|
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):
|
2016-01-24 10:02:15 +08:00
|
|
|
# Note: if a subclass overrides __init__(), it will likely need to
|
|
|
|
# override __copy__() and __deepcopy__() as well.
|
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):
|
|
|
|
"""
|
2013-11-15 21:23:10 +08:00
|
|
|
Must be implemented by subclasses to initialize the wrapped object.
|
2009-03-02 12:48:22 +08:00
|
|
|
"""
|
2013-09-07 02:24:52 +08:00
|
|
|
raise NotImplementedError('subclasses of LazyObject must provide a _setup() method')
|
2009-03-02 12:48:22 +08:00
|
|
|
|
2014-01-29 12:53:30 +08:00
|
|
|
# Because we have messed with __class__ below, we confuse pickle as to what
|
2015-09-12 14:06:25 +08:00
|
|
|
# class we are pickling. We're going to have to initialize the wrapped
|
|
|
|
# object to successfully pickle it, so we might as well just pickle the
|
|
|
|
# wrapped object since they're supposed to act the same way.
|
|
|
|
#
|
|
|
|
# Unfortunately, if we try to simply act like the wrapped object, the ruse
|
|
|
|
# will break down when pickle gets our id(). Thus we end up with pickle
|
|
|
|
# thinking, in effect, that we are a distinct object from the wrapped
|
|
|
|
# object, but with the same __dict__. This can cause problems (see #25389).
|
|
|
|
#
|
|
|
|
# So instead, we define our own __reduce__ method and custom unpickler. We
|
|
|
|
# pickle the wrapped object as the unpickler's argument, so that pickle
|
|
|
|
# will pickle it normally, and then the unpickler simply returns its
|
|
|
|
# argument.
|
|
|
|
def __reduce__(self):
|
2014-01-29 12:53:30 +08:00
|
|
|
if self._wrapped is empty:
|
|
|
|
self._setup()
|
2015-09-12 14:06:25 +08:00
|
|
|
return (unpickle_lazyobject, (self._wrapped,))
|
|
|
|
|
2016-01-24 10:02:15 +08:00
|
|
|
def __copy__(self):
|
|
|
|
if self._wrapped is empty:
|
|
|
|
# If uninitialized, copy the wrapper. Use type(self), not
|
|
|
|
# self.__class__, because the latter is proxied.
|
|
|
|
return type(self)()
|
|
|
|
else:
|
|
|
|
# If initialized, return a copy of the wrapped object.
|
|
|
|
return copy.copy(self._wrapped)
|
|
|
|
|
2014-01-29 12:53:30 +08:00
|
|
|
def __deepcopy__(self, memo):
|
|
|
|
if self._wrapped is empty:
|
|
|
|
# We have to use type(self), not self.__class__, because the
|
|
|
|
# latter is proxied.
|
|
|
|
result = type(self)()
|
|
|
|
memo[id(self)] = result
|
|
|
|
return result
|
|
|
|
return copy.deepcopy(self._wrapped, memo)
|
|
|
|
|
2016-12-01 18:38:01 +08:00
|
|
|
__bytes__ = new_method_proxy(bytes)
|
|
|
|
__str__ = new_method_proxy(str)
|
|
|
|
__bool__ = new_method_proxy(bool)
|
2014-01-29 12:53:30 +08:00
|
|
|
|
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
|
|
|
|
2014-01-29 12:53:30 +08:00
|
|
|
# Need to pretend to be the wrapped class, for the sake of objects that
|
|
|
|
# care about this (especially in equality tests)
|
|
|
|
__class__ = property(new_method_proxy(operator.attrgetter("__class__")))
|
|
|
|
__eq__ = new_method_proxy(operator.eq)
|
2018-09-20 01:51:01 +08:00
|
|
|
__lt__ = new_method_proxy(operator.lt)
|
|
|
|
__gt__ = new_method_proxy(operator.gt)
|
2014-01-29 12:53:30 +08:00
|
|
|
__ne__ = new_method_proxy(operator.ne)
|
|
|
|
__hash__ = new_method_proxy(hash)
|
|
|
|
|
2015-03-07 22:51:17 +08:00
|
|
|
# List/Tuple/Dictionary methods support
|
2013-08-19 00:43:41 +08:00
|
|
|
__getitem__ = new_method_proxy(operator.getitem)
|
|
|
|
__setitem__ = new_method_proxy(operator.setitem)
|
|
|
|
__delitem__ = new_method_proxy(operator.delitem)
|
2015-03-07 22:51:17 +08:00
|
|
|
__iter__ = new_method_proxy(iter)
|
2013-08-18 21:58:55 +08:00
|
|
|
__len__ = new_method_proxy(len)
|
|
|
|
__contains__ = new_method_proxy(operator.contains)
|
2013-03-18 18:09:29 +08:00
|
|
|
|
2009-10-15 22:12:34 +08:00
|
|
|
|
2015-09-12 14:06:25 +08:00
|
|
|
def unpickle_lazyobject(wrapped):
|
|
|
|
"""
|
|
|
|
Used to unpickle lazy objects. Just return its argument, which will be the
|
|
|
|
wrapped object.
|
|
|
|
"""
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
2009-10-15 22:12:34 +08:00
|
|
|
class SimpleLazyObject(LazyObject):
|
|
|
|
"""
|
2014-03-02 22:25:53 +08:00
|
|
|
A lazy object initialized from any function.
|
2009-10-15 22:12:34 +08:00
|
|
|
|
|
|
|
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
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__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
|
|
|
|
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
|
2014-01-29 12:53:30 +08:00
|
|
|
return '<%s: %r>' % (type(self).__name__, repr_attr)
|
2013-03-07 08:00:05 +08:00
|
|
|
|
2016-01-24 10:02:15 +08:00
|
|
|
def __copy__(self):
|
|
|
|
if self._wrapped is empty:
|
|
|
|
# If uninitialized, copy the wrapper. Use SimpleLazyObject, not
|
|
|
|
# self.__class__, because the latter is proxied.
|
|
|
|
return SimpleLazyObject(self._setupfunc)
|
|
|
|
else:
|
|
|
|
# If initialized, return a copy of the wrapped object.
|
|
|
|
return copy.copy(self._wrapped)
|
|
|
|
|
2014-01-29 12:53:30 +08:00
|
|
|
def __deepcopy__(self, memo):
|
|
|
|
if self._wrapped is empty:
|
2014-03-13 06:09:18 +08:00
|
|
|
# We have to use SimpleLazyObject, not self.__class__, because the
|
2014-01-29 12:53:30 +08:00
|
|
|
# latter is proxied.
|
|
|
|
result = SimpleLazyObject(self._setupfunc)
|
|
|
|
memo[id(self)] = result
|
|
|
|
return result
|
|
|
|
return copy.deepcopy(self._wrapped, memo)
|
2011-06-01 21:47:00 +08:00
|
|
|
|
|
|
|
|
2011-09-10 03:22:28 +08:00
|
|
|
def partition(predicate, values):
|
|
|
|
"""
|
2017-01-25 04:32:33 +08:00
|
|
|
Split the values into two sets, based on the return value of the function
|
2011-09-10 03:22:28 +08:00
|
|
|
(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
|