Is it possible to save data as python modules?

This is what I did for the project. I have several data structures that are basic dictionaries with some methods that work with data. When I save them to disk, I write them to .py files as code that, when imported as a module, loads the same data into such a data structure.

It is reasonable? Are there any major flaws? The advantage that I see is that when I want to work with stored data, I can quickly import the modules that I need. In addition, modules can be used separately from the rest of the application because you do not need a separate parser or bootloader function.

+3
source share
5 answers

This is reasonable, and I do it all the time. Obviously, this is not a format that you use to exchange data, so it is not a good format for anything like a save file.

But, for example, when I transfer sites to Plone, I often get information about the site (for example, a list of pages to be redirected to, or a list of how old URLs should be displayed on new ones, or lists of tags). This you usually get in Word och Excel format. In addition, the data often needs to be massaged by bits, and I end up with dictionaries for all purposes and purposes that display one URL for some other information.

Of course, I could save this as CVS and parse it in a dictionary. But instead, I usually save it as a Python file with a dictionary. Saves the code.

, , , , , . , , , .

+3

, , . , , , , ; - , , , (pickle - , , , , ; -).

, : ( , R/W ) - . ( ..) , (, , JSON, CSV, XML,... c, , ).

+7

, , , , . , - , , .

+3

Pickle, python .

+3

, . : JSON.

JSON , Python ; JSON. json Python 3.0 simplejson, simplejson Python 2.x json Python 3.0 .

- XML. XML ( ), , , ..

, , . SQLite : , , SQL. ORM Python , SQL ; ORM SQLite Autumn ( ), ORM Django ( SQL!). , - SQLite, , PostgreSQL. If you find that you write a lot of loops that look at your saved data, and especially if you need to force dependencies (for example, if foo is deleted, the panel must also be deleted), consider moving to the database.

+3
source

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


All Articles