https://github.com/knutwalker/namespacetrie
A Trie implementation that manages not the single characters but treats its values as typical namespaces.
https://github.com/knutwalker/namespacetrie
Last synced: 8 months ago
JSON representation
A Trie implementation that manages not the single characters but treats its values as typical namespaces.
- Host: GitHub
- URL: https://github.com/knutwalker/namespacetrie
- Owner: knutwalker
- License: gpl-3.0
- Archived: true
- Created: 2012-05-06T20:58:40.000Z (about 14 years ago)
- Default Branch: master
- Last Pushed: 2014-01-02T06:18:04.000Z (over 12 years ago)
- Last Synced: 2025-01-28T23:38:54.459Z (over 1 year ago)
- Language: Python
- Homepage: http://blog.knutwalker.de/namespacetrie/
- Size: 141 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: COPYING
Awesome Lists containing this project
README
[](http://travis-ci.org/knutwalker/namespacetrie)
This is the Namespace Trie.
Namespace Trie is a implementation of a Trie data structure. Unlike typical
implementations, which are splitting its value into single characters,
Namespace Trie treats its values as namespaces. Namespaces are strings that
are delimited by a period. Such namespaces often occur in programming
languages, e.g. Java or Python and may also appear while using some libraries
for programming languages that itself do not offer namespacing (e.g. the
Google Closure library offers a namespace feature for JavaScript). The
Namespace Trie may help you find flaws in the namespace structure.
Namespace Trie is developed for use with Closure Depresolver and may at the
moment not be very useful as there is not standalone interface.
## Install
from pip:
pip install namespacetrie
from source:
pip install git+git://github.com/knutwalker/namespacetrie.git
## Usage
```python
from namespacetrie.nstrie import NsTrie
modules = ['com.example.foo', 'com.example.bar', 'com.example.baz.Foo',
'com.example.baz.Bar', 'org.example']
trie = NsTrie(modules)
'com.example' in trie
# True
trie.has('com.example')
# False
trie.has('com.example', False)
# True
trie.has('com.example.foo')
# True
node = trie.get('com.example')
node.keys()
# ['foo', 'bar', 'baz']
trie.to_dict()
# {'com': {'example': {'bar': 'com.example.bar',
# 'baz': {'Bar': 'com.example.baz.Bar', 'Foo': 'com.example.baz.Foo'},
# 'foo': 'com.example.foo'}},
# 'org': {'example': 'org.example'}}
list(trie.iterdepth())
# ['com', 'com.example', 'com.example.foo', 'com.example.bar',
# 'com.example.baz', 'com.example.baz.Foo', 'com.example.baz.Bar',
# 'org', 'org.example']
list(trie.iterbreadth())
# ['com', 'org', 'com.example', 'org.example', 'com.example.foo',
# 'com.example.bar', 'com.example.baz', 'com.example.baz.Foo',
# 'com.example.baz.Bar']
trie.remove('com')
list(trie)
# [('org', [('example', 'org.example')])]
```