By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,173 Members | 1,686 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,173 IT Pros & Developers. It's quick & easy.

Retrieving modification time of file class was declared in

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
na*****************@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


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 :-)

--
Orlando Vazquez
Jul 18 '05 #2

P: n/a
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 Vazquez wrote:
na*****************@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


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 :-)

Jul 18 '05 #3

P: n/a
On 24 Jan 2005 20:39:11 -0800, na*****************@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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.