How to enable logging in python-ldap

December 18th, 2007 by Leons Petrazickis

When writing Python scripts which rely on python-ldap and openLDAP, it is often useful to turn on debug messages as follows:

import ldap;

# enable python-ldap logging
ldap.set_option(ldap.OPT_DEBUG_LEVEL, 4095)

# enable openLDAP logging
l = ldap.initialize(‘ldap://yourserver:port’, trace_level=2)
 

This is also useful when debugging the LDAP Plugin for Trac.

Posted in python, ldap, trac | No Comments »

ECMAScript 4

November 8th, 2007 by Leons Petrazickis

John Resig has posted a whitepaper outlining the new features in ECMAScript4 (aka the Javascript standard), how it differs from ECMAScript3, and the rationale for any incompatibilities.

Many of the features have already made their way into Opera and Firefox, which is at Javascript 1.7 level. ES3 is equivalent to JS1.3, and ES4 is the basis for forthcoming JS2.

I look forward to optional strict typing, multiline strings, comprehensions, and generators making their way into browsers. A lot of the new features make Javascript more like Python without losing all the nice things tabout Javascript.

Posted in javascript | No Comments »

Some Facebook Network Stats

August 30th, 2007 by Leons Petrazickis

I’m part of three Facebook networks, and I’ve been keeping track of their size since May of this year.

Facebook network size stats

Toronto has gone from 600k people in May to 800k people in September. That’s 32% of the municipality or 16% of the metropolitan area, which is an impressive proportion.

University of Toronto has been stable at 55k, but there should be a flurry of new users in September when first-year students get their UofT email addresses.

The population of IBMers on Facebook has actually declined. Conversely, I suspect our population on LinkedIn, a career-oriented networking site, has not.

Posted in facebook | No Comments »

Array structure and virtual memory optimization

August 28th, 2007 by Leons Petrazickis

(This applies to Java and C, but the code is given in Python for readability.)

Is it faster to iterate over multiple separate arrays (tuples) of simple variables?

for i in range(0, n):
	phone = phones[i];
	# ...
	email = emails[i];

Or over a single array of complex variables?

for i in range(0, n):
	phone = people[i].phone;
	# ...
	email = people[i].email;

One array is faster than multiple arrays. This is because an array is stored in a contiguous block of memory. Accessing data in different arrays at the same time can require several different pages to be loaded from virtual memory. Memory access, especially hard drive access, is slow. As your application and data set grows, a significant performance difference may manifest itself.

Arrays from the Second Dimension

When iterating over a multidimensional array with indexes i and j, is it faster to iterate over j inside i?

for i in range(0, n):
	for j in range(0, m):
		cell = cells[i][j];

Or over i inside j?

for j in range(0, m):
	for i in range(0, n):
		cell = cells[i][j];

In Java and C, a multidimensional array[n][m] is stored as contiguous m-block of contiguous n-blocks. Let i be in n and j be in m. For a given i-cell, j-cells will be far apart. For a given j-cell, i-cells will be adjacent. Accessing adjacent values in memory is always faster.

For an array[i][j], putting j in the outer loop and i in the inner loop will significantly reduce potential virtual memory slowdowns.

This is the right way:

for j in range(0, m):
	for i in range(0, n):
		cell = cells[i][j];

The above only makes a difference with large data sets, but I like to cultivate good habits.

Posted in java, c, python | 3 Comments »

Creating Start Menu Shortcuts with Javascript

August 14th, 2007 by Leons Petrazickis

While preparing the installer for the Web 2.0 Starter Toolkit for IBM DB2, I had to set up Start Menu shortcuts. The way to do that is to work through the Windows Scripting Host (WSH).

The WSH supports two built-in languages – VBScript and Jscript – and a theoretical number of third-party alternatives. VBScript is the better documented of the two in terms of examples, but I find its syntax ugly and constrained. Fortunately, Jscript can do anything VBScript can.

So here’s how we can create Start Menu shortcuts with Javascript.

Locate the Start Menu Programs folder

Most interesting Windows folders can be found by working with special folders

var shell = new ActiveXObject("WScript.Shell");
var startmenu = shell.SpecialFolders("Programs");
 

The shell object will be reused in code below, but you can redeclare it every time if you like.

Create a Folder

Scripting Guy has more details

var folder = "My App";
var group = startmenu + "\\" + name;

var fso = new ActiveXObject("Scripting.FileSystemObject");
if (!fso.FolderExists(folder)) fso.CreateFolder(folder);
 

startmenu is defined above.

Create an LNK Shortcut

The very hidden file extension of standard Windows shortcuts is LNK. This distinguishes them from website shortcuts, which have the equally hidden extension of URL.

If you are interested in something closer to the symbolic links of Unix, the NTFS equivalent is called junctions. You may find Junction Link Magic of interest.

var name = "My Shortcut";
var file = "myfile.txt";
var path = "C:\\Program Files";

var shortcut = shell.CreateShortcut(group + "\\" + name + ".lnk");
shortcut.TargetPath = path + "\\" + file;
shortcut.WorkingDirectory = path;
shortcut.Save();
 

See the full list of properties. I recommend always setting the working directory for application links. If you don’t, your program won’t be able to load resources from it’s installation folder.

shell and group are defined above.

Create a URL Shortcut

This is very similar. The main difference is that you set the extension to URL.

var name2 = "My Other Shortcut";
var address = "http://example.org/";

var shortcut = shell.CreateShortcut(group + "\\" + name2 +".url");
shortcut.TargetPath = address;
shortcut.Save();
 

See the full list of properties.

shell and group are defined above.

Locate Program Files

When creating shortcuts, it is often useful to know where a user’s Program Files directory is located. It is called different things in different versions of Windows, and some advanced users like to move it or rename it.

var REG_PF = "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ProgramFilesDir";

var progFiles = null;
var process = shell.Environment("PROCESS");
if (process)
        progFiles = process("ProgramFiles");
if (!progFiles)
        progFiles = shell.RegRead(REG_PF);
 

shell is defined in the first code excerpt.

Posted in javascript, windows | No Comments »

Behold, the Web 2.0 Starter Toolkit for DB2!

August 9th, 2007 by Leons Petrazickis

The Starter Toolkit is an easy way to deploy a WAD-on-PHP stack – Windows Apache DB2 PHP. It includes some neat functionality for generating Atom feeds from XML data in your DB2 databases, and some excellent REST-ful web service wrappers from tables and stored procedures.

I’ve spent my last few months tinkering with it, so do take a look. There should be a few posts about it coming down the line, from both external and internal perspectives.
Install Apache, DB2, PHP, and lots of goodiesControl Panel MenuCreate Atom FeedsCreate REST-ful PHP web services
How is it different from Zend Core for IBM? Well, the toolkit has the Atom stuff, and the web services stuff, and a bunch of helpful resources to help folks get started making apps full of Web 2.0-ey goodness.

We’ll be pushing out a fresh revision in double-time, so give me some feedback, consarnit!:-)

