DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   Python (http://www.daniweb.com/forums/forum114.html)
-   -   Trouble with a certain Exception (http://www.daniweb.com/forums/thread122434.html)

Seagull One May 4th, 2008 7:24 pm
Trouble with a certain Exception
 
Hello everyone! I haven't programmed in quite a while and now that I'm getting back into the programming environment for my robotics project again, I'm a little stumped on something:

I'm getting this error message for an exception raised in my VAST python script for my robot. It goes like this:

Quote:

Traceback (most recent call last):
File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Users\Loren\Desktop\My Robots\Nina Verbal Raw Input.py", line 12, in <module>
from wxPython.wx import *
File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wxPython\__init__.py", line 15, in <module>
import _wx
File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wxPython\_wx.py", line 8, in <module>
from _misc import *
File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wxPython\_misc.py", line 456, in <module>
wxDateTime_GetNumberOfDaysinYear = wx._misc.DateTime_GetNumberOfDaysinYear
AttributeError: 'module' object has no attribute 'DateTime_GetNumberOfDaysinYear'

What stumps me is when I go to line 456, I only see this:

Quote:

"I'll get over it" : "speaker.Speak(random.choice(Health2))",

I went back to Health2, but didn't see anything on DateTime_GetNumberOfDaysinYear, there. I even did a find for the phrase in my scripts, but it says it can't find it. I brushed up on my python books, but can't isolate the problem.

If someone could give me any help with this, I'd be obliged. Thanks in advance.

jrcagle May 4th, 2008 9:58 pm
Re: Trouble with a certain Exception
 
My money's on a bug in speaker.Speak() OR on an improperly installed wxPython module. Here's why:

Your line chooses a random item from Health2, which is presumed to be a list.

If Health2 is not a list, then random.choice throws a TypeError, which is not your issue.

Further, random.choice() is a well-tested function that couldn't possibly throw the error you're getting. So ... that leaves speaker.Speak() as the source of the error.

Next task: identify the class of "speaker", and check out the code for its Speak() method.

It looks like from the first line of your Traceback that speaker.Speak() tries to import wxPython and do something fancy from there.

Here's how to test for a problem with your wxPython install:

From the command line of IDLE:

>>> import wx
>>> e = wx.DateTime()
>>> e.GetNumberOfDaysInYear(2008)
366

If you get the right result, then the bug's in Speak(). If not, then you should (re?)install wx.

Jeff

Seagull One May 4th, 2008 10:31 pm
Re: Trouble with a certain Exception
 
Hello again, Jeff!

I test the wxPython install like you said and got the result as you indicated, so its no wxPython.

I think I've isolated the problem to somewhere much earlier in my program:
Quote:

from wxPython.wx import *

I tried typing this in the command prompt and it gave back this result:

Quote:

from wxPython.wx import *
__main__:1: DeprecationWarning: The wxPython compatibility package is no longer automatically generated or actively maintained. Please switch to the wx package as soon as possible.
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wxPython\__init__.py", line 15, in <module>
import _wx
File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wxPython\_wx.py", line 8, in <module>
from _misc import *
File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wxPython\_misc.py", line 456, in <module>
wxDateTime_GetNumberOfDaysinYear = wx._misc.DateTime_GetNumberOfDaysinYear
AttributeError: 'module' object has no attribute 'DateTime_GetNumberOfDaysinYear'

If I understand correctly, the package I just installed isn't good anymore, so I need to switch to the "wx" package? Where can I find that?

Seagull One May 5th, 2008 6:33 pm
Re: Trouble with a certain Exception
 
Okay, I think I found the answer to that. All I had to do was change, say wxbitmap to wx.bitmap, in my script.

But now I'm getting something else. When I try running the script, I get this error message:

Quote:

Traceback (most recent call last):
File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Users\Loren\Desktop\My Robots\Nina Verbal Raw Input.py", line 75, in <module>
class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
TypeError: Error when calling the metaclass bases
cannot create 'NoneType' instances

I first thought this had something to do with the SAPI SDK 5.1 not being install on my New computer (my old laptop suffered a spill, so that's why I'm having trouble getting this script to work again on my new one). But apparently, after reinstalling it, I get the same message. Could it be that the python win32 extention isn't installed on my laptop? Because I thought I installed it...

Seagull One May 6th, 2008 8:19 pm
Re: Trouble with a certain Exception
 
I believe the bug is indeed, in speaker.Speak(). I tried retyping the script in the comman line to see step by step what was working and what was going through. The moment I finished typing in
Quote:

class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
def OnRecognition(self, StreamNumber, StreamPosition, RecognitionType, Result):
newResult = win32com.client.Dispatch(Result)
I got that
Quote:

TypeError: Error when calling the metaclass bases

I only have a rough idea of whats happening in the computer as far as coding is concerned, so I'm not sure how to fix this problem...

jrcagle May 8th, 2008 8:47 pm
Re: Trouble with a certain Exception
 
And I have no knowledge at all of the Voice Recognition module... :lol:

This error is saying something interesting:

Quote:

class ContextEvents(win32com.client.getevents("SAPI.SpSharedRecoContext")):
TypeError: Error when calling the metaclass bases
cannot create 'NoneType' instances


Apparently, it is defining a class that inherits from the result of the call to

win32com.client.getevents("SAPI.SpSharedRecoContext")

I've not heard of dynamic inheritance before, but it makes sense that Python could do it!

I was able to reproduce the bug like this:

class MyClass(eval(raw_input("Enter a Class: "))):
    pass
>>> Enter a Class: object
>>> m = MyClass()
>>> print m
<__main__.MyClass object at 0x00BFB250>

---

>>>
Enter a Class: str
>>> m = MyClass()
>>> print m

>>> m
''
>>>

---

>>> Enter a Class: None
>>> m = MyClass()
Traceback (most recent call last):
  File "C:/Python25/dyninherit.py", line 1, in <module>
    class MyClass(eval(raw_input("Enter a Class"))):
TypeError: Error when calling the metaclass bases
    cannot create 'NoneType' instances

So apparently, win32com.client.getevents("SAPI.SpSharedRecoContext") is returning None when it ought to return some class name.

Can you find anything out about that?

Jeff

Seagull One May 8th, 2008 11:07 pm
Re: Trouble with a certain Exception
 
Hi Jeff. I think my problem was I hadn't used the MakePy Windows speech Object Library on it, because it got rid of that error. Now I'm getting something else, much farther down the script:
Quote:

Traceback (most recent call last):
File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Users\Loren\Desktop\My Robots\Nina Verbal Raw Input.py", line 674, in <module>
app = MyApp(0)
File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 7836, in __init__
self._BootstrapApp()
File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 7433, in _BootstrapApp
return _core_.PyApp__BootstrapApp(*args, **kwargs)
File "C:\Users\Loren\Desktop\My Robots\Nina Verbal Raw Input.py", line 551, in OnInit
self.InitSpeech()
File "C:\Users\Loren\Desktop\My Robots\Nina Verbal Raw Input.py", line 527, in InitSpeech
self.turnedOn = true
NameError: global name 'true' is not defined

From what I understand, 'true' is a type of switch or property. It should work in any python script without importing or fancy defining or anything like that.

Here's the chunk of code that seems to be affected:

Quote:

def InitSpeech(self):
listener = win32com.client.Dispatch("SAPI.SpSharedRecognizer")
self.context = listener.CreateRecoContext()
self.grammar = self.context.CreateGrammar()
self.grammar.DictationSetState(0)
self.ListItemsRule = self.grammar.Rules.Add("ListItemsRule", constants.SRATopLevel + constants.SRADynamic, 0)
events = ContextEvents(self.context)
self.turnedOn = true
self.SetWords()

Its part of a class called "MyApp(wx.App):" which is extremely vast with the lists of phrases my robot responds to.

Any ideas? I think we're both sort of exploring this speech recognition python territory. I'll keep investigating on my own as well.

jrcagle May 9th, 2008 12:45 am
Re: Trouble with a certain Exception
 
'True', not 'true'.

It should turn purple when spelled correctly.

Seagull One May 9th, 2008 2:10 pm
Re: Trouble with a certain Exception
 
Oh, right. Thanks, Jeff! That solved that problem.

Now when I run the script, the windows speed recognition activates! Theres another exception that comes up though, but since the windows speech recognition comes up just before the exception, I know we're getting close.

Here's whats coming up now:

Quote:

Setting words - turned on
Traceback (most recent call last):
File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Users\Loren\Desktop\My Robots\Nina Verbal Raw Input.py", line 674, in <module>
app = MyApp(0)
File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 7836, in __init__
self._BootstrapApp()
File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 7433, in _BootstrapApp
return _core_.PyApp__BootstrapApp(*args, **kwargs)
File "C:\Users\Loren\Desktop\My Robots\Nina Verbal Raw Input.py", line 553, in OnInit
self.frame = wx.Frame(NULL, -1, "Speech tester", wx.Point(10,10), wx.Size(770,300))
NameError: global name 'NULL' is not defined

Here's the chunk of code that seems to be affected.

Quote:

def OnInit(self):
self.setItems()
self.InitSpeech()

self.frame = wx.Frame(NULL, -1, "Speech tester", wx.Point(10,10), wx.Size(770,300))
self.listBox = wx.ListBox(self.frame, self.LISTBOX_ID, wx.Point(10, 10), wx.Size(120, 200),
self.items.keys(), wx.LB_SINGLE)
self.addButton = wx.Button(self.frame, self.ADD_BUTTON_ID, "Add", wx.Point(10,230), wx.Size(50, 30))
self.deleteButton = wx.Button(self.frame, self.DELETE_BUTTON_ID, "Delete", wx.Point(80, 230), wx.Size(50, 30))
self.editor = wx.TextCtrl(self.frame, self.EDITOR_ID, "", wx.Point(140,10), wx.Size(600,200),
style=wx.SUNKEN_BORDER+wx.TE_MULTILINE+wx.TE_PROCESS_TAB)
self.testButton = wx.Button(self.frame, self.TEST_BUTTON_ID, "Test", wx.Point(140, 230), wx.Size(50, 30))

self.turnonButton = wx.Button(self.frame, self.TURNON_BUTTON_ID, "On", wx.Point(210, 230), wx.Size(50, 30))
self.turnoffButton = wx.Button(self.frame, self.TURNOFF_BUTTON_ID, "Off", wx.Point(280, 230), wx.Size(50, 30))

self.SetUpTaskbar()

EVT_LISTBOX(self, self.LISTBOX_ID, self.OnListBoxSelect)
EVT_BUTTON(self, self.ADD_BUTTON_ID, self.OnAddClick)
EVT_BUTTON(self, self.DELETE_BUTTON_ID, self.OnDeleteClick)
EVT_BUTTON(self, self.TEST_BUTTON_ID, self.OnTestClick)
EVT_BUTTON(self, self.TURNON_BUTTON_ID, self.OnTurnOnClick)
EVT_BUTTON(self, self.TURNOFF_BUTTON_ID, self.OnTurnOffClick)
EVT_TEXT(self, self.EDITOR_ID, self.OnTextEntered)

EVT_ICONIZE(self.frame, self.OnIconize)
EVT_CLOSE(self.frame, self.OnExitFrame)

self.listBox.SetSelection(0)
self.displayTextBox()

self.frame.Show(True)
self.SetTopWindow(self.frame)
return True

Once again, I'm not sure how that works. Isn't NULL an expression you can use without defining it? I think that might be typed incorrectly, like with 'true,' but I'm not sure how to fix it. You know what's going on? I'll hit the books in the meantime.

Loren

jrcagle May 9th, 2008 5:07 pm
Re: Trouble with a certain Exception
 
Yes, it's a bug that was clearly written by a C++ coder who has migrated to Python.

The first argument to wx.Frame() is its parent, which should be None in a typical case. In C++, the corresponding value would be NULL.

So if you change NULL to None, it oughta work (up to the next exception :) )

Jeff


All times are GMT -4. The time now is 8:41 pm.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC