Warning
This documentation is for an old version of IPython. You can find docs for newer versions here.
Utilities for working with strings and text.
Inheritance diagram:
Bases: str
String derivative with a special access attributes.
These are normal strings, but with the special attributes:
.l (or .list) : value as list (split on newlines). .n (or .nlstr): original value (the string itself). .s (or .spstr): value as whitespace-separated string. .p (or .paths): list of path objects
Any values which require transformations are computed only once and cached.
Such strings are very useful to efficiently interact with the shell, which typically only understands whitespace-separated options for commands.
Bases: list
List derivative with a special access attributes.
These are normal lists, but with the special attributes:
.l (or .list) : value as list (the list itself). .n (or .nlstr): value as a string, joined on newlines. .s (or .spstr): value as a string, joined on spaces. .p (or .paths): list of path objects
Any values which require transformations are computed only once and cached.
Collect whitespace-separated fields from string list
Allows quick awk-like usage of string lists.
-rwxrwxrwx | 1 ville None 18 Dec 14 2006 ChangeLog |
drwxrwxrwx+ 6 ville None 0 Oct 24 18:05 IPython
a.fields(0) is [‘-rwxrwxrwx’, ‘drwxrwxrwx+’] a.fields(1,0) is [‘1 -rwxrwxrwx’, ‘6 drwxrwxrwx+’] (note the joining by space). a.fields(-1) is [‘ChangeLog’, ‘IPython’]
IndexErrors are ignored.
Without args, fields() just split()’s the strings.
Return all strings matching ‘pattern’ (a regex or callable)
This is case-insensitive. If prune is true, return all items NOT matching the pattern.
If field is specified, the match must occur in the specified whitespace-separated field.
Examples:
a.grep( lambda x: x.startswith('C') )
a.grep('Cha.*log', prune=1)
a.grep('chm', field=-1)
sort by specified fields (see fields())
Sorts a by second field, in numerical order (so that 21 > 3)
Bases: string.Formatter
A String Formatter that allows evaluation of simple expressions.
Note that this version interprets a : as specifying a format string (as per standard string formatting), so if slicing is required, you must explicitly create a slice.
This is to be used in templating cases, such as the parallel batch script templates, where simple arithmetic on arguments is useful.
Examples
In [1]: f = EvalFormatter() In [2]: f.format(‘{n//4}’, n=8) Out [2]: ‘2’
In [3]: f.format(“{greeting[slice(2,4)]}”, greeting=”Hello”) Out [3]: ‘ll’
Bases: string.Formatter
A String Formatter that allows evaluation of simple expressions.
Any time a format key is not found in the kwargs, it will be tried as an expression in the kwargs namespace.
Note that this version allows slicing using [1:2], so you cannot specify a format string. Use EvalFormatter to permit format strings.
Examples
In [1]: f = FullEvalFormatter() In [2]: f.format(‘{n//4}’, n=8) Out[2]: u‘2’
In [3]: f.format(‘{list(range(5))[2:4]}’) Out[3]: u’[2, 3]’
In [4]: f.format(‘{3*2}’) Out[4]: u‘6’
Bases: IPython.utils.text.FullEvalFormatter
Formatter allowing Itpl style $foo replacement, for names and attribute access only. Standard {foo} replacement also works, and allows full evaluation of its arguments.
Examples
In [1]: f = DollarFormatter() In [2]: f.format(‘{n//4}’, n=8) Out[2]: u‘2’
In [3]: f.format(‘23 * 76 is $result’, result=23*76) Out[3]: u‘23 * 76 is 1748’
In [4]: f.format(‘$a or {b}’, a=1, b=2) Out[4]: u‘1 or 2’
Indent a string a given number of spaces or tabstops.
indent(str,nspaces=4,ntabs=0) -> indent str by ntabs+nspaces.
Parameters: | instr : basestring
nspaces : int (default: 4)
ntabs : int (default: 0)
flatten : bool (default: False)
|
---|---|
Returns: | str|unicode : string indented by ntabs and nspaces. |
Always return a list of strings, given a string or list of strings as input.
Examples: | In [7]: list_strings(‘A single string’) Out[7]: [‘A single string’] In [8]: list_strings([‘A single string in a list’]) Out[8]: [‘A single string in a list’] In [9]: list_strings([‘A’,’list’,’of’,’strings’]) Out[9]: [‘A’, ‘list’, ‘of’, ‘strings’] |
---|
Return the input string centered in a ‘marquee’.
Examples: | In [16]: marquee(‘A test’,40) Out[16]: ‘************ A test ************‘ In [17]: marquee(‘A test’,40,’-‘) Out[17]: ‘—————- A test —————-‘ In [18]: marquee(‘A test’,40,’ ‘) Out[18]: ‘ A test ‘ |
---|
Return the number of initial spaces in a string
Format a string for screen printing.
This removes some latex-type format codes.
Equivalent of textwrap.dedent that ignores unindented first line.
This means it will still dedent strings like: ‘’‘foo is a bar ‘’‘
For use in wrap_paragraphs.
Wrap multiple paragraphs to fit a specified width.
This is equivalent to textwrap.wrap, but with support for multiple paragraphs, as separated by empty lines.
Returns: | list of complete paragraphs, wrapped to fill `ncols` columns. : |
---|
Return the longest common substring in a list of strings.
Strip leading email quotation characters (‘>’).
Removes any combination of leading ‘>’ interspersed with whitespace that appears identically in all lines of the input text.
Parameters: | text : str |
---|
Examples
Simple uses:
In [2]: strip_email_quotes('> > text')
Out[2]: 'text'
In [3]: strip_email_quotes('> > text\n> > more')
Out[3]: 'text\nmore'
Note how only the common prefix that appears in all lines is stripped:
In [4]: strip_email_quotes('> > text\n> > more\n> more...')
Out[4]: '> text\n> more\nmore...'
So if any line has no quote marks (‘>’) , then none are stripped from any of them
In [5]: strip_email_quotes('> > text\n> > more\nlast different')
Out[5]: '> > text\n> > more\nlast different'
Returns a nested list, and info to columnize items
Parameters: | items : :
empty : (default None)
separator_size : int (default=2)
displaywidth : int (default=80)
|
---|---|
Returns: | Returns a tuple of (strings_matrix, dict_info) : strings_matrix : :
dict_info : :
|
Examples
Transform a list of strings into a single string with columns.
Parameters: | items : sequence of strings
separator : str, optional [default is two spaces]
displaywidth : int, optional [default is 80]
|
---|---|
Returns: | The formatted string. : |