Skip to content

Instantly share code, notes, and snippets.

View jarshwah's full-sized avatar

Josh Smeaton jarshwah

View GitHub Profile
@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
@jarshwah
jarshwah / launch.json
Created April 23, 2017 11:28
Webpack Source Maps with vscode debugging
// debug config for running project under vscode debugger
{
"version": "0.2.0",
"configurations": [
{
"trace": true,
"name": "Chrome Debug",
"type": "chrome",
"request": "launch",
"url": "http://localhost:8000/",