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
#!/usr/bin/python
# -*- coding: utf-8 -*-
+import collections
import unittest
import os
import json
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__":