GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — deepdiff/deepdiff/path.py at master · epou/deepdiff Expand file tree/ Copy pathpath.pyMore file actions 169 lines (144 loc) · 4.99 KB / Copy pathpath.pyFile metadata and controls169 lines (144 loc) · 4.99 KB 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 import logging from ast import literal_eval from functools import lru_cache logger = logging.getLogger(__name__) GETATTR = 'GETATTR' GET = 'GET' class PathExtractionError(ValueError): pass class RootCanNotBeModified(ValueError): pass def _add_to_elements(elements, elem, inside): # Ignore private items if not elem: return if not elem.startswith('__'): try: elem = literal_eval(elem) except (ValueError, SyntaxError): pass action = GETATTR if inside == '.' else GET elements.append((elem, action)) DEFAULT_FIRST_ELEMENT = ('root', GETATTR) @lru_cache(maxsize=1024 * 128) def _path_to_elements(path, root_element=DEFAULT_FIRST_ELEMENT): """ Given a path, it extracts the elements that form the path and their relevant most likely retrieval action. >>> from deepdiff import _path_to_elements >>> path = "root[4.3].b['a3']" >>> _path_to_elements(path, root_element=None) [(4.3, 'GET'), ('b', 'GETATTR'), ('a3', 'GET')] """ if isinstance(path, (tuple, list)): return path elements = [] if root_element: elements.append(root_element) elem = '' inside = False prev_char = None path = path[4:] # removing "root from the beginning" brackets = [] inside_quotes = False for char in path: if prev_char == '\\': elem += char elif char in {'"', "'"}: elem += char inside_quotes = not inside_quotes if not inside_quotes: _add_to_elements(elements, elem, inside) elem = '' elif inside_quotes: elem += char elif char == '[': if inside == '.': _add_to_elements(elements, elem, inside) inside = '[' elem = '' # we are already inside. The bracket is a part of the word. elif inside == '[': elem += char else: inside = '[' brackets.append('[') elem = '' elif char == '.': if inside == '[': elem += char elif inside == '.': _add_to_elements(elements, elem, inside) elem = '' else: inside = '.' elem = '' elif char == ']': if brackets and brackets[-1] == '[': brackets.pop() if brackets: elem += char else: _add_to_elements(elements, elem, inside) elem = '' inside = False else: elem += char prev_char = char if elem: _add_to_elements(elements, elem, inside) return tuple(elements) def _get_nested_obj(obj, elements): for (elem, action) in elements: if action == GET: obj = obj[elem] elif action == GETATTR: obj = getattr(obj, elem) return obj def extract(obj, path): """ Get the item from obj based on path. Example: >>> from deepdiff import extract >>> obj = {1: [{'2': 'b'}, 3], 2: [4, 5]} >>> path = "root[1][0]['2']" >>> extract(obj, path) 'b' Note that you can use extract in conjunction with DeepDiff results or even with the search and :ref:`deepsearch_label` modules. For example: >>> from deepdiff import grep >>> obj = {1: [{'2': 'b'}, 3], 2: [4, 5]} >>> result = obj | grep(5) >>> result {'matched_values': OrderedSet(['root[2][1]'])} >>> result['matched_values'][0] 'root[2][1]' >>> path = result['matched_values'][0] >>> extract(obj, path) 5 .. note:: Note that even if DeepDiff tried gives you a path to an item in a set, there is no such thing in Python and hence you will get an error trying to extract that item from a set. If you want to be able to get items from sets, use the OrderedSet module to generate the sets. In fact Deepdiff uses OrderedSet as a dependency. >>> from deepdiff import grep, extract >>> obj = {"a", "b"} >>> obj | grep("b") Set item detected in the path.'set' objects do NOT support indexing. But DeepSearch will still report a path. {'matched_values': OrderedSet(['root[0]'])} >>> extract(obj, 'root[0]') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "deepdiff/deepdiff/path.py", line 126, in extract return _get_nested_obj(obj, elements) File "deepdiff/deepdiff/path.py", line 84, in _get_nested_obj obj = obj[elem] TypeError: 'set' object is not subscriptable >>> from deepdiff.helper import OrderedSetPlus >>> obj = OrderedSetPlus(["a", "b"]) >>> extract(obj, 'root[0]') 'a' """ elements = _path_to_elements(path, root_element=None) return _get_nested_obj(obj, elements) |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |