MCPcopy Create free account
hub / github.com/ActiveState/code / LN

Function LN

recipes/Python/279155_linenumpy/recipe-279155.py:5–38  ·  view source on GitHub ↗

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.

(*args, **kwargs)

Source from the content-addressed store, hash-verified

3import traceback, textwrap
4
5def LN(*args, **kwargs):
6 """Prints a line number and some text.
7
8 A variable number of positional arguments are allowed. If
9 LN(obj0, obj1, obj2)
10 is called, the text part of the output looks like the output from
11 print obj0, obj1, obj2
12 The optional keyword "wrap" causes the message to be line-wrapped. The
13 argument to "wrap" should be "1" or "True". "name" is another optional
14 keyword parameter. This is best explained by an example:
15 from linenum import LN
16 def fun1():
17 print LN('error', 'is', 'here')
18 def fun2():
19 print LN('error', 'is', 'here', name='mess')
20 fun1()
21 fun2()
22 The output is:
23 L3 fun1: error is here
24 L5 mess: error is here
25 """
26 stack = traceback.extract_stack()
27 a, b, c, d = stack[-2]
28 out = []
29 for obj in args:
30 out.append(str(obj))
31 text = ' '.join(out)
32 if 'name' in kwargs:
33 text = 'L%s %s: %s' % (b, kwargs['name'], text)
34 else:
35 text = 'L%s %s: %s' % (b, c, text)
36 if 'wrap' in kwargs and kwargs['wrap']:
37 text = textwrap.fill(text)
38 return text
39
40#=====================================================
41# LNtest.py

Callers 1

functionFunction · 0.90

Calls 4

strFunction · 0.85
fillMethod · 0.80
appendMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected