📜 ⬆️ ⬇️

Pyrant + PyModels - the easiest replacement for Django ORM *

Quietly and imperceptibly, the first version of the Pyrant library came out , i.e. Pythonic Tyrant.

Pyrant is a complete python binding for Tokyo Tyrant, a network interface for Tokyo Cabinet.

Tokyo Cabinet is a modern, lightweight, flexible and super data warehouse ( DBM ). It supports several types of databases, including both the simplest key / value and the “table”, or rather document-oriented. The latter puts Tokyo on a par with such glorious products as CouchDB, MongoDB, and others, while TC / TT is much simpler and easier, and therefore faster. In addition, it supports fairly complex data queries (see list of operators ) with the distinction between string and numeric values. And for Tokyo Cabinet, you can write extensions to Lua.
And still, and still - PyModels ! Yes, Pyrant + PyModels already strongly reminds Django ORM. Only easier hundreds of times :)
')
Example of working with Pyrant:

 $ ttserver test.tct &
 $ python
 >>> import pyrant
 >>> t = pyrant.Tyrant ()
 >>> t.query
 []
 >>> t ['guido'] = {'name': 'Guido', 'age': 53}
 >>> t ['larry'] = {'name': 'Larry', 'age': 55}
 >>> t.query.filter (age__gt = 53)
 [(u'guido ', {u'name': u'Guido ', u'age': u'53 '})]
 >>> t.query.filter (name__matches = '^. + ry $')
 [(u'larry ', {u'name': u'Larry ', u'age': u'55 '})]]
 >>> guys = t.query.filter (name = 'Guido') |  t.query.filter (name = 'Larry')
 >>> guys.columns ('age')
 [{u'age ': u'53'}, {u'age ': u'55'}]

Here we see that numbers are stored as strings (although it is possible to make selections and sorting by numbers as by numbers). The aforementioned PyModels library allows you to automatically process incoming and outgoing values ​​as needed, and also wraps the sample results into model instances, so that everything is as convenient as possible:

 >>> storage = Storage () # uses Pyrant
 >>> class Person (Model):
 ... name = Property ()
 ... age = Number ()
 ... def __unicode __ (self):
 ... return self.name
 >>> q = Person.query (storage) .filter (name__contains = 'G')
 >>> guido = q [0]
 >>> guido
 <Person Guido>
 >>> guido.age
 53

_________
Notes:
* Pyrant + PyModels - really a replacement for Django ORM, but no silver bullet. Let's live in peace. Databases of all types, unite. =)

UPD: Models have been renamed to PyModels, the topic is updated.

Source: https://habr.com/ru/post/80062/


All Articles