I am reading an XML file and reorganizing the desired data into Python data structures (lists, tuples, etc.)
For example, one of my XML parser modules produces the following data:
# data_miner.py
animals = ['Chicken', 'Sheep', 'Cattle', 'Horse']
population = [150, 200, 50, 30]
Then I have a plotter module that roughly says, e.g.:
# plotter.py
from data_miner import animals, population
plot(animals, population)
Using this method, I have to parse the XML file every time I do a plot. I’m still testing other aspects of my program and the XML file doesn’t change as frequently for now. Avoiding the parse stage would dramatically improve my testing time.
This is my desired result:
In between data_miner.py and plotter.py, I want a file that contains animals and population such that they can be accessed by plotter.py natively (e.g. no change in plotting code), without having to run data_miner.py every time. If possible, it shouldn’t be in csv or any ASCII format, just a natively-accessible format. plotter.py should now look roughly like:
# plotter.py
# This line may not necessarily be a one-liner.
from data_file import animals, population
# But I want this portion to stay the same
plot(animals, population)
Analogy:
This is roughly equivalent to MATLAB’s save command that saves the active workspace’s variables into a .mat file. I’m looking for something similar to the .mat file for Python.
Recent experience:
I have seen pickle and cpickle, but I’m not sure how to get it to work. If that is the right tool to use, example code would be very helpful. There may also be other tools that I don’t know yet.
The
picklemodule, or its faster equivalentcPickle, should serve your needs well.Specifically:
and
Here, I’ve made
data_miner.pyquite explicit regarding what needs to be saved (always an excellent idea to be very explicit unless you have extremely specific reasons to do otherwise). Some things (such as modules and open files) cannot be pickled anyway, so a simple pickling ofglobals()would not work.If you absolutely must, you could make a copy of
globals()while removing all objects whose types make them unsuitable for saving; or, perhaps better, religiously use a leading_in every name you don’t want to save (soimport pickle as _pickle,with open ... as _f, and so forth) and exclude from the copy ofglobals()all names with a leading underscore == with such an approach, thepickle.loadwould retrieve adict, then the variables of interest would be extracted from it by indexing. However, I would strongly recommend the simple alternative of saving alist(ordict, if you want;-) with the specific values that are actually of interest, rather than taking a “wholesale” approach.