summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Maw <richard.maw@codethink.co.uk>2015-04-29 20:36:09 +0000
committerBaserock Gerrit <gerrit@baserock.org>2015-05-11 10:47:58 +0000
commit6aec7d787094616b820fa15d2f24d3d320d2ba41 (patch)
tree2f502bcfacde3e37855ee5342b69e0f5ba97bd15
parent7fe5c488e81163691847b54c6df15ea0f5f5143b (diff)
downloadmorph-6aec7d787094616b820fa15d2f24d3d320d2ba41.tar.gz
morphlib.util: add word_join_list
This is useful for representing lists of items in status or exception messages. Change-Id: I530eecdc311ac77fca9922dab063f550ea810c06
-rw-r--r--morphlib/util.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/morphlib/util.py b/morphlib/util.py
index 70808cac..ab774dd5 100644
--- a/morphlib/util.py
+++ b/morphlib/util.py
@@ -692,3 +692,13 @@ def write_from_dict(filepath, d, validate=lambda x, y: True): #pragma: no cover
os.fchown(f.fileno(), 0, 0)
os.fchmod(f.fileno(), 0644)
+
+
+def word_join_list(l): # pragma: no cover
+ if len(l) == 0:
+ return ''
+ elif len(l) == 1:
+ return l[0]
+ else:
+ commasep = ', '.join(l[:-1])
+ return ' and '.join((commasep, l[-1]))