-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathduplicates.py
executable file
·54 lines (37 loc) · 1.1 KB
/
duplicates.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
#!venv/bin/python
"""Remove duplicates."""
import json
from format import format_main_json
FILENAME = "epithets.json"
ROOT_PROPERTY = "epithets"
def read_epithets():
with open(FILENAME, encoding="utf-8") as file:
return json.load(file)
def write_epithets(epithets):
with open(FILENAME, "w", encoding="utf-8") as file:
json.dump(epithets, file)
def without_duplicates(epithets):
content = {}
cleaned = {ROOT_PROPERTY: content}
for epithet, mentions in epithets[ROOT_PROPERTY].items():
content[epithet] = without_duplicate_dicts(mentions)
return cleaned
def without_duplicate_dicts(dicts):
seen = set()
deduplicated = []
for each in dicts:
a_tuple = dict_to_tuple(each)
if a_tuple in seen:
continue
seen.add(a_tuple)
deduplicated.append(each)
return deduplicated
def dict_to_tuple(dictionary):
return tuple(dictionary.items())
def main():
epithets = read_epithets()
without = without_duplicates(epithets)
write_epithets(without)
format_main_json()
if __name__ == "__main__":
main()