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
60f6802d
Commit
60f6802d
authored
Dec 16, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More improvement tweaks.
parent
afb7f98d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
14 deletions
+25
-14
pystache/context.py
+11
-10
tests/test_context.py
+14
-4
No files found.
pystache/context.py
View file @
60f6802d
...
...
@@ -57,12 +57,13 @@ def _get_item(obj, key):
class
Context
(
object
):
"""
Provides dictionary-like access to a stack of zero or more
object
s.
Provides dictionary-like access to a stack of zero or more
item
s.
Instances of this class are meant to represent the rendering context
when rendering mustache templates in accordance with mustache(5).
Querying the stack for the value of a key queries the objects in the
Instances encapsulate a private stack of objects and dictionaries.
Querying the stack for the value of a key queries the items in the
stack in order from last-added objects to first (last in, first out).
See the docstrings of the methods of this class for more information.
...
...
@@ -71,16 +72,16 @@ class Context(object):
# We reserve keyword arguments for future options (e.g. a "strict=True"
# option for enabling a strict mode).
def
__init__
(
self
,
*
obj
):
def
__init__
(
self
,
*
items
):
"""
Construct an instance, and initialize the stack.
Construct an instance, and initialize the
internal
stack.
The
variable argument list *obj are the objects with which to
populate the initial stack. Objects in the argument list are added
to the
stack in order so that, in particular, items at the end of
The
*items arguments are the items with which to populate the
initial stack. Items in the argument list are added to the
stack in order so that, in particular, items at the end of
the argument list are queried first when querying the stack.
The
objects should be dictionary-like in the following sense
:
The
items should satisfy the following
:
(1) They can be dictionaries or objects.
(2) If they implement __getitem__, a KeyError should be raised
...
...
@@ -100,7 +101,7 @@ class Context(object):
also need to take __iter__() into account]....
"""
self
.
stack
=
list
(
obj
)
self
.
_stack
=
list
(
items
)
def
get
(
self
,
key
,
default
=
None
):
"""
...
...
@@ -116,7 +117,7 @@ class Context(object):
This method returns None if no item in the stack contains the key.
"""
for
obj
in
reversed
(
self
.
stack
):
for
obj
in
reversed
(
self
.
_
stack
):
val
=
_get_item
(
obj
,
key
)
if
val
is
_NOT_FOUND
:
continue
...
...
tests/test_context.py
View file @
60f6802d
...
...
@@ -77,6 +77,16 @@ class GetItemTestCase(TestCase):
obj
=
{}
self
.
assertNotFound
(
obj
,
"missing"
)
def
test_dictionary__attributes_not_checked
(
self
):
"""
Test that dictionary attributes are not checked.
"""
obj
=
{}
attr_name
=
"keys"
self
.
assertEquals
(
getattr
(
obj
,
attr_name
)(),
[])
self
.
assertNotFound
(
obj
,
attr_name
)
### Case: obj does not implement __getitem__().
def
test_object__attribute_present
(
self
):
...
...
@@ -119,10 +129,10 @@ class GetItemTestCase(TestCase):
"""
obj
=
MappingObject
()
self
.
assertEquals
(
obj
.
fuzz
,
"buzz"
)
# The presence of __getitem__ causes obj.fuzz not to be checked,
#
as desir
ed.
self
.
assertNotFound
(
obj
,
"fuzz"
)
key
=
"fuzz"
self
.
assertEquals
(
getattr
(
obj
,
key
),
"buzz"
)
#
As desired, __getitem__()'s presence causes obj.fuzz not to be check
ed.
self
.
assertNotFound
(
obj
,
key
)
def
test_mapping_object__not_implementing_contains
(
self
):
"""
...
...
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