https://github.com/nitely/kua
:zap: Lightning fast URL routing in Python (trie router)
https://github.com/nitely/kua
python router trie
Last synced: 7 months ago
JSON representation
:zap: Lightning fast URL routing in Python (trie router)
- Host: GitHub
- URL: https://github.com/nitely/kua
- Owner: nitely
- License: mit
- Created: 2016-08-28T17:03:53.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2022-07-16T05:11:01.000Z (about 3 years ago)
- Last Synced: 2024-10-12T07:14:17.021Z (12 months ago)
- Topics: python, router, trie
- Language: Python
- Homepage: http://kua.readthedocs.io
- Size: 30.3 KB
- Stars: 23
- Watchers: 3
- Forks: 9
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# kua
[](https://travis-ci.org/nitely/kua)
[](https://coveralls.io/r/nitely/kua)
[](https://pypi.python.org/pypi/kua)
[](https://raw.githubusercontent.com/nitely/kua/master/LICENSE)Lightning fast URL routing in Python.
kua is a [Trie](https://en.wikipedia.org/wiki/Trie)-like based router.
It scales better than regex based routers and due to this it's usually faster.It's pretty bare bones and it's meant to be used in more feature-rich routers.
## Compatibility
* Python 3.5
## Install
```
$ pip install kua
```## Usage
```python
import kuaroutes = kua.Routes()
routes.add('api/:foo', {'GET': my_get_controller})
route = routes.match('api/hello-world')
route.params
# {'foo': 'hello-world'}
route.anything
# {'GET': my_get_controller}
```-> [More](http://kua.readthedocs.io/en/latest/api.html#kua.routes.Routes)
# Docs
[Read The Docs](http://kua.readthedocs.io)
## Tests
```
$ make test
```# Backtracking
Backtracking means kua will have to try more than one path to match the URL.
This *may* hurt performance, depending how much backtracking is involved,
usually you should not worry about it, though.This reduces to not placing two variables next to each other and not placing
a variable where there is a static one in a similar pattern.Here are some examples of good and bad schemas:
```python
# bad
routes.add(':var', ...) # clashes with pretty much every pattern# still bad
routes.add(':var1/:var2', ...)# bad
routes.add(':var1/foo', ...)# still bad
routes.add(':var1/:var2/foo', ...)# bad
routes.add('api/:var1', ...)
routes.add('api/foo', ...)# still bad
routes.add('api/:var1/:var2', ...)
routes.add('api/foo', ...)# still bad
routes.add('api/:var1/foo', ...)
routes.add('api/foo', ...)# good
routes.add('api', ...)
routes.add('api/:var', ...)
routes.add('api/:var/foo', ...)
routes.add('api/:var/bar', ...)
routes.add('api/:var/foo/:var2', ...)
routes.add('api/:var/bar/:var2', ...)# good
routes.add('topics', ...)
routes.add('topics/:var', ...)
routes.add('authors', ...)
routes.add('authors/:var', ...)# good
routes.add('shelf', ...)
routes.add('shelf/books', ...)
routes.add('shelf/books/:var', ...)# good
routes.add('books/all', ...)
routes.add('books/removed', ...)
routes.add('books/pending', ...)
```## License
MIT