PyQt ProgressBar: Update progress from other threads

Categories: Python; Tagged with: ; @ May 14th, 2015 23:40

Issue 1: UI is no responding when the progress is running


Create new thread to perform the progress:
http://stackoverflow.com/questions/6783194/background-thread-with-qthread-in-pyqt

Issue 2: QThread: Destroyed while thread is still running

Try to keep a reference:
http://stackoverflow.com/questions/8359469/python-threading-thread-scopes-and-garbage-collection

Issue 3: QWidget::repaint: Recursive repaint detected


Use single to update progress:
http://stackoverflow.com/questions/9682376/progress-bar-with-pyqt

 

Example: UploadThread:

class UploadTaskQtThread(QtCore.QThread):
    tick = QtCore.pyqtSignal(int, name="upload_changed")

    def __init__(self, client, local_path, remote_path):
        QtCore.QThread.__init__(self)
        self.client = client
        self.local_path = local_path
        self.remote_path = remote_path
        
    def on_progress(self, progress):
        self.tick.emit(progress * 100)
        
    def run(self):
        self.client.upload_file(self.local_path, self.remote_path, self.on_progress)

Client:

    def upload(self, local_file_path, remote_path):
        self.upload_thread = UploadTaskQtThread(client, local_file_path, remote_path)
        self.upload_thread.start()
        self.upload_thread.tick.connect(ui.progressBar.setValue)

My first Python Class

Categories: Python; Tagged with: ; @ May 14th, 2014 23:24

User class, has one attribute ‘name’: 

#User.py
class User(object):
        def __init__(self, name):
                self.name=name

        def sayHi(self):
                print self.name, " say hi to you"


        def hello(self, *friends):
                f1, f2 = friends
                print "hello my friends: ", f1", and ", f2

Initialize User instance and call the methods:

>>> import User
>>> john = User.User("John")
>>> john.sayHi()
John  say hi to you
>>> john.hello("Luna", "Peter")
hello my friends:  Luna  and  Peter

I got confused, because I come from Java, why every method need a ‘self’? we use the instance invoke the method,  why we need to supply the instance itself?

Maybe they are static?  let’s try to invoke the method in ‘Static’ way:

>>> User.User.sayHi()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unbound method sayHi() must be called with User instance as first argument (got nothing instead)

Ok, how about this:

>>> User.User.sayHi(john)
John  say hi to you

It seems these instance method can be invoked both by Class and instance… that’s why  every method requires a ‘self’?

Ok, let’s add a static method:

        @staticmethod
        def staticHi():
                print "Static Hi"

Invoke it:

>>> User.User.staticHi()
Static Hi
>>> john.staticHi()
Static Hi

Static method can be called both by Class  and instance.

 

Some topic about ‘slef’:

http://stackoverflow.com/questions/2709821/python-self-explained

http://neopythonic.blogspot.sg/2008/10/why-explicit-self-has-to-stay.html

<<Learn Python the Hard Way>> Notes

Categories: Python; Tagged with: ; @ May 14th, 2014 21:04

by Zed A. Shaw

Stop reading my book,  I don’t write it for you. I wrote it for people who don;t already know everything.

Empty before you fill. you will have a hard time learning from someone with more knowledge if you already know everything;

Go learn Lisp, I hear people who know everything really like Lisp.

A programmer may try to get you to install Python 3 and learn that, Say, “When all the Python code on your computer is Python 3, then Ii’ll try to learn it”

What I discovered after this journey of learning ( about 20 programming languages) is that it’s not the languages that matter but what you do with them … which programming language you learn and use doesn’t matter. Do not get sucked into the religion surrounding programming languages, as that will only blind you to their true purpose of being your tool for doing interesting things.

Programming as a profession is only moderately interesting. It can be a good job, but you could make about the same money and be happier running a fast food joint.  You’re much better off using code as your secret weapon in another profession.

Of course, all this advice is pointless.

Finally, I’ll say that learning to create software changes you and makes you different – not better or worse, just different. you may find that people treat you harshly because you can create software, maybe using words like  “nerd”. maybe you’ll find that because you can dissect their logic, they hate arguing with you . you may even find that simply knowing how a computer working makes you annoying and weird to them.  to this, I have just one piece of advice: they can go to hell.  the world needs more weird people who know how things work and who love to figure it all out. when they treat you like this, just remember that this is your journey, not theirs. Being different is not a crime, and people who tell you it is are just jealous that you’ve picked up a skill they never in their wildest dreams could acquire.

After I typed all these funny staff, I found this:

http://learnpythonthehardway.org/book/advice.html

Crete PHP Cron Jobs in LunarPages Host Server

Categories: PHP; Tagged with: ; @ May 13th, 2014 23:15

Go to Cpanel > Cron Jobs, create new cron job.

Select / modify the scheduling, then add the command:

php /home/xx/public_html/job.php

or

/usr/local/cpanel/3rdparty/bin/php /home/xx/public_html/job.php

Monitoring Website using PHP file_get_contents and mail function

Categories: PHP; Tagged with: ; @ May 13th, 2014 22:20

As a lazy user, I want to get alerted when website goes down.

Solution:

	$homepage = file_get_contents('http://,monitoring--domain.com/');
        if(strlen($homepage) > 0) {
                echo "Got response, no action";
        }else {
                echo "No response from server, send out email...";
                $to      = '[email protected]';
                $subject = 'Weak up, you website is down!';
                $message = 'Weak up, you website is down!';
                $headers = 'From: [email protected]' . "\r\n" .
                'Reply-To: [email protected]' . "\r\n" .
                'X-Mailer: PHP/' . phpversion();
                mail($to, $subject, $message, $headers);
        }

Newer Posts <-> Older Posts



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.