Search code examples
pythonclojurescriptedn

Generating Clojure EDN from Python with :symbols


I'm trying to use EDN to communicate between a simple Python server and an in browser app written using ClojureScript.

So the server needs to generate and return chunks of EDN format for the browser.

I've installed https://github.com/swaroopch/edn_format which seems to be a recommended EDN library for Python.

But I want to be able to generate a map that uses Clojure symbols for keys. Eg. {:a 1 :b 2}

However, if I create a Python dictionary {"a":1, "b":2} and pass it to the dumps function, the final EDN keeps the keys as strings, not symbols.

Obviously there are no :symbols in Python. But is there a way to convince edn_format.dumps to turn dictionary string-keys into them? Or is there a better edn library for Python that can do this?


Solution

  • You can use the keyword_keys argument:

    edn_format.dumps({'a': 1}, keyword_keys=True)
    # => {:a 1}
    

    Or the Keyword constructor:

    edn_format.dumps({edn_format.Keyword('a'): 1})
    # => {:a 1}