Skip to content

Commit

Permalink
Adodbapi prefer f-string > format > printf-style autofixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Apr 10, 2024
1 parent cd72712 commit 27a182c
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 137 deletions.
61 changes: 15 additions & 46 deletions adodbapi/adodbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ def format_parameters(ADOparameters, show_value=False):
try:
if show_value:
desc = [
'Name: %s, Dir.: %s, Type: %s, Size: %s, Value: "%s", Precision: %s, NumericScale: %s'
% (
'Name: {}, Dir.: {}, Type: {}, Size: {}, Value: "{}", Precision: {}, NumericScale: {}'.format(
p.Name,
adc.directions[p.Direction],
adc.adTypeNames.get(p.Type, str(p.Type) + " (unknown type)"),
Expand All @@ -138,8 +137,7 @@ def format_parameters(ADOparameters, show_value=False):
]
else:
desc = [
"Name: %s, Dir.: %s, Type: %s, Size: %s, Precision: %s, NumericScale: %s"
% (
"Name: {}, Dir.: {}, Type: {}, Size: {}, Precision: {}, NumericScale: {}".format(
p.Name,
adc.directions[p.Direction],
adc.adTypeNames.get(p.Type, str(p.Type) + " (unknown type)"),
Expand Down Expand Up @@ -256,7 +254,7 @@ def connect(self, kwargs, connection_maker=make_COM_connecter):
self.mode = kwargs.get("mode", adc.adModeUnknown)
self.kwargs = kwargs
if verbose:
print('%s attempting: "%s"' % (version, self.connection_string))
print(f'{version} attempting: "{self.connection_string}"')
self.connector = connection_maker()
self.connector.ConnectionTimeout = self.timeout
self.connector.ConnectionString = self.connection_string
Expand Down Expand Up @@ -415,8 +413,7 @@ def __setattr__(self, name, value):
if value not in api.accepted_paramstyles:
self._raiseConnectionError(
api.NotSupportedError,
'paramstyle="%s" not in:%s'
% (value, repr(api.accepted_paramstyles)),
f'paramstyle="{value}" not in:{repr(api.accepted_paramstyles)}',
)
elif name == "variantConversions":
value = copy.copy(
Expand Down Expand Up @@ -465,7 +462,7 @@ def printADOerrors(self):
print("ADO Errors:(%i)" % j)
for e in self.connector.Errors:
print("Description: %s" % e.Description)
print("Error: %s %s " % (e.Number, adc.adoErrors.get(e.Number, "unknown")))
print("Error: {} {} ".format(e.Number, adc.adoErrors.get(e.Number, "unknown")))
if e.Number == adc.ado_error_TIMEOUT:
print(
"Timeout Error: Try using adodbpi.connect(constr,timeout=Nseconds)"
Expand Down Expand Up @@ -564,8 +561,7 @@ def __init__(self, connection):
connection._i_am_here(self)
if verbose:
print(
"%s New cursor at %X on conn %X"
% (version, id(self), id(self.connection))
f"{version} New cursor at {id(self):X} on conn {id(self.connection):X}"
)

def __iter__(self): # [2.1 Zamarev]
Expand Down Expand Up @@ -672,8 +668,7 @@ def format_description(self, d):
if isinstance(d, int):
d = self.description[d]
desc = (
"Name= %s, Type= %s, DispSize= %s, IntSize= %s, Precision= %s, Scale= %s NullOK=%s"
% (
"Name= {}, Type= {}, DispSize= {}, IntSize= {}, Precision= {}, Scale= {} NullOK={}".format(
d[0],
adc.adTypeNames.get(d[1], str(d[1]) + " (unknown type)"),
d[2],
Expand Down Expand Up @@ -750,10 +745,7 @@ def _execute_command(self):
_message = ""
if hasattr(e, "args"):
_message += str(e.args) + "\n"
_message += "Command:\n%s\nParameters:\n%s" % (
self.commandText,
format_parameters(self.cmd.Parameters, True),
)
_message += f"Command:\n{self.commandText}\nParameters:\n{format_parameters(self.cmd.Parameters, True)}"
klass = self.connection._suggest_error_class()
self._raiseCursorError(klass, _message)
try:
Expand Down Expand Up @@ -783,9 +775,8 @@ def get_returned_parameters(self):
for p in tuple(self.cmd.Parameters):
if verbose > 2:
print(
'Returned=Name: %s, Dir.: %s, Type: %s, Size: %s, Value: "%s",'
" Precision: %s, NumericScale: %s"
% (
'Returned=Name: {}, Dir.: {}, Type: {}, Size: {}, Value: "{}",'
" Precision: {}, NumericScale: {}".format(
p.Name,
adc.directions[p.Direction],
adc.adTypeNames.get(p.Type, str(p.Type) + " (unknown type)"),
Expand Down Expand Up @@ -880,13 +871,7 @@ def _buildADOparameterList(self, parameters, sproc=False):
)
except Exception as e:
_message = (
"Error Converting Parameter %s: %s, %s <- %s\n"
% (
p.Name,
adc.ado_type_name(p.Type),
p.Value,
repr(parameters[pm_name]),
)
f"Error Converting Parameter {p.Name}: {adc.ado_type_name(p.Type)}, {p.Value} <- {repr(parameters[pm_name])}\n"
)
self._raiseCursorError(
api.DataError, _message + "->" + repr(e.args)
Expand All @@ -903,13 +888,7 @@ def _buildADOparameterList(self, parameters, sproc=False):
_configure_parameter(p, value, p.Type, parameters_known)
except Exception as e:
_message = (
"Error Converting Parameter %s: %s, %s <- %s\n"
% (
p.Name,
adc.ado_type_name(p.Type),
p.Value,
repr(value),
)
f"Error Converting Parameter {p.Name}: {adc.ado_type_name(p.Type)}, {p.Value} <- {repr(value)}\n"
)
self._raiseCursorError(
api.DataError, _message + "->" + repr(e.args)
Expand All @@ -929,12 +908,7 @@ def _buildADOparameterList(self, parameters, sproc=False):
try:
self.cmd.Parameters.Append(p)
except Exception as e:
_message = "Error Building Parameter %s: %s, %s <- %s\n" % (
p.Name,
adc.ado_type_name(p.Type),
p.Value,
repr(elem),
)
_message = f"Error Building Parameter {p.Name}: {adc.ado_type_name(p.Type)}, {p.Value} <- {repr(elem)}\n"
self._raiseCursorError(
api.DataError, _message + "->" + repr(e.args)
)
Expand All @@ -955,12 +929,7 @@ def _buildADOparameterList(self, parameters, sproc=False):
try:
self.cmd.Parameters.Append(p)
except Exception as e:
_message = "Error Building Parameter %s: %s, %s <- %s\n" % (
p.Name,
adc.ado_type_name(p.Type),
p.Value,
repr(elem),
)
_message = f"Error Building Parameter {p.Name}: {adc.ado_type_name(p.Type)}, {p.Value} <- {repr(elem)}\n"
self._raiseCursorError(
api.DataError, _message + "->" + repr(e.args)
)
Expand Down Expand Up @@ -1150,7 +1119,7 @@ def _last_query(self): # let the programmer see what query we actually used
if self.parameters is None:
ret = self.commandText
else:
ret = "%s,parameters=%s" % (self.commandText, repr(self.parameters))
ret = f"{self.commandText},parameters={repr(self.parameters)}"
except:
ret = None
return ret
Expand Down
6 changes: 3 additions & 3 deletions adodbapi/apibase.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def pyTypeToADOType(d):
return adc.adBigInt
if isinstance(d, numbers.Real):
return adc.adDouble
raise DataError('cannot convert "%s" (type=%s) to ADO' % (repr(d), tp))
raise DataError(f'cannot convert "{repr(d)}" (type={tp}) to ADO')


# # # # # # # # # # # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -511,7 +511,7 @@ def __getattr__(self, name): # used for row.columnName type of value access
try:
return self._getValue(self.rows.columnNames[name.lower()])
except KeyError:
raise AttributeError('Unknown column name "{}"'.format(name))
raise AttributeError(f'Unknown column name "{name}"')

def _getValue(self, key): # key must be an integer
if (
Expand Down Expand Up @@ -546,7 +546,7 @@ def __getitem__(self, key): # used for row[key] type of value access
except (KeyError, TypeError):
er, st, tr = sys.exc_info()
raise er(
'No such key as "%s" in %s' % (repr(key), self.__repr__())
f'No such key as "{repr(key)}" in {self.__repr__()}'
).with_traceback(tr)

def __iter__(self):
Expand Down
4 changes: 2 additions & 2 deletions adodbapi/examples/xls_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
except IndexError:
filename = "xx.xls"

constr = "Provider=%s;Data Source=%s;%s" % (driver, filename, extended)
constr = f"Provider={driver};Data Source={filename};{extended}"

conn = adodbapi.connect(constr)

Expand All @@ -30,7 +30,7 @@
# use ADO feature to get the name of the first worksheet
sheet = conn.get_table_names()[0]

print("Shreadsheet=%s Worksheet=%s" % (filename, sheet))
print(f"Shreadsheet={filename} Worksheet={sheet}")
print("------------------------------------------------------------")
crsr = conn.cursor()
sql = "SELECT * from [%s]" % sheet
Expand Down
4 changes: 2 additions & 2 deletions adodbapi/examples/xls_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
filename = "xx.xls" # file will be created if it does not exist
extended = 'Extended Properties="Excel 8.0;Readonly=False;"'

constr = "Provider=%s;Data Source=%s;%s" % (driver, filename, extended)
constr = f"Provider={driver};Data Source={filename};{extended}"

conn = adodbapi.connect(constr)
with conn: # will auto commit if no errors
Expand All @@ -38,4 +38,4 @@
sql, ["John Jones", "Pvt", 987654321, 140.0, datetime.date(1921, 7, 4)]
) # another row of data
conn.close()
print("Created spreadsheet=%s worksheet=%s" % (filename, "SheetOne"))
print("Created spreadsheet={} worksheet={}".format(filename, "SheetOne"))
4 changes: 2 additions & 2 deletions adodbapi/process_connect_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def macro_call(macro_name, args, kwargs):
not "user" in kwargs or not kwargs["user"]
): # missing, blank, or Null username
return new_key, "Integrated Security=SSPI"
return new_key, "User ID=%(user)s; Password=%(password)s" % kwargs
return new_key, "User ID={user}; Password={password}".format(**kwargs)

elif (
macro_name == "find_temp_test_path"
Expand All @@ -70,7 +70,7 @@ def macro_call(macro_name, args, kwargs):

raise ValueError("Unknown connect string macro=%s" % macro_name)
except:
raise ValueError("Error in macro processing %s %s" % (macro_name, repr(args)))
raise ValueError(f"Error in macro processing {macro_name} {repr(args)}")


def process(
Expand Down
Loading

0 comments on commit 27a182c

Please sign in to comment.