From a2907fc4b9508c58e88569b2b2b7766ac9d34492 Mon Sep 17 00:00:00 2001 From: Themi Date: Sun, 9 Jan 2022 00:09:36 -0600 Subject: [PATCH 1/4] Added __enter__ and __exit__ methods and tests in order to support context managers --- lazy_db/lazy_db.py | 6 ++++++ tests/test_lazy_db.py | 30 ++++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lazy_db/lazy_db.py b/lazy_db/lazy_db.py index cb05f04..2167fd1 100644 --- a/lazy_db/lazy_db.py +++ b/lazy_db/lazy_db.py @@ -303,3 +303,9 @@ def delete(self, key: Union[str, int]): def close(self): """Closes the database""" self.f.close() + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.close() diff --git a/tests/test_lazy_db.py b/tests/test_lazy_db.py index 166ce28..d27a44f 100644 --- a/tests/test_lazy_db.py +++ b/tests/test_lazy_db.py @@ -8,7 +8,7 @@ from lazy_db import lazy_db -class TestLazy_db(unittest.TestCase): +class TestLazyDb(unittest.TestCase): """Tests for `lazy_db` package.""" def setUp(self): @@ -147,9 +147,35 @@ def test_write_int_list_restart(self): self.assertEqual([435, 4636, 123, 768, 2356, 436], self.db.read("test_str")) self.assertEqual([436, 2356, 35, 235, 6546, 4537], self.db.read("test_str2")) - def delete_last_value(self): + def test_delete_last_value(self): """Deletes the last value in a database and restarts""" self.db.write("test_str", "test") self.db.delete("test_str") self.restart() self.assertEqual(0, len(self.db.headers)) + + +class TestLazyDbSetupless(unittest.TestCase): + """Tests for `lazy_db` package that don't use to use setup or teardown methods.""" + + def test_context(self): + """Tests if the database can be opened and closed via the with statement.""" + with lazy_db.LazyDb("test_db.lazydb") as db: + db.write("test_str", "test val") + self.assertEqual("test val", db.read("test_str")) + self.assertFalse(db.f.closed, "file prematurely closed") + self.assertTrue(db.f.closed, "file not closed after exiting context") + os.remove("test_db.lazydb") + + def test_context_exception(self): + """Tests if the database can be opened and closed via the with statement cleanly with an exception""" + try: + with lazy_db.LazyDb("test_db.lazydb") as db: + db.write("test_str", "test val") + db.write("test_str", "test val") + self.fail("No exception was triggered after inserting 2 keys of the same name into the database") + except: + pass + + self.assertTrue(db.f.closed) + os.remove("test_db.lazydb") From 6c2ba1df60e63c5806f08296441bec7c97e08a17 Mon Sep 17 00:00:00 2001 From: Themi Date: Sun, 9 Jan 2022 00:10:18 -0600 Subject: [PATCH 2/4] =?UTF-8?q?Bump=20version:=201.0.1=20=E2=86=92=201.1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lazy_db/__init__.py | 2 +- setup.cfg | 2 +- setup.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lazy_db/__init__.py b/lazy_db/__init__.py index 24f3ac2..5fb32a9 100644 --- a/lazy_db/__init__.py +++ b/lazy_db/__init__.py @@ -3,4 +3,4 @@ __author__ = """Themi Megas""" __email__ = 'tcm4760@gmail.com' -__version__ = '1.0.1' +__version__ = '1.1.0' diff --git a/setup.cfg b/setup.cfg index 9c796d9..c6b29f7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 1.0.1 +current_version = 1.1.0 commit = True tag = False diff --git a/setup.py b/setup.py index 5f9d6ac..26e273e 100644 --- a/setup.py +++ b/setup.py @@ -37,6 +37,6 @@ test_suite='tests', tests_require=test_requirements, url='https://github.com/Themis3000/lazy_db', - version='1.0.1', + version='1.1.0', zip_safe=False, ) From b625ed7e9f0c930a97a9aeb8b059bfa161b56d60 Mon Sep 17 00:00:00 2001 From: Themi Date: Sun, 9 Jan 2022 00:14:57 -0600 Subject: [PATCH 3/4] Added update notes to HISTORY.rst --- HISTORY.rst | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 36e1984..125199c 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -20,7 +20,12 @@ History * Readme updated * Docstrings added -1.0.1 (2022-1-2) ----------------- +1.0.1 (2022-01-02) +------------------ * Updated python require version to >=3.8 due to usage of the walrus operator + +1.1.0 (2022-01-09) +------------------ + +* Added support for context managers, the with statement can now be used to open and cleanly exit databases From 83228d441467e85732ef350863bf50d0e703b2ef Mon Sep 17 00:00:00 2001 From: Themi Date: Sun, 9 Jan 2022 00:26:15 -0600 Subject: [PATCH 4/4] Modified README.md to suggest using a with statement when using the database --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index ab55002..1580549 100644 --- a/README.md +++ b/README.md @@ -15,10 +15,16 @@ Example usage: ```python from lazy_db import LazyDb +# Simple example usage db = LazyDb("test.lazy") db.write("test_value", "value") print(db.read("test_value")) # prints "value" db.close() + +# Or use a with statement to insure the database file is closed cleanly and avoid having to call db.close() on your own +with LazyDb("test2.lazy") as db: + db.write("test_value", "value") + print(db.read("test_value")) ``` ## How it works