首页 Python 进阶 - deepget 和 deepset
文章
取消

Python 进阶 - deepget 和 deepset

实现类似于 deepcopy 的 3 个函数 deepget, deepset, deeppop,代码如下:

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python3.10

import re
import operator
import typing as t

from functools import reduce

T = t.TypeVar('T')


def parse_deepkey(deepkey: str, sep: str = '.') -> list:
    """
    深度路径分割

    :param deepkey: 深度路径
    :param sep: 分隔符
    :return: 列表格式的深度路径

    >>> parse_deepkey('a.b1')
    ['a', 'b1']
    >>> parse_deepkey('a.b2[0]')
    ['a', 'b2', 0]
    >>> parse_deepkey('a.b2[0].c2')
    ['a', 'b2', 0, 'c2']
    >>> parse_deepkey('a.b2[x=1].c2')
    ['a', 'b2', {'x': 1}, 'c2']
    >>> parse_deepkey('a.b2[x=1, y="z"].c2')
    ['a', 'b2', {'x': 1, 'y': 'z'}, 'c2']
    """
    keys = []
    for k in re.split(r'%s|\[' % re.escape(sep), deepkey):
        if k.endswith(']'):
            k = k[:-1]
            if k.isdigit():
                keys.append(int(k))
            else:
                try:
                    keys.append(eval(f'dict({k})'))
                except SyntaxError as e:
                    raise SyntaxError(f'Invalid expr `{k}` in deepkey `{deepkey}`: {str(e)}.')
        else:
            keys.append(k)
    return keys


def dump_deepkey(keys: list[str | int | dict], sep: str = '.') -> list:
    """
    深度路径合并

    :param keys: 切割后的深度路径
    :param sep: 分隔符
    :return: 合并后的深度路径

    >>> dump_deepkey(['a', 'b1'])
    'a.b1'
    >>> dump_deepkey(['a', 'b2', 0])
    'a.b2[0]'
    >>> dump_deepkey(['a', 'b2', 0, 'c2'])
    'a.b2[0].c2'
    >>> dump_deepkey(['a', 'b2', {'x': 1}, 'c2'])
    'a.b2[x=1].c2'
    >>> dump_deepkey(['a', 'b2', {'x': 1, 'y': 'z'}, 'c2'])
    'a.b2[x=1, y="z"].c2'
    """
    normkeys = []
    for key in keys:
        if isinstance(key, int):
            normkeys.append(f'[{key}]')
        elif isinstance(key, dict):
            parts = []
            for k, v in key.items():
                if isinstance(v, (int, float)):
                    parts.append(f'{k}={v}')
                else:
                    parts.append(f'{k}="{v}"')
            normkeys.append(f'[{", ".join(parts)}]')
        else:
            normkeys.append(key)
    return sep.join(normkeys).replace('.[', '[')


def deep_getitem(obj: object, key: t.Union[str, int, dict]) -> t.Any:
    """
    专为 deep* 函数设计的获取对象中的值函数。

    :param obj: 对象
    :param key: 键
    :return: 获取到的值
    """
    if obj is None:
        return None
    if isinstance(obj, list) and isinstance(key, dict):
        return GetableList(obj).get(musthave=True, **key)
    else:
        return operator.getitem(obj, key)


def deepget(obj: object, deepkey: str, sep: str = '.') -> t.Any:
    """
    深度获取对象中的值

    :param obj: 对象
    :param deepkey: 深度路径
    :param sep: 分隔符
    :return: 获取到的值

    >>> d = {
    ...     'a': {
    ...         'b1': 'c',
    ...         'b2': [1, 2, 3],
    ...         'b3': [{'x': 1, 'y': 'h'}, 
    ...                {'x': 2, 'y': 'i'},
    ...                {'x': 1, 'y': 'j'}],
    ...         'b4': None
    ...      }
    ... }
    >>> deepget(d, 'a.b1')
    'c'
    >>> deepget(d, 'a.b2[0]')
    1
    >>> deepget(d, 'a.b3[0].x')
    1
    >>> deepget(d, 'a.b3[x=1]')
    {'x': 1, 'y': 'h'}
    >>> deepget(d, 'a.b3[x=1, y="j"]')
    {'x': 1, 'y': 'j'}
    >>> deepget(d, 'a.b4[999]') == None
    True
    >>> deepget(d, 'a.b4.x') == None
    True
    """
    keys = parse_deepkey(deepkey, sep)
    return reduce(deep_getitem, keys, obj)


