Skip to content

Instantly share code, notes, and snippets.

@rmartinjak
rmartinjak / sliding_window.py
Last active October 4, 2017 19:20
sliding window generator, should work in python>=2.6
from itertools import chain, repeat, islice
from collections import deque
def sliding_window(iterable, n, fill=False, fillvalue=None):
it = iter(iterable)
if fill:
it = chain(it, repeat(fillvalue, n - 1))
w = deque(islice(it, n - 1))
for x in it:
w.append(x)
import functools
import contextlib
def cmwrap(cm, *cmargs, **cmkwargs):
"""Decorator wrapping a context manager around a function call."""
def wrapper(f):
@functools.wraps(f)
def wrapped_f(*args, **kwargs):
with cm(*cmargs, **cmkwargs):
#!/usr/bin/env python3
"""Interface to a dict stored as json, useful for shell scripts."""
import argparse
import json
parser = argparse.ArgumentParser(description="set/list/get dict contents")
parser.add_argument("file", metavar="FILE", nargs=1, help='Path to dict file')
parser.add_argument("command", metavar="CMD", nargs=1,