CPickle class with saving data to a file

I have a big class in Python - it's a class like "DataBase". I want to save it to a file - everything, including data.

This is the input (an example to show the problem, in the script the database is 10,000 records):

import cPickle
# DataBase-like class
class DataBase:
    class Arrays: pass
    class Zones: pass
    class Nodes: 
        class CR: pass    
    class Links: 
        class CR: pass
    class Turns:
        class CR: pass    
    class OrigConnectors: pass
    class DestConnectors: pass    
    class Paths: pass
    pass
# some basic input into database
DataBase.Arrays.Data=[]
for i in range(1000):
    DataBase.Arrays.Data.append([i+4]) 

print DataBase.Arrays.Data[56]

# and now I want to save it to file

import cPickle    

filename='D:/results/file.lft'
file=open(filename,'w')
cPickle.dump(DataBase, file, protocol=2)`

And the file that I get:

€c__main__
DataBase
q.`

No data, just a definition. How can I overcome this? Perhaps saving my data as a class is not a good idea?

0
source share
1 answer

You etch "DataBase", which is a class definition. You need to instantiate an object of the DataBase class, and then sort it.

objDataBase = DataBase()
objDataBase.Arrays.Data = etc....

filename='D:/results/file.lft'
file=open(filename,'w')
cPickle.dump(objDataBase, file, protocol=2)
file.close()
+4
source

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


All Articles