Skip to content

Instantly share code, notes, and snippets.

@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)
import re
from ConfigParser import NoOptionError
class ValidationFailed(Exception):
def __init__(self, reason):
self.reason = reason
def __str__(self):
return repr(self)
#!/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
@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.
@cdunklau
cdunklau / .gitignore
Last active August 29, 2015 14:08
SQLite3-backed mutable set
*.py[co]
@cdunklau
cdunklau / hobbes_and_bacon.txt
Created August 3, 2014 19:54
Hobbes and Bacon: all four comics. Tribute to Calvin and Hobbes
http://www.pantsareoverrated.com/archive/2011/05/10/hobbes-and-bacon/
http://www.pantsareoverrated.com/archive/2011/05/12/hobbes-and-bacon-002/
http://www.pantsareoverrated.com/archive/2011/10/11/hobbes-and-bacon-03-2/
http://www.pantsareoverrated.com/archive/2011/10/13/hobbes-and-bacon-04-2/
@cdunklau
cdunklau / OpenCV_transform.py
Last active June 4, 2017 09:00
One use case for staticmethod
class _Transform:
transform = None
transargs = ()
def __call__(self, image):
"""
Run the transformation and return the tranformed image data
"""
args = [getattr(self, arg) for arg in self.transargs]
return self.transform(image, *args)
@cdunklau
cdunklau / release_me.txt
Last active August 29, 2015 14:02
Great reggae song
Release Me (The Fear)
as performed by Cas Haley (arrangement, maybe song?)
https://www.youtube.com/watch?v=zRgvtu62sYQ
A#m Ddim7 D#m9 G# F# D#m C# Fm
e|---1------x-1----x------4------2------6------4------1---|
B|---2------6-0----6------4------2------7------6------1---|
G|---3------4-1----6------5------3------8------6------1---|
D|---3------6-0----4------6------4------8------6------3---|
@cdunklau
cdunklau / registry_pattern.py
Last active August 29, 2015 14:02
Registry pattern - avoid magic, be (reasonably) explicit
registry = {}
def registered(cls):
registry[cls.__name__] = cls
return cls
@registered
class Foo(object):
def introduce(self):
@cdunklau
cdunklau / spacepasteit.py
Created May 28, 2014 11:50
Pastebin a file to bpaste.net
#!/usr/bin/env python
import argparse
import requests
SPACEPASTE_URL = 'http://bpaste.net/'
parser = argparse.ArgumentParser()
parser.add_argument(
'file',