https://github.com/joeyespo/hack-function
"Goto Definition" for the Python Interpreter
https://github.com/joeyespo/hack-function
debugging-tools productivity python python-3 source-code tools
Last synced: 8 months ago
JSON representation
"Goto Definition" for the Python Interpreter
- Host: GitHub
- URL: https://github.com/joeyespo/hack-function
- Owner: joeyespo
- License: mit
- Created: 2018-02-10T22:34:04.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-12T06:01:41.000Z (almost 8 years ago)
- Last Synced: 2025-04-22T17:18:48.191Z (9 months ago)
- Topics: debugging-tools, productivity, python, python-3, source-code, tools
- Language: Python
- Size: 6.84 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Hack - "Goto Definition" for the Python Interpreter
===================================================
[](http://pypi.python.org/pypi/hack-function/)
[](https://saythanks.io/to/joeyespo)
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](#configuring) 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()`.
#### 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
------------
```bash
$ pip install hack-function
```
After installing, the `hack` function will be available globally.
Usage
-----
View the source of an object in your code editor:
```py
>>> import os
>>> hack(os.path.isabs)
```
You can also view it directly in the interpreter:
```py
>>> 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:
```py
>>> hack(os.path.join, 'path')
/usr/lib/python3.5/posixpath.py:62
```
Each action can also be aliased by its first character:
```py
>>> hack(os.path, 'p')
/usr/lib/python3.5/posixpath.py
```
And some actions can be configured further:
```py
>>> 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:
```py
>>> 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:
```bash
$ EDITOR="vi +\$LINE \$FILE" python
>>> hack(hack)
```
Windows:
```batch
>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](https://twitter.com/joeyespo).