Retrieving modification time of file class was declared in

Discussion in 'Python' started by nathan_kent_bullock@yahoo.ca, Jan 25, 2005.

  1. Guest

    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
     
    , Jan 25, 2005
    #1
    1. Advertising

  2. 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
     
    Orlando Vazquez, Jan 25, 2005
    #2
    1. Advertising

  3. 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:
    > 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, Jan 25, 2005
    #3
  4. On 24 Jan 2005 20:39:11 -0800, 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
     
    Bengt Richter, Jan 25, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JohnZing

    declared or not declared ?

    JohnZing, Feb 5, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,596
    Jon Paal
    Feb 5, 2006
  2. Replies:
    0
    Views:
    279
  3. Replies:
    3
    Views:
    674
    F. Petitjean
    May 7, 2005
  4. Replies:
    18
    Views:
    560
    Matthew Woodcraft
    Apr 28, 2009
  5. James E Keenan
    Replies:
    1
    Views:
    125
    Steve Grazzini
    Oct 14, 2003
Loading...

Share This Page