cengal.data_manipulation.dict_path.versions.v_0.dict_path

  1#!/usr/bin/env python
  2# coding=utf-8
  3
  4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
  5# 
  6# Licensed under the Apache License, Version 2.0 (the "License");
  7# you may not use this file except in compliance with the License.
  8# You may obtain a copy of the License at
  9# 
 10#     http://www.apache.org/licenses/LICENSE-2.0
 11# 
 12# Unless required by applicable law or agreed to in writing, software
 13# distributed under the License is distributed on an "AS IS" BASIS,
 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15# See the License for the specific language governing permissions and
 16# limitations under the License.
 17
 18
 19__all__ = ['get_dict_item', 'get_dict_item_default', 'set_dict_item', 'change_dict_item', 'del_dict_item', 'try_del_dict_item', 'srt_to_dict_path', 'dict_path_to_str']
 20
 21
 22"""
 23Module Docstring
 24Docstrings: http://www.python.org/dev/peps/pep-0257/
 25"""
 26
 27__author__ = "ButenkoMS <gtalk@butenkoms.space>"
 28__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
 29__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
 30__license__ = "Apache License, Version 2.0"
 31__version__ = "4.4.1"
 32__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
 33__email__ = "gtalk@butenkoms.space"
 34# __status__ = "Prototype"
 35__status__ = "Development"
 36# __status__ = "Production"
 37
 38
 39from typing import Hashable, Dict, List
 40
 41
 42def get_dict_item(data: Dict, keys: List[Hashable]):
 43    rv = data
 44    for key in keys:
 45        rv = rv[key]
 46    
 47    return rv
 48
 49
 50def get_dict_item_default(data: Dict, keys: List[Hashable], default=None):
 51    rv = data
 52    for key in keys:
 53        try:
 54            rv = rv[key]
 55        except KeyError:
 56            return default
 57    
 58    return rv
 59
 60
 61def set_dict_item(data: Dict, keys: List[Hashable], value):
 62    rv = data
 63    path_len = len(keys)
 64    for index, key in enumerate(keys):
 65        if index == path_len - 1:
 66            rv[key] = value
 67            break
 68
 69        if key not in rv:
 70            rv[key] = dict()
 71
 72        rv = rv[key]
 73
 74
 75def change_dict_item(data: Dict, keys: List[Hashable], value):
 76    rv = data
 77    path_len = len(keys)
 78    for index, key in enumerate(keys):
 79        if index == path_len - 1:
 80            rv[key] = value
 81            break
 82
 83        rv = rv[key]
 84        
 85
 86def del_dict_item(data: Dict, keys: List[Hashable]):
 87    rv = data
 88    path_len = len(keys)
 89    for index, key in enumerate(keys):
 90        rv = rv[key]
 91        if index == path_len - 2:
 92            del rv[keys[-1]]
 93            break
 94    
 95    return rv
 96        
 97
 98def try_del_dict_item(data: Dict, keys: List[Hashable]) -> bool:
 99    try:
100        del_dict_item(data, keys)
101        return True
102    except KeyError:
103        return False
104
105
106def srt_to_dict_path(str_path) -> List[str]:
107    return eval(str_path)
108
109
110def dict_path_to_str(path: List[str]) -> str:
111    path_str = ', '.join([f"'{sub_path}'" for sub_path in path])
112    return f'[{path_str}]'
def get_dict_item(data: Dict, keys: List[Hashable]):
43def get_dict_item(data: Dict, keys: List[Hashable]):
44    rv = data
45    for key in keys:
46        rv = rv[key]
47    
48    return rv
def get_dict_item_default(data: Dict, keys: List[Hashable], default=None):
51def get_dict_item_default(data: Dict, keys: List[Hashable], default=None):
52    rv = data
53    for key in keys:
54        try:
55            rv = rv[key]
56        except KeyError:
57            return default
58    
59    return rv
def set_dict_item(data: Dict, keys: List[Hashable], value):
62def set_dict_item(data: Dict, keys: List[Hashable], value):
63    rv = data
64    path_len = len(keys)
65    for index, key in enumerate(keys):
66        if index == path_len - 1:
67            rv[key] = value
68            break
69
70        if key not in rv:
71            rv[key] = dict()
72
73        rv = rv[key]
def change_dict_item(data: Dict, keys: List[Hashable], value):
76def change_dict_item(data: Dict, keys: List[Hashable], value):
77    rv = data
78    path_len = len(keys)
79    for index, key in enumerate(keys):
80        if index == path_len - 1:
81            rv[key] = value
82            break
83
84        rv = rv[key]
def del_dict_item(data: Dict, keys: List[Hashable]):
87def del_dict_item(data: Dict, keys: List[Hashable]):
88    rv = data
89    path_len = len(keys)
90    for index, key in enumerate(keys):
91        rv = rv[key]
92        if index == path_len - 2:
93            del rv[keys[-1]]
94            break
95    
96    return rv
def try_del_dict_item(data: Dict, keys: List[Hashable]) -> bool:
 99def try_del_dict_item(data: Dict, keys: List[Hashable]) -> bool:
100    try:
101        del_dict_item(data, keys)
102        return True
103    except KeyError:
104        return False
def srt_to_dict_path(str_path) -> List[str]:
107def srt_to_dict_path(str_path) -> List[str]:
108    return eval(str_path)
def dict_path_to_str(path: List[str]) -> str:
111def dict_path_to_str(path: List[str]) -> str:
112    path_str = ', '.join([f"'{sub_path}'" for sub_path in path])
113    return f'[{path_str}]'