Skip to content

Instantly share code, notes, and snippets.

@inklesspen
inklesspen / example.py
Last active August 29, 2015 14:06
Pyramid SQLAlchemy session setup
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine
from zope.sqlalchemy import register
def get_sessionmaker(url, zope=True):
"""Configure a sessionmaker for use by requests.
By default, sessions will attach themselves to the current transaction
manager. Then can be turned off by setting ``zope=False``.

Adapting Pyramid to Fit your Application

Description

There are many ways to build Pyramid apps, and knowing your options can help you make maintainable choices. Most people generate Pyramid apps using a scaffold and plug in one or two add-ons like pyramid_redis_session or pyramid_tm, but if you're interested in stepping beyond that, I'll show you some real-world examples of how we've adapted Pyramid to fit our apps.

Abstract

Pyramid offers an enormous amount of customization power; most apps only use a small portion of it. I'll show you how to identify the right way to customize the framework for your particular use case and how to use this power to make your app simpler and easier to understand.

View mappers

Go edit your theme HTML and paste the following lines into it at line 670
(right after the PostNotes section and before the second {block: Date}
{block:HasTags}
<div class="Tags">
{block:Tags}<a href="{TagURL}">#{Tag}</a>{/block:Tags}
</div><!-- End Post Tags -->
{/block:HasTags}
@inklesspen
inklesspen / query-1-explain.txt
Created August 4, 2014 22:46
Postgres CTE optimization problems
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
CTE Scan on pn_downstream_thread (cost=28135.55..28281.37 rows=7291 width=8) (actual time=0.131..0.410 rows=6 loops=1)
CTE pn_downstream_thread
-> Recursive Union (cost=1.71..28135.55 rows=7291 width=12) (actual time=0.126..0.391 rows=6 loops=1)
-> Nested Loop (cost=1.71..68.15 rows=1 width=12) (actual time=0.118..0.275 rows=2 loops=1)
-> Nested Loop (cost=1.28..59.69 rows=1 width=12) (actual time=0.102..0.230 rows=3 loops=1)
-> Nested Loop (cost=0.85..21.12 rows=5 width=8) (actual time=0.058..0.100 rows=12 loops=1)
-> Index Scan using idx_16485_unique_asset_typ_id on ams_asset (cost=0.42..8.45 rows=1 width=8) (actual time=0.038..0.039 rows=1 loops=1)
@inklesspen
inklesspen / ColorMixer.py
Created July 27, 2014 17:32
color dealie
# ColorMixer
# A simple RGB color mixer with three sliders.
import ui
import clipboard
from random import random
from console import hud_alert
def slider_action(sender):
# Get the root view:
jon=# \d foo
Table "public.foo"
Column | Type | Modifiers
--------+-----------------------+--------------------------------------------------
id | integer | not null default nextval('foo_id_seq'::regclass)
key | character varying(20) | not null
value | character varying | not null
Indexes:
"foo_pkey" PRIMARY KEY, btree (id)
"foo_key_key" UNIQUE CONSTRAINT, btree (key)
@inklesspen
inklesspen / post-checkout.py
Created June 10, 2014 21:54
Post-checkout hook to detect alembic issues when switching branches.
#!/usr/bin/env python
"""
Provide useful alembic information after switching branches.
"""
import argparse
import subprocess
import os
import os.path
import py.path
(require 'mwheel)
(progn (require 'flymake-cursor) (require 'mouse) (xterm-mouse-mode t) (defun track-mouse (e)) (mouse-wheel-mode))
# in your ini file, have this:
# env.use_env_variables = true
# sqlalchemy.url = DATABASE_URL
# then, in your __init__.py file, the first thing you do in your main function is this:
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
# Baseview has crud methods set up to do crud, based on some class attributes which are intended
# to be overridden by subclasses. Needless to say I have the full set of CRUD, and they actually
# do stuff.
class BaseView(object):
def __init__(self, request):
self.request = request
def create(self):
return "New object"