[英]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.