请帮我解决(我认为很有趣)算法问题。我今天花了一整天来解决这个问题。这是我的代码:
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函数很好,但它不告诉我识别哪个元组(只返回真假)。我使用上面的代码的问题是如何映射下一行:
if line.startswith(tuple_to_search, 10):如果它是真到rman_new_parameters 列表.
我不知道怎么解决这个问题?我会非常感谢你的帮助。
发布于 2014-03-25 18:55:58
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,只是在一个循环中逐个测试。
还有,else。
发布于 2014-03-28 14:59:01
我不知道你想做什么。但不管怎样,我写的代码。看看这个答案是否符合你的问题。主要区别在于您的if-else块变成了try-except块。重点是index函数,它返回参数在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; # defaultP.S.
我的输出与您想要的输出略有不同(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 herehttps://stackoverflow.com/questions/22643688
复制相似问题