Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions redis_ds/redis_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
not a Redis hashmap. If you prefer an interface to a hashmap, the
``redis_hash_dict`` file does just that.
"""
import UserDict
try:
import UserDict
_DictMixin = UserDict.DictMixin
except ImportError:
from collections import UserDict
from collections import MutableMapping as DictMixin
_DictMixin = DictMixin

import redis_ds.redis_config as redis_config
from redis_ds.serialization import PassThroughSerializer, PickleSerializer, JSONSerializer


class RedisDict(UserDict.DictMixin, PassThroughSerializer):
class RedisDict(_DictMixin, PassThroughSerializer):
"Dictionary interface to Redis database."
def __init__(self, redis_client=redis_config.CLIENT):
"""
Expand Down Expand Up @@ -51,6 +58,10 @@ def get(self, key, default=None):
"Retrieve a key's value from the database falling back to a default."
return self.__getitem__(key) or default

def __iter__(self):
"Return iterator over dictionary keys"
return iter(self.keys())


class PickleRedisDict(RedisDict, PickleSerializer):
"Serialize redis dictionary values via pickle."
Expand Down
17 changes: 13 additions & 4 deletions redis_ds/redis_hash_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
"""
import redis_ds.redis_config as redis_config
from redis_ds.serialization import PassThroughSerializer, PickleSerializer, JSONSerializer
import UserDict


class RedisHashDict(UserDict.DictMixin, PassThroughSerializer):
try:
import UserDict
_DictMixin = UserDict.DictMixin
except ImportError:
from collections import UserDict
from collections import MutableMapping as DictMixin
_DictMixin = DictMixin

class RedisHashDict(_DictMixin, PassThroughSerializer):
"A dictionary interface to Redis hashmaps."
def __init__(self, hash_key, redis_client=redis_config.CLIENT):
"Initialize the redis hashmap dictionary interface."
Expand Down Expand Up @@ -43,6 +48,10 @@ def get(self, key, default=None):
"Retrieve a key's value or a default value if the key does not exist."
return self.__getitem__(key) or default

def __iter__(self):
"Return iterator over dictionary keys"
return iter(self.keys())


class PickleRedisHashDict(RedisHashDict, PickleSerializer):
"Serialize hashmap values using pickle."
Expand Down
6 changes: 4 additions & 2 deletions redis_ds/serialization.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"Mixins for serializing objects."
import json
import cPickle as pickle

try:
import cPickle as pickle
except ImportError:
import _pickle as pickle

class PassThroughSerializer(object):
"Don't serialize."
Expand Down