Skip to content

Commit

Permalink
Flesh out abstract base mixins (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
brainix authored Oct 3, 2016
1 parent 6667faa commit a0a6a81
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
19 changes: 18 additions & 1 deletion pottery/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ class _Clearable(metaclass=abc.ABCMeta):
def redis(self):
'Redis client.'

@abc.abstractproperty
def key(self):
'Redis key.'

def clear(self):
'Remove the elements in a Redis-backed container. O(n)'
self.redis.delete(self.key)
Expand All @@ -155,11 +159,16 @@ class _ContextManaged(metaclass=abc.ABCMeta):
def redis(self):
'Redis client.'

@abc.abstractmethod
def __del__(self):
...

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, traceback):
self.__del__()
self.__del__() # We can't do del self because we still need a
# referense to self for the next line...
self.redis.connection_pool.disconnect()


Expand All @@ -170,6 +179,14 @@ class Base(_Common, _ContextManaged, _Clearable, Pipelined):


class Iterable(metaclass=abc.ABCMeta):
@abc.abstractmethod
def _decode(value):
...

@abc.abstractproperty
def key(self):
'Redis key.'

@abc.abstractmethod
def _scan(self, key, *, cursor=0):
...
Expand Down
2 changes: 1 addition & 1 deletion pottery/dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@



class RedisDict(Iterable, Base, collections.abc.MutableMapping):
class RedisDict(Base, Iterable, collections.abc.MutableMapping):
'Redis-backed container compatible with Python dicts.'

def __init__(self, iterable=tuple(), *, redis=None, key=None, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion pottery/set.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@



class RedisSet(Iterable, Base, collections.abc.MutableSet):
class RedisSet(Base, Iterable, collections.abc.MutableSet):
'Redis-backed container compatible with Python sets.'

def __init__(self, iterable=tuple(), *, redis=None, key=None):
Expand Down

0 comments on commit a0a6a81

Please sign in to comment.