python-diskcache icon indicating copy to clipboard operation
python-diskcache copied to clipboard

JSONDisk example not working

Open mrtolkien opened this issue 3 years ago • 2 comments

There is a Disk tutorial in the documentation at this address: https://grantjenks.com/docs/diskcache/tutorial.html#disk

It seems obsolete, as running the code errors with:

./tests/test_storage.py::test_dump_diskcache_zstd Failed: [undefined]TypeError: JSONDisk.store() got an unexpected keyword argument 'key'
def test_dump_diskcache_zstd():
>       time = storage.dump_diskcache_zstd()

tests/test_storage.py:65: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
storage/diskcache_zstd.py:88: in dump_diskcache_zstd
    cache.set(file, data)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <diskcache.core.Cache object at 0x7f56111b8a60>
key = 'EUW1_5534893159_matchtimeline.json'
value = {'info': {'frameInterval': 60000, 'frames': [{'events': [{'realTimestamp': 1635859409575, 'timestamp': 0, 'type': 'PAU...4WUdQDTxpG4uNYadBFtEA2VXf5mw', 'Pud-6KdqMMcPp3fWG2RDUMZe840NHsSjb0iLixC_-8uN5OVrObmUI28ObrAHoWDiM_L2OoV7af14iw', ...]}}
expire = None, read = False, tag = None, retry = False

    def set(self, key, value, expire=None, read=False, tag=None, retry=False):
        """Set `key` and `value` item in cache.
    
        When `read` is `True`, `value` should be a file-like object opened
        for reading in binary mode.
    
        Raises :exc:`Timeout` error when database timeout occurs and `retry` is
        `False` (default).
    
        :param key: key for item
        :param value: value for item
        :param float expire: seconds until item expires
            (default None, no expiry)
        :param bool read: read value as bytes from file (default False)
        :param str tag: text to associate with key (default None)
        :param bool retry: retry if database timeout occurs (default False)
        :return: True if item was set
        :raises Timeout: if database timeout occurs
    
        """
        now = time.time()
        db_key, raw = self._disk.put(key)
        expire_time = None if expire is None else now + expire
>       size, mode, filename, db_value = self._disk.store(value, read, key=key)
E       TypeError: JSONDisk.store() got an unexpected keyword argument 'key'

../../../.cache/pypoetry/virtualenvs/json-cold-storage-comparison-2wgndtiW-py3.10/lib/python3.10/site-packages/diskcache/core.py:772: TypeError

I'm not exactly sure what needs updating as I'm not familiar with the project!

Edit: looking at the source code of the project it seems the doc is simply missing the key argument: https://github.com/grantjenks/python-diskcache/blob/d55a50ee083784afa9c85e14e41c4a2d132f3111/diskcache/core.py#L335

mrtolkien avatar Apr 28 '22 06:04 mrtolkien

Yeah, docs need updating.

Rather than copy/pasting from the docs, use the tested version in the source: https://github.com/grantjenks/python-diskcache/blob/master/diskcache/core.py#L335

grantjenks avatar Apr 28 '22 06:04 grantjenks

Did that and it indeed works! Was able to implement a ZSTDDisk class instead which is both faster and compresses better than the existing JSONDisk class.

mrtolkien avatar Apr 28 '22 06:04 mrtolkien

Fixed, will release in v5.6.1

grantjenks avatar Apr 18 '23 05:04 grantjenks