Skip to content

joeyespo/hack-function

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hack - "Goto Definition" for the Python Interpreter

Current version on PyPI Say Thanks!

Exposes a hack function to "hack open" an object from the Python Interpreter, which loads the object's source file at the line its defined on using the default or configured text editor. It can also print out or return a string of this metadata instead.

Why?

Sometimes you just need to read the source.

The Python Interpreter, dir, and help are fantastic tools for tinkering and sanity checking. However, at times the only way to gain a better understanding is to see what's going on below the surface. Spending the time searching or tracking down where an object is defined can be tedious and distracting.

Now all you have to remember is hack(<object>).

Use cases

  • Read the source of a function when its documentation is unclear or incomplete
  • Quickly jump to a package you're developing when tinkering in the interpreter
  • Explore a dependency to confirm a bug without having to learn its file layout

Installation

$ pip install hack-function

After installing, the hack function will be available globally.

Usage

View the source of an object in your code editor:

>>> import os
>>> hack(os.path.isabs)

You can also view it directly in the interpreter:

>>> hack(os.path.isabs, 'source')
def isabs(s):
    """Test whether a path is absolute"""
    s = os.fspath(s)
    s = splitdrive(s)[1]
    return len(s) > 0 and s[0] in _get_bothseps(s)

Or print just the path and line number:

>>> hack(os.path.join, 'path')
/usr/lib/python3.5/posixpath.py:62

Each action can also be aliased by its first character:

>>> hack(os.path, 'p')
/usr/lib/python3.5/posixpath.py

And some actions can be configured further:

>>> out = hack(os.path.isabs, 's', capture=True)
>>> out.split('\n')[0]
'def isabs(s):\n'

Take a look at the hack source code for more details:

>>> hack(hack)

Configuring

By default, hack takes a best-guess of which text editor to use. You can override this by setting one of the following to your editor's run command:

  • The contents of ~/.hack-function
  • The EDITOR environment variable
  • The HACK_FUNCTION_EDITOR environment variable

In the above, $FILE will be replaced with the filename to open and $LINE with the line number. If these aren't preset, the filename will be appended to the command.

Examples

Linux and Mac:

$ EDITOR="vi +\$LINE \$FILE" python
>>> hack(hack)

Windows:

>SET EDITOR="C:\Program Files\Sublime Text 3\sublime_text.exe" $FILE:$LINE
>py
>>> hack(hack)

Or persist one of the above EDITOR values in a ~/.hack-function file.

Additional notes

  • hack will not work on builtin objects and modules (since they're likely implemented outside of Python code)
  • hack uses the inspect module internally (look here if you need programmatic access to this information)

Contributing

  1. Check the open issues or open a new issue to start a discussion around your feature idea or the bug you found
  2. Fork the repository and make your changes
  3. Open a new pull request

If your PR has been waiting a while, feel free to ping me on Twitter.

About

"Goto Definition" for the Python Interpreter

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages