rfc: a self-editing script
gb345 at invalid.com
Sat Oct 10 22:04:40 CEST 2009
In <0059c2b1$0$26930$c3e8da3 at news.astraweb.com> Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
>But if you absolutely have to write to the program file...
No, don't have to, beyond the urge to satisfy a very idiosyncratic
>then append your
>data to the end of the file (as a comment) and later read that, rather
>than modifying the actual code in place. That is, you fetch the
>LAST_VERSION by reading the last non-empty line in the file, something
> """Retrieves the last version number from the given filename,
> taken from the last non-empty line."""
> candidate = ''
> for line in open(filename, 'r'):
> line = line.strip()
> if line and line.startswith('#'):
> candidate = line.lstrip('# \t')
> # error checking goes here
> return candidate
>LAST_VERSION = get_last_version(sys.argv)
>more code goes here
># === Version number history goes here. ===
># === DO NOT insert any code after this point!!! ===
>This has the added advantage that you can track the updates made to the
Thanks, these are great ideas. Just the feedback I was looking for.
More information about the Python-list