[Tutor] Text matching and replacing

Gardner, Dean Dean.Gardner at barco.com
Tue Jul 24 14:37:43 CEST 2007


Hi 

I have implemented some functionality that now forces test records to be
stored against the product they were run against.  This is fine however
it was apparent that we needed to bring the legacy records in line with
new format. So I wrote a script that went through each test record and
appended the "Product Run: " field at the end of each test record. This
works however I thought I could try and be a bit clever, as in some
cases previously people have logged the product that the was run against
in a comments field, and take the product name out of the comment field
and place it in the ProductRun field. 

However I have got myself slightly confused as the best way to proceed.
Here is my script  and below a sample test record and what the record i
am ultimately aiming for. I have changed the product names to something
generic. The regular expression actually works and finds all instances
of a test being run against a product but I am confused as to how I can
then further process the information

I will also take code critique as well. 

import os
import re
debug = False

def writeUpdatedRecordsToFile(path,updated_records):

    for x,j in updated_records.iteritems():
        f = open(path+x,"w")
        j.pop()
        for record in j:
            f.write(record)



def findTestDirectories(path):
    os.chdir(path)
    directory_listing = os.listdir(os.getcwd())
    test_record_directories = []
    for directory in directory_listing:
        if "TestRecords" in directory:
            test_record_directories.append(directory)
    return test_record_directories

def findProductFromComments(records_from_record_file):
    '''
        Attempt to find products run against in the comment field
        if we find one. Write it to the newly created product run field
    '''

    searchText = re.compile(r'(<product 1>|<product 2>|<product
3>|<product 4>)', re.IGNORECASE)
    for record in records_from_record_file:
        if searchText.findall(record) !=[]:
            print record.split("\n\n")

def amendProductField(dir):
    fileList = os.listdir(dir)
    currPath =  os.getcwd()+"\\"+dir+"\\"
    dict_of_amended_records = {}
    list_of_amended_records = []
    for file in fileList:
        if "CVS" in file:
            pass
        else:
            f = open(currPath+"\\"+file)
            if debug:
                print "opening %s for reading" %file
            fileContents = f.read()
            fileContents = fileContents.split("\n\n")
            findProductFromComments(fileContents)
            for record in fileContents:

                record+="\nProductRun:\n\n"
                list_of_amended_records.append(record)
                dict_of_amended_records[file] = list_of_amended_records
            list_of_amended_records = []
    #writeUpdatedRecordsToFile(currPath,dict_of_amended_records)


test_dir = findTestDirectories("C:\\Sandbox")
if debug:
    print "Opening %s for amending" %test_dir[0]

#for directory in test_dir:
    #amendProductField(directory)
amendProductField(test_dir[0])

Current Record:

TestedDate: 2005-04-30
TestId: 001591
Branch: xxxx
Version: 3351
SpecId: Specification-0000-0966
Cpu: Pentium 4
OperatingSystem: Windows 2000
CpuCount: Single
Tester: someone
Comment: Run on <product 1>
MinutesTaken: 5
PassOrFail: Pass

Desired Record:

TestedDate: 2005-04-30
TestId: 001591
Branch: xxxx
Version: 3351
SpecId: Specification-0000-0966
Cpu: Pentium 4
OperatingSystem: Windows 2000
CpuCount: Single
Tester: someone
Comment: Run on <product 1>
MinutesTaken: 5
PassOrFail: Pass
Product: <product 1> 


Dean Gardner





DISCLAIMER:
Unless indicated otherwise, the information contained in this message is privileged and confidential, and is intended only for the use of the addressee(s) named above and others who have been specifically authorized to receive it. If you are not the intended recipient, you are hereby notified that any dissemination, distribution or copying of this message and/or attachments is strictly prohibited. The company accepts no liability for any damage caused by any virus transmitted by this email. Furthermore, the company does not warrant a proper and complete transmission of this information, nor does it accept liability for any delays. If you have received this message in error, please contact the sender and delete the message. Thank you.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070724/3dbb19c9/attachment.html 


More information about the Tutor mailing list