# Function to activate virtualenv if present
auto_activate_virtualenv() {
if [ -f "bin/activate" ]; then
source bin/activate
fi
}
# Add the function to chpwd hooks
add-zsh-hook chpwd auto_activate_virtualenv
How to indicate that a single file module is typed to mypy
You can’t. Repackage your module as the only module in a package.
mypy needs a py.typed in the package’s directory and if you have a single module at the top level of your repo there’s no way to get setuptools to package that in such a way that the wheel will contain the py.typed in a useful place.
Example of the changes needed: github.com/wheerd/multiset/pull/127/files
Remember the Milk Alfred Workflow
This uses rtm-cli which you can install with npm.
query=$1 logfile="/Users/bakert/u/scratch/rtm.log" rtm=/opt/homebrew/bin/rtm max_lines=1000 # Log the query echo "[$(date)] $query" >> "$logfile" # Ensure the logfile does not grow beyond $max_lines tail -n $max_lines "$logfile" > "$logfile.tmp" && mv "$logfile.tmp" "$logfile" # Send the task to Remember the Milk "$rtm" add $query if [ $? -ne 0 ]; then echo "SOMETHING WENT WRONG! Please check that rtm-cli is installed: npm install -g rtm-cli" else echo "[Added] $query" fi
Gmail Conversations Archiving Has Gotten Worse
assertRaises in table-driven tests
This is what I want to do but it fails passing None to assertRaises:
tests = [
(0, None),
(1, None),
(-1, TooFewException),
(99, None),
(100, TooManyException),
]
for n, exc in tests:
with self.assertRaises(exc):
results = my_code(n)
assert len(results) == n
Here’s a version of assertRaises that will let you do that:
def assert_raises(self, exception: Type[Exception]):
if exception:
return self.assertRaises(exception)
return contextlib.nullcontext()
Line Count
Number of lines in a (python + js) codebase:
#!/bin/zsh
# This script counts the number of non-blank lines of code in a directory and its subdirectories
# for Python and JavaScript code only
# Set the directory to search for code files
dir='.'
# Count the number of non-blank lines of code for Python and JavaScript files
num_lines=$(find "$dir" -type f \( -name '*.py' -or -name '*.js' \) -not -path '*venv*' -not -path '*node_modules*' -exec grep -he '^[^[:space:]]' {} + | awk 'NF{count++} END{print count}')
echo "Number of non-blank lines of code: $num_lines"
Number of lines per-file in a (python + js) codebase:
#!/bin/zsh # This script counts the number of non-blank lines of code in a directory and its subdirectories # for Python and JavaScript code only # Set the directory to search for code files dir='.' # Count the number of non-blank lines of code for Python and JavaScript files num_lines=$(find "$dir" -type f \( -name '*.py' -or -name '*.js' \) -not -path '*venv*' -print0 | xargs -0 grep -cve '^[[:space:]]* ) echo "Number of non-blank lines of code: $num_lines"
The Surprising Behavior of urljoin
>>> from urllib.parse import urljoin
>>> urljoin('http://example.com/', '1')
'http://example.com/1'
>>> urljoin('http://example.com/', '2629:1828:4025')
'2629:1828:4025'
Better not try to urljoin anything with a colon in it!
Queries on local so much faster than queries on server
I was encountering a situation where big aggregating queries that took seconds on local were taking minutes or even hours on prod. After trying about a million things this Stack Exchange post finally cleared things up.
Setting innodb_buffer_pool_size = 5G in MariaDB’s configuration and restarting instantly changed the queries to taking seconds. The default is 128M which is … not useful 🙂
Change Terminal Background Color When ssh’ed – Mac
Save this as “ssh” somewhere on your $PATH ahead of /usr/bin/ssh and make it executable
#!/bin/sh
HOSTNAME=$(echo $@ | sed 's/.*@//')
set_theme () {
osascript -e "tell application \"Terminal\" to set current settings of first window to settings set \"$1\""
}
on_exit () {
set_theme "Solarized Light"
}
trap on_exit EXIT
case $HOSTNAME in
# These all do the same thing but we could have different themes for different locations
hb|pd|decksite) set_theme "Solarized Dark" ;;
*) set_theme "Solarized Dark" ;;
esac
/usr/bin/ssh "$@"


