Skip to content

Commit

Permalink
Handle non string types in key_for function
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaifee committed Oct 22, 2017
1 parent fa18e43 commit 6964edf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 13 additions & 2 deletions limits/limits.py
Expand Up @@ -2,11 +2,22 @@
"""
from six import add_metaclass

try:
from functools import total_ordering
except ImportError: # pragma: no cover
from .backports.total_ordering import total_ordering # pragma: no cover

def safe_string(value):
"""
consistently converts a value to a string
:param value:
:return: str
"""
if isinstance(value, bytes):
return value.decode()
return str(value)

TIME_TYPES = dict(
day=(60 * 60 * 24, "day"),
month=(60 * 60 * 24 * 30, "month"),
Expand Down Expand Up @@ -71,8 +82,8 @@ def key_for(self, *identifiers):
each identifier appended with a '/' delimiter.
"""
remainder = "/".join(
identifiers +
(str(self.amount), str(self.multiples), self.granularity[1])
[safe_string(k) for k in identifiers] +
[safe_string(self.amount), safe_string(self.multiples), self.granularity[1]]
)
return "%s/%s" % (self.namespace, remainder)

Expand Down
1 change: 1 addition & 0 deletions limits/util.py
Expand Up @@ -6,6 +6,7 @@

from .limits import GRANULARITIES


SEPARATORS = re.compile(r"[,;|]{1}")
SINGLE_EXPR = re.compile(
r"\s*([0-9]+)\s*(/|\s*per\s*)\s*([0-9]+)*\s*(hour|minute|second|day|month|year)s?\s*",
Expand Down

0 comments on commit 6964edf

Please sign in to comment.