How to avoid re-importing modules and re-defining large object every time a script runs
Question: This must have an answer but I cant find it. I am using a quite large python
module called quippy. With this module one can define an intermolecular
potential to use as a calculator in ASE like so:

    
    
    from quippy import *
    from ase import atoms
    pot=Potential("Potential  xml_label=gap_h2o_2b_ccsdt_3b_ccsdt",param_filename="gp.xml")
    some_structure.set_calculator(pot)
    

This is the beginning of a script. The problem is that the `import` takes
about 3 seconds and `pot=Potential...` takes about 30 seconds with 100% cpu
load. (I believe it is due to parsing a large ascii xml-file.) If I would be
typing interactively I could keep the module imported and the potential
defined, but when running the script it is done again on each run.

Can I save the module and the potential object in memory/disk between runs?
Maybe keep a python process idling and keeping those things in memory? Or run
these lines in the interpreter and somehow call the rest of the script from
there?

Any approach is fine, but some help is be appreciated!


Answer: You can either use raw files or modules such as `pickle` to store data easily.

    
    
    import cPickle as pickle
    from quippy import Potential
    try:  # try previously calculated value
        with open('/tmp/pot_store.pkl') as store:
            pot = pickle.load(store)
    except OSError:  # fall back to calculating it from scratch
        pot = quippy.Potential("Potential  xml_label=gap_h2o_2b_ccsdt_3b_ccsdt",param_filename="gp.xml")
        with open('/tmp/pot_store.pkl', 'w') as store:
            pot = pickle.dump(pot, store)
    

There are various optimizations to this, e.g. checking whether your pickle
file is older than the file generating it's value.

