Last active
July 28, 2020 08:05
-
-
Save mahmoud/ce593a4736b9d410669a3dbf5492950e to your computer and use it in GitHub Desktop.
improved branched glom error ascii design
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
before = \ | |
''' | |
Traceback (most recent call last): | |
File "tmp.py", line 9, in _make_stack | |
glom(target, spec) | |
File "/home/mahmoud/projects/glom/glom/core.py", line 2024, in glom | |
raise err | |
glom.core.PathAccessError: error raised while processing, details below. | |
Target-spec trace (most recent last): | |
- Target: [None] | |
- Spec: Match(Switch([(1, 1), ('a', 'a'), ([None], Switch([(1, 1), ('a', 'a'), ([None], T.a)]))])) | |
+ Spec: Switch([(1, 1), ('a', 'a'), ([None], Switch([(1, 1), ('a', 'a'), ([None], T.a)]))]) | |
> Failed Branch: | |
|- Spec: 1 | |
|- glom.matching.MatchError: [None] does not match 1 | |
> Failed Branch: | |
|- Spec: 'a' | |
|- glom.matching.MatchError: [None] does not match 'a' | |
> Failed Branch: | |
|- Spec: [None] | |
|+ Spec: Switch([(1, 1), ('a', 'a'), ([None], T.a)]) | |
|> Failed Branch: | |
||- Spec: 1 | |
||- glom.matching.MatchError: [None] does not match 1 | |
|> Failed Branch: | |
||- Spec: 'a' | |
||- glom.matching.MatchError: [None] does not match 'a' | |
|> Failed Branch: | |
||- Spec: [None] | |
||- Spec: T.a | |
glom.core.PathAccessError: could not access 'a', part 0 of T.a, got error: AttributeError("'list' object has no attribute 'a'") | |
''' | |
after = \ | |
''' | |
Traceback (most recent call last): | |
File "tmp.py", line 9, in _make_stack | |
glom(target, spec) | |
File "/home/mahmoud/projects/glom/glom/core.py", line 2024, in glom | |
raise err | |
glom.core.PathAccessError: error raised while processing, details below. | |
Target-spec trace (most recent last): | |
- Target: [None] | |
- Spec: Match(Switch([(1, 1), ('a', 'a'), ([None], Switch([(1, 1), ('a', 'a'), ([None], T.a)]))])) | |
+ Spec: Switch([(1, 1), ('a', 'a'), ([None], Switch([(1, 1), ('a', 'a'), ([None], T.a)]))]) | |
|\ Spec: 1 | |
|X glom.matching.MatchError: [None] does not match 1 | |
|\ Spec: 'a' | |
|X glom.matching.MatchError: [None] does not match 'a' | |
|\ Spec: [None] | |
|+ Spec: Switch([(1, 1), ('a', 'a'), ([None], T.a)]) | |
||\ Spec: 1 | |
||X glom.matching.MatchError: [None] does not match 1 | |
||\ Spec: 'a' | |
||X glom.matching.MatchError: [None] does not match 'a' | |
||\ Spec: [None] | |
||| Spec: T.a | |
glom.core.PathAccessError: could not access 'a', part 0 of T.a, got error: AttributeError("'list' object has no attribute 'a'") | |
''' | |
steps = \ | |
''' | |
1. Eliminate "Failed Branch" | |
2. Instead, "X" on the exceptions | |
3. And \ on the first spec after exceptions and switches | |
''' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import traceback | |
from glom import Match, Switch, T, glom, GlomError, Coalesce | |
def _make_stack(spec, **kwargs): | |
target = kwargs.pop('target', [None]) | |
assert not kwargs | |
try: | |
glom(target, spec) | |
except GlomError as e: | |
stack = traceback.format_exc() | |
return stack | |
switch_spec = Match(Switch( | |
[(1, 1), ('a', 'a'), ([None], Switch( | |
[(1, 1), ('a', 'a'), ([None], T.a)]))])) | |
stack = _make_stack(switch_spec) | |
print(stack) | |
val = {'a': {'b': 'c'}, # basic dictionary nesting | |
'd': {'e': ['f'], # list in dictionary | |
'g': 'h'}, | |
'i': [{'j': 'k', 'l': 'm'}], # list of dictionaries | |
'n': 'o', | |
'xxx': {'z': {'v': 0}}} | |
actual = _make_stack(Coalesce(('xxx', 'z', 'n'), 'yyy'), target=val) | |
print(actual) | |
# or let, delete, assign? | |
# or let, assign (overwrite), delete, assign (if fail, reassign back) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Traceback (most recent call last): | |
File "tmp.py", line 9, in _make_stack | |
glom(target, spec) | |
File "/home/mahmoud/projects/glom/glom/core.py", line 2068, in glom | |
raise err | |
glom.core.PathAccessError: error raised while processing, details below. | |
Target-spec trace (most recent last): | |
- Target: [None] | |
- Spec: Match(Switch([(1, 1), ('a', 'a'), ([None], Switch([(1, 1), ('a', '... | |
+ Spec: Switch([(1, 1), ('a', 'a'), ([None], Switch([(1, 1), ('a', 'a'), (... | |
|\ Spec: 1 | |
|X glom.matching.MatchError: [None] does not match 1 | |
|\ Spec: 'a' | |
|X glom.matching.MatchError: [None] does not match 'a' | |
|\ Spec: [None] | |
|+ Spec: Switch([(1, 1), ('a', 'a'), ([None], T.a)]) | |
||\ Spec: 1 | |
||X glom.matching.MatchError: [None] does not match 1 | |
||\ Spec: 'a' | |
||X glom.matching.MatchError: [None] does not match 'a' | |
||\ Spec: [None] | |
||| Spec: T.a | |
glom.core.PathAccessError: could not access 'a', part 0 of T.a, got error: AttributeError("'list' object has no attribute 'a'") | |
Traceback (most recent call last): | |
File "tmp.py", line 9, in _make_stack | |
glom(target, spec) | |
File "/home/mahmoud/projects/glom/glom/core.py", line 2068, in glom | |
raise err | |
glom.core.CoalesceError: error raised while processing, details below. | |
Target-spec trace (most recent last): | |
- Target: {'a': {'b': 'c'}, 'd': {'e': ['f'], 'g': 'h'}, 'i': [{'j... (len=5) | |
+ Spec: Coalesce(('xxx', 'z', 'n'), 'yyy') | |
|\ Spec: ('xxx', 'z', 'n') | |
|| Spec: 'xxx' | |
|| Target: {'z': {'v': 0}} | |
|| Spec: 'z' | |
|| Target: {'v': 0} | |
|| Spec: 'n' | |
|X glom.core.PathAccessError: could not access 'n', part 0 of Path('n'), got error: KeyError('n') | |
|\ Spec: 'yyy' | |
|X glom.core.PathAccessError: could not access 'yyy', part 0 of Path('yyy'), got error: KeyError('yyy') | |
glom.core.CoalesceError: no valid values found. Tried (('xxx', 'z', 'n'), 'yyy') and got (PathAccessError, PathAccessError) (at path ['xxx', 'z']) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Actually did get it working shortly after saying I wouldn't. It's a toy/hack, but it works (as seen above).
(don't judge lol)