简体   繁体   中英

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_new_parameters - new parameters in random order to override defaults where:
    • first element in the tuple is a parameter (ie CONTROLFILE AUTOBACKUP )
    • second element in the tuple is a new value (ie 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 ):

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

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.

Also, 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. And the point is index function which returns the position of the first occurrence of the parameter in tuple_to_search here. I assume all value of parameters in rman_new_parameters is changed from 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

My output is bit different with your desired output(2 lines differ). 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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