Closed sushovannits closed 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
Support for edn_format.loads("#tag {:a :b}"). Thanks