text.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. import os
  9. import re
  10. import string
  11. import sys
  12. import textwrap
  13. import warnings
  14. from string import Formatter
  15. from pathlib import Path
  16. from typing import List, Union, Optional, Dict, Tuple
  17. class LSString(str):
  18. """String derivative with a special access attributes.
  19. These are normal strings, but with the special attributes:
  20. .l (or .list) : value as list (split on newlines).
  21. .n (or .nlstr): original value (the string itself).
  22. .s (or .spstr): value as whitespace-separated string.
  23. .p (or .paths): list of path objects (requires path.py package)
  24. Any values which require transformations are computed only once and
  25. cached.
  26. Such strings are very useful to efficiently interact with the shell, which
  27. typically only understands whitespace-separated options for commands."""
  28. def get_list(self):
  29. try:
  30. return self.__list
  31. except AttributeError:
  32. self.__list = self.split('\n')
  33. return self.__list
  34. l = list = property(get_list)
  35. def get_spstr(self):
  36. try:
  37. return self.__spstr
  38. except AttributeError:
  39. self.__spstr = self.replace('\n',' ')
  40. return self.__spstr
  41. s = spstr = property(get_spstr)
  42. def get_nlstr(self):
  43. return self
  44. n = nlstr = property(get_nlstr)
  45. def get_paths(self):
  46. try:
  47. return self.__paths
  48. except AttributeError:
  49. self.__paths = [Path(p) for p in self.split('\n') if os.path.exists(p)]
  50. return self.__paths
  51. p = paths = property(get_paths)
  52. # FIXME: We need to reimplement type specific displayhook and then add this
  53. # back as a custom printer. This should also be moved outside utils into the
  54. # core.
  55. # def print_lsstring(arg):
  56. # """ Prettier (non-repr-like) and more informative printer for LSString """
  57. # print "LSString (.p, .n, .l, .s available). Value:"
  58. # print arg
  59. #
  60. #
  61. # print_lsstring = result_display.register(LSString)(print_lsstring)
  62. class SList(list):
  63. """List derivative with a special access attributes.
  64. These are normal lists, but with the special attributes:
  65. * .l (or .list) : value as list (the list itself).
  66. * .n (or .nlstr): value as a string, joined on newlines.
  67. * .s (or .spstr): value as a string, joined on spaces.
  68. * .p (or .paths): list of path objects (requires path.py package)
  69. Any values which require transformations are computed only once and
  70. cached."""
  71. def get_list(self):
  72. return self
  73. l = list = property(get_list)
  74. def get_spstr(self):
  75. try:
  76. return self.__spstr
  77. except AttributeError:
  78. self.__spstr = ' '.join(self)
  79. return self.__spstr
  80. s = spstr = property(get_spstr)
  81. def get_nlstr(self):
  82. try:
  83. return self.__nlstr
  84. except AttributeError:
  85. self.__nlstr = '\n'.join(self)
  86. return self.__nlstr
  87. n = nlstr = property(get_nlstr)
  88. def get_paths(self):
  89. try:
  90. return self.__paths
  91. except AttributeError:
  92. self.__paths = [Path(p) for p in self if os.path.exists(p)]
  93. return self.__paths
  94. p = paths = property(get_paths)
  95. def grep(self, pattern, prune = False, field = None):
  96. """ Return all strings matching 'pattern' (a regex or callable)
  97. This is case-insensitive. If prune is true, return all items
  98. NOT matching the pattern.
  99. If field is specified, the match must occur in the specified
  100. whitespace-separated field.
  101. Examples::
  102. a.grep( lambda x: x.startswith('C') )
  103. a.grep('Cha.*log', prune=1)
  104. a.grep('chm', field=-1)
  105. """
  106. def match_target(s):
  107. if field is None:
  108. return s
  109. parts = s.split()
  110. try:
  111. tgt = parts[field]
  112. return tgt
  113. except IndexError:
  114. return ""
  115. if isinstance(pattern, str):
  116. pred = lambda x : re.search(pattern, x, re.IGNORECASE)
  117. else:
  118. pred = pattern
  119. if not prune:
  120. return SList([el for el in self if pred(match_target(el))])
  121. else:
  122. return SList([el for el in self if not pred(match_target(el))])
  123. def fields(self, *fields):
  124. """ Collect whitespace-separated fields from string list
  125. Allows quick awk-like usage of string lists.
  126. Example data (in var a, created by 'a = !ls -l')::
  127. -rwxrwxrwx 1 ville None 18 Dec 14 2006 ChangeLog
  128. drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython
  129. * ``a.fields(0)`` is ``['-rwxrwxrwx', 'drwxrwxrwx+']``
  130. * ``a.fields(1,0)`` is ``['1 -rwxrwxrwx', '6 drwxrwxrwx+']``
  131. (note the joining by space).
  132. * ``a.fields(-1)`` is ``['ChangeLog', 'IPython']``
  133. IndexErrors are ignored.
  134. Without args, fields() just split()'s the strings.
  135. """
  136. if len(fields) == 0:
  137. return [el.split() for el in self]
  138. res = SList()
  139. for el in [f.split() for f in self]:
  140. lineparts = []
  141. for fd in fields:
  142. try:
  143. lineparts.append(el[fd])
  144. except IndexError:
  145. pass
  146. if lineparts:
  147. res.append(" ".join(lineparts))
  148. return res
  149. def sort(self,field= None, nums = False):
  150. """ sort by specified fields (see fields())
  151. Example::
  152. a.sort(1, nums = True)
  153. Sorts a by second field, in numerical order (so that 21 > 3)
  154. """
  155. #decorate, sort, undecorate
  156. if field is not None:
  157. dsu = [[SList([line]).fields(field), line] for line in self]
  158. else:
  159. dsu = [[line, line] for line in self]
  160. if nums:
  161. for i in range(len(dsu)):
  162. numstr = "".join([ch for ch in dsu[i][0] if ch.isdigit()])
  163. try:
  164. n = int(numstr)
  165. except ValueError:
  166. n = 0
  167. dsu[i][0] = n
  168. dsu.sort()
  169. return SList([t[1] for t in dsu])
  170. # FIXME: We need to reimplement type specific displayhook and then add this
  171. # back as a custom printer. This should also be moved outside utils into the
  172. # core.
  173. # def print_slist(arg):
  174. # """ Prettier (non-repr-like) and more informative printer for SList """
  175. # print "SList (.p, .n, .l, .s, .grep(), .fields(), sort() available):"
  176. # if hasattr(arg, 'hideonce') and arg.hideonce:
  177. # arg.hideonce = False
  178. # return
  179. #
  180. # nlprint(arg) # This was a nested list printer, now removed.
  181. #
  182. # print_slist = result_display.register(SList)(print_slist)
  183. def indent(instr,nspaces=4, ntabs=0, flatten=False):
  184. """Indent a string a given number of spaces or tabstops.
  185. indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
  186. Parameters
  187. ----------
  188. instr : basestring
  189. The string to be indented.
  190. nspaces : int (default: 4)
  191. The number of spaces to be indented.
  192. ntabs : int (default: 0)
  193. The number of tabs to be indented.
  194. flatten : bool (default: False)
  195. Whether to scrub existing indentation. If True, all lines will be
  196. aligned to the same indentation. If False, existing indentation will
  197. be strictly increased.
  198. Returns
  199. -------
  200. str|unicode : string indented by ntabs and nspaces.
  201. """
  202. if instr is None:
  203. return
  204. ind = '\t'*ntabs+' '*nspaces
  205. if flatten:
  206. pat = re.compile(r'^\s*', re.MULTILINE)
  207. else:
  208. pat = re.compile(r'^', re.MULTILINE)
  209. outstr = re.sub(pat, ind, instr)
  210. if outstr.endswith(os.linesep+ind):
  211. return outstr[:-len(ind)]
  212. else:
  213. return outstr
  214. def list_strings(arg):
  215. """Always return a list of strings, given a string or list of strings
  216. as input.
  217. Examples
  218. --------
  219. ::
  220. In [7]: list_strings('A single string')
  221. Out[7]: ['A single string']
  222. In [8]: list_strings(['A single string in a list'])
  223. Out[8]: ['A single string in a list']
  224. In [9]: list_strings(['A','list','of','strings'])
  225. Out[9]: ['A', 'list', 'of', 'strings']
  226. """
  227. if isinstance(arg, str):
  228. return [arg]
  229. else:
  230. return arg
  231. def marquee(txt='',width=78,mark='*'):
  232. """Return the input string centered in a 'marquee'.
  233. Examples
  234. --------
  235. ::
  236. In [16]: marquee('A test',40)
  237. Out[16]: '**************** A test ****************'
  238. In [17]: marquee('A test',40,'-')
  239. Out[17]: '---------------- A test ----------------'
  240. In [18]: marquee('A test',40,' ')
  241. Out[18]: ' A test '
  242. """
  243. if not txt:
  244. return (mark*width)[:width]
  245. nmark = (width-len(txt)-2)//len(mark)//2
  246. if nmark < 0: nmark =0
  247. marks = mark*nmark
  248. return '%s %s %s' % (marks,txt,marks)
  249. ini_spaces_re = re.compile(r'^(\s+)')
  250. def num_ini_spaces(strng):
  251. """Return the number of initial spaces in a string"""
  252. warnings.warn(
  253. "`num_ini_spaces` is Pending Deprecation since IPython 8.17."
  254. "It is considered fro removal in in future version. "
  255. "Please open an issue if you believe it should be kept.",
  256. stacklevel=2,
  257. category=PendingDeprecationWarning,
  258. )
  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. warnings.warn(
  300. "`wrap_paragraphs` is Pending Deprecation since IPython 8.17."
  301. "It is considered fro removal in in future version. "
  302. "Please open an issue if you believe it should be kept.",
  303. stacklevel=2,
  304. category=PendingDeprecationWarning,
  305. )
  306. paragraph_re = re.compile(r'\n(\s*\n)+', re.MULTILINE)
  307. text = dedent(text).strip()
  308. paragraphs = paragraph_re.split(text)[::2] # every other entry is space
  309. out_ps = []
  310. indent_re = re.compile(r'\n\s+', re.MULTILINE)
  311. for p in paragraphs:
  312. # presume indentation that survives dedent is meaningful formatting,
  313. # so don't fill unless text is flush.
  314. if indent_re.search(p) is None:
  315. # wrap paragraph
  316. p = textwrap.fill(p, ncols)
  317. out_ps.append(p)
  318. return out_ps
  319. def strip_email_quotes(text):
  320. """Strip leading email quotation characters ('>').
  321. Removes any combination of leading '>' interspersed with whitespace that
  322. appears *identically* in all lines of the input text.
  323. Parameters
  324. ----------
  325. text : str
  326. Examples
  327. --------
  328. Simple uses::
  329. In [2]: strip_email_quotes('> > text')
  330. Out[2]: 'text'
  331. In [3]: strip_email_quotes('> > text\\n> > more')
  332. Out[3]: 'text\\nmore'
  333. Note how only the common prefix that appears in all lines is stripped::
  334. In [4]: strip_email_quotes('> > text\\n> > more\\n> more...')
  335. Out[4]: '> text\\n> more\\nmore...'
  336. So if any line has no quote marks ('>'), then none are stripped from any
  337. of them ::
  338. In [5]: strip_email_quotes('> > text\\n> > more\\nlast different')
  339. Out[5]: '> > text\\n> > more\\nlast different'
  340. """
  341. lines = text.splitlines()
  342. strip_len = 0
  343. for characters in zip(*lines):
  344. # Check if all characters in this position are the same
  345. if len(set(characters)) > 1:
  346. break
  347. prefix_char = characters[0]
  348. if prefix_char in string.whitespace or prefix_char == ">":
  349. strip_len += 1
  350. else:
  351. break
  352. text = "\n".join([ln[strip_len:] for ln in lines])
  353. return text
  354. def strip_ansi(source):
  355. """
  356. Remove ansi escape codes from text.
  357. Parameters
  358. ----------
  359. source : str
  360. Source to remove the ansi from
  361. """
  362. warnings.warn(
  363. "`strip_ansi` is Pending Deprecation since IPython 8.17."
  364. "It is considered fro removal in in future version. "
  365. "Please open an issue if you believe it should be kept.",
  366. stacklevel=2,
  367. category=PendingDeprecationWarning,
  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. class FullEvalFormatter(Formatter):
  393. """A String Formatter that allows evaluation of simple expressions.
  394. Any time a format key is not found in the kwargs,
  395. it will be tried as an expression in the kwargs namespace.
  396. Note that this version allows slicing using [1:2], so you cannot specify
  397. a format string. Use :class:`EvalFormatter` to permit format strings.
  398. Examples
  399. --------
  400. ::
  401. In [1]: f = FullEvalFormatter()
  402. In [2]: f.format('{n//4}', n=8)
  403. Out[2]: '2'
  404. In [3]: f.format('{list(range(5))[2:4]}')
  405. Out[3]: '[2, 3]'
  406. In [4]: f.format('{3*2}')
  407. Out[4]: '6'
  408. """
  409. # copied from Formatter._vformat with minor changes to allow eval
  410. # and replace the format_spec code with slicing
  411. def vformat(self, format_string:str, args, kwargs)->str:
  412. result = []
  413. for literal_text, field_name, format_spec, conversion in \
  414. self.parse(format_string):
  415. # output the literal text
  416. if literal_text:
  417. result.append(literal_text)
  418. # if there's a field, output it
  419. if field_name is not None:
  420. # this is some markup, find the object and do
  421. # the formatting
  422. if format_spec:
  423. # override format spec, to allow slicing:
  424. field_name = ':'.join([field_name, format_spec])
  425. # eval the contents of the field for the object
  426. # to be formatted
  427. obj = eval(field_name, kwargs)
  428. # do any conversion on the resulting object
  429. obj = self.convert_field(obj, conversion)
  430. # format the object and append to the result
  431. result.append(self.format_field(obj, ''))
  432. return ''.join(result)
  433. class DollarFormatter(FullEvalFormatter):
  434. """Formatter allowing Itpl style $foo replacement, for names and attribute
  435. access only. Standard {foo} replacement also works, and allows full
  436. evaluation of its arguments.
  437. Examples
  438. --------
  439. ::
  440. In [1]: f = DollarFormatter()
  441. In [2]: f.format('{n//4}', n=8)
  442. Out[2]: '2'
  443. In [3]: f.format('23 * 76 is $result', result=23*76)
  444. Out[3]: '23 * 76 is 1748'
  445. In [4]: f.format('$a or {b}', a=1, b=2)
  446. Out[4]: '1 or 2'
  447. """
  448. _dollar_pattern_ignore_single_quote = re.compile(r"(.*?)\$(\$?[\w\.]+)(?=([^']*'[^']*')*[^']*$)")
  449. def parse(self, fmt_string):
  450. for literal_txt, field_name, format_spec, conversion \
  451. in Formatter.parse(self, fmt_string):
  452. # Find $foo patterns in the literal text.
  453. continue_from = 0
  454. txt = ""
  455. for m in self._dollar_pattern_ignore_single_quote.finditer(literal_txt):
  456. new_txt, new_field = m.group(1,2)
  457. # $$foo --> $foo
  458. if new_field.startswith("$"):
  459. txt += new_txt + new_field
  460. else:
  461. yield (txt + new_txt, new_field, "", None)
  462. txt = ""
  463. continue_from = m.end()
  464. # Re-yield the {foo} style pattern
  465. yield (txt + literal_txt[continue_from:], field_name, format_spec, conversion)
  466. def __repr__(self):
  467. return "<DollarFormatter>"
  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 range(ncols):
  476. yield [l[j] for j in range(i, len(l), ncols)]
  477. else:
  478. for i in range(0, len(l), max_rows):
  479. yield l[i:(i + max_rows)]
  480. def _find_optimal(rlist, row_first: bool, separator_size: int, displaywidth: int):
  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(
  500. items, row_first: bool = False, empty=None, *, separator_size=2, displaywidth=80
  501. ) -> Tuple[List[List[int]], Dict[str, int]]:
  502. """Returns a nested list, and info to columnize items
  503. Parameters
  504. ----------
  505. items
  506. list of strings to columize
  507. row_first : (default False)
  508. Whether to compute columns for a row-first matrix instead of
  509. column-first (default).
  510. empty : (default None)
  511. default value to fill list if needed
  512. separator_size : int (default=2)
  513. How much characters will be used as a separation between each columns.
  514. displaywidth : int (default=80)
  515. The width of the area onto which the columns should enter
  516. Returns
  517. -------
  518. strings_matrix
  519. nested list of string, the outer most list contains as many list as
  520. rows, the innermost lists have each as many element as columns. If the
  521. total number of elements in `items` does not equal the product of
  522. rows*columns, the last element of some lists are filled with `None`.
  523. dict_info
  524. some info to make columnize easier:
  525. num_columns
  526. number of columns
  527. max_rows
  528. maximum number of rows (final number may be less)
  529. column_widths
  530. list of with of each columns
  531. optimal_separator_width
  532. best separator width between columns
  533. Examples
  534. --------
  535. ::
  536. In [1]: l = ['aaa','b','cc','d','eeeee','f','g','h','i','j','k','l']
  537. In [2]: list, info = compute_item_matrix(l, displaywidth=12)
  538. In [3]: list
  539. Out[3]: [['aaa', 'f', 'k'], ['b', 'g', 'l'], ['cc', 'h', None], ['d', 'i', None], ['eeeee', 'j', None]]
  540. In [4]: ideal = {'num_columns': 3, 'column_widths': [5, 1, 1], 'optimal_separator_width': 2, 'max_rows': 5}
  541. In [5]: all((info[k] == ideal[k] for k in ideal.keys()))
  542. Out[5]: True
  543. """
  544. warnings.warn(
  545. "`compute_item_matrix` is Pending Deprecation since IPython 8.17."
  546. "It is considered fro removal in in future version. "
  547. "Please open an issue if you believe it should be kept.",
  548. stacklevel=2,
  549. category=PendingDeprecationWarning,
  550. )
  551. info = _find_optimal(
  552. list(map(len, items)),
  553. row_first,
  554. separator_size=separator_size,
  555. displaywidth=displaywidth,
  556. )
  557. nrow, ncol = info["max_rows"], info["num_columns"]
  558. if row_first:
  559. return ([[_get_or_default(items, r * ncol + c, default=empty) for c in range(ncol)] for r in range(nrow)], info)
  560. else:
  561. return ([[_get_or_default(items, c * nrow + r, default=empty) for c in range(ncol)] for r in range(nrow)], info)
  562. def columnize(items, row_first=False, separator=" ", displaywidth=80, spread=False):
  563. """Transform a list of strings into a single string with columns.
  564. Parameters
  565. ----------
  566. items : sequence of strings
  567. The strings to process.
  568. row_first : (default False)
  569. Whether to compute columns for a row-first matrix instead of
  570. column-first (default).
  571. separator : str, optional [default is two spaces]
  572. The string that separates columns.
  573. displaywidth : int, optional [default is 80]
  574. Width of the display in number of characters.
  575. Returns
  576. -------
  577. The formatted string.
  578. """
  579. warnings.warn(
  580. "`columnize` is Pending Deprecation since IPython 8.17."
  581. "It is considered fro removal in in future version. "
  582. "Please open an issue if you believe it should be kept.",
  583. stacklevel=2,
  584. category=PendingDeprecationWarning,
  585. )
  586. if not items:
  587. return "\n"
  588. matrix: List[List[int]]
  589. matrix, info = compute_item_matrix(
  590. items,
  591. row_first=row_first,
  592. separator_size=len(separator),
  593. displaywidth=displaywidth,
  594. )
  595. if spread:
  596. separator = separator.ljust(int(info["optimal_separator_width"]))
  597. fmatrix: List[filter[int]] = [filter(None, x) for x in matrix]
  598. sjoin = lambda x: separator.join(
  599. [y.ljust(w, " ") for y, w in zip(x, info["column_widths"])]
  600. )
  601. return "\n".join(map(sjoin, fmatrix)) + "\n"
  602. def get_text_list(list_, last_sep=' and ', sep=", ", wrap_item_with=""):
  603. """
  604. Return a string with a natural enumeration of items
  605. >>> get_text_list(['a', 'b', 'c', 'd'])
  606. 'a, b, c and d'
  607. >>> get_text_list(['a', 'b', 'c'], ' or ')
  608. 'a, b or c'
  609. >>> get_text_list(['a', 'b', 'c'], ', ')
  610. 'a, b, c'
  611. >>> get_text_list(['a', 'b'], ' or ')
  612. 'a or b'
  613. >>> get_text_list(['a'])
  614. 'a'
  615. >>> get_text_list([])
  616. ''
  617. >>> get_text_list(['a', 'b'], wrap_item_with="`")
  618. '`a` and `b`'
  619. >>> get_text_list(['a', 'b', 'c', 'd'], " = ", sep=" + ")
  620. 'a + b + c = d'
  621. """
  622. if len(list_) == 0:
  623. return ''
  624. if wrap_item_with:
  625. list_ = ['%s%s%s' % (wrap_item_with, item, wrap_item_with) for
  626. item in list_]
  627. if len(list_) == 1:
  628. return list_[0]
  629. return '%s%s%s' % (
  630. sep.join(i for i in list_[:-1]),
  631. last_sep, list_[-1])