Loading Files Into Variables
I am trying to write a small function that gets a variable name, check if it exists, and if not loads it from a file (using pickle) to the global namespace. I tried using this in a
Solution 1:
Using 'globals' has the problem that it only works for the current module. Rather than passing 'globals' around, a better way is to use the 'setattr' builtin directly on a namespace. This means you can then reuse the function on instances as well as modules.
import cPickle
## Load if neccesary#defloadfile(variable, filename, namespace=None):
if module isNone:
import __main__ as namespace
setattr(namespace, variable, cPickle.load(file(filename,'r')))
# From the main script just do:
loadfile('myvar','myfilename')
# To set the variable in module 'mymodule':import mymodule
...
loadfile('myvar', 'myfilename', mymodule)
Be careful about the module name: the main script is always a module main. If you are running script.py and do 'import script' you'll get a separate copy of your code which is usually not what you want.
Solution 2:
You could alway avoid exec entirely:
import cPickle
## Load if neccesary#defloadfile(variable, filename):
g=globals()
if variable notin g:
g[variable]=cPickle.load(file(filename,'r'))
EDIT: of course that only loads the globals into the current module's globals.
If you want to load the stuff into the globals of another module you'd be best to pass in them in as a parameter:
import cPickle
## Load if neccesary#defloadfile(variable, filename, g=None):
if g isNone:
g=globals()
if variable notin g:
g[variable]=cPickle.load(file(filename,'r'))
# then in another module do this
loadfile('myvar','myfilename',globals())
Post a Comment for "Loading Files Into Variables"