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 BaseRobot(object): | |
def __init__(self): | |
assert self.owner | |
@pytest.fixture | |
def robot(): | |
return type('TestRobot', (BaseRobot,), dict(owner='antonio'))() | |
# instead of | |
def TestRobot(BaseRobot): |
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
@pytest.fixture | |
def stream(): | |
return mock.Mock(spec=Stream) | |
@pytest.fixture | |
def output(): | |
return open('test.txt', 'w') | |
@pytest.fixture | |
def tailer(self, stream, output): |
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
menu = { | |
'square': lambda x: x * x, | |
'cube': lambda x: x * x * x, | |
'half': lambda x: x / 2, | |
'double': lambda x: x * 2, | |
} | |
command = raw_input() | |
menu[command](4) |
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 collection import defaultdict | |
class Number(object): | |
def __init__(self, N): | |
self.N = N | |
def __repr__(self): | |
return str(self.N) | |
d = defaultdict(Number) |
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 func(arg1, arg2, arg3=10, arg4=0): | |
... | |
# problem | |
func(1, 2, 10, 0) | |
# or ? | |
func(1, 2, 0, 10) | |
# PYTHON 3 ONLY | |
def func(arg1, arg2, *, arg3=10, arg4=0): |
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 func(msg, numbers): | |
print msg | |
for x in numbers: | |
print x | |
func("My numbers are:", [0, 1, 2]) | |
"My numbers are:" | |
0 | |
1 | |
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
from collection import Counter | |
from collection import OrderedDict | |
Counter('luppolo') | |
Counter({'p': 2, 'l': 2, 'o': 2, 'u': 1}) | |
help(Counter) | |
| Method resolution order: | |
| Counter | |
| __builtin__.dict | |
| __builtin__.object |
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 itertools import product | |
product(('ENDPOINT1', 'ENDPOINT2'), ('GET', 'POST'), ('200', '404', '500')) | |
# returns a generator | |
[('ENDPOINT1', 'GET', '200'), ('ENDPOINT1', 'GET', '404'), ('ENDPOINT1', 'GET', '500'), ('ENDPOINT1', 'POST', '200'), ('ENDPOINT1', 'POST', '404'), ('ENDPOINT1', 'POST', '500'), ('ENDPOINT2', 'GET', '200'), ('ENDPOINT2', 'GET', '404'), ('ENDPOINT2', 'GET', '500'), ('ENDPOINT2', 'POST', '200'), ('ENDPOINT2', 'POST', '404'), ('ENDPOINT2', 'POST', '500')] |
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 RoutingTable(object): | |
def __init__(self, crappy_rt): | |
self.crappy_rt = crappy_rt | |
def __len__(self): | |
return self.crappy_rt.getSize() | |
def __getitem__(self, index): | |
if index >= len(self): | |
raise IndexError |
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
@f1(arg) | |
@f2 | |
def func(): pass | |
# is equivalent to | |
def func(): pass | |
func = f1(arg)(f2(func)) | |
def my_decorator(a_function_to_decorate): | |