Last active
December 14, 2020 17:59
-
-
Save pybites/87318a06c8cfef8b40ddd1967768a446 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3.9 | |
import argparse | |
import importlib | |
import inspect | |
import pydoc | |
def get_callable(arg): | |
module_str, name = arg.rsplit(".", 1) | |
module = importlib.import_module(module_str) | |
return getattr(module, name) | |
def print_source(func, pager=False): | |
output = pydoc.pager if pager else print | |
output(inspect.getsource(func)) | |
if __name__ == "__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) |
added a pager with pydoc.pager
and argparse
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This SO answer helped a lot: https://stackoverflow.com/a/8790232