Formatting a regular string which could be a f-string
222
223 for table_name, table in MappedBase.metadata.tables.items():
224 if len(table.primary_key.columns) == 0 and table_name not in MappedBase.classes:
225 logger.warning('Table "{}" does not have primary key and will be ignored'.format(table_name))226
227 connections[connection_id] = {
228 'engine': engine,
Formatting a regular string which could be a f-string
215 metadata = MetaData(schema=schema, bind=connection)
216 logger.info('Getting schema for "{}"...'.format(log_address))
217 reflect(metadata, engine, only=only)
218 logger.info('Connected to "{}"...'.format(log_address))219
220 MappedBase = automap_base(metadata=metadata)
221 reload_mapped_base(MappedBase)
Formatting a regular string which could be a f-string
213 type_code_to_sql_type = fetch_type_code_to_sql_type(session)
214
215 metadata = MetaData(schema=schema, bind=connection)
216 logger.info('Getting schema for "{}"...'.format(log_address))217 reflect(metadata, engine, only=only)
218 logger.info('Connected to "{}"...'.format(log_address))
219
Formatting a regular string which could be a f-string
209 logger.info('Connecting to database "{}"...'.format(log_address))
210
211 with session.connection() as connection:
212 logger.info('Getting db types for "{}"...'.format(log_address))213 type_code_to_sql_type = fetch_type_code_to_sql_type(session)
214
215 metadata = MetaData(schema=schema, bind=connection)
Formatting a regular string which could be a f-string
206 if tunnel:
207 log_address += ' (via {}@{}:{})'.format(conf.get('ssh_user'), conf.get('ssh_host'), conf.get('ssh_port'))
208
209 logger.info('Connecting to database "{}"...'.format(log_address))210
211 with session.connection() as connection:
212 logger.info('Getting db types for "{}"...'.format(log_address))
Formatting a regular string which could be a f-string
204 if log_address:
205 log_address = log_address.replace(password_token, '********')
206 if tunnel:
207 log_address += ' (via {}@{}:{})'.format(conf.get('ssh_user'), conf.get('ssh_host'), conf.get('ssh_port'))208
209 logger.info('Connecting to database "{}"...'.format(log_address))
210
Formatting a regular string which could be a f-string
49
50 try:
51 base64.b64decode(conf.get('password'))
52 url.append('?credentials_base64={}'.format(conf.get('password'))) 53
54 if conf.get('extra'):
55 url.append('&')
Description
f-strings are the fastest way to format strings as compared to the following methods:
- using format specifiers
%
- using
format()
- using
str.join
- using
+
operator to concatinate string - using
Template.substitute
Bad practice
Some less preferred ways to format strings are the following:
from string import Template
menu = ('eggs', 'spam', 42.4)
old_order = "%s and %s: %.2f ¤" % menu # [consider-using-f-string]
beginner_order = menu[0] + " and " + menu[1] + ": " + str(menu[2]) + " ¤"
joined_order = " and ".join(menu[:2])
format_order = "{} and {}: {:0.2f} ¤".format(menu[0], menu[1], menu[2])
named_format_order = "{eggs} and {spam}: {price:0.2f} ¤".format(eggs=menu[0], spam=menu[1], price=menu[2])
template_order = Template('$eggs and $spam: $price ¤').substitute(eggs=menu[0], spam=menu[1], price=menu[2])
Recommended
Consider using f-strings as shown below:
menu = ('eggs', 'spam', 42.4)
f_string_order = f"{menu[0]} and {menu[1]}: {menu[2]:0.2f} ¤"