In Python, how do I bind a JSON file on disk to a process built into the dictionary?

Perl had this idea of โ€‹โ€‹a link operator, where writing or changing a variable can execute arbitrary code (for example, updating some basic Berkeley database file). I am pretty sure there is this concept of overloading in python too.

I am interested to know what the most idiomatic way is to basically consider the local JSON file as the canonical source of the necessary hierarchical information while the python script is running, so that changes to the local dictionary are automatically reflected in the JSON file. Iโ€™ll leave it in the OS to optimize recording and caching (I donโ€™t mind if the file is mainly updated dozens of times throughout the script), but in the end itโ€™s just a kilobyte of metadata, which Iโ€™d โ€‹โ€‹like to get around. No need to contact parallel access to this.I just want to have access to the hierarchical structure (for example, a nested dictionary) in the python process and read (and write) that the structure automatically leads to reading (and changing) the local JSON file.

+5
source share
3 answers

ok, since python itself does not have signal slots, I think you can instead make your own dictionaries class, inheriting it from the python dictionary. The class is exactly the same as the python dict, only in each of its methods, which can change the values โ€‹โ€‹of the dict, you reset json.

also you can use smth, e.g. PyQt4 QAbstractItemModel , which has signals. And when this data changes the signal, it comes out, do your dumping - it will only be in one place, which is nice.

I know these two are silly ways, maybe yes. :) If someone knows better, go ahead and tell me!

+2
source

This is a response from aspect_mkn8rd 'given Gerrat's comments, but it is too long for a true comment.

You will need 2 special classes of containers emulating a list and a dictionary. In both cases, you add a pointer to a top-level object and override the following methods:

  • __setitem__(self, key, value)
  • __delitem__(self, key)
  • __reversed__(self)

All these methods are called in modification and must have a top-level object for writing to disk.

In addition, __setitem__(self, key, value) should look to see if the value is a list and transfers it to a special list object or, if it is a dictionary, inserts it into a special dictionnary object. In both cases, the method should set the top-level object in a new container. If none of them and the object defines __setitem__ , it should raise an exception message that does not support the object. Of course, you must then modify the method to take this new class into account.

Of course, there is a lot of code for writing and testing, but it should work - left to the reader as an exercise :-)

+1
source

If concurrency is not required, maybe you should write two functions to read and write data to the shelf file? Our idea is for the dictionary to โ€œknowโ€ about changes to update the file without these kinds of things?

0
source

Source: https://habr.com/ru/post/1200702/


All Articles