Skip to content

Instantly share code, notes, and snippets.

View mmerickel's full-sized avatar

Michael Merickel mmerickel

View GitHub Profile
def commit_veto(environ, status, headers):
for good in ('1', '2', '3'):
if status.startswith(good):
break
else:
return True
for header_name, header_value in headers:
if header_name.lower() == 'x-tm-abort':
return True
@mmerickel
mmerickel / root.py
Created January 27, 2011 00:20 — forked from wwitzel3/root.py
class DayZeroContainer(ModelContainer):
def __init__(self, request, cls):
self.cls = cls
self.request = request
@property
def __acl__(self):
return [
(Allow, 'owner:{0}'.format(cls.ownerid), ('add', 'edit)),
# ... some other acls for this particular resource?
@mmerickel
mmerickel / gist:805439
Created February 1, 2011 05:00
How to setup sqlalchemy without scoped_sessions or circular imports.
# tutorial/__init__.py
from pyramid.config import Configurator
from tutorial.model.meta import init_model
from tutorial.request import TutorialRequest
def main(global_config, **settings):
init_model(settings)
@mmerickel
mmerickel / resources.py
Created February 9, 2011 23:21
Adapted example from rocky on #pylons for wrapping SQLA objects into a resource tree for pyramid's traversal algorithm.
from pyramid.decorator import reify
from pyramid_traversalwrapper import LocationProxy
class traversable_attrs(object):
""" A decorator that adds a "wrap" attribute to the given class
in the form of a dict-like class that does item lookup based on
the attrs given.
"""
from pyramid.response import Response
from pyramid.view import view_config
class Test12:
def __init__(self, request):
self.request = request
@view_config(route_name='test1')
def test1(self):
return Response('I am from test 1')
@mmerickel
mmerickel / gist:5286023
Created April 1, 2013 16:32
poor man's rest resource in pyramid
class rest_resource(object):
all_methods = frozenset([
'head', 'get', 'post', 'put', 'patch', 'delete'])
def __init__(self, **kw):
self.kw = kw # view defaults
def __call__(self, wrapped):
# grab the supported methods from the class
methods = self.all_methods.intersection(set(vars(wrapped).keys()))
@mmerickel
mmerickel / test.py
Created April 1, 2013 17:54
weird sqlalchemy
from sqlalchemy import (
create_engine,
Column,
ForeignKey,
)
from sqlalchemy import (
Integer,
String,
)
from sqlalchemy.ext.declarative import declarative_base
@mmerickel
mmerickel / README.rst
Last active December 19, 2015 12:49
A workflow for docker that separates build-time dependencies from run-time dependencies.

A workflow for docker that separates build-time dependencies from run-time dependencies.

Create the image used for building apps.

docker build -t pybuilder pybuilder

Create the image used for running apps.

@mmerickel
mmerickel / settings.py
Created December 10, 2013 22:25
How to load a more useful ini file for use with pyramid.
from ConfigParser import RawConfigParser
def load_settings(global_config, app_settings):
"""
Load an INI file with multiple sections into a single dictionary.
.. code-block:: ini
[sqlalchemy]
url = sqlite://%(here)s
@mmerickel
mmerickel / gist:8035611
Created December 19, 2013 07:25
backend-based session storage in pyramid
from hashlib import sha256
import os
from pyramid.session import SignedCookieSessionFactory
def make_session_id():
rand = os.urandom()
return sha256(sha256(rand).digest()).hexdigest()
class MemorySessionSerializer(object):