[英]How to use the startswith function to produce output like in the diff tool?
請幫我解決(我覺得有趣)算法問題。 我今天花了一整天時間(迷路)來解決這個問題。 這是我的代碼:
rman_config = ('''CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
''')
rman_new_parameters = [('RETENTION POLICY', 'TO RECOVERY WINDOW OF 2 DAYS'),
('CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE', 'DISK TO controlfile_%F'),
('CONTROLFILE AUTOBACKUP', 'ON'),
('DEVICE TYPE', 'DISK PARALLELISM 4 BACKUP TYPE TO BACKUPSET')]
tuple_to_search = tuple([i[0] for i in rman_new_parameters])
for line in rman_config.splitlines():
if line.startswith(tuple_to_search, 10):
print('- ' + line)
print('+ CONFIGURE ' + '[parameter] ' + '[new value]' + ';')
else:
print(' ' + line)
哪里:
rman_config
- 具有默認配置的變量 rman_new_parameters
- 以隨機順序覆蓋默認值的新參數,其中:
CONTROLFILE AUTOBACKUP
) ON
) 到目前為止我有這個輸出:
- CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
+ CONFIGURE [parameter] [new value];
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
- CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
+ CONFIGURE [parameter] [new value];
- CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
+ CONFIGURE [parameter] [new value];
- CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
+ CONFIGURE [parameter] [new value];
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
但我想要這個(就像在Diff
):
- CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
+ CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 2 DAYS;
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
- CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
+ CONFIGURE CONTROLFILE AUTOBACKUP ON;
- CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
+ CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO cf_%F;
- CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
+ CONFIGURE DEVICE TYPE DISK PARALLELISM 4 BACKUP TYPE TO BACKUPSET;
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
startswith
函數很棒,但它沒有告訴我哪個元組被識別(只返回True或False)。 我上面代碼的問題是如何映射以下行:
if line.startswith(tuple_to_search, 10):
如果它對rman_new_parameters
列表是真的。
我還沒有弄清楚如何解決它? 我會非常感激任何幫助。
for line in rman_config.splitlines():
for params in rman_new_parameters:
if line.startswith(params[0], 10):
print('- {}'.format(line))
print('+ CONFIGURE {} {}'.format(*params))
break
else:
print(' {}'.format(line))
基本上,我沒有將元組傳遞給.startswith
,只是在一個循環中逐個測試。
另外, for
+ else
。
我不知道你要做什么。 但無論如何我寫代碼。 看看這個答案是否符合您的問題。 主要區別在於你的if-else
塊變為try-except
塊。 並且重點是index
函數,它返回tuple_to_search
第一次出現參數的tuple_to_search
。 我假設rman_new_parameters
所有參數值都是從rman_config
更改的。
rman_config = ('''CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
''')
rman_new_parameters = [('RETENTION POLICY', 'TO RECOVERY WINDOW OF 2 DAYS'),
('CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE', 'DISK TO controlfile_%F'),
('CONTROLFILE AUTOBACKUP', 'ON'),
('DEVICE TYPE', 'DISK PARALLELISM 4 BACKUP TYPE TO BACKUPSET')]
tuple_to_search = [i[0] for i in rman_new_parameters]
for line in rman_config.splitlines():
try:
index = [line.startswith(tup, 10) for tup in tuple_to_search].index(True)
print('- ' + line)
print('+ CONFIGURE ' + " ".join(rman_new_parameters[index]) + ';')
#Add these if you don't need to check parameter once matched.
#del rman_new_parameters[index]
#del tuple_to_search[index]
except:
print(' ' + line)
輸出:
- CONFIGURE RETENTION POLICY TO REDUNDANCY 1; # default
+ CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 2 DAYS;
CONFIGURE BACKUP OPTIMIZATION OFF; # default
CONFIGURE DEFAULT DEVICE TYPE TO DISK; # default
- CONFIGURE CONTROLFILE AUTOBACKUP OFF; # default
+ CONFIGURE CONTROLFILE AUTOBACKUP ON;
- CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'; # default
+ CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO controlfile_%F;
- CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO BACKUPSET; # default
+ CONFIGURE DEVICE TYPE DISK PARALLELISM 4 BACKUP TYPE TO BACKUPSET;
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
PS
我的輸出與您想要的輸出略有不同(2行不同)。 我不知道為什么。 所以請確保它是您想要的輸出。
你:
...
+ CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO cf_%F;
...
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
礦:
...
+ CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO controlfile_%F;
...
CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default
^
no space here
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.