python shelve 不保存/加载

发布于 2025-01-14 04:29:20 字数 1358 浏览 3 评论 0原文

当我通过子文件中的函数保存/加载工作区时,搁置不起作用(test1)。 但是,如果我在一个文件中执行相同的操作,它就会起作用(test2)。这是为什么?如何解决第一种情况的问题?

在主文件中:

# 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()

When I save/load my workspace via functions in a subfile, shelve doesn't work (test1).
However, if I do the same in one file, it works (test2). Why is that? How can I fix the problem for the first case?

In the main file:

# 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() 

In the main file:

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何其悲哀 2025-01-21 04:29:20

这是 globals()-你的问题的相关部分(我猜):

返回实现当前模块命名空间的字典。 对于函数内的代码,这是在定义函数时设置的,并且无论在何处调用该函数都保持不变。

因此函数中的 globals() 始终是命名空间saveWS.py

以及此处 dir()-相关一:

不带参数,返回当前本地范围内的名称列表。使用参数,尝试返回该对象的有效属性列表。

因此,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())

您可以通过使用

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

而不 使用 variables 参数来解决这个问题(导入sys之后)。在您的用例中,您可能可以进一步将 namespace['__name__'] 替换为 '__main__'。如果您想摆脱所有附加参数,您可以尝试如果这对您有用:

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's the globals()-related part of your problem (I guess):

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.

So globals() in your functions is always the namespace of saveWS.py.

And here the dir()-related one:

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.

Therefore dir() refers to the local namespace within the function.

You probably could fix that by passing dir() and globals() as arguments:

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() 

Calling:

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

You could do without the variables argument by using

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

instead (after importing sys). In your usecase you could probably further replace namespace['__name__'] with '__main__'. If you want to get rid of all the additional arguments you could try if this works for you:

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() 

Check here if that's appropriate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文