Skip to content

Commit

Permalink
Fix iteration over list of tuples.
Browse files Browse the repository at this point in the history
  • Loading branch information
sallner committed Oct 5, 2018
1 parent cb2909c commit cc27036
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -22,6 +22,7 @@ Changelog

- Raise proper error if prefix is not simple.
- Fix complex multisort in Python 3.
- Fix iteration over list of tuples in Python 3.


3.0b4 (2018-07-12)
Expand Down
2 changes: 2 additions & 0 deletions src/DocumentTemplate/DT_In.py
Expand Up @@ -341,6 +341,7 @@
from DocumentTemplate.DT_Util import ValidationError, Eval
from DocumentTemplate.DT_Util import simple_name, add_with_prefix
from DocumentTemplate.DT_InSV import sequence_variables, opt
from zope.sequencesort.ssort import _Smallest

if sys.version_info > (3, 0):
unicode = str
Expand Down Expand Up @@ -831,6 +832,7 @@ def sort_sequence(self, sequence, md):
try:
k = k()
except Exception:
k = _Smallest
pass

s.append((k, client))
Expand Down
54 changes: 53 additions & 1 deletion src/DocumentTemplate/tests/test_DT_In.py
Expand Up @@ -10,9 +10,14 @@ class DummySection(object):
class Dummy(object):
"""Dummy with attribute"""

def __init__(self, name, number=0):
def __init__(self, name, number=0, _callable=0):
self.name = name
self.number = number
self._callable = _callable

@property
def maybe_callable(self):
return self._callable


class TestIn(unittest.TestCase):
Expand Down Expand Up @@ -306,6 +311,53 @@ def test_DT_In__InClass__renderwob__07(self):
'Item 4: alberta , 2')
self.assertEqual(res, expected)

def test_DT_In__InClass__renderwob__08(self):
"""It can iterate over list of tuples."""
seq = [('alberta', 3), ('ylberta', 1), ('barnie', 2)]
html = self.doc_class(
'<dtml-in seq>'
'Item <dtml-var sequence-number>: '
'<dtml-var sequence-item>'
'</dtml-in>')
res = html(seq=seq)
expected = (
'Item 1: 3'
'Item 2: 1'
'Item 3: 2')
self.assertEqual(res, expected)
# also sorted
html = self.doc_class(
'<dtml-in seq sort>'
'Item <dtml-var sequence-number>: '
'<dtml-var sequence-item>'
'</dtml-in>')
res = html(seq=seq)
expected = (
'Item 1: 3'
'Item 2: 2'
'Item 3: 1')
self.assertEqual(res, expected)

def test_DT_In__InClass__renderwob__09(self):
"""It can also contain callables as values."""
# It catches errors and treats those as smallest
seq = [
Dummy('alberta', _callable=lambda: 3),
Dummy('ylberta', _callable=lambda: 0),
Dummy('barnie', _callable=lambda: [][2]),
]
html = self.doc_class(
'<dtml-in seq sort=maybe_callable/cmp>'
'Item <dtml-var sequence-number>: '
'<dtml-var sequence-var-name>'
'</dtml-in>')
res = html(seq=seq)
expected = (
'Item 1: barnie'
'Item 2: ylberta'
'Item 3: alberta')
self.assertEqual(res, expected)

def test_DT_In__InClass__renderwb__01(self):
"""It does not allow strings as sequence in batch mode."""
html = self.doc_class(
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -30,7 +30,7 @@ commands =
coverage combine
coverage html
coverage xml
coverage report --fail-under=68
coverage report --fail-under=70

[testenv:flake8]
basepython = python3.6
Expand Down

0 comments on commit cc27036

Please sign in to comment.