##// END OF EJS Templates
adding get_text_list helper inspired in django's one
Martín Gaitán -
Show More
@@ -724,3 +724,32 b" def columnize(items, separator=' ', displaywidth=80):"
724 fmatrix = [filter(None, x) for x in matrix]
724 fmatrix = [filter(None, x) for x in matrix]
725 sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['columns_width'])])
725 sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['columns_width'])])
726 return '\n'.join(map(sjoin, fmatrix))+'\n'
726 return '\n'.join(map(sjoin, fmatrix))+'\n'
727
728
729 def get_text_list(list_, last_word='and', wrap_item_with=""):
730 """
731 Return a string with a natural enumeration of items
732
733 >>> get_text_list(['a', 'b', 'c', 'd'])
734 'a, b, c and d'
735 >>> get_text_list(['a', 'b', 'c'], 'or')
736 'a, b or c'
737 >>> get_text_list(['a', 'b'], 'or')
738 'a or b'
739 >>> get_text_list(['a'])
740 'a'
741 >>> get_text_list([])
742 ''
743 >>> get_text_list(['a', 'b'], wrap_item_with="`")
744 '`a` and `b`'
745 """
746 if len(list_) == 0:
747 return ''
748 if wrap_item_with:
749 list_ = ['%s%s%s' % (wrap_item_with, item, wrap_item_with) for
750 item in list_]
751 if len(list_) == 1:
752 return list_[0]
753 return '%s %s %s' % (
754 ', '.join(i for i in list_[:-1]),
755 last_word, list_[-1]) No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now