|
| 1 | +NodeTrie |
| 2 | +========== |
| 3 | + |
| 4 | +A Trie data structure library. |
| 5 | + |
| 6 | +.. image:: https://img.shields.io/pypi/v/nodetrie.svg |
| 7 | + :target: https://pypi.python.org/pypi/nodetrie |
| 8 | + :alt: Latest Version |
| 9 | +.. image:: https://travis-ci.org/NodeTrie/NodeTrie_Py.svg?branch=master |
| 10 | + :target: https://travis-ci.org/NodeTrie/NodeTrie_Py |
| 11 | + :alt: CI status |
| 12 | + |
| 13 | +Installation |
| 14 | +============= |
| 15 | + |
| 16 | +:: |
| 17 | + |
| 18 | + pip install nodetrie |
| 19 | + |
| 20 | +Example Usage |
| 21 | +============== |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + from nodetrie import Node |
| 26 | +
|
| 27 | + # This is the head of the trie, keep a reference to it |
| 28 | + node = Node() |
| 29 | + # Insert a linked tree so that a->b->c->d where -> means 'has child node' |
| 30 | + node.insert_split_path(['a', 'b', 'c', 'd']) |
| 31 | + node.children[0].name == 'a' |
| 32 | + a_node = node.children[0] |
| 33 | + a_node.name == 'a' |
| 34 | + a_node.children[0].name == 'b' |
| 35 | +
|
| 36 | + # Insertions create only new nodes |
| 37 | + # Insert linked tree so that a->b->c->dd |
| 38 | + node.insert_split_path(['a', 'b', 'c', 'dd']) |
| 39 | +
|
| 40 | + # Only one 'a' node |
| 41 | + len(node.children) == 1 |
| 42 | +
|
| 43 | + # 'c' node has two children, 'd' and 'dd' |
| 44 | + c_node = node.children[0].children[0].children[0] |
| 45 | + len(c_node.children) == 2 |
| 46 | +
|
| 47 | +Searching |
| 48 | +---------- |
| 49 | + |
| 50 | +NodeTrie supports exact name as well as file mask matching tree search. |
| 51 | + |
| 52 | +.. code-block:: python |
| 53 | +
|
| 54 | + from __future__ import print_function |
| 55 | + from nodetrie import Node |
| 56 | +
|
| 57 | + node = Node() |
| 58 | + for paths in [['a', 'b', 'c1', 'd1'], ['a', 'b', 'c1', 'd2'], |
| 59 | + ['a', 'b', 'c2', 'd1'], ['a', 'b', 'c2', 'd2']]: |
| 60 | + node.insert_split_path(paths) |
| 61 | + for path, _node in node.search(node, ['a', 'b', '*', '*'], []): |
| 62 | + print(path, _node.name) |
| 63 | +
|
| 64 | +Output |
| 65 | + |
| 66 | +.. code-block:: python |
| 67 | +
|
| 68 | + [u'a', u'b', u'c1', u'd1'] d1 |
| 69 | + [u'a', u'b', u'c1', u'd2'] d2 |
| 70 | + [u'a', u'b', u'c2', u'd1'] d1 |
| 71 | + [u'a', u'b', u'c2', u'd2'] d2 |
| 72 | +
|
| 73 | +A separator joined path list is return by the query function. |
| 74 | + |
| 75 | +.. code:: python |
| 76 | +
|
| 77 | + for match in node.query('a.b.*.*'): |
| 78 | + print(match) |
| 79 | +
|
| 80 | + for match in node.query('a|b|*|*', separator='|'): |
| 81 | + print(match) |
| 82 | +
|
| 83 | +Output |
| 84 | + |
| 85 | +.. code:: python |
| 86 | +
|
| 87 | + (u'a.b.c1.d1', <nodetrie.nodetrie.Node at 0x7f1899fa7730>), |
| 88 | + (u'a.b.c1.d2', <nodetrie.nodetrie.Node at 0x7f1899fa7130>), |
| 89 | + (u'a.b.c2.d1', <nodetrie.nodetrie.Node at 0x7f1899fa7110>), |
| 90 | + (u'a.b.c2.d2', <nodetrie.nodetrie.Node at 0x7f1899fa73f0>) |
| 91 | +
|
| 92 | + (u'a|b|c1|d1', <nodetrie.nodetrie.Node object at 0x7f436d09c750>) |
| 93 | + (u'a|b|c1|d2', <nodetrie.nodetrie.Node object at 0x7f436d09c770>) |
| 94 | + (u'a|b|c2|d1', <nodetrie.nodetrie.Node object at 0x7f436d09c790>) |
| 95 | + (u'a|b|c2|d2', <nodetrie.nodetrie.Node object at 0x7f436d09c7b0>) |
0 commit comments