Skip to content

Instantly share code, notes, and snippets.

def switch(item):
two = Default()
one = Case(item, two)
return (one, two)
class Case(object):
def __init__(self, item, default):
self.default = default
self.skip = False
@orez-
orez- / continuation.py
Created May 20, 2014 17:05
Neat Scripts - A collection of neat scripts to show off features of the language used.
# Credit to daviddonna for most of this.
import inspect
def continuation(f):
def helper(f, args):
count = len(inspect.getargspec(f).args)
if len(args) == count:
return f(*args)
elif len(args) < count:
return lambda x, *eargs: helper(f, args + (x,) + tuple(eargs))
@orez-
orez- / breakable_with.py
Created June 10, 2014 16:01
Breakable 'with' block
# http://stackoverflow.com/questions/11195140/python-break-or-exit-out-of-with-statement/23665658#23665658
class fragile(object):
class Break(Exception):
"""Break out of the with statement"""
def __init__(self, value):
self.value = value
def __enter__(self):
@orez-
orez- / Default (OSX).sublime-keymap
Last active August 29, 2015 14:04
Sublime Settings
[
{ "keys": ["super+f"], "command": "show_panel", "args": {"panel": "find", "toggle": true} },
{ "keys": ["shift+tab"], "command": "unindent"},
{ "keys": ["super+g"], "command": "show_overlay", "args": {"overlay": "goto", "text": ":"} },
{ "keys": ["ctrl+tab"], "command": "next_view" },
{ "keys": ["ctrl+shift+tab"], "command": "prev_view" },
{ "keys": ["super+h"], "command": "show_panel", "args": {"panel": "replace"} },
{ "keys": ["super+shift+o"], "command": "prompt_add_folder"},
{ "keys": ["super+shift+r"], "command": "toggle_setting", "args": {"setting": "word_wrap"}},
{ "keys": ["tab"], "command": "indent", "context":
@orez-
orez- / git-stash-inbox.py
Last active October 2, 2021 16:18
Run through your git stashes, either committing them to branches or deleting them.
#!/usr/bin/env python2.7
import collections
import os
import re
import readline
import subprocess
tty_bold = '\x1b[1m'
tty_af = '\x1b[{}m'.format
@orez-
orez- / slice_when.py
Created December 17, 2015 01:33
Chunk `iterable` between elements that satisfy `predicate`.
import itertools
def slice_when(iterable, predicate):
"""
Chunk `iterable` between elements that satisfy `predicate`.
This method splits each chunk by passing adjacent elements from the iterable into `predicate`.
Splits occur where `predicate` returns true for adjacent elements.
@orez-
orez- / git-localprune.sh
Created January 20, 2016 23:01
Prune already merged branches.
#!/usr/bin/env bash
branch_name=$(git rev-parse --abbrev-ref HEAD)
if [[ "$branch_name" == "master" ]]; then
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
echo "Done!"
else
echo "Must be on master, not $branch_name!"
fi
try:
y = reversed(enumerate(range(5, 15, 2)))
except TypeError:
print("A problem.")
class enumerate(enumerate):
def __init__(self, iterable, start=0):
self.iterable = iterable
self.start = start
@orez-
orez- / io.py
Created August 7, 2017 15:45
py2 issue
print("Oh crap")
@orez-
orez- / main.py
Created February 7, 2019 21:11
Codec to enable ++x, x++, --x, and x-- in python3.8+
import plusplus
import module
module.main()