rfc: a self-editing script

gb345 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
aesthetic imperative...

>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 
>like this:

># Untested
>def get_last_version(filename):
>    """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[0])

>...
>more code goes here
>...


># ==================================================
># ===      Version number history goes here.     ===
># === DO NOT insert any code after this point!!! ===
># ==================================================
># 1.0.1
># 1.0.2a
># 1.0.2
># 1.0.5


>This has the added advantage that you can track the updates made to the 
>version number.


Thanks, these are great ideas.  Just the feedback I was looking for.

G.



More information about the Python-list mailing list