Skip to main content

Helper functions to make fitting outputs for doctests.

Project description

treenodedefinition

treenodedefinition is 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).

Installation

Installing the latest release using pip is recommended.

    $ pip install treenodedefinition

The latest development state can be obtained from gitlab using pip.

    $ pip install git+https://gitlab.com/david.scheliga/treenodedefinition.git@dev

Basic Usage

Use treenodedefinition.this_item_is_a_leaf for this module's default definition of tree leafs. For more details read function definition of treenodedefinition .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"})
False

Or use treenodedefinition.DetectsATreeLeaf = Callable[[Any], bool] to declare the type of a custom detection within function arguments or a classes attributes.

Module treenodedefinition

treenodedefinition.predict_tensor_item_count(potential_tensor: Sequence) -> int

Predicts the assumed tensor size on basis of the first element within the potentially nested sequence potential_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]]
...     ]
... )
8

In 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])
6

treenodedefinition.count_tensor_items(potential_tensor: Sequence) -> int

Counts the actual items within the potentially nested sequence potential_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])
4

treenodedefinition.is_proper_sized_tensor(potential_tensor: Sequence) -> bool

Estimates whether the given potential tensor has a proper shape of all items, or is inadequately filled.

Raises TypeError, if given potential_tensor doesn't implement len, which means that potential_tensor is 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."])
True

treenodedefinition.this_sequence_is_a_leaf(potential_tensor: Sequence) -> bool

States if the given sequence potential_tensor is a leaf or node.

All sequences, which are proper 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"}])
False

treenodedefinition.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) -> bool

Differentiates a tree_node_item being a tree leaf or not.

Examples

An 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())
True

A nested sequence with different containers is a node, with leafs.

>>> this_item_is_a_leaf([["a", "good", "start"], {"ended": "wrong"}])
False

A dictionary is always a node, not a leaf.

>>> this_item_is_a_leaf({"ended": "wrong"})
False

Contribution

Any 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 Code

Code style

This project follows the recommendations of PEP8. The project is using black as the code formatter.

Workflow

  1. Fork the project on Gitlab.
  2. Commit changes to your own branch.
  3. Submit a pull request from your fork's branch to our branch 'dev'.

Authors

License

This project is licensed under the GNU GENERAL PUBLIC LICENSE - see the LICENSE file for details

Acknowledge

Code style: black

Changelog

This changelog is inspired by Keep a Changelog.

Release 0.1 [2022-01-10]

Added

  • coverage, makefile, setup.cfg

Changed

  • Using setup.cfg instead of setup.py

Release 0.0b2.post2 [2020-08-21]

Fixed

  • Missing module declaration within setup.pyChanged

Release 0.0b2.post1 [2020-08-20]

Changed

  • Changed headings and removed redundant 'example' statement within readme for better readability.

Release 0.0b2 [2020-08-20]

Changed

  • Typos within readme and module.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

treenodedefinition-0.1.tar.gz (18.1 kB view details)

Uploaded Source

Built Distribution

treenodedefinition-0.1-py2.py3-none-any.whl (18.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file treenodedefinition-0.1.tar.gz.

File metadata

  • Download URL: treenodedefinition-0.1.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for treenodedefinition-0.1.tar.gz
Algorithm Hash digest
SHA256 617a2bb01c07657b3198edad158a7df7aa60c3e6dfc756cb85d4a9cb44d58869
MD5 16635dae583a28cf3e4e11604d7faea9
BLAKE2b-256 51ec2c90db5d4d031bc693ec1707832225616f30638268fcc7bf6fe7f6bbf199

See more details on using hashes here.

File details

Details for the file treenodedefinition-0.1-py2.py3-none-any.whl.

File metadata

  • Download URL: treenodedefinition-0.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for treenodedefinition-0.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 217ff31e139819973e68bc5388eee021ad5877579d6e9bfcdf1cf2d307781292
MD5 72a48cd7a0eac3d14bd2255a17af94df
BLAKE2b-256 ba07e62d5017a4c63ab76ffcd8d84956f1bc71e44ff05b5b1ccf1509e9f956aa

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page