Visual Components' Community

New Thinking for Factory Simulation
Welcome to Visual Components' Community Sign in | Help
in Search

Wrap functions within other functions to get some deep debugging info

Last post pe, huhti 4 2008 16:10 by Janne. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • pe, huhti 4 2008 16:10

    • Janne
    • Top 10 Contributor
    • Joined on pe, helmi 22 2008
    • Visual Components HQ
    • Posts 21

    Wrap functions within other functions to get some deep debugging info

    Sometimes its hard to see whats going on during the debug process, then the traceback module can help you out. It would be useful if you could monitor individual function calls too. Here i demonstrate how a simple function can be used to wrap around a function and give some meaningful feedback on what function call was issued and what it was issued with.

    The downside is that this can slow down the process immensely and produce a lot of data so a easy way to switch it off is needed

    A quick sample follows:

    from vcScript import *
    
    # make a simple global switch for easy switching
    DEBUG=True
    
    def doDebug(fn):
        import traceback
        def new(*args):
             try:
               a=traceback.extract_stack()
               print a[0][0]+" line "+repr(a[0][1])+ ": "+ fn.__name__+repr(args)+" returns "+repr(fn(*args))
               return fn(*args)
             except: 
               pass
        #if debug is on lets wrap the function else leave it be
        if DEBUG:
          return new
        else:
          return fn
    
    # demontsration
    def dummy(aa,bb):  
       return aa+bb+1
    
    # now redefine it to get the wrap around
    dummy=doDebug(dummy)
    
    #even our api commands can be surrounded
    getComponent=doDebug(getComponent)
    
    # and finally some test that runs 
    # when you compile to demo the effect
    getComponent()
    dummy(10,2)
    
    Filed under: , ,
Page 1 of 1 (1 items)