__LINE__ and __FILE__ functionality in Python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joakim Hove

    __LINE__ and __FILE__ functionality in Python?


    Hello,

    i have simple[1] function like this:

    def log_msg(msg , file , line):
    print "%s:%s %s" % (file,line,msg)

    the file and line arguments should be the filename and linenumber of
    the source file where the function is called. If this were C I would
    have used the __FILE__ and __LINE__ macros as:

    log_msg(msg , __FILE__ , __LINE__)

    Is there a way to emulate this behaviour in Python?


    Best Regards

    Joakim Hove



    [1]: It is not *that* simple, but you get the point.


    --
    Joakim Hove
    hove AT ntnu.no /
    Tlf: +47 (73 5)9 34 27 / Stabburveien 18
    Fax: ............... .. / N-5231 Paradis
    http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04
  • Maric Michaud

    #2
    Re: __LINE__ and __FILE__ functionality in Python?

    Le dimanche 13 août 2006 13:31, Joakim Hove a écrit :
    Hello,
    >
    i have simple[1] function like this:
    >
    def log_msg(msg , file , line):
    print "%s:%s %s" % (file,line,msg)
    >
    the file and line arguments should be the filename and linenumber of
    the source file where the function is called. If this were C I would
    have used the __FILE__ and __LINE__ macros as:
    >
    log_msg(msg , __FILE__ , __LINE__)
    >
    Is there a way to emulate this behaviour in Python?
    Sure, try :

    In [46]: import inspect

    In [47]: c=inspect.curre ntframe()

    In [48]: c.f_lineno
    Out[48]: 1

    In [49]: c.f_code.co_fil ename
    Out[49]: '<ipython console>'

    Of course, in the console, these are not truly relevant.


    --
    _____________

    Maric Michaud
    _____________

    Aristote - www.aristote.info
    3 place des tapis
    69004 Lyon
    Tel: +33 426 880 097

    Comment

    • John Machin

      #3
      Re: __LINE__ and __FILE__ functionality in Python?

      Joakim Hove wrote:
      Hello,
      >
      i have simple[1] function like this:
      >
      def log_msg(msg , file , line):
      print "%s:%s %s" % (file,line,msg)
      >
      the file and line arguments should be the filename and linenumber of
      the source file where the function is called. If this were C I would
      have used the __FILE__ and __LINE__ macros as:
      >
      log_msg(msg , __FILE__ , __LINE__)
      >
      Is there a way to emulate this behaviour in Python?
      >
      It's better in Python not to emulate that but to let the caller do the
      work:

      C:\junk>type caller_id.py
      import inspect

      def logger(msg):
      print "logger:", msg
      print "called from %s:%d" % inspect.stack()[1][1:3]
      print

      def client1():
      logger("one")

      def client2():
      logger("two")

      client1()
      client2()


      C:\junk>caller_ id.py
      logger: one
      called from C:\junk\caller_ id.py:9

      logger: two
      called from C:\junk\caller_ id.py:12

      If you care to search for __LINE__ in this newsgroup, there's a slight
      chance you might find a thread or two or twenty-two on the topic :-)

      I don't usually go for one-liners, especially ugly ones like
      "inspect.stack( )[1][1:3]" but it avoids the risk of hanging on to a
      reference to the frame object -- see the warning in the inspect docs.

      You can get the name of the calling function or method (and,
      indirectly, the method's class) if you want to log the whole dossier --
      details left as an exercise :-)

      HTH,
      John

      Comment

      • Joakim Hove

        #4
        Re: __LINE__ and __FILE__ functionality in Python?


        Maric Michaud <maric@aristote .infowrites:

        Sure, try :
        >
        In [46]: import inspect
        >
        In [47]: c=inspect.curre ntframe()
        >
        In [48]: c.f_lineno
        Out[48]: 1
        >
        In [49]: c.f_code.co_fil ename
        Out[49]: '<ipython console>'
        Thanks a lot - that was just what I wanted.


        Regards - Joakim


        --
        Joakim Hove
        hove AT ntnu.no /
        Tlf: +47 (73 5)9 34 27 / Stabburveien 18
        Fax: ............... .. / N-5231 Paradis
        http://www.ift.uib.no/~hove/ / 55 91 28 18 / 92 68 57 04

        Comment

        • Maric Michaud

          #5
          Re: __LINE__ and __FILE__ functionality in Python?

          Le dimanche 13 août 2006 14:18, John Machin a écrit :
          I don't usually go for one-liners, especially ugly ones like
          "inspect.stack( )[1][1:3]" but it avoids the risk of hanging on to a
          reference to the frame object -- see the warning in the inspect docs.
          Yes, my mistake, thanks for pointing this out, my suggestion should have been
          more accurate :

          import inspect

          try :
          c=inspect.curre ntframe()
          log_msg(c.f_lin eno, c.f_code.co_fil ename)
          finally :
          del c

          or shorter ;

          from inspect import currentframe
          log_msg(current frame().f_linen o,
          currentframe(). f_code.co_filen ame)


          --
          _____________

          Maric Michaud
          _____________

          Aristote - www.aristote.info
          3 place des tapis
          69004 Lyon
          Tel: +33 426 880 097

          Comment

          Working...