text.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. """
  2. Utilities for working with strings and text.
  3. Inheritance diagram:
  4. .. inheritance-diagram:: IPython.utils.text
  5. :parts: 3
  6. """
  7. import os
  8. import re
  9. import string
  10. import sys
  11. import textwrap
  12. import warnings
  13. from string import Formatter
  14. from pathlib import Path
  15. from typing import List, Dict, Tuple, Optional, cast, Sequence, Mapping, Any
  16. if sys.version_info < (3, 12):
  17. from typing_extensions import Self
  18. else:
  19. from typing import Self
  20. class LSString(str):
  21. """String derivative with a special access attributes.
  22. These are normal strings, but with the special attributes:
  23. .l (or .list) : value as list (split on newlines).
  24. .n (or .nlstr): original value (the string itself).
  25. .s (or .spstr): value as whitespace-separated string.
  26. .p (or .paths): list of path objects (requires path.py package)
  27. Any values which require transformations are computed only once and
  28. cached.
  29. Such strings are very useful to efficiently interact with the shell, which
  30. typically only understands whitespace-separated options for commands."""
  31. __list: List[str]
  32. __spstr: str
  33. __paths: List[Path]
  34. def get_list(self) -> List[str]:
  35. try:
  36. return self.__list
  37. except AttributeError:
  38. self.__list = self.split('\n')
  39. return self.__list
  40. l = list = property(get_list)
  41. def get_spstr(self) -> str:
  42. try:
  43. return self.__spstr
  44. except AttributeError:
  45. self.__spstr = self.replace('\n',' ')
  46. return self.__spstr
  47. s = spstr = property(get_spstr)
  48. def get_nlstr(self) -> Self:
  49. return self
  50. n = nlstr = property(get_nlstr)
  51. def get_paths(self) -> List[Path]:
  52. try:
  53. return self.__paths
  54. except AttributeError:
  55. self.__paths = [Path(p) for p in self.split('\n') if os.path.exists(p)]
  56. return self.__paths
  57. p = paths = property(get_paths)
  58. # FIXME: We need to reimplement type specific displayhook and then add this
  59. # back as a custom printer. This should also be moved outside utils into the
  60. # core.
  61. # def print_lsstring(arg):
  62. # """ Prettier (non-repr-like) and more informative printer for LSString """
  63. # print("LSString (.p, .n, .l, .s available). Value:")
  64. # print(arg)
  65. #
  66. #
  67. # print_lsstring = result_display.register(LSString)(print_lsstring)
  68. class SList(list):
  69. """List derivative with a special access attributes.
  70. These are normal lists, but with the special attributes:
  71. * .l (or .list) : value as list (the list itself).
  72. * .n (or .nlstr): value as a string, joined on newlines.
  73. * .s (or .spstr): value as a string, joined on spaces.
  74. * .p (or .paths): list of path objects (requires path.py package)
  75. Any values which require transformations are computed only once and
  76. cached."""
  77. __spstr: str
  78. __nlstr: str
  79. __paths: List[Path]
  80. def get_list(self) -> Self:
  81. return self
  82. l = list = property(get_list)
  83. def get_spstr(self) -> str:
  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) -> str:
  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) -> List[Path]:
  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, str):
  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.register(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, str):
  237. return [arg]
  238. else:
  239. return arg
  240. def marquee(txt='',width=78,mark='*'):
  241. """Return the input string centered in a 'marquee'.
  242. Examples
  243. --------
  244. ::
  245. In [16]: marquee('A test',40)
  246. Out[16]: '**************** A test ****************'
  247. In [17]: marquee('A test',40,'-')
  248. Out[17]: '---------------- A test ----------------'
  249. In [18]: marquee('A test',40,' ')
  250. Out[18]: ' A test '
  251. """
  252. if not txt:
  253. return (mark*width)[:width]
  254. nmark = (width-len(txt)-2)//len(mark)//2
  255. if nmark < 0: nmark =0
  256. marks = mark*nmark
  257. return '%s %s %s' % (marks,txt,marks)
  258. ini_spaces_re = re.compile(r'^(\s+)')
  259. def num_ini_spaces(strng):
  260. """Return the number of initial spaces in a string"""
  261. warnings.warn(
  262. "`num_ini_spaces` is Pending Deprecation since IPython 8.17."
  263. "It is considered fro removal in in future version. "
  264. "Please open an issue if you believe it should be kept.",
  265. stacklevel=2,
  266. category=PendingDeprecationWarning,
  267. )
  268. ini_spaces = ini_spaces_re.match(strng)
  269. if ini_spaces:
  270. return ini_spaces.end()
  271. else:
  272. return 0
  273. def format_screen(strng):
  274. """Format a string for screen printing.
  275. This removes some latex-type format codes."""
  276. # Paragraph continue
  277. par_re = re.compile(r'\\$',re.MULTILINE)
  278. strng = par_re.sub('',strng)
  279. return strng
  280. def dedent(text: str) -> str:
  281. """Equivalent of textwrap.dedent that ignores unindented first line.
  282. This means it will still dedent strings like:
  283. '''foo
  284. is a bar
  285. '''
  286. For use in wrap_paragraphs.
  287. """
  288. if text.startswith('\n'):
  289. # text starts with blank line, don't ignore the first line
  290. return textwrap.dedent(text)
  291. # split first line
  292. splits = text.split('\n',1)
  293. if len(splits) == 1:
  294. # only one line
  295. return textwrap.dedent(text)
  296. first, rest = splits
  297. # dedent everything but the first line
  298. rest = textwrap.dedent(rest)
  299. return '\n'.join([first, rest])
  300. def wrap_paragraphs(text, ncols=80):
  301. """Wrap multiple paragraphs to fit a specified width.
  302. This is equivalent to textwrap.wrap, but with support for multiple
  303. paragraphs, as separated by empty lines.
  304. Returns
  305. -------
  306. list of complete paragraphs, wrapped to fill `ncols` columns.
  307. """
  308. warnings.warn(
  309. "`wrap_paragraphs` is Pending Deprecation since IPython 8.17."
  310. "It is considered fro removal in in future version. "
  311. "Please open an issue if you believe it should be kept.",
  312. stacklevel=2,
  313. category=PendingDeprecationWarning,
  314. )
  315. paragraph_re = re.compile(r'\n(\s*\n)+', re.MULTILINE)
  316. text = dedent(text).strip()
  317. paragraphs = paragraph_re.split(text)[::2] # every other entry is space
  318. out_ps = []
  319. indent_re = re.compile(r'\n\s+', re.MULTILINE)
  320. for p in paragraphs:
  321. # presume indentation that survives dedent is meaningful formatting,
  322. # so don't fill unless text is flush.
  323. if indent_re.search(p) is None:
  324. # wrap paragraph
  325. p = textwrap.fill(p, ncols)
  326. out_ps.append(p)
  327. return out_ps
  328. def strip_email_quotes(text):
  329. """Strip leading email quotation characters ('>').
  330. Removes any combination of leading '>' interspersed with whitespace that
  331. appears *identically* in all lines of the input text.
  332. Parameters
  333. ----------
  334. text : str
  335. Examples
  336. --------
  337. Simple uses::
  338. In [2]: strip_email_quotes('> > text')
  339. Out[2]: 'text'
  340. In [3]: strip_email_quotes('> > text\\n> > more')
  341. Out[3]: 'text\\nmore'
  342. Note how only the common prefix that appears in all lines is stripped::
  343. In [4]: strip_email_quotes('> > text\\n> > more\\n> more...')
  344. Out[4]: '> text\\n> more\\nmore...'
  345. So if any line has no quote marks ('>'), then none are stripped from any
  346. of them ::
  347. In [5]: strip_email_quotes('> > text\\n> > more\\nlast different')
  348. Out[5]: '> > text\\n> > more\\nlast different'
  349. """
  350. lines = text.splitlines()
  351. strip_len = 0
  352. for characters in zip(*lines):
  353. # Check if all characters in this position are the same
  354. if len(set(characters)) > 1:
  355. break
  356. prefix_char = characters[0]
  357. if prefix_char in string.whitespace or prefix_char == ">":
  358. strip_len += 1
  359. else:
  360. break
  361. text = "\n".join([ln[strip_len:] for ln in lines])
  362. return text
  363. def strip_ansi(source):
  364. """
  365. Remove ansi escape codes from text.
  366. Parameters
  367. ----------
  368. source : str
  369. Source to remove the ansi from
  370. """
  371. warnings.warn(
  372. "`strip_ansi` is Pending Deprecation since IPython 8.17."
  373. "It is considered fro removal in in future version. "
  374. "Please open an issue if you believe it should be kept.",
  375. stacklevel=2,
  376. category=PendingDeprecationWarning,
  377. )
  378. return re.sub(r'\033\[(\d|;)+?m', '', source)
  379. class EvalFormatter(Formatter):
  380. """A String Formatter that allows evaluation of simple expressions.
  381. Note that this version interprets a `:` as specifying a format string (as per
  382. standard string formatting), so if slicing is required, you must explicitly
  383. create a slice.
  384. This is to be used in templating cases, such as the parallel batch
  385. script templates, where simple arithmetic on arguments is useful.
  386. Examples
  387. --------
  388. ::
  389. In [1]: f = EvalFormatter()
  390. In [2]: f.format('{n//4}', n=8)
  391. Out[2]: '2'
  392. In [3]: f.format("{greeting[slice(2,4)]}", greeting="Hello")
  393. Out[3]: 'll'
  394. """
  395. def get_field(self, name, args, kwargs):
  396. v = eval(name, kwargs)
  397. return v, name
  398. #XXX: As of Python 3.4, the format string parsing no longer splits on a colon
  399. # inside [], so EvalFormatter can handle slicing. Once we only support 3.4 and
  400. # above, it should be possible to remove FullEvalFormatter.
  401. class FullEvalFormatter(Formatter):
  402. """A String Formatter that allows evaluation of simple expressions.
  403. Any time a format key is not found in the kwargs,
  404. it will be tried as an expression in the kwargs namespace.
  405. Note that this version allows slicing using [1:2], so you cannot specify
  406. a format string. Use :class:`EvalFormatter` to permit format strings.
  407. Examples
  408. --------
  409. ::
  410. In [1]: f = FullEvalFormatter()
  411. In [2]: f.format('{n//4}', n=8)
  412. Out[2]: '2'
  413. In [3]: f.format('{list(range(5))[2:4]}')
  414. Out[3]: '[2, 3]'
  415. In [4]: f.format('{3*2}')
  416. Out[4]: '6'
  417. """
  418. # copied from Formatter._vformat with minor changes to allow eval
  419. # and replace the format_spec code with slicing
  420. def vformat(
  421. self, format_string: str, args: Sequence[Any], kwargs: Mapping[str, Any]
  422. ) -> str:
  423. result = []
  424. conversion: Optional[str]
  425. for literal_text, field_name, format_spec, conversion in self.parse(
  426. format_string
  427. ):
  428. # output the literal text
  429. if literal_text:
  430. result.append(literal_text)
  431. # if there's a field, output it
  432. if field_name is not None:
  433. # this is some markup, find the object and do
  434. # the formatting
  435. if format_spec:
  436. # override format spec, to allow slicing:
  437. field_name = ':'.join([field_name, format_spec])
  438. # eval the contents of the field for the object
  439. # to be formatted
  440. obj = eval(field_name, dict(kwargs))
  441. # do any conversion on the resulting object
  442. # type issue in typeshed, fined in https://github.com/python/typeshed/pull/11377
  443. obj = self.convert_field(obj, conversion) # type: ignore[arg-type]
  444. # format the object and append to the result
  445. result.append(self.format_field(obj, ''))
  446. return ''.join(result)
  447. class DollarFormatter(FullEvalFormatter):
  448. """Formatter allowing Itpl style $foo replacement, for names and attribute
  449. access only. Standard {foo} replacement also works, and allows full
  450. evaluation of its arguments.
  451. Examples
  452. --------
  453. ::
  454. In [1]: f = DollarFormatter()
  455. In [2]: f.format('{n//4}', n=8)
  456. Out[2]: '2'
  457. In [3]: f.format('23 * 76 is $result', result=23*76)
  458. Out[3]: '23 * 76 is 1748'
  459. In [4]: f.format('$a or {b}', a=1, b=2)
  460. Out[4]: '1 or 2'
  461. """
  462. _dollar_pattern_ignore_single_quote = re.compile(r"(.*?)\$(\$?[\w\.]+)(?=([^']*'[^']*')*[^']*$)")
  463. def parse(self, fmt_string):
  464. for literal_txt, field_name, format_spec, conversion \
  465. in Formatter.parse(self, fmt_string):
  466. # Find $foo patterns in the literal text.
  467. continue_from = 0
  468. txt = ""
  469. for m in self._dollar_pattern_ignore_single_quote.finditer(literal_txt):
  470. new_txt, new_field = m.group(1,2)
  471. # $$foo --> $foo
  472. if new_field.startswith("$"):
  473. txt += new_txt + new_field
  474. else:
  475. yield (txt + new_txt, new_field, "", None)
  476. txt = ""
  477. continue_from = m.end()
  478. # Re-yield the {foo} style pattern
  479. yield (txt + literal_txt[continue_from:], field_name, format_spec, conversion)
  480. def __repr__(self):
  481. return "<DollarFormatter>"
  482. #-----------------------------------------------------------------------------
  483. # Utils to columnize a list of string
  484. #-----------------------------------------------------------------------------
  485. def _col_chunks(l, max_rows, row_first=False):
  486. """Yield successive max_rows-sized column chunks from l."""
  487. if row_first:
  488. ncols = (len(l) // max_rows) + (len(l) % max_rows > 0)
  489. for i in range(ncols):
  490. yield [l[j] for j in range(i, len(l), ncols)]
  491. else:
  492. for i in range(0, len(l), max_rows):
  493. yield l[i:(i + max_rows)]
  494. def _find_optimal(
  495. rlist: List[str], row_first: bool, separator_size: int, displaywidth: int
  496. ) -> Dict[str, Any]:
  497. """Calculate optimal info to columnize a list of string"""
  498. for max_rows in range(1, len(rlist) + 1):
  499. col_widths = list(map(max, _col_chunks(rlist, max_rows, row_first)))
  500. sumlength = sum(col_widths)
  501. ncols = len(col_widths)
  502. if sumlength + separator_size * (ncols - 1) <= displaywidth:
  503. break
  504. return {'num_columns': ncols,
  505. 'optimal_separator_width': (displaywidth - sumlength) // (ncols - 1) if (ncols - 1) else 0,
  506. 'max_rows': max_rows,
  507. 'column_widths': col_widths
  508. }
  509. def _get_or_default(mylist, i, default=None):
  510. """return list item number, or default if don't exist"""
  511. if i >= len(mylist):
  512. return default
  513. else :
  514. return mylist[i]
  515. def compute_item_matrix(
  516. items: List[str],
  517. row_first: bool = False,
  518. empty: Optional[str] = None,
  519. *,
  520. separator_size: int = 2,
  521. displaywidth: int = 80,
  522. ) -> Tuple[List[List[int]], Dict[str, int]]:
  523. """Returns a nested list, and info to columnize items
  524. Parameters
  525. ----------
  526. items
  527. list of strings to columize
  528. row_first : (default False)
  529. Whether to compute columns for a row-first matrix instead of
  530. column-first (default).
  531. empty : (default None)
  532. default value to fill list if needed
  533. separator_size : int (default=2)
  534. How much characters will be used as a separation between each columns.
  535. displaywidth : int (default=80)
  536. The width of the area onto which the columns should enter
  537. Returns
  538. -------
  539. strings_matrix
  540. nested list of string, the outer most list contains as many list as
  541. rows, the innermost lists have each as many element as columns. If the
  542. total number of elements in `items` does not equal the product of
  543. rows*columns, the last element of some lists are filled with `None`.
  544. dict_info
  545. some info to make columnize easier:
  546. num_columns
  547. number of columns
  548. max_rows
  549. maximum number of rows (final number may be less)
  550. column_widths
  551. list of with of each columns
  552. optimal_separator_width
  553. best separator width between columns
  554. Examples
  555. --------
  556. ::
  557. In [1]: l = ['aaa','b','cc','d','eeeee','f','g','h','i','j','k','l']
  558. In [2]: list, info = compute_item_matrix(l, displaywidth=12)
  559. In [3]: list
  560. Out[3]: [['aaa', 'f', 'k'], ['b', 'g', 'l'], ['cc', 'h', None], ['d', 'i', None], ['eeeee', 'j', None]]
  561. In [4]: ideal = {'num_columns': 3, 'column_widths': [5, 1, 1], 'optimal_separator_width': 2, 'max_rows': 5}
  562. In [5]: all((info[k] == ideal[k] for k in ideal.keys()))
  563. Out[5]: True
  564. """
  565. warnings.warn(
  566. "`compute_item_matrix` is Pending Deprecation since IPython 8.17."
  567. "It is considered fro removal in in future version. "
  568. "Please open an issue if you believe it should be kept.",
  569. stacklevel=2,
  570. category=PendingDeprecationWarning,
  571. )
  572. info = _find_optimal(
  573. list(map(len, items)), # type: ignore[arg-type]
  574. row_first,
  575. separator_size=separator_size,
  576. displaywidth=displaywidth,
  577. )
  578. nrow, ncol = info["max_rows"], info["num_columns"]
  579. if row_first:
  580. return ([[_get_or_default(items, r * ncol + c, default=empty) for c in range(ncol)] for r in range(nrow)], info)
  581. else:
  582. return ([[_get_or_default(items, c * nrow + r, default=empty) for c in range(ncol)] for r in range(nrow)], info)
  583. def columnize(
  584. items: List[str],
  585. row_first: bool = False,
  586. separator: str = " ",
  587. displaywidth: int = 80,
  588. spread: bool = False,
  589. ) -> str:
  590. """Transform a list of strings into a single string with columns.
  591. Parameters
  592. ----------
  593. items : sequence of strings
  594. The strings to process.
  595. row_first : (default False)
  596. Whether to compute columns for a row-first matrix instead of
  597. column-first (default).
  598. separator : str, optional [default is two spaces]
  599. The string that separates columns.
  600. displaywidth : int, optional [default is 80]
  601. Width of the display in number of characters.
  602. Returns
  603. -------
  604. The formatted string.
  605. """
  606. warnings.warn(
  607. "`columnize` is Pending Deprecation since IPython 8.17."
  608. "It is considered for removal in future versions. "
  609. "Please open an issue if you believe it should be kept.",
  610. stacklevel=2,
  611. category=PendingDeprecationWarning,
  612. )
  613. if not items:
  614. return "\n"
  615. matrix: List[List[int]]
  616. matrix, info = compute_item_matrix(
  617. items,
  618. row_first=row_first,
  619. separator_size=len(separator),
  620. displaywidth=displaywidth,
  621. )
  622. if spread:
  623. separator = separator.ljust(int(info["optimal_separator_width"]))
  624. fmatrix: List[filter[int]] = [filter(None, x) for x in matrix]
  625. sjoin = lambda x: separator.join(
  626. [y.ljust(w, " ") for y, w in zip(x, cast(List[int], info["column_widths"]))]
  627. )
  628. return "\n".join(map(sjoin, fmatrix)) + "\n"
  629. def get_text_list(list_, last_sep=' and ', sep=", ", wrap_item_with=""):
  630. """
  631. Return a string with a natural enumeration of items
  632. >>> get_text_list(['a', 'b', 'c', 'd'])
  633. 'a, b, c and d'
  634. >>> get_text_list(['a', 'b', 'c'], ' or ')
  635. 'a, b or c'
  636. >>> get_text_list(['a', 'b', 'c'], ', ')
  637. 'a, b, c'
  638. >>> get_text_list(['a', 'b'], ' or ')
  639. 'a or b'
  640. >>> get_text_list(['a'])
  641. 'a'
  642. >>> get_text_list([])
  643. ''
  644. >>> get_text_list(['a', 'b'], wrap_item_with="`")
  645. '`a` and `b`'
  646. >>> get_text_list(['a', 'b', 'c', 'd'], " = ", sep=" + ")
  647. 'a + b + c = d'
  648. """
  649. if len(list_) == 0:
  650. return ''
  651. if wrap_item_with:
  652. list_ = ['%s%s%s' % (wrap_item_with, item, wrap_item_with) for
  653. item in list_]
  654. if len(list_) == 1:
  655. return list_[0]
  656. return '%s%s%s' % (
  657. sep.join(i for i in list_[:-1]),
  658. last_sep, list_[-1])