Skip to main content

Deep inspection of Python objects

Project description

🙀 WAT

Deep inspection of Python objects.

WAT is a powerful inspection tool that allows you to delve into and examine unknown objects at runtime.

"Wat" is a variant of the English word "what" that is often used to express confusion or disgust

If you find yourself deep in the Python console, feeling lost and confused, and wondering "WAT? What is this thing?", that's where the wat inspector comes in handy.

Start the Python Interpreter (or attach to one) and execute wat / object on any object to investigate its type, formatted value, variables, methods, parent types, signature, documentation, and even its source code. Alternatively, you can use wat(object) syntax.

Import

Insta-Load

If you want to quickly debug something, you can use this inspector without installing anything, in the same session.

Load it on the fly by pasting this snippet to your Python interpreter:

import base64, zlib
code = b'eJzVW+tu3MYV/u+nGLg/yLU3WyluU0AJ3Sq26gZw4kJRGhiyQXCXsxJjLrkguZbUxQJ5iD5DHixP0nOZO8ldWcmPVoAlcmbON+ecOXMuw/GyqVciz7psUWZtK1tRrNZ109mmR6qhqNq1XHT6tTYdjdRP7V37aIl43d26qK401Gl1NxVv1l1RV1k5FRd3a/nobxaffotvGP5FXS2Lq5NHAn7aa6A+EfO6Luk931S5bJyGqs7rRes0lHV15bwu6ly6r1lZGvoxBk67rinmm04yD1W2Aoi2a+jtY1Zu4BUEolcQE95IHo2fzUu5j+V1U3zMOndIW1xVWbdpoE3r6BLme8/09SJszuVSr0W6rJtV1sU0tJ7/NKWHJ1OrveTvWdnKqcOL28L6c1tQge47atB7JxW6LdCgXycnigaXMPEWNGZu6PdUc8J/RN0gyFRzw3+mzAr+MgOIGfxlW5gd/qNaJ6yNTbfedElZtF2c3hWyzFOttLKoZBujuhSrEyYplmjAs7bLgXhWtFnX3cUTkVW5SBd1WTeprHB581gJSiYgm1UBa5PeFHl3ndTt7Ep2qWlti3/LeDID8s2qag0VczcDjmTTxUdT8f3F29dn6devfzgTT0X0639+jsSTABs6zs++P7uYhCjZei2rPP5kiE7edkn0ropmP9VFFaNaBBiUoIeiUvCoFm5pYYU68V1dSaMvbNinG5ggBaMt1jwoxhYmbiSYfEVDyKIPrpFyCV2T0iZMlO3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+9jtVhfRLOWfTdeLt6eOEF840p2+Ve2ff/nsL9Cu85LJfwGvPQFk'
exec(zlib.decompress(base64.b64decode(code)).decode(), globals())

Now you can use wat object.

[!Warning] Before executing Insta-Load snippet, it's recommended to verify what you're about to run. You can either:

  • Verify what's inside the extracted code beforehand:
    print(zlib.decompress(base64.b64decode(code)).decode())
    
  • Paste the content of inspection.py into your interpreter. It has the same effect.
  • Install package with pip and review the code.

Install with pip

Alternatively, install wat-inspector package and import inspection tool from wat module:

pip install wat-inspector
import wat

This package has no dependencies.

Usage & modifiers

wat object can quickly inspect things by using the division operator (for faster typing without parentheses). A short syntax wat / foo is equivalent to wat(foo).

You can call wat.modifiers / foo with the following modifiers:

  • .short or .s to hide the attributes (variables and methods inside the object)
  • .dunder to display dunder attributes (starting with __)
  • .code to reveal the source code of a function, method, or class
  • .long to show non-abbreviated values and docstrings
  • .nodocs to hide documentation for functions and classes
  • .caller to show how and where the inspection was called (works in files, not REPL)
  • .all to include all available information
  • .ret to return the inspected object
  • .str to return the output string instead of printing it
  • .gray to disable colorful output in the console

You can chain modifiers, e.g. wat.long.dunder.nodocs / foo.

Call wat.locals or wat() to inspect local variables.
Call wat.globals to inspect global variables.

Type wat in the interpreter to learn more about this object itself.

There are several alternative syntaxes that are equivalent. Choose the one that works best for you:

wat.short / 'foo'  # fast typing
wat.short('foo')
wat('foo', short=True)  # natural Python syntax
'foo' | wat.short  # Unix piping

