From 003411ebcd23b9707bb9f2fe24a4356add4e8060 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 18 Apr 2011 12:18:31 -0400 Subject: [PATCH] eng.plural! --- clint/eng.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 clint/eng.py diff --git a/clint/eng.py b/clint/eng.py new file mode 100644 index 0000000..1a4bff2 --- /dev/null +++ b/clint/eng.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- + +""" +clint.eng +~~~~~~~~~ + +This module provides English language string helpers. + +""" + +MORON_MODE = False +COMMA = ',' +CONJUNCTION = 'and' +SPACE = ' ' + + +def join(l, conj=CONJUNCTION, im_a_moron=MORON_MODE, seperator=COMMA): + """Joins lists of words. Oxford comma and all.""" + + collector = [] + left = len(l) + seperator = seperator + SPACE + conj = conj + SPACE + + for _l in l[:]: + + left += -1 + + collector.append(_l) + if left > 1: + collector.append(seperator) + elif left == 1: + if len(l) == 2 or im_a_moron: + print + collector.append(SPACE) + else: + collector.append(seperator) + + collector.append(conj) + elif left == 0: + pass + + return unicode(''.join(collector))