Skip to content

Commit

Permalink
feat(database): support datetime.timedelta as an expire object
Browse files Browse the repository at this point in the history
  • Loading branch information
hearot committed Jun 11, 2020
1 parent d5debbf commit 6b3453f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
- Do no more raise `TypeError` while formatting commits ([56f1e533c12bdf75e63714d125319bb72da7d916](https://github.com/hearot/pyrubrum/commit/56f1e533c12bdf75e63714d125319bb72da7d916))
- Fix `commit-message` for Dependabot ([a13cfeae848d83339a902373ddcb0287fc306d87](https://github.com/hearot/pyrubrum/commit/a13cfeae848d83339a902373ddcb0287fc306d87))
- Hide the sha-1 hash of the current commit ([ab6478e610ef828744d5c6e54a1d54fff92372e8](https://github.com/hearot/pyrubrum/commit/ab6478e610ef828744d5c6e54a1d54fff92372e8))
- Make databases consistent with the documentation
- Make databases consistent with the documentation ([d5debbfa52eb16e012ad8e022a5b3d8a06051a59](https://github.com/hearot/pyrubrum/commit/d5debbfa52eb16e012ad8e022a5b3d8a06051a59))

### New features

- Automatically detect the commit URL for changelog ([a5be95bf1f782bff658552ee4b54552f611409c4](https://github.com/hearot/pyrubrum/commit/a5be95bf1f782bff658552ee4b54552f611409c4))
- Include the release dates inside changelog ([d4397f396c64eeec6549cd90c64cef6359017dd5](https://github.com/hearot/pyrubrum/commit/d4397f396c64eeec6549cd90c64cef6359017dd5))
- Integrate Dependabot ([2bb0a43b0f7f8266c7d0544209194c2b26b878a4](https://github.com/hearot/pyrubrum/commit/2bb0a43b0f7f8266c7d0544209194c2b26b878a4))
- Make parameters optional for `on_callback` and `on_message` ([562f323da2d19913405a5efcfe531e37fa54baf5](https://github.com/hearot/pyrubrum/commit/562f323da2d19913405a5efcfe531e37fa54baf5))
- Support `datetime.timedelta` as a expire object

### Testing changes

Expand Down
10 changes: 7 additions & 3 deletions pyrubrum/database/base_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@

from abc import ABC
from abc import abstractmethod
from datetime import timedelta
from typing import Optional
from typing import Union

Expire = Optional[Union[int, timedelta]]


class BaseDatabase(ABC):
Expand Down Expand Up @@ -52,7 +56,7 @@ def get(self, key: str) -> Optional[str]:
raise NotImplementedError

@abstractmethod
def set(self, key: str, value: str, expire: Optional[int] = None):
def set(self, key: str, value: str, expire: Expire = None):
"""This abstract method is intended to be implemented in order to assign a
value to a certain key inside the database. It may even be marked with
an expire as to avoid having too much unused data stored inside the
Expand All @@ -61,8 +65,8 @@ def set(self, key: str, value: str, expire: Optional[int] = None):
Args:
key (str): The key you are adding or updating the value of.
value (str): The value which is being assigned to the key.
expire (Optional[int]): The expire in seconds. Defaults to
``None``.
expire (Expire): The expire in seconds or as a `timedelta` object.
Defaults to ``None``.
Raises:
ExpireError: If an error occured while setting the expire for the
Expand Down
3 changes: 2 additions & 1 deletion pyrubrum/database/dict_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
from typing import Optional

from .base_database import BaseDatabase
from .base_database import Expire
from .errors import DeleteError


class DictDatabase(dict, BaseDatabase):
def get(self, key: str) -> Optional[str]:
return dict.get(self, key)

def set(self, key: str, value: str, expire: Optional[int] = None):
def set(self, key: str, value: str, expire: Expire = None):
self.update({key: value})

def delete(self, key: str):
Expand Down
9 changes: 4 additions & 5 deletions pyrubrum/database/redis_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrubrum. If not, see <http://www.gnu.org/licenses/>.

from datetime import timedelta
from typing import Optional
from typing import Union

from .base_database import BaseDatabase
from .base_database import Expire
from .errors import DeleteError
from .errors import ExpireError
from .errors import SetError
Expand All @@ -33,14 +32,14 @@

class RedisDatabase(BaseDatabase):
encoding = "utf-8"
default_expire: Optional[Union[int, timedelta]] = 86400
default_expire: Expire = 86400
server: "redis.Redis"

def __init__(
self,
server: "redis.Redis",
encoding="utf-8",
default_expire: Optional[Union[int, timedelta]] = 86400,
default_expire: Expire = 86400,
):
self.default_expire = default_expire
self.encoding = encoding
Expand All @@ -50,7 +49,7 @@ def get(self, key: str) -> Optional[str]:
content = self.server.get(key)
return content.decode(self.encoding) if content else None

def set(self, key: str, value: str, expire: int = None):
def set(self, key: str, value: str, expire: Expire = None):
if not self.server.set(key, value):
raise SetError

Expand Down

0 comments on commit 6b3453f

Please sign in to comment.