def__getitem__(self, slash_key): keys = slash_key.split("/") dict_now = self.dict good_keys = [] for key in keys: if key in dict_now: dict_now = dict_now[key] good_keys.append(key) else: raise KeyError(f"Dict under key '{'/'.join(good_keys)}' does not have key '{key}'") ifisinstance(dict_now, dict): return SlashDict(dict_now) return dict_now
def__setitem__(self, slash_key, value): keys = slash_key.split("/") keys_num = len(keys) dict_now = self.dict for i inrange(keys_num): key = keys[i] if i == keys_num - 1: dict_now[key] = value break if key in dict_now: dict_now = dict_now[key] else: dict_now[key] = {} def__iter__(self): returniter(self.dict) def__str__(self): returnstr(self.dict) def__repr__(self): returnf"SlashDict({str(self.dict)})" defpop(self, slash_key): keys = slash_key.split("/") dict_now = self.dict good_keys = [] for key in keys[0:-1]: if key in dict_now: dict_now = dict_now[key] good_keys.append(key) else: raise KeyError(f"Dict under key '{'/'.join(good_keys)}' does not have key '{key}'") element = dict_now[keys[-1]] del dict_now[keys[-1]] return element defdeep_keys(self): self.deep_keys_list = [] for key in self.dict: self._key_dfs(key) return self.deep_keys_list def_key_dfs(self, key): ifisinstance(self[key], SlashDict): for key_ in self[key]: self._key_dfs(f"{key}/{key_}") else: self.deep_keys_list.append(key)