Skip to content

Instantly share code, notes, and snippets.

View mbarkhau's full-sized avatar
💭

mbarkhau

💭
  • Cyberspace
View GitHub Profile
@mbarkhau
mbarkhau / safe_eval.py
Last active August 29, 2015 14:04
safe python eval
try:
import builtins
except ImportError:
# PY2 compat
import __builtin__ as builtins
import re
import math
from functools import partial
@mbarkhau
mbarkhau / lazy.py
Created March 4, 2014 00:21
LazyObject and decorator
# "Of being lazy" February 2, 2013 by Paul Masurel
# http://fulmicoton.com/posts/lazy/
class LazyObject(object):
__slots__ = [ "_recipe", "_result", "_evaluated" ]
def __init__(self, recipe):
object.__setattr__(self, "_recipe", recipe)
object.__setattr__(self, "_result", None)
@mbarkhau
mbarkhau / gen_wallet_seed.py
Last active May 5, 2022 23:35
Snippet to create electrum seed from rolls of a six sided dice and a secret passphrase.
#!/usr/bin/env python
"""Electrum Wallet Seed Generator (use at own risk).
Usage:
-h --help
-s --seed-bits=<bits> Size of the wallet seed in bits
[default: 256]
-i --iterations=<iterations> Number of pbkdf2 iterations
[default: 500000]
def eval_sexpr(sexpr):
fn = sexpr[0]
args = sexpr[1:]
args = tuple(eval_sexpr(e) if isinstance(e, tuple) else e for e in args)
return fn(*args)
import operator as op
eval_sexpr((op.add, (op.mul, 3, 4), (op.mul, 2, 5)))
@mbarkhau
mbarkhau / freshfile.py
Last active December 20, 2015 15:59
Class which will always give the fresh contents of a file
import codecs
from os.path import getmtime
from time import time
class FreshFile(object):
def __init__(self, filename, mode='r', encoding='utf-8', process_func=None,
max_age=0):
self._filename = filename
@mbarkhau
mbarkhau / redict.py
Last active December 18, 2015 12:49
Limited size dictionar with Random Eviction
from random import randint
class REDict(dict):
def __init__(self, maxsize, *args, **kwargs):
self._maxsize = maxsize
self._keys = []
self._evict_cb = kwargs.pop('_evict_cb', None)
dict.__init__(self, *args, **kwargs)
class Memoizer(object):
"""Memoizer for pure functions with positional hashable arguments.
Eviction from cache is done more or less at random, which
in practice is surprisingly close to a LRU strategy.
"""
def __init__(self, func, maxsize=10000):
self.func = func
self.maxsize = maxsize
@mbarkhau
mbarkhau / filepaths.py
Last active December 15, 2015 03:09
Iterate over all filenames in a directory tree
import os
def filepaths(rootdir, ext_filter=None):
if isinstance(ext_filter, basestring):
_filter = lambda p: p.endswith(ext_filter)
elif isinstance(ext_filter, tuple):
_filter = lambda p: os.path.splitext(p)[1] in ext_filter
else:
_filter = ext_filter
@mbarkhau
mbarkhau / KSON.js
Last active December 26, 2015 13:31
KSON: Keyless Schemafied Object Notation
/* KSON: Keyless Schemafied Object Notation
*
* Version: 0.1 Alpha
*
* A serialization format with two goals in mind:
* 1. Easily parsable using minimal javascript.
* 2. Reduce serialized size compared to JSON.
*
* 1. is accomplished by using the (comparativly fast) JSON parse/stringify
* functions, thus reducing the task of KSON to packing/unpacking the values
@mbarkhau
mbarkhau / unicode_test.txt
Last active June 23, 2016 14:27
i18n test string
イërnâ七iônàنلzætiøn