Skip to content

Instantly share code, notes, and snippets.

View mattjmorrison's full-sized avatar

Matthew J Morrison mattjmorrison

View GitHub Profile
sub _t{if(uc(ref($_[1]))=~m/^HASH/g){$s.='{';for(keys%{$_[1]}){$s.=$_.'=>';$_[0]->_t($_[1]->{$_});$s.=',';}$s.='},';}elsif(uc(ref($_[1]))=~m/^ARRAY/g){$s.='[';for(@{$_[1]}){$_[0]->_t($_);$s.=',';}$s.='],';}elsif(ref($_[1])){$s.='bless(';$_[0]->_t(scalar{%{$_[1]}});$s.=ref($_[1]).');';}else{$s.=qq|'$_[1]'|;}}
@mattjmorrison
mattjmorrison / models.py
Created February 20, 2011 04:43
Simple django mocking test example 1
from django.db import models
class SampleManager(models.Manager):
def get_by_user(self, user):
self.filter(user=user)
class Sample(models.Model):
pass
@mattjmorrison
mattjmorrison / tests.py
Created February 20, 2011 05:24
Simple django mocking test example 2
@mock.patch('django_testing.models.SampleManager.filter', mock.Mock())
def test_filters_by_user_with_patch(self):
user = mock.Mock()
models.Sample.objects.get_by_user(user)
models.Sample.objects.filter.assert_called_with(user=user)
@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)
@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 / 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 / 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)
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)
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 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