Package: kim-edn
KIM-EDN encoder and decoderednExtensible data notation [eed-n]ednis an extensible data
notation. A superset ofednis used by Clojure to represent programs, and
it is used byKIMand other applications as a
data format.kim-ednTheKIMinfrastructure embraces a subset ofednas astandard data format. The
primary purpose of this data format choice is to serve as a notational
superset toJSONwith the
enhancements being that it (1) allows for comments and (2) treats commas as
whitespace enabling easier templating.The subset ofednallowed is constrained to:BooleansStringsIntegersFloating point numbersVectors(or arrays)Maps(or hash, dicts, hashmaps, etc.)Exceptions:nilis not allowed, this includes
JSON's null which is not allowed. Instead consider:using an empty string ("") as the value,using the number 0 as the value,or omitting a key-value pair.Symbolsare not allowedKeywordsare not allowedListsare not allowed, please
usevectorsinsteadSetsare not allowedTagged elementsare
not allowedkim-ednhas been adapted and updated from the Pythonjsonmodule. It
exposes an API familiar to users of the standard library.
(Seepickle,
ormarshal, orjsonmodules.)Encoding basic Python object hierarchies::>>>importkim_edn>>>kim_edn.dumps(["short-name",{"source-value":["hcp"]}])'["short-name" {"source-value" ["hcp"]}]'>>>print(kim_edn.dumps("\"P6_3/mmc"))"\"P6_3/mmc">>>print(kim_edn.dumps('\\'))"\\">>>print(kim_edn.dumps({"domain":"openkim.org","data-method":"computation","author":"John Doe"},sort_keys=True)){"author""John Doe""data-method""computation""domain""openkim.org"}>>>fromioimportStringIO>>>io=StringIO()>>>kim_edn.dump(['streaming API'],io)>>>io.getvalue()'["streaming API"]'Pretty printing::>>>importkim_edn>>>print(kim_edn.dumps({"domain":"openkim.org","data-method":"computation","author":"John Doe"},sort_keys=True,indent=4)){"author""John Doe""data-method""computation""domain""openkim.org"}Decoding KIM-EDN::>>>importkim_edn>>>obj=["a",{"source-value":6.9790981921,"source-unit":"angstrom"}]>>>kim_edn.loads('["a", {"source-value": 6.9790981921, "source-unit": "angstrom"}]')==objTrue>>>kim_edn.load('["a", {"source-value": 6.9790981921, "source-unit": "angstrom"}]')==objTrue>>>kim_edn.loads('"\\"foo\\bar"')=='"foo\x08ar'True>>>kim_edn.load(kim_edn.dumps(obj))==objTrue>>>fromioimportStringIO>>>io=StringIO('["openkim.org"]')>>>kim_edn.load(io)[0]=='openkim.org'TrueDecoding Commented KIM-EDN::>>>obj={"property-id":"tag:brunnels@noreply.openkim.org,2016-05-11:property/atomic-mass"}>>>c_str='{\n; property-id\n"property-id"           "tag:brunnels@noreply.openkim.org,2016-05-11:property/atomic-mass" ; property id containing the unique ID of the property.\n}'>>>kim_edn.load(c_str)==objTrueSpecializing KIM-EDN object decoding::>>>importkim_edn>>>defas_complex(dct):...if'__complex__'indct:...returncomplex(dct['real'],dct['imag'])...returndct...>>>kim_edn.loads('{"__complex__": true, "real": 1, "imag": 2}',...object_hook=as_complex)(1+2j)>>>fromdecimalimportDecimal>>>kim_edn.loads('1.1',parse_float=Decimal)==Decimal('1.1')TrueSpecializing KIM-EDN object encoding::>>>importkim_edn>>>defencode_complex(obj):...ifisinstance(obj,complex):...return[obj.real,obj.imag]...msg='Object of type{}is not '.format(obj.__class__.__name__)...msg+='KIM-EDN serializable'...raiseTypeError(msg)...>>>kim_edn.dumps(2+1j,default=encode_complex)'[2.0 1.0]'>>>kim_edn.KIMEDNEncoder(default=encode_complex).encode(2+1j)'[2.0 1.0]'>>>''.join(kim_edn.KIMEDNEncoder(default=encode_complex).iterencode(2+1j))'[2.0 1.0]'Usingkim_edn.toolfrom the shell to validate and pretty-print::$echo'{"kim_edn" "obj"}'|python-mkim_edn.tool{"kim_edn""obj"}$echo'{"property-id" "tag:staff@noreply.openkim.org,2014-04-15:property/cohesive-energy-relation-cubic-crystal"}'|python-mkim_edn.tool{"property-id""tag:staff@noreply.openkim.org,2014-04-15:property/cohesive-energy-relation-cubic-crystal"}$echo'{"foo": ["bar", "baz"]}'|python-mkim_edn.tool{"foo"["bar""baz"]}$echo'{"foo" ["bar" "baz"]}'|python-mkim_edn.tool{"foo"["bar""baz"]}$echo'{"property-id" "tag:staff@noreply.openkim.org,2014-04-15:property/cohesive-potential-energy-hexagonal-crystal" "instance-id" 1 "space-group" {"source-value" "P6_3/mmc"} "basis-atom-coordinates" {"source-value" [[0, 0, 0][0.5, 0, 0.5]]}}'|python-mkim_edn.tool{"property-id""tag:staff@noreply.openkim.org,2014-04-15:property/cohesive-potential-energy-hexagonal-crystal""instance-id"1"space-group"{"source-value""P6_3/mmc"}"basis-atom-coordinates"{"source-value"[[000][0.500.5]]}}Note:This module's encoders and decoders preserve input and output order by
default. Order is only lost if the underlying containers are unordered.Encoders and DecodersKIM-EDN decoder (KIMEDNDecoder) object, performs the following translations
in decoding by default:KIM-EDNPythonobjectdictVectors (or "arrays")listStringsstrIntegers numbers (int)intFloating point numbers (real)floattrueTruefalseFalseKIM-EDN encoder (KIMEDNEncoder) for OpenKIM Python data structures, supports
the following objects and types by default:PythonKIM-EDNdictMaps (or "hash", "dicts", "hashmaps", etc.)listVectors (or "arrays")strStringsintIntegers numbersfloatFloating point numbersTruetrueFalsefalseInstalling kim-ednRequirementsYou need Python 3.6 or later to runkim-edn. You can have multiple Python
versions (2.x and 3.x) installed on the same system without problems.To install Python 3 for different Linux flavors, macOS and Windows, packages
are available athttps://www.python.org/getit/Using pippipis the most popular tool for installing Python packages, and the one
included with modern versions of Python.kim-edncan be installed withpip:pipinstallkim-ednNote:Depending on your Python installation, you may need to usepip3instead ofpip.pip3installkim-ednDepending on your configuration, you may have to runpiplike this:python3-mpipinstallkim-ednUsing pip (GIT Support)pipcurrently supports cloning overgitpipinstallgit+https://github.com/openkim/kim-edn.gitFor more information and examples, see thepip installreference.Using condacondais the package management tool for Anaconda Python installations.Installingkim-ednfrom theconda-forgechannel can be achieved by addingconda-forgeto your channels with:condaconfig--addchannelsconda-forgeOnce theconda-forgechannel has been enabled,kim-edncan be installed
with:condainstallkim-ednIt is possible to list all of the versions ofkim-ednavailable on your
platform with:condasearchkim-edn--channelconda-forgeReferencesThis module has been adapted and updated from thepythonjsonmodule to comply with thesubset ofednformat used inKIM.CopyrightCopyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 Python Software
Foundation;All Rights ReservedCopyright (c) 2019-2021, Regents of the University of Minnesota.All Rights ReservedContributingContributors:Yaser Afshar