简体   繁体   English

使用Klepto保存和编辑Python

[英]Python Saving and Editing with Klepto

Okay so my question is pretty specific and I apologize in advance. 好的,我的问题很具体,我事先表示歉意。 I'm a new programmer and tried developing on my own from scratch. 我是一名新程序员,并尝试从头开始自己开发。 It was relatively successful only I have one last problem, that I can see. 我能看到的最后一个问题是相对成功的。 You can view my code here in its entirety. 您可以在此处完整查看我的代码。

Project 项目

So the problem I'm having is related to the way I save the file. 因此,我遇到的问题与保存文件的方式有关。 I first tried to pickle it since it's a dictionary but I kept getting an error because my dictionary is 我首先尝试将其腌制,因为它是一本字典,但是由于我的字典是
(name, class) pairs. (名称,类别)对。

I searched around on here and saw I could try JSON to do the same. 我在这里搜索了一下,发现可以尝试使用JSON进行相同的操作。 Ended up with the same kind of errors. 最终出现同样的错误。 Eventually I found the klepto module which worked. 最终,我找到了有效的klepto模块。 I saved my dictionary successfully and loaded it successfully. 我成功保存了字典并成功加载了字典。 It wasn't until later I found that I can add new items to the file but whenever I remove something from my dict and save. 直到后来,我发现可以将新项目添加到文件中,但是每当我从字典中删除某些内容并保存时。 The next time I load it. 下次我加载它。 The key I removed is still there. 我删除的钥匙仍然在那里。

TLDR: Can add things just fine to my dict and save to txt file but when I remove from the dict and save it won't save the removed key. TLDR:可以向我的字典添加适当的内容并保存到txt文件,但是当我从dict中删除并保存它时,不会保存已删除的键。

Anyway I'm stumped on where my problem lies in the way I'm saving the file or the way I'm loading it or both? 无论如何,我为问题所在在于保存文件的方式或加载文件的方式,或两者兼而有之? Any help would be very appreciated. 任何帮助将不胜感激。

Edit: Ok I'm assuming it's the way I have it currently set to save and load. 编辑:好的,我假设这是我目前设置为保存和加载的方式。

try:
    alcohols = file_archive('Alcohols.txt')
    alcohols.load()
except IOError:
    alcohols = {}
    print('alcohols doesn\'t exist.')

and

print('Exiting and saving the data.')
        alcohols.dump('Alcohols.txt') #saves the dictionary data as is

It saves the dictionary fine while adding new items but say I have to make an edit and remove something then save and exit. 在添加新项目时,它可以很好地保存字典,但要说我必须进行编辑并删除某些内容,然后保存并退出。 Next time loaded up it would have the old items as well as any new ones. 下次加载时,它将包含旧项目以及任何新项目。 Oddly enough I seem to have broken something in all my editing. 奇怪的是,我似乎在所有编辑中都破坏了某些内容。 Doesn't save new entries. 不保存新条目。

Edit2: EDIT2:

                del alcohols[name] #deletes the key out of the dict

This is how I remove the keys. 这就是我卸下钥匙的方法。 Originally I was using the pop method but when it wouldn't save the changes I tried this. 最初,我使用的是pop方法,但是当它无法保存更改时,我尝试了此方法。 As a note it DID remove they key, value from the dictionary but saving and reloading wouldn't reflect that change. 值得注意的是,它DID从字典中删除了它们的键,值,但是保存和重新加载不会反映出该更改。

                alcohols[name] = Alcohol() #instantiates the new class

Ths is how I am creating new key, value pairs. 这就是我创建新的键,值对的方式。

SOLVED edit: 解决了:

My problem was with the way I deleted them from the dictionary. 我的问题是从字典中删除它们的方式。 In case anyone stumbles into here later. 万一以后有人偶然发现这里。 Look at @Mike Mckerns answer. 看看@Mike Mckerns的答案。 Had to remove from the archived dictionary. 不得不从存档字典中删除。

Basically, you are deleting from the "in-memory" cache, and not from the "file" cache. 基本上,您是从“内存中”缓存中删除,而不是从“文件”缓存中删除。 A klepto archive by default gives you "in-memory" cache, which you use directly through the dict interface, and it also gives you an archive which is the back-end. 默认情况下, klepto存档为您提供“内存中”缓存,您可以直接通过dict界面使用它,还为您提供后端的archive

Thus, when you dump , you transfer the in-memory items to the back-end. 因此,当您dump ,会将内存中的项目传输到后端。 To delete from the cache and from the archive, you have to delete from both. 要从缓存和存档中删除,必须同时从两者中删除。

>>> from klepto.archives import *
>>> arch = file_archive('foo.txt')
>>> arch['a'] = 1
>>> arch['b'] = 2
>>> # look at the "in-memory" copy
>>> arch
file_archive('foo.txt', {'a': 1, 'b': 2}, cached=True)
>>> # look at the "on-disk" copy
>>> arch.archive
file_archive('foo.txt', {}, cached=False)
>>> # dump from memory to the file
>>> arch.dump()
>>> arch.archive
file_archive('foo.txt', {'a': 1, 'b': 2}, cached=False)
>>> arch 
file_archive('foo.txt', {'a': 1, 'b': 2}, cached=True)
>>> # delete from the in-memory cache
>>> arch.pop('a')
1
>>> # delete from the on-disk cache
>>> arch.archive.pop('a')
1
>>> arch
file_archive('foo.txt', {'b': 2}, cached=True)
>>> arch.archive
file_archive('foo.txt', {'b': 2}, cached=False)

I guess I could make it easier to delete from both in a single function call... 我想我可以在一个函数调用中更轻松地从两者中删除...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM