[Tutor] Text matching and replacing

Gardner, Dean Dean.Gardner at barco.com
Wed Jul 25 14:11:10 CEST 2007


 
Cheers for the critique I'll take you points on board .....especially
this schoolboy error 

def findTestDirectories(path):
    os.chdir(path)
    directory_listing = os.listdir(os.getcwd())
------------------

Change this to
directory_listing = os.listdir(path)

Why look up the current directory when you have *just* set what it is?
--------------------------------------------------------


One thing to note about the re expression is that the products are not
<product n> these were just substitutes. In reality these are product
names with no commonality e.g. ('baked beans'|'tuna'|'salad')

So with that in mind is the way I have set the re way the best way or is
there an another more pythonic way.

As an aside I don't believe there were any tips in there to help solve
the problems I have...again any help would be warmly appreciated.

Cheers

Dean

Message: 3
Date: Tue, 24 Jul 2007 14:18:46 -0500
From: "Tiger12506" <keridee at jayco.net>
Subject: Re: [Tutor] Text matching and replacing
To: <tutor at python.org>
Message-ID: <015201c7ce27$78b87770$f3fce004 at JSLAPTOP>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
	reply-type=original

def findTestDirectories(path):
    os.chdir(path)
    directory_listing = os.listdir(os.getcwd())
------------------

Change this to
directory_listing = os.listdir(path)

Why look up the current directory when you have *just* set what it is?

----------------
    test_record_directories = []
    for directory in directory_listing:
        if "TestRecords" in directory:
            test_record_directories.append(directory)
--------------------------
This whole block could be turned into a list comprehension

test_record directories = [di for di in directory_listing if
"TestRecords" 
in di]

-------------------------
    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)
---------------------------------------
Woah! Regular expression could use work.
Try:

re.compile(r'<product \d>', re.IGNORECASE)

This will match product #s 1-9
If you want to match all product numbers to infinity put a * after \d

--------------------------------------
    for record in records_from_record_file:
        if searchText.findall(record) !=[]:
-----------------------------------
if searchText.findall(record):

is sufficient
---------------------------------
            print record.split("\n\n")

def amendProductField(dir):
    fileList = os.listdir(dir)
    currPath =  os.getcwd()+"\\"+dir+"\\"
--------------------
This could be

currPath = os.path.join(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)
---------------------------
And again ~

f = open(os.path.join(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.



------------------------------------------------------------------------
--------


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


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.


More information about the Tutor mailing list