-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdto.py
43 lines (30 loc) · 1.16 KB
/
dto.py
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
34
35
36
37
38
39
40
41
42
43
import codecs
from collections.abc import Iterable
import pickle
import json
def save_array(arr, path, print_len=False, sort_by=None, descending=False, sep=u'\t', encoding='utf-8'):
if sort_by:
arr = sorted(arr, key=lambda x: x[sort_by], reverse=descending)
with codecs.open(path, 'w', encoding) as f:
if print_len:
f.write('{}\n'.format(len(arr)))
for item in arr:
if sep and isinstance(item, Iterable):
f.write('{}\n'.format(sep.join([str(i) for i in item])))
else:
f.write('{}\n'.format(item))
def make_file(path):
save_array([], path)
def save_map(mp, path, reverse_kv=False, sort_by_key=False, **kwargs):
arr = [(v, k) if reverse_kv else (k, v) for k, v in mp.items()]
if sort_by_key:
kwargs['sort_by'] = 0
save_array(arr, path, **kwargs)
def saveobj(obj, fname):
with open(fname, 'wb') as f:
pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
def readobj(fname):
with open(fname, 'rb') as f:
return pickle.load(f)
def to_json(obj):
return json.dumps(obj, default=lambda o: o.__dict__, sort_keys=False, indent=4)