-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path.python-functions
173 lines (149 loc) · 4.28 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# what are the system paths?
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 \
ipython-autoimport \
IPythonClipboard \
ipython_ctrlr_fzf \
docrepr \
pyfzf \
jedi \
pretty-traceback \
pre-commit \
sqlparse \
git+https://github.com/iloveitaly/ipython-suggestions.git@ipython-9.x \
rpdb \
datamodel-code-generator \
funcy-pipe \
colorama \
pipdeptree \
icecream \
httpdbg"
# TODO can reference local versions of the plugin via: $HOME/Projects/python/ipython-suggestions \
# install my favorite list of helpful py development packages in a custom group
function py-debugging-install() {
if [ ! -f "uv.lock" ]; then
pip install --upgrade pip
fi
if [ -f "uv.lock" ]; then
uv add --group debugging-extras ${(s: :)PYTHON_DEBUGGING_PACKAGES}
else
pip install --upgrade --force-reinstall "$PYTHON_DEBUGGING_PACKAGES"
fi
python-inject-startup
}
# start an ipython session, with `playground.py` if it exists
function ipy() {
if [ -f "playground.py" ]; then
if [ -f "uv.lock" ]; then
uv run $(echo "$PYTHON_DEBUGGING_PACKAGES" | sed 's/[^ ][^ ]*/--with &/g') ipython -i playground.py
else
chmod +x playground.py
./playground.py
fi
else
if [ -f "uv.lock" ]; then
uv tool run $(echo "$PYTHON_DEBUGGING_PACKAGES" | sed 's/[^ ][^ ]*/--with &/g') ipython
else
pip install --upgrade ${=PYTHON_DEBUGGING_PACKAGES}
ipython
fi
fi
}
# run python, inside of uv if it exists
function py() {
if [ -f "uv.lock" ]; then
uv run python "$@"
else
python "$@"
fi
}
function venv-pristine() {
if [ -f "uv.lock" ]; then
rm -rf .venv
uv sync
else
poetry env remove --all -n && poetry install && (poetry shell && py-debugging-install)
fi
}
# what does this do?
# 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
if [[ -n "$package_path" ]]; then
cd $package_path
code $package_path
else
echo "Package not found: $1"
fi
}
# 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
cat <<'EOF' >"$py_file"
def run_startup_script():
try:
import pretty_traceback
pretty_traceback.install()
except ImportError:
pass
try:
from icecream import install
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"
}