Is there a way to get a local variable from a running function?

Desperate. Let's say we have the following:

def main():
 ALotOFCode
 list1 = []
 list2 = []
 while condition:
  # a lot of times where raw_input is used in this loop
  # e.g. 
  x = raw_input('lots of steps to compute x')
  y = raw_input('lots of steps to compute y')  
  list1 = list1.append(x)
  list2 = list2.append(y)
  stream.write({'x':list1,'y':list2}) #send new data point to plot.ly via raspberry pi

      

I don't know what happened. But my plot in plot.ly is gone. Removed completely. I fiddled with what I had on the plot so far on my PC and then kept on entering data and building the plot from the raspberry pi. I could see that the plot was built without problems. Then I went back to my computer and updated. The plot and all data are gone. Returned to the raspberries. Already synced. Gone.

I know that this list1 and list2, now as I write this, contain all the data I need to rewrite my plot. But is there anyway to access it and save it to a file? My python shell is currently expecting input via raw_input, so I cannot use the shell. Is there any way that I can hook up to the variables currently within the program? Obviously, after the program finishes, local variables are deleted.

Of course, if history is saved for each plot on plot.ly, that would help, but I can't find any recovery to the previous state.

Refresh . So, thank goodness I saved the results of some of my intermediate steps for the file. I actually gave up trying to access the variable and instead tried to create list1

and list2

through a new script on the fly. At 8:30 am I received my data, and at 10 am my supervisor looked at him with joy, not wiser. This question remains open, although so far no one has given a clear answer or explained that access to list1

and list2

directly is not possible. (I looked through every suggestion in the comments but couldn't find anything that would provide an answer)

Obligatory final comment: My faith in Plot.ly has been shattered, at least for a while.

+3


source to share


1 answer


To be able to "read" internal variables for debugging purposes, I see the following ideas:

  • create a log file with a line for every variable change and before every lock function. Even if the magazine is huge. Then do a raspberry-tailed log (new console or new remote connection).

  • Convert the variables to globals and add some code that prints all variables to standard output when you press a special key like Ctrl + C, which breaks almost everything. If converting variables to global is not possible (due to nested calls reasons, for example), create new variables that contain the last known values.

Ctrl + C processing can be done like this (from here ):



#!/usr/bin/env python
import signal
import sys
def signal_handler(signal, frame):
        print('You pressed Ctrl+C!')
        sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C')
signal.pause()

      

As you can see, my options involve modifying the code to make it frequent or requested.

0


source







All Articles