jeudi 13 septembre 2018

How can I track the values of a local variable in Python?

My algorithm loops over seconds of data. For each second-worth-of-data, I return a value, that is the result of a fairly involved computation involving multiple submodules and subclasses. Some of these classes are re-initialized each second.

For debugging purposes, a few times I have been in the situation that I have wanted to plot the value of a certain local variable in one of these classes over time. Something external would have to serialize and log the values, because the class exists for only a second. It has been a different local variable each time.

How can I do achieve my aim properly in terms of software design, without it taking me hours every time and without writing more than a line or two of new code each time I want to do this?

Ideally, one solution I have considered would be to have something like a global IO stream, or something like that, that I would "just be around" without having to initialize each class with it, so I could just insert some sort of MySerializer << [mylocalvariable, timestamp] command at any point in the code, and then when the run finished I could check if MySerializer is empty and if not I could plot what what is in it... or something like that. Better still if I could do this for multiple local variables in different classes. Would this solution be good? How could I do this?

Or to be able to do this in an aspect-oriented way, with some outside object "looking at the code" without changing it, building a buffer of values of that local variable, and spitting them out to a plot in the end. How might I do this?

Is there a better solution that either of these? Which design patterns suit this situation?

What I have done is in the past is to return that local variable to whoever holds the function, who then in turn has to return that value it has received, and so on and so forth, all the way up to the top. This is a huge mess and has to be written and deleted each time.

Aucun commentaire:

Enregistrer un commentaire