def deepset(obj: object, deepkey: str, value: any, sep: str = '.') -> None:
    """
    深度设置对象中的值。
    如果路径不存在则创建(路径中带索引的情况除外,如 a.b[0])

    :param obj: 对象
    :param deepkey: 深度路径
    :param value: 待设置的值
    :param sep: 分隔符

    >>> from pprint import pprint
    >>> d = {
    ...     'a': {
    ...         'b1': 'c',
    ...         'b2': [1, 2, 3],
    ...         'b3': [{'x': 1, 'y': 'h'}, 
    ...                {'x': 2, 'y': 'i'},
    ...                {'x': 1, 'y': 'j'}]
    ...      }
    ... }
    >>> deepset(d, 'a.b1', 'd')
    >>> pprint(d)
    {'a': {'b1': 'd',
           'b2': [1, 2, 3],
           'b3': [{'x': 1, 'y': 'h'}, {'x': 2, 'y': 'i'}, {'x': 1, 'y': 'j'}]}}
    >>> deepset(d, 'a.b2[0]', '-1')
    >>> pprint(d)
    {'a': {'b1': 'd',
           'b2': ['-1', 2, 3],
           'b3': [{'x': 1, 'y': 'h'}, {'x': 2, 'y': 'i'}, {'x': 1, 'y': 'j'}]}}
    >>> deepset(d, 'i.j', 'x')
    >>> pprint(d)
    {'a': {'b1': 'd',
           'b2': ['-1', 2, 3],
           'b3': [{'x': 1, 'y': 'h'}, {'x': 2, 'y': 'i'}, {'x': 1, 'y': 'j'}]},
     'i': {'j': 'x'}}
    >>> deepset(d, 'a.b2[999]', 4)
    >>> pprint(d)
    {'a': {'b1': 'd',
           'b2': ['-1', 2, 3, 4],
           'b3': [{'x': 1, 'y': 'h'}, {'x': 2, 'y': 'i'}, {'x': 1, 'y': 'j'}]},
     'i': {'j': 'x'}}
    >>> deepset(d, 'a.b4[0].c1[0]', 'x')
    >>> pprint(d)
    {'a': {'b1': 'd',
           'b2': ['-1', 2, 3, 4],
           'b3': [{'x': 1, 'y': 'h'}, {'x': 2, 'y': 'i'}, {'x': 1, 'y': 'j'}],
           'b4': [{'c1': ['x']}]},
     'i': {'j': 'x'}}
    >>> deepset(d, 'a.b3[x=1].y', 'k')
    >>> pprint(d)
    {'a': {'b1': 'd',
           'b2': ['-1', 2, 3, 4],
           'b3': [{'x': 1, 'y': 'k'}, {'x': 2, 'y': 'i'}, {'x': 1, 'y': 'j'}],
           'b4': [{'c1': ['x']}]},
     'i': {'j': 'x'}}
    >>> deepset(d, 'a.b3[x=1, y="j"].y', 'k')
    >>> pprint(d)
    {'a': {'b1': 'd',
           'b2': ['-1', 2, 3, 4],
           'b3': [{'x': 1, 'y': 'k'}, {'x': 2, 'y': 'i'}, {'x': 1, 'y': 'k'}],
           'b4': [{'c1': ['x']}]},
     'i': {'j': 'x'}}
    >>> deepset(d, 'a.b3[x=2]', {'x': 2, 'y': 'k'})
    >>> pprint(d)
    {'a': {'b1': 'd',
           'b2': ['-1', 2, 3, 4],
           'b3': [{'x': 1, 'y': 'k'}, {'x': 2, 'y': 'k'}, {'x': 1, 'y': 'k'}],
           'b4': [{'c1': ['x']}]},
     'i': {'j': 'x'}}
    >>> deepset(d, 'a.b3[x=8]', {'x': 8, 'y': 'k'})
    >>> pprint(d)
    {'a': {'b1': 'd',
           'b2': ['-1', 2, 3, 4],
           'b3': [{'x': 1, 'y': 'k'},
                  {'x': 2, 'y': 'k'},
                  {'x': 1, 'y': 'k'},
                  {'x': 8, 'y': 'k'}],
           'b4': [{'c1': ['x']}]},
     'i': {'j': 'x'}}
    """
    keys = parse_deepkey(deepkey, sep)
    for i, k in enumerate(keys[:-1]):
        try:
            child = deep_getitem(obj, k)
            if child is None:
                v = [] if isinstance(keys[i+1], (int, dict)) else {}
                operator.setitem(obj, k, v)
                child = deep_getitem(obj, k)
            obj = child
        except KeyError:
            obj[k] = [] if isinstance(keys[i+1], (int, dict)) else {}
            obj = obj[k]
        except (IndexError, AttributeError):
            obj.append([] if isinstance(keys[i+1], (int, dict)) else {})
            obj = obj[-1]
    if isinstance(obj, list) and isinstance(keys[-1], int) and len(obj) <= keys[-1]:
        obj.append(value)
    elif isinstance(obj, list) and isinstance(keys[-1], dict):
        v = GetableList(obj).get(musthave=False, **keys[-1])
        if v is None:
            obj.append(value)
        else:
            obj[obj.index(v)] = value
    else:
        operator.setitem(obj, keys[-1], value)