Posted in javascript, php, db2 | 1 Comment »

Smart Marketing, Dumb Marketing

July 31st, 2007 by Leons Petrazickis

My Firefox upgraded itself and I saw this:

Smart Marketing

Ah, the Firefox crowd is encouraging casual users to discover Firefox extensions. Firefox extensions are a great way to get people addicted to Firefox. That’s pretty smart.

Dumb Marketing

And, naturally, the extension that the casual crowd needs most is Firebug. Not an extension for music, or photos, or weather. An extension for web development. That’s pretty dumb.

Disclaimer: I use Opera.

Posted in opinion | No Comments »

Why Dojo?

July 9th, 2007 by Leons Petrazickis

Dojo Toolkit | Why Dojo?

Personally:

  • Widgets are very handy to have when you are working on web or intranet apps.
  • Code pedigree is very nice to have when you are trying to release a project at a big company.
  • Once you wrap your head around it, the architecture of Dojo is very logical and predictable.

Posted in dojo | No Comments »

Setting up SVN with Trac on a web server

June 21st, 2007 by Leons Petrazickis

Trac is an excellent web-based wrapper for SVN that adds bug tracking, a wiki, and several handy project management features. I keep setting up new repositories up for all the little projects we cook up in DB2 Technical Marketing, so I thought I’d write up a guide.

