Skip to content

dschenck/easytree

Repository files navigation

easytree

pythons build PyPI version Documentation Status Code style: black Coverage

A recursive dot-styled defaultdict to read and write deeply-nested trees

Documentation

Documentation is hosted on read the docs

Installation

pip install easytree

Quickstart

>>> import easytree

>>> tree = easytree.dict()
>>> tree.foo.bar.baz = "Hello world!"
>>> tree 
{
    "foo": {
        "bar": {
            "baz": "Hello world!"
        }
    }
}

Creating trees that combine both list and dict nodes is easy

>>> friends = easytree.list()
>>> friends.append({"firstname":"Alice"})
>>> friends[0].address.country = "Netherlands"
>>> friends[0]["interests"].append("science")
>>> friends
[
    {
        "firstname": "Alice",
        "address": {
            "country": "Netherlands"
        },
        "interests": [
            "science"
        ]
    }
]

Writing deeply-nested trees with list nodes is easy with a context-manager:

>>> profile = easytree.dict()
>>> with profile.friends.append({"firstname":"Flora"}) as friend: 
...     friend.birthday = "25/02"
...     friend.address.country = "France"
>>> profile
{
    "friends": [
        {
            "firstname": "Flora",
            "birthday": "25/02",
            "address": {
                "country": "France"
            }
        }
    ]
}

About

A recursive dot-styled defaultdict to read and write deeply-nested trees

Topics

Resources

License

Stars

Watchers

Forks

Languages