nested dictionary and SqliteDict ?
Closed this issue · 5 comments
instasck commented
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
Yes, you can use dicts with sqlitedict. See the README for examples.
instasck commented
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
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
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
Correct, sqlitedict is a key-value store: the keys are strings, the values Python objects.