A fast and thorough lazy object proxy.
A fast and thorough lazy object proxy.
Note that this is based on wrapt
_'s ObjectProxy with one big change: it calls a function the first time the proxy object is
used, while wrapt.ObjectProxy
just forwards the method calls to the target object.
In other words, you use lazy-object-proxy
when you only have the object way later and you use wrapt.ObjectProxy
when you
want to override few methods (by subclassing) and forward everything else to the target object.
Example::
import lazy_object_proxy
def expensive_func():
from time import sleep
print('starting calculation')
# just as example for a very slow computation
sleep(2)
print('finished calculation')
# return the result of the calculation
return 10
obj = lazy_object_proxy.Proxy(expensive_func)
# function is called only when object is actually used
print(obj) # now expensive_func is called
print(obj) # the result without calling the expensive_func
::
pip install lazy-object-proxy
https://python-lazy-object-proxy.readthedocs.io/
To run all the tests run::
tox
This project is based on some code from wrapt
_ as you can see in the git history.
.. _wrapt: https://github.com/GrahamDumpleton/wrapt
@
).from lazy_object_proxy import Proxy
and the C extension is not available).
Previously the "slots" implementation was used but as it turns out it is slower on Python 3.#62 <https://github.com/ionelmc/python-lazy-object-proxy/pull/62>
_.Removed most of the Python 2 support code and fixed python_requires
to require at least Python 3.6.
Note that 1.7.0 has been yanked because it could not install on Python 2.7. Installing lazy-object-proxy on Python 2.7 should automatically fall back to the 1.6.0 release now.
Switched CI to GitHub Actions, this has a couple consequences:
musllinux
and manylinux2014
variants.Fixed __index__
to fallback to int
if the wrapped object doesn't have an __index__
method.
This prevents situations where code using a proxy would otherwise likely just call int
had the object
not have an __index__
method.
Added support for async special methods (__aiter__
, __anext__
,
__await__
, __aenter__
, __aexit__
).
These are used in the async for
, await` and
async with`` statements.
Note that __await__
returns a wrapper that tries to emulate the crazy
stuff going on in the ceval loop, so there will be a small performance overhead.
Added the __resolved__
property. You can use it to check if the factory has
been called.
__fspath__
.pyproject.toml
to allow users install the sdist with old python/setuptools, as the
setuptools-scm dep will be fetched by pip instead of setuptools.
Fixes #30 <https://github.com/ionelmc/python-lazy-object-proxy/issues/30>
_.-coverage
cflags. No more issues about bogus cext.gcda
files.setup.py
to use setuptools-scm.__mod__
for the slots backend. Contributed by Ran Benita in
#28 <https://github.com/ionelmc/python-lazy-object-proxy/pull/28>
_.#24 <https://github.com/ionelmc/python-lazy-object-proxy/pull/24>
_.sdist
had a broken MANIFEST.in
).cext.Proxy
subclasses.manylinux <https://www.python.org/dev/peps/pep-0513/>
_ wheels.#10 <https://github.com/ionelmc/python-lazy-object-proxy/pull/10>
_.#8 <https://github.com/ionelmc/python-lazy-object-proxy/pull/8>
_.