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 |
|
No summary |
Class |
|
Syntax highlighter for Python AST (and some builtins types). |
Function | colorize |
Used to colorize type annotations and parameters default values. |
Function | colorize |
Get a ColorizedPyvalRepr instance for this piece of ast. |
Function | decode |
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. |
Class | _ |
An object uesd to keep track of the current state of the pyval colorizer. The mark() /restore() methods can be used to set a backup point, and restore back to that backup point. This is used by several colorization methods that first try colorizing their object on a single line (setting linebreakok=False); and then fall back on a multi-line output if that fails. |
Class | _ |
Undocumented |
Class | _ |
A context manager that can add enclosing delimiters to nested operators when needed. |
Exception | _ |
A control-flow exception that is raised when PyvalColorizer generates a string containing a newline, but the state object's linebreakok variable is False. |
Exception | _ |
A control-flow exception that is raised when PyvalColorizer exeeds the maximum number of allowed lines. |
Function | _bytes |
Undocumented |
Function | _get |
Undocumented |
Function | _str |
Encode a string such that it's correctly represented inside simple quotes. |
Any
, refmap: dict[ str, str] | None
= None, is_annotation: bool
= False) -> ColorizedPyvalRepr
:
(source)
¶
Used to colorize type annotations and parameters default values.
Returns | |
ColorizedPyvalRepr | colorize_pyval(pyval, linelen=None, linebreakok=False) |
Any
, linelen: int | None
, maxlines: int
, linebreakok: bool
= True, refmap: dict[ str, str] | None
= None, is_annotation: bool
= False) -> ColorizedPyvalRepr
:
(source)
¶
Get a ColorizedPyvalRepr
instance for this piece of ast.
Parameters | |
pyval:Any | Undocumented |
linelen:int | None | Undocumented |
maxlines:int | Undocumented |
linebreakok:bool | Undocumented |
refmap:dict[ | A mapping that maps local names to full names. This can be used to explicitely links some objects by assigning an explicit 'refuri' value on the obj_reference node. This can be used for cases the where the linker might be wrong, obviously this is just a workaround. |
isbool | Undocumented |
Returns | |
ColorizedPyvalRepr | A ColorizedPyvalRepr describing the given pyval. |