Reputation: 2149
So i have following rename statement that I want to execute:
"RENAME TABLE `my_table` TO `my_table_temp`, `my_new_table` TO `my_table`,
`my_table_temp` TO `my_new_table`"
I want to make a function using this query so I could apply it to different tables.
The case is that I'm looking for best readable way of formatting string.
Tool of choice seems to be .format
method and obviously I do not want to make something like this
"RENAME TABLE '{0}' TO '{1}', '{2}' TO '{3}',
'{4}' TO '{5}'".format('my_table','my_table_temp','my_new_table','my_table','my_table_temp','my_new_table')
But what I do want to make is something like this
.format('my_table','my_table_temp','my_new_table')
and somehow just indicate numbers which should be replaced with each string. Just to be clear - I do not want use 6 arguments for string formatting but just 3 and make each of them be repeated twice in string So I'm seeking an advice of how this should be implemented. Apologize if this question is silly and obvious but I just want to improve my code style to maximum level even in such details before it become too late and I find myself somewhere in gutter, writing awful crap using Python language
Upvotes: 0
Views: 1078
Reputation: 5061
In [42]: '{0}, {0}' .format('a')
Out[42]: 'a, a'
In [43]: '{0}, {0}' .format('a', 'b')
Out[43]: 'a, a'
In [44]: '{0}, {1}' .format('a', 'b')
Out[44]: 'a, b'
foramt string works like this.
so you only need to do:-
"RENAME TABLE '{0}' TO '{1}', '{2}' TO '{0}',\
'{1}' TO '{2}'".format('my_table','my_table_temp',\
'my_new_table','my_table')
>>>
RENAME TABLE 'my_table' TO 'my_table_temp', 'my_new_table' TO 'my_table', 'my_table_temp' TO 'my_new_table'
Upvotes: 3