Use Case Examples

Determine type

In a dynamic typing language like Python, it's often hard to determine the type of an object. WAT Inspector can help you with that by showing the name of the type with the module it comes from.

>>> wat.short / (1,)
value: (1,)
type: tuple
len: 1
>>> wat.short / {None}
value: {None}
type: set
len: 1
>>> wat.short / user
str: admin
repr: <User: admin>
type: django.contrib.auth.models.User
parents: django.contrib.auth.models.AbstractUser, django.contrib.auth.base_user.AbstractBaseUser, django.contrib.auth.models.PermissionsMixin, django.db.models.base.Model, django.db.models.utils.AltersData

Now that you've identified the actual type, you can put the type annotations in your code to reduce further confusion.

Look up methods

By listing out methods with their signatures and docstrings, you can easily grasp how to use the unknown object.

wat / ['foo']


Use wat.long if you want to see full doscstrings.

Discover function's signature

See the docstrings and the signature of a function to learn how to use it.

wat / str.split

Look up attributes

List the attribues and their types to see what's really inside the inspected object.

wat / re.match('(\d)_(.*)', '1_title')

Explore modules

Another use case is to explore modules. You can list the functions, classes and sub-modules of a selected module.

import pathlib
wat / pathlib

Then, you can navigate further, e.g. wat / pathlib.fnmatch.

Explore dunder attributes

By default, WAT Inspector hides attributes starting with __. Use wat.dunder to see them.

wat.dunder / {}

Review the code

Look up the source code of a function to see how it really works.

wat.code / wat.__call__

Prettify unreadable collections

Nested dictionaries and lists get nicely formatted, indented output:

Debug with breakpoint

You can use Python's breakpoint() keyword to launch an interactive debugger in your program. Attach to the interpreter and inspect things on the spot.

(Pdb) import wat  # or paste Insta-Load snippet
(Pdb) wat / foo  # inspect local variables
...
(Pdb) c  # continue execution

Look up local variables

Use wat.locals or wat.globals to look up the local and global variables respectively.

Learn Python

With these snippets you can better understand Python internals.

reversed([]) == reversed([])
# False
wat.s / reversed([])
# value: <list_reverseiterator object at 0x76749d1cb400>
# type: list_reverseiterator
wat / type('ObjectCreator', (), {})
# value: <class '__main__.ObjectCreator'>
# type: type
# signature: class ObjectCreator()

wat / type
# value: <class 'type'>
# type: type
# signature: class type(…)
# """
# type(object) -> the object's type
# type(name, bases, dict, **kwds) -> a new type
# """
# 
# Public attributes:
#   def mro(self, /) # Return a type's method resolution order.
from typing import List
wat.s / List[str]
# value: typing.List[str]
# type: typing._GenericAlias
# parents: typing._BaseGenericAlias, typing._Final
# signature: def List(*args, **kwargs)

wat(str | None)
# value: str | None
# type: types.UnionType

Explore Python built-ins:

wat / __builtins__
wat / ...

Inspect WAT itself

wat.dunder / wat
wat.code / wat.__truediv__

Environment variables

  • WAT_COLOR="false" to disable colorful output in the console.
  • WAT_COLOR="true" to enforce colorful outputs even in non-tty environment.

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wat_inspector-0.4.0.tar.gz (22.4 kB view details)

Uploaded Source

Built Distribution

wat_inspector-0.4.0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file wat_inspector-0.4.0.tar.gz.

File metadata

  • Download URL: wat_inspector-0.4.0.tar.gz
  • Upload date:
  • Size: 22.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for wat_inspector-0.4.0.tar.gz
Algorithm Hash digest
SHA256 4262ecdf1fc2ea8e5c35ae65ca1ae18b37d30061936e8bd2f4d6b4020cc05ff3
MD5 74ff19be024a6af2f917747057b5755b
BLAKE2b-256 46f957ec4544a1f47f144923a35ab405d6081ae26134e286a9a979f046e8bccd

See more details on using hashes here.

File details

Details for the file wat_inspector-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for wat_inspector-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2664ccc10ce22b6bc064be7316ea5c34d51fb1ff2d5f543c9ecef22226ad49dc
MD5 5b9a6ae78d1b5db6ec116fb6adae02b9
BLAKE2b-256 185151da11a88db681cd4d6be1f9d7607756cf0451453dea780981ba7af14a57

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page