Joe Strout wrote:
This isn't a question, but something I thought others may find useful (and if somebody can spot any errors with it, I'll be grateful).

We had a case recently where the client was running an older version of our app, and didn't realize it. In other languages I've avoided this by displaying the compile date in the About box, but of course Python doesn't really have a meaningful compile date. So, instead we're now displaying the latest modification date of any .py file in the project. Here's the function that finds that:


def lastModDate():
"Get the latest modification date (as a string) of any .py file in this project."
    import os, time
    latest = 0
    dir = os.path.dirname(__file__)
    if dir == '': dir = '.'  # HACK, but appears necessary
    for fname in os.listdir(dir):
        if fname.endswith('.py'):
            modtime = os.stat(os.path.join(dir, fname)).st_mtime
            if modtime > latest: latest = modtime
    out = time.strftime('%Y-%m-%d', time.localtime(latest))
    return out
Here's how I'd do it with your specs:
 def lastModDate(directory=None):
    "Latest modification ISO date string of .py files in this directory"
    import os, time
    latest = 0
    if directory is None:
        directory = os.path.dirname(__file__)
    for fname in os.listdir(directory or '.'):
        if fname.endswith('.py'):
            modtime = os.stat(os.path.join(directory, fname)).st_mtime
            if modtime > latest:
                latest = modtime
    return time.strftime('%Y-%m-%d', time.localtime(latest))

And I'd prefer:
 def lastPyDate(directory=None):
    "Latest modification ISO date string of .py files in this file tree"
    import os, time
    latest = 0
    if directory is None:
        directory = os.path.dirname(__file__)
    for base, dirs, files in os.walk(directory):
        for name in files:
            if name.endswith('.py'):
                modtime = os.stat(os.path.join(base, name)).st_mtime
                if modtime > latest:
                    latest = modtime
    return time.strftime('%Y-%m-%d', time.localtime(latest))

But, as someone mentioned, if one of your users edits one of your files
without changing it, he has accidentally changed the version of the
code.  This you could forestall by invoking your function at packaging
time, and writing a file with the version string in it.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to