Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 392
  • Last Modified:

call module from different class

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
dolamitejenkins
Asked:
dolamitejenkins
  • 4
  • 3
  • 2
2 Solutions
 
gelonidaCommented:
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
 
dolamitejenkinsAuthor Commented:
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
 
gelonidaCommented:
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
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
dolamitejenkinsAuthor Commented:
thanks
0
 
peprCommented:
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
 
gelonidaCommented:
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
 
dolamitejenkinsAuthor Commented:
ok I will try it ... thanks to you both for your guidance
0
 
peprCommented:
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
 
dolamitejenkinsAuthor Commented:
thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now