added get_raw_json and fixed formatting, switched to setuptools

This commit is contained in:
Kevin Froman 2020-03-28 00:03:16 -05:00
parent a33a56e6ad
commit f6d3bac205
4 changed files with 72 additions and 50 deletions

View File

@ -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

View File

@ -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):
""" """
@ -45,7 +49,8 @@ class DeadSimpleKV:
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,18 +62,19 @@ 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.
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 Will return value error if either key or value are not JSON serializable
""" """
self._data[key] = value # Set the key self._data[key] = value # Set the 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()

View File

@ -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",
],
) )

View File

@ -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()