inspector
This module provides introspection utilities.
The inspect
method can be used to create an Inspector
widget, which can
then be used to see what is happening inside any python object. This method is
usually preferred for instantiating an Inspector
, as it sets up overwriteable default
arguments passed to the new widget.
These defaults are meant to hide the non-important information when they are not needed,
in order to allow the least amount of code for the most usability. For example, by
default, when passed a class, inspect
will clip the docstrings to their first lines,
but show all methods. When an class' method is given it will hide show the full
docstring, and also use the method's fully qualified name.
Inspector
Bases: Container
A widget to inspect any Python object.
Source code in pytermgui/inspector.py
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 224 225 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 445 446 447 448 449 450 451 452 453 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 |
|
__init__(target=None, show_private=False, show_dunder=False, show_methods=False, show_full_doc=False, show_qualname=True, show_header=True, **attrs)
Initializes an inspector.
Note that most of the time, using inspect
to do this is going to be more
useful.
Some styles of the inspector can be changed using the code.name
,
code.file
and code.keyword
markup aliases. The rest of the
highlighting is done using pprint
, with all of its respective colors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
show_private |
bool
|
Whether |
False
|
show_dunder |
bool
|
Whether |
False
|
show_methods |
bool
|
Whether methods should be shown when encountering a class. |
False
|
show_full_doc |
bool
|
If not set, docstrings are cut to only include their first line. |
False
|
show_qualname |
bool
|
Show fully-qualified name, e.g. |
True
|
show_header |
bool
|
If not set, the header containing the path to the object and its qualname will not be added. |
True
|
Source code in pytermgui/inspector.py
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 224 225 226 227 228 229 230 |
|
debug()
Returns identifiable information used in repr.
Source code in pytermgui/inspector.py
474 475 476 477 478 479 480 |
|
highlight(text)
staticmethod
Applies highlighting to a given string.
This highlight includes keywords, builtin types and more.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text |
str
|
The string to highlight. |
required |
Returns:
Type | Description |
---|---|
str
|
Unparsed markup. |
Source code in pytermgui/inspector.py
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 |
|
inspect(target)
Inspects a given object, and sets self.target to it.
Returns:
Type | Description |
---|---|
Inspector
|
Self, with the new content based on the inspection. |
Source code in pytermgui/inspector.py
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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 |
|
ObjectType
Bases: Enum
All types an object can be.
Source code in pytermgui/inspector.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
|
BUILTIN = _auto()
class-attribute
instance-attribute
Some sort of a builtin object.
As builtins are often implemented in C, a lot of the standard python APIs won't work on them, so we need to treat them separately.
CLASS = _auto()
class-attribute
instance-attribute
A class object.
FUNCTION = _auto()
class-attribute
instance-attribute
A callable object, that is not a class.
LIVE = _auto()
class-attribute
instance-attribute
An instance that does not fit the other types.
MODULE = _auto()
class-attribute
instance-attribute
A module object.
get_origin(_)
Spoofs typing.get_origin, which is used to determine type-hints.
Since this function is only available >=3.8, we need to have some implementation on it for 3.7. The code checks for the origin to be non-null, as that is the value returned by this method on non-typing objects.
This will cause annotations to show up on 3.7, but not on 3.8+.
Source code in pytermgui/inspector.py
38 39 40 41 42 43 44 45 46 47 48 49 |
|
inspect(target, **inspector_args)
Inspects an object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
target |
object
|
The object to inspect. |
required |
**inspector_args |
Any
|
See |
{}
|
Source code in pytermgui/inspector.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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
|