[Tutor] Text matching

Gardner, Dean Dean.Gardner at barco.com
Fri May 4 10:27:46 CEST 2007


 
So here it is....it might not be pretty (it seems a bit un-python like
to me) but it works exactly as required. If anyone can give any tips for
possible optimisation or refactor I would be delighted to hear from
them. 

Thanks

	  uid = self.item.Uid()
        record=[]
        logList=[]
        displayList=[]
        f = open(filename)
        logTextFile="temp.txt"
        """ searched through the changelog 'breaking' it up into
            individual entries"""
        try:
            while 1:
                endofRecord=0
                l = f.next()
                if l.startswith("----"):
                    record.append(l)
                l=f.next()
                while endofRecord==0:
                    if "Reviewed: 000" not in l:
                        record.append(l)
                        l=f.next()
                    else:
                        logList.append(record)
                        record=[]
                        endofRecord=1
        except StopIteration:
            pass
        """ searches to determine if we can find entries for
            a particualr item"""
        for record in logList:
            currRec = record
            for item in currRec:
                if uid in item:
                    displayList.append(currRec)
        """ creates a temporary file to write our find results to """
        removeFile = os.path.normpath( os.path.join(os.getcwd(),
logTextFile))

        # if the file exists, get rid of it before writing our new
findings
        if Shared.config.Exists(removeFile):
            os.remove(removeFile)
        recordLog = open(logTextFile,"a")

        for record in range(len(displayList)):
            for item in displayList[record]:
                recordLog.write(item)
        recordLog.close()
        #display our results
        commandline = "start cmd /C " + logTextFile
        os.system(commandline) 

Dean Gardner
Test Engineer 
Barco
Bonnington Bond, 2 Anderson Place, Edinburgh EH6 5NP, UK
Tel + 44 (0) 131 472 5731 Fax + 44 (0) 131 472 4799
www.barco.com 
dean.gardner at barco.com 


-----Original Message-----
From: Kent Johnson [mailto:kent37 at tds.net] 
Sent: 03 May 2007 14:39
To: Gardner, Dean
Cc: tutor at python.org
Subject: Re: [Tutor] Text matching

Gardner, Dean wrote:
> Here is a test sample of what I have: This currently correctly 
> identifies the start and the end of the changelog entries and I can 
> identify the requested item records. What I am struggling with at 
> present is to how to create records of the full changelog entries for 
> each of the found items.
> 
> Again any help greatly appreciated.
> 
> 
> uid = "000028.txt"
> 
> 
> file = open("C:\projects\SuiteSpecification\Notes\ChangeLog.txt")
> 
> output = file.readlines()
> changeLogList = []
> itemList = []
> for strOut in output:
> 
>     if "----" in strOut:
>         changeLogStart = 1
>     itemList.append(strOut)       
>     if "Reviewed:" in strOut:
>         changeLogStart=0
> for item in itemList:
>     if uid in item:
>         print item

I usually solve problems like this by pulling multiple lines from the
file within the loop. Something like this:
f = open('...ChangeLog.txt')
try:
   while True:
     line = f.next()
     if line.startswith('----'):
       line1 = line
       line2 = f.next()
       line3 = f.next()
       line4 = f.next()
       # Do something to process the lines
     line = f.next()
except StopIteration:
   pass

The basic idea is that you can collect multiple lines by calling
f.next() explicitly inside the loop.

Kent


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