Prabhat Nagpal
Prabhat Nagpal

Reputation: 51

Python: How to keep a variable in memory such that it can be called from other python scripts?

There is a module named tzwhere in python. When I load it in the tz variable as in:-

from tzwhere import tzwhere
tz = tzwhere.tzwhere(shapely=True)

The line above takes about 45 seconds to load up and its scope is until that particular python session is stopped. So after this line I can get as many outputs of tz.tzNameAt(latitude, longitude) as I want, but the problem is that these outputs are quickly calculated only in that python shell.

I want to make tz variable shareable just like an API, such that if tz variable is called from any python session or even if from any java program using exec command, neither it should take 45 seconds to load up again and nor should it give me NameError: name 'tz' is not defined.

Please Help. Thank you very much!!

Upvotes: 3

Views: 3939

Answers (2)

CodenameLambda
CodenameLambda

Reputation: 1496

You cannot share any python object in memory even after python has closed. You can, however, save the state of the object (if the library does support it, if it doesn't, this solution will not work) with pickle. pickle is a library that ships with python, and can save the state of most objects to a file.

Here is an example for pickle:

To save the state:

import pickle
obj = [1, 2, 3, 4]
f = open("/path/to/the/file/where/the/data/should/be/stored.pickle", 'wb')  # you do not have to use the suffix '.pickle' however.
pickle.dump(obj, f)
f.close()

To retrieve it:

import pickle
f = open("/path/to/the/file/where/the/data/should/be/stored.pickle", 'rb')
obj = pickle.load(f)
f.close()

Or for your example, run this thing once:

from tzwhere import tzwhere
import pickle
f = open("/path/to/the/file", 'wb')
pickle.dump(tzwhere.tzwhere(shapely=True))
f.close()

And use this to retrieve it:

import pickle
f = open("/path/to/the/file", 'rb')
tz = pickle.load(f)
f.close()

Or as a one-liner, so that it doesn't take up that much space:

import pickle;f=open("/path/to/the/file",'rb');tz=pickle.load(f);f.close()

I hope that helps,

CodenameLambda

PS: If you want to know how pickle works, just look at the documentation.

Upvotes: 0

Andrei Tumbar
Andrei Tumbar

Reputation: 480

You could probably use the pickle module which can store class instances in files.

Try something like this:

 from tzwhere import tzwhere
 tz = tzwhere.tzwhere(shapely=True)

 import pickle

 # Dump the variable tz into file save.p
 pickle.dump( tz, open( "save.p", "wb" ) )

To load tz from another script just do:

import pickle

tz = pickle.load( open( "save.p", "rb" ) )

NOTE: Python 2 ONLY, will use faster version automatically if on Python3

If you are still unhappy with the speed when loading tz from another script, there is an accelerated version of pickle called cPickle

Just do:

import cPickle as pickle

For more information goto this link: https://wiki.python.org/moin/UsingPickle

Upvotes: 1

Related Questions