Welcome, guest | Sign In | My Account | Store | Cart

Prints the current line number, function name and some text. Useful for debugging.

Python, 54 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# linenum.py

import traceback, textwrap

def LN(*args, **kwargs):
    """Prints a line number and some text.

    A variable number of positional arguments are allowed. If
        LN(obj0, obj1, obj2)
    is called, the text part of the output looks like the output from
        print obj0, obj1, obj2
    The optional keyword "wrap" causes the message to be line-wrapped. The
    argument to "wrap" should be "1" or "True". "name" is another optional
    keyword parameter. This is best explained by an example:
        from linenum import LN
        def fun1():
            print LN('error', 'is', 'here')
        def fun2():
            print LN('error',  'is', 'here', name='mess')
        fun1()
        fun2()
    The output is:
        L3 fun1: error is here
        L5 mess: error is here
    """
    stack = traceback.extract_stack()
    a, b, c, d = stack[-2]
    out = []
    for obj in args:
        out.append(str(obj))
    text = ' '.join(out)
    if 'name' in kwargs:
        text = 'L%s %s: %s' % (b, kwargs['name'], text)
    else:
        text = 'L%s %s: %s' % (b, c, text)
    if 'wrap' in kwargs and kwargs['wrap']:
        text = textwrap.fill(text)
    return text

#=====================================================
# LNtest.py

#! /usr/bin/env python

from linenum import LN

def function():
    print LN()
    print LN('print', 'me')
    print LN('abc', name='AName')
    print LN('When the name variable is of the form ' \
      'package.module, normally, the top-level package', wrap=1)

function()

"linenum" is useful for debugging medium sized programs that don't need a full scale bebugger. When "LNtest.py", above, is executed the output is:

L6 function: L7 function: print me L8 AName: abc L10 function: When the name variable is of the form package.module, normally, the top-level package