piskvorky / sqlitedict

Persistent dict, backed by sqlite3 and pickle, multithread-safe.
Apache License 2.0
1.17k stars 131 forks source link

nested dictionary and SqliteDict ? #104

Closed instasck closed 5 years ago

instasck commented 5 years ago

The way I use nested dictonery is as so:

nested_dict = lambda: defaultdict(nested_dict)
ips = nested_dict()
ips[ip]['count'] = 1
ips[ip]['proxy'] = 2
...

Can it be used with this sqlitedict but not in a form of object (that not do autocommit)

piskvorky commented 5 years ago

Yes, you can use dicts with sqlitedict. See the README for examples.

instasck commented 5 years ago

Yes, you can use dicts with sqlitedict. See the README for examples.

This is not dict, it is defaultdict, nested dict, I could not do it. can you show an example for it ?

instasck commented 5 years ago
from collections import defaultdict
from sqlitedict import SqliteDict

class nested_dict_sqlite(defaultdict):
    'like defaultdict but default_factory receives the key'

    def __missing__(self, key):
        self[key] = value = self.default_factory(key)
        return value

dicts = nested_dict_sqlite(lambda table: SqliteDict('dicts.sqlite3', table, 'c', True))
dicts['dict1']['x'] = 1
dicts['dict2']['y'] = 2

print(dicts['dict1']['x'])
print(dicts['dict2']['y'])
print(dicts['dict2']['yy'])

This is the only thing I could make, using tables as first argument, still not sure if I get it right.

instasck commented 3 years ago

Yes, you can use dicts with sqlitedict. See the README for examples.

But each time you want to change a value of specific ket - you need to fetch the whole dict.

piskvorky commented 3 years ago

Correct, sqlitedict is a key-value store: the keys are strings, the values Python objects.