Skip to content

Instantly share code, notes, and snippets.

View aezell's full-sized avatar
😶

Alex Ezell aezell

😶
View GitHub Profile
@aezell
aezell / shakes.py
Created August 27, 2012 21:20
Sunset and Altitude for Shakespeare's Plays
# Caveat, the dates included are Julian but PyEphem and Python only really know about Gregorian.
# Additionally, not all of these performances were at The Globe so we should be moving the Observer
# accordingly. It still largely proves the hypothesis.
import argparse
from datetime import datetime
import ephem
from prettytable import PrettyTable
@aezell
aezell / test_twilio.py
Created August 29, 2012 19:58
Mock Twilio
@patch('twilio.rest.resources.SmsMessages')
def test_send_sms_valid_args(self, MockClass):
instance = MockClass.return_value
instance.create.return_value = None
to_number = 5555555555
msg = 'Hello world'
send_sms(to_number, msg)
instance.create.assert_called_once_with(to='+1' + str(to_number),
body=msg, from_=settings.TWILIO_FROM_NUM)
@aezell
aezell / nonce.py
Created September 13, 2012 13:29
nonce creation
# PHP code
function create_nonce()
{
$mt = microtime();
$rand = mt_rand();
return preg_replace('/[^a-zA-Z0-9]/', '', base64_encode(sha1($mt . $rand, true)));
}
# Python code
@aezell
aezell / sa_middleware.py
Created September 14, 2012 18:46
SA Middleware
class SqlAlchemyMiddleware:
"""
A simple SA middleware class that creates a global scoped session from the
DATABASE_URL parameter in the settings file.
"""
def process_view(self, request, view_func, view_args, view_kwargs):
from django.conf import settings
from django.http import HttpResponseRedirect
try:
@aezell
aezell / verbatim.py
Created October 14, 2012 00:32
verbatim Django template tag
"""
jQuery templates use constructs like:
{{if condition}} print something{{/if}}
This, of course, completely screws up Django templates,
because Django thinks {{ and }} mean something.
Wrap {% verbatim %} and {% endverbatim %} around those
blocks of jQuery templates and this will try its best
@aezell
aezell / backbone_app.js
Created October 14, 2012 01:27
Backbone Attempt
var app = app || {};
$(function(){
var app.Signature = Backbone.Model.extend({
initialize: function(){
return $.ajax({
url: '/auth/'
});
}
});
@aezell
aezell / dict_comp.py
Created November 14, 2012 15:39
Dictionary comprehension
# stolen from http://stackoverflow.com/a/7276556/64266
def countChar(word):
return dict((item, word.count(item)) for item in set(word))
>>> countChar('google')
{'e': 1, 'g': 2, 'o': 2, 'l': 1}
>>> countChar('apple')
{'a': 1, 'p': 2, 'e': 1, 'l': 1}
@aezell
aezell / empty_object.js
Created November 28, 2012 16:14
Is Javascript Object Empty?
// found here http://stackoverflow.com/a/2673229/64266
function isEmptyObject(obj) {
for(var prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return true;
}
@aezell
aezell / fabfile.py
Created April 24, 2013 18:41
Fabfile fun
# Usage: fab provision:type=cache_server
from fabric.api import task, run
import boto
@task
def provision(type="cache_server"):
# do some boto stuff
if type == "app_server":
# build an app server
An elder was teaching his grandchildren about life. He said to them,
"A fight is going on inside me.. it is a terrible fight and it is
between two wolves. One wolf represents fear, anger, envy, sorrow,
regret, greed, arrogance, self-pity, guilt, resentment, inferiority,
lies, false pride, superiority, and ego.
The other stands for joy, peace, love, hope, sharing, serenity,
humility, kindness, benevolence, friendship, empathy, generosity,
truth, compassion, and faith."