Skip to content

Instantly share code, notes, and snippets.

Avatar
🐞

Ben Lopatin bennylope

🐞
View GitHub Profile
View annotated.py
from functools import wraps
def annotated(annotation_name):
"""
Decorator for returning pre-calculated, annotated values
This should be used with model instance methods that fetch some
kind of related or calculated data. If the method is called on
a single instance in isolation, we should expect the method to
View remotefile.py
import requests
class RemoteFile:
"""
Context manager for handling remote OR local files
Can be used like so:
>>> with RemoteFile("/path/to/local/file.txt") as f:
@bennylope
bennylope / console.js
Created May 27, 2020
Download your Papertrail gzipped archives
View console.js
/*
Copy and paste into your browser console window after loading
your archives page, e.g. with a selected long date range.
Original: https://www.reddit.com/r/javascript/comments/5my92r/console_script_to_click_all_download_links_on_a/dc7cktr/
The problem I had with my first attempt was that there was no delay,
so I was only downloading the last entry on the page. The linked
solution above pointed to the problem (each subsequent request was
killing the preceding request) which was including a delay. Here
@bennylope
bennylope / stripe_subscription_stream.py
Created Feb 28, 2020
Common (?) pattern for consuming paged objects as a stream in Python, using Stripe Subscriptions as an example
View stripe_subscription_stream.py
import functools
import stripe
from typing import Any, Optional
def stripe_subscriptions(limit: Optional[int] = 100, **params: Any):
"""
Returns an iterator of all matching subscriptions
View install.sh
#!/usr/bin/env bash
PACKAGES=(pagecolor titling sourcecodepro mweights ly1 sourcesanspro needspace mdframed csquotes)
for item in ${PACKAGES[*]}
do
tlmgr install $item
done
@bennylope
bennylope / create_db_restore.sh
Last active Aug 20, 2020
Create a named database and restore from the lastest downloaded file
View create_db_restore.sh
#!/usr/bin/env bash
LATEST_DB="$HOME/Downloads/$(ls -t ~/Downloads | head -1)"
BACKUP_PATH=${2-$LATEST_DB}
DB=$1
echo "Creating database name $DB from $BACKUP_PATH"
psql -c "CREATE DATABASE $DB;"
View mutable_cached_property.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bennylope
bennylope / annotated.py
Created Sep 8, 2018
A Django model instance method decorator for creating queryset annotation compatibility.
View annotated.py
def annotated(attr_name):
"""
Decorator for returning pre-calculated, annotated values
This should be used with model instance methods that fetch some
kind of related or calculated data. If the method is called on
a single instance in isolation, we should expect the method to
execute and return its value. However if the method is called
on a member of a queryset where the `attr_name` was added as an
View numpy_append.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@bennylope
bennylope / drain.rb
Last active Jul 12, 2018
A funky little script to download Drip broadcast messages and create Jekyll-ready Markdown files. See https://github.com/bennylope/drain.rb for updated version.
View drain.rb
#!/usr/bin/env ruby
# A little Ruby script to download your Drip broadcast messages to Jekyll-ready
# Markdown files. These could be used as posts or, better yet, as a collection.
# Inspired by Jonathan Stark's Drain https://github.com/jonathanstark/drain/
#
# Copyright Ben Lopatin, 2018
# Shared with an MIT license: https://opensource.org/licenses/MIT
# Since this is a standalone script, installing the dependencies is your responsibility