This file contains hidden or 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
>>> 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) |
This file contains hidden or 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
(((a+b)/2+(c+d)/2)/2, (a-b)/2, ((a+b)/2-(c+d)/2)/2, (c-d)/2) |
This file contains hidden or 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
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)) |
This file contains hidden or 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 |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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) |