Skip to content

Instantly share code, notes, and snippets.

View cdunklau's full-sized avatar

Colin Dunklau cdunklau

  • Chicago, IL, USA
View GitHub Profile
@cdunklau
cdunklau / test_undertest.py
Created November 20, 2014 10:15
One reason to import modules instead of names directly
import unittest
import mock # or `from unittest import mock` in py3.3+
import undertest1
import undertest2
class MockDemoTestCase(unittest.TestCase):
# we can directly patch givestring's module namespace.
#!/usr/bin/env python
"""
Find failed login attempts in auth log, save results in a sqlite database,
and print a report.
"""
from __future__ import print_function
import os
import re
import re
from ConfigParser import NoOptionError
class ValidationFailed(Exception):
def __init__(self, reason):
self.reason = reason
def __str__(self):
return repr(self)
@cdunklau
cdunklau / progress.py
Last active August 29, 2015 14:12
Progress-reporting line reader
import os
class ProgressLineReader(object):
def __init__(self, filepath, mode='r'):
self.path = filepath
self.total_size = os.stat(filepath).st_size
self.already_read = 0
self.fobj = open(filepath, mode)
def investigate_loop()
i = 0
numbers = []
while i < 6:
print "At the top i is %d" % i
numbers.append(i)
i = i + 1
print "Numbers now: ", numbers
@cdunklau
cdunklau / reqs.py
Last active September 12, 2015 16:00
Basic url processor
import sys
import itertools
import datetime
from twisted.internet import task, defer
from twisted.python import log
import treq
def main(reactor, urls, chunksize):
# Original
def process_item(self, item, spider):
try:
with self.conn:
query = 'INSERT INTO articles VALUES (?, ?, ?, ?, ?,(CURRENT_TIMESTAMP))'
try:
args = (item['url'], item['title'], item['intro'], item['body'], item['publication_date'])
except KeyError as kerr:
if kerr.args[0] == 'intro':
args = (item['url'], item['title'], "", item['body'], item['publication_date'])
@cdunklau
cdunklau / dicegames.py
Last active February 9, 2017 10:22
Dependancy injection demonstration for making random stuff deterministic
import random
def default_diceroller():
return random.randint(1, 6)
class CrapsGame(object):
_diceroller = default_diceroller
@cdunklau
cdunklau / foo.py
Last active January 13, 2017 10:28
SQLite3 Injection demonstration
import sqlite3
conn = sqlite3.connect(':memory:')
conn.execute('CREATE TABLE user (user_id INTEGER PRIMARY KEY, name TEXT)')
conn.commit()
cursor = conn.cursor()
for name in ['alice', 'bob', 'carol', 'david']:
cursor.execute('INSERT INTO user (name) VALUES (?)', (name,))
@cdunklau
cdunklau / coroutine_limiter.py
Last active October 19, 2017 23:41
Constrain number of simultanous HTTP requests with asyncio
import asyncio
import itertools
import aiohttp
import async_timeout
async def fetch_with_response_delay(session, delay):
if not 0 <= delay <= 10:
raise ValueError('Delay must be between 0 and 10 inclusive')