Add some typing annotations.
pyupgrade.
Run black.
pyopen
is a tool to open Python files by import path.
If you've ever had to read the source code for a Python object, you've probably done something like this:
Python 3.7.4 (default, Jul 16 2019, 07:12:58)
[GCC 9.1.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.get.__module__
'requests.api'
>>> requests.api
<module 'requests.api' from '/usr/lib/python3.7/site-packages/requests/api.py'>
Or you guessed the path, and tabbed-completed your way there, and then found out you
need to hop around a bit to actually find it. pyopen
instead lets you do this:
nathan@nathan ~ $ pyvim requests.get
Which will change to the directory containing the found file, and open it
relatively with $EDITOR to the definition of the get
function.
Opening it relative-wise is helpful for navigating the rest of the module, so it is done by default.
You can even use this to get at method definitions on classes:
$ pyopen.py pathlib.Path.iterdir --verbose --absolute-open
kak /usr/lib/python3.7/pathlib.py +1084
Clone the repository to somewhere sensible, and modify your $PATH to include
the bin
directory of this project. For example:
$ cd projects
$ git clone https://git.sr.ht/~nhoad/pyopen
$ echo 'export PATH=~/projects/pyopen/bin:$PATH' >> ~/.bashrc
pyopen.py --help has some options that should be useful in integrating with your current workflow.
Usage: pyopen.py [options]
Options:
-h, --help show this help message and exit
-d, --print-dir Print directory to standard out rather than opening the
file.
-l, --print-path Print absolute file path to standard out rather than opening
opening the file.
-a, --absolute-open Open the file using the absolute path rather than
changing to the parent directory.
-v, --verbose Display commands before they're run.
Easy! Add a function to your .bashrc
(or equivalent).
pycd() {
cd `pyvim --print-dir $1`
}
There are certain editors that embed Python, for example Vim. If you try
jumping to a package that contains modules with the same names as builtins
(the most common seems to be site
) then your editor will likely break.
The fix for this is to use --absolute-open
, which doesn't change to the
containing directory. This has the disadvantage that you can't easily browse
the rest of a given package, but it means you can still view that one file.