Package XPyLIB :: Module dbg
[hide private]
[frames] | no frames]

Module dbg

source code

Simple debug module.

Used for fast debugging. You may set debug output into two different streams: outstream and fstream. Preferred assignment for streams is:

Function for file output starts with f letter. Initialization of streams (open file) is doing only if calling writing function and stream is not initialized. Before start debug output you can initialize streams you own objects which support write method. Then program exit autocalling shutdown function which close fstream if it was opened. If in program no debug output then streams are not created and file are not opened.

Also you may use indentation level by calling lvlup(), lvldown() and decorators level, wlevel and fwlevel. Indentation level used only in next functions: fwriteln() and writeln(). For tune indentation use levelbodychar, levelendchar, levelwidth.

Debug output example, see _debug):
   *** DEBUG START ***
   
   >456
   ..>tst1:test
   >CALL tst2()
   ..>tst2 start
   ..>tst1:from tst2
   ..>CALL tst3()
   ....>tst3
   ..>RET from tst3()
   ..>tst2 end
   >RET from tst2()
   >CALL tst2()
   ..>tst2 start
   ..>tst1:from tst2
   ..>CALL tst3()
   ....>tst3
   ..>RET from tst3()
   ..>tst2 end
   >RET from tst2()
   
   
   *** DEBUG END ***


Functions [hide private]
 
lvlup()
Increase indentation level.
source code
 
lvldown()
Decrease indentation level.
source code
 
lvlreset()
Reset indentation level to "0".
source code
 
level(fun)
Auto level decorator for functions.
source code
 
fwlevel(fun)
Auto level and name decorator for functions.
source code
 
wlevel(fun)
Auto level and name decorator for functions.
source code
 
shutdown()
Close opened file stream fstream.
source code
 
outstream_init()
Initialize output stream.
source code
 
