reprexlite.code¶
Attributes¶
NO_RETURN = object()
module-attribute
¶
Explicit placeholder object for statements, which have no return value (as opposed to expressions).
Classes¶
CodeBlock
¶
Class that takes a block of Python code input and evaluates it. Call str(...) on an
instance to get back a string containing the original source with evaluated outputs embedded
as comments below each statement.
Attributes:
| Name | Type | Description |
|---|---|---|
input |
str
|
Block of Python code |
style |
bool
|
Whether to use black to autoformat code in returned string representation. |
comment |
str
|
Line prefix to use when rendering the evaluated results. |
terminal |
bool
|
Whether to apply syntax highlighting to the string representation. Requires optional dependency Pygments. |
tree |
Module
|
Parsed LibCST concrete syntax tree of input code. |
statements |
List[Statement]
|
List of individual statements parsed from input code. |
results |
List[Result]
|
List of evaluated results corresponding to each item of statements. |
Source code in reprexlite/code.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
Attributes¶
input = input
instance-attribute
¶
namespace = {}
instance-attribute
¶
results = [stmt.evaluate(self.namespace) for stmt in self.statements]
instance-attribute
¶
statements = [Statement(stmt, style=style) for stmt in self.tree.body]
instance-attribute
¶
terminal = terminal
instance-attribute
¶
tree = cst.parse_module(input)
instance-attribute
¶
Functions¶
__init__(input, style=False, comment='#>', terminal=False, old_results=False)
¶
Initializer method.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
str
|
Block of Python code |
required |
style
|
bool
|
Whether to use black to autoformat code in returned string representation. Defaults to False. |
False
|
comment
|
str
|
Line prefix to use when rendering the evaluated results. Defaults to "#>". |
'#>'
|
terminal
|
bool
|
Whether to apply syntax highlighting to the string representation. Requires optional dependency Pygments. Defaults to False. |
False
|
old_results
|
bool
|
Whether to keep old results, i.e., comment lines in input that
match the |
False
|
Source code in reprexlite/code.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
Result
¶
Class that holds the result of evaluated code. Use str(...) on an instance to produce a
pretty-formatted comment block representation of the result.
Attributes:
| Name | Type | Description |
|---|---|---|
result |
Any
|
Some Python object, intended to be the return value of evaluated Python code. |
comment |
str
|
Line prefix to use when rendering the result for a reprex. |
Source code in reprexlite/code.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
Attributes¶
comment = comment
instance-attribute
¶
result = result
instance-attribute
¶
stdout = stdout
instance-attribute
¶
Functions¶
__init__(result, stdout=None, comment='#>')
¶
Source code in reprexlite/code.py
24 25 26 27 | |
Statement
¶
Class that holds a LibCST parsed statement. The evaluate method will evaluate the statement
and return a Result object. To recover the original source code
for an instancement, call str(...) on it. You can optionally autoformat the returned source
code, controlled by the style attribute.
Attributes:
| Name | Type | Description |
|---|---|---|
stmt |
Union[SimpleStatementLine, BaseCompoundStatement]
|
LibCST parsed statement. |
style |
bool
|
Whether to autoformat the source code with black. |
Source code in reprexlite/code.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
Attributes¶
stmt = stmt
instance-attribute
¶
style = style
instance-attribute
¶
Functions¶
__init__(stmt, style=False)
¶
Source code in reprexlite/code.py
58 59 60 61 62 | |
evaluate(scope)
¶
Source code in reprexlite/code.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
Functions¶
repl_to_reprex_code(input, comment='#>')
¶
Reformat a code block copied from a Python REPL to a reprex-style code block.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
str
|
code block |
required |
comment
|
str
|
Line prefix to use when rendering the evaluated results. Defaults to "#>". |
'#>'
|
Returns:
| Type | Description |
|---|---|
str
|
Reformatted code block in reprex-style. |
Source code in reprexlite/code.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |