diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2015-04-29 20:36:09 +0000 |
---|---|---|
committer | Baserock Gerrit <gerrit@baserock.org> | 2015-05-11 10:47:58 +0000 |
commit | 6aec7d787094616b820fa15d2f24d3d320d2ba41 (patch) | |
tree | 2f502bcfacde3e37855ee5342b69e0f5ba97bd15 /morphlib/util.py | |
parent | 7fe5c488e81163691847b54c6df15ea0f5f5143b (diff) | |
download | morph-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
Diffstat (limited to 'morphlib/util.py')
-rw-r--r-- | morphlib/util.py | 10 |
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])) |