text.py 26 KB

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