Skip to content

Instantly share code, notes, and snippets.

View mattjmorrison's full-sized avatar

Matthew J Morrison mattjmorrison

View GitHub Profile
@mattjmorrison
mattjmorrison / oo.py
Created April 4, 2012 13:05
Python: Roll your own OO
def Module():
module_state = {}
def Class(val):
def get_val():
return val
class_state = {'get_val': get_val}
return class_state
@mattjmorrison
mattjmorrison / README
Created October 11, 2011 16:22
HTML Table Hilighting
Example In action: http://jsfiddle.net/r8wcY/
@mattjmorrison
mattjmorrison / example_one.py
Created October 6, 2011 03:54
Python Mixins
import datetime
import json
class Jsonable(object):
def date_handler(self, obj):
return obj.isoformat() if isinstance(obj, (datetime.datetime, datetime.date)) else None
def save_json(self, file_name):
with open(file_name, 'w') as output:
@mattjmorrison
mattjmorrison / dsl.py
Created September 28, 2011 23:34
Create a DSL in Python
import types
import re
buffer_regex = re.compile(r'^([\t\s]*)\w')
def dedent(code):
replace_chunk = ''
for line in code.split('\n'):
matches = buffer_regex.search(line)
if matches:
@mattjmorrison
mattjmorrison / mixin.py
Created September 22, 2011 00:32
Python Mixin Recipe
# http://littletutorials.com/2008/06/23/inheritance-not-for-code-reuse/
def mixin(mixin_cls):
def mixin_decorator(cls):
if not hasattr(cls, '__mixed__'):
cls.__mixed__ = []
for name, val in mixin_cls.__dict__.items():
if not name.startswith('__') and not name.endswith('__'):
if not hasattr(cls, name):
setattr(cls, name, val)
@mattjmorrison
mattjmorrison / synergy.conf
Created September 14, 2011 12:02
My Home Synergy Config
section: screens
master-control:
ubuntu:
mac:
end
section: links
master-control:
down(65, 100) = ubuntu(0, 100)
down(0, 30) = mac(0, 100)
ubuntu:
@mattjmorrison
mattjmorrison / codeigniter.php
Created August 31, 2011 13:40
PHP/CodeIgniter Example
public function create_offer(){
$merchant = new Merchant();
$merchant->where('id', 1);//TODO get currently logged in merchant
$merchant->get();
$this->load->library('form_validation');
$this->form_validation->set_rules('description', 'Description', 'required');
$this->form_validation->set_rules('from_time', 'From Time', 'required');
$this->form_validation->set_rules('to_time', 'To Time', 'required');
$this->form_validation->set_rules('deal', 'Deal', 'required');
@mattjmorrison
mattjmorrison / .git_and_virtualenv.bash
Created May 21, 2011 13:49
Add current virtualenv and git branch to shell prompt
#!/bin/bash
#
# Take your boring old prompt from this:
# matthew-morrisons-macbook:~$
# and cd into a git repo and add the current branch to the prompt for a more glorious experience:
# matthew-morrisons-macbook:django-media-masher(master)$
# then use `workon` to select a virtualenv and it will be added your prompt for additional glory:
# (media-masher)matthew-morrisons-macbook:django-media-masher(master)$
# then freely switch branches and virtualenvs and have your prompt always reflect what you're
# currently working with
@mattjmorrison
mattjmorrison / test_django_q.py
Created April 28, 2011 19:35
How to mock django's Q object
@patch('quote_options.models.LiabilityLimit.objects.filter')
@patch('quote.models.Q')
def should_filter_with_q_object_in_get_liability_limits(self, q_object, limit_filter):
manager_instance = Mock(spec=quote_models.LiabilityManager())
policy = mock.Mock()
q_instance = mock.Mock()
q_or_method = mock.Mock()
q_or_method.return_value = q_instance
q_instance.__or__ = q_or_method
@mattjmorrison
mattjmorrison / crop_and_resize.py
Created April 20, 2011 19:03
Resize and Crop images with Python and PIL
from PIL import Image, ImageChops
def trim(im, border):
bg = Image.new(im.mode, im.size, border)
diff = ImageChops.difference(im, bg)
bbox = diff.getbbox()
if bbox:
return im.crop(bbox)
def create_thumbnail(path, size):