Skip to content

Commit

Permalink
Merge 31cacc2 into 78f1a38
Browse files Browse the repository at this point in the history
  • Loading branch information
tchebb committed May 23, 2020
2 parents 78f1a38 + 31cacc2 commit 517282e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 9 additions & 4 deletions chevron/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,16 @@ def _get_key(key, scopes):
scope = scope[child]
except (TypeError, AttributeError):
try:
# Try the dictionary (Complex types)
scope = scope.__dict__[child]
# Try namedtuple (which does not have __dict__ in
# Python 3: https://bugs.python.org/issue24931)
scope = scope._asdict()[child]
except (TypeError, AttributeError):
# Try as a list
scope = scope[int(child)]
try:
# Try the dictionary (Complex types)
scope = scope.__dict__[child]
except (TypeError, AttributeError):
# Try as a list
scope = scope[int(child)]

# Return an empty string if falsy, with two exceptions
# 0 should return 0, and False should return False
Expand Down
13 changes: 13 additions & 0 deletions test_spec.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import collections
import unittest
import os
import json
Expand Down Expand Up @@ -435,6 +436,18 @@ def test_indexed(self):

self.assertEqual(result, expected)

def test_namedtuple_data(self):
NT = collections.namedtuple('NT', ['foo', 'bar'])
args = {
'template': '{{foo}} {{bar}}',
'data': NT('hello', 'world')
}

result = chevron.render(**args)
expected = 'hello world'

self.assertEqual(result, expected)


# Run unit tests from command line
if __name__ == "__main__":
Expand Down

0 comments on commit 517282e

Please sign in to comment.