added get_raw_json and fixed formatting, switched to setuptools
This commit is contained in:
parent
a33a56e6ad
commit
f6d3bac205
@ -2,6 +2,12 @@
|
|||||||
|
|
||||||
This project uses Semantic Versioning
|
This project uses Semantic Versioning
|
||||||
|
|
||||||
|
## 0.3.0
|
||||||
|
|
||||||
|
Added get_raw_json
|
||||||
|
Improved code formatting
|
||||||
|
Switched to setuptools
|
||||||
|
|
||||||
## 0.2.0
|
## 0.2.0
|
||||||
|
|
||||||
Added automatic creation of path for file writing
|
Added automatic creation of path for file writing
|
||||||
|
@ -15,18 +15,22 @@
|
|||||||
You should have received a copy of the GNU General Public License
|
You should have received a copy of the GNU General Public License
|
||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
import json, time, math, os, atexit
|
import json
|
||||||
|
import time
|
||||||
|
import math
|
||||||
|
import os
|
||||||
|
import atexit
|
||||||
|
|
||||||
|
|
||||||
def _is_serializable(data):
|
def _is_serializable(data):
|
||||||
"""
|
"""Test if something is able to be in JSON format"""
|
||||||
Test if something is able to be in JSON format
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
json.dumps(data)
|
json.dumps(data)
|
||||||
return True
|
return True
|
||||||
except TypeError:
|
except TypeError:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
class DeadSimpleKV:
|
class DeadSimpleKV:
|
||||||
def __init__(self, file_path=None, refresh_seconds=0, flush_seconds=0, flush_on_exit=True):
|
def __init__(self, file_path=None, refresh_seconds=0, flush_seconds=0, flush_on_exit=True):
|
||||||
"""
|
"""
|
||||||
@ -35,17 +39,18 @@ class DeadSimpleKV:
|
|||||||
Refresh is an integer specifying how many seconds should pass before data is re-read from 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 (except where flush_on_exit applies)
|
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
|
temp_time = DeadSimpleKV._get_epoch() # Initialization time
|
||||||
self.file_path = file_path # The file path where we write our data in JSON format
|
self.file_path = file_path # The file path where we write our data in JSON format
|
||||||
self.refresh_seconds = refresh_seconds # How often to refresh the
|
self.refresh_seconds = refresh_seconds # How often to refresh the
|
||||||
self.flush_seconds = flush_seconds # How often to flush out when setting
|
self.flush_seconds = flush_seconds # How often to flush out when setting
|
||||||
self._data = {} # The key store (in memory)
|
self._data = {} # The key store (in memory)
|
||||||
self._last_refresh = temp_time # time last refreshed from disk
|
self._last_refresh = temp_time # time last refreshed from disk
|
||||||
self._last_flush = temp_time # time last flushed to disk
|
self._last_flush = temp_time # time last flushed to disk
|
||||||
|
|
||||||
if not file_path is None:
|
if not file_path is None:
|
||||||
abs_path = os.path.dirname(os.path.abspath(file_path))
|
abs_path = os.path.dirname(os.path.abspath(file_path))
|
||||||
if not os.path.exists(abs_path): os.makedirs(abs_path)
|
if not os.path.exists(abs_path):
|
||||||
|
os.makedirs(abs_path)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if os.path.exists(file_path):
|
if os.path.exists(file_path):
|
||||||
@ -57,21 +62,22 @@ class DeadSimpleKV:
|
|||||||
atexit.register(self.flush)
|
atexit.register(self.flush)
|
||||||
|
|
||||||
def get(self, key):
|
def get(self, key):
|
||||||
"""
|
"""Accepts key, which must be json serializable."""
|
||||||
Accepts key, which must be json serializable
|
|
||||||
"""
|
|
||||||
self._do_auto_refresh()
|
self._do_auto_refresh()
|
||||||
try:
|
try:
|
||||||
return self._data[key]
|
return self._data[key]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def get_raw_json(self) -> str:
|
||||||
|
"""Return raw json string of the data"""
|
||||||
|
return json.dumps(self._data)
|
||||||
|
|
||||||
def put(self, key, value):
|
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
|
self._data[key] = value # Set the key
|
||||||
Will return value error if either key or value are not JSON serializable
|
|
||||||
"""
|
|
||||||
self._data[key] = value # Set the key
|
|
||||||
|
|
||||||
# Check if key and value can be put in JSON
|
# Check if key and value can be put in JSON
|
||||||
if not _is_serializable(key):
|
if not _is_serializable(key):
|
||||||
@ -82,16 +88,12 @@ class DeadSimpleKV:
|
|||||||
return (key, value)
|
return (key, value)
|
||||||
|
|
||||||
def delete(self, key):
|
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]
|
del self._data[key]
|
||||||
self._do_auto_flush()
|
self._do_auto_flush()
|
||||||
|
|
||||||
def refresh(self):
|
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:
|
try:
|
||||||
self._data = json.loads(DeadSimpleKV._read_in(self.file_path))
|
self._data = json.loads(DeadSimpleKV._read_in(self.file_path))
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
@ -99,9 +101,7 @@ class DeadSimpleKV:
|
|||||||
self._last_refresh = DeadSimpleKV._get_epoch()
|
self._last_refresh = DeadSimpleKV._get_epoch()
|
||||||
|
|
||||||
def flush(self):
|
def flush(self):
|
||||||
"""
|
"""Write out to file then mark time flushed. Can be manually called."""
|
||||||
Write out to file then mark time flushed. Can be manually called
|
|
||||||
"""
|
|
||||||
DeadSimpleKV._write_out(self.file_path, json.dumps(self._data))
|
DeadSimpleKV._write_out(self.file_path, json.dumps(self._data))
|
||||||
self._last_flush = DeadSimpleKV._get_epoch()
|
self._last_flush = DeadSimpleKV._get_epoch()
|
||||||
|
|
||||||
|
12
setup.py
12
setup.py
@ -1,10 +1,16 @@
|
|||||||
from distutils.core import setup
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
setup(name='deadsimplekv',
|
setup(name='deadsimplekv',
|
||||||
version='0.2.0',
|
version='0.3.0',
|
||||||
description='Very simple key-value store for Python',
|
description='Very simple key-value store for Python',
|
||||||
author='Kevin Froman',
|
author='Kevin Froman',
|
||||||
author_email='beardog@mailbox.org',
|
author_email='beardog@mailbox.org',
|
||||||
url='https://github.com/beardog108/deadsimplekv',
|
url='https://github.com/beardog108/deadsimplekv',
|
||||||
packages=['deadsimplekv'],
|
packages=find_packages(exclude=['contrib', 'docs', 'tests']),
|
||||||
|
install_requires=[],
|
||||||
|
classifiers=[
|
||||||
|
"Programming Language :: Python :: 3",
|
||||||
|
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
|
||||||
|
"Operating System :: OS Independent",
|
||||||
|
],
|
||||||
)
|
)
|
||||||
|
@ -37,6 +37,16 @@ class TestInit(unittest.TestCase):
|
|||||||
kv = deadsimplekv.DeadSimpleKV(test_id)
|
kv = deadsimplekv.DeadSimpleKV(test_id)
|
||||||
self.assertEqual(kv.get('my_key'), 'test')
|
self.assertEqual(kv.get('my_key'), 'test')
|
||||||
|
|
||||||
|
def test_get_raw_json(self):
|
||||||
|
test_id = get_test_id()
|
||||||
|
|
||||||
|
with open(test_id , 'w') as test_file:
|
||||||
|
test_file.write('{"my_key": "test"}')
|
||||||
|
|
||||||
|
# assert we can get written data
|
||||||
|
kv = deadsimplekv.DeadSimpleKV(test_id)
|
||||||
|
self.assertEqual(kv.get_raw_json(), '{"my_key": "test"}')
|
||||||
|
|
||||||
def test_delete(self):
|
def test_delete(self):
|
||||||
# test key deletion
|
# test key deletion
|
||||||
test_id = get_test_id()
|
test_id = get_test_id()
|
||||||
|
Loading…
Reference in New Issue
Block a user