heh... forwarding to the list, too.<br><br>---------- Forwarded message ----------<br><span class="gmail_quote">From: <b class="gmail_sendername">Jason Massey</b> <<a href="mailto:jason.massey@gmail.com">jason.massey@gmail.com
</a>><br>Date: May 15, 2007 6:51 AM<br>Subject: Re: [Tutor] Text matching<br>To: "Gardner, Dean" <<a href="mailto:Dean.Gardner@barco.com">Dean.Gardner@barco.com</a>><br><br></span>Look at it a different way. If the one thing that is sure to be there is the SomeSpec portion, then how about making a listing of where they occur and slicing everything up.
<br><br>Concretely, as Mr. Yoo would say:<br><br>
f = open(r"c:\python24\test.txt",'r').readlines()<br>logList = []<br><br>indices = [x for x,line in enumerate(f) if 'SomeSpec' in line]<br>for x in range(len(indices)-1):<br> logList.append(f[indices[x]:indices[x+1]])
<br> <br>#tack on the last record<br>logList.append(f[indices[-1]:])<br><br>for count,x in enumerate(logList):<br> print count,':',x<br><br>C:\Python24>test.py<br>0 : ['---- SomeSpec 0000-0001 ----\n', '\n', '> some commit 1\n', '\n', 'Reviewed By: someone\n']
<br>1 : ['---- SomeSpec 0000-0002 ----\n', '> some commit 2\n', '\n']<br>2 : ['---- SomeSpec 0000-0003 ----\n', '>some commit 1\n', 'Reviewed By: Someone']<div><span class="e" id="q_1128f943d2426251_1">
<br><br><div>
<span class="gmail_quote">On 5/15/07, <b class="gmail_sendername">Gardner, Dean</b> <<a href="mailto:Dean.Gardner@barco.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">Dean.Gardner@barco.com
</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
So I took Kents advice and refactored but I have uncovered another<br>problem which I am hoping people may be able to help with. I realized<br>that the string I was using to identify the end of a record can in some<br>cases not be found (
i.e. if a commit wasn't reviewed). This can lead to<br>additional records being returned.<br><br>Can anyone suggest how I can get round this?<br><br>Text file example ( in this case looking for commit 1 would give details
<br>of commit two also):<br><br>---- SomeSpec 0000-0001 ----<br><br>> some commit 1<br><br>Reviewed By: someone<br>---- SomeSpec 0000-0002 ----<br>> some commit 2<br><br>---- SomeSpec 0000-0003 ----<br>>some commit 1
<br>Reviewed By: Someone<br><br><br>Code:<br><br> def searchChangeLog(self,filename):<br> uid = self.item.Uid()<br> record=[]<br> logList=[]<br> displayList=[]<br> f = open(filename)<br>
logTextFile="temp.txt"<br> """ searched through the changelog 'breaking' it up into<br> individual entries"""<br><br> for line in f:<br> if ("Reviewed: 000" in line):
<br> logList.append(record)<br> record = []<br> else:<br> record.append(line)<br><br> """ searches to determine if we can find entries for<br>
a particualr item"""
<br> for record in logList:<br> for item in record:<br> if uid in item:<br> displayList.append(record)<br> """ creates a temporary file to write our find results to """
<br> removeFile = os.path.normpath( os.path.join(os.getcwd(),<br>logTextFile))<br><br> # if the file exists, get rid of it before writing our new<br>findings<br> if Shared.config.Exists(removeFile):<br>
os.remove(removeFile)<br> recordLog = open(logTextFile,"a")<br><br> for record in range(len(displayList)):<br> for item in displayList[record]:<br> recordLog.write
(item)<br> recordLog.close()<br> #display our results<br> commandline = "start cmd /C " + logTextFile<br> os.system(commandline)<br><br>Dean Gardner<br>Test Engineer<br>Barco<br>Bonnington Bond, 2 Anderson Place, Edinburgh EH6 5NP, UK
<br>Tel + 44 (0) 131 472 5731 Fax + 44 (0) 131 472 4799<br><a href="http://www.barco.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">www.barco.com</a><br><a href="mailto:dean.gardner@barco.com" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
dean.gardner@barco.com</a><br><br><br>-----Original Message-----<br>From: Kent Johnson [mailto:
<a href="mailto:kent37@tds.net" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">kent37@tds.net</a>]<br>Sent: 04 May 2007 11:26<br>To: Gardner, Dean<br>Cc: <a href="mailto:tutor@python.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
tutor@python.org</a><br>Subject: Re: [Tutor] Text matching<br><br>Gardner, Dean wrote:
<br>><br>> So here it is....it might not be pretty (it seems a bit un-python like<br><br>> to me) but it works exactly as required. If anyone can give any tips<br>> for possible optimisation or refactor I would be delighted to hear
<br>> from them.<br>><br>> Thanks<br>><br>> uid = self.item.Uid()<br>> record=[]<br>> logList=[]<br>> displayList=[]<br>> f = open(filename)<br>> logTextFile="
temp.txt"<br>> """ searched through the changelog 'breaking' it up into<br>> individual entries"""<br>> try:<br>> while 1:<br>
> endofRecord=0<br>> l = f.next()<br>> if l.startswith("----"):<br>> record.append(l)<br>> l=f.next()<br>> while endofRecord==0:
<br>> if "Reviewed: 000" not in l:<br>> record.append(l)<br>> l=f.next()<br>> else:<br>>
logList.append(record)<br>> record=[]<br>> endofRecord=1<br>> except StopIteration:<br>> pass<br><br>I don't think you need endofRecord and the nested loops here. In fact I
<br>think you could use a plain for loop here. AFAICT all you are doing is<br>accumulating records with no special handling for anything except the<br>end records. What about this:<br>record = []<br>for line in f:<br> if "Reviewed: 000" in line:
<br> logList.append(record)<br> record = []<br> else:<br> record.append(line)<br><br>> """ searches to determine if we can find entries for<br>> a particualr item"""
<br>> for record in logList:<br>> currRec = record<br>> for item in currRec:<br>> if uid in item:<br>> displayList.append(currRec)<br><br>
The currRec variable is not needed, just use record directly.<br>If uid can only be in a specific line of the record you can test that<br>directly, e.g.<br>for record in logList:<br> if uid in record[1]:<br><br>> """ creates a temporary file to write our find results to """
<br>> removeFile = os.path.normpath( os.path.join(os.getcwd(),<br>> logTextFile))<br>><br>> # if the file exists, get rid of it before writing our new<br>> findings<br>> if Shared.config.Exists
(removeFile):<br>> os.remove(removeFile)<br>> recordLog = open(logTextFile,"a")<br>><br>> for record in range(len(displayList)):<br>> for item in displayList[record]:
<br>> recordLog.write(item)<br><br>for record in displayList:<br> recordLog.writelines(record)<br><br>> recordLog.close()<br>> #display our results<br>> commandline = "start cmd /C " + logTextFile
<br>> os.system(commandline)<br>><br><br>Kent<br><br><br>DISCLAIMER:<br>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.
<br>_______________________________________________<br>Tutor maillist - <a href="mailto:Tutor@python.org" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">Tutor@python.org</a><br><a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">
http://mail.python.org/mailman/listinfo/tutor
</a><br></blockquote></div><br>
</span></div>