-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.py
83 lines (60 loc) · 2.09 KB
/
common.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import json
import dill
def intsec(list1: list, list2: list) -> list:
"""Simple intesection of two lists.
Args:
list1 (list): list1
list2 (list): list2
Returns:
list (list): intersection of lists
"""
return list(set.intersection(set(list1), set(list2)))
def diff(list1: list, list2: list) -> list:
"""Simple difference of items in list2 from list1.
Args:
list1 (list): list1
list2 (list): list2
Returns:
list (list): difference of lists
"""
return list(set(list1).difference(set(list2)))
def json_load(file_loc: str):
"""Helper function to open/close json file,
otherwise the python outputs warning that the file remains opened
Args:
file_loc (str): location of the file
Returns:
content (dict): content of json file, usually dictionary
"""
with open(file_loc, "rb") as f:
content = json.load(f)
return content
def json_dump(file_loc: str, content: object):
"""Helper function to open/close json file and dump content into it,
otherwise the python outputs warning that the file remains opened
Args:
file_loc (str): location of the file
content (object): data that will be saved to json, usually dictionary
"""
with open(file_loc, "wb") as f:
json.dump(content, f)
def dill_load(file_loc: str):
"""Helper function to open/close dill file,
otherwise the python outputs warning that the file remains opened
Args:
file_loc (str): location of the file
Returns:
content (dict): content of dill file, usually dictionary
"""
with open(file_loc, "rb") as f:
content = dill.load(f)
return content
def dill_dump(file_loc: str, content: object):
"""Helper function to open/close dill file and dump content into it,
otherwise the python outputs warning that the file remains opened
Args:
file_loc (str): location of the file
content (object): data that will be saved to dill, usually dictionary
"""
with open(file_loc, "wb") as f:
dill.dump(content, f)