Skip to content

Instantly share code, notes, and snippets.

@shazow
shazow / memoizer.go
Last active December 22, 2015 02:59
Generic memoizer in Go. (http://play.golang.org/p/ct8ySJ3eh4)
package main
import (
"errors"
"fmt"
"reflect"
"runtime"
)
var ErrMissedCache = errors.New("Memoizer: Missed cache.")
@shazow
shazow / controller.go
Created September 1, 2013 20:37
Controller miniframework for webapps (in progress)
package foo
import (
"appengine"
"appengine/urlfetch"
"code.google.com/p/goauth2/oauth"
"code.google.com/p/google-api-go-client/analytics/v3"
"code.google.com/p/google-api-go-client/oauth2/v2"
"github.com/gorilla/sessions"
"html/template"
@shazow
shazow / gist:6228559
Created August 14, 2013 06:40
OAuth2 for GA in Go
func AccountConnectHandler(w http.ResponseWriter, r *http.Request) {
// TODO: Check state? r.FormValue("state")
transport := &oauth.Transport{
Config: &config,
Transport: &urlfetch.Transport{
Context: appengine.NewContext(r),
},
}
@shazow
shazow / gist:5754021
Last active December 18, 2015 08:29
I want a Python key namespace utility that lets me do things like this...
# Import from an imaginary package I wish existed...
from keyspace import KeySpace, Key, Prefix
class MyKeys(KeySpace):
prefix_users = Prefix('u')
user_set = Key('users')
KEYS = MyKeys(prefix='prod', delim=':')
@shazow
shazow / gist:5666792
Last active December 17, 2015 20:19
Bitcoin helpers, candidate for unstdlib.py?
import re
import hashlib
import unstdlib
RE_WALLET_ADDRESS = re.compile('^[a-zA-Z1-9]{27,35}$')
ALPHABET_BASE58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
class NETWORK_VERSIONS:
@shazow
shazow / gist:5658843
Created May 27, 2013 20:04
Parsing nginx-style access logs in Python.
import re
RE_LOG = re.compile(
'(?P<ip>[(\d\.)]+) '
'- - '
'\[(?P<timestamp>.*?)\] '
'"(?P<request>.*?)" '
'(?P<status>\d+) '
'(?P<size>\d+) '
'"(?P<referrer>.*?)" '
@shazow
shazow / sa_enum_type.py
Created April 28, 2013 22:20
SQLAlchemy custom Enum type that uses Integer-based indexing.
from sqlalchemy import types
class Enum(types.TypeDecorator):
impl = types.Integer
def __init__(self, value_map, strict=True, *args, **kw):
"""Emulate Enum type with integer-based indexing.
value_map:
@shazow
shazow / salt.md
Last active December 15, 2015 06:09
Questions & Feedback for SaltStack

Questions & Feedback for SaltStack

I've read as much introduction documentation as I can find. Here are some outstanding questions that I'm still seeking answers for. Links or answers in comments are welcome.

Questions

  1. Is it possible to define salt configurations for different server roles (e.g. webserver, database, loadbalancer, etc) which could be distributed to an arbitrary number of servers? (e.g. one server that is a {webserver,database,loadbalancer} or three servers where each is one of the kid.) I have a feeling this has something to do with Grains, but not sure how.

    Answered: Use grains or nodegroups.

@shazow
shazow / api.py
Created March 9, 2013 21:40
My API controller for my Pyramid meta-framework thing.
import json
from pyramid.httpexceptions import HTTPSeeOther, HTTPBadRequest
from pyramid.response import Response
from jobcupid.lib.exceptions import APIControllerError, LoginRequired
from jobcupid.model.meta import SchemaEncoder
API_METHOD_MAP = {}
@shazow
shazow / account.py
Last active December 14, 2015 17:58
My Pyramid class-based view base class.
# A sample view from my upcoming meta-framework.
# You probably won't have things like `request.features` or `api` or `expose_api` etc. Coming soon.
@expose_api('account.create')
def account_create(request):
if request.features.get('invite_required'):
raise APIControllerError("Method not permitted: %s" % account_create.exposed_name)
email, password, password_confirm = get_many(request.params, required=['email'], optional=['password', 'password_confirm'])