Skip to content

Instantly share code, notes, and snippets.

View davidthewatson's full-sized avatar

david watson davidthewatson

View GitHub Profile
@davidthewatson
davidthewatson / fizzbuzz.py
Created January 14, 2012 17:13 — forked from kk6/fizzbuzz.py
無駄にitertoolsを駆使したFizzBuzz(Python3では動きません)
#-*- coding:utf8 -*-
# 無駄にitertoolsを駆使したFizzBuzz
from itertools import repeat, ifilterfalse, starmap, count
N = 31
LEN = range(1, N)
FIZZ, BUZZ = map(repeat, ("Fizz", "Buzz"))
fizz, buzz = (
@davidthewatson
davidthewatson / gist:1638451
Created January 19, 2012 06:43 — forked from joshfinnie/gist:1102695
Flask Signup/Login Template
from datetime import datetime
from flask import *
from flaskext.wtf import *
from flaskext.sqlalchemy import *
from werkzeug import generate_password_hash, check_password_hash
app = Flask()
app.config.from_pyfile('app_settings.py')
db = SQLAlchemy(app)
@davidthewatson
davidthewatson / gist:1638459
Created January 19, 2012 06:46 — forked from masahitojp/gist:706164
flask_mobile.py
from flask import Flask,request,g
#from middleware import SimpleMiddleware
app = Flask(__name__)
@app.before_request
def before_request():
from uamobile import detect
g.d = detect({'HTTP_USER_AGENT': request.headers["User-Agent"]})
@davidthewatson
davidthewatson / ext.py
Created January 19, 2012 06:48
File upload with Flask
import os
from PIL import Image
from flask import Flask, request, redirect, url_for
from werkzeug import secure_filename
from flaskext.uploads import (UploadSet, configure_uploads, IMAGES,
UploadNotAllowed)
app = Flask(__name__)
app.config['UPLOADED_PHOTOS_DEST'] = '/tmp/testuploadext'
@davidthewatson
davidthewatson / python-pep8.el
Created February 8, 2012 21:12 — forked from ieure/python-pep8.el
Run pep8.py on Python source in Emacs.
;;; python-pep8.el --- minor mode for running `pep8'
;; Copyright (c) 2009, 2010 Ian Eure <ian.eure@gmail.com>
;; Author: Ian Eure <ian.eure@gmail.com>
;; Keywords: languages python
;; Last edit: 2010-02-12
;; Version: 1.01
#Newbie programmer
def factorial(x):
if x == 0:
return 1
else:
return x * factorial(x - 1)
print factorial(6)
#First year programmer, studied Pascal
@davidthewatson
davidthewatson / gist:1979326
Created March 5, 2012 17:03 — forked from albarrentine/gist:1326477
Flask-Celery lazy configuration + app factory
# In myapp.tasks __init__.py
from celery import Celery
celery = Celery()
Task = celery.create_task_cls()
class MyBaseTask(Task):
abstract = True
# ...
@davidthewatson
davidthewatson / dataframe_list_of_dicts.py
Created March 8, 2012 13:55 — forked from gregglind/dataframe_list_of_dicts.py
Proposed handling for 'list of dicts' in pandas.DataFrame
"""
Sketch of proposed behaviour... make 'list of dicts'
create a (potentially) 'ragged' array, with autoguessed column names,
and sensible default values, where the keys don't match.
Current behaviour
In [215]: pandas.DataFrame([dict(a=1),dict(a=2)],columns=['a'])
Out[215]:
a
@davidthewatson
davidthewatson / gist:2054892
Created March 17, 2012 04:15
The Zen of R
# I am a scientist who has been using R for about 2 years. Today I achieved a measure of enlightenment into
# the zen of R, and I want to share it with you.
# I was simulating a set of independent random walks, which are formed by a multiplicative process. Here is
# the code I had built up over a few months of working on it and modifying it on and off as research
# questions changed:
TimeSteps <- 1000
Walks <- 100
ErrorMagnitude <- 0.03
@davidthewatson
davidthewatson / git-merged.rb
Created March 19, 2012 23:17 — forked from jcromartie/git-merged.rb
Git command to list merged (or unmerged with -u) branches
#!/bin/ruby
show_unmerged = ARGV[0] == '-u'
branches = `git branch -r`.split("\n")
branches.each do |branch|
is_merged = `git branch --contains #{branch}` =~ /master/
puts branch if show_unmerged ^ is_merged
end