From e4f32ffdeca7959933787d9db71e0f40c988d5dd Mon Sep 17 00:00:00 2001 From: Akemi Izuko Date: Thu, 8 Dec 2022 20:54:20 -0700 Subject: [PATCH] Add python manpage script --- bash/.bash_functions | 4 +++ bin/man_py.sh | 69 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 bin/man_py.sh diff --git a/bash/.bash_functions b/bash/.bash_functions index 4537627..c171aae 100644 --- a/bash/.bash_functions +++ b/bash/.bash_functions @@ -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: diff --git a/bin/man_py.sh b/bin/man_py.sh new file mode 100755 index 0000000..00a444e --- /dev/null +++ b/bin/man_py.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash +# `man` for python functions +# +# A few libraries are imported by default: +# /[alias] +# numpy/np +# pandas/pd +print_help() { + cat < + +Options: + --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