-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTagsTree.py
More file actions
84 lines (64 loc) · 1.99 KB
/
TagsTree.py
File metadata and controls
84 lines (64 loc) · 1.99 KB
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
84
import json
import copy
__author__ = 'centimo'
class TagsTree:
def __init__(self):
self._data = dict()
def __eq__(self, other):
"""
:type other: treeNode_Data
"""
if not isinstance(other, TagsTree):
return NotImplemented
return self._data == other._data
def __repr__(self):
return "TagsTree()({0._data! r})".format(self)
def loads(self, str_in):
self._data = json.loads(str_in)
def dumps(self):
"""
:return: Сериализованную _data в виде строки
"""
return json.dumps(self._data)
def clear(self):
self._data.clear()
def get(self, keys_list):
"""
:return:
:type keys_list: list
"""
if not isinstance(keys_list, list):
return NotImplemented
temp = self._data
i = 0
for key in keys_list:
temp2 = temp.get(key)
if temp2 is None:
break
else:
temp = temp2
i += 1
return list(temp.get(str('0')).keys()), i
def add_snippet(self, snippet_id, tags_list):
if not (isinstance(tags_list, list) & isinstance(snippet_id, str)):
return NotImplemented
temp = self._data
for tag in tags_list:
temp2 = temp.get(tag)
if temp2 is None:
temp.update({tag: {str('0'): {snippet_id: 1}}})
temp = temp.get(tag)
else:
temp2.get(str('0')).update({snippet_id: 1})
temp = temp2
return None
def delete_snippet(self, snippet_id, tags_list):
if not (isinstance(tags_list, list) & isinstance(snippet_id, str)):
return NotImplemented
temp = self._data
for tag in tags_list:
temp = temp.get(tag)
if temp is None:
return None
temp.get(str('0')).pop(snippet_id)
return None