Python Forum
Regular Expression search to comment lines of code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Regular Expression search to comment lines of code
#1
Hello!

I'm hoping you guys can help me with a thorny issue I'm having.
I'm a beginner, not having touched Python since University 12 years ago :-)

I think this may be extremely easy for you guys, any tips you can provide would help greatly!

At a high level- I have a set of Oracle SQL DDL scripts that are generating errors because they have statements setting fields to be 'Not Null' because they are already not null...

Here's what the error looks like:

Error starting at line : 159 File 'XXX.sql'
In command -
ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE)
Error report -
ORA-01442: column to be modified to NOT NULL is already NOT NULL
01442. 00000 - "column to be modified to NOT NULL is already NOT NULL"
*Cause:
*Action:

What I want to do is write a simple script with two parts- one has a regular expression search that takes 'TABLE' and 'PK' from this error log and writes to a separate file using a regular expression search- perhaps using re.split(). Since this is an error log, a regular expression search for the active 'ALTER TABLE' line would suffice.

Then, I want to read this file and add line comments.. '--' to the beginning if each line in the original DDL script (A different file) that adds the erroneous constraints:
ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE)
~becomes~
-- ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE)

The things I don't understand:
How to load the key terms into variables with the regular expression search
How to add a comment before a line containing a regular expression search using terms parsed from the original file.

Anything that can help would be greatly appreciated.

Thanks Everyone!
Reply
#2
Maybe this will give you some ideas.

I presume PK is a column name??

If you split on " you can get what you want, I think.

def myApp():
    path2log = '/home/pedro/myPython/re/error.log'
    with open(path2log) as errors:
        error_data = errors.readlines()
    # save the table names and column names
    problem_tables = ''
    for i in range(len(error_data)):
        line = error_data[i]               
        if 'ALTER TABLE' in line:
            print(line)
            mylist = line.split('"')        
            tablename = mylist[1]
            column = mylist[3]
            print('table name is', tablename)
            print('column name is', column)
            problem_tables = problem_tables + tablename + ' ' + column + '\n'
            newline = '--' + line
            error_data[i] = newline
            
    savepath = '/home/pedro/myPython/re/problem_tables.log'
    with open(savepath, 'w') as sf:
        sf.write(problem_tables)
        
    new_error_log = ''.join(error_data)
    path2newlog = '/home/pedro/myPython/re/modified_error.log'
    with open(path2newlog, 'w') as errors:
        errors.write(new_error_log)
        
    print('Modified error.log saved to', path2newlog)
    print('Problem table names and columns saved to', savepath)
Out:

Output:
>>> myApp() ALTER TABLE "girlfriends" MODIFY ("rich" NOT NULL ENABLE) table name is girlfriends column name is rich ALTER TABLE "boyfriends" MODIFY ("handsome" NOT NULL ENABLE) table name is boyfriends column name is handsome ALTER TABLE "saints" MODIFY ("catholic" NOT NULL ENABLE) table name is saints column name is catholic >>>
Reply
#3
Thanks so much Pedroski55!!!
This really helps!

I'll need to make a small modification, where instead of making a new copy of the original log file with the alter table statements commented out, I have to modify a different Oracle DDL script that created these errors and comment lines out there.

I may be able to do this by recycling some of your code, making a new array similar to error_data but using the creation script, and having a nested loop that searches the original error_data array for table name and column name, and if exists, adding the line comment- but otherwise reconstructing the file like you do with new_error_log.

Does that make sense? I may not be explaining clearly Smile
Reply
#4
An Oracle DDL script is just text, I presume?

Python is very good at doing things with text files.

I use Python to make html and php. Enter a few details, click a button, done!

html and php are also just text files!
Reply
#5
Can do like this and don't need regex in this case.
with open('error.log') as fp,open('error_out.log', 'w') as fp_out:
    for line in fp:
        if 'ALTER TABLE' in line:
            fp_out.write(f'--{line}')
        else:
            fp_out.write(line)
Output:
Error starting at line : 159 File 'XXX.sql' In command - --ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE) Error report - ORA-01442: column to be modified to NOT NULL is already NOT NULL 01442. 00000 - "column to be modified to NOT NULL is already NOT NULL" *Cause: *Action:
Reply
#6
(Sep-06-2022, 09:36 PM)Gman2233 Wrote: Then, I want to read this file and add line comments.. '--' to the beginning if each line in the original DDL script (A different file) that adds the erroneous constraints:
ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE)
~becomes~
-- ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE)

Why write a script to do this at all? If you're using this to learn Python, that's fine, but do be aware that there are often other tools that can help. In this case, at least on Unix*, I'd use sed. Assuming the file is called changes.sql:

Output:
$ cat changes.sql ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE) $ sed -i .original 's/^/--/' changes.sql $ cat changes.sql --ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE) $ cat changes.sql.original ALTER TABLE "TABLE" MODIFY ("PK" NOT NULL ENABLE)
So, to break down what the sed command does here:

- The -i option will modify the file in place, but leave the original in a file whose name has the given extension (.original here).
- 's/^/--/' specifies what we want sed to do - substitute the beginning of the line (i.e. the regular expression ^) with the comment characters --. It will do this for each line.

Printing out the file shows that the line has been changed and printing out the one with .original on the end shows we've indeed kept the original there.

A good tutorial on sed can be found here: https://www.grymoire.com/Unix/Sed.html.

Note also that text editors are powerful these days and have useful search and replace functionality, so you may choose to do it that way.

* I don't use Windows, so I don't know what the options are there.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add multi-line comment section? Winfried 1 139 Mar-24-2024, 04:34 PM
Last Post: deanhystad
  data validation with specific regular expression shaheen07 0 296 Jan-12-2024, 07:56 AM
Last Post: shaheen07
Question remove all comment ? SpongeB0B 7 1,188 Oct-27-2023, 05:40 PM
Last Post: deanhystad
  Is the following code returning a generator expression? quazirfan 8 1,533 Apr-11-2023, 11:44 PM
Last Post: quazirfan
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,602 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  Regex Expression With Code Query In Pandas eddywinch82 8 2,274 Apr-13-2022, 09:12 AM
Last Post: snippsat
  Need help with my code (regular expression) shailc 5 1,872 Apr-04-2022, 07:34 PM
Last Post: shailc
  Regular Expression for matching words xinyulon 1 2,132 Mar-09-2022, 10:34 PM
Last Post: snippsat
  I want to simplify this python code into fewer lines, it's about string mandaxyz 5 2,046 Jan-15-2022, 01:28 PM
Last Post: mandaxyz
  regular expression question Skaperen 4 2,419 Aug-23-2021, 06:01 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020