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
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
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
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
@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
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
test_params = { | |
'empty_line': ('', {}), | |
'get_ok': ('GET 200', {'request': 'GET', 'status': '200'}), | |
'get_not_found': ('GET 404', {'request': 'GET', 'status': '404'}), | |
} | |
@pytest.mark.parametrize('line,expected', test_params.values(), ids=test_params.keys()) | |
def test_decode(self, line, expected): | |
assert Decoder().decode(line) == expected | |
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
# raise exception | |
mock = Mock(side_effect=KeyError('foo')) | |
mock() | |
# KeyError: 'foo' | |
# return value based on argument | |
values = {'a': 1, 'b': 2, 'c': 3} | |
def side_effect(arg): | |
return values[arg] |
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
wwwlog = open("access-log") | |
total = 0 | |
for line in wwwlog: | |
bytestr = line.rsplit(None,1)[1] | |
if bytestr != '-': | |
total += int(bytestr) | |
print "Total", total | |
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
import time | |
def follow(thefile): | |
thefile.seek(0,2) | |
while True: | |
line = thefile.readline() | |
if not line: | |
time.sleep(0.1) | |
continue | |
yield line |