Skip to content

Instantly share code, notes, and snippets.

View poros's full-sized avatar

Antonio Uccio Verardi poros

View GitHub Profile
@poros
poros / state_machine.py
Created March 25, 2017 17:40
Basic state machine implementation in Python using namedtuples as states and functions as transitions
from collections import namedtuple
import re
import sys
SIGNALFX_API = 'https://app.signalfx.com/#/'
RES_TYPE_RE= re.compile(r"^(?P<type>\S+?)\..*:$")
# STATES
@poros
poros / container.py
Last active October 30, 2015 01:40
Containers and magic methods
from md5 import md5
class FakeHashRing(object):
def __init__(self, nodes):
self.nodes = list(nodes)
def _hash(self, key):
return int(md5(str(key).encode('utf-8')).hexdigest(), 16) % len(self.nodes)
@poros
poros / cleanupjob_class.py
Created October 26, 2015 01:03
Using objects and decorators instead of classes with just one method and some constants
from job_class import Job
def delete_expired_stuff(time):
print "Delete expired stuff before %s" % time
class CleanupJob(Job):
# configuration: run at 5am
run_at = '05:00'
# implementation: nuke expired sessions
@poros
poros / storelast_gen.py
Created October 7, 2015 09:43
Generator storing the last value returned
class storelast(object):
def __init__(self,source):
self.source = source
def next(self):
item = self.source.next()
self.last = item
return item
def __iter__(self):
return self
@poros
poros / trace_gen.py
Created October 7, 2015 09:41
Trace a generator for debugging
def trace(source):
for item in source:
print item
yield item
r404 = trace(r for r in log if r['status'] == 404)
@poros
poros / stop_gen.py
Created October 7, 2015 09:23
Stop a generator midway
lines = (for line in open("run/foo/access-log"))
# PYTHON 3 ONLY!!!
for i,line in enumerate(lines):
print line
if i == 10: lines.close()
@poros
poros / tail_gen.py
Created October 7, 2015 09:18
Follow a file like tail -f with generators
import time
def follow(thefile):
thefile.seek(0,2)
while True:
line = thefile.readline()
if not line:
time.sleep(0.1)
continue
yield line
@poros
poros / genlog.py
Last active October 7, 2015 09:06
Process a file in a functional style with generators (no temporary lists!)
wwwlog = open("access-log")
total = 0
for line in wwwlog:
bytestr = line.rsplit(None,1)[1]
if bytestr != '-':
total += int(bytestr)
print "Total", total
@poros
poros / mock_side_effect.py
Created October 6, 2015 01:42
Mock side effect
# 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]
@poros
poros / pytest_parametrize_ids.py
Created October 6, 2015 01:38
Parametrized pytest testcase with dictionary of parameters and human readable testcase names
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