Reputation: 456
Is there a way to debug a code in parts? Meaning, I wish to debug the code until a certain point, save the variables state and continue to debug from that point on. Thanks
Upvotes: 16
Views: 609
Reputation: 640
With its debug function, Pycharm offers a fantastic opportunity to see the properties of certain variables if the breakpoint has been set accordingly.
Apart from that, Python itself offers an amazing way to serialize and de-serialize object structures with its built-in feature pickle (Pickle documentation).
The pickle.dump(VARIABLE) command can be used to dump variables at a certain state into a file or to be printed.
Sometimes I'm using pickle f.e. to dump a response variable into a file for later being used.
import pickle
import requests
r = requests.get('https://www.strava.com/api/v3/athlete')
#
# with open('assets/requests_test.pickle', 'wb') as write:
# pickle.dump(r.json(), write)
With that you're able to open this file manually or load it with pickle.load (VARIABLE) later in your code to do something useful with it.
Upvotes: 1