Skip to content

Commit

Permalink
Merge pull request #11 from Attumm/feature_add_list
Browse files Browse the repository at this point in the history
Added first storing of dict, list experimental
  • Loading branch information
Attumm authored Dec 4, 2021
2 parents 0db64cb + 4985dc2 commit 7fd7631
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ As of writing, redis-dict supports the following types.
* Boolean
* None

#### Other Types not fully supported
Experimental support for the following types.
List, Dictionary supported provided with json serialization.
If your list or Dictionary can be serializate by json this feature will work.

Although is not the best solution, it could work for many usecases. So use at your discretion.
If there is need for other referenced types open issue on github.
* List
* Dictionary

#### Expire
Redis has the great feature of expiring keys. This feature is supported.
1. You can set the default expiration when creating a redis-dict instance.
Expand Down
22 changes: 16 additions & 6 deletions redis_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@

@python_2_unicode_compatible
class RedisDict:
trans = {
'json': json.loads,
transform = {
type('').__name__: str,
type(1).__name__: int,
type(0.1).__name__: float,
type(True).__name__: lambda x: x == "True",
type(None).__name__: lambda x: None,
type(None).__name__: lambda x: None,

"list": json.loads,
"dict": json.loads,
}

pre_transform = {
"list": json.dumps,
"dict": json.dumps,
}

def __init__(self, **kwargs):
Expand All @@ -32,8 +40,10 @@ def __init__(self, **kwargs):
def _format_key(self, key):
return '{}:{}'.format(self.namespace, str(key))

def _store(self, key, value, set_type=None):
store_type = set_type if set_type is not None else type(value).__name__
def _store(self, key, value):
store_type = type(value).__name__

value = self.pre_transform.get(store_type, lambda x: x)(value)
store_value = '{}:{}'.format(store_type, value)
self.redis.set(self._format_key(key), store_value, ex=self.expire)

Expand All @@ -42,11 +52,11 @@ def _load(self, key):
if result is None:
return False, None
t, value = result.split(':', 1)
return True, self.trans.get(t, lambda x: x)(value)
return True, self.transform.get(t, lambda x: x)(value)

def _transform(self, result):
t, value = result.split(':', 1)
return self.trans.get(t, lambda x: x)(value)
return self.transform.get(t, lambda x: x)(value)

def add_type(self, k, v):
self.trans[k] = v
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
long_description=long_description,
long_description_content_type='text/markdown',

version='1.5.2',
version='1.6.0',
py_modules=['redis_dict'],
install_requires=['redis', 'future'],
license='MIT',
Expand Down
14 changes: 12 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ def clear_test_namespace(cls):
def setUp(self):
self.clear_test_namespace()

@unittest.skip
def test_python3_all_methods_from_dictionary_are_implemented(self):
import sys
if sys.version_info[0] == 3:
redis_dic = self.create_redis_dict()
dic = dict()

self.assertEqual(len(set(dir({})) - set(dir(RedisDict))), 0)
# reversed is currently not supported
self.assertEqual(set(dir({})) - set(dir(RedisDict)), set())
self.assertEqual(len(set(dir(dic)) - set(dir(redis_dic))), 0)

def test_input_items(self):
Expand Down Expand Up @@ -82,7 +84,15 @@ def test_supported_types(self):
redis_dic = self.create_redis_dict()
dic = dict()

input_values = (("int", 1), ("float", 0.9), ("str", "im a string"), ("bool", True), ("None", None))
input_values = [
("int", 1),
("float", 0.9),
("str", "im a string"),
("bool", True),
("None", None),
("list", [1, 2, 3]),
("dict", {"foo": "bar"}),
]

for key, value in input_values:
redis_dic[key] = value
Expand Down

0 comments on commit 7fd7631

Please sign in to comment.