Skip to content

Instantly share code, notes, and snippets.

@jaredks
jaredks / listdict_setitem_subclass.py
Last active May 23, 2019 06:42
OrderedDict subclass implementing insertion methods to adjust the order.
from collections import OrderedDict as _OrderedDict
try:
from thread import get_ident as _get_ident
except ImportError:
from dummy_thread import get_ident as _get_ident
class ListDict(_OrderedDict):
def __init__(self, *args, **kwds):
try:
@jaredks
jaredks / setqueue.py
Created August 26, 2013 05:11
Set with constant time operations. Also maintains order and will remove older elements to make room for newer ones, allowing up to maxlen elements.
from collections import MutableSet as _MutableSet, OrderedDict as _OrderedDict
from itertools import chain as _chain
class SetQueue(_MutableSet):
def __init__(self, iterable=(), maxlen=None):
self._queue = _OrderedDict()
self._maxlen = maxlen
self.update(iterable)
@jaredks
jaredks / dict.js
Created December 28, 2014 08:41
Python dict methods for JS objects
Object.defineProperty(Object.prototype, "setdefault", {
value: function(key, value) {
if (!this.hasOwnProperty(key)) {
if (typeof value === 'undefined') value = null;
this[key] = value;
return value;
}
return this[key];
}
});
@jaredks
jaredks / html_diff.py
Last active August 29, 2015 14:07
Create html diff of two given lists
import difflib
import re
def html_diff(list1, list2, title='', left='', right=''):
"""Returns HTML as string representing a visual diff for the given lists."""
diff_html = difflib.HtmlDiff().make_file(list1, list2, left, right)
diff_html = re.sub(r'<title>.*</title>', '<title>%s</title>' % title, diff_html)
diff_html = diff_html.replace('content="text/html; charset=ISO-8859-1"',
'content="text/html; charset=UTF-8"')
diff_html = diff_html.encode('ascii', 'xmlcharrefreplace')