Python Forum

Full Version: NameError: global name 'PyAssertionError' is not defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How may I handle this error message?

The PyAssertionError appears when I try to set values into a grid:
Error:
Traceback (most recent call last):   File "...code01d.py", line 1097, in <module>     mainframe = Mainframe(None, -1, "Mainframe")   File "....code01d.py", line 372, in __init__     self.datenpanel = Datenpanel(self, wx.ID_ANY)   File "...code01d.py", line 180, in __init__     gridcontent = AZD["tiro"])   File "...code01d.py", line 287, in __init__     self.grid.SetCellValue(1, dyn, dystri)   File "...wx-2.8-msw-unicode\wx\grid.py", line 1880, in SetCellValue     return _grid.Grid_SetCellValue(*args, **kwargs) PyAssertionError: C++ assertion "(row < GetNumberRows()) && (col < GetNumberCols())" failed at ..\..\src\generic\grid.cpp(3516) in wxGridStringTable::SetValue(): invalid row or column index in wxGridStringTable
So I tried to make a try / except statement:

                    try:
                        self.grid.SetCellValue(1, dyn, dystri)
                    except PyAssertionError:
                        print "wrong row or column"


Then I get another error message:

Error:
Traceback (most recent call last):   File "...code01d.py", line 1096, in <module>     mainframe = Mainframe(None, -1, "Mainframe")   File "...code01d.py", line 371, in __init__     self.datenpanel = Datenpanel(self, wx.ID_ANY)   File "...code01d.py", line 180, in __init__     gridcontent = AZD["tiro"])   File "...code01d.py", line 289, in __init__     except PyAssertionError: NameError: global name 'PyAssertionError' is not defined
But I got it a moment before as error name... how is this possible?
And from where can I import this error so that it can be handled by try / except?
I'm not a wx programmer, but use tkinter a lot, so the answer I give here may not apply

the error indicates that the value of dyn (which I assume is the column number)  is out or range.
I am assuming that row 1 is valid (but may not be if you haven't used row 0)
Error:
[code]PyAssertionError: C++ assertion "(row < GetNumberRows()) && (col < GetNumberCols())" failed at ..\..\src\generic\grid.cpp(3516) in wxGridStringTable::SetValue(): invalid row or column index in wxGridStringTable[/code]
Try printing out the value of dyn to see what it contains
As mentioned in your previous thread, show a small runnable snippet of code that shows the error.
(Jan-27-2017, 09:38 PM)Larz60+ Wrote: [ -> ]the error indicates that the value of dyn (which I assume is the column number)  is out or range.

Yes, that's the case, but it's not my question.
I know where the PyAssertionError comes from. But where does the NameError comes from?

And does it really need a snippet for a (basically theoretical) question why the own errors are not known to python and how to handle this? But I'll prepare one. May take some hours, then I'll send it.
Ok I was thinking you wanted help with fixing the error.
PyAssertionError is not a built in python error its a wx python specific error, so you need to put wx. infront of it.
>>> import wx
>>> wx.PyAssertionError
<class 'wx._core.wxAssertionError'>
Thanks a lot, Yoritz!

With this, it works :-) .
( I wish the wypython docs would give hints to those kind of things, but I can search and search and search and never come across things like that... *sigh*)

And, well, as I have spend (wasted) time in it, the code... if ever somebody would be interested how to get such an error in an easy way.

import wx, wx.grid

class Teilgridpanel(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id,
                         pos = wx.DefaultPosition,
                         size = wx.DefaultSize,
                         style = wx.TAB_TRAVERSAL|wx.NO_BORDER)

        self.gridsize = gridsize = (1, 5)

        self.grid = wx.grid.Grid(self, style=wx.BORDER_SUNKEN)
        self.grid.CreateGrid(gridsize[0], gridsize[1])
        for dyn in range(6):
            dystri = "x"

            try:
                self.grid.SetCellValue(0, dyn, dystri)
            except PyAssertionError:
                print "wrong row or column"



class Mainframe(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)

        # und das Datenpanel in seinem BoxSizer
        self.datenpanel = Teilgridpanel(self, wx.ID_ANY)

        self.dpbox = dpbox = wx.BoxSizer(wx.HORIZONTAL) 
        dpbox.Add(self.datenpanel, 1, wx.EXPAND)

        hbox = wx.BoxSizer(wx.HORIZONTAL)
        hbox.Add(dpbox, 1, wx.EXPAND)
        self.SetSizerAndFit(hbox)


if __name__ == "__main__": 

    app = wx.App(0)
    
    mainframe = Mainframe(None, -1, "Mainframe")   
    app.MainLoop()