DictionaryTreeBrowser#
- class pyxem.utils.calibration.DictionaryTreeBrowser(dictionary=None, double_lines=False, lazy=True)#
Bases:
object
A class to comfortably browse a dictionary using a CLI.
In addition to accessing the values using dictionary syntax the class enables navigating a dictionary that constains nested dictionaries as attribures of nested classes. Also it is an iterator over the (key, value) items. The
__repr__
method provides pretty tree printing. Private keys, i.e. keys that starts with an underscore, are not printed, counted when calling len nor iterated.Examples
>>> tree = DictionaryTreeBrowser() >>> tree.set_item("Branch.Leaf1.color", "green") >>> tree.set_item("Branch.Leaf2.color", "brown") >>> tree.set_item("Branch.Leaf2.caterpillar", True) >>> tree.set_item("Branch.Leaf1.caterpillar", False) >>> tree └── Branch ├── Leaf1 │ ├── caterpillar = False │ └── color = green └── Leaf2 ├── caterpillar = True └── color = brown >>> tree.Branch ├── Leaf1 │ ├── caterpillar = False │ └── color = green └── Leaf2 ├── caterpillar = True └── color = brown >>> for label, leaf in tree.Branch: ... print("%s is %s" % (label, leaf.color)) Leaf1 is green Leaf2 is brown >>> tree.Branch.Leaf2.caterpillar True >>> "Leaf1" in tree.Branch True >>> "Leaf3" in tree.Branch False >>>
Methods
DictionaryTreeBrowser.add_dictionary
(dictionary)Add new items from dictionary.
DictionaryTreeBrowser.add_node
(node_path)Adds all the nodes in the given path if they don't exist.
Returns its dictionary representation.
Returns a shallow copy using
copy.copy()
.Returns a deep copy using
copy.deepcopy()
.DictionaryTreeBrowser.export
(filename[, ...])Export the dictionary to a text file
DictionaryTreeBrowser.get_item
(item_path[, ...])Given a path, return it's value if it exists, or default value if missing.
DictionaryTreeBrowser.has_item
(item_path[, ...])Given a path, return True if it exists.
Returns a list of non-private keys.
Run the DictionaryTreeBrowser machinery for the lazy attributes.
DictionaryTreeBrowser.set_item
(item_path, value)iven the path and value, create the missing nodes in the path and assign the given value.