lst = infile.readlines() infile.close() ## taking the useful part of the file into file_list check = day_trans(day_of_week) program = map(lambda item: item.split(','), lst)[2:] file_list = [] for shows in program: days = shows[3] if days[check-1] != '.': file_list.append(shows[:2]) ## writing to a file filename = day_of_week + '-' + ratings_file output_file = file(filename, 'w') output_file.write(day_string % day_of_week) for seg in file_list: output_file.write(seg) output_file.close() # Tests ## base case: the given file only contains the first two lines and ## there is no program data in the file check.expect('q2t1', ratings_by_day('q2t1.txt', 'Sunday'), None) check.set_file('Sunday-q2t1.txt', 'q2t1-check.txt') ## other case: the given file contains some of the program infomation check.expect('q2t2', ratings_by_day('q2t2.txt', 'Tuesday'), None) check.set_file('Tuesday-q2t2.txt', 'q2t2-check.txt') ## case that data in the given file has no programs on the given day check.expect('q2t3', ratings_by_day('q2t3.txt', 'Sunday'), None) check.set_file('Sunday-q2t3.txt', 'q2t3-check.txt')
# ( 0 words): # ( 2 words): 5 words def add_word_count(filename): try: F = file(filename) except: print 'Ooops - problems!' return lines = F.readlines() F.close() total_words = 0 F = file('wc-'+filename, 'w') for line in lines: words = line.split() total_words += len(words) F.write("(%2d words): %s" % (len(words), line)) F.write("(%d total words)" % total_words) F.close() return total_words check.set_file("wc-small.txt", "expected-wc-small.txt") check.expect("word-T1", add_word_count("small.txt"), 5) ## Things to notice/discuss # must import check # different situations require different test (possibly a combination) ## Not illustrated in examples: # difference between check.expect and check.within # read cs116 documentation on check.py
## a ## b ## c ## done ## ## results in 'output.txt' containing the following three lines ## ## ccccccccccccccccccccccc ## bbbbbbbbbbbbbbbbbbbbbbb ## aaaaaaaaaaaaaaaaaaaaaaa def silly(): with open("output.txt",'w') as outFile : ## silly_helper: None -> None ## ## Effects: Same as silly, but assumes the write file is already open def silly_helper() : nextLine = raw_input() if (nextLine == "done") : return silly_helper() outFile.write(nextLine*23) outFile.write("\n") silly_helper() ## Test by running function with varying number and types of input. # Be sure to do lots more of your own testing! check.set_input(['a','b','c','d','e',"done"]) check.set_file("output.txt","expected_output") check.expect("Simple test",silly(),None)
## c ## done ## ## results in 'output.txt' containing the following three lines ## ## ccccccccccccccccccccccc ## bbbbbbbbbbbbbbbbbbbbbbb ## aaaaaaaaaaaaaaaaaaaaaaa def silly(): with open("output.txt", "w") as outFile: ## silly_helper: None -> None ## ## Effects: Same as silly, but assumes the write file is already open def silly_helper(): nextLine = raw_input() if nextLine == "done": return silly_helper() outFile.write(nextLine * 23) outFile.write("\n") silly_helper() ## Test by running function with varying number and types of input. # Be sure to do lots more of your own testing! check.set_input(["a", "b", "c", "d", "e", "done"]) check.set_file("output.txt", "expected_output") check.expect("Simple test", silly(), None)