Skip to content

Instantly share code, notes, and snippets.

View mmerickel's full-sized avatar

Michael Merickel mmerickel

View GitHub Profile
@mmerickel
mmerickel / workflow.txt
Last active May 22, 2017 18:50
pyramid exception handling workflow
pyramid_retry +--------> if exc not retryable, render response
if exc retryable, ignore exc and try again
+
|
|
v
pyramid_debugtoolbar
+
@mmerickel
mmerickel / Procfile
Last active May 4, 2017 23:33
pyramid on heroku
release: ./entrypoint.sh run alembic -c site.ini upgrade head
web: ./entrypoint.sh run pserve site.ini
@mmerickel
mmerickel / templating.py
Created April 24, 2017 22:17
localizable strings
import jinja2
from pyramid.renderers import render
from pyramid.threadlocal import get_current_request
from translationstring import TranslationString
import uuid
from myapp.utils.i18n import DEFAULT_DOMAIN
log = __import__('logging').getLogger(__name__)
@mmerickel
mmerickel / main.py
Created March 3, 2017 23:39
update shared data in the background
import threading
import time
import transaction
from .models import get_tm_session
def worker(dbsession_factory, registry):
while True:
tm = transaction.TransactionManager(explicit=True)
with tm:
@mmerickel
mmerickel / migratedb.py
Created January 20, 2017 04:37
alembic db:migrate script
from alembic.config import Config
from alembic.command import stamp
from alembic.command import upgrade
from alembic.migration import MigrationContext
from myapp.model.meta import metadata
from myapp.model.meta import get_engine
log = __import__('logging').getLogger(__name__)
@mmerickel
mmerickel / localized_routes.py
Created November 5, 2016 17:27
localized routes in pyramid
def add_localized_route(config, name, pattern, factory=None, pregenerator=None, **kw):
orig_factory = factory
def wrapper_factory(request):
lang = request.matchdict['lang']
# determine if this is a supported lang and convert it to a locale,
# likely defaulting to your default language if the requested one is
# not supported by your app
request._LOCALE_ = lang
if orig_factory:
return orig_factory(request)
@mmerickel
mmerickel / resolver.py
Created August 3, 2016 21:12
maybe_resolve dotted python path
import inspect
import sys
def caller_module(depth=1):
frm = inspect.stack()[depth + 1]
caller = inspect.getmodule(frm[0])
return caller
def caller_package(depth=1):
module = caller_module(depth + 1)
@mmerickel
mmerickel / basic_auth1.py
Last active March 6, 2016 17:04
roll your own basic auth in pyramid
"""
Let's pretend Pyramid doesn't have an enterprise-grade auth system
that can support several workflows. Make our own with decorators!
"""
import base64
from pyramid.httpexceptions import HTTPUnauthorized
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
@mmerickel
mmerickel / unity_gzip_tween.py
Last active November 8, 2016 18:34
pre-compressed unity files
UNITY_GZIP_FILES = frozenset([
'js',
'mem',
'data',
'unity3d',
])
def can_gzip_request(request):
if (
request.method in ('GET', 'HEAD') and
@mmerickel
mmerickel / notfound.py
Created December 1, 2015 04:03
serving cache busted assets without renaming files on disk
_static_regex = re.compile(
r'''
(?P<root>/static/[a-zA-Z0-9._/-]+)
-
(?P<buster>[a-fA-F0-9]+)
(?P<ext>\.[a-zA-Z0-9]+)
$''',
re.VERBOSE,
)