Package: treenodedefinition
treenodedefinitiontreenodedefinitionis a helper module providing an interface for
definition of which parts of a nested list or dictionary are handled as a
node (containing further tree items) or a leaf (being a top item of the tree).InstallationInstalling the latest release using pip is recommended.$pipinstalltreenodedefinitionThe latest development state can be obtained from gitlab using pip.$pipinstallgit+https://gitlab.com/david.scheliga/treenodedefinition.git@devBasic UsageUsetreenodedefinition.this_item_is_a_leaffor this module's default definition of
tree leafs. For more details read function definition oftreenodedefinition .this_item_is_a_leaf.>>> from treenodedefinition import this_item_is_a_leaf
>>> this_item_is_a_leaf([[1, 2], [3, 4]])
True
>>> this_item_is_a_leaf([[1, 2], [3]])
False
>>> this_item_is_a_leaf("any single data type")
True
>>> this_item_is_a_leaf({"any": "dict or mapping"})
FalseOr usetreenodedefinition.DetectsATreeLeaf = Callable[[Any], bool]to declare the
type of a custom detection within function arguments or a classes attributes.Moduletreenodedefinitiontreenodedefinition.predict_tensor_item_count(potential_tensor: Sequence) -> intPredicts the assumed tensor size on basis of the first element within the potentially
nested sequencepotential_tensor. It is assumed that the given potential tensor has a
shape of (i, j, ...). The shape's product reflects the tensor's item count. It also
implies that all sub tensors within each level of the tensor have the same item count,
therefore the first item within each level defines the shape of this particular level.For 'clean' tensors the prediction is equal to the actual item count.>>> from treenodedefinition import predict_tensor_item_count
>>> predict_tensor_item_count([])
0
>>> predict_tensor_item_count([[], []])
0
>>> predict_tensor_item_count([1, 2])
2
>>> predict_tensor_item_count([[1, 1], [2, 2]])
4
>>> predict_tensor_item_count(
...     [
...         [[1, 1], [2, 2]],
...         [[3, 3], [4, 4]]
...     ]
... )
8In this case the tensor should had an item count of 6, while two
items are lacking in their tensor size.>>> predict_tensor_item_count([[1, 2], 3, 4])
6treenodedefinition.count_tensor_items(potential_tensor: Sequence) -> intCounts the actual items within the potentially nested sequencepotential_tensor.>>> from treenodedefinition import count_tensor_items
>>> count_tensor_items([1, 2])
2
>>> count_tensor_items([[1, 1], [2, 2]])
4
>>> count_tensor_items(
...     [
...         [[1, 1], [2, 2]],
...         [[3, 3], [4, 4]]
...     ]
... )
8
>>> count_tensor_items([[1, 2], 3, 4])
4treenodedefinition.is_proper_sized_tensor(potential_tensor: Sequence) -> boolEstimates whether the given potential tensor has a proper shape of all
items, or is inadequately filled.Raises TypeError, if givenpotential_tensordoesn't implement len, which means
thatpotential_tensoris not a sequence.>>> from treenodedefinition import is_proper_sized_tensor
>>> is_proper_sized_tensor([])
False
>>> is_proper_sized_tensor([1, 2])
True
>>> is_proper_sized_tensor([[1, 1], [2, 2]])
True
>>> is_proper_sized_tensor(
...     [
...         [[1, 1], [2, 2]],
...         [[3, 3], [4, 4]]
...     ]
... )
True
>>> is_proper_sized_tensor([[1, 1], 2, 3])
False
>>> is_proper_sized_tensor("A string is a sequence, but not a tensor,")
False
>>> is_proper_sized_tensor(["while", "a", "sequence", "of", "strings", "is."])
Truetreenodedefinition.this_sequence_is_a_leaf(potential_tensor: Sequence) -> boolStates if the given sequencepotential_tensoris a leaf or node.All sequences, which areproper sized tensor(is_proper_sized_tensor(..)) are considered as values.Sequences which contain mixed containers (Sequences and Mappings) are
considered as nodes. Also if the sequence contains different sized
sequences.>>> from treenodedefinition import this_sequence_is_a_leaf
>>> this_sequence_is_a_leaf([])
False
>>> this_sequence_is_a_leaf([1, 2])
True
>>> this_sequence_is_a_leaf([[1, 1], [2, 2]])
True
>>> this_sequence_is_a_leaf([[1, 1], 2, 3])
False
>>> this_sequence_is_a_leaf("A single string is a leaf, ")
True
>>> this_sequence_is_a_leaf(["while", "a", "sequence", "of", "strings", "is."])
True
>>> this_sequence_is_a_leaf([["a", "good", "start"], {"ended": "wrong"}])
Falsetreenodedefinition.DetectsATreeLeaf = Callable[[Any], bool]Declares a function, which task is to detect, whether the given single argument is a
tree leaf.treenodedefinition.this_item_is_a_leaf(tree_node_item: Any) -> boolDifferentiates atree_node_itembeing a tree leaf or not.ExamplesAn empty sequence is treatend as a node, as a potential placeholder for
a future nested sequence.>>> from treenodedefinition import this_item_is_a_leaf
>>> this_item_is_a_leaf([])
False(Nested) sequences are treatend as leafs, as long the resemble a proper
filled tensor. The item type doesn't matter.>>> this_item_is_a_leaf([1, 2])
True
>>> this_item_is_a_leaf([[1, 1], [2, 2]])
True
>>> this_item_is_a_leaf([[1, 1], 2, 3])
False
>>> this_item_is_a_leaf("A string is.")
True
>>> this_item_is_a_leaf(["As", "is", "also", "a", "sequence", "of", "strings"])
True
>>> this_item_is_a_leaf(1)
True
>>> this_item_is_a_leaf(object())
TrueA nested sequence with different containers is a node, with leafs.>>> this_item_is_a_leaf([["a", "good", "start"], {"ended": "wrong"}])
FalseA dictionary is always a node, not a leaf.>>> this_item_is_a_leaf({"ended": "wrong"})
FalseContributionAny contribution by reporting a bug or desired changes are welcomed. The preferred
way is to create an issue on the gitlab's project page, to keep track of everything
regarding this project.Contribution of Source CodeCode styleThis project follows the recommendations ofPEP8.
The project is usingblackas the code formatter.WorkflowFork the project on Gitlab.Commit changes to your own branch.Submit apull requestfrom your fork's branch to our branch'dev'.AuthorsDavid Scheliga@gitlab@LinkedinInitial workMaintainerLicenseThis project is licensed under the GNU GENERAL PUBLIC LICENSE - see theLICENSEfile for detailsAcknowledgeCode style: blackChangelogThis changelog is inspired byKeep a Changelog.Release 0.1 [2022-01-10]Addedcoverage, makefile, setup.cfgChangedUsing setup.cfg instead of setup.pyRelease 0.0b2.post2 [2020-08-21]FixedMissing module declaration within setup.pyChangedRelease 0.0b2.post1 [2020-08-20]ChangedChanged headings and removed redundant 'example' statement within readme for
better readability.Release 0.0b2 [2020-08-20]ChangedTypos within readme and module.