Thursday, July 26, 2007

CSS two column float layout - 3px bug workaround

Take this typical two columns float layout. Left column holds menu, right column is for content. What's wrong with it? Nothing, unless the talk is about IE6. By CSS design there should not be any space between left menu column and content on the right, but in IE6 there is.

<html>
<head>
<title>Float Layout - 3px IE Bug</title>
<style type="text/css">

html, body {
margin:0px;
padding:0px;
border:0px;
}

#wrapper {
margin:0px;
padding:0px;
border:0px;

height:100%;

background:#ffffff;
}

#left {
float:left;
width:175px;
margin:0px;
padding:0px;
border:0px;
background:#eedddd;
}

#right {
height:100%;
background:#bbeebb;
border:0px;
margin:0px;
padding:0px;
margin-left:175px;
}

p {
margin-top: 0px;
}

</style>
</head>

<body>
<div id="wrapper">
<div id="left">
<p>menu</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
</div>

<div id="right">
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
</div>
</div>
</body>
</html>

If IE5 is a history for now, IE6 is used by one third of Internet users. So, for CSS beginners, the quick fix to solve this IE problem with additional space around floats is to make this space eaten by negative margin of the float itself. In practice this looks like:

#left {
float:left;
width:175px;
margin:0 -175px 0 0;
padding:0px;
border:0px;
background:#eedddd;
}

See example files at: http://rainforce.org/pile/css/

Thursday, July 19, 2007

Firefox window top margin "bug?"

Sometimes Firefox may render additional margin at the top of the window even though all margins of top-level boxes and html/body container are set to zero. For example:

<html>
<head>
<title>FireFox Top Margin "Bug?"</title>
<style type="text/css">

html, body {
margin: 0px;
padding: 0px;
border: 0px;

font-family:sans-serif, Tahoma, Arial, Helvetica;
font-size: 10pt;
background-color: #ffffff;
}

#maincontents {
margin: 0px;
padding: 0px;
border: 0px;

height:100%;

background:#bbeebb;
}
</style>
</head>

<body>
<div id="maincontents">
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
<p>filler</p>
</div>
</body>
</html>


The strange thing is that margin disappears if you set border and make it visible, i.e. add border:1px solid to #maincontents style.

While it looks like a bug this case is actually covered in specification. In particular it is said that adjoining margins are those of boxes not separated by padding or border areas. It is also said that these are collapsed to form a single margin for unlimited number of nested boxes. The margin height is equal to maximum margin from the set of boxes.

So if you do not want any margins like that - and top window margin is just the specific case of it - you may try to make troublesome box floated or teach children how to behave properly.

#maincontents {
margin: 0px;
padding: 0px;
border: 0px;

height:100%;

background:#bbeebb;
}

#maincontents > :first-child {
margin-top:0px;
}

See example files at: http://rainforce.org/pile/css/

Friday, July 06, 2007

How to recover windows registry keys from NTUSER.DAT

Have you ever experienced crashed windows? Crashed so badly so you have to reinstall it almost from scratch with simple file copy as the only mean for backup your precious files. Even if documents can be successfully retrieved by copying HDD contents from another (or even from the same) station, there is no way to get back precious customizations and passwords previously hidden in the depth of windows registry. Well, unless you haven't forgot to copy your NTUSER.DAT from within your profile located in "Documents and Settings".

There are two ways to get back missed registry keys. Manual and automated. Before starting with manual bot instruction of doing things let me introduce some concepts. NTUSER.DAT is a registry hive. Windows allows to temporary mount hives into subkey of HKLM or HKU root entries either manually with regedt32.exe tool or in batch mode with reg.exe (standard utility in WinXP and part of SUPPORT\TOOLS of Win2000 installation CD). Then hive is mounted it could be accessed by other tools, such as regedit.exe

The process of manual export of registry keys from backuped NTUSER.DAT:
1. start regedt32
2. select HKEY_LOCAL_MACHINE window and root key
3. Registry->Load Hive... specify your NTUSER.DAT
4. enter new key name to place registry tree under (I chose temphive)
5. now launch regedit and navigate to the same entry (i.e. HKEY_LOCAL_MACHINE\temphive)
6. Registry->Export Registry File... specify .reg file name
7. make sure temphive is not accessed by regedit.exe and switch back to regedt32.exe
8. make sure temphive is selected and issue Registry->Unload Hive...
That's all.

Automatic export with reg.exe is governed by the following ntuset.dat.export.bat:

