简体   繁体   English

如何使用startswith函数生成像diff工具中的输出?

[英]How to use the startswith function to produce output like in the diff tool?

Please help me solve (I think interesting) algorithmic problem. 请帮我解决(我觉得有趣)算法问题。 I spent (lost) the whole day today to solve this. 我今天花了一整天时间(迷路)来解决这个问题。 Here is my code: 这是我的代码:

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)

Where: 哪里:

  • rman_config - variable with the default configuration rman_config - 具有默认配置的变量
  • rman_new_parameters - new parameters in random order to override defaults where: rman_new_parameters - 以随机顺序覆盖默认值的新参数,其中:
    • first element in the tuple is a parameter (ie CONTROLFILE AUTOBACKUP ) 元组中的第一个元素是一个参数(即CONTROLFILE AUTOBACKUP
    • second element in the tuple is a new value (ie ON ) 元组中的第二个元素是一个新值(即ON

So far I have this output: 到目前为止我有这个输出:

-  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

But I want this (like in Diff ): 但我想要这个(就像在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 

The startswith function is great, but it doesn't tell me which tuple is recognized (returns only True or False). startswith函数很棒,但它没有告诉我哪个元组被识别(只返回True或False)。 My problem with above code is how to map following line: 我上面代码的问题是如何映射以下行:

if line.startswith(tuple_to_search, 10):

if it's True to the rman_new_parameters list. 如果它对rman_new_parameters列表是真的。

I haven't got a clue how to resolve it? 我还没有弄清楚如何解决它? I will be really grateful for any help. 我会非常感激任何帮助。

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))

Basically, I didn't pass a tuple to .startswith , just tested one by one in a loop. 基本上,我没有将元组传递给.startswith ,只是在一个循环中逐个测试。

Also, for + else . 另外, for + else

I don't know what you are trying to do. 我不知道你要做什么。 But anyway I write the code. 但无论如何我写代码。 See if this answer matches your question. 看看这个答案是否符合您的问题。 The main difference is your if-else block becomes try-except block. 主要区别在于你的if-else块变为try-except块。 And the point is index function which returns the position of the first occurrence of the parameter in tuple_to_search here. 并且重点是index函数,它返回tuple_to_search第一次出现参数的tuple_to_search I assume all value of parameters in rman_new_parameters is changed from rman_config . 我假设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)

output: 输出:

-  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 PS

My output is bit different with your desired output(2 lines differ). 我的输出与您想要的输出略有不同(2行不同)。 I don't know why. 我不知道为什么。 So please ensure it is your desired output. 所以请确保它是您想要的输出。

Yours: 你:

...
+  CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO cf_%F;
...
   CONFIGURE DATAFILE BACKUP COPIES FOR DEVICE TYPE DISK TO 1; # default 

Mine: 矿:

...
+  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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM