1

I am trying to create a function that will shelve all my variables from the caller function. In the following example: shelve.out would contain junk1, junk2, junk3 when the variables are loaded.

def SavePoint():
    print('shelving stuff')
    filename = 'shelve.out'
    my_shelf = shelve.open(filename, 'n')  # 'n' for new

    for key in dir():
        try:
            my_shelf[key] = globals()[key]
        except TypeError:
            #
            # __builtins__, my_shelf, and imported modules can not be shelved.
            #
            print('ERROR shelving: {0}'.format(key))
    my_shelf.close()

if __name__ == '__main__':
    junk1 = 'a'
    junk2 = 'b'
    junk3 = 'c'

    SavePoint()

My goal is to troubleshoot a error. I have a hard time replicating the error and the code is taking a long time to run. I know I can turn on "Python Exception Breakpoint" but the code change breaks something else so this would just allow me to skip this code that take a while to run. I am also trying to minimize the amount of code it takes. I know I can also paste in the function and that will work but this would just be one line change as opposed to 10. Not sure this can be done but I thought I would see if anyone had any ideas.

ttarter3
  • 83
  • 8
  • This question is answered [here](https://stackoverflow.com/questions/8857768/shelve-code-gives-keyerror) in a round about way -- see hyperlink – ttarter3 Apr 02 '18 at 21:53

0 Answers0