Skip to content

Instantly share code, notes, and snippets.

@aaugustin
aaugustin / djangoauth.py
Created July 29, 2014 12:49
WSGI middleware that authenticates against a Django user database.
"""
WSGI middleware that authenticates against a Django user database.
DJANGO_SETTINGS_MODULE should point to a valid Django settings module.
In addition, the following settings are available:
- BASIC_AUTH_LOGIN_URL: DjangoAuth will trigger basic authentication on this
URL. Since browsers only propagate auth to resources on the same level or
below, this URL will usually be '/<something>' without a trailing slash.
@aaugustin
aaugustin / gameaboutsquares.py
Last active November 4, 2015 05:02
Quick'n'dirty solver for http://gameaboutsquares.com/.
#!/usr/bin/env python
# Copyright (c) 2014 Aymeric Augustin
# Released under the WTFPLv2 - http://www.wtfpl.net/txt/copying/
"""
Quick'n'dirty solver for http://gameaboutsquares.com/.
Install requests or save http://gameaboutsquares.com/game.c.js next to this
file. Then run: python gameaboutsquares.py. Tested with Python 2.7 and 3.4.
<!DOCTYPE html>
<html>
<head>
<title>Vega - bug 259</title>
<script src="http://vega.github.io/vega/lib/d3.v3.min.js"></script>
<script src="http://vega.github.io/vega/vega.min.js"></script>
</head>
<body>
<div id="chart"></div>
<script type="text/javascript">
#!/usr/bin/env python3.4
"""Git pre-commit hook"""
import os
import subprocess
import sys
def run_linter(cmd, files):
@aaugustin
aaugustin / question.md
Last active January 4, 2023 05:04
Accessibility in browsers: zoom level vs. font size

Scroll to the bottom for the answer

Question

There's two ways to increase the default font size in browsers:

  1. set a default zoom level > 100% ("page zooming")
  2. set a default font size > 16px ("text scaling")

Option 1 relies on the browser's proportional scaling. This feature was

@aaugustin
aaugustin / isValidDate.js
Created February 15, 2017 15:12
isValidDate in JavaScript
export const DAYS_IN_MONTH = [null, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
function daysInMonth(year, month) {
// isValidDate checked that year and month are integers already.
// February of leap years. Assumes that the Gregorian calendar extends
// infinitely in the future and in the past.
if (month === 2 && (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0))) {
return 29
}
@aaugustin
aaugustin / dates.js
Created April 21, 2017 09:31
Date arithmetic in JavaScript
export function addYears(date, numYears) {
// Make a copy to avoid mutating the date argument
date = new Date(date.getTime())
// If adding years to a February 29th ends up in a non-leap year, this
// returns March 1st, which is the expected result.
date.setFullYear(date.getFullYear() + numYears)
// DST can also mess with us. Thanks, JavaScript, for not having dates.
if (date.getHours() === 23) {
@aaugustin
aaugustin / compression.py
Last active December 30, 2018 20:18
WebSocket compression benchmark
#!/usr/bin/env python
import getpass
import json
import pickle
import subprocess
import sys
import time
import zlib
@aaugustin
aaugustin / powermate.py
Last active January 25, 2022 14:59
Hook a Griffin PowerMate to a Raspberry Pi running HifiBerry!
#!/usr/bin/env python3.8
"""
To enable this service, copy this file to /opt, then:
# chmod +x /opt/powermate.py
# pip3.8 install websockets
# cat > /etc/systemd/system/powermate.service
[Unit]
Description=PowerMate
@aaugustin
aaugustin / check_media_files.py
Created November 20, 2021 18:00
Locate and remove unreferenced media files.
#!/usr/bin/env python
"""
Locate and remove unreferenced media files.
Expects DJANGO_SETTINGS_MODULE to be set in the environment.
"""
import collections