Add python manpage script

This commit is contained in:
Akemi Izuko 2022-12-08 20:54:20 -07:00
parent ca1103c45e
commit e4f32ffdec
No known key found for this signature in database
GPG key ID: 905D444F6A5E4BE4
2 changed files with 73 additions and 0 deletions

View file

@ -57,6 +57,10 @@ viw() {
fi
}
manpy() {
~/.configs_pointer/bin/man_py.sh $@
}
# Ronald's Universal Number Kounter, standardized syntax for unix calculators
# BSD-style arguments. v for verbose. c/p/b for the corresponding calculator
# Syntax features:

69
bin/man_py.sh Executable file
View file

@ -0,0 +1,69 @@
#!/usr/bin/env bash
# `man` for python functions
#
# A few libraries are imported by default:
# <name>/[alias]
# numpy/np
# pandas/pd
print_help() {
cat <<HELP
Open the help page for a python function
Usage: $(basename $0) [options] <function>
Options:
--lib <lib> Specify a library to import
Numpy as np and Pandas as pd work by default. Prefixing with a library works
Examples:
$(basename $0) exit
$(basename $0) --lib os os.getcwd
$(basename $0) os.getcwd # Implicity imports \`os\`
$(basename $0) numpy.random.randint # Implicity imports \`numpy.random\`
$(basename $0) np.random.randint # np -> numpy, then same as above
HELP
}
declare lookup lib
declare -r py="/usr/bin/env python3"
# Parse args
while [[ "$#" -gt 0 ]]; do
case "$1" in
-h | --help)
print_help
exit 0
;;
--lib)
shift
if [[ -z "$1" ]]; then
print_help
exit 1
else
lib="$1"
fi
;;
*)
lookup="$1"
;;
esac
shift
done
# Expand aliases
lookup="$(echo "$lookup" | sed 's#^np\.#numpy.#')"
lookup="$(echo "$lookup" | sed 's#^pd\.#pandas.#')"
# Open manpage
if [[ -z "$lookup" ]]; then
print_help
elif [[ -n "$lib" ]]; then
$py -c "import $lib; help($lookup)"
elif [[ "$lookup" =~ \. ]]; then
$py -c "import ${lookup%.*}; help($lookup)"
else
$py -c "help($lookup)"
fi