Skip to content

Instantly share code, notes, and snippets.

View matyasfodor's full-sized avatar
🦕

Mátyás Fodor matyasfodor

🦕
  • BenevolentAI
  • London
View GitHub Profile
@matyasfodor
matyasfodor / parse_json_object.py
Last active July 16, 2017 19:47
This is how to parse JSON as object (recursively)
import json
from collections import namedtuple
x = json.loads(data, object_hook=lambda d: namedtuple('X', d.keys())(*d.values()))
def myfunc(foo):
...
barById = {b.id: b for b in foo.bar.bazs.keys()}
...
bazById = {baz.id: baz for baz in foo.bazs}
foo.bar.bazs = {bazById[k]:v for k, v in foo.bar.bazs.items()}
>>> 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'
nt = namedtuple('X', 'a b')
mydict = {}
dict[nt(1, 2)] = 5
dict[nt(1, 2)]
# 5
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__,
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__,
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)
// 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);
};
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) {