Tuesday, June 30, 2009

How to unload a python extension dll

I haven't found the answer. But I would like to put here
as a place holder. Also hoping if an expert can comment
on it. :)

Basically, the problem is: I have a python extension
written in C/C++. As a python module, I import it
in my interactive shell (IPython indeed). I modified
the C/C++ code, rebuild the extension, and want to
test it out in my python shell. But even I deleted the
module from the interactive shell namespace, the python
process is still holding a handle to the DLL (pyd). I
have to kill the python process and restart the testing
from beginning again. It will be nice if I can ask python
VM to release the handle of the pyd module and re-import
the module again.

Can I do it?

Sunday, June 28, 2009

How to find out system information on Window

Sometimes, we need to know the properties of the OS we are running on,
such information of interests include, page size, etc. On Linux this
is quite easy to get, because Linux has the proc file system to export
kernel information to user land as a file systems. On Windows I haven't
known a good and easy way. But Win32 API has a GetSystemInfo call,
which fulfills most of the need. Below is the sample code to print out
the page size of Windows.

#include <stdio.h>
#include <windows.h>

int main() {
SYSTEM_INFO si;
GetSystemInfo(&si);

printf("The page size for this system is %u bytes.\n", si.dwPageSize);

return 0;
}

Thursday, June 25, 2009

GVim diff mode integration with svn on windows


How to use GVim diff mode as the diff viewer for svn
has been a research task for me for a long time.
Today, finally, I got a chance to devote some time
to set it up once and for all. On Windows, I use
two svn client. The GUI Tortoise svn client, and the
command line CollabNet client.
For the GUI client, it is quite easy to set up. Fire up the Tortoise svn
settings diaglog. Change the Diff Viewer setting to where your Windows GVim exe
is. Put the command line swith '-d' following the exe location. That is it.
Mine is like this:
C:\Program Files\Vim\vim72\gvim.exe -s "C:\Program Files\Vim\vimfiles\scripts\vimdiffSize.vim" -d %base %mine

Why I have another '-s' swith with argument? That's because
I want the diff window to be big and the two diff window split
equally. There may be other ways to do it. But mine way is giving
a vim command script to do it. The content of the command script
is like this:

:winsize 300 100^M
^W=


The '^M' and '^W' is one character, Control-M and Control-W. How to
enter it? In Vim type: Ctrl-V Ctrl-M and Ctrl-V Ctrl-W.

How about on the command line. svn has a config file. On Windows
it is in: c:\Documents and Settings\$UserName\Application Data\Subversion\config.
In this config file you can specify the option for diff-cmd,
which is equivalent to the --diff-cmd swith for 'svn diff' command.
The argument for this option need to be a Windows executable. So
you need a bat file to wrap you GVim exe. My diff-cmd option
setting is like this:

diff-cmd = "c:\msys\1.0\home\khong\bin\svndiff.bat"


The content of the svndiff.bat file is as following:

@ECHO OFF

REM Configure your favorite diff program here.
SET DIFF="C:\msys\1.0\opt\GVimPortable\gvim.exe"

REM Subversion provides the paths we need as the last two parameters.
REM These are parameters 6 and 7 (unless you use svn diff -x, in
REM which case, all bets are off).
SET LEFT=%6
SET RIGHT=%7

REM Call the diff command (change the following line to make sense for
REM your diff program).
%DIFF% -s c:\msys\1.0\home\khong\.vim\scripts\vimdiffSize.vim -d %LEFT% %RIGHT%

REM Return an errorcode of 0 if no differences were detected, 1 if some were.
REM Any other errorcode will be treated as fatal.

I am not the author. I got it from the (I think) svn official site.
The original name is svnwrap.bat.

Anyway, now I can use GVim diff mode to view diffs from svn.

Sunday, June 21, 2009

Setting up command line mail client on Windows

Command line email client? There is nothing wrong with it if you hate the
sluggishness and tediousness of firing up a slow GUI email client.
So I set up mutt/fetchmail/procmail/ssmtp on my Windows box (on cygwin of course).
This is the post I follow to set them up. Works like a breeze!

http://www.gentoo.org/doc/en/guide-to-mutt.xml

Firefox plugins

You know, I am a hardcore Vim user. I even want my browsing experience to be
like Vim as much as possible. There are two Firefox plugins that I highly
recommend.

The first one is "It's all text!". It is a Firefox extension that allow you
to edit textareas in a webpage in external editor of choice. In my case
of course it is Vim. :) "It's all text!" homepage is here:
https://addons.mozilla.org/en-US/firefox/addon/4125


The second one is Vimperator. It is live changing for me.
It can be found here: http://vimperator.org/trac/wiki/vimperator

Happy Vimming!

Friday, June 19, 2009

How to make IPython shell Ctrl-h working on Windows

