-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Load edn as a Python dict with string keys #76
Comments
I’ll let @swaroopch weight in but personnally I’d be interested in some option to |
Indeed yes that sounds reasonable. |
Agree with @bfontaine ! Thanks @piotr-yuxuan :-) |
I see two issues here:
These two points are very opinionated, and perhaps you will disagree with me. I would prefer to know the opinion of the maintainers about them before refactoring everything and making breaking changes ;-) If we were to move forward, I would see four possible different ways to parse an edn keyword, and two possible ways to parse a symbol:
Methinks a naive implementation for Keyword which store the parsing strategy as a lexer state would be: @ply.lex.TOKEN(KEYWORD)
def t_KEYWORD(t):
if t.lexer.keyword_parsing == KeywordParsing.keyword_object:
t.value = Keyword(t.value[1:])
elif t.lexer.keyword_parsing == KeywordParsing.name_string:
t.value = Keyword(t.value[1:]).name
elif t.lexer.keyword_parsing == KeywordParsing.ns_slash_name_string:
t.value = t.value[1:]
elif t.lexer.keyword_parsing == KeywordParsing.leading_colon_string:
pass
return t …which leads to the remarks above. |
For the impatient, here how I would iterate through what is returned by keyword_name = lambda keyword: \
keyword.name.split("/", 1)[1] if "/" in keyword.name else keyword.name
strinfigy_keyword = lambda x: \
keyword_name(x) if isinstance(x, edn_format.Keyword) else x
def process_edn(x):
if isinstance(x, edn_format.ImmutableDict):
return {strinfigy_keyword(k): process_edn(v) for k, v in x.items()}
elif isinstance(x, edn_format.ImmutableList):
return [process_edn(v) for v in x]
else:
return strinfigy_keyword(x) Obviously keyword_name can be changed for any other callable. |
Hello, sorry for the late response. That’s interesting; I was thinking of post-processing the result of the parsing like you did in your second comment; not of doing that during the lexing/parsing.
Yes, it has bit me before. I don’t know the reason behind this. Changing this would be a breaking change, though.
I don’t understand how you would do that in the lexer; its job is to tokenize the input, not to make sense of those tokens. The lexer doesn’t know what a map looks like: in |
Just thought it was worth mentioning that you can create a tag with @piotr-yuxuan solution. Which you can add at the top of your edn file if you don't want to actively call edn.add_tag('python', process_edn) and then you can: #python
{:tags
[{:name "foo"},
{:name "bar"} ]} |
Starting from @piotr-yuxuan 's suggestion I used
it's incomplete but it seems ok so far in my case (parsing simple edns to python dicts without keywords), so posting it here in case that's helpful for someone else. |
Would you consider a PR to load edn form just as standard Python dict with string keys?
The text was updated successfully, but these errors were encountered: