Skip to main content

Python in-memory embedded and non-relationnal database

Project description

Python in-memory embedded and non-relational database.

For development and test, heavily inspired by Mongodb.

There are three serializers at this time, msgpack, Pickle and Json for storage.

Sofart can be use in-memory synced with multi mode..

Sofart is Python 3 ready.


Install with pip:

pip install sofart


Easy use:

>>> from sofart import Database

Create Database and a collection:

>>> db = Database('/tmp/so.fart')
>>> db.create_collection('test_collection')
>>> db.collection_names()

Play with collection:

>>> c = db.test_collection
>>> post = {"artist": "Jambon",
...         "track": "I love my jambon"}
>>> for i in c.find_one():
...     print(i)
{'track': 'I love my jambon', '_id': 'b2d6bf60-6c11-4e26-9357-efb28056e60d', 'artist': 'Jambon'}
>>> c.remove('b2d6bf60-6c11-4e26-9357-efb28056e60d')
>>> c.find_one()

Some filter:

      >>> c.find_one({"artist": "Jambon"})
      {'track': 'I love my jambon', '_id': 'b2d6bf60-6c11-4e26-9357-efb28056e60d', 'artist': 'Jambon'}
      >>> c.find_one({"artist":"Bieber"})


You can run test under tests/ And there is a populate script into tests/


Single(in-memory) is highly faster than multi cause it’s mainly work in RAM and just down data when sync method is called. In otherwise multi down data at each request.

But you can have a pretty data control with sync method which down data in file when you call it.


Serializer Py2 Py3 Pypy
Json 1 1 1
MsgPack 1 1 X
Pickle 1 1 1

Pypy and msgpack-pure are not supported.


You can easily write your own serializer, have a look at serializers/ or file.

Remember that if someone write Ruby or other language driver for sofart, maybe using json or msgpack could be a good idea.


class Database


Database(str(path), str(mode), str(serializer)) : Path is database file path
                                                : Mode is single or multi (Default: `single`)
                                                : Serializer like msgpack, json or pickle (Default: `json`)


path: Return database path


create_collection(str(name))    : Create new collection
drop_collection(str(name))      : Drop collection
rename(str(name), str(new_name)): Rename collection `name` to `new_name`
collection_names(): Return database collections list (same as `collections` attribut)
count()           : Return total database entries
sync()            : Save every changes in database file

Retrieve collection with the followings methods:

c = db.my_collection
c = db['my_collection']

class Collection


name: Return collection name


drop()           : Drop collection
count()          : Return total collection entries
save(dict(enreg)): Save entry into collection
remove(str(_id)) : Remove entry from collection
sync()           : Save every changes in database file
rename(str(name)): Rename collection to `name`
find_one(spec_or_id)              : Return first founded result
find(dict(spec_or_id), int(limit)): Iterator which return `limit` result founded (limit=0 return all)


At this moment just following operands are available:

  • <
  • <=
  • >
  • >=
  • all
  • exists
  • mod
  • ne
  • in
  • nin

This is an example:

>>>{"value": 2})
>>> c.find({"value": {"$exists": True}})
[{'_id': '47e53aea-85b4-434b-8961-40e89c877b41', 'value': 2}]
>>> c.find({"value": {"$in": [2, 3, 67]}})
[{'_id': '47e53aea-85b4-434b-8961-40e89c877b41', 'value': 2}]
>>> c.find({"value" : { "$gt": 1 }})
[{'_id': '42567296-7d78-43b7-a4e0-50447b80eca8', 'value': 2}]

And another:

>>> c.find({"value" : { "$gte": 2 }})
[{'_id': '42567296-7d78-43b7-a4e0-50447b80eca8', 'value': 2}]
>>> c.find({"value" : { "$gte": 2, "$lt" : 1 }})
>>> c.find({"value": {"$mod": [2, 0]}})
[{'_id': '47e53aea-85b4-434b-8961-40e89c877b41', 'value': 2}]

More informations here.


Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for sofart, version 0.3.6
Filename, size File type Python version Upload date Hashes
Filename, size sofart-0.3.6.tar.gz (10.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page