This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]'|;}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class SampleManager(models.Manager): | |
def get_by_user(self, user): | |
self.filter(user=user) | |
class Sample(models.Model): | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import models | |
class SampleManager(models.Manager): | |
def get_first(self): | |
pass | |
def get_last(self): | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer