text
stringlengths 0
828
|
---|
function call the default behavior before or after your custom
|
logic. See `this example`_.
|
Duplicate and self-referential objects (aka reference loops) are
|
automatically handled internally, `as shown here`_.
|
.. _this example: http://sedimental.org/remap.html#sort_all_lists
|
.. _as shown here: http://sedimental.org/remap.html#corner_cases
|
""""""
|
# TODO: improve argument formatting in sphinx doc
|
# TODO: enter() return (False, items) to continue traverse but cancel copy?
|
if not callable(visit):
|
raise TypeError('visit expected callable, not: %r' % visit)
|
if not callable(enter):
|
raise TypeError('enter expected callable, not: %r' % enter)
|
if not callable(exit):
|
raise TypeError('exit expected callable, not: %r' % exit)
|
reraise_visit = kwargs.pop('reraise_visit', True)
|
if kwargs:
|
raise TypeError('unexpected keyword arguments: %r' % kwargs.keys())
|
path, registry, stack = (), {}, [(None, root)]
|
new_items_stack = []
|
while stack:
|
key, value = stack.pop()
|
id_value = id(value)
|
if key is _REMAP_EXIT:
|
key, new_parent, old_parent = value
|
id_value = id(old_parent)
|
path, new_items = new_items_stack.pop()
|
value = exit(path, key, old_parent, new_parent, new_items)
|
registry[id_value] = value
|
if not new_items_stack:
|
continue
|
elif id_value in registry:
|
value = registry[id_value]
|
else:
|
res = enter(path, key, value)
|
try:
|
new_parent, new_items = res
|
except TypeError:
|
# TODO: handle False?
|
raise TypeError('enter should return a tuple of (new_parent,'
|
' items_iterator), not: %r' % res)
|
if new_items is not False:
|
# traverse unless False is explicitly passed
|
registry[id_value] = new_parent
|
new_items_stack.append((path, []))
|
if value is not root:
|
path += (key,)
|
stack.append((_REMAP_EXIT, (key, new_parent, value)))
|
if new_items:
|
stack.extend(reversed(list(new_items)))
|
continue
|
if visit is _orig_default_visit:
|
# avoid function call overhead by inlining identity operation
|
visited_item = (key, value)
|
else:
|
try:
|
visited_item = visit(path, key, value)
|
except Exception:
|
if reraise_visit:
|
raise
|
visited_item = True
|
if visited_item is False:
|
continue # drop
|
elif visited_item is True:
|
visited_item = (key, value)
|
# TODO: typecheck?
|
# raise TypeError('expected (key, value) from visit(),'
|
# ' not: %r' % visited_item)
|
try:
|
new_items_stack[-1][1].append(visited_item)
|
except IndexError:
|
raise TypeError('expected remappable root, not: %r' % root)
|
return value"
|
4973,"def get_path(root, path, default=_UNSET):
|
""""""Retrieve a value from a nested object via a tuple representing the
|
lookup path.
|
>>> root = {'a': {'b': {'c': [[1], [2], [3]]}}}
|
>>> get_path(root, ('a', 'b', 'c', 2, 0))
|
3
|
The path format is intentionally consistent with that of
|
:func:`remap`.
|
One of get_path's chief aims is improved error messaging. EAFP is
|
great, but the error messages are not.
|
For instance, ``root['a']['b']['c'][2][1]`` gives back
|
``IndexError: list index out of range``
|
What went out of range where? get_path currently raises
|
``PathAccessError: could not access 2 from path ('a', 'b', 'c', 2,
|
1), got error: IndexError('list index out of range',)``, a
|
subclass of IndexError and KeyError.
|
You can also pass a default that covers the entire operation,
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.