Saturday, August 23, 2008

Improving blogger template style - Sand Dollar

There is something what I don't like on my currently selected blogger template style - the Sand Dollar. It spans all available screen real estate from left to right. I would like to make it fixed-width and centered. To achieve this, I've changed the '#outer-wrapper' part of the template by adding 'width: 980px;'. This makes the rendered web page stay only 980 pixels wide which should be enough even on 1024 pixel wide common displays.
To center web page contents, just add 'margin: 0 auto;' to the same section. This means in english: top and bottom margins are zero, left and right margins are set by browser (which means content will be centered). You can find the specifications of the margin command here.

The entire outer-wrapper section now looks like this:

#outer-wrapper {
font:$bodyfont;
width: 980px;
margin: 0 auto;
}

Thursday, August 21, 2008

scipy.weave doesn't work - compiler troubles

The scientific python package SciPy contains a very powerful library called weave. Weave enables one to relatively easily embed C/C++ code into Python code. Thus, if you happen to have a computationally intensive code you can gain a lot of speed by rewriting it in C/C++. There are a lot of ways of how to accomplish this but weave makes this feat relatively easy even for an amateur like me.
There is however one caveate for Windows users. I commonly install all software into the 'C:\Program files\' directory. If you do this with Python and a weave requirement, MinGW compiler, weave will not work. The trouble is the space within directory name. Weave scripts and possibly also gcc are not written to deal with spaces within paths and will not find required files like gcc.exe. You will end up with error messages like:
error: command 'gcc' failed: No such file or directory
ValueError: The 'mingw32' compiler was not found.
At first I have made a hack in the first affected script, the 'platform_info.py', but this only resulted in another error:
Could not locate executable g++
Executable g++ does not exist
Traceback (most recent call last):
File "G:\Temp\tmpfgm8bn\_ev-uq.py", line 8, in
s,o = exec_command(cmd, _with_python=0, **{})
File "C:\Program Files\Python25\lib\site-packages\numpy\distutils\exec_command.py", line 253, in exec_command
use_tee=use_tee,**env)
File "C:\Program Files\Python25\lib\site-packages\numpy\distutils\exec_command.py", line 402, in _exec_command
so_dup = os.dup(so_fileno)
OSError: [Errno 9] Bad file descriptor
So, to make things short, just install Python and MinGW into C:\. This will save you from a lot of trouble.
UPDATE 08/10/22: It turned out today that your user name also may not have a space within it. I've just spent an hour trying to get weave working on a colleague's computer where the username is actually a "user name". No, renaming the user does not do the trick. I 'solved' it by creating a new user with 'valid' username.

Tuesday, August 19, 2008

Strange scipy.weave import error

Today, I've booted my PC and tried to run a script which uses scipy.weave package. What I got was a rather unusual error:
EOFError: EOF read where object expected

I've tried to type
import scipy.weave
which just reproduced the error. After some thinking I got an idea that some files were broken. A scipy reinstall however warned that some files are broken and cannot be (re)written. So I run the scandisk/chkdsk/whatever its name currently is. It did find some errors within files mentioned in the error message. Nonetheless the import still didn't work, this time replying with another error:
ValueError: bad marshal data

After this I tried repairing both scipy and Python installations to no success. No warnings this time though.
Finally, I uninstalled Python alltogether and deleted all files and directories within C:\Python25\. After installing everything back everything seems to fine allright. I think it is the first such error since I've started using NTFS as my filesystem some five or six years ago.

Monday, August 18, 2008

Text box with scrollbar

A very nice example can be found here.
I you want to move focus so that the last entry in the text box will be visible, you may modify the code as follows:


import Tkinter

root = Tkinter.Tk()
s = Tkinter.Scrollbar(root)
T = Tkinter.Text(root)

T.focus_set()
s.pack(side=Tkinter.RIGHT, fill=Tkinter.Y)
T.pack(side=Tkinter.LEFT, fill=Tkinter.Y)
s.config(command=T.yview)
T.config(yscrollcommand=s.set)

for i in range(40):
T.insert(Tkinter.END, "This is line %d\n" % i)
T.yview(Tkinter.MOVETO, 1.0)

Tkinter.mainloop()

The 1.0 parameter of .yview means the end of the Text widget contents.
I'm usually using the Grid geometry manager and aligning the scrollbar with it requires some more work than with pack. Nothing out of ordinary though.