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

Support for edn_format.loads("#tag {:a :b}") #58

Closed sushovannits closed 4 years ago

sushovannits commented 4 years ago

Support for edn_format.loads("#tag {:a :b}"). Thanks

bfontaine commented 4 years ago

This is already implemented, althought it lacks documentation:

import edn_format

# create a custom element
class DogElement(edn_format.TaggedElement):
    def __init__(self, attrs):
        self.name = attrs.get(edn_format.Keyword("name"), "Rex")

    def __str__(self):
        return "<Dog name=%s>" % self.name

# add a tag for it
edn_format.add_tag("dog", DogElement)

# edn_format.loads uses it
print(edn_format.loads("#dog {:name \"Max\"}"))
# => <Dog name=Max>
import edn_format

# this works with a function too
def parse_dog(attrs):
    return "I'm a dog and my name is %s" % attrs.get(edn_format.Keyword("name"), "Rex")

# add a tag for it
edn_format.add_tag("dog", parse_dog)

# edn_format.loads uses it
print(edn_format.loads("#dog {:name \"Max\"}"))
# => I'm a dog and my name is Max