Skip to content

Instantly share code, notes, and snippets.

Avatar
💭

mbarkhau

💭
  • Cyberspace
View GitHub Profile
@mbarkhau
mbarkhau / lazy.py
Created Mar 4, 2014
LazyObject and decorator
View lazy.py
# "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 Aug 29, 2015
safe python eval
View safe_eval.py
try:
import builtins
except ImportError:
# PY2 compat
import __builtin__ as builtins
import re
import math
from functools import partial
@mbarkhau
mbarkhau / multisort.py
Created Sep 5, 2014
Sort items in a list by multiple fields
View multisort.py
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 Aug 29, 2015
Pretty print replacement for timeit
View pp_timeit.py
# 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 Aug 29, 2015
Installation script for rusti with racer (autocompletion) support
View install_rusti.sh
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
View 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 Jul 14, 2015
todo completion plugin for sublime
View todo.py
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 Aug 29, 2015
script to minify javascript using google closure compiler
View minify.py
#!/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 Aug 29, 2015
Convert between str using arbitrary base and integer (positive integers only)
View int2str.py
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 Sep 2, 2015
Failed attempt at a performant SortedCounter based on sortedcontainers.SortedDict and defaultdict
View sortedcounter.py
"""
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