Skip to content

Instantly share code, notes, and snippets.

View mbarkhau's full-sized avatar
💭

mbarkhau

💭
  • Cyberspace
View GitHub Profile
@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 / 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 / multisort.py
Created September 5, 2014 12:32
Sort items in a list by multiple fields
from operator import itemgetter
def multisort(vals, sort_spec, in_place=False):
comparers = []
for i, spec in enumerate(sort_spec):
if isinstance(spec, tuple):
key, polarity = spec
elif isinstance(spec, (str, unicode)):
key = spec.replace("-", "", 1)
polarity = 1
@mbarkhau
mbarkhau / pp_timeit.py
Last active August 29, 2015 14:15
Pretty print replacement for timeit
# coding: utf-8
"""
Pretty print replacement for timeit
pp_timeit.auto_timeit runs a statement and automatically
determines the number of repetitions required to gain
satisfactory accuracy (similar to timeit.main).
pp_timeit uses auto_timeit and prints the result so that the
different magnitudes of results are visible by their alignment.
@mbarkhau
mbarkhau / install_rusti.sh
Last active August 29, 2015 14:22
Installation script for rusti with racer (autocompletion) support
cd rust-nightly-x86_64-unknown-linux-gnu
cd $HOME/rust-nightly-x86_64-unknown-linux-gnu
bash install.sh
cd $HOME
chown -R $USER:$USER rust-nightly-x86_64-unknown-linux-gnu
git clone https://github.com/murarth/rusti.git
chown -R $USER:$USER rusti
cd $HOME/rusti
cargo build
@mbarkhau
mbarkhau / unchain.py
Created June 23, 2015 16:41
unchain.py
import sys
import itertools as it
PY2 = sys.version_info.major == 2
if PY2:
range = xrange
def unchain(elems, chain_size):
@mbarkhau
mbarkhau / todo.py
Created July 14, 2015 12:18
todo completion plugin for sublime
import datetime as dt
import sublime_plugin
DEV_NAME = "mb"
class TODOCommand(sublime_plugin.EventListener):
def _todo_str(self):
@mbarkhau
mbarkhau / minify.py
Last active August 29, 2015 14:25
script to minify javascript using google closure compiler
#!/usr/bin/env python
from __future__ import print_function
import os
import sys
import json
from zlib import compress
from httplib import HTTPSConnection
from urllib import urlencode
COMPILER_JAR_PATH = os.path.expanduser("~/closure-compiler/compiler.jar")
@mbarkhau
mbarkhau / int2str.py
Last active August 29, 2015 14:26
Convert between str using arbitrary base and integer (positive integers only)
DIGITS = (
string.digits +
string.ascii_lowercase +
string.ascii_uppercase +
string.punctuation
)
def int2str_converter(base):
assert 2 <= base <= len(DIGITS)
@mbarkhau
mbarkhau / sortedcounter.py
Last active September 2, 2015 19:11
Failed attempt at a performant SortedCounter based on sortedcontainers.SortedDict and defaultdict
"""
Failed attempt at a performant SortedCounter based on
sortedcontainers.SortedDict and defaultdict
At least it is not useful for my use case which is insertion
heavy, YMMV.
Since the collections.Counter class doesn't keep the sort order
of its items, it has to recalculate the order for every call
to most_common is done. Also it doesn't provide any function