Alternatives#
easytree
shares some functionality with other existing libraries, but differs from each in some respect.
collections.defaultdict
#
The recursive nature of the easytree.dict
can be replicated with the native collections.defaultdict
.
>>> import collections
>>> recursivedict = lambda: collections.defaultdict(lambda: recursivedict())
>>> data = recursivedict()
>>> data["foo"]["bar"]["baz"] = "Hello world!"
>>> data
{
"foo": {
"bar": {
"baz": "Hello world!"
}
}
}
However
easytree.dict
allows for the dot notationnew nodes in an
easytree.dict
can become a neweasytree.dict
oreasytree.list
.
>>> import easytree
>>> data = easytree.dict()
>>> data.foo.bar.baz.append("Hello world!")
>>> data
{
"foo": {
"bar": {
"baz": ["Hello world!"]
}
}
}
dictdot
#
dictdot
is an alternative library (see here) which allows for the use of the dot notation.
However
easytree
allows you to recursively write new nested nodes
Compare:
>>> import easytree
>>> data = easytree.dict()
>>> data.foo.bar.baz = "Hello world!"
>>> data
{
"foo": {
"bar": {
"baz": "Hello world!"
}
}
}
with
>>> from dictdot import dictdot
>>> data = dictdot()
>>> data.foo.bar.baz
AttributeError: 'NoneType' object has no attribute 'bar'
jsontree
#
Another competing alternative is jsontree
(see here)
However
dictionaries and lists, when attached or appended to an
easytree.list
, are recursively cast aseasytree.dict
andeasytree.list
.
Compare:
>>> import easytree
>>> tree = easytree.dict()
>>> tree.friends = [{"name":"David"},{"name":"Celine"}]
>>> tree.friends[0].age = 29 #this works
>>> tree
{
'friends': [
{'age': 29, 'name': 'David'},
{'name': 'Celine'}
]
}
with:
>>> import jsontree
>>> tree = jsontree.jsontree()
>>> tree.friends = [{"name":"David"},{"name":"Celine"}]
>>> tree.friends[0].age = 29 #this does not work
AttributeError: 'dict' object has no attribute 'age'
easytree.dict
andeasytree.list
objects inherit from the builtindict
andlist
objects, allowing for seamless integration into existing codebases
>>> data = easytree.dict({"foo":"bar"})
>>> isinstance(data, dict)
True
>>> items = easytree.list([1,2,3])
>>> isinstance(items, list)
True
easytree.dict
andeasytree.list
support freezing and sealing.