This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from collections import namedtuple | |
x = json.loads(data, object_hook=lambda d: namedtuple('X', d.keys())(*d.values())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def myfunc(foo): | |
... | |
barById = {b.id: b for b in foo.bar.bazs.keys()} | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bazById = {baz.id: baz for baz in foo.bazs} | |
foo.bar.bazs = {bazById[k]:v for k, v in foo.bar.bazs.items()} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> hash(nt(1, 2)) | |
3713081631934410656 | |
>>> hash(nt(1, {'a': 5})) | |
Traceback (most recent call last): | |
File "<stdin>", line 1, in <module> | |
TypeError: unhashable type: 'dict' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
nt = namedtuple('X', 'a b') | |
mydict = {} | |
dict[nt(1, 2)] = 5 | |
dict[nt(1, 2)] | |
# 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def record_factory(cls_name, field_names): | |
# ... | |
def _asdict(self): | |
return {k: getattr(self, k) for k in self.__slots__} | |
cls_attrs = dict(__slots__ = field_names, | |
__init__ = __init__, | |
__iter__ = __iter__, | |
__repr__ = __repr__, | |
__hash__ = __hash__, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def record_factory(cls_name, field_names): | |
# ... | |
def __hash__(self): | |
try: | |
return hash(self.id) | |
except AttributeError: | |
raise TypeError('unhashable type {}'.format(cls_name)) | |
# ... | |
cls_attrs = dict(__slots__ = field_names, | |
__init__ = __init__, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def record_factory(cls_name, field_names): | |
# ... | |
def __hash__(self): | |
try: | |
return hash(self.id) | |
except AttributeError: | |
raise TypeError('unhashable type {}'.format(cls_name)) | |
def __eq__(self, other): | |
try: | |
return hash(self) == hash(other) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Hack for monkey patching $scope.$watch | |
this.$scope.$watch = ((originalWatchFn) => { | |
const scope = this.$scope; | |
// tslint:disable-next-line | |
return function(watchExpression, listener, objectEquality) { | |
// tslint:disable-next-line | |
const wrappedCB = function () { | |
console.log(watchExpression, arguments[0], arguments[1]); | |
listener.apply(null, arguments); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var incognito = function(require, module, exports) { | |
"use strict"; | |
function isWebkitRequestFileSystem() { | |
return !!window.webkitRequestFileSystem | |
} | |
function isFirefoxIndexedDB() { | |
return !!window.indexedDB && /Firefox/.test(window.navigator.userAgent) | |
} | |
function isIE10PlusOrEdge() { | |
if (document.documentMode) { |
OlderNewer