Python Saving and Editing with Klepto

前端 未结 1 627
没有蜡笔的小新
没有蜡笔的小新 2021-01-03 05:31

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 hav

1条回答
  •  失恋的感觉
    2021-01-03 06:05

    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.

    Thus, when you dump, you transfer the in-memory items to the back-end. 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...

    0 讨论(0)
提交回复
热议问题