Skip to content

Instantly share code, notes, and snippets.

@jakab922
jakab922 / fib_comp.hs
Created October 6, 2015 20:44
Simple fibonacci generator
import System.Environment
fib_gen = 0 : 1 : [a + b | (a, b) <- zip fib_gen (tail fib_gen)]
main = do
args <- getArgs
let first_arg = if null args then "1" else head args
let fa = read first_arg :: Int
print $ last $ take fa fib_gen
def check(A, B, C, index):
totals = [0 for _ in xrange(index)]
for i in xrange(index - 1, -1, -1):
totals[i] += B[i]
if C[i] != -1:
totals[C[i]] += totals[i]
return all([totals[i] <= A[i] for i in xrange(index)])
# BaseTestClient is flask.testing.FlaskClient
class TestClient(BaseTestClient):
"""
Test Client.
"""
def __exit__(self, exc_type, exc_value, tb):
self.preserve_context = False
top = _request_ctx_stack.top
@jakab922
jakab922 / bubba
Created April 11, 2014 11:32
i8042 error
[ 0.637461] Write protecting the kernel text: 4232k
[ 0.637500] Write protecting the kernel read-only data: 1272k
[ 0.647427] random: systemd-tmpfile urandom read with 0 bits of entropy available
[ 0.650489] systemd-udevd[47]: starting version 212
[ 0.653627] i8042: PNP: PS/2 Controller [PNP0303:PS2K] at 0x60,0x64 irq 1
[ 0.653634] i8042: PNP: PS/2 appears to have AUX port disabled, if this is incorrect please boot with i8042.nopnp
[ 0.654699] i8042: Warning: Keylock active
[ 0.654847] serio: i8042 KBD port at 0x60,0x64 irq 1
[ 0.660105] input: AT Translated Set 2 keyboard as /devices/platform/i8042/serio0/input/input0
[ 0.679289] sdhci: Secure Digital Host Controller Interface driver
@jakab922
jakab922 / sys.path.py
Created January 8, 2014 13:24
sys path manipulation
import sys
import os
from os.path import join
from mock import Mock, patch
from twisted.internet import defer
src_path = os.path.realpath(join(os.path.dirname(__file__), "../src/"))
current_path = os.path.realpath(os.path.dirname(__file__))
sys.path.insert(0, current_path)
sys.path.insert(0, src_path)
@jakab922
jakab922 / decla_mock.py
Last active December 23, 2015 17:39
Declarative mock definitions for tests on a Test class.
from unittest import TestCase
from mock import patch
class IDontNeedTheRest(Exception):
pass
DEBUG = False
@jakab922
jakab922 / measure_decorator.py
Created September 18, 2013 15:19
Measure decorator and context manager, for measuring execution time. For multiple processes, multiple instances are needed.
class Measure(object):
def __init__(self):
self.__elapsed = None
def __enter__(self):
self.__start = time()
def __exit__(self, *args):
self.__elapsed = time() - self.__start
@jakab922
jakab922 / cache_decorator.py
Last active July 5, 2018 11:51
Caching decorator with timeout. Might not be good for class instance/class methods.
from functools import wraps
from json import dumps
from datetime import datetime
def timeout_cache(max_timeout):
def timeout_cache_inner(fn):
fn._cache = {}
fn._timeout = max_timeout
@wraps(fn)
@jakab922
jakab922 / metaclass_reminder.py
Last active May 5, 2018 19:27
Python metaclass reminder
class MetaClass(type):
counter = 1
def __new__(meta, name, bases, dct):
""" Gets called when we read in the class definition for which this is the metaclass of"""
print "MetaClass.__new__(%s, %s, %s, %s)" % (meta, name, bases, dct)
dct['class_num'] = meta.counter
meta.counter += 1
return super(MetaClass, meta).__new__(meta, name, bases, dct)
@jakab922
jakab922 / gist:5904427
Created July 1, 2013 20:41
preserve order unique
your_list
unique_d = {}
for i, el in enumerate(your_list):
unique_d.setdefault(el, i)
preserved_order_unique = map(lambda x: x[0], sorted([(key, val) for key, val in unique_d.iteritems()], lambda y: y[1]))