Question

I can't comprehend why the worker function will not accept self.OpenDir object on the first line. It will print the directory! Any explanations are appreciated.

class MyThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)       
        dispatcher.connect(self.showFrame2, signal="show.mainframe3",
                     sender=dispatcher.Any)       

    def showFrame2(self, message, arg2=None, arg3=None):
        self.new_dbf = message
        self.RegRemove = arg2
        self.OpenDir = arg3 

    def run(self):
        """ worker """
        with open('E:\csv_sorted\Row2.dbf') as infile:
##        with open(self.OpenDir) as infile:  Attribute error!            
            print self.OpenDir            
            reader = csv.reader(infile)
            data = StringIO()
            writer = csv.writer(data)
        for line in csv.reader(self.new_dbf.splitlines()):
            row = line
            row_edit = re.sub(self.RegRemove,'', row[1])
            writer.writerow([row[0], row_edit])
            msg = data.getvalue()                
            wx.CallAfter(Publisher().sendMessage, "update", msg)
Was it helpful?

Solution

You are defining self.OpenDir inside of a function that isn't called in __init__. Thus it does not exist when run is called and you receive an AttributeError.

OTHER TIPS

Have a look at this:

    class thing:
        def __init__(self):
            pass

        def something(self):
            self.foo = "bar"


        def tryme(self):
            print self.foo

    a = thing()

    # a.something()

    a.tryme()

When you run it, you will get an attribute error, because when tryme() is called, something() hasn't been run, so there is no such thing as self.foo.

If you uncomment a.something() it will work.

This is the same problem your code probably has. You either need to make sure showFrame2() gets called before run(), or initialize self.OpenDir some other way before calling run().


(This is the same answer Mike gave, only more explicit. You probably should just upvote this one, and accept Mike's, if this turns out to be the right answer for you (or vice versa)).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top