Skip to content

Instantly share code, notes, and snippets.

@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'])
@shazow
shazow / meta.py
Created March 6, 2013 23:20
My latest SQLAlchemy model base class.
"""SQLAlchemy Metadata and Session object"""
import datetime
import json
import time
from sqlalchemy import MetaData
from sqlalchemy.orm import scoped_session, sessionmaker
__all__ = ['Session', 'metadata', 'Model', 'SchemaEncoder']
@shazow
shazow / settings.py
Last active December 14, 2015 09:59
Django settings.py query logging
# ...
LOGGING = {
# ...
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
},
# ...
},
@shazow
shazow / autobackup.sh
Last active December 11, 2015 05:18
Database autobackup script.
#!/bin/bash
# Backup all the things, and delete old backups. (Perfect for a nightly cron job.)
# How many old backups should we keep?
NUM_OLD_BACKUPS=5
# Which databases should we worry about?
DATABASE_PREFIX="foo"
##
@shazow
shazow / develop.screenrc
Created January 5, 2013 23:35
Open development daemons in split gnu screen.
startup_message off
hardstatus alwayslastline
hardstatus string "%= %{r} Quit by hitting: qq"
escape q;
bind q quit
screen -t "Serve HTTP" 1 make serve
screen -t "Serve CSS" 2 make css_watch
@shazow
shazow / v1
Last active June 6, 2017 06:57
post-receive hook to push-deploy with git
#!/bin/bash
# post-receive hook to push-deploy multiple branches using a git repo.
#
# Deploy by pushing $DEPLOY_BRANCH
# Rollback by force-pushing a new reference to $OVERRIDE_TAG
#
# 1. Setup your git repo:
#
# git init --bare
#
@shazow
shazow / gist:4227021
Created December 6, 2012 18:48
Closure scopes in JavaScript.
(function() {
// This is a new closure. If things that happen here don't have side-effects on outside scope,
// then whatever happens here will not be visible outside of it.
(function() {
// You can make closures inside of closure (inside of closures). Wee.
})();
})();
def brandon_rhodes():
"""Complete the foo of the 1st argument.
Completing the foo is always difficult, you know?
But this function does it, else raises ValueError.
"""
pass
@shazow
shazow / gist:3928595
Last active November 18, 2022 14:24
Makefile which handles Python requirements.txt and *.egg-info (with optional virtualenv checking).
REQUIREMENTS_FILE=requirements.txt
REQUIREMENTS_OUT=requirements.txt.log
SETUP_OUT=*.egg-info
all: setup requirements
requirements: $(REQUIREMENTS_OUT)
$(REQUIREMENTS_OUT): $(REQUIREMENTS_FILE)
pip install -r $(REQUIREMENTS_FILE) | tee $(REQUIREMENTS_OUT)