Although I am a vim user. I like the shell emacs binding.
I can't live without it. I have been using IPython a lot
on Windows. IPython's pyreadline is marvalous. But it has
one shortcoming (defect?). That is the 'Ctrl-h' key doesn't
work like 'backward-delete-char'. I have been searching for
a solution, for a long time. Only when I found this post,
has the problem been sovled:
http://makunouchi.jp/zope3/9900417093

:) It is in Japanese. But who minds. Apply the patch to your
pyreadline's keysyms.py file. Ctrl-h is back!!

Post the patch below if the Japanese link scares you.

--- pyreadline/keysyms/keysyms.py.orig
+++ pyreadline/keysyms/keysyms.py
@@ -119,6 +119,10 @@
char = chr(VkKeyScan(ord(char)) & 0xff)
elif control:
char=chr(keycode)
+ if control and ord(char)==8 and keycode==72:
+ keycode=8
+ control=False
+ state &= 0xfffffff7
try:
keyname=code2sym_map[keycode]
except KeyError:

Explanation of why Windows console-based app hangs on Rxvt

http://code.google.com/p/mintty/issues/detail?id=56
This post is clear and comprehensive. I am glad I found it.

But it doesn't solve the problem totally... I can now run
ipython shell in the msys Rxvt terminal. But the pyreadline
key bindings are screwed up totally. :( Still stay with Console2
for now.

Thursday, June 18, 2009

Productivity Tool on Windows (For the GNU/Linux minded) -- Third


Today, I want to recommend a graphical diff tool on Windows.
I think most developers will always have their favorite diff
tool. But if you need to develop on 3 or more platforms, for
example, Mac OS X, Linux and Windows, having one favorite is
not that easy. If you are also using multiple revision
control systems on all these platforms. It will be even
harder. I am a Vim user. Whenever possible I use Vim, even
diff with Vim. Vim can run on all the above OSs. The only
problem I found is using Vim diff mode with different
revision control systems. I need to write script to let Vim
integrate with different revision control system. I am a
lazy person. Even though I like Vim, I don't like this
inconvenience.

So I heard about Tkdiff. Tkdiff is written in Tcl/Tk toolkit. You may
have a little despisement in your heart, right? Well, as the Chinese
saying goes: A man can not be judged by his appearance. Tkdiff is, in
my opinion, powerful, yet simple. The features I found useful are as
following:

1. Understand multiple revision control tools. Can diff different
versions of the file (work with revision control tools).
Currently tkdiff can work with cvs, subversion, perforce out of
the box. But with some patch, it can work with bazaar and mercurial.
2. Easy navigation. Can navigate to differnt diff region easily with
single key press, like 'N', 'P', 'C', etc.
3. Can integrate with external editor, such as gvim to edit the file.
4. Easy refreshing the diff. Just one key press 'R'
5. Run on multiple platform. As long as there is Tcl for that platform.

In a word. TkDiff is a marvelous tool, with humble appearance and name.

Wednesday, June 17, 2009

Installing pyQt

Today I installed PyQt on both Windows and Mac. The Windows version is quite easy.
It is just an executable installer. Run it and that is it. The Mac version takes me
a bit of effort. I follow the instructions on this link
(http://kdl.nobugware.com/post/2008/06/05/PyQt-4-on-Mac-Os-X/). You may need to
search for the newest packages since this post is a bit old now. But anyway,
it works. The only hickup is: I am also using fink packages on my Mac and there is
qt3 fink package installed. I need to tweak the path to eliminate the '/sw/bin'
and '/sw/usr/bin' (the fink's binary path) from PATH before I ran the configuration
step. So the configure.py script will not think it find the Qt installation in
the fink package locations. OK. Time to learn some PyQt and wxPython.

Tuesday, June 16, 2009

Productivity Tool on Windows (For the GNU/Linux minded) - Second

It is great to have a proper terminal window and proper shell on Windows. But
sometimes, this is not applicable to run some programs that relying on the
cmd.exe terminal. Python and iPython for Windows unfortunately falls into this
category. The native Windows Python build (not Python in cygwin) has to be run
in cmd.exe terminals (so is the iPython Windows native build, which runs with
the native Python). If we run Windows Python within a Rxvt (or xterm)
terminal, it just hangs without any input and output. Yuck! What is the
problem? I don't really know, either. (If someone knows, please tell. Better
if you have a good hack to solve it :). But anyway, I have to run Windows
Python and iPython, and I cannot use Rxvt for that. So it triggers my search
for a better cmd.exe terminal that won't hang Python. The result is
Console2! http://sourceforge.net/projects/console/. In Console2 you can
copy and paste terminal text just like Rxvt (with correct settings),
left mouse key select, middle button paste. It can even has transparency!
Truetype font, Multi-tab, customized tab names and settings... You name it.
It is just incredible. Now I run my python shell and ipython shell in
Console2. No more pain-in-the-arse cmd.exe copy/paste retardedness.
I hope this helps you, too.

Monday, June 15, 2009

Productivity Tool on Windows (For the GNU/Linux minded) -- First

I myself is a follower of open source software and also an addict of
GNU/Linux software tools. I cannot live without GNU tools, nor can I
program without OSS software. But in my current job, I am stuck with Windows
platform... Nevertheless, I gradually found my way to the old comfort
and joy all the GNU and other OSS software brought to me. Now, my Windows
development environment is good enough that I can live with all the short-
comings of Windows. I would like to share in my blog my experience. Hope
it will help all similar-minded others, too.

Foremost and most importantly, of course, is the shell. God knows why
Windows is still stuck with cmd.exe command line. That is really insanely
inconvenient. So a bash shell and a xterm like terminal window is of the
utmost importance.

I think most people will know the cygwin environment. Yes, that is also my
first choice. I will not talk about how to install cygwin here, as it is
so easy and www.cygwin.com is so easy to find. But I do want to point out
that, even some people install cygwin, they are still using the cmd.exe
terminal, which is default after cygwin installed. I would highly recommend
using the rxvt terminal of cygwin instead. As this make you copy and paste
from the command line so much easier than using the cmd.exe terminal. This is
easy to do. After cygwin installation, there will be a shortcut to the cygwin
lauch bat file on the Desktop (normally, unless you choose not to). Right-click
on it to bring up the context menu. Choose edit to edit the actual bat file's
content. Put something like this in it:

@echo off

REM C:
REM chdir C:\cygwin\bin

c:\cygwin\bin\rxvt --loginShell

There you go. Rxvt and bash! See you stupid cmd.exe

Friday, June 12, 2009

Building wxWidgets on Windows

I just learned how to compile wxWidgets on Windows recently. Don't want to
forget about it. So just make a blog entry. May help others, too, if the
search engines rank it high enough...

wxWidget can be compile on Windows for different compiler tools. For
example, VC, MingW, borland C++, etc. I am only interested in VC and
MingW. If you are interested in commercial native tools, VC is sufficient
(just use the free beer version). If you are interested in OSS tools,
nothing is better than MingW. OK, that is my opinion anyway. I am very
glad to find out wxWidget support these two builds very easily and well.
Espeically the VC build can be just invoking the nmake tools. Hehe, go
to hell VC XML project files... The MingW build is simple enough, any
autoconf/automake user knows how to do it. Configure and make. Life is
so good!

I learned most of the knowledge from these two posts:
http://www.freelists.org/post/programmingblind/Tutorials-for-compilation-of-wxWidgets-on-Microsoft-windows
http://www.freelists.org/post/programmingblind/tutorials-for-compilation-of-wxWidgets-on-windows-part-2A-wxWidgets-and-MinGW

If there was ever a chance that these links are broken. Use a search engine
to search these keywords "compilation of wxWidgets on windows" (without quote).
A link can be broken, but Internet history will never die.

The only thing I think I should put down here is that, how to tweak various
settings of the build. wxWidgets has different build flavour, most importantly,
monolithic build or multi-lib build, unicode build or non-unicode build,
static lib build or DLL lib build, debug build or non-debug build, etc.
For VC build, these settings are tweaked through the config.vc file under
build/msw subdir of the wx Windows source tree. While for MingW build, the
settings are tweak on the configure script command line options. Running
./configure --help will show all the tweakable options.

After setting config.vc correctly, to build VC build, just need to invoke
nmake /f makefile.vc. Slick! For MingW build, run ./configure on top level
and then make. Nothing simpler!

OK. So much for wxWidgets building. Time to hack the code.

PS. Comiling wxWidget on Mac is no different than compiling it on Linux
or other UNIX system. Configure and make is the way to go.

PyQt vs wxPython

Recently, because of my job (I am a programmer working with C++, Python, GUI,
image processing, embedded system, Linux, Windows and Mac), I need to choose a
GUI framework for Python that are mature and cross-platform. Of course, the
best of the breed are wxPython and PyQt. But it seems to be hard to make a choice
between these two. The top search result of 'wxPython vs PyQt' is about 4 years
old. Qt/PyQt has gone through great changes. So is wxPython I believe. By
reading the old post doesn't seem to help me make a decision. I think the old
license issue (especially on Windows) is not very big an issue any more, since
Qt is open source (in the true sense) on Windows already. Yet, of course, the
GPL license is still scaring off quite a lot of people. I guess what
differentiate will be API beauty/neatness, documentation, support and maturity.
I used Qt before in the Qt2 days. I kind of think its API is neat. But some
post about PyQt pointed out that PyQt is not *Pythonic* enough. So I am not so
sure about the beauty of PyQt's API. From bits and pieces of reading, I think
now PyQt has improved a lot in this aspect, but I am not sure. If any one has
any experience, please leave a comment. Finally, I guess you don't really know,
until you try. Knowledge learn from other people's mouth is always not as good
as your own experience. I think I am going to give both PyQt and wxPython a
try.

I hear, I forget.
I see, I remember.
I do, I understand.