Skip to content

Instantly share code, notes, and snippets.

@barahilia
barahilia / tamper.py
Last active July 28, 2016 06:46
Tampering class method in python (temporarily replacing/mocking/faking for unit test)
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)
"""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 / yield-profiling.md
Created January 6, 2016 08:19
Yield profiling

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.

@barahilia
barahilia / points-compression.md
Last active August 29, 2015 14:07
Points compression algo
@barahilia
barahilia / highlight.md
Created October 12, 2014 19:01
Kramdown yellow highlight
Fill details here...
@barahilia
barahilia / tdd-trees-comp-c++
Last active August 29, 2015 14:05
TDD for trees comparison in C++
Build with: `g++ test.cpp && ./a.out`
For editing two files in `vim` run command `:vsplit tree.h`.
http://www.cs.swarthmore.edu/help/vim/windows.html
@barahilia
barahilia / json-pattern-matcher
Last active August 29, 2015 14:04
JSON pattern matching
Allows match JSON objects by pattern. Intended for APIs comparison and verification.
E.g.:
compare(
{ a: 1, b: ['x', 'y'] }, // pattern
{ a: 42, b: ['first', 'second', 'any', 'last'] } // object for check
);