File: api.rst

package info (click to toggle)
dm-tree 0.1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 368 kB
  • sloc: python: 1,472; cpp: 614; makefile: 17; sh: 5
file content (62 lines) | stat: -rw-r--r-- 1,265 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#############
API Reference
#############

All ``tree`` functions operate on nested tree-like structures. A *structure*
is recursively defined as::

  Structure = Union[
      Any,
      Sequence['Structure'],
      Mapping[Any, 'Structure'],
      'AnyNamedTuple',
  ]

.. TODO(slebedev): Support @dataclass classes if we make @attr.s
.. support public.

A single (non-nested) Python object is a perfectly valid structure::

  >>> tree.map_structure(lambda v: v * 2, 42)
  84
  >>> tree.flatten(42)
  [42]

You could check whether a structure is actually nested via
:func:`~tree.is_nested`::

  >>> tree.is_nested(42)
  False
  >>> tree.is_nested([42])
  True

Note that ``tree`` only supports acyclic structures. The behavior for
structures with cycle references is undefined.

.. currentmodule:: tree

.. autofunction:: is_nested

.. autofunction:: assert_same_structure

.. autofunction:: unflatten_as

.. autofunction:: flatten

.. autofunction:: flatten_up_to

.. autofunction:: flatten_with_path

.. autofunction:: flatten_with_path_up_to

.. autofunction:: map_structure

.. autofunction:: map_structure_up_to

.. autofunction:: map_structure_with_path

.. autofunction:: map_structure_with_path_up_to

.. autofunction:: traverse

.. autodata:: MAP_TO_NONE