python搁置不saving/loading

python shelve is not saving/loading

当我通过子文件中的函数 save/load 我的工作区时,搁置不起作用 (test1)。 但是,如果我在一个文件中执行相同的操作,它就可以工作(测试 2)。这是为什么?第一种情况如何解决?

在主文件中:

# in saveWS.py   

# to save
def saveSlv(fileName):
    destination='./'+fileName+'_shelve.pkl'
    bk = shelve.open(destination,'n')
    for k in dir():
        try:
            bk[k] = globals()[k]
        except Exception:
            pass
    bk.close()
        
# to restore
def loadSlv(fileName):
    myshelve ='./'+fileName+'_shelve.pkl'
    bk_restore = shelve.open(myshelve)
    for k in bk_restore:
        globals()[k] = bk_restore[k]
    bk_restore.close() 

在主文件中:

import shelve
# User defined functions
from saveWS import saveSlv, loadSlv 

# It doesn't work
a=1,2,3
b='ypk'
fileName='test1'
# save the variables in work space by calling a function
saveSlv(fileName)
del a, b
# restore the work space by calling a function
loadSlv(fileName)
 
# It works
a=1,2,3
b='ypk'
fileName='test2'

# save the variables in work space
destination='./'+fileName+'_shelve.pkl'
bk = shelve.open(destination,'n')
for k in dir():
    try:
        bk[k] = globals()[k]
    except Exception:
        pass
bk.close()
del a, b

# restore the work space
myshelve ='./'+fileName+'_shelve.pkl'
bk_restore = shelve.open(myshelve)
for k in bk_restore:
    globals()[k] = bk_restore[k]
bk_restore.close()

Here's 您问题的 globals() 相关部分(我猜):

Return the dictionary implementing the current module namespace. For code within functions, this is set when the function is defined and remains the same regardless of where the function is called.

所以在你的函数中 globals() 总是 saveWS.py 的命名空间。

here dir()相关的一个:

Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.

因此 dir() 在 函数中引用本地命名空间

您可能可以通过传递 dir()globals() 作为参数来解决这个问题:

def saveSlv(fileName, variables, namespace):
    destination='./'+fileName+'_shelve.pkl'
    bk = shelve.open(destination,'n')
    for k in variables:
        try:
            bk[k] = namespace[k]
        except Exception:
            pass
    bk.close()

def loadSlv(fileName, namespace):
    myshelve ='./'+fileName+'_shelve.pkl'
    bk_restore = shelve.open(myshelve)
    for k in bk_restore:
        namespace[k] = bk_restore[k]
    bk_restore.close() 

通话中:

saveSlv(fileName, dir(), globals())
loadSlv(fileName, globals())

你可以不用 variables 参数,使用

...
    for k in dir(sys.modules[namespace['__name__']]):
        ...

(在导入 sys 之后)。在您的用例中,您可能会进一步用 '__main__' 替换 namespace['__name__']。如果你想摆脱所有额外的参数,你可以尝试如果这对你有用:

import shelve
import sys

def saveSlv(fileName):
    destination='./'+fileName+'_shelve.pkl'
    bk = shelve.open(destination,'n')
    namespace = sys.modules['__main__'].__dict__
    for k in dir(sys.modules['__main__']):
        try:
            bk[k] = namespace[k]
        except Exception:
            pass
    bk.close()


def loadSlv(fileName):
    namespace = sys.modules['__main__'].__dict__
    myshelve ='./'+fileName+'_shelve.pkl'
    bk_restore = shelve.open(myshelve)
    for k in bk_restore:
        namespace[k] = bk_restore[k]
    bk_restore.close() 

检查 here 是否合适。