Skip to content

Instantly share code, notes, and snippets.

View lost-theory's full-sized avatar

Steven Kryskalla lost-theory

View GitHub Profile
@lost-theory
lost-theory / gist:3091483
Created July 11, 2012 16:12
flask-dropbox 0.1.2 install
stevek@thor ~
$ virtualenv flaskdropbox_env
New python executable in flaskdropbox_env/bin/python
Installing setuptools............done.
stevek@thor ~
$ cd flaskdropbox_env/
stevek@thor ~/flaskdropbox_env
$ bin/pip install Flask-Dropbox
@lost-theory
lost-theory / tests.py
Created August 18, 2012 07:09
mock render_template
from mock import patch #http://pypi.python.org/pypi/mock
import flask
import myapp
@patch('flask.templating._render', return_value='')
def test_mocked_render(mocked):
t = myapp.app.test_client()
print "mocked", repr(t.get("/").data)
print "was _render called?", mocked.called
@lost-theory
lost-theory / advice.py
Created August 28, 2012 07:53
coffeescript advice method combinators a la raganwald, in python
#http://news.ycombinator.com/item?id=4439352
'''
before = (decoration) ->
(base) ->
->
decoration.apply(this, arguments)
base.apply(this, arguments)
after = (decoration) ->
@lost-theory
lost-theory / decorators.py
Created August 28, 2012 07:55
coffeescript decorator examples a la raganwald, but in python
#http://news.ycombinator.com/item?id=4439352
def has_permission_to(user, verb, subj):
if user == "steve" and verb == "write":
return True
elif user == "alice" and verb == "read":
return True
return False
def current_user():
@lost-theory
lost-theory / cookbookdiff.py
Created September 18, 2012 22:55
chef cookbook diff
#!/usr/bin/env python
'''\
Usage: cookbookdiff COOKBOOK_NAME COOKBOOK_VER LOCAL_PATH [--nocolor]
Diff your LOCAL_PATH against what is on the chef server for a
given cookbook and version.
--nocolor: don't pipe output through 'colordiff' (in case you want to pipe to something else)
Examples:
cookbookdiff percona_toolkit 0.0.4 ~/chef-repo/cookbooks/percona_toolkit
@lost-theory
lost-theory / gist:3772472
Created September 23, 2012 17:48
upload file with flask test client
import unittest
from app import app
from cStringIO import StringIO
class UploadTest(unittest.TestCase):
def setUp(self):
self.app = app
self.app.config['TESTING'] = True
self.client = self.app.test_client()
@lost-theory
lost-theory / gist:3925738
Created October 21, 2012 04:29
different delimiters in jinja2 + flask
from flask import Flask, render_template_string, request
class CustomFlask(Flask):
jinja_options = Flask.jinja_options.copy()
jinja_options.update(dict(
block_start_string='<%',
block_end_string='%>',
variable_start_string='%%',
variable_end_string='%%',
comment_start_string='<#',
@lost-theory
lost-theory / gist:3925780
Created October 21, 2012 04:35
pass mustache template into jinja template
from flask import Flask, render_template_string, request
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route("/")
def index():
#of course you would put these into files
jinja_template = """
{# this is a jinja2 comment #}
@lost-theory
lost-theory / gist:3928813
Created October 21, 2012 22:48
flask dynamic url_prefix for blueprints
from flask import Flask, Blueprint, request
## blueprint ##################################################################
bp = Blueprint('category_functionality', __name__)
@bp.route('/')
def index(category):
return "this is the index page for %r" % category
@lost-theory
lost-theory / composition.py
Created October 22, 2012 05:10
composition vs. inheritance
class ListView(object):
def get_items(self):
return []
class CategoryView(ListView):
def get_items(self):
return [
{'id': 1, 'title': 'First post', 'body': 'Hello world.', 'tags': ['red', 'blue'], 'category': 'something'},
{'id': 2, 'title': 'Second post', 'body': 'Jello world.', 'tags': ['blue', 'green'], 'category': 'cool'},
{'id': 3, 'title': 'Third post', 'body': 'Yellow world.', 'tags': ['yellow'], 'category': 'cool'},