@@ -740,16 +740,17 @@ def determine_slot(self, *args):
740
740
raise RedisClusterException (
741
741
"No way to dispatch this command to Redis Cluster. "
742
742
"Missing key.\n You can execute the command by specifying "
743
- "target nodes.\n Command: {}" . format ( args )
743
+ f "target nodes.\n Command: { args } "
744
744
)
745
745
746
746
if len (keys ) > 1 :
747
747
# multi-key command, we need to make sure all keys are mapped to
748
748
# the same slot
749
749
slots = {self .keyslot (key ) for key in keys }
750
750
if len (slots ) != 1 :
751
- raise RedisClusterException ("{} - all keys must map to the "
752
- "same key slot" .format (args [0 ]))
751
+ raise RedisClusterException (
752
+ f"{ args [0 ]} - all keys must map to the same key slot"
753
+ )
753
754
return slots .pop ()
754
755
else :
755
756
# single key command
@@ -774,12 +775,12 @@ def _parse_target_nodes(self, target_nodes):
774
775
# rc.cluster_save_config(rc.get_primaries())
775
776
nodes = target_nodes .values ()
776
777
else :
777
- raise TypeError ("target_nodes type can be one of the "
778
- "followings: node_flag (PRIMARIES, "
779
- " REPLICAS, RANDOM, ALL_NODES),"
780
- "ClusterNode, list<ClusterNode>, or "
781
- "dict<any, ClusterNode>. The passed type is {0}" .
782
- format ( type ( target_nodes )) )
778
+ raise TypeError (
779
+ "target_nodes type can be one of the following: "
780
+ "node_flag (PRIMARIES, REPLICAS, RANDOM, ALL_NODES),"
781
+ "ClusterNode, list<ClusterNode>, or dict<any, ClusterNode>. "
782
+ f" The passed type is { type ( target_nodes ) } "
783
+ )
783
784
return nodes
784
785
785
786
def execute_command (self , * args , ** kwargs ):
@@ -866,9 +867,10 @@ def _execute_command(self, target_node, *args, **kwargs):
866
867
command in READ_COMMANDS )
867
868
moved = False
868
869
869
- log .debug ("Executing command {0} on target node: {1} {2}" .
870
- format (command , target_node .server_type ,
871
- target_node .name ))
870
+ log .debug (
871
+ f"Executing command { command } on target node: "
872
+ f"{ target_node .server_type } { target_node .name } "
873
+ )
872
874
redis_node = self .get_redis_connection (target_node )
873
875
connection = get_connection (redis_node , * args , ** kwargs )
874
876
if asking :
@@ -1005,13 +1007,13 @@ def __init__(self, host, port, server_type=None, redis_connection=None):
1005
1007
self .redis_connection = redis_connection
1006
1008
1007
1009
def __repr__ (self ):
1008
- return '[host={},port={},' \
1009
- 'name={},server_type={},redis_connection={}]' \
1010
- . format ( self .host ,
1011
- self .port ,
1012
- self .name ,
1013
- self .server_type ,
1014
- self . redis_connection )
1010
+ return (
1011
+ f'[host= { self . host } ,'
1012
+ f'port= { self .port } ,'
1013
+ f'name= { self .name } ,'
1014
+ f'server_type= { self .server_type } ,'
1015
+ f'redis_connection= { self .redis_connection } ]'
1016
+ )
1015
1017
1016
1018
def __eq__ (self , obj ):
1017
1019
return isinstance (obj , ClusterNode ) and obj .name == self .name
@@ -1133,9 +1135,8 @@ def get_node_from_slot(self, slot, read_from_replicas=False,
1133
1135
if self .slots_cache .get (slot ) is None or \
1134
1136
len (self .slots_cache [slot ]) == 0 :
1135
1137
raise SlotNotCoveredError (
1136
- 'Slot "{}" not covered by the cluster. '
1137
- '"require_full_coverage={}"' .format (
1138
- slot , self ._require_full_coverage )
1138
+ f'Slot "{ slot } " not covered by the cluster. '
1139
+ f'"require_full_coverage={ self ._require_full_coverage } "'
1139
1140
)
1140
1141
1141
1142
if read_from_replicas is True :
@@ -1194,7 +1195,7 @@ def node_require_full_coverage(node):
1194
1195
except Exception as e :
1195
1196
raise RedisClusterException (
1196
1197
'ERROR sending "config get cluster-require-full-coverage"'
1197
- ' command to redis server: {}, {}' . format ( node .name , e )
1198
+ f ' command to redis server: { node .name } , { e } '
1198
1199
)
1199
1200
1200
1201
# at least one node should have cluster-require-full-coverage yes
@@ -1267,7 +1268,7 @@ def initialize(self):
1267
1268
msg = e .__str__
1268
1269
log .exception ('An exception occurred while trying to'
1269
1270
' initialize the cluster using the seed node'
1270
- ' {}:\n {}' . format ( startup_node . name , msg ) )
1271
+ f ' { startup_node . name } :\n { msg } ' )
1271
1272
continue
1272
1273
except ResponseError as e :
1273
1274
log .exception (
@@ -1281,15 +1282,13 @@ def initialize(self):
1281
1282
else :
1282
1283
raise RedisClusterException (
1283
1284
'ERROR sending "cluster slots" command to redis '
1284
- 'server: {}. error: {}' .format (
1285
- startup_node , message )
1285
+ f'server: { startup_node } . error: { message } '
1286
1286
)
1287
1287
except Exception as e :
1288
1288
message = e .__str__ ()
1289
1289
raise RedisClusterException (
1290
1290
'ERROR sending "cluster slots" command to redis '
1291
- 'server: {}. error: {}' .format (
1292
- startup_node , message )
1291
+ f'server: { startup_node } . error: { message } '
1293
1292
)
1294
1293
1295
1294
# CLUSTER SLOTS command results in the following output:
@@ -1340,17 +1339,16 @@ def initialize(self):
1340
1339
else :
1341
1340
# Validate that 2 nodes want to use the same slot cache
1342
1341
# setup
1343
- if tmp_slots [i ][0 ].name != target_node .name :
1342
+ tmp_slot = tmp_slots [i ][0 ]
1343
+ if tmp_slot .name != target_node .name :
1344
1344
disagreements .append (
1345
- '{} vs {} on slot: {}' .format (
1346
- tmp_slots [i ][0 ].name , target_node .name , i )
1345
+ f'{ tmp_slot .name } vs { target_node .name } on slot: { i } '
1347
1346
)
1348
1347
1349
1348
if len (disagreements ) > 5 :
1350
1349
raise RedisClusterException (
1351
- 'startup_nodes could not agree on a valid'
1352
- ' slots cache: {}' .format (
1353
- ", " .join (disagreements ))
1350
+ f'startup_nodes could not agree on a valid '
1351
+ f'slots cache: { ", " .join (disagreements )} '
1354
1352
)
1355
1353
1356
1354
if not startup_nodes_reachable :
@@ -1368,9 +1366,8 @@ def initialize(self):
1368
1366
# Despite the requirement that the slots be covered, there
1369
1367
# isn't a full coverage
1370
1368
raise RedisClusterException (
1371
- 'All slots are not covered after query all startup_nodes.'
1372
- ' {} of {} covered...' .format (
1373
- len (self .slots_cache ), REDIS_CLUSTER_HASH_SLOTS )
1369
+ f'All slots are not covered after query all startup_nodes. '
1370
+ f'{ len (self .slots_cache )} of { REDIS_CLUSTER_HASH_SLOTS } covered...'
1374
1371
)
1375
1372
elif not fully_covered and not self ._require_full_coverage :
1376
1373
# The user set require_full_coverage to False.
@@ -1387,8 +1384,7 @@ def initialize(self):
1387
1384
'cluster-require-full-coverage configuration to no on '
1388
1385
'all of the cluster nodes if you wish the cluster to '
1389
1386
'be able to serve without being fully covered.'
1390
- ' {} of {} covered...' .format (
1391
- len (self .slots_cache ), REDIS_CLUSTER_HASH_SLOTS )
1387
+ f'{ len (self .slots_cache )} of { REDIS_CLUSTER_HASH_SLOTS } covered...'
1392
1388
)
1393
1389
1394
1390
# Set the tmp variables to the real variables
@@ -1642,8 +1638,10 @@ def annotate_exception(self, exception, number, command):
1642
1638
Provides extra context to the exception prior to it being handled
1643
1639
"""
1644
1640
cmd = ' ' .join (map (safe_str , command ))
1645
- msg = 'Command # %d (%s) of pipeline caused error: %s' % (
1646
- number , cmd , exception .args [0 ])
1641
+ msg = (
1642
+ f'Command # { number } ({ cmd } ) of pipeline '
1643
+ f'caused error: { exception .args [0 ]} '
1644
+ )
1647
1645
exception .args = (msg ,) + exception .args [1 :]
1648
1646
1649
1647
def execute (self , raise_on_error = True ):
@@ -1832,12 +1830,12 @@ def _send_cluster_commands(self, stack,
1832
1830
# If a lot of commands have failed, we'll be setting the
1833
1831
# flag to rebuild the slots table from scratch.
1834
1832
# So MOVED errors should correct themselves fairly quickly.
1835
- msg = 'An exception occurred during pipeline execution. ' \
1836
- 'args: {0}, error: {1} {2}' .\
1837
- format ( attempt [- 1 ].args ,
1838
- type (attempt [- 1 ].result ).__name__ ,
1839
- str (attempt [- 1 ].result ))
1840
- log . exception ( msg )
1833
+ log . exception (
1834
+ f'An exception occurred during pipeline execution. '
1835
+ f'args: { attempt [- 1 ].args } , '
1836
+ f'error: { type (attempt [- 1 ].result ).__name__ } '
1837
+ f' { str (attempt [- 1 ].result )} '
1838
+ )
1841
1839
self .reinitialize_counter += 1
1842
1840
if self ._should_reinitialized ():
1843
1841
self .nodes_manager .initialize ()
@@ -1929,8 +1927,8 @@ def block_pipeline_command(func):
1929
1927
1930
1928
def inner (* args , ** kwargs ):
1931
1929
raise RedisClusterException (
1932
- "ERROR: Calling pipelined function {} is blocked when "
1933
- "running redis in cluster mode..." . format ( func . __name__ ) )
1930
+ f "ERROR: Calling pipelined function { func . __name__ } is blocked when "
1931
+ f "running redis in cluster mode..." )
1934
1932
1935
1933
return inner
1936
1934
0 commit comments