Solved

call module from  different class

Posted on 2011-09-15
9
372 Views
Last Modified: 2012-05-12
I want to call the populate module from the threading class ... How can I do that ? right now I have insertdata.populate and its not throwing and error but it's also not doing what I want




class InsertData(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(990,668),style=wx.DEFAULT_FRAME_STYLE|wx.WS_EX_CONTEXTHELP)
	self.Bind(wx.EVT_CLOSE, self.OnClose)
def populate(self):
        do something




class ThreadClass(threading.Thread,InsertData):
    def run(self):
          do_something
          once_done
       
	    self.repopulate()
    def repopulate(self):	           
	InsertData.populate
	print "done"
ThreadClass().start() 

Open in new window

0
Comment
Question by:dolamitejenkins
  • 4
  • 3
  • 2
9 Comments
 
LVL 16

Expert Comment

by:gelonida
ID: 36547365
Very first issue. is line 18,

which should be

InsertData.populate()

If this doesn't solve the issue I'd go for an approach without multiple inheritance.

create the InsertData intance within your thread and store it for example under self.insert_data.
If you have to pass args during construction time, then use the args / kwargs of the threading object.

http://docs.python.org/library/threading.html#thread-objects



Not knowing wxWidgets and not knowing, what you try to do in populate, and whether you also try to access the widgets from other threads I'd just like to mention, that
many graphical frameworks are rather sensitive if being combined with threading, thus they have very specific means (PyQT has for example signals), which you should use to avoid breaking the framework.

Often you are not allowed to add modify GUI elements from another thread tan the originating GUI thread.



0
 

Author Comment

by:dolamitejenkins
ID: 36547390
thanks ... I will let you know how it goes... i have to do some research on wxpython ... should I build my future programs with ptqt ?
0
 
LVL 16

Expert Comment

by:gelonida
ID: 36547490
WxWidgets and PyQt are rather popular and both good options.

I think both require certain measures to work well in a multithreaded setup.

Normally only one thread is allowed to create / destroy manipulate widgets and run the main loop.
other threads had to communicate via signals/slots (Qt) or pipes (Wx)

SW licensing might be one reason to pay attention with PyQt.
- http://www.riverbankcomputing.co.uk/software/pyqt/license
- http://www.wxwidgets.org/about/newlicen.htm

You might also look at PySide  (rather recent LGPL alternative to PyQt)
- http://en.wikipedia.org/wiki/PySide


0
 

Author Comment

by:dolamitejenkins
ID: 36551539
thanks
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 28

Accepted Solution

by:
pepr earned 250 total points
ID: 36551574
The InsertData.populate() will not work either.  It's because the populate() expects the self as argument.  The InsertData is the class.  However, the method expect the reference to the object -- i.e. to the instance of the InsertData class.  The object of the class must be created first.

The InsertData class is part of GUI with some functionality. It should be given a noun name -- say DataFrame.

You should probably better describe your intention and the related design decisions with plain words.

To add to gelonida's comment.  The InsertData is the class, and the populate is one of its attributes -- the name of its method.  Try the following simpler example in the interactive mode:

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Cls:
...   def method(self):
...     print 'the method called'
...
>>> Cls
<class __main__.Cls at 0x00000000022577C8>
>>> Cls.method
<unbound method Cls.method>
>>> obj = Cls()
>>> obj
<__main__.Cls instance at 0x000000000226EF48>
>>> obj.method
<bound method Cls.method of <__main__.Cls instance at 0x000000000226EF48>>
>>> obj.method()
the method called
>>>

Open in new window


The Cls.method is the same case as your InsertData.populate.  You can call also the unbound method of the class, but then you have to pass the reference to the object as the first argument -- it will be assigned to the internal self.

>>> Cls.method()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method method() must be called with Cls instance as first arg
ument (got nothing instead)
>>> Cls.method(obj)
the method called
>>>

Open in new window

0
 
LVL 16

Assisted Solution

by:gelonida
gelonida earned 250 total points
ID: 36552449
Pepr You're absolutely  right.

I was answering too quickly.

Should have been
 self.populate()
or InsertData.populate(self)

Knowing, that there's no populate method in threading.Thread, I'd probably go for
 self.populate()




0
 

Author Comment

by:dolamitejenkins
ID: 36552478
ok I will try it ... thanks to you both for your guidance
0
 
LVL 28

Expert Comment

by:pepr
ID: 36553561
It seems to me that you did not want to make your InsertData the base class of your thread class.  You probably wanted to pass the InsertData (bad name) instance (i.e. object of the class) to the thread object (say via __init__ method or somehow else).

You should think more about whether you really need a thread.  It is better to avoid them if threads are not neccessary.

Try to imagine the object personified or visualize it in some other way when thinking about how it all should work. Make the childish drawings (i.e. circles, squares, arrows... all informal).  They are powerful means to help our imagination.
0
 

Author Closing Comment

by:dolamitejenkins
ID: 36578405
thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now