reg.exe load HKLM\temphive NTUSER.DAT
regedit.exe /e regexport.reg HKEY_LOCAL_MACHINE\temphive
reg.exe unload HKLM\temphive

This .bat file creates regexport.reg file with contents of NTUSER.DAT from current directory.

Tuesday, June 26, 2007

python : Change file attributes on Windows

I was too lazy to register here to correct recipe for setting file atttibutes with Python on windows, so I just blog it. You will need pywin32 module and some precautions, because you probably want to avoid cases when your cross-platform application fails due to the absence of some windows-specific stubs.

try:
import win32file
win32file.SetFileAttributes(report_name, win32file.FILE_ATTRIBUTE_HIDDEN)
except ImportError:
pass

Monday, May 28, 2007

How to compile pysqlite for Windows

If you are reading this page that means you know that pysqlite is and probably need updated version of this python extension for windows compiled against newer version of SQLite. Why would anyone need it? Perhaps because of fixed bugs or SQLAlchemy complaining that you need a better SQLite version as it happened in my case.

Original instructions are available for discontinued MS Toolkit Compiler at http://initd.org/pub/software/pysqlite/doc/install-source-win32.html and these are for MinGW compiler. First of all you need to install MinGW somewhere and preferably add directory with gcc.exe into %PATH%. Then unpack pysqslite sources, edit setup.cfg to look like this:

[build_ext]
define=
include_dirs=sqlite3
library_dirs=sqlite3
libraries=sqlite3

That means sqlite3/ directory should contain additional includes and libraries required to compile the extension. These are sqlite3.dll and sqlite3.h available at SQLite downloads from sqlite-source-x_x_x.zip and sqlitedll-3_3_17.zip archives. Unpack them into sqlite3/subdirectory of already unpacked pysqlite source. Now issue:

setup.py build --compiler=mingw32

If everything compiles Ok patch setup.py to carry sqlite3.dll inside to avoid messages about missing .dll

--- pysqlite-2.3.3/setup_old.py Sun Jan 14 02:56:12 2007
+++ pysqlite-2.3.3/setup.py Mon May 28 15:59:05 2007
@@ -109,7 +109,9 @@
+ glob.glob("doc/*.txt") \
+ glob.glob("doc/*.css")),
("pysqlite2-doc/code",
- glob.glob("doc/code/*.py"))]
+ glob.glob("doc/code/*.py")),
+ ("Lib/site-packages/pysqlite2",
+ ["sqlite3/sqlite3.dll"])]

py_modules = ["sqlite"]
setup_args = dict(


Then build an installer to save your work for the future. I saved mine at http://rainforce.org/sqlite/bindings/

setup.py bdist_wininst

Friday, February 09, 2007

Project release

Something to care about when releasing open source project to the public.
How to make it visible?

Every project solves a problem and the first thing people would like to know is not a coolness degree of the project, but where the problem is. There are of course professionals who do not need anything to explain, but for major part of an audience this might be an issue. Why would you care? Perhaps because people might not be interested in your project right now, but they (or their colleagues) can stumble upon the same problem in future and they will have better chances to remember your project if you will interest them enough.

Try to figure out what assumptions your end-users might not have about the problem (they may not realize that this is a problem that needs solving). If possible, explain the problem in terms that even a beginning programmer could understand. Consider your audience though.

Next, how does the package/project uniquely solve the problem? This is something that most documentation lacks. For example, there are many template engines. All of them solve the same problem, but none of them do it in the same way. A block-based template engine does not have any logic at all, whereas a template like Smarty defines a whole new template language. Some template engines compile their templates, others don't. What is unique about your package? Can someone who has never seen the code get a good idea of how does it solve the problem?

In the end (and this is rather obvious) every publicly accessible aspect of the package must be thoroughly documented. If your package/project is a re-usable class, document every method that users should expect to use. If your package has public constants, document their meaning and where they should be used. Document any interfaces that users must use, such as a database DSN, command-line arguments for applications, configuration file contents, or any other non-code elements.

Include at least 2 code examples: one simple and one complex. This is obvious, but projects and packages are designed to be used - show your users how to use them! :) An ideal approach would be to provide a CookBook with recipes for intended meaning and not just testcases, so that users can contribute some.

At last, proofread your documentation. If possible, have someone else who is not as familiar with your project take a look at the documentation. They will catch assumptions that you have missed. This is probably the most important step - to get a test look from outside.