Skip to content

Instantly share code, notes, and snippets.

View carljm's full-sized avatar

Carl Meyer carljm

View GitHub Profile
diff --git a/docs/ref/forms/models.txt b/docs/ref/forms/models.txt
index 46f7df1..dd0a422 100644
--- a/docs/ref/forms/models.txt
+++ b/docs/ref/forms/models.txt
@@ -33,8 +33,8 @@ Model Form Functions
``formfield_callback`` and ``widgets`` are all passed through to
:func:`~django.forms.models.modelform_factory`.
- Arguments ``formset``, ``extra``, ``max_num``, ``can_order``, and
- ``can_delete`` are passed through to
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
@carljm
carljm / decorators.py
Created March 27, 2011 18:28
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):
@wraps(view_func)
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 / 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
@carljm
carljm / runner.py
Created December 9, 2011 04:07
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 ``./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 / staticmethod.py
Created June 27, 2012 14:55
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):
~$ bench-jobs compare req-compile-bench-1664914413-esnow req-compile-bench-1664903905-esnow
All benchmarks:
===============
+-------------------------+------------------------------------------------------------------+------------------------------------------------------------------+
| Benchmark | req-compile-bench-1664914413-esnow/pyperformance-results.json.gz | req-compile-bench-1664903905-esnow/pyperformance-results.json.gz |
+=========================+==================================================================+==================================================================+
| unpickle | 13.7 us | 13.2 us: 1.04x faster |
+-------------------------+------------------------------------------------------------------+------------------------------------------------------------------+
@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