Retrieving modification time of file class was declared in

Bengt Richter bokr at oz.net
Tue Jan 25 03:27:44 EST 2005


On 24 Jan 2005 20:39:11 -0800, nathan_kent_bullock at yahoo.ca wrote:

>Assume I am using a class Foo. I want to find out the modification time
>of the file that that class was defined in. How would I go about this?
>
>If I could find out the name of the file that Foo was defined in then
>it is easy, I could use os.path.getmtime(), but I can't even figure
>that out.
>
>I realize that this wouldn't be a completely accurate way to tell the
>last time this class was modified because it could inherit info from
>other classes, or use functions from other modules that have been
>modified, etc.
>
>Nathan Bullock
>

try this for about any object you can pass to finfo:

----< finfo.py >----------------
import sys, os
def finfo(obj):
    if not hasattr(obj, '__name__'):
        obj = type(obj)
        name = '<%s instance>'%obj.__name__
    else:
        name = obj.__name__
    if type(obj) == type(sys):  # module type
        modname = name
    else:  # not module type, but be class now
        modname = obj.__module__
    mod = sys.modules[modname]
    if modname == '__builtin__' or repr(mod) == "<module '%s' (built-in)>"%modname:
        path = sys.executable
    else:
        path = vars(sys.modules[modname]).get('__file__','??')
    if path != '??': tmod = os.path.getmtime(path)
    else: tmod = '???'
    return name, modname, path, tmod
--------------------------------

Not very tested, but seems to retrieve info. The file in question for builtins is
the interpreter executable, I supposed.

Ignore the "ut." here, that's just my utilities grabbag

 [ 0:25] C:\pywk\sovm>py24
 Python 2.4b1 (#56, Nov  3 2004, 01:47:27)
 [GCC 3.2.3 (mingw special 20030504-1)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> from ut.finfo import finfo
 >>> finfo(finfo)
 ('finfo', 'ut.finfo', 'c:\\pywk\\ut\\finfo.pyc', 1106641080)
 >>> import time
 >>> time.ctime(finfo(finfo)[-1])
 'Tue Jan 25 00:18:00 2005'

HIH

Regards,
Bengt Richter



More information about the Python-list mailing list