fstream_init()
Initialize file output stream.
source code
None
fwrite(*arg)
Write concatenated arguments into outstream.
source code
None
write(*arg)
Write concatenated arguments into outstream.
source code
None
_writeln(fun, *arg)
Write concatenated arguments into outstream and append new line ('\n').
source code
None
fwriteln(*arg)
Write concatenated arguments into fstream and append new line ('\n').
source code
None
writeln(*arg)
Write concatenated arguments into outstream and append new line ('\n').
source code
tuple
_rwrite(fun, *arg)
As fwrite but return arguments tuple and for output use fun.
source code
tuple
frwrite(*arg)
As fwrite but return arguments tuple.
source code
tuple
rwrite(*arg)
As write but return arguments tuple.
source code
object or Exception
_eval(fun, expr, g={'__builtins__': {'ArithmeticError': <type 'exceptions.Arithme..., l={'__builtins__': {'ArithmeticError': <type 'exceptions.Arithme...)
Write expr, evaluate it and write result to output using fun.
source code
object or Exception
weval(expr, g={'__builtins__': {'ArithmeticError': <type 'exceptions.Arithme..., l={'__builtins__': {'ArithmeticError': <type 'exceptions.Arithme...)
Write expr, evaluate it and write result to outstream.
source code
object or Exception
fweval(expr, g={'__builtins__': {'ArithmeticError': <type 'exceptions.Arithme..., l={'__builtins__': {'ArithmeticError': <type 'exceptions.Arithme...)
Write expr, evaluate it and write result to fstream.
source code
 
_debug()
Test function.
source code
Variables [hide private]
stream(file-like object). outstream = None
Output stream, default is sys.stdout.
stream(file-like object). fstream = None
Output stream, default is "dbg.log" file into "~/.XPyLIB/logs" directory or sys.stderr if file is not available.
str levelbodychar = '.'
Character that fill indentation spaces.
str levelendchar = '>'
Character that insert before string output but after level indentation.
int levelwidth = 2
Number of indentation characters in one indentation level.
int levelerr = '*** LEVEL ERROR = '
Error message then level indentation is invalid thrn return from wrapped function.
str descfuncall = 'CALL '
Function call description.
str descfunret = 'RET from '
Function return description.
str desceval = 'EVAL: '
String that prepend to eval expression.
str descevalrez = ' ER='
String that prepend to eval resultat.
str descevalexc = ' EXCEPTION:'
String that prepend to eval exception.
str descevalln = ' '
String that added at end of eval output.
int _level = 0
Indentation level.

Imports: sys, atexit, os, functools.wraps


Function Details [hide private]

level(fun)

source code 

Auto level decorator for functions.

Call lvlup() befor function call and lvldown() after wrapped function return.
Parameters:
  • fun (func) - Wrapped function.

fwlevel(fun)

source code 

Auto level and name decorator for functions.

Write first 'Call "fun.__name__"' using fwriteln(). Also as level(). If level error tested then write '*** DBG LEVEL ERROR = different' where 'different' is level different. Write last 'Return from "fun.__name__"' using fwriteln().
Parameters:
  • fun (func) - Wrapped function.

wlevel(fun)

source code 

Auto level and name decorator for functions.

Write first 'Call "fun.__name__"' using writeln(). Also as level(). If level error tested then write '*** DBG LEVEL ERROR = different' where 'different' is level different. Write last 'Return from "fun.__name__"' using writeln().
Parameters:
  • fun (func) - Wrapped function.

fstream_init()

source code 

Initialize file output stream.

Default is "dbg.log" file into "~/.XPyLIB/logs" directory or sys.stderr if file is not available. Where "~" is a user home directory. Path autocreate if need. Mode of dirs is 0660.

fwrite(*arg)

source code 
Write concatenated arguments into outstream.
Parameters:
  • arg (*object) - Object to output. All objects convert to string str() function.
Returns: None
None.

write(*arg)

source code 
Write concatenated arguments into outstream.
Parameters:
  • arg (*object) - Object to output. All objects convert to string str() function.
Returns: None
None.

_writeln(fun, *arg)

source code 
Write concatenated arguments into outstream and append new line ('\n'). For output used fun.
Parameters:
  • fun (func) - Function for output (write or fwrite for example).
  • arg (*object) - Object to output. All objects convert to string str() function.
Returns: None
None.

Note: For work this function will be call fwrite function.

fwriteln(*arg)

source code 
Write concatenated arguments into fstream and append new line ('\n').
Parameters:
  • arg (*object) - Object to output. All objects convert to string str() function.
Returns: None
None.

Note: For work this function will be used fwrite function.

writeln(*arg)

source code 
Write concatenated arguments into outstream and append new line ('\n').
Parameters:
  • arg (*object) - Object to output. All objects convert to string str() function.
Returns: None
None.

Note: For work this function will be used write function.

_rwrite(fun, *arg)

source code 
As fwrite but return arguments tuple and for output use fun.
Parameters:
  • fun (func) - Function for output (write or fwrite for example).
  • arg (*object) - Object to output. All objects convert to string str() function.
Returns: tuple
Arguments tuple.

frwrite(*arg)

source code 
As fwrite but return arguments tuple.
Parameters:
  • arg (*object) - Object to output. All objects convert to string str() function.
Returns: tuple
Arguments tuple.

rwrite(*arg)

source code 
As write but return arguments tuple.
Parameters:
  • arg (*object) - Object to output. All objects convert to string str() function.
Returns: tuple
Arguments tuple.

_eval(fun, expr, g={'__builtins__': {'ArithmeticError': <type 'exceptions.Arithme..., l={'__builtins__': {'ArithmeticError': <type 'exceptions.Arithme...)

source code 

Write expr, evaluate it and write result to output using fun.

Write algorithm:

Then write this function you must get globals() and locals() if you using global or local scope variables into exprwssion.

For example see weval.
Parameters:
  • fun (func) - Function for output (write or fwrite for example).
  • expr (str) - Expression string.
  • g (dict = globals()) - Global variables.
  • l (dict = locals()) - Local variables.
Returns: object or Exception
Result of evaluation or Exception.

weval(expr, g={'__builtins__': {'ArithmeticError': <type 'exceptions.Arithme..., l={'__builtins__': {'ArithmeticError': <type 'exceptions.Arithme...)

source code 

Write expr, evaluate it and write result to outstream.

Write algorithm:

Then write this function you must get globals() and locals() if you using global or local scope variables into exprwssion.

For example: dbg.weval('dir(a)', globals(), locals())
Parameters:
  • expr (str) - Expression string.
  • g (dict = globals()) - Global variables.
  • l (dict = locals()) - Local variables.
Returns: object or Exception
Result of evaluation or Exception.

fweval(expr, g={'__builtins__': {'ArithmeticError': <type 'exceptions.Arithme..., l={'__builtins__': {'ArithmeticError': <type 'exceptions.Arithme...)

source code 

Write expr, evaluate it and write result to fstream.

Write algorithm:

Then write this function you must get globals() and locals() if you using global or local scope variables into exprwssion.

For example: dbg.feval('dir(a)', globals(), locals())
Parameters:
  • expr (str) - Expression string.
  • g (dict = globals()) - Global variables.
  • l (dict = locals()) - Local variables.
Returns: object or Exception
Result of evaluation or Exception.

_debug()

source code 

Test function.

Output to outstream is a next:
   *** DEBUG START ***
   
   >456
   ..>tst1:test
   >CALL tst2()
   ..>tst2 start
   ..>tst1:from tst2
   ..>CALL tst3()
   ....>tst3
   ..>RET from tst3()
   ..>tst2 end
   >RET from tst2()
   >CALL tst2()
   ..>tst2 start
   ..>tst1:from tst2
   ..>CALL tst3()
   ....>tst3
   ..>RET from tst3()
   ..>tst2 end
   >RET from tst2()
   arg1arg2>
   rwrite return: ('arg1', 'arg2')
   >EVAL: "str(5+6)"
   >  ER="11"
   > 
   >
   weval return: 11
   >CALL tst4()
   >RET from tst4()
   >Exception OK
   
   
   *** DEBUG END ***

Variables Details [hide private]

fstream

Output stream, default is "dbg.log" file into "~/.XPyLIB/logs" directory or sys.stderr if file is not available. This stream autoclosed then exit from application (used atexit module).
Type:
stream(file-like object).
Value:
None