-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path.python-functions
118 lines (95 loc) · 3.34 KB
/
.python-functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
alias py-paths="python -c 'import sys; print(sys.path)'"
PYTHON_DEBUGGING_PACKAGES="ipython pudb debugpy git+https://github.com/iloveitaly/ipdb@support-executables \"pdbr[ipython]\" rich git+https://github.com/anntzer/ipython-autoimport.git IPythonClipboard ipython_ctrlr_fzf docrepr pyfzf jedi pretty-traceback pre-commit sqlparse ipython-suggestions rpdb datamodel-code-generator funcy-pipe colorama pipdeptree"
# # TODO https://github.com/anntzer/ipython-autoimport/issues/11
function py-debugging-install() {
if [ ! -f "uv.lock" ]; then
pip install --upgrade pip
fi
if [ -f "uv.lock" ]; then
uv pip install --upgrade --force-reinstall "$PYTHON_DEBUGGING_PACKAGES"
else
pip install --upgrade --force-reinstall "$PYTHON_DEBUGGING_PACKAGES"
fi
python-inject-startup
if command -v poetry &>/dev/null; then
poetry self add poetry-plugin-up
fi
}
function ipy() {
if [ -f "uv.lock" ]; then
uv tool run $(echo "$PYTHON_DEBUGGING_PACKAGES" | sed 's/[^ ][^ ]*/--with &/g') ipython
else
ipython
fi
}
# pbpaste | python3 -c 'import sys, re; print(re.sub(r"^[\"]|[\"]$", "", sys.stdin.read().replace("\\n", "\n")).strip())'
# find the location of a pip package
pip-show() {
package_name=${1//-/_}
package_path=$(find --no-ignore-vcs -t d --glob "*${package_name}*" -E "*dist-info*" .venv)
echo $package_path
cd $package_path
code $package_path
}
# TODO should make it more flexible: check for venv and different py versions
py-site-packages() {
cd .venv/lib/python3.13/site-packages/
}
# launch a py executable with ipdb3 to launch a debugger automatically
pyd() {
local executable=$1
if ! [ -f "$executable" ]; then
executable=$(which "$executable")
fi
echo "$executable ${@:2}"
eval "ipdb3 -c continue -- $executable ${@:2}"
}
# when hacking on a package with `pip-show` this will properly wipe + reinstall the package
# also allows installing a package from a git repo
pip-pristine() {
if [[ $1 == http* ]]; then
# Fetch pyproject.toml from GitHub using Python's requests library
package_name=$(python -c "
import requests
import toml
from io import StringIO
try:
response = requests.get('$1/raw/main/pyproject.toml')
response.raise_for_status()
except requests.exceptions.HTTPError:
# If fetching from the 'main' branch fails, try the 'master' branch
response = requests.get('$1/raw/master/pyproject.toml')
data = StringIO(response.text)
config = toml.load(data)
print(config['tool']['poetry']['name'])
")
pip uninstall -y $package_name
pip cache remove $package_name
else
pip uninstall -y $1
pip cache remove $1
fi
pip install $1
}
# create a file to automatically import pretty-traceback on startup
python-inject-startup() {
local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")
local pth_file=$site_packages/mbianco_injection.pth
local py_file=$site_packages/_mbianco_injection.py
if [[ -f "$pth_file" ]]; then
echo "python startup already exists: $target_file"
return 1
fi
cat <<'EOF' >"$py_file"
def run_startup_script():
try:
import pretty_traceback
pretty_traceback.install()
except ImportError:
pass
run_startup_script()
EOF
# the pth file must have a single line, so it's easier to import another file
echo "import _mbianco_injection" >"$pth_file"
echo "Python startup injection created: $pth_file"
}