Installing Trac, SVN, and dav_svn for Apache2 is left as an exercise for the reader.

Create a new SVN repository:

svnadmin create /var/svn/Project
 

Create a new Trac environment:

trac-admin /var/trac/Project initenv
 

Change the owner to Apache so that it can read and write:

cd /var/svn
chown -R www-data Project
cd ../trac
chown -R www-data Project
 

Navigate to Apache site settings:

cd /etc/apache2/sites-enabled
 

If you want Trac to support multiple repositories, edit the trac file to look like this:

<VirtualHost *>
        ServerAdmin me@somewhere.com
        ServerName mysite.com
        DocumentRoot /usr/share/trac/cgi-bin/
        <Directory /usr/share/trac/cgi-bin/>
                Options Indexes FollowSymLinks MultiViews ExecCGI
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>
        Alias /var/trac/chrome/common /usr/share/trac/htdocs
        <Directory "/usr/share/trac/htdocs">
                Order allow,deny
                Allow from all
        </Directory>
        Alias /trac "/usr/share/trac/htdocs"

        <Location /trac.cgi>
                SetEnv TRAC_ENV_PARENT_DIR "/var/trac"
        </Location>
        <LocationMatch "/trac.cgi/[^/]+/login">
                AuthType Basic
                AuthName "Trac"
                AuthUserFile /etc/apache2/trac.passwd
                Require valid-user
        </LocationMatch>

        DirectoryIndex trac.cgi
        ErrorLog /var/log/apache2/error.trac.log
        CustomLog /var/log/apache2/access.trac.log combined
</VirtualHost>
 

The above assumes that all the repositories are in /var/trac

Navigate to Apache settings:

cd /etc/apache2/mods-enabled/
 

Append to dav_svn.conf:

<Location /svn/Project>
   DAV svn
   SVNPath /var/svn/Project

   AuthType Basic
   AuthName "Subversion Repository"
   AuthUserFile /etc/apache2/dav_svn.passwd

  AuthzSVNAccessFile /etc/apache2/dav_svn.authz

  <LimitExcept GET PROPFIND OPTIONS REPORT>
    Require valid-user
  </LimitExcept>

</Location>
 

The above lets you check out from http://yoursite/svn/Project

If you like, you can add a new user to dav_svn.psswd:

cd ..
htpasswd2 /etc/apache2/dav_svn.passwd NewUser
 

Users can then be granted permissions by editing the dav_svn.authz file. Sample file:

[groups]
developers = NewUser, OtherUser
others = ThirdUser

# Restrictions on the entire repository.
[/]
# Anyone can read.
* = r
# Developers can change anything.
@developers = rw

# Other can write here
[/trunk/public/images]
@others = rw

[/trunk/public/stylesheets]
@others = rw
 

Restart Apache:

killall apache2
apache2

You now have have an Trac/SVN install with SVN at http://yoursite/svn/Project and Trac at http://yoursite/trac.cgi

Posted in unix | No Comments »

Using Safari Web Inspector on Windows

June 21st, 2007 by Leons Petrazickis

The latest nightlies of Webkit (development branch of Apple Safari) now have the web inspector in them. It doesn’t replace the tools in the other browsers, but it does have one very effective piece of unique functionality — a page load graph that gives an exact breakdown of order-of-loading and time-to-load of each script, stylesheet, and image.

First of all, enable the debug menu. It gives you access to the Javascript console and such things.

  1. Open C:\Documents and Settings\chng1me.T40-92U-V46\Application Data\Apple Computer\Safari
  2. Open Preferences.plist
  3. Insert the lines below mid-file:
        <key>IncludeDebugMenu</key>
        <true/>
 

Safari on lpetr.org
Open Safari by executing run-nightly-webkit.cmd in your Webkit install. Right-click and the inspect an element from the context menu.
Safari Web Inspector
Now, click on the icon in the bottom left-hand corner and choose Network.
Safari Web Inspector - Network mode
Voila!

Posted in javascript, css | 2 Comments »

« Previous Entries Next Entries »