[Tutor] Text matching and replacing
Tiger12506
keridee at jayco.net
Tue Jul 24 21:18:46 CEST 2007
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
>
More information about the Tutor
mailing list