<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7652.24">
<TITLE>Text matching and replacing</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->
<P><SPAN LANG="en-gb"><FONT SIZE=2 FACE="Arial">Hi </FONT></SPAN>
</P>
<P><SPAN LANG="en-gb"><FONT SIZE=2 FACE="Arial">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. </FONT></SPAN></P>
<P><SPAN LANG="en-gb"><FONT SIZE=2 FACE="Arial">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</FONT></SPAN></P>
<P><SPAN LANG="en-gb"><FONT SIZE=2 FACE="Arial">I will also take code critique as well. </FONT></SPAN>
</P>
<P><SPAN LANG="en-gb"><FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">import</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> os</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">import</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> re</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New">debug =</FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">False</FONT></SPAN>
</P>
<P><SPAN LANG="en-gb"><FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">def</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"><B> writeUpdatedRecordsToFile</B>(path,updated_records):</FONT></SPAN>
</P>
<P><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">for</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> x,j</FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">in</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> updated_records.iteritems():</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> f = open(path+x,</FONT><I><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">"w"</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New">)</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> j.pop()</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">for</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> record</FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">in</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> j:</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> f.write(record)</FONT></SPAN>
</P>
<BR>
<BR>
<P><SPAN LANG="en-gb"><FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">def</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></FONT><B> <FONT COLOR="#000000" SIZE=2 FACE="Courier New">findTestDirectories</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New">(path):</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> os.chdir(path)</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> directory_listing = os.listdir(os.getcwd())</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> test_record_directories = []</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">for</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> directory</FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">in</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> directory_listing:</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">if</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></FONT><I> <FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">"TestRecords"</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">in</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> directory:</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> test_record_directories.append(directory)</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">return</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> test_record_directories</FONT></SPAN>
</P>
<P><SPAN LANG="en-gb"><FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">def</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></FONT><B> <FONT COLOR="#000000" SIZE=2 FACE="Courier New">findProductFromComments</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New">(records_from_record_file):</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT><I> <FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">'''</FONT></I></SPAN>
<BR><SPAN LANG="en-gb"><I><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New"> Attempt to find products run against in the comment field</FONT></I></SPAN>
<BR><SPAN LANG="en-gb"><I><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New"> if we find one. Write it to the newly created product run field</FONT></I></SPAN>
<BR><SPAN LANG="en-gb"><I><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New"> '''</FONT></I></SPAN>
</P>
<P><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> searchText = re.compile(r</FONT><I><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">'(</FONT><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New"><product 1></FONT><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">|</FONT><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New"><product 2></FONT><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">|</FONT><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New"><product 3></FONT><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">|</FONT><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New"><product 4></FONT><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">)'</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New">, re.IGNORECASE)</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">for</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> record</FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">in</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> records_from_record_file:</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">if</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> searchText.findall(record) !=[]:</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">print</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> record.split(</FONT><I><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">"\n\n"</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New">)</FONT></SPAN>
</P>
<P><SPAN LANG="en-gb"><FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">def</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></FONT><B> <FONT COLOR="#000000" SIZE=2 FACE="Courier New">amendProductField</FONT></B><FONT COLOR="#000000" SIZE=2 FACE="Courier New">(dir):</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> fileList = os.listdir(dir)</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> currPath = os.getcwd()+</FONT><I><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">"\\"</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New">+dir+</FONT><I><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">"\\"</FONT></I></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> dict_of_amended_records = {}</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> list_of_amended_records = []</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">for</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> file</FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">in</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> fileList:</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">if</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></FONT><I> <FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">"CVS"</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">in</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> file:</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">pass</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">else</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New">:</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> f = open(currPath+</FONT><I><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">"\\"</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New">+file)</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">if</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> debug:</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">print</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></FONT><I> <FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">"opening %s for reading"</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> %file</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> fileContents = f.read()</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> fileContents = fileContents.split(</FONT><I><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">"\n\n"</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New">)</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> findProductFromComments(fileContents)</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">for</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> record</FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">in</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> fileContents:</FONT></SPAN>
</P>
<P><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> record+=</FONT><I><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">"\nProductRun:\n\n"</FONT></I></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> list_of_amended_records.append(record)</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> dict_of_amended_records[file] = list_of_amended_records</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> list_of_amended_records = []</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#C0C0C0" SIZE=2 FACE="Courier New">#writeUpdatedRecordsToFile(currPath,dict_of_amended_records)</FONT></SPAN>
</P>
<BR>
<P><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New">test_dir = findTestDirectories(</FONT><I><FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">"C:\\Sandbox"</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New">)</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">if</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> debug:</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#0000FF" SIZE=2 FACE="Courier New">print</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New"></FONT><I> <FONT COLOR="#00AA00" SIZE=2 FACE="Courier New">"Opening %s for amending"</FONT></I><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> %test_dir[</FONT><FONT COLOR="#800000" SIZE=2 FACE="Courier New">0</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New">]</FONT></SPAN>
</P>
<P><SPAN LANG="en-gb"><FONT COLOR="#C0C0C0" SIZE=2 FACE="Courier New">#for directory in test_dir:</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New"> </FONT> <FONT COLOR="#C0C0C0" SIZE=2 FACE="Courier New">#amendProductField(directory)</FONT></SPAN>
<BR><SPAN LANG="en-gb"><FONT COLOR="#000000" SIZE=2 FACE="Courier New">amendProductField(test_dir[</FONT><FONT COLOR="#800000" SIZE=2 FACE="Courier New">0</FONT><FONT COLOR="#000000" SIZE=2 FACE="Courier New">])</FONT></SPAN>
</P>
<P><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Current Record:</FONT></SPAN>
</P>
<P><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">TestedDate: 2005-04-30</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">TestId: 001591</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Branch: xxxx</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Version: 3351</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">SpecId: Specification-0000-0966</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Cpu: Pentium 4</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">OperatingSystem: Windows 2000</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">CpuCount: Single</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Tester: someone</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Comment: Run on <product 1></FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">MinutesTaken: 5</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">PassOrFail: Pass</FONT></SPAN>
</P>
<P><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Desired Record:</FONT></SPAN>
</P>
<P><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">TestedDate: 2005-04-30</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">TestId: 001591</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Branch: xxxx</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Version: 3351</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">SpecId: Specification-0000-0966</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Cpu: Pentium 4</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">OperatingSystem: Windows 2000</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">CpuCount: Single</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Tester: someone</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Comment: Run on <product 1></FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">MinutesTaken: 5</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">PassOrFail: Pass</FONT></SPAN>
<BR><SPAN LANG="en-nz"><FONT SIZE=2 FACE="Arial">Product: <product 1> </FONT></SPAN>
</P>
<BR>
<P><B><SPAN LANG="en-gb"><FONT SIZE=2 FACE="Arial">Dean Gardner</FONT></SPAN></B><SPAN LANG="en-gb"><BR>
<U></U><U></U></SPAN>
</P>
<BR>
<p></p><p>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.</BODY>
</HTML>