Skip to content

Instantly share code, notes, and snippets.

View poros's full-sized avatar

Antonio Uccio Verardi poros

View GitHub Profile
@poros
poros / type_subclass.py
Created October 6, 2015 01:31
Build a subclass on-the-fly in a pytest fixture
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):
@poros
poros / fixture_composition_mock.py
Last active January 19, 2021 17:02
Compose pytest fixtures at the same level and mock.patch
@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):
@poros
poros / dictionary_callbacks.py
Created October 6, 2015 01:02
Dictionary of callbacks or constructors
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)
@poros
poros / defaultdict_callable.py
Created October 6, 2015 00:03
Defaultdict with default custom class
from collection import defaultdict
class Number(object):
def __init__(self, N):
self.N = N
def __repr__(self):
return str(self.N)
d = defaultdict(Number)
@poros
poros / keyword_only_args.py
Last active October 23, 2015 01:34
Keyword-only arguments
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):
@poros
poros / star.py
Created October 5, 2015 23:11
Create APIs with multiple aguments instead of a list
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
@poros
poros / super_dependency_injection.py
Created October 5, 2015 22:58
Use super() for dependency injection
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
@poros
poros / product.py
Last active October 5, 2015 22:37
Combine several sets of items
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')]
@poros
poros / sequence.py
Created October 5, 2015 00:33
Make something iterable
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
@poros
poros / decorator.py
Last active October 28, 2015 22:49
Define a decorator
@f1(arg)
@f2
def func(): pass
# is equivalent to
def func(): pass
func = f1(arg)(f2(func))
def my_decorator(a_function_to_decorate):