Skip to main content
# stupidtree
A generic tree implementation in Python.


## Installation

From pypi:
```bash
pip install stupidtree
```

or clone this repo:
```bash
git clone https://github.com/luozhouyang/stupidtree
```

## Architecture

The design of this lib is flexible and expandable.

The module `stupidtree.core` has already defines the `Node` class, which works for most situations.
Here is the `__init__` function's signature:
```python
def __init__(data, tag, parent):
pass
```

For your special needs, you can inherit the `Node` class and override some functions as your need.

This module also defines the `BaseTree` class, which is the base class of all trees. It has already implemented the
interface `BaseTreeInterface`. This interface(or abstract class) contains these useful methods:
```python
class BaseTreeInterface(abc.ABC):

def put(self, words):
raise NotImplementedError()

def remove(self, key, rm_filter):
raise NotImplementedError()

def get(self, key):
raise NotImplementedError()
```

**You can put nodes into the tree with any depth and any counts**.

For improving searching performance, a `Indexer` has been implemented. It is based on `dict`, using `hash` of keys to search nodes in O(1).

Module `stupidtree.core` has implemented a tree `IndexedTree` that supports index. In most situations, the index will be helpful. And if you
want to add another index, just inherit `NodeIndexInterface` and implements the abstract methods.

## Usage
The usage of `stupidtree` is quitely simple. You just need to put a string or a list into a constructed tree that derived from `BaseTree` or `IndexedTree`.
Here is a example of Chinese address in `stupidtree.examples` package.

For intuitive, I am showing you a few test examples:

```python
import unittest

from stupidtree.examples.address.level import Level
from stupidtree.examples.address.pcd_tree import PCDTree


class TestPCDTree(unittest.TestCase):

def test_put(self):
tree = PCDTree()
a0 = '浙江省 杭州市 西湖区'
tree.put(a0)
self.assertEqual(4, tree.size())
tree.print()
a1 = '浙江省 杭州市 江干区'
tree.put(a1)
self.assertEqual(5, tree.size())

nodes = tree.get('浙江省')
self.assertEqual(1, len(nodes))
for n in nodes:
self.assertEqual(Level.PROVINCE, n.tag)

def test_remove(self):
tree = PCDTree()
a0 = '浙江省 杭州市 西湖区'
tree.put(a0)
a1 = '浙江省 杭州市 江干区'
tree.put(a1)
tree.print()

tree.remove('江干区')
self.assertEqual(4, tree.size())
tree.print()

tree.remove('浙江省')
self.assertEqual(1, tree.size())
tree.print()

tree.remove('')
print()
tree.print()
self.assertEqual(0, tree.size())


if __name__ == "__main__":
unittest.main()
```

And here are the outputs:
```bash
+--
| +--浙江省
| | +--杭州市
| | | +--西湖区
.+--
| +--浙江省
| | +--杭州市
| | | +--西湖区
| | | +--江干区
+--
| +--浙江省
| | +--杭州市
| | | +--西湖区
| | | +--江干区
+--
| +--浙江省
| | +--杭州市
| | | +--西湖区
| | | +--江干区

+--
| +--浙江省
| | +--杭州市
| | | +--西湖区
| | | +--江干区
```

## License
```bash
MIT License

Copyright (c) 2018 luozhouyang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```


Metadata

Release files for stupidtree 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stupidtree 0.1.0
File Size Uploaded Provenance
stupidtree-0.1.0.tar.gz 7.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for stupidtree 0.1.0
File Interpreter ABI Platform
stupidtree-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 28.1 kB

Release files / stupidtree-0.1.0.tar.gz

Download URL stupidtree-0.1.0.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
044291aae3e006b5a0cdfc8d42e9cfb4ac06087ed2ad9443b432176e4d509c31
BLAKE2b-256 checksum
How to use checksums
cca7544bfa026cd49c8bbd0768cab09eedd22b6ff4bb748de3f543aff065ebd1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / stupidtree-0.1.0-py3-none-any.whl

Download URL stupidtree-0.1.0-py3-none-any.whl
Size 20.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
722dc08945e1aff262c75eb89f909261bdb3c06e4c1a87d4d5e7e23c534a4b61
BLAKE2b-256 checksum
How to use checksums
8bbf474d0326ba75fa8186a45cbd27e30320740a13a66f26d3b85f4cd8e83bc7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for stupidtree 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page