Skip to content

Instantly share code, notes, and snippets.

View mattjmorrison's full-sized avatar

Matthew J Morrison mattjmorrison

View GitHub Profile
class Var:
def __init__(self, name):
self.name = name
def __repr__(self):
return self.name
def __add__(self, other):
return Var('({0.name}+{1.name})'.format(self, other))
def __sub__(self, other):
return Var('({0.name}-{1.name})'.format(self, other))
(((a+b)/2+(c+d)/2)/2, (a-b)/2, ((a+b)/2-(c+d)/2)/2, (c-d)/2)
>>> x = "(((a+b)/2+(c+d)/2)/2, (a-b)/2, ((a+b)/2-(c+d)/2)/2, (c-d)/2)"
>>> eval(x, {'a':1, 'b':2, 'c':3, 'd':4})
(2.5, -0.5, -1.0, -0.5)
@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):
@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 / .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 / 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 / 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 / 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 / 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: