reprexlite.reprexes¶
Attributes¶
Classes¶
ParsedResult
dataclass
¶
Class that holds parsed result from reading a reprex.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
ReprexConfig
|
Configuration for formatting and parsing |
lines |
List[str]
|
String content of result parsed from a reprex |
Source code in reprexlite/reprexes.py
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 103 104 105 106 107 108 109 110 111 | |
RawResult
dataclass
¶
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 |
|---|---|---|
config |
ReprexConfig
|
Configuration for formatting and parsing |
raw |
Any
|
Some Python object that is the raw return value of evaluated Python code. |
stdout |
str
|
Standard output from evaluated Python code. |
Source code in reprexlite/reprexes.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
Reprex
dataclass
¶
Dataclass for a reprex, which holds Python code and results from evaluation.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
ReprexConfig
|
Configuration for formatting and parsing |
statements |
List[Statement]
|
List of parsed Python code statements |
results |
List[RawResult]
|
List of results evaluated from statements |
old_results |
List[ParsedResult]
|
List of any old results parsed from input code |
scope |
Dict[str, Any]
|
Dictionary holding the scope that the reprex was evaluated in |
Source code in reprexlite/reprexes.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 | |
Attributes¶
results_match: bool
property
¶
Whether results of evaluating code match old results parsed from input.
Functions¶
from_input(input: str, config: Optional[ReprexConfig] = None, scope: Optional[Dict[str, Any]] = None) -> Self
classmethod
¶
Create a Reprex instance from parsing and evaluating code from a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
str
|
Input code |
required |
config
|
Optional[ReprexConfig]
|
Configuration. Defaults to None, which will use default settings. |
None
|
scope
|
Optional[Dict[str, Any]]
|
Dictionary holding scope that the parsed code will be evaluated with. Defaults to None, which will create an empty dictionary. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Reprex |
Self
|
New instance of Reprex. |
Source code in reprexlite/reprexes.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 | |
from_input_lines(lines: Sequence[Tuple[str, LineType]], config: Optional[ReprexConfig] = None, scope: Optional[Dict[str, Any]] = None) -> Self
classmethod
¶
Creates a Reprex instance from the output of parse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
Sequence[Tuple[str, LineType]]
|
Output from parse. |
required |
config
|
Optional[ReprexConfig]
|
Configuration. Defaults to None, which will use default settings. |
None
|
scope
|
Optional[Dict[str, Any]]
|
Dictionary holding scope that the parsed code will be evaluated with. Defaults to None, which will create an empty dictionary. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Reprex |
Self
|
New instance of Reprex. |
Source code in reprexlite/reprexes.py
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
Statement
dataclass
¶
Dataclass that holds a LibCST parsed statement. of code.
Attributes:
| Name | Type | Description |
|---|---|---|
config |
ReprexConfig
|
Configuration for formatting and parsing |
stmt |
Union[SimpleStatementLine, BaseCompoundStatement]
|
LibCST parsed statement. |
Source code in reprexlite/reprexes.py
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | |
Attributes¶
code: str
property
¶
Code of contained statement. May be autoformatted depending on configuration.
raw_code: str
property
¶
Raw code of contained statement as a string.
Functions¶
evaluate(scope: dict) -> RawResult
¶
Evaluate code statement and produce a RawResult dataclass instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scope
|
dict
|
scope to use for evaluation |
required |
Returns:
| Name | Type | Description |
|---|---|---|
RawResult |
RawResult
|
Dataclass instance holding evaluation results. |
Source code in reprexlite/reprexes.py
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 | |
Functions¶
reprex(input: str, outfile: Optional[Union[str, os.PathLike]] = None, print_: bool = True, terminal: bool = False, config: Optional[ReprexConfig] = None, **kwargs) -> Reprex
¶
A convenient functional interface to render reproducible examples of Python code for
sharing. This function will evaluate your code and, by default, print out your code with the
evaluated results embedded as comments, formatted with additional markup appropriate to the
sharing venue set by the venue keyword argument. The function returns an instance of
Reprex which holds the relevant data.
For example, for the gh venue for GitHub Flavored Markdown, you'll get a reprex whose
formatted output looks like:
```python
x = 2
x + 2
#> 4
```
<sup>Created at 2021-02-15 16:58:47 PST by [reprexlite](https://github.com/jayqi/reprexlite)</sup>
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
str
|
Input code to create a reprex for. |
required |
outfile
|
Optional[str | PathLike]
|
If provided, path to write formatted reprex output to. Defaults to None, which does not write to any file. |
None
|
print_
|
bool
|
Whether to print formatted reprex output to console. |
True
|
terminal
|
bool
|
Whether currently in a terminal. If true, will automatically apply code highlighting if pygments is installed. |
False
|
config
|
Optional[ReprexConfig]
|
Instance of the configuration dataclass. Default of none will instantiate one with default values. |
None
|
**kwargs
|
Configuration options from ReprexConfig. Any provided values will override values from provided config or the defaults. |
{}
|
Returns:
| Type | Description |
|---|---|
Reprex
|
(Reprex) Reprex instance |
Source code in reprexlite/reprexes.py
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 | |