Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pystache_custom
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pystache_custom
Commits
e542a896
Commit
e542a896
authored
Dec 24, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Plain Diff
Merge 'issue_11' into development: closing issue #11 (callables executing incorrectly)
Added a unit test and doctest.
parents
e2305c75
59283fcb
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
0 deletions
+40
-0
pystache/context.py
+28
-0
tests/test_context.py
+12
-0
No files found.
pystache/context.py
View file @
e542a896
...
@@ -125,6 +125,34 @@ class Context(object):
...
@@ -125,6 +125,34 @@ class Context(object):
(3) If there is no attribute with the same name as the key, then
(3) If there is no attribute with the same name as the key, then
the key is considered not found in the item.
the key is considered not found in the item.
*Caution*:
Callables resulting from a call to __getitem__ (as in (1)
above) are handled differently from callables that are merely
attributes (as in (2) above).
The former are returned as-is, while the latter are first called
and that value returned. Here is an example:
>>> def greet():
... return "Hi Bob!"
>>>
>>> class Greeter(object):
... greet = None
>>>
>>> obj = Greeter()
>>> obj.greet = greet
>>> dct = {'greet': greet}
>>>
>>> obj.greet is dct['greet']
True
>>> Context(obj).get('greet')
'Hi Bob!'
>>> Context(dct).get('greet') #doctest: +ELLIPSIS
<function greet at 0x...>
TODO: explain the rationale for this difference in treatment.
"""
"""
for
obj
in
reversed
(
self
.
_stack
):
for
obj
in
reversed
(
self
.
_stack
):
val
=
_get_item
(
obj
,
key
)
val
=
_get_item
(
obj
,
key
)
...
...
tests/test_context.py
View file @
e542a896
...
@@ -69,6 +69,18 @@ class GetItemTestCase(TestCase):
...
@@ -69,6 +69,18 @@ class GetItemTestCase(TestCase):
obj
=
{
"foo"
:
"bar"
}
obj
=
{
"foo"
:
"bar"
}
self
.
assertEquals
(
_get_item
(
obj
,
"foo"
),
"bar"
)
self
.
assertEquals
(
_get_item
(
obj
,
"foo"
),
"bar"
)
def
test_dictionary__callable_not_called
(
self
):
"""
Test that callable values are returned as-is (and in particular not called).
"""
def
foo_callable
(
self
):
return
"bar"
obj
=
{
"foo"
:
foo_callable
}
self
.
assertNotEquals
(
_get_item
(
obj
,
"foo"
),
"bar"
)
self
.
assertTrue
(
_get_item
(
obj
,
"foo"
)
is
foo_callable
)
def
test_dictionary__key_missing
(
self
):
def
test_dictionary__key_missing
(
self
):
"""
"""
Test getting a missing key from a dictionary.
Test getting a missing key from a dictionary.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment