easy_install Twisted: unable to find vcvarsall.bat

Error "unable to find vcvarsall.bat" generally means that Visual C++ need be installed.

But for installing Twisted we get another option: Twisted windows installer, please check here http://twistedmatrix.com/trac/wiki/Downloads


After installing it, if you try to "import twisted", you will get ImportError: Twisted requires zope.interfaces: no module named zope.interface.

We can install this missed module by using easy_install:

easy_install zope.interface

pygtk get window hwnd

Searching "pygtk get window hwnd" on Google for a while, I found there is not much help in top results. So I decide to write this short article.

gtk.Window has a property called "window", it's almost same level as Window in winapi.

Running above code will get following output:

<gtk.gdk.Window object at 0x2713be8 (GdkWindow at 0x1d9e160)>

Then we can get the hwnd by retrieving "handle" property of this gtk.gdk.Window object


python windll.kernel32.CreateProcessA returns 0 (FALSE)

Above simple code didn't work in the latest version of my program, but it works well in previous version.

After reading documentation from MSDN, I got that CreateProcessA will return TRUE(1) if execution succeed, and return FALSE(0) if failed. Then I put "print ret" right after that line of code, exactly the return value is 0 (FALSE).

But how to get more details about the error? We can use GetLastError() function.

And after calling GetLastError(), it returns 2.

By looking up this error code page at MSDN, we can get the return value is actually ERROR_FILE_NOT_FOUND, meaning the system cannot find the file specified. But after printing the file path to console and checking the file path twice, I found the file existed actually.


Searching on google for some time, I found this post from stackoverflow

This guy met same problem: the executable file exists but calling CreateProcessA failed. The answer is use CreateProcessA with ANSI string, and use CreateProcessW with Unicode string. Then I tried CreateProcessW instead of CreateProcessA and it works perfectly!


Actually in previous version of the application, the executable path is read only from configuration file. But in the latest version, it can be loaded from registry key value, using _winreg.QueryValueEx. And this bug is introduced as well. So, is registry key value Unicode? or QueryValueEx returns an Unicode string?

Reading this documentation, we know that REG_SZ key value can either be ANSI or Unicode, it's depending on what function we use. And this bug post tells us that QueryValueEx will convert REG_SZ to unicode automatically, so finally we found where problem is.


PySide downloading file with progress bar

In this tutorial, we will download a file by HTTP and display the downloading progress by progress bar. The final screenshot is like this:



Downloading File

To display the downloading progress, we need know file size and currently downloaded bytes size.

So how to get the size of file need download? If you're familiar with HTTP, it will go easy. When downloading a file, first client will send a HTTP request to server for the file URL, then the server will send back a HTTP response to client. If requested URL is valid, the response body will be the file content.In the response header, there's a field called "Content-Length", which is used to indicate size of the response body. Since response body here is the file content, so this "Content-Length" is what we're looking for.

First we need use urllib2.urlopen() to open an url, this method returns a response object. Chain method info().get_headers() of response object can fetch the response header.So we can get file size like this:

And next, we need know how many bytes we have downloaded.
the read(n) method of response object will receive n bytes of data from server. Actually read() can be called with no parameter, then it will receive all data sent by server. But calling read() method like that will let urllib2 module to handle the whole transferring progress. we cannot know how many bytes have been downloaded until downloading is finished.

To get currently downloaded bytes size, we need download fixed size of bytes everytime and update the downloaded bytes size after that block of bytes are downloaded. Then continue downloading until all data is received. You got the idea? so lets implement it.


Display Progress Bar

Now we need QProgressBar to display our downloading progress. set_value() method of QProgressBar is used to set value of progress bar. The default value range is 0-100. and you can modify them by using setMinimum() and setMaximum() of QProgressBar.
Following code will create a window and put a progress bar in it. a setProgress() method is provided to set the progress bar value, so the downloading thread can set progress bar value by calling this method.



We will need put downloading part in a separate thread, otherwise it will block UI.

So here's the complete source code


Display Progress Bar in Console using Python

Have you used any command line program which represents its progress by displaying a dynamic progress bar in console? Think about Git, a popular version control system, when using git clone to fetch a repository, you will find that it's updating the downloading progress all the time.


The first program I wrote in my life is "Hello world" (maybe everyone is same as me, lol). I found that after "Hello world" text is outputted to console, the cursor moves to the end of line. Since then, I thought once text is outputted to console, it cannot be updated anymore. But later I found such dynamic progress bar (see above image), it's really amazing!


But how can we make one by our own hands? The key is to use the control character "\r", it's called Carriage Return.

Carriage Return is used to reset cursor's position to the beginning of line. Imagine we print "Hello" first, then we use Carriage Return to reset cursor's position to beginning of line, and print "world" next. What we will see in console is "world" replaced "Hello". To implement our thoughts, we wrote following code:

You may tell me running above code only display "world" in the console, and most importantly you didn't see that "world" is replacing "Hello". Well, actually that happens,  just because the progress is too fast, your eyes cannot capture it:)

What we need do is let program blocks for some time after "Hello" is printed on console. time.sleep() function will make current thread sleep for specified number of seconds. So the improved code is following:

Now you can see "world" is replacing "Hello". You may ask me why not use "print" statement? Because print will send newline to console, we will not use it.


Next is the real progress bar. Progress bar is consisted of a bar which is increasing by time and a value indicates the progress. So let's focus on the "bar" first

Combining it with progress "value", we got a progress bar