Skip to content

Instantly share code, notes, and snippets.

View haskel_tutorial.hs
import Data.List
import Text.Regex
import System.Random
import Data.Ord
type Point = (Float,Float)
type Color = (Int,Int,Int)
type Polygon = [Point]
type Person = [Int]
type Link = [Point]
@barahilia
barahilia / lines.py
Last active Jan 29, 2019
Lines on image
View lines.py
#!/usr/bin/env python
# http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html
import cv2
import numpy as np
from rectangles import Line, line_frequencies, rectangles
def lines_to_file(lines):
@barahilia
barahilia / freezable_defaultdict.py
Created Feb 8, 2017
A defaultdict that can be frozen after which it will not add new missing keys but still return a default object
View freezable_defaultdict.py
from collections import defaultdict
class freezable_defaultdict(dict):
def __init__(self, default_factory, *args, **kwargs):
self.frozen = False
self.default_factory = default_factory
super(freezable_defaultdict, self).__init__(*args, **kwargs)
def __missing__(self, key):
View discover-unit-tests.py
from unittest import TestLoader, TextTestRunner
def discover_and_run(verbose=False, names=None):
# names is None or a list [name, ...]
# name is test_suite[.class[.name]]
verbosity = 2 if verbose else 1
loader = TestLoader()
if names:
View list-unit-tests.py
from unittest import TestSuite
def print_tests(tests):
if isinstance(tests, TestSuite):
for child in tests:
print_tests(child)
else:
print tests.id() # package.module.TestClass.test_name
View fractal-hedge-maze-solution.py
"""Find exit from the fractal maze.
See http://puzzling.stackexchange.com/questions/37675/alice-and-the-fractal-hedge-maze
"""
from unittest import TestCase
from Queue import PriorityQueue
from collections import defaultdict
from itertools import count, product
@barahilia
barahilia / tamper.py
Last active Jul 28, 2016
Tampering class method in python (temporarily replacing/mocking/faking for unit test)
View tamper.py
import functools
def tamper(target, attr_name, mock_obj):
def decor(func):
@functools.wraps(func)
def worker(*args, **kwargs):
backup = getattr(target, attr_name)
try:
setattr(target, attr_name, mock_obj)
View yield-profiling.md

Demonstration of phenomenon, when caller cumulative time can be less than the callee, given it's the only caller. This may happen if callee is generator and caller returns callee object as is. Then profiler sees a lot of time spent in callee and almost no time at caller.