Search code examples
klepto

Keys for klepto dir_archive?


I've created a klepto dir_archive.

On subsequent archive access, how can the archive keys be determined without loading the entire archive into memory?


Solution

  • Something like this?

    >>> import klepto as kl
    >>> kl.archives.dir_archive()
    dir_archive('memo', {}, cached=True)
    >>> d = _
    >>> d['a'] = 0
    >>> d['b'] = 1
    >>> d['c'] = 2
    >>> d
    dir_archive('memo', {'a': 0, 'c': 2, 'b': 1}, cached=True)
    >>> d.dump()
    >>> 
    

    Then restart the session...

    >>> import klepto as kl
    >>> d = kl.archives.dir_archive()
    >>> d
    dir_archive('memo', {}, cached=True)
    >>> d.archive.keys()
    ['a', 'c', 'b']
    

    There are also several private methods, if you'd need something peculiar:

    >>> d.archive._keydict()
    {'a': None, 'c': None, 'b': None}
    

    But, the main point is: you can easily interact with the dir_archive without loading it, by using the archive attribute.