Retrieving modification time of file class was declared in

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nathan_kent_bullock@yahoo.ca

    #1

    Retrieving modification time of file class was declared in

    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.getmtim e(), 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

  • Orlando Vazquez

    #2
    Re: Retrieving modification time of file class was declared in

    nathan_kent_bul lock@yahoo.ca wrote:[color=blue]
    > 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.getmtim e(), 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
    >[/color]

    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 modificationtim e of the Thread class in the threading
    # module
    [color=blue][color=green][color=darkred]
    >>> import threading
    >>> import time
    >>>
    >>> module_filename = vars()[threading.Threa d.__module__].__file__
    >>>
    >>> mtime = os.path.getmtim e(module_filena me)
    >>>
    >>> print time.ctime(mtim e)[/color][/color][/color]
    Sun Nov 14 20:29:42 2004


    I hope that answer's your question :-)

    --
    Orlando Vazquez

    Comment

    • Orlando Vazquez

      #3
      Re: Retrieving modification time of file class was declared in

      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:[color=blue]
      > nathan_kent_bul lock@yahoo.ca wrote:
      >[color=green]
      >> 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.getmtim e(), 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
      >>[/color]
      >
      > 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 modificationtim e of the Thread class in the threading
      > # module
      >[color=green][color=darkred]
      > >>> import threading
      > >>> import time
      > >>>
      > >>> module_filename = vars()[threading.Threa d.__module__].__file__
      > >>>
      > >>> mtime = os.path.getmtim e(module_filena me)
      > >>>
      > >>> print time.ctime(mtim e)[/color][/color]
      > Sun Nov 14 20:29:42 2004
      >
      >
      > I hope that answer's your question :-)
      >[/color]

      Comment

      • Bengt Richter

        #4
        Re: Retrieving modification time of file class was declared in

        On 24 Jan 2005 20:39:11 -0800, nathan_kent_bul lock@yahoo.ca wrote:
        [color=blue]
        >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.getmtim e(), 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
        >[/color]

        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.module s[modname]).get('__file__ ','??')
        if path != '??': tmod = os.path.getmtim e(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>py 24
        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.[color=blue][color=green][color=darkred]
        >>> from ut.finfo import finfo
        >>> finfo(finfo)[/color][/color][/color]
        ('finfo', 'ut.finfo', 'c:\\pywk\\ut\\ finfo.pyc', 1106641080)[color=blue][color=green][color=darkred]
        >>> import time
        >>> time.ctime(finf o(finfo)[-1])[/color][/color][/color]
        'Tue Jan 25 00:18:00 2005'

        HIH

        Regards,
        Bengt Richter

        Comment

        Working...