Solved

call module from  different class

Posted on 2011-09-15
9
383 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:dolamitejenkins
ID: 36551539
thanks
0
 
LVL 29

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 29

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

789 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