Skip to content

Instantly share code, notes, and snippets.

@mahmoud
Created January 13, 2023 21:14
Show Gist options
  • Save mahmoud/a0923541c2c59c7cb167802c0d09a895 to your computer and use it in GitHub Desktop.
Save mahmoud/a0923541c2c59c7cb167802c0d09a895 to your computer and use it in GitHub Desktop.
Contrasting Python default stack trace vs Glom data trace
# python 3.10 with glom 23
>>> import glom
>>> target = {'planets': [{'name': 'earth', 'moons': 1}]}
>>> spec = ('planets', ['rings']) # a spec we expect to fail
>>> glom.glom(target, spec)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/mahmoud/projects/glom/glom/core.py", line 2297, in glom
raise err
glom.core.PathAccessError: error raised while processing, details below.
Target-spec trace (most recent last):
- Target: {'planets': [{'moons': 1, 'name': 'earth'}]}
- Spec: ('planets', ['rings'])
- Spec: 'planets'
- Target: [{'moons': 1, 'name': 'earth'}]
- Spec: ['rings']
- Target: {'moons': 1, 'name': 'earth'}
- Spec: 'rings'
glom.core.PathAccessError: could not access 'rings', part 0 of Path('rings'), got error: KeyError('rings')
# python 3.10 with glom==20.5.0
>>> import glom
>>> target = {'planets': [{'name': 'earth', 'moons': 1}]}
>>> spec = ('planets', ['rings']) # a spec we expect to fail
>>> glom.glom(target, spec)
Traceback (most recent call last):
File "/home/mahmoud/projects/glom/tmpvenv/lib/python3.10/site-packages/glom/core.py", line 1204, in _t_eval
cur = get(cur, arg)
KeyError: 'rings'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/mahmoud/projects/glom/tmpvenv/lib/python3.10/site-packages/glom/core.py", line 1819, in glom
ret = _glom(target, spec, scope)
File "/home/mahmoud/projects/glom/tmpvenv/lib/python3.10/site-packages/glom/core.py", line 1840, in _glom
return scope[MODE](target, spec, scope)
File "/home/mahmoud/projects/glom/tmpvenv/lib/python3.10/site-packages/glom/core.py", line 1849, in AUTO
return _handle_tuple(target, spec, scope)
File "/home/mahmoud/projects/glom/tmpvenv/lib/python3.10/site-packages/glom/core.py", line 1549, in _handle_tuple
nxt = scope[glom](res, subspec, scope)
File "/home/mahmoud/projects/glom/tmpvenv/lib/python3.10/site-packages/glom/core.py", line 1840, in _glom
return scope[MODE](target, spec, scope)
File "/home/mahmoud/projects/glom/tmpvenv/lib/python3.10/site-packages/glom/core.py", line 1847, in AUTO
return _handle_list(target, spec, scope)
File "/home/mahmoud/projects/glom/tmpvenv/lib/python3.10/site-packages/glom/core.py", line 1537, in _handle_list
val = scope[glom](t, subspec, scope)
File "/home/mahmoud/projects/glom/tmpvenv/lib/python3.10/site-packages/glom/core.py", line 1840, in _glom
return scope[MODE](target, spec, scope)
File "/home/mahmoud/projects/glom/tmpvenv/lib/python3.10/site-packages/glom/core.py", line 1851, in AUTO
return Path.from_text(spec).glomit(target, scope)
File "/home/mahmoud/projects/glom/tmpvenv/lib/python3.10/site-packages/glom/core.py", line 373, in glomit
return _t_eval(target, self.path_t, scope)
File "/home/mahmoud/projects/glom/tmpvenv/lib/python3.10/site-packages/glom/core.py", line 1206, in _t_eval
raise PathAccessError(e, Path(_t), i // 2)
glom.core.PathAccessError: could not access 'rings', part 0 of Path('rings'), got error: KeyError('rings')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment