From e6733ba7ff2594bd813528b8eba694484317ca14 Mon Sep 17 00:00:00 2001 From: fariparedes Date: Fri, 18 Jun 2021 14:07:59 -0400 Subject: [PATCH] Update iter_to_string with handling for list size of 2 --- CHANGELOG.md | 1 + evennia/utils/tests/test_utils.py | 5 +++-- evennia/utils/utils.py | 16 +++++++++------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e45bc07133..3408f0d9e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/evennia/utils/tests/test_utils.py b/evennia/utils/tests/test_utils.py index 2c4011c73b..9c18e1ccd8 100644 --- a/evennia/utils/tests/test_utils.py +++ b/evennia/utils/tests/test_utils.py @@ -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): diff --git a/evennia/utils/utils.py b/evennia/utils/utils.py index ee8274efe5..6e3d274080 100644 --- a/evennia/utils/utils.py +++ b/evennia/utils/utils.py @@ -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