Although RavenDB is designed to be used with C# and HTTP, a Python client also exists. This client is developed by Patrick McEvoy aka ‘firegrass’ and can be found at https://github.com/firegrass/ravendb-py. Installation is very straightforward and can be done by downloading the zip file with the source code, unzipping that to a directory and executing the following command:
python setup.py install.
Working with this client can be a bit tricky as the documentation is minimal and the readme is not always completely correct. So next I will describe the steps that you can take to create a connection and get some data from the server.
First you have to import the ravendb-py module:
from ravendb import *
This way you can just use the plain name instead of typing ravendb.name
Next you have to create a connection to the database:
If you know the documentIDs of the documents that you want to retrieve, you can use the following code. Make sure to pass the documentIDs as an array or otherwise you will get an HTTP error (500). With this code you can use a ‘/’ in the ID but when using indexes, this doesn’t work anymore (cfr. infra).
If you now want to access a field, e.g. the email address you just type: results.Email. This will give you: u’firstname.lastname@example.org’. As you can see there is a u prefix when you look at the results in a python interpreter. This just means that the following string is Unicode string. Printing the variable to screen will not display the u and comparing the string in python to the same string without the u will also yield True. This only happens in python 2.x as in python 3.x every string is assumed to be Unicode while in python 2.x it is assumed to be an 8-bit string.
Another way to get documents is by using the available indexes. The command seems straightforward but this is only true if you use indexes that don’t contain a ‘/’. For instance, the following code will give you a result if you have a index called documentsByState and searched for deleted documents.
If, however, your index contains a ‘/’, like e.g. Companies/ByCodeAndName, the client will report an HTTP 500 error. This is because when creating the http request, the ‘/’ still remains there and therefore the server doesn’t find the appropriate index. The key here is to replace the ‘/’ with ‘%2F’. So the following code gives an error:
While the following code will give you a result:
As you can see, the returned resultset is a bunch itself. The collected documents are also placed in an array. So if you would want to get the first document you would have to run the code:
As the python client actually just sends http requests to the server, it is possible to extend the code to allow for more complex or other queries should that be necessary.