Skip to content

Instantly share code, notes, and snippets.

@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
);
@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 / highlight.md
Created October 12, 2014 19:01
Kramdown yellow highlight
Fill details here...
@barahilia
barahilia / points-compression.md
Last active August 29, 2015 14:07
Points compression algo
@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.

"""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 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)
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:
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
@barahilia
barahilia / workaround.js
Created September 9, 2014 13:27
Workaround for R# complaining on use of an implicitly declared global variable
// This is a workaround for R# complaining on undefined global variables.
// In practice they come from and are defined by Jasmine and Protractor
// frameworks, so are not a real issues.
// Jasmine
var describe = function () { };
var beforeEach = function () { };
var afterEach = function () { };