lazy object getitem

This commit is contained in:
Anthony Scopatz 2016-06-27 20:53:10 -04:00
parent 33f9dac753
commit 55ec4d4afb
2 changed files with 18 additions and 3 deletions

11
tests/test_lazyasd.py Normal file
View file

@ -0,0 +1,11 @@
"""Tests lazy and self destruictive objects."""
from xonsh.lazyasd import LazyObject
#
# LazyObject Tests
#
def test_lazyobject_getitem():
lo = LazyObject(lambda: {'x': 1}, {}, 'lo')
assert 1 == lo['x']

View file

@ -19,7 +19,7 @@ class LazyObject(object):
load : function with no arguments
A loader function that performs the actual object construction.
ctx : Mapping
Context to replace the LazyAndSelfDestructiveObject instance in
Context to replace the LazyObject instance in
with the object returned by load().
name : str
Name in the context to give the loaded object. This *should*
@ -56,6 +56,10 @@ class LazyObject(object):
obj = self._lazy_obj()
yield from obj
def __getitem__(self, item):
obj = self._lazy_obj()
return obj[item]
class LazyDict(abc.MutableMapping):
@ -81,7 +85,7 @@ class LazyDict(abc.MutableMapping):
A mapping of loader function that performs the actual value
construction upon acces.
ctx : Mapping
Context to replace the LazyAndSelfDestructiveDict instance in
Context to replace the LazyDict instance in
with the the fully loaded mapping.
name : str
Name in the context to give the loaded mapping. This *should*
@ -145,7 +149,7 @@ class LazyBool(object):
load : function with no arguments
A loader function that performs the actual boolean evaluation.
ctx : Mapping
Context to replace the LazyAndSelfDestructiveDict instance in
Context to replace the LazyBool instance in
with the the fully loaded mapping.
name : str
Name in the context to give the loaded mapping. This *should*