diff options
| -rw-r--r-- | README.md | 6 | ||||
| -rw-r--r-- | tests/test_stash.py | 8 | ||||
| -rw-r--r-- | ymlstash/stash.py | 8 |
3 files changed, 14 insertions, 8 deletions
@@ -49,6 +49,12 @@ Load from file: user = stash.load("yuval") ``` +List all keys existing in stash: + +```python +keys = stash.list_keys() +``` + ## License [MIT](LICENSE) diff --git a/tests/test_stash.py b/tests/test_stash.py index 6a11db9..6b9e47f 100644 --- a/tests/test_stash.py +++ b/tests/test_stash.py @@ -23,11 +23,11 @@ def test_stash(): stash = YmlStash(User, "/tmp/") yuval = User(name="yuval", age=42) stash.save(yuval, "foo") - assert stash.list_all_keys() == ["foo"] + assert stash.list_keys() == ["foo"] obj = stash.load("foo") assert obj == yuval stash.drop() - assert stash.list_all_keys() == [] + assert stash.list_keys() == [] def test_key_field(): @@ -46,10 +46,10 @@ def test_key_field(): stash = YmlStash(Dog, "/tmp/") terra = Dog(name="terra") stash.save(terra) - assert stash.list_all_keys() == ["terra"] + assert stash.list_keys() == ["terra"] terra = Dog(name="terra") stash.save(terra, key="dupe") # override key - assert stash.list_all_keys() == ["terra", "dupe"] + assert stash.list_keys() == ["terra", "dupe"] stash.drop() diff --git a/ymlstash/stash.py b/ymlstash/stash.py index 55bf016..a0c585a 100644 --- a/ymlstash/stash.py +++ b/ymlstash/stash.py @@ -36,13 +36,13 @@ class YmlStash: with open(self._get_path(key), "w") as f: f.write(yaml.dump(asdict(obj))) - def _list_all_files(self): + def _list_files(self): return [f for f in os.listdir(self.path) if f.endswith(self.file_suffix)] - def list_all_keys(self): - return [f.replace(self.file_suffix, "") for f in self._list_all_files()] + def list_keys(self): + return [f.replace(self.file_suffix, "") for f in self._list_files()] def drop(self): - for f in self._list_all_files(): + for f in self._list_files(): if f.endswith(self.file_suffix): os.remove(self.path / f) |
