alias py='python'
alias pyfind='find . -name "*.py"'
function pyclean() {
find "${@:-.}" -type f -name "*.py[co]" -delete
find "${@:-.}" -type d -name "__pycache__" -delete
find "${@:-.}" -depth -type d -name ".mypy_cache" -exec rm -r "{}" +
find "${@:-.}" -depth -type d -name ".pytest_cache" -exec rm -r "{}" +
}
function pyuserpaths() {
setopt localoptions extendedglob
local user_base="${PYTHONUSERBASE:-"${HOME}/.local"}"
local python version site_pkgs
for python in python2 python3; do
(( ${+commands[$python]} )) || continue
version=${(M)${"$($python -V 2>&1)":7}#[^.]##.[^.]##}
site_pkgs="${user_base}/lib/python${version}/site-packages"
[[ -d "$site_pkgs" && ! "$PYTHONPATH" =~ (^|:)"$site_pkgs"(:|$) ]] || continue
export PYTHONPATH="${site_pkgs}${PYTHONPATH+":${PYTHONPATH}"}"
done
}
alias pygrep='grep -nr --include="*.py"'
alias ipython="python -c 'import IPython; IPython.terminal.ipapp.launch_new_instance()'"
alias pyserver="python -m http.server"
function vrun() {
local name="${1:-venv}"
local venvpath="${name:P}"
if [[ ! -d "$venvpath" ]]; then
echo >&2 "Error: no such venv in current directory: $name"
return 1
fi
if [[ ! -f "${venvpath}/bin/activate" ]]; then
echo >&2 "Error: '${name}' is not a proper virtual environment"
return 1
fi
. "${venvpath}/bin/activate" || return $?
echo "Activated virtual environment ${name}"
}
function mkv() {
local name="${1:-venv}"
local venvpath="${name:P}"
python3 -m venv "${name}" || return
echo >&2 "Created venv in '${venvpath}'"
vrun "${name}"
}