__LINE__ and __FILE__ functionality in Python?

Discussion in 'Python' started by Joakim Hove, Aug 13, 2006.

  1. Joakim Hove

    Joakim Hove Guest

    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
     
    Joakim Hove, Aug 13, 2006
    #1
    1. Advertisements

  2. Le dimanche 13 août 2006 13:31, Joakim Hove a écrit :
    Sure, try :

    In [46]: import inspect

    In [47]: c=inspect.currentframe()

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

    In [49]: c.f_code.co_filename
    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
     
    Maric Michaud, Aug 13, 2006
    #2
    1. Advertisements

  3. Joakim Hove

    John Machin Guest

    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
     
    John Machin, Aug 13, 2006
    #3
  4. Joakim Hove

    Joakim Hove Guest

    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
     
    Joakim Hove, Aug 13, 2006
    #4
  5. Le dimanche 13 août 2006 14:18, John Machin a écrit :
    Yes, my mistake, thanks for pointing this out, my suggestion should have been
    more accurate :

    import inspect

    try :
    c=inspect.currentframe()
    log_msg(c.f_lineno, c.f_code.co_filename)
    finally :
    del c

    or shorter ;

    from inspect import currentframe
    log_msg(currentframe().f_lineno,
    currentframe().f_code.co_filename)


    --
    _____________

    Maric Michaud
    _____________

    Aristote - www.aristote.info
    3 place des tapis
    69004 Lyon
    Tel: +33 426 880 097
     
    Maric Michaud, Aug 13, 2006
    #5
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.