diff --git a/cache_manager/_cache.py b/cache_manager/_cache.py index 6bb5e79..49a4524 100644 --- a/cache_manager/_cache.py +++ b/cache_manager/_cache.py @@ -15,6 +15,7 @@ import datetime import functools as ft import collections +from collections.abc import Mapping from pypath_common import _misc import platformdirs @@ -1108,7 +1109,7 @@ def update( ( 0, self._quotes(group) - if isinstance(vals, dict) + if isinstance(vals, Mapping) else 'NULL', k, v @@ -1116,7 +1117,7 @@ def update( for group, vals in update.get('attrs', {}).items() for k, v in ( vals - if isinstance(vals, dict) else + if isinstance(vals, Mapping) else {group: vals} ).items() if ( diff --git a/cache_manager/_item.py b/cache_manager/_item.py index 62c0f13..62e2230 100644 --- a/cache_manager/_item.py +++ b/cache_manager/_item.py @@ -283,6 +283,19 @@ def path(self) -> str: return os.path.join(d, self.cache_fname) + @property + def size(self) -> int | None: + """ + Size of the file in bytes. + + Returns: + Size of the file in bytes. + """ + + if os.path.exists(path := self.path): + + return os.path.getsize(path) + @property def rstatus(self) -> int: diff --git a/tests/test_item.py b/tests/test_item.py new file mode 100644 index 0000000..2fad4f1 --- /dev/null +++ b/tests/test_item.py @@ -0,0 +1,15 @@ +import os + +import pytest + +def test_size(test_cache): + it = test_cache.create('test-file-size') + path = it.path + + assert it.size is None + + with open(path, 'w') as fp: + fp.write('something') + + assert os.path.exists(path) + assert it.size > 8