Skip to content

Instantly share code, notes, and snippets.

Avatar

Jared Suttles jaredks

View GitHub Profile
@jaredks
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
jaredks / html_diff.py
Last active Aug 29, 2015
Create html diff of two given lists
View html_diff.py
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
jaredks / setqueue.py
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.
View setqueue.py
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 / listdict_setitem_subclass.py
Last active May 23, 2019
OrderedDict subclass implementing insertion methods to adjust the order.
View listdict_setitem_subclass.py
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: