diff --git a/README.md b/README.md index d0200dc..e6b01aa 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ It doesn't do anything crazy. It just takes json serializable data and stores it You can control when and where to read/write, and that's it. -No bloat, only 3 public methods. +No bloat, only 5 public methods. ## Usage @@ -43,4 +43,4 @@ kv.get('my_key') # returns True. kv.delete('my_key') ~~~ -***Warning:*** **Be sure to manually flush when destroying the kv instance (such as by exiting your program) to avoid losing data, as flushes only happen when .put has been called.** \ No newline at end of file +***Warning:*** **Be sure to keep flush_on_exit set to true or manually flush when destroying the kv instance (such as by exiting your program) to avoid losing data.** \ No newline at end of file diff --git a/deadsimplekv/__init__.py b/deadsimplekv/__init__.py index 36d1584..54a2dd3 100644 --- a/deadsimplekv/__init__.py +++ b/deadsimplekv/__init__.py @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import json, time, math, os +import json, time, math, os, atexit def _is_serializable(data): ''' @@ -28,12 +28,12 @@ def _is_serializable(data): return False class DeadSimpleKV: - def __init__(self, file_path=None, refresh_seconds=0, flush_seconds=0): + def __init__(self, file_path=None, refresh_seconds=0, flush_seconds=0, flush_on_exit=True): ''' Accepts a file path and refresh. If file_path is not set, no data will be written to disk. Refresh is an integer specifying how many seconds should pass before data is re-read from disk. - If refresh_seconds or flush_seconds are set to None they will not be done automatically + If refresh_seconds or flush_seconds are set to None they will not be done automatically (except where flush_on_exit applies) ''' temp_time = DeadSimpleKV._get_epoch() # Initialization time self.file_path = file_path # The file path where we write our data in JSON format @@ -48,6 +48,9 @@ class DeadSimpleKV: self.refresh() except TypeError: pass + + if flush_on_exit: + atexit.register(self.flush) def get(self, key): ''' diff --git a/docs/index.html b/docs/index.html index 41c4d7a..fd44ae7 100644 --- a/docs/index.html +++ b/docs/index.html @@ -3,14 +3,14 @@ - + deadsimplekv API documentation - + @@ -57,6 +57,9 @@ If not, see https://www.gnu.org/licenses import json, time, math, os def _is_serializable(data): + ''' + Test if something is able to be in JSON format + ''' try: json.dumps(data) return True @@ -86,6 +89,9 @@ class DeadSimpleKV: pass def get(self, key): + ''' + Accepts key, which must be json serializable + ''' self._do_auto_refresh() try: return self._data[key] @@ -93,8 +99,9 @@ class DeadSimpleKV: return None def put(self, key, value): - '''Value setter. Will automatically flush if auto_flush is True and file_path is not None - Will return value error if either key or value are not JSON serializable + ''' + Value setter. Will automatically flush if auto_flush is True and file_path is not None + Will return value error if either key or value are not JSON serializable ''' self._data[key] = value # Set the key @@ -107,12 +114,16 @@ class DeadSimpleKV: return (key, value) def delete(self, key): - '''Deletes value. Will automatically flush if auto_flush is True and file_path is not None''' + ''' + Deletes value. Will automatically flush if auto_flush is True and file_path is not None + ''' del self._data[key] self._do_auto_flush() def refresh(self): - '''Refresh data and then mark time read. Can be manually called''' + ''' + Refresh data and then mark time read. Can be manually called + ''' try: self._data = json.loads(DeadSimpleKV._read_in(self.file_path)) except FileNotFoundError: @@ -120,7 +131,9 @@ class DeadSimpleKV: self._last_refresh = DeadSimpleKV._get_epoch() def flush(self): - '''Write out then mark time flushed. Can be manually called''' + ''' + Write out then mark time flushed. Can be manually called + ''' DeadSimpleKV._write_out(self.file_path, json.dumps(self._data)) self._last_flush = DeadSimpleKV._get_epoch() @@ -131,8 +144,8 @@ class DeadSimpleKV: self.flush() def _do_auto_refresh(self): + # Automatically flush if it is enabled and time to do so if self.refresh_seconds is not None and self.file_path is not None: - # refresh if automatic and time to do so if DeadSimpleKV._get_epoch() - self._last_refresh >= self.refresh_seconds: self.refresh() @@ -192,6 +205,9 @@ class DeadSimpleKV: pass def get(self, key): + ''' + Accepts key, which must be json serializable + ''' self._do_auto_refresh() try: return self._data[key] @@ -199,8 +215,9 @@ class DeadSimpleKV: return None def put(self, key, value): - '''Value setter. Will automatically flush if auto_flush is True and file_path is not None - Will return value error if either key or value are not JSON serializable + ''' + Value setter. Will automatically flush if auto_flush is True and file_path is not None + Will return value error if either key or value are not JSON serializable ''' self._data[key] = value # Set the key @@ -213,12 +230,16 @@ class DeadSimpleKV: return (key, value) def delete(self, key): - '''Deletes value. Will automatically flush if auto_flush is True and file_path is not None''' + ''' + Deletes value. Will automatically flush if auto_flush is True and file_path is not None + ''' del self._data[key] self._do_auto_flush() def refresh(self): - '''Refresh data and then mark time read. Can be manually called''' + ''' + Refresh data and then mark time read. Can be manually called + ''' try: self._data = json.loads(DeadSimpleKV._read_in(self.file_path)) except FileNotFoundError: @@ -226,7 +247,9 @@ class DeadSimpleKV: self._last_refresh = DeadSimpleKV._get_epoch() def flush(self): - '''Write out then mark time flushed. Can be manually called''' + ''' + Write out then mark time flushed. Can be manually called + ''' DeadSimpleKV._write_out(self.file_path, json.dumps(self._data)) self._last_flush = DeadSimpleKV._get_epoch() @@ -237,8 +260,8 @@ class DeadSimpleKV: self.flush() def _do_auto_refresh(self): + # Automatically flush if it is enabled and time to do so if self.refresh_seconds is not None and self.file_path is not None: - # refresh if automatic and time to do so if DeadSimpleKV._get_epoch() - self._last_refresh >= self.refresh_seconds: self.refresh() @@ -300,7 +323,9 @@ If refresh_seconds or flush_seconds are set to None they will not be done automa
Source code
def delete(self, key):
-    '''Deletes value. Will automatically flush if auto_flush is True and file_path is not None'''
+    '''
+        Deletes value. Will automatically flush if auto_flush is True and file_path is not None
+    '''
     del self._data[key]
     self._do_auto_flush()
@@ -313,7 +338,9 @@ If refresh_seconds or flush_seconds are set to None they will not be done automa
Source code
def flush(self):
-    '''Write out then mark time flushed. Can be manually called'''
+    '''
+        Write out then mark time flushed. Can be manually called
+    '''
     DeadSimpleKV._write_out(self.file_path, json.dumps(self._data))
     self._last_flush = DeadSimpleKV._get_epoch()
@@ -322,10 +349,13 @@ If refresh_seconds or flush_seconds are set to None they will not be done automa def get(self, key)
-
+

Accepts key, which must be json serializable

Source code
def get(self, key):
+    '''
+        Accepts key, which must be json serializable
+    '''
     self._do_auto_refresh()
     try:
         return self._data[key]
@@ -342,8 +372,9 @@ Will return value error if either key or value are not JSON serializable

Source code
def put(self, key, value):
-    '''Value setter. Will automatically flush if auto_flush is True and file_path is not None
-    Will return value error if either key or value are not JSON serializable
+    '''
+        Value setter. Will automatically flush if auto_flush is True and file_path is not None
+        Will return value error if either key or value are not JSON serializable
     '''
     self._data[key] = value # Set the key
 
@@ -364,7 +395,9 @@ Will return value error if either key or value are not JSON serializable

Source code
def refresh(self):
-    '''Refresh data and then mark time read. Can be manually called'''
+    '''
+        Refresh data and then mark time read. Can be manually called
+    '''
     try:
         self._data = json.loads(DeadSimpleKV._read_in(self.file_path))
     except FileNotFoundError:
@@ -402,7 +435,7 @@ Will return value error if either key or value are not JSON serializable

diff --git a/setup.py b/setup.py index 969684e..40b5a4f 100644 --- a/setup.py +++ b/setup.py @@ -1,10 +1,10 @@ from distutils.core import setup -setup(name='deadsimplekv-beardog', - version='0.0.1', +setup(name='deadsimplekv', + version='0.1.1', description='Very simple key-value store for Python', author='Kevin Froman', - author_email='beardog@firemail.cc', + author_email='beardog@mailbox.org', url='https://github.com/beardog108/deadsimplekv', packages=['deadsimplekv'], ) diff --git a/tests/test_basic.py b/tests/test_basic.py index 624a01c..dd4725a 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -23,6 +23,7 @@ def get_test_id(): return str(uuid.uuid4()) + '.dat' class TestInit(unittest.TestCase): + def test_init(self): kv = deadsimplekv.DeadSimpleKV(get_test_id())