Python Signal/Slot + QThred code analysis

Juan Christian juan0christian at gmail.com
Sun Nov 23 08:44:31 EST 2014


This is a continuation of my other topic "How to access Qt components
loaded from file", it was getting big and the focus changed completely, the
real question there was already answered, and we were talking about
signal/slot, QThred and other things.

So, I read on the web (didn't find books talking about QThred, Signal/Slot)
and I came up with this code:

Python 3.4.2
PySide 1.2.2

from PySide.QtCore import QObject, QThread, Signal
import requests
import bs4

class Outpost(QObject, QThread):
    received = Signal()

def __init__(self, uid, *args, **kwargs):
    super().__init__(*args, **kwargs)
    self.url = 'http://www.myurl.com/user/' + uid

def run(self):
    soup = bs4.BeautifulSoup(requests.get(self.url).text)

    with soup.select('div.stat_box div.value')[0].get_text().replace(',',
'') as a, soup.select('div.stat_box div.value')[1].get_text() as b:
        self.info_a = a
        self.info_b = b

    self.received.emit(self)

Is this code good?

I'll explain what I think this code does, when I was doing this: First I
created my custom signal, 'received'. Then I made my __init__ just to set a
uid.

Then, the run func, I read that's the one that will run when working with
threads. Here I set the soup and I get the page content using requests,
that's the part that will take more time.

Then I use bs4 to set my data, and yes, I know that getting data this way
isn't the best option, but I don't have other choice, they don't give APIs
for this stuff.

Later I emit my signal when I have everything set and I pass the entire
class with it so I can call the 'var.info_a' and 'bar.info_b' wherever I
want.

Did I think correctly, can I do anything better here? Many thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20141123/2e01ec5f/attachment.html>


More information about the Python-list mailing list