Skip to content

Instantly share code, notes, and snippets.

View scott2b's full-sized avatar

Scott Bradley scott2b

  • Northwestern University
  • Evanston
View GitHub Profile
@scott2b
scott2b / Twitter Bootstrap Collapsible Accordion Example
Created April 3, 2012 18:21
A less invasive approach to using the collapsible plugin from Twitter Bootstrap. Requires the use of less and a bit of additional javascript.
/*
Twitter Bootstrap is a nice library, but the way they handle markup for styles and plugins is a bit invasive at
times. Since Bootstrap is built with Less CSS anyway, it makes sense to take advantage of Less to cleanup the
markup.
This is an attempt at cleaning up the collapsible accordion example. If you don't like the HTML5 tags, you can
replace them with divs with classes (make the appropriate changes in the less).
One quirk that I know of: if you use the "in" class to indicate a default open article, the first accordion
click will not transition smoothly. I haven't figured out why this is happening, but generally with the
@scott2b
scott2b / fountain.bnf
Created June 26, 2012 14:48
Super rough draft pass at a grammar for Fountain, for discussion purposes only
<GT> :: = '>'
<capchar> ::= 'A' | 'B' | .... | 'Z'
<lowerchar> ::= 'a' | 'b' | .... | 'z'
<punc> ::= '.' | ',' | ....
<WS> ::= ' ' | <TAB>
<TO> ::= 'TO:'
<DOT> ::= '.'
<LP> ::= '('
<RP> ::= ')'
<intext> ::= 'INT' | 'EXT' | 'INT/EXT'
@scott2b
scott2b / Versionable Resources in Pyramid
Created December 11, 2012 03:53
An approach to route and view configuration in Pyramid that provides resource-level versions with content-type specification and api-version convenience URLs
"""REST principles state that we should version at the resource level. Both the version and the content-type of a
resource should be specified in the Accept header. But many developers prefer an approach which contains API
versions in the URL.
The approach in this gist finds a happy medium. Resource level versioning is provided, with resources being
specified for both version and content-type. Convenience URLs for API version levels are also configured, with each
API version being a specific set of versioned resources.
vendor-specified content types are configured in the form of vnd.namespace.Resource+mimetype. Routes for each of
these are configured as well as generic routes for the appropriate content type with the resource version that is
@scott2b
scott2b / accept_middleware.py
Created February 14, 2013 21:31
Override the HTTP_ACCEPT header with _accept parameter in Django. Taken from Django Snippets: http://djangosnippets.org/snippets/1042/
def parse_accept_header(accept):
"""Parse the Accept header *accept*, returning a list with pairs of
(media_type, q_value), ordered by q values.
"""
result = []
for media_range in accept.split(","):
parts = media_range.split(";")
media_type = parts.pop(0)
media_params = []
q = 1.0
@scott2b
scott2b / models.py
Created November 18, 2013 23:54
public and private User views in Pyramid
from pyramid.security import ALL_PERMISSIONS
from pyramid.security import Allow
from pyramid.security import Everyone
ADMINS = [ __some__list__of__admins__ ]
def groupfinder(userid, request):
"""groupfinder should be the callback to your authentication policy.
"""
if userid in ADMINS:
return ['g:admins']
@scott2b
scott2b / auth_views.py
Last active January 28, 2016 11:23
login override for whitelisting/blacklisting and new-user forwarding for use with pyramid_persona
from pyramid.view import view_config
from pyramid.security import remember
from pyramid.security import authenticated_userid
from pyramid_persona.views import verify_login
USE_WHITELIST = False
WHITELIST_REJECT_MESSAGE = 'Sorry, you are not authorized to access this site.'
WHITELIST_REJECT_REDIRECT = '/'
USE_BLACKLIST = False
BLACKLIST_REJECT_MESSAGE = 'Sorry, you are not authorized to access this site.'
@scott2b
scott2b / user.py
Last active December 28, 2015 17:59
SQLAlchemy User model for pyramid
from pyramid.security import ALL_PERMISSIONS
from pyramid.security import DENY_ALL
from pyramid.security import Allow
from pyramid.security import Everyone
from sqlalchemy import Column, Integer, Text, Unicode
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, synonym
from zope.sqlalchemy import ZopeTransactionExtension
import cryptacular.bcrypt
@scott2b
scott2b / twitter_searcher.py
Last active January 26, 2019 18:26
TwitterSearcher. Class to manage aggressive Twitter API searching with the birdy AppClient.
import logging
import time
import urlparse
from birdy.twitter import AppClient
from birdy.twitter import TwitterRateLimitError, TwitterClientError
from delorean import parse, epoch
"""
Utilization:
searcher = TwitterSearcher(
TWITTER_CONSUMER_KEY,
@scott2b
scott2b / base.html
Created April 30, 2014 16:07
base.html for Bookshelf project using Twitter Bootstrap
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="../../assets/ico/favicon.ico">
<title>Starter Template for Bootstrap</title>
@scott2b
scott2b / Elasticsearch client wrapper
Last active August 29, 2015 14:04
Working toward a more usable Elasticsearch client
"""
I find the Elasticsearch Python client a bit quirky to work with. A lot of this
has to do with the odd way that Elasticsearch documents and results are
organized (e.g. ['hits']['hits'] WTF?). The weird organization is exemplified
well in the need for the client to expose both `get` and `get_source` methods.
Also, there is a pretty bad lack of consistency: ['hits']['hits'] vs. ['docs'],
_version (with _) vs. found (without _), etc.
This is my crude attempt to make the client a bit more usable. Currently
implements `search`, `get`, and `mget`. `get_source` maps to `get` because there