File tree 2 files changed +14
-2
lines changed 2 files changed +14
-2
lines changed Original file line number Diff line number Diff line change @@ -54,13 +54,17 @@ def environmentfilter(f):
54
54
def make_attrgetter (environment , attribute ):
55
55
"""Returns a callable that looks up the given attribute from a
56
56
passed object with the rules of the environment. Dots are allowed
57
- to access attributes of attributes.
57
+ to access attributes of attributes. Integer parts in paths are
58
+ looked up as integers.
58
59
"""
59
- if not isinstance (attribute , string_types ) or '.' not in attribute :
60
+ if not isinstance (attribute , string_types ) \
61
+ or ('.' not in attribute and not attribute .isdigit ()):
60
62
return lambda x : environment .getitem (x , attribute )
61
63
attribute = attribute .split ('.' )
62
64
def attrgetter (item ):
63
65
for part in attribute :
66
+ if part .isdigit ():
67
+ part = int (part )
64
68
item = environment .getitem (item , part )
65
69
return item
66
70
return attrgetter
Original file line number Diff line number Diff line change @@ -256,6 +256,14 @@ def test_sum_attributes_nested(self):
256
256
{'real' : {'value' : 18 }},
257
257
]) == '42'
258
258
259
+ def test_sum_attributes_tuple (self ):
260
+ tmpl = env .from_string ('''{{ values.items()|sum('1') }}''' )
261
+ assert tmpl .render (values = {
262
+ 'foo' : 23 ,
263
+ 'bar' : 1 ,
264
+ 'baz' : 18 ,
265
+ }) == '42'
266
+
259
267
def test_abs (self ):
260
268
tmpl = env .from_string ('''{{ -1|abs }}|{{ 1|abs }}''' )
261
269
assert tmpl .render () == '1|1' , tmpl .render ()
You can’t perform that action at this time.
0 commit comments