Commit e00931d4 by Chris Jerdonek

Finished fleshing out the initial docstrings for the context module.

parent c8defb3f
...@@ -106,14 +106,24 @@ class Context(object): ...@@ -106,14 +106,24 @@ class Context(object):
""" """
Query the stack for the given key, and return the resulting value. Query the stack for the given key, and return the resulting value.
Querying for a key queries objects in the stack in order from Querying for a key queries items in the stack in order from last-
last-added objects to first (last in, first out). added objects to first (last in, first out). The value returned
is the value of the key for the first item for which the item
Querying an item in the stack is done as follows: contains the key. If the key is not found in any item in the
stack, then this method returns the default value. The default
(1) The __getitem__ method is attempted first, if it exists. value defaults to None.
This method returns None if no item in the stack contains the key. Querying an item in the stack is done in the following way:
(1) If the item defines __getitem__() and the item contains the
key (i.e. __contains__() returns True), then the corresponding
value is returned.
(2) Otherwise, the method looks for an attribute with the same
name as the key. If such an attribute exists, the value of
this attribute is returned. If the attribute is callable,
however, the attribute is first called with no arguments.
(3) If there is no attribute with the same name as the key, then
the key is considered not found in the item.
""" """
for obj in reversed(self._stack): for obj in reversed(self._stack):
......
...@@ -105,6 +105,14 @@ class GetItemTestCase(TestCase): ...@@ -105,6 +105,14 @@ class GetItemTestCase(TestCase):
obj = SimpleObject() obj = SimpleObject()
self.assertNotFound(obj, "missing") self.assertNotFound(obj, "missing")
def test_object__attribute_is_callable(self):
"""
Test getting a callable attribute from an object.
"""
obj = SimpleObject()
self.assertEquals(_get_item(obj, "foo_callable"), "called...")
### Case: obj implements __getitem__() (i.e. a "mapping object"). ### Case: obj implements __getitem__() (i.e. a "mapping object").
def test_mapping__key_present(self): def test_mapping__key_present(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment