0

I get an error:

AttributeError: 'Park_Stat_Thread' object has no attribute 'park_stat

when I run the script below. Any idea how to pass back the park_stat variable?

As you can see I use multi-threading to crawl a list of URLs from the web:

class Park_Stat_Thread(Thread):

    def __init__(self, thread_num):
        super().__init__()
        self.thread_num = thread_num

    def run(self):
        print('starting thread %s.' % self.thread_num)
        process_queue(self)
        print('exiting thread %s.' % self.thread_num)

    def process_queue(self):
        while True:
            try:
                x = my_queue.get(block=False)

            except queue.Empty:
                return

            else:
                parking_stat_getter(self,x)

            time.sleep(1)

    def parking_stat_getter(self,x):
        base = 'http://www.ahuzot.co.il/Parking/ParkingDetails/?ID={}'
        response = requests.get(base.format(self.thread_num))
        soup = BeautifulSoup(response.text, "lxml")

        try:

            self.park_stat = 0
            for img in soup.find_all('img' , attrs={'src': re.compile("ParkingIcons")}):
                soup_img = str(img)

                if 'male' in soup_img:
                    self.park_stat=1
                elif 'meat' in soup_img:
                    self.park_stat=2
                elif 'panui' in soup_img:
                    self.park_stat=3  

        except AttributeError:
          self.park_stat = np.nan

        return self.park_stat

def get_parkings_Stat(parking_id_lists):
    threads = [Park_Stat_Thread(t) for t in range(1, 8)]  

    for thread in threads:        
        thread.start()
    for thread in threads:
        thread.join()

    park_details = dict(zip(parking_id_lists, [thread.park_stat for thread in threads]))

    return park_details
5
  • I think the problem is that you need to include the park_stat property in the class initialisation too. I think it's not possible to create class properties on the go, but you can modify them on the go. Commented Mar 14, 2020 at 21:51
  • if I add it, then it will be asked as input when I call the thread Park_Stat_Thread(t), but its not an input rather the output. Commented Mar 15, 2020 at 12:47
  • Don't look at it as input-output, but rather like Age for a typical example of class People(). Every new born is initialised as person.Age=0 , but modified every year. Here, I think you could do the same, but you have to make sure that the "modification" is done already, before calling the getter for that property. Commented Mar 15, 2020 at 12:51
  • I added park_stat as property and it worked! THANKS! Commented Mar 15, 2020 at 16:08
  • please add it as an answer so I can mark it Commented Mar 15, 2020 at 16:09

1 Answer 1

1

You are missing a class attribute from the class constructor. You cannot declare a class attribute later, but you can set it to some dummy value.

class Park_Stat_Thread(Thread):

    def __init__(self, thread_num):
        super().__init__()
        self.thread_num = thread_num
        self.park_stat = 0


    def run(self):
        print('starting thread %s.' % self.thread_num)
        process_queue(self)
        print('exiting thread %s.' % self.thread_num)

    def process_queue(self):
        while True:
            try:
                x = my_queue.get(block=False)

            except queue.Empty:
                return

            else:
                parking_stat_getter(self,x)

            time.sleep(1)

    def parking_stat_getter(self,x):
        base = 'http://www.ahuzot.co.il/Parking/ParkingDetails/?ID={}'
        response = requests.get(base.format(self.thread_num))
        soup = BeautifulSoup(response.text, "lxml")

        try:

            self.park_stat = 0
            for img in soup.find_all('img' , attrs={'src': re.compile("ParkingIcons")}):
                soup_img = str(img)

                if 'male' in soup_img:
                    self.park_stat=1
                elif 'meat' in soup_img:
                    self.park_stat=2
                elif 'panui' in soup_img:
                    self.park_stat=3  

        except AttributeError:
          self.park_stat = np.nan

        return self.park_stat

def get_parkings_Stat(parking_id_lists):
    threads = [Park_Stat_Thread(t) for t in range(1, 8)]  

    for thread in threads:        
        thread.start()
    for thread in threads:
        thread.join()

    park_details = dict(zip(parking_id_lists, [thread.park_stat for thread in threads]))

    return park_details
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.