patx / pickledb

pickleDB is an open source key-value store using Python's json module.
https://patx.github.io/pickledb
BSD 3-Clause "New" or "Revised" License
925 stars 125 forks source link

some improvements #1

Closed jabbalaci closed 12 years ago

jabbalaci commented 12 years ago

Hi,

I would have some ideas for improvements.

First, it should be called jsondb. You use json instead of pickle, so the name is very misleading. The line "import json as pickle" is terrible.

I would put everything in a class. Then you could have an interface like this:

jdb = JsonDb('test.db')      # __init__ would do the job of your load() function
jdb.set('key', 'value')
jdb.get('key')

Global variables are discouraged to use, but if you group everything in a class, you could use those variables in the class with 'self', like self.db, etc.

Also, you do lots of I/O operations. It should be kept in memory and the user could call dump himself: jdb.dump() , and then you write everything to a file.

Add comments to every function. At the end of the source, add this:

if __name__ == "__main__":
    # write an example for every function
    # then the users will understand immediately how to use it

Laszlo

medecau commented 12 years ago

The name of the db is irrelevant at this point. Today it's json tomorrow may be tomorrows fancy format. I agree that it is a bit misleading because of the existence of the pickle module.

I've applied changes that put everything in a class: https://github.com/medecau/pickledb/commit/694cbbcf5c33364773faafbbb2357d41fd5aaa41

I agree with the point about too many I/O ops, I have some ideas about fixing this: Timed updates, forced updates(like @jabbalaci suggested) and or update inside __del__()

Regarding comments to the functions take a look at: https://github.com/medecau/pickledb/commit/feefa83a70b64ef7181cda1cd2d813004bdfc0f8

patx commented 12 years ago

medecau's fork is really good and basically does everything this suggestion says. The last commit added the "dump" function is really good.