Debian Codegraph @

pyinstrument/vendor/keypath.py

raw
1 # keypath vendored from https://github.com/fictorial/keypath
2
3 # keypath is released under the BSD license:
4
5 # Copyright 2016, Fictorial LLC
6
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10
11 # * Redistributions of source code must retain the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer.
14
15 # * Redistributions in binary form must reproduce the above
16 # copyright notice, this list of conditions and the following
17 # disclaimer in the documentation and/or other materials
18 # provided with the distribution.
19
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
21 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
24 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25 # OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29 # TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30 # THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # SUCH DAMAGE.
32 #
33 # Includes modifications by joerick, which fall under the same license.
34
35 from typing import Any
36
37
38 def value_at_keypath(obj: Any, keypath: str) -> Any:
39 """
40 Returns value at given key path which follows dotted-path notation.
41
42 >>> x = dict(a=1, b=2, c=dict(d=3, e=4, f=[2,dict(x='foo', y='bar'),5]))
43 >>> assert value_at_keypath(x, 'a') == 1
44 >>> assert value_at_keypath(x, 'b') == 2
45 >>> assert value_at_keypath(x, 'c.d') == 3
46 >>> assert value_at_keypath(x, 'c.e') == 4
47 >>> assert value_at_keypath(x, 'c.f.0') == 2
48 >>> assert value_at_keypath(x, 'c.f.-1') == 5
49 >>> assert value_at_keypath(x, 'c.f.1.y') == 'bar'
50
51 """
52 for part in keypath.split('.'):
53 if isinstance(obj, dict):
54 obj = obj.get(part, {})
55 elif type(obj) in [tuple, list]:
56 obj = obj[int(part)]
57 else:
58 obj = getattr(obj, part, {})
59 return obj
60
61
62 def set_value_at_keypath(obj: Any, keypath: str, val: Any):
63 """
64 Sets value at given key path which follows dotted-path notation.
65
66 Each part of the keypath must already exist in the target value
67 along the path.
68
69 >>> x = dict(a=1, b=2, c=dict(d=3, e=4, f=[2,dict(x='foo', y='bar'),5]))
70 >>> assert set_value_at_keypath(x, 'a', 2)
71 >>> assert value_at_keypath(x, 'a') == 2
72 >>> assert set_value_at_keypath(x, 'c.f.-1', 6)
73 >>> assert value_at_keypath(x, 'c.f.-1') == 6
74 """
75 parts = keypath.split('.')
76 for part in parts[:-1]:
77 if isinstance(obj, dict):
78 obj = obj[part]
79 elif type(obj) in [tuple, list]:
80 obj = obj[int(part)]
81 else:
82 obj = getattr(obj, part)
83 last_part = parts[-1]
84 if isinstance(obj, dict):
85 obj[last_part] = val
86 elif type(obj) in [tuple, list]:
87 obj[int(last_part)] = val
88 else:
89 setattr(obj, last_part, val)
90 return True