Skip to content

Instantly share code, notes, and snippets.

Avatar

Carl Meyer carljm

View GitHub Profile
@carljm
carljm / runner.py
Created Dec 9, 2011
Unittest2 test discovery and real dotted-path named test selection for Django
View runner.py
"""
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 ``./manage.py test``, it'll discover and run all tests
underneath the ``TEST_DISCOVERY_ROOT`` setting (a path). If you run
``./manage.py test full.dotted.path.to.test_module``, it'll run the tests in
that module (you can also pass multiple modules).
@carljm
carljm / db.py
Last active Jun 23, 2020
SQLAlchemy and Postgres autocommit
View db.py
"""
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 / staticmethod.py
Created Jun 27, 2012
pure Python implementation of the staticmethod decorator
View staticmethod.py
class StaticMethod(object):
def __init__(self, func):
self.func = func
def __get__(self, obj, cls):
return self.func
def staticmethod(func):
@carljm
carljm / test_ws.py
Last active Apr 22, 2019
Establishing a websocket connection to SocketIO 1.x from Python
View test_ws.py
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 / envsettings.py
Created Apr 13, 2016
Django settings from env.
View envsettings.py
"""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
carljm / jinja2backend.py
Created Jun 14, 2015
Django Jinja2 backend subclass with context processor support
View jinja2backend.py
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)
View simplified.diff
diff --git a/mypy/semanal.py b/mypy/semanal.py
index 7bac809..33d798f 100644
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -2943,21 +2943,9 @@ class SemanticAnalyzer(NodeVisitor):
if isinstance(base.node, TypeInfo):
# C.bar where C is a class
type_info = base.node
- elif isinstance(base.node, Var) and self.function_stack:
- # check for self.bar or cls.bar in method/classmethod
@carljm
carljm / postactivate
Created Jul 12, 2011
Yo dawg, I heard you like Ruby...
View postactivate
#!/bin/bash
# This hook is run after every virtualenv is activated.
export OLD_GEM_HOME=$GEM_HOME
export GEM_HOME=$VIRTUAL_ENV/gems/
export GEM_PATH=
export PATH=$VIRTUAL_ENV/gems/bin:$PATH
@carljm
carljm / decorators.py
Created Mar 27, 2011
An example of a decorator for a view that returns a TemplateResponse, modifying the template context before it is rendered.
View decorators.py
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):
@wraps(view_func)
View transaction_middleware.py
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
You can’t perform that action at this time.