Skip to content

Commit ba910d8

Browse files
committed
Improved DottedPathDict.get
1 parent 426e794 commit ba910d8

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

telegram2elastic.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,29 @@ def bytes_to_human_readable(size_bytes: int):
5757

5858
class DottedPathDict(dict):
5959
def get(self, path, default=None):
60-
path = path.split(".", 1)
61-
62-
key = path.pop(0)
63-
64-
if key not in self:
65-
return default
66-
67-
if not path:
68-
return super().get(key)
60+
node = self
61+
path_parts = path.split(".")
6962

70-
nested_dict = self[key]
63+
for level in path_parts:
64+
if not level:
65+
continue
7166

72-
if not isinstance(nested_dict, DottedPathDict):
73-
return default
67+
if level in node:
68+
node = node[level]
69+
else:
70+
return default
7471

75-
return nested_dict.get(path[0], default)
72+
return node
7673

7774
def set(self, path, value):
7875
node = self
7976
path_parts = path.split(".")
8077

8178
for level in path_parts[:-1]:
82-
if level:
83-
node = node.setdefault(level, {})
79+
if not level:
80+
continue
81+
82+
node = node.setdefault(level, {})
8483

8584
node[path_parts[-1]] = value
8685

0 commit comments

Comments
 (0)