Skip to content

Pyupgrade + flynt + f-strings #1759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 30, 2021
Merged
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
6 changes: 3 additions & 3 deletions benchmarks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def run_benchmark(self):
group_values = [group['values'] for group in self.ARGUMENTS]
for value_set in itertools.product(*group_values):
pairs = list(zip(group_names, value_set))
arg_string = ', '.join(['%s=%s' % (p[0], p[1]) for p in pairs])
sys.stdout.write('Benchmark: %s... ' % arg_string)
arg_string = ', '.join(f'{p[0]}={p[1]}' for p in pairs)
sys.stdout.write(f'Benchmark: {arg_string}... ')
sys.stdout.flush()
kwargs = dict(pairs)
setup = functools.partial(self.setup, **kwargs)
run = functools.partial(self.run, **kwargs)
t = timeit.timeit(stmt=run, setup=setup, number=1000)
sys.stdout.write('%f\n' % t)
sys.stdout.write(f'{t:f}\n')
sys.stdout.flush()
23 changes: 10 additions & 13 deletions benchmarks/basic_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ def wrapper(*args, **kwargs):
count = kwargs['num']
else:
count = args[1]
print('{} - {} Requests'.format(func.__name__, count))
print('Duration = {}'.format(duration))
print('Rate = {}'.format(count/duration))
print(f'{func.__name__} - {count} Requests')
print(f'Duration = {duration}')
print(f'Rate = {count/duration}')
print()
return ret
return wrapper
Expand All @@ -62,10 +62,9 @@ def set_str(conn, num, pipeline_size, data_size):
if pipeline_size > 1:
conn = conn.pipeline()

format_str = '{:0<%d}' % data_size
set_data = format_str.format('a')
set_data = 'a'.ljust(data_size, '0')
for i in range(num):
conn.set('set_str:%d' % i, set_data)
conn.set(f'set_str:{i}', set_data)
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()

Expand All @@ -78,10 +77,9 @@ def set_int(conn, num, pipeline_size, data_size):
if pipeline_size > 1:
conn = conn.pipeline()

format_str = '{:0<%d}' % data_size
set_data = int(format_str.format('1'))
set_data = 10 ** (data_size - 1)
for i in range(num):
conn.set('set_int:%d' % i, set_data)
conn.set(f'set_int:{i}', set_data)
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()

Expand All @@ -95,7 +93,7 @@ def get_str(conn, num, pipeline_size, data_size):
conn = conn.pipeline()

for i in range(num):
conn.get('set_str:%d' % i)
conn.get(f'set_str:{i}')
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()

Expand All @@ -109,7 +107,7 @@ def get_int(conn, num, pipeline_size, data_size):
conn = conn.pipeline()

for i in range(num):
conn.get('set_int:%d' % i)
conn.get(f'set_int:{i}')
if pipeline_size > 1 and i % pipeline_size == 0:
conn.execute()

Expand All @@ -136,8 +134,7 @@ def lpush(conn, num, pipeline_size, data_size):
if pipeline_size > 1:
conn = conn.pipeline()

format_str = '{:0<%d}' % data_size
set_data = int(format_str.format('1'))
set_data = 10 ** (data_size - 1)
for i in range(num):
conn.lpush('lpush_key', set_data)
if pipeline_size > 1 and i % pipeline_size == 0:
Expand Down
11 changes: 4 additions & 7 deletions benchmarks/command_packer_benchmark.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import socket
from redis.connection import (Connection, SYM_STAR, SYM_DOLLAR, SYM_EMPTY,
SYM_CRLF)
from base import Benchmark
Expand All @@ -11,14 +10,13 @@ def send_packed_command(self, command, check_health=True):
self.connect()
try:
self._sock.sendall(command)
except socket.error as e:
except OSError as e:
self.disconnect()
if len(e.args) == 1:
_errno, errmsg = 'UNKNOWN', e.args[0]
else:
_errno, errmsg = e.args
raise ConnectionError("Error %s while writing to socket. %s." %
(_errno, errmsg))
raise ConnectionError(f"Error {_errno} while writing to socket. {errmsg}.")
except Exception:
self.disconnect()
raise
Expand All @@ -43,14 +41,13 @@ def send_packed_command(self, command, check_health=True):
command = [command]
for item in command:
self._sock.sendall(item)
except socket.error as e:
except OSError as e:
self.disconnect()
if len(e.args) == 1:
_errno, errmsg = 'UNKNOWN', e.args[0]
else:
_errno, errmsg = e.args
raise ConnectionError("Error %s while writing to socket. %s." %
(_errno, errmsg))
raise ConnectionError(f"Error {_errno} while writing to socket. {errmsg}.")
except Exception:
self.disconnect()
raise
Expand Down
1 change: 1 addition & 0 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
flake8>=3.9.2
flynt~=0.69.0
pytest==6.2.5
pytest-timeout==2.0.1
tox==3.24.4
Expand Down
18 changes: 9 additions & 9 deletions redis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ def parse_set_result(response, **options):
return response and str_if_bytes(response) == 'OK'


class Redis(RedisModuleCommands, CoreCommands, SentinelCommands, object):
class Redis(RedisModuleCommands, CoreCommands, SentinelCommands):
"""
Implementation of the Redis protocol.

Expand Down Expand Up @@ -918,7 +918,7 @@ def __init__(self, host='localhost', port=6379,
self.__class__.RESPONSE_CALLBACKS)

def __repr__(self):
return "%s<%s>" % (type(self).__name__, repr(self.connection_pool))
return f"{type(self).__name__}<{repr(self.connection_pool)}>"

def set_response_callback(self, command, callback):
"Set a custom Response Callback"
Expand Down Expand Up @@ -1141,7 +1141,7 @@ def __enter__(self):
# check that monitor returns 'OK', but don't return it to user
response = self.connection.read_response()
if not bool_ok(response):
raise RedisError('MONITOR failed: %s' % response)
raise RedisError(f'MONITOR failed: {response}')
return self

def __exit__(self, *args):
Expand Down Expand Up @@ -1517,12 +1517,10 @@ def run_in_thread(self, sleep_time=0, daemon=False,
exception_handler=None):
for channel, handler in self.channels.items():
if handler is None:
raise PubSubError("Channel: '%s' has no handler registered" %
channel)
raise PubSubError(f"Channel: '{channel}' has no handler registered")
for pattern, handler in self.patterns.items():
if handler is None:
raise PubSubError("Pattern: '%s' has no handler registered" %
pattern)
raise PubSubError(f"Pattern: '{pattern}' has no handler registered")

thread = PubSubWorkerThread(
self,
Expand Down Expand Up @@ -1807,8 +1805,10 @@ def raise_first_error(self, commands, response):

def annotate_exception(self, exception, number, command):
cmd = ' '.join(map(safe_str, command))
msg = 'Command # %d (%s) of pipeline caused error: %s' % (
number, cmd, exception.args[0])
msg = (
f'Command # {number} ({cmd}) of pipeline '
f'caused error: {exception.args[0]}'
)
exception.args = (msg,) + exception.args[1:]

def parse_response(self, connection, command_name, **options):
Expand Down
Loading