Module mainline (for isolation testing)
()
| 95 | os.remove(pathname) |
| 96 | |
| 97 | def main(): |
| 98 | """Module mainline (for isolation testing)""" |
| 99 | basename = "TestFile.txt" |
| 100 | if os.path.exists(basename): |
| 101 | os.remove(basename) |
| 102 | for i in range(10): |
| 103 | outf = VersionedOutputFile(basename) |
| 104 | outf.write("This is version %s.\n" % i) |
| 105 | outf.close() |
| 106 | |
| 107 | # Now there should be just four versions of TestFile.txt: |
| 108 | expectedSuffixes = ["", ".~7~", ".~8~", ".~9~"] |
| 109 | expectedVersions = [] |
| 110 | for suffix in expectedSuffixes: |
| 111 | expectedVersions.append("%s%s" % (basename, suffix)) |
| 112 | matchingFiles = glob.glob("%s*" % basename) |
| 113 | for filename in matchingFiles: |
| 114 | if filename not in expectedVersions: |
| 115 | sys.stderr.write("Found unexpected file %s.\n" % filename) |
| 116 | else: |
| 117 | # Unit tests should clean up after themselves... |
| 118 | os.remove(filename) |
| 119 | |
| 120 | # Finally, here's an example of how to use versioned |
| 121 | # output files in concert with marshal. |
| 122 | import marshal |
| 123 | |
| 124 | outf = VersionedOutputFile("marshal.dat") |
| 125 | # Marshal out a sequence: |
| 126 | marshal.dump([1, 2, 3], outf.asFile()) |
| 127 | outf.close() |
| 128 | os.remove("marshal.dat") |
| 129 | |
| 130 | if __name__ == "__main__": |
| 131 | main() |
no test coverage detected