Skip to content

Instantly share code, notes, and snippets.

View jarshwah's full-sized avatar

Josh Smeaton jarshwah

View GitHub Profile
@jarshwah
jarshwah / README.md
Created May 14, 2024 11:07
Use postgres unlogged tables when testing

In theory this should speed up tests running on Postgres as no WAL will be written, at the cost of completely hosing the test database if the instance crashes (which shouldn't be a problem with a test database).

When comparing builds with and without this patch applied though I had really inconsistent findings. Some builds ran much faster, but most ran much slower.

We already start postgres with the following CLI options which minimises WAL anyway, which likely renders the UNLOGGED change mostly redundant:

-c max_wal_senders=0
-c wal_level=minimal
-c min_wal_size=1024
@jarshwah
jarshwah / models.py
Created December 21, 2022 22:04
annotating your manager for useful type hints
class MyModel(models.Model):
name = models.CharField()
objects: models.QuerySet["MyModel"]
MyModel.objects.filter().first()
def pytest_configure(config):
config.addinivalue_line(
"markers", "enable_inline_css: run css postprocessing over generated emails"
)
@pytest.fixture(autouse=True)
def disable_inline_css(request, monkeypatch):
if "enable_inline_css" in request.keywords:
return
#!/bin/bash
# ~/.osx — http://mths.be/osx
# Close any open System Preferences panes, to prevent them from overriding
# settings we’re about to change
osascript -e 'tell application "System Preferences" to quit'
# Ask for the administrator password upfront
import pickle
import six
from django_redis.serializers.pickle import PickleSerializer
def compat_pickle_loads(value, **kwargs):
if six.PY2:
return _py2_pickle_loads(value)
return _py3_pickle_loads(value, **kwargs)
@jarshwah
jarshwah / tests.py
Last active June 7, 2019 12:41
Unit test for confirming there are no outstanding migrations
from django.core.management import call_command
from django.test import TestCase
class MigrationsTestCase(TestCase):
def test_no_outstanding_migrations(self):
"""No migrations need to be generated"""
try:
result = call_command("makemigrations", "--dry-run", "--check", "--no-input", "-v", "0")
except SystemExit:
self.fail("There were outstanding migrations")
@jarshwah
jarshwah / rabbitmq_to_cloudwatch.sh
Created June 5, 2019 13:23
Send RabbitMQ queue length to Cloudwatch
#! /bin/bash
# This script reads rabbitmq statistics and report them as CloudWatch metrics.
# https://gist.github.com/GoodMirek/2dc39100d18c72eed3f0f3569e221f4f
# RabbitMQ::messages_ready
# RabbitMQ::messages_unacknowledged
# RabbitMQ::consumers
# Dimensions: QueueName, InstanceId, VHost
@jarshwah
jarshwah / suspendingreceiver.py
Created August 23, 2018 12:04
Prevent receivers from running in test suites
import functools
from django.conf import settings
from django.dispatch import receiver
def suspendingreceiver(signal, **decorator_kwargs):
"""
A wrapper around the standard django receiver that prevents the receiver
from running if the setting `SUSPEND_SIGNALS` is `True`.
module: {
rules: [
// ...
// Rules for Style Sheets
{
test: /\.(scss|sass)$/,
include: [path.resolve(__dirname, 'src'), path.resolve(__dirname, 'assets/scss')],
use: extractSass.extract({
use: [
// Process internal/project styles (from assets/scss folder)
@jarshwah
jarshwah / object_cache.py
Created July 12, 2017 14:01
Django LRU in memory cache backend
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
import time
from contextlib import contextmanager
from django.core.cache.backends.base import DEFAULT_TIMEOUT, BaseCache
from django.utils.synch import RWLock
from lru import LRU # dependency: pip install lru-dict