module documentation

Syntax highlighter for Python values. Currently provides special colorization support for:

  • lists, tuples, sets, frozensets, dicts
  • numbers
  • strings
  • compiled regexps
  • a variety of AST expressions

The highlighter also takes care of line-wrapping, and automatically stops generating repr output as soon as it has exceeded the specified number of lines (which should make it faster than pprint for large values). It does not bother to do automatic cycle detection, because maxlines is typically around 5, so it's really not worth it.

The syntax-highlighted output is encoded using a ParsedDocstring, which can then be used to generate output in a variety of formats.

Implementation note: we use exact tests for builtin classes (list, etc) rather than using isinstance, because subclasses might override __repr__.

Usage: >>>

Class ​Colorized​Pyval​Repr
Class ​Pyval​Colorizer Syntax highlighter for Python values.
Function colorize​_inline​_pyval Used to colorize type annotations and parameters default values. @returns: colorize_pyval(pyval, linelen=None, linebreakok=False)
Function colorize​_pyval
Function decode​_with​_backslashreplace No summary
Class _​Colorizer​State No summary
Class _​Linebreak A control-flow exception that is raised when PyvalColorizer generates a string containing a newline, but the state object's linebreakok variable is False.
Class _​Marked​Colorizer​State Undocumented
Class _​Maxlines A control-flow exception that is raised when PyvalColorizer exeeds the maximum number of allowed lines.
Class _​Operator​Delimiter A context manager that can add enclosing delimiters to nested operators when needed.
Class _​Parentage Add parent attribute to ast nodes instances.
Function ​_bytes​_escape Undocumented
Function ​_get​_str​_func Undocumented
Function ​_str​_escape Encode a string such that it's correctly represented inside simple quotes.
def colorize_inline_pyval(pyval):
Used to colorize type annotations and parameters default values.
Parameters
pyval:AnyUndocumented
Returns
ColorizedPyvalReprcolorize_pyval(pyval, linelen=None, linebreakok=False)
def colorize_pyval(pyval, linelen, maxlines, linebreakok=True):
Parameters
pyval:AnyUndocumented
linelen:Optional[int]Undocumented
maxlines:intUndocumented
linebreakok:boolUndocumented
Returns
ColorizedPyvalReprA ColorizedPyvalRepr describing the given pyval.
def decode_with_backslashreplace(s):

Convert the given 8-bit string into unicode, treating any character c such that ord(c)<128 as an ascii character, and converting any c such that ord(c)>128 into a backslashed escape sequence.

>>> decode_with_backslashreplace(b'abc\xff\xe8')
'abc\\xff\\xe8'
Parameters
s:bytesUndocumented
Returns
strUndocumented
def _bytes_escape(b):

Undocumented

Parameters
b:bytesUndocumented
Returns
strUndocumented
def _get_str_func(pyval):

Undocumented

Parameters
pyval:AnyStrUndocumented
Returns
Callable[[str], AnyStr]Undocumented
def _str_escape(s):
Encode a string such that it's correctly represented inside simple quotes.
Parameters
s:strUndocumented
Returns
strUndocumented