File size: 1,027 Bytes
ee61dae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | from copy import deepcopy
from pathlib import Path
import hashlib
import importlib.util
import json
import sys
def canonical(value):
return json.dumps(value,sort_keys=True,ensure_ascii=False,separators=(',',':'),allow_nan=False).encode()
def read(path):return json.loads(Path(path).read_text())
def write(path,value):
path=Path(path);path.parent.mkdir(parents=True,exist_ok=True)
path.write_bytes(canonical(value)+b'\n')
def module(name,path):
spec=importlib.util.spec_from_file_location(name,path);obj=importlib.util.module_from_spec(spec)
sys.modules[name]=obj;spec.loader.exec_module(obj);return obj
def equal(a,b):
if type(a) is bool or type(b) is bool:return type(a) is type(b) and a==b
if isinstance(a,(float,int)) and isinstance(b,(float,int)):return a==b
if type(a)!=type(b):return False
if isinstance(a,dict):return set(a)==set(b) and all(equal(a[k],b[k]) for k in a)
if isinstance(a,list):return len(a)==len(b) and all(equal(x,y) for x,y in zip(a,b))
return a==b
|