1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- builtin which py > /dev/null || alias py='python3'
- 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
- # Check if command exists
- (( ${+commands[$python]} )) || continue
- # Get minor release version.
- # The patch version is variable length, truncate it.
- version=${(M)${"$($python -V 2>&1)":7}#[^.]##.[^.]##}
- # Add version specific path, if:
- # - it exists in the filesystem
- # - it isn't in $PYTHONPATH already.
- 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='python3 -c "import IPython, sys; sys.exit(IPython.start_ipython())"'
- alias pyserver="python3 -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}"
- }
|