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

  1. easytree.dict allows for the dot notation

  2. new nodes in an easytree.dict can become a new easytree.dict or easytree.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

  1. 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

  1. dictionaries and lists, when attached or appended to an easytree.list, are recursively cast as easytree.dict and easytree.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'
  1. easytree.dict and easytree.list objects inherit from the builtin dict and list 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
  1. easytree.dict and easytree.list support freezing and sealing.