A pythonic python-only implementation of Tokyo Tyrant protocol. Table extension and query operations are also implementend. Python 2.4+ is needed
This library takes a "pythonic" approach to make it more clear and easy to implement. This code is based on pytyrant
More information about Tokyo Cabinet:
http://tokyocabinet.sourceforge.net/
More information about Tokyo Tyrant:
http://tokyocabinet.sourceforge.net/tyrantdoc/
Hash DatabaseUsing hash database is like using dictionaries on python with some extra features. (You can see the docstrings to learn all the features)
>>> import pyrant
>>> t = pyrant.Tyrant(host='127.0.0.1', port=1978)
>>> t['key'] = 'foo'
>>> print t['key']
foo
>>> t.concat('key', 'bar')
>>> print t['key']
foobar
>>> 'key' in t
True
>>> del t['key']
>>> print t['key']
Traceback (most recent call last):
...
KeyError: 'key'Table Databasepyrant supports table records and query operations. To insert a record just use as before but set a dictionary as value.
Table record example
>>> from pyrant import Tyrant, Q
>>> t = Tyrant(host='127.0.0.1', port=1978)
>>> t['i'] = {'name': 'Martin Conte Mac Donell', 'gender': 'M', 'age': 26}
>>> t['you'] = {'name': 'Guido', 'gender': 'M', 'age': 33}
>>> print t['i']
{'name': 'Martin Conte Mac Donell', 'gender': 'M'}FilterYou can query elements using lazy filters. Every filter is added using "AND" operator, if you want to "OR" some field, you should use Q object (see below).
Keys that you can use in queries are:
__eq: Equals (default) to expression __lt: Less than expression __le: Less or equal to expression __gt: Greater than expression __ge: Greater or equal to expression Query filter example code
>>> res = t.query.filter(gender='M') # Query is not done yet
>>> res # Here query is performed
[{'i': {'gender': 'M', 'age': '26', 'name': 'Martin Conte Mac Donell'}}, {'you': {'gender': 'M', 'age': '33', 'name': 'Guido'}}]Query example using Q
>>> res = t.query.filter(gender='M') # Query is not done yet
>>> res = res.filter(Q(age=26) | Q(age=33)) # Query is not done yet
>>> print res # Here query is performed
[{'i': {'gender': 'M', 'age': '26', 'name': 'Martin Conte Mac Donell'}}, {'you': {'gender': 'M', 'age': '33', 'name': 'Guido'}}]OrderOrdering elements are another great feature. It is used to define result order.
Name parameter is the column name, also you can prefix "-" to order desc. If "#" is added just before column name, column are ordered as numbers
Examples: order('-name') order('-#ranking') order('name')
Order example code
# You can order using:
>>> res.order('#age') # New query is performed
[{'i': {'gender': 'M', 'age': '26', 'name': 'Martin Conte Mac Donell'}}, {'you': {'gender': 'M', 'age': '33', 'name': 'Guido'}}]
>>> res.order('-#age') # New query is performed
[{'you': {'gender': 'M', 'age': '33', 'name': 'Guido'}}, {'i': {'gender': 'M', 'age': '26', 'name': 'Martin Conte Mac Donell'}}]Limit / OffsetYou can also subscript Query objects to limit or offset your results.
Limit example code
>>> res[1:2]
[{'i': {'gender': 'M', 'age': '26', 'name': 'Martin Conte Mac Donell'}}]