Skip to content

About

A simple library to support working with recursively nested objects.

Primarily built around the nested class which offers in, len, index() and count() functionality.
Additionally, the flatten() function provides the ability to configure behaviour for strings etc.

A note on strings in nested objects

When iterating through a nested object, string-like elements (str, bytes) will be returned in their original blocks, not as individual characters.
When calculating len, index, count and __contains__; individual characters are considered.

A future version will aim to provide a similar interface to the preserve keyword argument in flatten.

Quick reference - nested

Full details under Reference - class nested

classDiagram
    direction RL
    nested <|.. Collection: implements
    class nested{
        contents
        __contains__()
        __iter__()
        __len__()
        count()
        index()
    }
    class Collection["collections.abc.Collection"]

A Collection which supports recursive versions of in, len and offers a recursive count and index.

ATTRIBUTE DESCRIPTION
contents

the original nested content

TYPE: Any

Examples:

>>> numberlists = [[1, 2], [3, 4], [5, 6], [[7, 8], 9]]
>>> nest = nested(numberlists)

>>> nest.contents
[[1, 2], [3, 4], [5, 6], [[7, 8], 9]]

>>> 5 in nest
True

>>> 10 in nest
False

>>> len(nest)
9

>>> [x for x in nest]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> list(nest)
[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> nest.count(5)
1
Source code in recurtools/nested.py
44
45
46
def __init__(self, contents: Container) -> None:
    """Initialise a nested container."""
    self.contents = contents