Skip to content

Commit

Permalink
Merge pull request #3 from Themis3000/context
Browse files Browse the repository at this point in the history
Support for context managers
  • Loading branch information
Themis3000 authored Jan 9, 2022
2 parents 94a7859 + 83228d4 commit ca52ce1
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 7 deletions.
9 changes: 7 additions & 2 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lazy_db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

__author__ = """Themi Megas"""
__email__ = '[email protected]'
__version__ = '1.0.1'
__version__ = '1.1.0'
6 changes: 6 additions & 0 deletions lazy_db/lazy_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 1.0.1
current_version = 1.1.0
commit = True
tag = False

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
30 changes: 28 additions & 2 deletions tests/test_lazy_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")

0 comments on commit ca52ce1

Please sign in to comment.