Skip to content

Commit 12ff78e

Browse files
cont f-strings
1 parent 89632d7 commit 12ff78e

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

smodels/tools/databaseClient.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def send_shutdown ( self ):
4343
self.send ( "shutdown", amount_expected = 0 )
4444

4545
def saveStats ( self ):
46-
self.pprint ( "client stats after %d queries" % self.nqueries )
46+
self.pprint ( f"client stats after {self.nqueries} queries" )
4747
self.pprint ( "=================================" )
4848
self.pprint ( f"number of results in cache: {len(self.cache)}" )
4949
maxhits=[]
@@ -124,8 +124,7 @@ def send ( self, message, amount_expected=32 ):
124124
except (ConnectionRefusedError,ConnectionResetError,BrokenPipeError,ConnectionAbortedError) as e:
125125
dt = self.getWaitingTime()
126126
self.ntries += 1
127-
self.log ( 'could not connect to %s. trying again in %d seconds' % \
128-
( self.nameAndPort(), dt ) )
127+
self.log ( f'could not connect to {self.nameAndPort()}. trying again in {dt} seconds' )
129128
time.sleep ( dt )
130129
self.pprint ( f"could not connect in send, after trying {self.ntries} times. aborting" )
131130
raise SModelSError ( f"Could not connect to database in send, tried {self.ntries} times" )
@@ -158,23 +157,26 @@ def findServerStatus ( self ):
158157
def log ( self, *args ):
159158
if type(self.verbose)==str or self.verbose > 35:
160159
self.setDefaults()
161-
print ( "[databaseClient%d] %s" % ( self.clientid, " ".join(map(str,args)) ) )
160+
s_args = ' '.join(map(str,args))
161+
print ( f"[databaseClient{self.clientid}] {s_args}" )
162162
with open ( self.logfile, "at" ) as f:
163-
f.write ( "[databaseClient%d-%s] %s\n" % \
164-
( self.clientid, time.strftime("%H:%M:%S"), " ".join(map(str,args)) ) )
163+
asctime = time.strftime("%H:%M:%S")
164+
f.write ( f"[databaseClient{self.clientid}-{asctime}] {s_args}\n" )
165165
f.close()
166166

167167
def pprint ( self, *args ):
168168
if type(self.verbose)==str or self.verbose > 25:
169169
self.setDefaults()
170-
print ( f"[databaseClient{self.clientid}] {' '.join(map(str,args))}" )
170+
s_args = ' '.join(map(str,args))
171+
print ( f"[databaseClient{self.clientid}] {s_args}" )
172+
print ( f"[databaseClient{self.clientid}] {s_args}" )
171173
with open ( self.logfile, "at" ) as f:
172-
f.write ( "[databaseClient%d-%s] %s\n" % \
173-
( self.clientid, time.strftime("%H:%M:%S"), " ".join(map(str,args)) ) )
174+
asctime = time.strftime("%H:%M:%S")
175+
f.write ( f"[databaseClient{self.clientid}-{asctime}] {s_args}\n" )
174176
f.close()
175177

176178
def nameAndPort ( self ):
177-
return "%s:%d" % ( self.servername, self.port )
179+
return f"{self.servername}:{self.port}"
178180

179181
def initialize( self ):
180182
if hasattr ( self, "sock" ):
@@ -195,19 +197,18 @@ def initialize( self ):
195197
except (socket.timeout,OSError,ConnectionRefusedError,ConnectionResetError,BrokenPipeError,ConnectionAbortedError) as e:
196198
dt = self.getWaitingTime()
197199
self.ntries += 1
198-
self.log ( 'could not connect to %s after %d times. trying again in %d seconds' % \
199-
( self.nameAndPort(), self.ntries, dt ) )
200+
self.log ( f'could not connect to {self.nameAndPort()} after {self.ntries} times. trying again in {dt} seconds' )
200201
time.sleep ( dt )
201202
self.pprint ( f'could not connect to database in initialize, after trying {self.ntries} times. aborting' )
202-
raise SModelSError ( "Could not connect to database in initialize, tried %d times" % self.ntries )
203+
raise SModelSError ( f"Could not connect to database in initialize, tried {self.ntries} times" )
203204

204205

205206
def stresstest( args ):
206207
""" this is one process in the stress test """
207208
verbosity, servername, port = "error", args[0], args[1]
208209
nr = args[2]
209210
client = DatabaseClient ( servername, port, verbose = verbosity,
210-
logfile="@@rundir@@/dbclient%d.log" % nr,
211+
logfile=f"@@rundir@@/dbclient{nr}.log",
211212
clientid = nr )
212213
mmother = random.uniform ( 200, 900 )
213214
mlsp = random.uniform ( 0, mmother )
@@ -217,9 +218,8 @@ def stresstest( args ):
217218
mlsp = random.uniform ( 0, mmother )
218219
msg = "obs:ATLAS-SUSY-2017-01:SRHad-Low:TChiWH:[[%.2f,%.2f],[%.2f,%.2f]]" % \
219220
( mmother, mlsp, mmother, mlsp )
220-
# print ( "client #%d" % pid )
221221
client.query ( msg )
222-
print ( "finished %d" % nr )
222+
print ( f"[stresstest] finished {nr}" )
223223

224224
if __name__ == "__main__":
225225
import argparse

smodels/tools/databaseServer.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def logServerStats ( self ):
8080
""" log our stats upon exit """
8181
self.pprint ( "server stats" )
8282
self.pprint ( "============" )
83-
self.pprint ( "total number of lookups: %d" % self.nlookups )
83+
self.pprint ( f"total number of lookups: {self.nlookups}" )
8484

8585
def shutdown ( self, fromwhere = "unknown" ):
8686
self.pprint ( f"Received shutdown request from {fromwhere}" )
@@ -173,8 +173,7 @@ def listen ( self ):
173173
if data:
174174
self.parseData ( str(data) )
175175
else:
176-
self.log ( 'no more data from %s:%s' % \
177-
( self.client_address[0], self.client_address[1] ) )
176+
self.log ( f'no more data from {self.client_address[0]}:{self.client_address[1]}' )
178177
break
179178
finally:
180179
# Clean up the connection
@@ -184,16 +183,16 @@ def log ( self, *args ):
184183
if self.verbose > 35:
185184
print ( "[databaseServer]", " ".join(map(str,args)) )
186185
with open ( self.logfile, "at" ) as f:
187-
f.write ( "[databaseServer-%s] %s\n" % \
188-
( time.strftime("%H:%M:%S"), " ".join(map(str,args)) ) )
186+
asctime = time.strftime("%H:%M:%S")
187+
f.write(f"[databaseServer-{asctime}] {' '.join(map(str,args))}\n")
189188
f.close()
190189

191190
def pprint ( self, *args ):
192191
if self.verbose > 25:
193192
print ( "[databaseServer]", " ".join(map(str,args)) )
194193
with open ( self.logfile, "at" ) as f:
195-
f.write ( "[databaseServer-%s] %s\n" % \
196-
( time.strftime("%H:%M:%S"), " ".join(map(str,args)) ) )
194+
asctime = time.strftime("%H:%M:%S")
195+
f.write(f"[databaseServer-{asctime}] {' '.join(map(str,args))}\n")
197196
f.close()
198197

199198
def initialize( self ):

0 commit comments

Comments
 (0)