Skip to content

Instantly share code, notes, and snippets.


Carl Meyer carljm

View GitHub Profile
carljm /
Created Dec 9, 2011
Unittest2 test discovery and real dotted-path named test selection for Django
An alternative Django ``TEST_RUNNER`` which uses unittest2 test discovery from
a base path specified in settings, rather than requiring all tests to be in
``tests`` module of an app.
If you just run ``./ test``, it'll discover and run all tests
underneath the ``TEST_DISCOVERY_ROOT`` setting (a path). If you run
``./ test``, it'll run the tests in
that module (you can also pass multiple modules).
carljm /
Last active Jun 23, 2020
SQLAlchemy and Postgres autocommit
SQLAlchemy, PostgreSQL (psycopg2), and autocommit
See blog post:
from contextlib import contextmanager
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker, Session as BaseSession
carljm /
Created Jun 27, 2012
pure Python implementation of the staticmethod decorator
class StaticMethod(object):
def __init__(self, func):
self.func = func
def __get__(self, obj, cls):
return self.func
def staticmethod(func):
carljm /
Last active Apr 22, 2019
Establishing a websocket connection to SocketIO 1.x from Python
import json
import requests
from websocket import create_connection
BASE = 'localhost:3000/'
# First establish a polling-transport HTTP connection to get a session ID
url = 'http://%s&transport=polling' % BASE
carljm /
Created Apr 13, 2016
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
carljm /
Created Jun 14, 2015
Django Jinja2 backend subclass with context processor support
class Jinja2Backend(jinja2backend.Jinja2):
def __init__(self, params):
self.context_processors = [
for p in params['OPTIONS'].pop('context_processors', [])
super(Jinja2Backend, self).__init__(params)
View simplified.diff
diff --git a/mypy/ b/mypy/
index 7bac809..33d798f 100644
--- a/mypy/
+++ b/mypy/
@@ -2943,21 +2943,9 @@ class SemanticAnalyzer(NodeVisitor):
if isinstance(base.node, TypeInfo):
# where C is a class
type_info = base.node
- elif isinstance(base.node, Var) and self.function_stack:
- # check for or in method/classmethod
carljm / postactivate
Created Jul 12, 2011
Yo dawg, I heard you like Ruby...
View postactivate
# This hook is run after every virtualenv is activated.
export GEM_HOME=$VIRTUAL_ENV/gems/
export GEM_PATH=
export PATH=$VIRTUAL_ENV/gems/bin:$PATH
carljm /
Created Mar 27, 2011
An example of a decorator for a view that returns a TemplateResponse, modifying the template context before it is rendered.
def paginate(ctx_name):
View decorator that handles pagination of a ListObject. Expects to find it
in the TemplateResponse context under the name ``ctx_name``.
This needs to not force delivery of the ListObject.
def decorator(view_func):
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:
return response
You can’t perform that action at this time.