Skip to content

Commit

Permalink
PEP8 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
timsavage committed Mar 29, 2016
1 parent 8e6f655 commit 2f4dc33
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
16 changes: 11 additions & 5 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,15 +735,18 @@ def _error_message(self, exception):
(exception.args[0], self.path, exception.args[1])


FALSE_STRINGS = ('0', 'F', 'FALSE', 'N', 'NO')


def to_bool(value):
if value is None or value == '':
return None
if isinstance(value, basestring) and value.upper() in ('0', 'F', 'FALSE', 'N', 'NO'):
if isinstance(value, basestring) and value.upper() in FALSE_STRINGS:
return False
return bool(value)


URL_QUERY_PARAMETER_TYPES = {
URL_QUERY_ARGUMENT_PARSERS = {
'socket_timeout': float,
'socket_connect_timeout': float,
'socket_keepalive': to_bool,
Expand Down Expand Up @@ -811,11 +814,14 @@ def from_url(cls, url, db=None, decode_components=False, **kwargs):

for name, value in iteritems(parse_qs(qs)):
if value and len(value) > 0:
if name in URL_QUERY_PARAMETER_TYPES:
parser = URL_QUERY_ARGUMENT_PARSERS.get(name)
if parser:
try:
url_options[name] = URL_QUERY_PARAMETER_TYPES[name](value[0])
url_options[name] = parser(value[0])
except (TypeError, ValueError):
warnings.warn(UserWarning("Invalid value for `%s` in connection URL." % name))
warnings.warn(UserWarning(
"Invalid value for `%s` in connection URL." % name
))
else:
url_options[name] = value[0]

Expand Down
14 changes: 11 additions & 3 deletions tests/test_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ def test_db_in_querystring(self):

def test_extra_typed_querystring_options(self):
pool = redis.ConnectionPool.from_url(
'redis://localhost/2?socket_timeout=20&socket_connect_timeout=10&socket_keepalive=&retry_on_timeout=Yes')
'redis://localhost/2?socket_timeout=20&socket_connect_timeout=10'
'&socket_keepalive=&retry_on_timeout=Yes'
)

assert pool.connection_class == redis.Connection
assert pool.connection_kwargs == {
Expand Down Expand Up @@ -267,9 +269,15 @@ def test_boolean_parsing(self):
def test_invalid_extra_typed_querystring_options(self):
import warnings
with warnings.catch_warnings(record=True) as warning_log:
redis.ConnectionPool.from_url('redis://localhost/2?socket_timeout=_&socket_connect_timeout=abc')
redis.ConnectionPool.from_url(
'redis://localhost/2?socket_timeout=_&'
'socket_connect_timeout=abc'
)
# Compare the message values
assert [str(m.message) for m in sorted(warning_log, key=lambda l: str(l.message))] == [
assert [
str(m.message) for m in
sorted(warning_log, key=lambda l: str(l.message))
] == [
'Invalid value for `socket_connect_timeout` in connection URL.',
'Invalid value for `socket_timeout` in connection URL.',
]
Expand Down

0 comments on commit 2f4dc33

Please sign in to comment.