fumitoh / modelx

Use Python like a spreadsheet!
https://modelx.io
GNU Lesser General Public License v3.0
96 stars 21 forks source link

debugging cell calculations #14

Closed alebaran closed 4 years ago

alebaran commented 4 years ago

I didn't manage to find the way to debug cell calculations using normal debugger (pyCharm / Spyder). I'm using the following function to get into the space namespace:

def debug_space(space):
    for c in space.cells:
        globals()['%s' % c] = space.cells[c]

How do you do it?

fumitoh commented 4 years ago

Formula definitions of cells in recent versions of modelx are constructed from the source code of their underlying functions so they are not calling the underlying functions anymore, so putting breakpoints in those functions won't work. I could add some option to construct formulas from functions, not from their sources for debug.

alebaran commented 4 years ago

It actually works pretty well with the trick above. No need to change, I was just checking.

fumitoh commented 4 years ago

debug_space is just importing all cells in a space to the global namespace as the same names. What was your issue? How does debug_space solve it?

alebaran commented 4 years ago
model, space = mx.new_model(), mx.new_space()
@mx.defcells
def a():
    return b()
@mx.defcells
def b():
    return 1

With the code above I can debug a() calculation by checking in the Python console what the value of b() is. I can't do the same, if the space is created from module. debug_space allows the same Python console debugging for the space created from module. I guess the best would be to be able to set current space with mx.cur_space(space) function to allow Python console debugging for the space.