Skip to content

Instantly share code, notes, and snippets.

View mattjmorrison's full-sized avatar

Matthew J Morrison mattjmorrison

View GitHub Profile
>>> 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)
(((a+b)/2+(c+d)/2)/2, (a-b)/2, ((a+b)/2-(c+d)/2)/2, (c-d)/2)
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))
def haar(x, y):
average = (x + y) / 2
width = (x - y) / 2
return average, width
def wavelet(signal):
a, b, c, d = signal
a, b, c, d = haar(a, b) + haar(c, d)
a, c = haar(a, c)
return a, b, c, d
from functools import lru_cache
@lru_cache()
def fibonacci(n):
global _count
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(120))
def one_third(x):
return x / 3.0
def make_table(pairs):
'Unoptimized Example'
result = []
for value in pairs:
x = one_third(value)
result.append(format(value, '9.5f'))
return '\n'.join(result)
@mattjmorrison
mattjmorrison / unit_test.py
Created March 3, 2011 15:07
Django unit test runner that does not setup the database
from django.core.management.base import BaseCommand
from optparse import make_option
import sys
from django.test.simple import DjangoTestSuiteRunner
class UnitTestSuiteRunner(DjangoTestSuiteRunner):
def run_tests(self, test_labels, extra_tests=None, **kwargs):
suite = self.build_suite(test_labels, extra_tests)
@mattjmorrison
mattjmorrison / models.py
Created February 20, 2011 05:58
Simple django mocking test example 4 implementation code
from django.db import models
class SampleManager(models.Manager):
def get_first(self):
pass
def get_last(self):
pass
@mattjmorrison
mattjmorrison / tests.py
Created February 20, 2011 05:49
Simple django mocking test example 4
@mock.patch('django_testing.models.SampleManager.get_last')
@mock.patch('django_testing.models.SampleManager.get_first')
def test_result_of_one_query_in_args_of_another(self, get_first, get_last):
result = models.Sample.objects.get_first_and_last()
self.assertEqual((get_first.return_value, get_last.return_value), result)
@mattjmorrison
mattjmorrison / tests.py
Created February 20, 2011 05:33
Simple django mocking test example 3
@mock.patch('django_testing.models.SampleManager.filter')
def test_filters_by_user_with_patch_and_filter_passed_in(self, filter_method):
user = mock.Mock()
models.Sample.objects.get_by_user(user)
filter_method.assert_called_with(user=user)