Other Tools¶
Below is a collection of miscellaneous tools that are available on any Nob
or NobView
object.
Paths and Leaves¶
Any Nob
(or NobView
) object can introspect itself to find all its valid paths:
n.paths >>> [Path(/),
Path(/city),
Path(/kennel),
Path(/kennel/name),
Path(/kennel/employees),
Path(/kennel/dogs),
Path(/kennel/dogs/0),
Path(/kennel/dogs/0/name),
Path(/kennel/dogs/0/age),
Path(/kennel/dogs/0/alpha),
Path(/kennel/dogs/1),
Path(/kennel/dogs/1/name),
Path(/kennel/dogs/1/age),
Path(/kennel/dogs/1/alpha)]
Alternatively, you can ask only for the leaves of the tree, i.e. the paths to values which are neither dicts nor lists:
n.leaves >>> [Path(/kennel/name),
Path(/kennel/employees),
Path(/kennel/dogs/0/name),
Path(/kennel/dogs/0/age),
Path(/kennel/dogs/0/alpha),
Path(/kennel/dogs/1/name),
Path(/kennel/dogs/1/age),
Path(/kennel/dogs/1/alpha)]
Tip
These are lists of nob.Path
objects. For more on them, see the
dedicated page: Path.
Find¶
In order to easily search in this path list, the .find
method is available:
n.find('name') >>> [Path(/kennel/name),
Path(/kennel/dogs/0/name),
Path(/kennel/dogs/1/name)]
The elements of these lists are not strings, but Path
objects, as
described in Path.
Iteration¶
Any tree or tree view is also iterable, yielding its children:
[nv for nv in n.kennel] >>> [NobView:/kennel/name,
NobView:/kennel/employees,
NobView:/kennel/dogs]
Warning
This feature can be counter-intuitive: iterating on a dict yields
its keys, but on a list it yields its values. We choose here to
behave uniformly like lists and always yield the values. To get
the keys, use the .keys
method (see below).
Copy¶
To make an independant copy of a tree, use its .copy()
method:
n_cop = n.copy()
n == n_cop >>> True
n_cop.city = 'London'
n == n_cop >>> False
A new standalone tree can also be produced from any tree view:
n_cop = n.city.copy()
n_cop == n.city >>> True
n_cop.city = 'London'
n_cop == n.city >>> False
Keys¶
You can access keys at any level, just like in a dict:
n.kennel.keys() >>> dict_keys(['name', 'employees', 'dogs'])
n.dogs.keys() >>> dict_keys([0, 1])
Comparisons¶
A numpy.allclose
clone is available for convenience, and can be applied to a pair
of Nob
objects:
allclose(a, b) >>> True or False
If you wish to dig deeper into the differences between two Nob
objects, use diff
:
diff(a, b) >>> ([Path('/only_in_a')], Path('/different_value'), Path('/only_in_b'))