1dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block# Copyright (c) 2009 Google Inc. All rights reserved. 2643ca7872b450ea4efacab6188849e5aac2ba161Steve Block# Copyright (c) 2009 Apple Inc. All rights reserved. 3231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# 4231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# Redistribution and use in source and binary forms, with or without 5231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# modification, are permitted provided that the following conditions are 6231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# met: 7d0825bca7fe65beaee391d30da42e937db621564Steve Block# 8231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# * Redistributions of source code must retain the above copyright 9231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# notice, this list of conditions and the following disclaimer. 10231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# * Redistributions in binary form must reproduce the above 11231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# copyright notice, this list of conditions and the following disclaimer 12231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# in the documentation and/or other materials provided with the 13231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# distribution. 14231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# * Neither the name of Google Inc. nor the names of its 15231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# contributors may be used to endorse or promote products derived from 16231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# this software without specific prior written permission. 17d0825bca7fe65beaee391d30da42e937db621564Steve Block# 18231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 30231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Blockimport re 31231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 32d0825bca7fe65beaee391d30da42e937db621564Steve Block 33643ca7872b450ea4efacab6188849e5aac2ba161Steve Blockdef plural(noun): 34d0825bca7fe65beaee391d30da42e937db621564Steve Block # This is a dumb plural() implementation that is just enough for our uses. 35643ca7872b450ea4efacab6188849e5aac2ba161Steve Block if re.search("h$", noun): 36643ca7872b450ea4efacab6188849e5aac2ba161Steve Block return noun + "es" 37643ca7872b450ea4efacab6188849e5aac2ba161Steve Block else: 38643ca7872b450ea4efacab6188849e5aac2ba161Steve Block return noun + "s" 39231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block 40d0825bca7fe65beaee391d30da42e937db621564Steve Block 41643ca7872b450ea4efacab6188849e5aac2ba161Steve Blockdef pluralize(noun, count): 42643ca7872b450ea4efacab6188849e5aac2ba161Steve Block if count != 1: 43643ca7872b450ea4efacab6188849e5aac2ba161Steve Block noun = plural(noun) 44643ca7872b450ea4efacab6188849e5aac2ba161Steve Block return "%d %s" % (count, noun) 458a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block 468a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block 47dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Blockdef join_with_separators(list_of_strings, separator=', ', only_two_separator=" and ", last_separator=', and '): 488a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block if not list_of_strings: 498a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block return "" 508a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block if len(list_of_strings) == 1: 518a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block return list_of_strings[0] 52dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block if len(list_of_strings) == 2: 53dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block return only_two_separator.join(list_of_strings) 548a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block return "%s%s%s" % (separator.join(list_of_strings[:-1]), last_separator, list_of_strings[-1]) 55