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


there module named tzwhere in python. when load in tz variable in:-

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

the line above takes 45 seconds load , scope until particular python session stopped. after line can many outputs of tz.tznameat(latitude, longitude) want, problem these outputs calculated in python shell.

i want make tz variable shareable api, such if tz variable called python session or if java program using exec command, neither should take 45 seconds load again , nor should give me nameerror: name 'tz' not defined.

please help. thank much!!

you use pickle module can store class instances in files.

try this:

 tzwhere import tzwhere  tz = tzwhere.tzwhere(shapely=true)   import pickle   # dump variable tz file save.p  pickle.dump( tz, open( "save.p", "wb" ) ) 

to load tz script do:

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

note: python 2 only, use faster version automatically if on python3

if still unhappy speed when loading tz script, there accelerated version of pickle called cpickle

just do:

import cpickle pickle 

for more information goto link: https://wiki.python.org/moin/usingpickle


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -