Have you ever wondered how to get Python source code quickly? It turns out the Standard Library can do this pretty effortlessly. Here is some code to read more Python source.
The source / project is here:
import argparse import importlib import inspect import pydoc
Here is where the magic happens (credit to this Stack Overflow thread): we use
importlib to import the module from a string and get the class or function from this imported module using
# src/pysource.py def get_callable(arg): module_str, name = arg.rsplit(".", 1) module = importlib.import_module(module_str) return getattr(module, name)
Then we print it out. By default we use
True we use
pydoc.pager which works like Unix
more: it waits for you to press spacebar and you can use
/ for searching / string matching, pretty cool!
# src/pysource.py def print_source(func, pager=False): output = pydoc.pager if pager else print output(inspect.getsource(func))
src/__main__.py we handle command line arguments. We require a
module(.submodule).name for which we want to see the source and we have an optional
pager argument. Then we call the two functions:
# src/__main__.py def main(): parser = argparse.ArgumentParser(description='Read Python source.') parser.add_argument("-m", "--module", required=True, dest='module', help='module(.submodule).name') parser.add_argument("-p", "--pager", action='store_true', dest='use_pager', help='page output (like Unix more command)') args = parser.parse_args() func = get_callable(args.module) print_source(func, pager=args.use_pager)
sys.argv in my first iteration, but
argparse makes this so much more cleaner and extensible! Of course there are more options as well ...)
I stored this script in my
$HOME/bin folder and Michael was so kind to come up with an alias to use it in Vim:
autocmd FileType python map <leader>py :exec '!python3.9 $HOME/bin/pysource.py -m <C-R><C-A> -p'<CR>
He even made this nice little gif demo how it calls the script when you press
<leader>py when you are on
Here we actually wanted to know how
pathlib did operator overloading (answer: it implements
Of course you can also just call it from the command line:
$ pysource -m re.match def match(pattern, string, flags=0): """Try to apply the pattern at the start of the string, returning a Match object, or None if no match was found.""" return _compile(pattern, flags).match(string)
Or when using paging:
$ pysource -m pathlib.PurePath -p
By the way, lesson learned: call your source folder something relevant because that is how it ends up in your virtual environment's
Try it out yourself! Just install it like
pip install pybites-pysource (not to be confused with
pysource which is another / unrelated package on PyPI, thanks Nils).
This is still experimental but I hope it will already make it easier for you to read more Python (Standard Library) code.
Keep Calm and Code in Python!
Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.
"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H
"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B
"This is now my favourite first Python go-to reference." - Anthony L
"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S