python - How to easy pass a single tuple to the startswith function from the list of tuples? -
i want realize search , replace strings functionality in python. wrote following code, main problem convert list tuple. realized in 2 loops, can me in easier way pass tuple startswith
function? (list in rcsv_list
variable)
rman_config = (''' rman configuration parameters database db_unique_name test are: configure retention policy redundancy 1; # default configure backup optimization off; # default configure default device type disk; # default configure controlfile autobackup off; # default configure device type disk parallelism 1 backup type backupset; # default ''') rcsv_list = [('retention policy', 'to recovery window of 5 days'), ('controlfile autobackup', 'on'), ('device type', 'disk parallelism 4 backup type backupset')] # unpack list of tuples single list rcsv_tmp_list = [] l in rcsv_list: t in l: rcsv_tmp_list.append(t) # convert list tuple rcsv_tuple = tuple(rcsv_tmp_list) = 0 line in rman_config.splitlines(): if line.startswith(rcsv_tuple, 10): line = 'configure ' + rcsv_tuple[i] + ' ' + rcsv_tuple[i+1] + ';' += 2 print(line) else: print(line)
output:
rman configuration parameters database db_unique_name test are: configure retention policy recovery window of 5 days; configure backup optimization off; # default configure default device type disk; # default configure controlfile autobackup on; configure device type disk parallelism 4 backup type backupset;
the main idea of above code pass startswith
function tuple search in line in rman_config
string. purpose, created rcsv_tuple
variable tuple type. code works fine, @ end have 2 additional questions:
it possible simplify above code without unpack , covert variables? in other words - how pass tuple directly
startswith
function list of tuples?is there other method search , replace strings in case?
ps. rcsv_list
variable list of tuple because -> cursor.fetchall()
you can replace loop , construction of tuple with:
rcsv_tuple = sum(rcsv_list, ())
also can directly iterate result of query instead of querying, split, etc:
cursor.execute(sql) row in cursor: # ...
finally might eliminate else clause printing independently of condition.
Comments
Post a Comment