Update iter_to_string with handling for list size of 2

This commit is contained in:
fariparedes 2021-06-18 14:07:59 -04:00
parent 0bd7860974
commit e6733ba7ff
3 changed files with 13 additions and 9 deletions

View file

@ -66,6 +66,7 @@ Up requirements to Django 3.2+
into a more consistent structure for overriding. Expanded webpage documentation considerably.
- REST API list-view was shortened (#2401). New CSS/HTML. Add ReDoc for API autodoc page.
- Update and fix dummyrunner with cleaner code and setup.
- Made `iter_to_str` format prettier strings
### Evennia 0.9.5 (2019-2020)

View file

@ -72,10 +72,11 @@ class TestListToString(TestCase):
def test_list_to_string(self):
self.assertEqual("1, 2, 3", utils.list_to_string([1, 2, 3], endsep=""))
self.assertEqual('"1", "2", "3"', utils.list_to_string([1, 2, 3], endsep="", addquote=True))
self.assertEqual("1, 2 and 3", utils.list_to_string([1, 2, 3]))
self.assertEqual("1, 2, and 3", utils.list_to_string([1, 2, 3]))
self.assertEqual(
'"1", "2" and "3"', utils.list_to_string([1, 2, 3], endsep="and", addquote=True)
'"1", "2", and "3"', utils.list_to_string([1, 2, 3], endsep="and", addquote=True)
)
self.assertEqual("1 and 2", utils.list_to_string([1, 2]))
class TestMLen(TestCase):

View file

@ -381,15 +381,13 @@ def iter_to_str(initer, endsep="and", addquote=False):
>>> list_to_string([1,2,3], endsep='')
'1, 2, 3'
>>> list_to_string([1,2,3], ensdep='and')
'1, 2 and 3'
'1, 2, and 3'
>>> list_to_string([1,2,3], endsep='and', addquote=True)
'"1", "2" and "3"'
'"1", "2", and "3"'
```
"""
if not endsep:
endsep = ","
else:
if endsep:
endsep = " " + endsep
if not initer:
return ""
@ -397,11 +395,15 @@ def iter_to_str(initer, endsep="and", addquote=False):
if addquote:
if len(initer) == 1:
return '"%s"' % initer[0]
return ", ".join('"%s"' % v for v in initer[:-1]) + "%s %s" % (endsep, '"%s"' % initer[-1])
elif len(initer) == 2:
return '"%s"' % ('"%s "' % endsep).join(str(v) for v in initer)
return ", ".join('"%s"' % v for v in initer[:-1]) + ",%s %s" % (endsep, '"%s"' % initer[-1])
else:
if len(initer) == 1:
return str(initer[0])
return ", ".join(str(v) for v in initer[:-1]) + "%s %s" % (endsep, initer[-1])
elif len(initer) == 2:
return ("%s " % endsep).join(str(v) for v in initer)
return ", ".join(str(v) for v in initer[:-1]) + ",%s %s" % (endsep, initer[-1])
# legacy aliases