text.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. # encoding: utf-8
  2. """
  3. Utilities for working with strings and text.
  4. Inheritance diagram:
  5. .. inheritance-diagram:: IPython.utils.text
  6. :parts: 3
  7. """
  8. from __future__ import absolute_import
  9. import os
  10. import re
  11. import sys
  12. import textwrap
  13. from string import Formatter
  14. try:
  15. from pathlib import Path
  16. except ImportError:
  17. # Python 2 backport
  18. from pathlib2 import Path
  19. from IPython.testing.skipdoctest import skip_doctest_py3, skip_doctest
  20. from IPython.utils import py3compat
  21. # datetime.strftime date format for ipython
  22. if sys.platform == 'win32':
  23. date_format = "%B %d, %Y"
  24. else:
  25. date_format = "%B %-d, %Y"
  26. class LSString(str):
  27. """String derivative with a special access attributes.
  28. These are normal strings, but with the special attributes:
  29. .l (or .list) : value as list (split on newlines).
  30. .n (or .nlstr): original value (the string itself).
  31. .s (or .spstr): value as whitespace-separated string.
  32. .p (or .paths): list of path objects (requires path.py package)
  33. Any values which require transformations are computed only once and
  34. cached.
  35. Such strings are very useful to efficiently interact with the shell, which
  36. typically only understands whitespace-separated options for commands."""
  37. def get_list(self):
  38. try:
  39. return self.__list
  40. except AttributeError:
  41. self.__list = self.split('\n')
  42. return self.__list
  43. l = list = property(get_list)
  44. def get_spstr(self):
  45. try:
  46. return self.__spstr
  47. except AttributeError:
  48. self.__spstr = self.replace('\n',' ')
  49. return self.__spstr
  50. s = spstr = property(get_spstr)
  51. def get_nlstr(self):
  52. return self
  53. n = nlstr = property(get_nlstr)
  54. def get_paths(self):
  55. try:
  56. return self.__paths
  57. except AttributeError:
  58. self.__paths = [Path(p) for p in self.split('\n') if os.path.exists(p)]
  59. return self.__paths
  60. p = paths = property(get_paths)
  61. # FIXME: We need to reimplement type specific displayhook and then add this
  62. # back as a custom printer. This should also be moved outside utils into the
  63. # core.
  64. # def print_lsstring(arg):
  65. # """ Prettier (non-repr-like) and more informative printer for LSString """
  66. # print "LSString (.p, .n, .l, .s available). Value:"
  67. # print arg
  68. #
  69. #
  70. # print_lsstring = result_display.when_type(LSString)(print_lsstring)
  71. class SList(list):
  72. """List derivative with a special access attributes.
  73. These are normal lists, but with the special attributes:
  74. * .l (or .list) : value as list (the list itself).
  75. * .n (or .nlstr): value as a string, joined on newlines.
  76. * .s (or .spstr): value as a string, joined on spaces.
  77. * .p (or .paths): list of path objects (requires path.py package)
  78. Any values which require transformations are computed only once and
  79. cached."""
  80. def get_list(self):
  81. return self
  82. l = list = property(get_list)
  83. def get_spstr(self):
  84. try:
  85. return self.__spstr
  86. except AttributeError:
  87. self.__spstr = ' '.join(self)
  88. return self.__spstr
  89. s = spstr = property(get_spstr)
  90. def get_nlstr(self):
  91. try:
  92. return self.__nlstr
  93. except AttributeError:
  94. self.__nlstr = '\n'.join(self)
  95. return self.__nlstr
  96. n = nlstr = property(get_nlstr)
  97. def get_paths(self):
  98. try:
  99. return self.__paths
  100. except AttributeError:
  101. self.__paths = [Path(p) for p in self if os.path.exists(p)]
  102. return self.__paths
  103. p = paths = property(get_paths)
  104. def grep(self, pattern, prune = False, field = None):
  105. """ Return all strings matching 'pattern' (a regex or callable)
  106. This is case-insensitive. If prune is true, return all items
  107. NOT matching the pattern.
  108. If field is specified, the match must occur in the specified
  109. whitespace-separated field.
  110. Examples::
  111. a.grep( lambda x: x.startswith('C') )
  112. a.grep('Cha.*log', prune=1)
  113. a.grep('chm', field=-1)
  114. """
  115. def match_target(s):
  116. if field is None:
  117. return s
  118. parts = s.split()
  119. try:
  120. tgt = parts[field]
  121. return tgt
  122. except IndexError:
  123. return ""
  124. if isinstance(pattern, py3compat.string_types):
  125. pred = lambda x : re.search(pattern, x, re.IGNORECASE)
  126. else:
  127. pred = pattern
  128. if not prune:
  129. return SList([el for el in self if pred(match_target(el))])
  130. else:
  131. return SList([el for el in self if not pred(match_target(el))])
  132. def fields(self, *fields):
  133. """ Collect whitespace-separated fields from string list
  134. Allows quick awk-like usage of string lists.
  135. Example data (in var a, created by 'a = !ls -l')::
  136. -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog
  137. drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython
  138. * ``a.fields(0)`` is ``['-rwxrwxrwx', 'drwxrwxrwx+']``
  139. * ``a.fields(1,0)`` is ``['1 -rwxrwxrwx', '6 drwxrwxrwx+']``
  140. (note the joining by space).
  141. * ``a.fields(-1)`` is ``['ChangeLog', 'IPython']``
  142. IndexErrors are ignored.
  143. Without args, fields() just split()'s the strings.
  144. """
  145. if len(fields) == 0:
  146. return [el.split() for el in self]
  147. res = SList()
  148. for el in [f.split() for f in self]:
  149. lineparts = []
  150. for fd in fields:
  151. try:
  152. lineparts.append(el[fd])
  153. except IndexError:
  154. pass
  155. if lineparts:
  156. res.append(" ".join(lineparts))
  157. return res
  158. def sort(self,field= None, nums = False):
  159. """ sort by specified fields (see fields())
  160. Example::
  161. a.sort(1, nums = True)
  162. Sorts a by second field, in numerical order (so that 21 > 3)
  163. """
  164. #decorate, sort, undecorate
  165. if field is not None:
  166. dsu = [[SList([line]).fields(field), line] for line in self]
  167. else:
  168. dsu = [[line, line] for line in self]
  169. if nums:
  170. for i in range(len(dsu)):
  171. numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()])
  172. try:
  173. n = int(numstr)
  174. except ValueError:
  175. n = 0
  176. dsu[i][0] = n
  177. dsu.sort()
  178. return SList([t[1] for t in dsu])
  179. # FIXME: We need to reimplement type specific displayhook and then add this
  180. # back as a custom printer. This should also be moved outside utils into the
  181. # core.
  182. # def print_slist(arg):
  183. # """ Prettier (non-repr-like) and more informative printer for SList """
  184. # print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):"
  185. # if hasattr(arg, 'hideonce') and arg.hideonce:
  186. # arg.hideonce = False
  187. # return
  188. #
  189. # nlprint(arg) # This was a nested list printer, now removed.
  190. #
  191. # print_slist = result_display.when_type(SList)(print_slist)
  192. def indent(instr,nspaces=4, ntabs=0, flatten=False):
  193. """Indent a string a given number of spaces or tabstops.
  194. indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
  195. Parameters
  196. ----------
  197. instr : basestring
  198. The string to be indented.
  199. nspaces : int (default: 4)
  200. The number of spaces to be indented.
  201. ntabs : int (default: 0)
  202. The number of tabs to be indented.
  203. flatten : bool (default: False)
  204. Whether to scrub existing indentation. If True, all lines will be
  205. aligned to the same indentation. If False, existing indentation will
  206. be strictly increased.
  207. Returns
  208. -------
  209. str|unicode : string indented by ntabs and nspaces.
  210. """
  211. if instr is None:
  212. return
  213. ind = '\t'*ntabs+' '*nspaces
  214. if flatten:
  215. pat = re.compile(r'^\s*', re.MULTILINE)
  216. else:
  217. pat = re.compile(r'^', re.MULTILINE)
  218. outstr = re.sub(pat, ind, instr)
  219. if outstr.endswith(os.linesep+ind):
  220. return outstr[:-len(ind)]
  221. else:
  222. return outstr
  223. def list_strings(arg):
  224. """Always return a list of strings, given a string or list of strings
  225. as input.
  226. Examples
  227. --------
  228. ::
  229. In [7]: list_strings('A single string')
  230. Out[7]: ['A single string']
  231. In [8]: list_strings(['A single string in a list'])
  232. Out[8]: ['A single string in a list']
  233. In [9]: list_strings(['A','list','of','strings'])
  234. Out[9]: ['A', 'list', 'of', 'strings']
  235. """
  236. if isinstance(arg, py3compat.string_types): return [arg]
  237. else: return arg
  238. def marquee(txt='',width=78,mark='*'):
  239. """Return the input string centered in a 'marquee'.
  240. Examples
  241. --------
  242. ::
  243. In [16]: marquee('A test',40)
  244. Out[16]: '**************** A test ****************'
  245. In [17]: marquee('A test',40,'-')
  246. Out[17]: '---------------- A test ----------------'
  247. In [18]: marquee('A test',40,' ')
  248. Out[18]: ' A test '
  249. """
  250. if not txt:
  251. return (mark*width)[:width]
  252. nmark = (width-len(txt)-2)//len(mark)//2
  253. if nmark < 0: nmark =0
  254. marks = mark*nmark
  255. return '%s %s %s' % (marks,txt,marks)
  256. ini_spaces_re = re.compile(r'^(\s+)')
  257. def num_ini_spaces(strng):
  258. """Return the number of initial spaces in a string"""
  259. ini_spaces = ini_spaces_re.match(strng)
  260. if ini_spaces:
  261. return ini_spaces.end()
  262. else:
  263. return 0
  264. def format_screen(strng):
  265. """Format a string for screen printing.
  266. This removes some latex-type format codes."""
  267. # Paragraph continue
  268. par_re = re.compile(r'\\$',re.MULTILINE)
  269. strng = par_re.sub('',strng)
  270. return strng
  271. def dedent(text):
  272. """Equivalent of textwrap.dedent that ignores unindented first line.
  273. This means it will still dedent strings like:
  274. '''foo
  275. is a bar
  276. '''
  277. For use in wrap_paragraphs.
  278. """
  279. if text.startswith('\n'):
  280. # text starts with blank line, don't ignore the first line
  281. return textwrap.dedent(text)
  282. # split first line
  283. splits = text.split('\n',1)
  284. if len(splits) == 1:
  285. # only one line
  286. return textwrap.dedent(text)
  287. first, rest = splits
  288. # dedent everything but the first line
  289. rest = textwrap.dedent(rest)
  290. return '\n'.join([first, rest])
  291. def wrap_paragraphs(text, ncols=80):
  292. """Wrap multiple paragraphs to fit a specified width.
  293. This is equivalent to textwrap.wrap, but with support for multiple
  294. paragraphs, as separated by empty lines.
  295. Returns
  296. -------
  297. list of complete paragraphs, wrapped to fill `ncols` columns.
  298. """
  299. paragraph_re = re.compile(r'\n(\s*\n)+', re.MULTILINE)
  300. text = dedent(text).strip()
  301. paragraphs = paragraph_re.split(text)[::2] # every other entry is space
  302. out_ps = []
  303. indent_re = re.compile(r'\n\s+', re.MULTILINE)
  304. for p in paragraphs:
  305. # presume indentation that survives dedent is meaningful formatting,
  306. # so don't fill unless text is flush.
  307. if indent_re.search(p) is None:
  308. # wrap paragraph
  309. p = textwrap.fill(p, ncols)
  310. out_ps.append(p)
  311. return out_ps
  312. def long_substr(data):
  313. """Return the longest common substring in a list of strings.
  314. Credit: http://stackoverflow.com/questions/2892931/longest-common-substring-from-more-than-two-strings-python
  315. """
  316. substr = ''
  317. if len(data) > 1 and len(data[0]) > 0:
  318. for i in range(len(data[0])):
  319. for j in range(len(data[0])-i+1):
  320. if j > len(substr) and all(data[0][i:i+j] in x for x in data):
  321. substr = data[0][i:i+j]
  322. elif len(data) == 1:
  323. substr = data[0]
  324. return substr
  325. def strip_email_quotes(text):
  326. """Strip leading email quotation characters ('>').
  327. Removes any combination of leading '>' interspersed with whitespace that
  328. appears *identically* in all lines of the input text.
  329. Parameters
  330. ----------
  331. text : str
  332. Examples
  333. --------
  334. Simple uses::
  335. In [2]: strip_email_quotes('> > text')
  336. Out[2]: 'text'
  337. In [3]: strip_email_quotes('> > text\\n> > more')
  338. Out[3]: 'text\\nmore'
  339. Note how only the common prefix that appears in all lines is stripped::
  340. In [4]: strip_email_quotes('> > text\\n> > more\\n> more...')
  341. Out[4]: '> text\\n> more\\nmore...'
  342. So if any line has no quote marks ('>') , then none are stripped from any
  343. of them ::
  344. In [5]: strip_email_quotes('> > text\\n> > more\\nlast different')
  345. Out[5]: '> > text\\n> > more\\nlast different'
  346. """
  347. lines = text.splitlines()
  348. matches = set()
  349. for line in lines:
  350. prefix = re.match(r'^(\s*>[ >]*)', line)
  351. if prefix:
  352. matches.add(prefix.group(1))
  353. else:
  354. break
  355. else:
  356. prefix = long_substr(list(matches))
  357. if prefix:
  358. strip = len(prefix)
  359. text = '\n'.join([ ln[strip:] for ln in lines])
  360. return text
  361. def strip_ansi(source):
  362. """
  363. Remove ansi escape codes from text.
  364. Parameters
  365. ----------
  366. source : str
  367. Source to remove the ansi from
  368. """
  369. return re.sub(r'\033\[(\d|;)+?m', '', source)
  370. class EvalFormatter(Formatter):
  371. """A String Formatter that allows evaluation of simple expressions.
  372. Note that this version interprets a : as specifying a format string (as per
  373. standard string formatting), so if slicing is required, you must explicitly
  374. create a slice.
  375. This is to be used in templating cases, such as the parallel batch
  376. script templates, where simple arithmetic on arguments is useful.
  377. Examples
  378. --------
  379. ::
  380. In [1]: f = EvalFormatter()
  381. In [2]: f.format('{n//4}', n=8)
  382. Out[2]: '2'
  383. In [3]: f.format("{greeting[slice(2,4)]}", greeting="Hello")
  384. Out[3]: 'll'
  385. """
  386. def get_field(self, name, args, kwargs):
  387. v = eval(name, kwargs)
  388. return v, name
  389. #XXX: As of Python 3.4, the format string parsing no longer splits on a colon
  390. # inside [], so EvalFormatter can handle slicing. Once we only support 3.4 and
  391. # above, it should be possible to remove FullEvalFormatter.
  392. @skip_doctest_py3
  393. class FullEvalFormatter(Formatter):
  394. """A String Formatter that allows evaluation of simple expressions.
  395. Any time a format key is not found in the kwargs,
  396. it will be tried as an expression in the kwargs namespace.
  397. Note that this version allows slicing using [1:2], so you cannot specify
  398. a format string. Use :class:`EvalFormatter` to permit format strings.
  399. Examples
  400. --------
  401. ::
  402. In [1]: f = FullEvalFormatter()
  403. In [2]: f.format('{n//4}', n=8)
  404. Out[2]: u'2'
  405. In [3]: f.format('{list(range(5))[2:4]}')
  406. Out[3]: u'[2, 3]'
  407. In [4]: f.format('{3*2}')
  408. Out[4]: u'6'
  409. """
  410. # copied from Formatter._vformat with minor changes to allow eval
  411. # and replace the format_spec code with slicing
  412. def vformat(self, format_string, args, kwargs):
  413. result = []
  414. for literal_text, field_name, format_spec, conversion in \
  415. self.parse(format_string):
  416. # output the literal text
  417. if literal_text:
  418. result.append(literal_text)
  419. # if there's a field, output it
  420. if field_name is not None:
  421. # this is some markup, find the object and do
  422. # the formatting
  423. if format_spec:
  424. # override format spec, to allow slicing:
  425. field_name = ':'.join([field_name, format_spec])
  426. # eval the contents of the field for the object
  427. # to be formatted
  428. obj = eval(field_name, kwargs)
  429. # do any conversion on the resulting object
  430. obj = self.convert_field(obj, conversion)
  431. # format the object and append to the result
  432. result.append(self.format_field(obj, ''))
  433. return u''.join(py3compat.cast_unicode(s) for s in result)
  434. @skip_doctest_py3
  435. class DollarFormatter(FullEvalFormatter):
  436. """Formatter allowing Itpl style $foo replacement, for names and attribute
  437. access only. Standard {foo} replacement also works, and allows full
  438. evaluation of its arguments.
  439. Examples
  440. --------
  441. ::
  442. In [1]: f = DollarFormatter()
  443. In [2]: f.format('{n//4}', n=8)
  444. Out[2]: u'2'
  445. In [3]: f.format('23 * 76 is $result', result=23*76)
  446. Out[3]: u'23 * 76 is 1748'
  447. In [4]: f.format('$a or {b}', a=1, b=2)
  448. Out[4]: u'1 or 2'
  449. """
  450. _dollar_pattern = re.compile("(.*?)\$(\$?[\w\.]+)")
  451. def parse(self, fmt_string):
  452. for literal_txt, field_name, format_spec, conversion \
  453. in Formatter.parse(self, fmt_string):
  454. # Find $foo patterns in the literal text.
  455. continue_from = 0
  456. txt = ""
  457. for m in self._dollar_pattern.finditer(literal_txt):
  458. new_txt, new_field = m.group(1,2)
  459. # $$foo --> $foo
  460. if new_field.startswith("$"):
  461. txt += new_txt + new_field
  462. else:
  463. yield (txt + new_txt, new_field, "", None)
  464. txt = ""
  465. continue_from = m.end()
  466. # Re-yield the {foo} style pattern
  467. yield (txt + literal_txt[continue_from:], field_name, format_spec, conversion)
  468. #-----------------------------------------------------------------------------
  469. # Utils to columnize a list of string
  470. #-----------------------------------------------------------------------------
  471. def _col_chunks(l, max_rows, row_first=False):
  472. """Yield successive max_rows-sized column chunks from l."""
  473. if row_first:
  474. ncols = (len(l) // max_rows) + (len(l) % max_rows > 0)
  475. for i in py3compat.xrange(ncols):
  476. yield [l[j] for j in py3compat.xrange(i, len(l), ncols)]
  477. else:
  478. for i in py3compat.xrange(0, len(l), max_rows):
  479. yield l[i:(i + max_rows)]
  480. def _find_optimal(rlist, row_first=False, separator_size=2, displaywidth=80):
  481. """Calculate optimal info to columnize a list of string"""
  482. for max_rows in range(1, len(rlist) + 1):
  483. col_widths = list(map(max, _col_chunks(rlist, max_rows, row_first)))
  484. sumlength = sum(col_widths)
  485. ncols = len(col_widths)
  486. if sumlength + separator_size * (ncols - 1) <= displaywidth:
  487. break
  488. return {'num_columns': ncols,
  489. 'optimal_separator_width': (displaywidth - sumlength) / (ncols - 1) if (ncols - 1) else 0,
  490. 'max_rows': max_rows,
  491. 'column_widths': col_widths
  492. }
  493. def _get_or_default(mylist, i, default=None):
  494. """return list item number, or default if don't exist"""
  495. if i >= len(mylist):
  496. return default
  497. else :
  498. return mylist[i]
  499. def compute_item_matrix(items, row_first=False, empty=None, *args, **kwargs) :
  500. """Returns a nested list, and info to columnize items
  501. Parameters
  502. ----------
  503. items
  504. list of strings to columize
  505. row_first : (default False)
  506. Whether to compute columns for a row-first matrix instead of
  507. column-first (default).
  508. empty : (default None)
  509. default value to fill list if needed
  510. separator_size : int (default=2)
  511. How much caracters will be used as a separation between each columns.
  512. displaywidth : int (default=80)
  513. The width of the area onto wich the columns should enter
  514. Returns
  515. -------
  516. strings_matrix
  517. nested list of string, the outer most list contains as many list as
  518. rows, the innermost lists have each as many element as colums. If the
  519. total number of elements in `items` does not equal the product of
  520. rows*columns, the last element of some lists are filled with `None`.
  521. dict_info
  522. some info to make columnize easier:
  523. num_columns
  524. number of columns
  525. max_rows
  526. maximum number of rows (final number may be less)
  527. column_widths
  528. list of with of each columns
  529. optimal_separator_width
  530. best separator width between columns
  531. Examples
  532. --------
  533. ::
  534. In [1]: l = ['aaa','b','cc','d','eeeee','f','g','h','i','j','k','l']
  535. ...: compute_item_matrix(l, displaywidth=12)
  536. Out[1]:
  537. ([['aaa', 'f', 'k'],
  538. ['b', 'g', 'l'],
  539. ['cc', 'h', None],
  540. ['d', 'i', None],
  541. ['eeeee', 'j', None]],
  542. {'num_columns': 3,
  543. 'column_widths': [5, 1, 1],
  544. 'optimal_separator_width': 2,
  545. 'max_rows': 5})
  546. """
  547. info = _find_optimal(list(map(len, items)), row_first, *args, **kwargs)
  548. nrow, ncol = info['max_rows'], info['num_columns']
  549. if row_first:
  550. return ([[_get_or_default(items, r * ncol + c, default=empty) for c in range(ncol)] for r in range(nrow)], info)
  551. else:
  552. return ([[_get_or_default(items, c * nrow + r, default=empty) for c in range(ncol)] for r in range(nrow)], info)
  553. def columnize(items, row_first=False, separator=' ', displaywidth=80, spread=False):
  554. """ Transform a list of strings into a single string with columns.
  555. Parameters
  556. ----------
  557. items : sequence of strings
  558. The strings to process.
  559. row_first : (default False)
  560. Whether to compute columns for a row-first matrix instead of
  561. column-first (default).
  562. separator : str, optional [default is two spaces]
  563. The string that separates columns.
  564. displaywidth : int, optional [default is 80]
  565. Width of the display in number of characters.
  566. Returns
  567. -------
  568. The formatted string.
  569. """
  570. if not items:
  571. return '\n'
  572. matrix, info = compute_item_matrix(items, row_first=row_first, separator_size=len(separator), displaywidth=displaywidth)
  573. if spread:
  574. separator = separator.ljust(int(info['optimal_separator_width']))
  575. fmatrix = [filter(None, x) for x in matrix]
  576. sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['column_widths'])])
  577. return '\n'.join(map(sjoin, fmatrix))+'\n'
  578. def get_text_list(list_, last_sep=' and ', sep=", ", wrap_item_with=""):
  579. """
  580. Return a string with a natural enumeration of items
  581. >>> get_text_list(['a', 'b', 'c', 'd'])
  582. 'a, b, c and d'
  583. >>> get_text_list(['a', 'b', 'c'], ' or ')
  584. 'a, b or c'
  585. >>> get_text_list(['a', 'b', 'c'], ', ')
  586. 'a, b, c'
  587. >>> get_text_list(['a', 'b'], ' or ')
  588. 'a or b'
  589. >>> get_text_list(['a'])
  590. 'a'
  591. >>> get_text_list([])
  592. ''
  593. >>> get_text_list(['a', 'b'], wrap_item_with="`")
  594. '`a` and `b`'
  595. >>> get_text_list(['a', 'b', 'c', 'd'], " = ", sep=" + ")
  596. 'a + b + c = d'
  597. """
  598. if len(list_) == 0:
  599. return ''
  600. if wrap_item_with:
  601. list_ = ['%s%s%s' % (wrap_item_with, item, wrap_item_with) for
  602. item in list_]
  603. if len(list_) == 1:
  604. return list_[0]
  605. return '%s%s%s' % (
  606. sep.join(i for i in list_[:-1]),
  607. last_sep, list_[-1])