Skip to content

Instantly share code, notes, and snippets.

@dmwyatt
dmwyatt / django.py
Created August 31, 2024 17:31
blue/green django
# models.py
from django.db import models
class User(models.Model):
# Old field (blue version)
name = models.CharField(max_length=100)
# New fields (green version)
first_name = models.CharField(max_length=50, null=True, blank=True)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dmwyatt
dmwyatt / encrypted_field.py
Last active June 8, 2024 16:43
An experimental Django EncryptedField for encryption-at-rest
"""
Encrypted field for Django for encrypting data at rest.
Inspired by: https://scottarc.blog/2024/06/02/encryption-at-rest-whose-threat-model-is-it-anyway/
"""
import base64
import os
from django.db import models
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
@dmwyatt
dmwyatt / checks.py
Last active September 6, 2024 21:31
Check that Django models inherit from a common base model and that they explictly define db_table
import ast
import inspect
from pathlib import Path
from django.apps import apps
from django.conf import settings
from django.core.checks import Error, register, Tags
from .models import UniversalBaseModel
@register(Tags.models)
@dmwyatt
dmwyatt / filetype_types.py
Last active February 21, 2024 21:18
A Union of all file types in `filetype` package
frofrom typing import Union
from filetype.types import application, archive, audio, document, font, image, video
ImageFileType = Union[
image.Dwg,
image.Xcf,
image.Jpeg,
image.Jpx,
image.Apng,
{
"manifest_version": 3,
"name": "Superpower ChatGPT",
"version": "5.1.2",
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzG6ZR+j3lpHF0XrDLIXdrk25idObfq+RK7WM+pIaQmDO2nM5Y+SZJJbFwyxjWX+3V6XOgS5v9Lpnqg46OJ/W9Q5i23Usx1MXgaJBTlEFz0XG+PYK6BElhc9itS7m6oCLknin97a533tusXmm8zW7kaDGy8vycMDY6Ffbqa3sn0PqZ8bXUlAjgO91dQcB8EtlT906hwhZjtfEYvp2hdxYkRFxfuaR1WMLkxttVXv506RXJowxq0LO3aqj83QeJoXkQF1wbzCxYO1VpVGEmYIQxIKw/csusZNZs8gwJrIWtOzhMgDNOFzXNeZl0ASgoj2M9UsZp+Dunn57VT8tQyaE6QIDAQAB",
"description": "ChatGPT with superpowers! Sync/search history locally, create folders, export all chats, pin messages, access thousands of prompts",
"icons": {
"16": "images/icon-16.png",
"32": "images/icon-32.png",
"48": "images/icon-48.png",
import ast
import astor
import tkinter as tk
from tkinter import scrolledtext, IntVar, Checkbutton
class EllipsisTransformer(ast.NodeTransformer):
def __init__(self, remove_args, remove_methods):
self.remove_args = remove_args
self.remove_methods = remove_methods
@dmwyatt
dmwyatt / render.schema.json
Created May 7, 2023 19:20
A start at a render.yaml json schema
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"services": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {"type": "string"},
@dmwyatt
dmwyatt / path_walk.py
Last active October 25, 2023 21:50
[breadth-first path walk] Walk a directory tree yielding all files and directories using a breadth-first algo.
def path_walk(path: str) -> Generator[str, None, None]:
"""Walks a directory tree yielding all files and directories.
This uses a breadth-first algorithm.
"""
queue = deque([path])
while queue:
current = queue.popleft()
for entry in os.scandir(current):
entry: os.DirEntry
@dmwyatt
dmwyatt / knownpaths.py
Created January 28, 2022 17:41 — forked from mkropat/knownpaths.py
Python wrapper around the SHGetKnownFolderPath Windows Shell function
import ctypes, sys
from ctypes import windll, wintypes
from uuid import UUID
class GUID(ctypes.Structure): # [1]
_fields_ = [
("Data1", wintypes.DWORD),
("Data2", wintypes.WORD),
("Data3", wintypes.WORD),
("Data4", wintypes.BYTE * 8)