Skip to content

Instantly share code, notes, and snippets.

@dcwatson
dcwatson / tabs2spaces.py
Last active April 16, 2018 15:57
A quick script to run through files in a directory changing leading tabs to spaces, trimming trailing whitespace, and converting line endings to UNIX (LF).
#!/usr/bin/env python
import argparse
import fnmatch
import os
import sys
DEFAULT_EXTENSIONS = ['py', 'css', 'html', 'js', 'txt', 'json', 'sh', 'sql', 'yml', 'yaml', 'xml']
@dcwatson
dcwatson / range_streaming.py
Last active May 6, 2023 20:10
Django streaming view accepting byte range requests
from django.http import StreamingHttpResponse
from wsgiref.util import FileWrapper
import mimetypes
import os
import re
range_re = re.compile(r'bytes\s*=\s*(\d+)\s*-\s*(\d*)', re.I)
class RangeFileWrapper (object):
def __init__(self, filelike, blksize=8192, offset=0, length=None):

Keybase proof

I hereby claim:

  • I am dcwatson on github.
  • I am dcwatson (https://keybase.io/dcwatson) on keybase.
  • I have a public key whose fingerprint is AA86 8D19 A671 C0CA A346 DC45 080F 948B CCBC 3A3F

To claim this, I am signing this object:

@dcwatson
dcwatson / fix_migrations.py
Created December 4, 2017 16:37
A hacky script to rewrite old Django migrations to include current on_delete values
#!/usr/bin/env python
# IMPORTANT! USAGE INSTRUCTIONS!
# Drop this file in the top level of your project *after* you've added on_delete to all your ForeignKey and
# OneToOneFields. Running it will rewrite your migration files to add whatever the current on_delete for each field is
# to the past migrations. Make sure you have a clean working copy so you can easily review the differences before
# committing. Assuming there were no pending migrations, running "makemigrations" should yield no new changes.
import os
@dcwatson
dcwatson / ondelete.py
Created July 10, 2018 20:05
Add on_delete to Django models for upgrading to 2.0
import os
import re
fk = re.compile(r'\s*([^\s]+)\s*=\s*models\.?(ForeignKey|OneToOneField)\(([^\s,\)]+)')
def fix(path, line):
line = line.rstrip()
# Weed out non-FKs, FKs with on_delete, or incomplete lines (will need to be done manually)
@dcwatson
dcwatson / Dockerfile
Created July 11, 2018 20:39
Django Dockerfile example
FROM python:3-slim
ENV LANG=C.UTF-8 \
PYTHONUNBUFFERED=1 \
DJANGO_SETTINGS_MODULE=project.settings.docker
COPY requirements.txt /app/
RUN pip install --no-cache-dir -Ur /app/requirements.txt
COPY . /app
@dcwatson
dcwatson / forker.py
Created August 2, 2018 01:41
MacOS resource fork parser
#!/usr/bin/env python3
# Parses out classic MacOS resource fork data into files
# See https://developer.apple.com/library/archive/documentation/mac/pdf/MoreMacintoshToolbox.pdf
import os
import struct
import sys
def CharConst(n):
@dcwatson
dcwatson / hkdf_sha256.swift
Created July 26, 2019 16:04
HKDF implementation in Swift using Apple's CryptoKit framework
func hkdf_sha256(_ seed: Data, salt: Data, info: Data, outputSize: Int = 32) -> Data? {
// It would be nice to make this generic over <H: HashFunction> if HashFunction had byteCount instead of each hash
// individually implementing it.
let iterations = UInt8(ceil(Double(outputSize) / Double(SHA256.byteCount)))
guard iterations <= 255 else {
return nil
}
let prk = HMAC<SHA256>.authenticationCode(for: seed, using: SymmetricKey(data: salt))
let key = SymmetricKey(data: prk)
@dcwatson
dcwatson / sealedbox.py
Created July 22, 2020 05:15
Basic Python implementation of CryptoKit's AES.GCM.SealedBox
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import binascii
import os
class SealedBox:
"""
Basic Python implementation of CryptoKit's AES.GCM.SealedBox
"""
@dcwatson
dcwatson / deltize.py
Created July 12, 2021 16:13
Convert between duration strings and Python timedelta objects
import re
from datetime import timedelta
format_specs = {
"y": 31536000,
"w": 604800,
"d": 86400,
"h": 3600,
"m": 60,
"s": 1,