def deeppop(obj: object, deepkey: str, sep: str = '.') -> t.Any:
    """
    深度删除对象中的值

    :param obj: 对象
    :param deepkey: 深度路径
    :param sep: 分隔符
    :return: deepkey 存在时返回删除的值,否则返回 None。

    >>> d = {
    ...     'a': {
    ...         'b1': 'c',
    ...         'b2': [1, 2, 3],
    ...         'b3': [{'x': 1, 'y': 'h'}, 
    ...                {'x': 2, 'y': 'i'},
    ...                {'x': 1, 'y': 'j'}]
    ...      }
    ... }
    >>> deeppop(d, 'a.b1')
    'c'
    >>> deeppop(d, 'a.b2[0]')
    1
    >>> deeppop(d, 'a.b2[5]') == None
    True
    >>> deeppop(d, 'a.b4') == None
    True
    >>> deeppop(d, 'a.b3[x=1]')
    {'x': 1, 'y': 'h'}
    >>> deeppop(d, 'a.b3[x=1, y="j"].y')
    'j'
    >>> deeppop(d, 'a.b3[x=3]') == None
    True
    """
    keys = parse_deepkey(deepkey, sep)
    if len(keys) == 1:
        return obj.pop(keys[0])
    else:
        v = deepget(obj, dump_deepkey(keys[:-1], sep=sep), sep=sep)
        if v is not None:
            if isinstance(v, list):
                if isinstance(keys[-1], dict):
                    r = GetableList(v).get(musthave=False, **keys[-1])
                    if r:
                        v.remove(r)
                    return r
                else:
                    try:
                        return v.pop(keys[-1])
                    except IndexError:
                        return None
            else:
                return v.pop(keys[-1], None)
            

class GetableList(t.Generic[T], list):
    """
    可自定义获取元素的列表。
    """
    def get(self, musthave=True, **attrs) -> t.Optional[T]:
        """
        获取第一个属性都匹配的元素,否则返回 None 或报错。

        :param musthave: 如果为 True,无匹配的元素时则报错。
        :param attrs: 属性名和属性值。

        >>> class Person:
        ...     def __init__(self, name, age):
        ...         self.name = name
        ...         self.age = age
        ... 
        >>> people = GetableList[Person]([
        ...     Person("Alice", 30),
        ...     Person("Bob", 25),
        ...     Person("Bob", 26),
        ...     Person("Charlie", 35)
        ... ])
        >>> people.get(name='Bob').age
        25
        >>> people.get(name='Tom', musthave=False) == None
        True
        """
        for e in self:
            for k, v in attrs.items():
                if isinstance(e, dict):
                    value = e.get(k)
                else:
                    value = getattr(e, k, None)
                if value != v:
                    break
            else:
                return e
        if musthave:
            raise AttributeError(f'No such element: {attrs}')
        
    def gets(self, **attrs) -> 'GetableList[T]':
        """
        获取所有属性都匹配的元素。

        :param attrs: 属性名和属性值。

        >>> class Person:
        ...     def __init__(self, name, age):
        ...         self.name = name
        ...         self.age = age
        ... 
        >>> people = GetableList[Person]([
        ...     Person("Alice", 30),
        ...     Person("Bob", 25),
        ...     Person("Bob", 26),
        ...     Person("Charlie", 35)
        ... ])
        >>> people.gets(name='Bob')[0].age
        25
        >>> people.gets(name='Tom') == []
        True
        """
        elements = []
        for e in self:
            for k, v in attrs.items():
                if getattr(e, k) != v:
                    break
            else:
                elements.append(e)
        return elements
    
本文由作者按照 CC BY 4.0 进行授权