Skip to main content

A generic tree implementation in Python.

Project description

# 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.
```


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

stupidtree-0.1.0.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

stupidtree-0.1.0-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file stupidtree-0.1.0.tar.gz.

File metadata

  • Download URL: stupidtree-0.1.0.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for stupidtree-0.1.0.tar.gz
Algorithm Hash digest
SHA256 044291aae3e006b5a0cdfc8d42e9cfb4ac06087ed2ad9443b432176e4d509c31
MD5 61f8c7539b86ed08a6111beb52f14ccc
BLAKE2b-256 cca7544bfa026cd49c8bbd0768cab09eedd22b6ff4bb748de3f543aff065ebd1

See more details on using hashes here.

File details

Details for the file stupidtree-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for stupidtree-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 722dc08945e1aff262c75eb89f909261bdb3c06e4c1a87d4d5e7e23c534a4b61
MD5 201d1677affb3bb927d22657e300ead7
BLAKE2b-256 8bbf474d0326ba75fa8186a45cbd27e30320740a13a66f26d3b85f4cd8e83bc7

See more details on using hashes here.

Supported by

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