Shelve writes blank lists/dictionarys

為{幸葍}努か 提交于 2021-01-29 03:24:02

问题


I'm trying to make an API to create custom maps on a game I made, but shelve just writes blank lists and dictionarys.

I try to store it like that: "name" for the shown name of the map "name_1" for the name that is treated as variable "textures" for the filepath of the texture "wind" for the amount of wind that's blowing

Heres my code:

import tkinter.filedialog as tfd
import shelve
import shutil as os

file1 = shelve.open("info")

if "written" in file1:
    pass
else:
    file1["name"] = []
    file1["name_1"] = []
    file1["textures"] = {}
    file1["wind"] = {}
    file1.close()
    file1 = shelve.open("info")

name = input("Name: ")

name1 = input("Name zur Variablenspeicherung: ")

wind = input("Wind (*-*): ")

print("Wähle eine 1200x820 GIF-Datei als Hintergrund aus")
texture = tfd.askopenfilename()
os.copy(texture,"textures/"+texture.split("/")[-1].split("\\")[-1])
texture = "custom/textures/"+texture.split("/")[-1].split("\\")[-1]

print("Schreibe Datei...")

file1["name"].append(name)
file1["name_1"].append(name1)
file1["textures"][name1] = texture
file1["wind"][name1] = [int(wind.split("-")[0]),int(wind.split("-")[1])]
file1["written"] = 1

file1.close()

回答1:


shelve will write out changes to keys when you set that key. It can't detect changes to values. Appending to a list or assigning to a key on a dictionary that is value is going to go undetected.

From the shelve documentation:

Because of Python semantics, a shelf cannot know when a mutable persistent-dictionary entry is modified. By default modified objects are written only when assigned to the shelf (see Example). If the optional writeback parameter is set to True, all entries accessed are also cached in memory, and written back on sync() and close(); this can make it handier to mutate mutable entries in the persistent dictionary, but, if many entries are accessed, it can consume vast amounts of memory for the cache, and it can make the close operation very slow since all accessed entries are written back (there is no way to determine which accessed entries are mutable, nor which ones were actually mutated).

Bold emphasis mine.

Either set the writeback parameter to True (and accept the downsides):

file1 = shelve.open("info", writeback=True)

or assign back to the key explicitly:

names = file1["name"]
names.append(name)
file1["name"] = names



回答2:


You need to open your file with writeback=True:

file1 = shelve.open("info",  writeback=True)

From the docs:

Because of Python semantics, a shelf cannot know when a mutable persistent-dictionary entry is modified. By default modified objects are written only when assigned to the shelf (see Example). If the optional writeback parameter is set to True, all entries accessed are also cached in memory, and written back on sync() and close(); this can make it handier to mutate mutable entries in the persistent dictionary, but, if many entries are accessed, it can consume vast amounts of memory for the cache, and it can make the close operation very slow since all accessed entries are written back (there is no way to determine which accessed entries are mutable, nor which ones were actually mutated).



来源:https://stackoverflow.com/questions/41661085/shelve-writes-blank-lists-dictionarys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!