Skip to content

Instantly share code, notes, and snippets.

View carljm's full-sized avatar

Carl Meyer carljm

View GitHub Profile
diff --git a/tox.ini b/tox.ini
--- a/tox.ini
+++ b/tox.ini
@@ -1,7 +1,9 @@
[tox]
envlist=
py26-1.3,py26-1.4,py26-1.5,py26-1.6,
- py27-1.3,py27-1.4,py27-1.5,py27-1.6,py27-1.7,py27-master
+ py27-1.3,py27-1.4,py27-1.5,py27-1.6,py27-1.7,py27-master,
+ py33-1.5,py33-1.6,py33-1.7,py33-master,
@carljm
carljm / db.py
Last active May 1, 2023 00:54
SQLAlchemy and Postgres autocommit
"""
SQLAlchemy, PostgreSQL (psycopg2), and autocommit
See blog post: http://oddbird.net/2014/06/14/sqlalchemy-postgres-autocommit/
"""
from contextlib import contextmanager
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker, Session as BaseSession
@carljm
carljm / greatest.py
Created August 21, 2014 20:46
GREATEST() of aggregates, in the Django ORM.
class Greatest(object):
"""A pseudo- (or meta-) aggregate; returns highest of sub-aggregates.
This has to be inserted directly into the Query internals; it can't go
through the usual ``qs.annotate(...)`` or ``qs.aggregate(...)`` path
(because that leads to ``Query.add_aggregate(...)`` which makes assumptions
we can't meet).
To use, instantiate ``Greatest`` with one or more normal aggregates, and
call ``greatest_instance.add_to_qs(queryset, 'name')``
$ ./tests/runtests.py migrations
Testing against Django installed in '/home/carljm/projects/django/django/django/django'
Creating test database for alias 'default'...
Creating test database for alias 'other'...
..................................................................................................................................s...............................EE.EE.E..........s.......................................
======================================================================
ERROR: test_squashmigrations_squashes (migrations.test_commands.SquashMigrationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/carljm/projects/django/django/django/django/test/utils.py", line 187, in inner
@carljm
carljm / test_ws.py
Last active November 10, 2022 20:05
Establishing a websocket connection to SocketIO 1.x from Python
import json
import requests
from websocket import create_connection
BASE = 'localhost:3000/socket.io/?EIO=3'
# First establish a polling-transport HTTP connection to get a session ID
url = 'http://%s&transport=polling' % BASE
@carljm
carljm / patch.diff
Created February 12, 2015 00:00
draft patch for --fake-initial doc updates
diff --git a/docs/topics/migrations.txt b/docs/topics/migrations.txt
index a0dc365..fae87a0 100644
--- a/docs/topics/migrations.txt
+++ b/docs/topics/migrations.txt
@@ -148,6 +148,13 @@ developers (or your production servers) check out the code, they'll
get both the changes to your models and the accompanying migration at the
same time.
+.. versionadded:: 1.8
+
@carljm
carljm / test.py
Created March 24, 2015 01:07
playing with logging
from logging.config import dictConfig
import logging
initial = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
@carljm
carljm / jinja2backend.py
Created June 14, 2015 21:21
Django Jinja2 backend subclass with context processor support
class Jinja2Backend(jinja2backend.Jinja2):
def __init__(self, params):
self.context_processors = [
import_string(p)
for p in params['OPTIONS'].pop('context_processors', [])
]
super(Jinja2Backend, self).__init__(params)
@carljm
carljm / envsettings.py
Created April 13, 2016 19:07
Django settings from env.
"""Utility for pulling settings from the environment."""
import os
from urllib.parse import urlparse
class EnvParser:
"""Utility for getting settings from the OS environ.
Instantiate with the name of an env var to get the current mode from (that
env var should be set to one of VALID_MODES, or not set), then call the
class TransactionMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
with transaction.atomic():
response = self.get_response(request)
if response.status_code == 500:
transaction.set_rollback()
return response