Retrieving modification time of file class was declared in

  • Thread starter nathan_kent_bullock
  • Start date
N

nathan_kent_bullock

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
 
O

Orlando Vazquez

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

Off the top of my head, without having done too much experimentation
here's what you could try. Caveat: there may be a more robust/cleaner
way of doing this:

# Checking the modificationtime of the Thread class in the threading
# module
>>> import threading
>>> import time
>>>
>>> module_filename = vars()[threading.Thread.__module__].__file__
>>>
>>> mtime = os.path.getmtime(module_filename)
>>>
>>> print time.ctime(mtime)
Sun Nov 14 20:29:42 2004


I hope that answer's your question :)
 
O

Orlando Vazquez

Actually, what my comment wass supposed to say was "Checking the
modification time of the file the Thread class was defined in", but I'm
sure you understood what I meant. ;-)

Orlando said:
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

Off the top of my head, without having done too much experimentation
here's what you could try. Caveat: there may be a more robust/cleaner
way of doing this:

# Checking the modificationtime of the Thread class in the threading
# module
import threading
import time

module_filename = vars()[threading.Thread.__module__].__file__

mtime = os.path.getmtime(module_filename)

print time.ctime(mtime)
Sun Nov 14 20:29:42 2004


I hope that answer's your question :)
 
B

Bengt Richter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top