swaroopch / edn_format

EDN reader and writer implementation in Python, using PLY (lex, yacc)
https://swaroopch.com/2012/12/24/edn-format-python/
Other
131 stars 31 forks source link

Fix sort_sets when values are keywords or complex structures #86

Closed vitorsrg closed 2 years ago

vitorsrg commented 2 years ago

Context

Currently, dumping with sort_sets=True will fail when values are keywords or complex structures:

import edn_format

obj = edn_format.loads(
    r"""#{:a :b :c #{:a :b}}""")

print(
    edn_format.dumps(
        obj,
        sort_sets=True))

Solution

  1. Add magic comparison methods to Keyword. I added both __cmp__ and the other ones so it is compatible with python 2 and 3.
  2. Sort the set's values when serializing by the serialized value (like the dict does)
vitorsrg commented 2 years ago

Hi @swaroopch

Do you have any suggestions or should I just close the PR?