Skip to content

Instantly share code, notes, and snippets.


Jared Suttles jaredks

View GitHub Profile
jaredks / dict.js
Created Dec 28, 2014
Python dict methods for JS objects
View dict.js
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 /
Last active Aug 29, 2015
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')
jaredks /
Created Aug 26, 2013
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
jaredks /
Last active May 23, 2019
OrderedDict subclass implementing insertion methods to adjust the order.
from collections import OrderedDict as _OrderedDict
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):