kim-edn
kim-edn is a Python library providing an encoder and decoder for the EDN (Extensible Data Notation) format. It aims to offer a robust and efficient way to serialize and deserialize EDN data structures in Python. The current version is 1.4.1, and releases typically focus on packaging improvements, dependency updates, and minor fixes.
Common errors
-
ModuleNotFoundError: No module named 'kim_edn'
cause The 'kim-edn' package is not installed in your current Python environment.fixRun `pip install kim-edn` to install the package. -
AttributeError: module 'kim_edn' has no attribute 'read_edn_string'
cause You are attempting to use an old API function (`read_edn_string`) that was removed or refactored in `kim-edn` v1.1.0.fixReplace `kim_edn.reader.read_edn_string` with `kim_edn.loads` and ensure you import `loads` directly: `from kim_edn import loads`. -
kim_edn.reader.BadEDNString: Invalid EDN string: ...
cause The EDN string you are trying to parse has a syntax error or is malformed according to the EDN specification.fixCarefully review the EDN string for correctness (e.g., mismatched brackets, incorrect keyword syntax, missing spaces, unescaped characters). EDN is case-sensitive and strict about its format.
Warnings
- breaking Starting from version 1.4.0, kim-edn officially requires Python 3.8 or higher. Installations or executions on older Python versions (e.g., Python 3.7) will fail.
- breaking The API underwent significant cleanup and interface changes in v1.1.0, including getting rid of unnecessary keywords and standardizing on `loads` and `dumps`. Older functions like `kim_edn.reader.read_edn_string` are no longer available.
- gotcha As of v1.4.0, the package build process was improved, and tests are no longer included in the final distributed package. If you relied on examining the bundled tests, they will now be absent.
Install
-
pip install kim-edn
Imports
- loads, dumps
import kim_edn.reader
from kim_edn import loads, dumps
Quickstart
from kim_edn import loads, dumps
# Example EDN string with a keyword and integer value
edn_string = '{:a 1}'
# Deserialize the EDN string to a Python dictionary
python_dict = loads(edn_string)
print(f"Deserialized: {python_dict} (type: {type(python_dict)})\n")
# Modify the dictionary (EDN keywords become Python keywords for keys)
python_dict[':b'] = "hello"
# Serialize the Python dictionary back to an EDN string
new_edn_string = dumps(python_dict)
print(f"Serialized: {new_edn_string}")
# Example with a list and map
complex_edn = '[1 2 {:c "test"}]'
complex_python = loads(complex_edn)
print(f"\nComplex Deserialized: {complex_python}")