Archive for December, 2007

Enable vim code (Python) auto complete

Written on December 27th, 2007 by shakir
Categories: Information Insemination

Beginning version 7 of vim, it has this nice auto completion feature. It is by default however limited to words that has already been in the current workspace. To use it, simply press [ctrl] +n or [ctrl] + p key while in edit mode. For example:

We can however *teach* vim to autocomplete a whole bunch of other stuffs as well, by using something so called Dictionaries. With this idea we can have auto completion for Python, Ruby, PHP, Bash, and any other programming languages code.

For an example, let’s try to install Python dictionary, by downloading it from here:


http://www.vim.org/scripts/script.php?script_id=850

The next thing to do is to extract the downloaded file to the appropriate folder:

shakir@herugrim ~ $ mkdir ~/.vim
shakir@herugrim ~ $ tar xvzf pydiction-0.5.tar.gz -C ~/.vim

and add this lines to your ~/.vimrc (be sure to replace "/home/shakir" to your own home directory)

if has("autocmd")
    autocmd FileType python set complete+=k/home/shakir/.vim/pydiction-0.5/pydiction isk+=.,(
endif " has("autocmd"

and let’s see the result:

Browse around the Vim script page and your customized Vim could be just as good if not better than some IDEs.. :)

CSS for code block

Written on December 26th, 2007 by shakir
Categories: Information Insemination

If you're wondering how do I actually get the nicely boxed code block in my blog as this;

 

then the answer to it is just to have this in my CSS:

pre {
        padding: 10px 15px;
        margin: 5px 0 15px;
        border-left: 5px solid #666666;
        background: #222222;
        font: 1em/1.5 "Courier News", monospace;
        color: #bbbbbb;
        overflow : auto;
}

and later I would just wrap whatever text I want to be in the box with the pre tag in my HTML codes, like this;

<pre>
This is a code example
</pre>

and should get this output (including the dashed-box):

This is a code example

and that's all. It's just so easy..

How to create a WordPress plugin

Written on December 17th, 2007 by shakir
Categories: Information Insemination

I’m into writing a plugin for WordPress, and so let me just share here how easy it is to actually create one.

For an example, we’re going to write a plugin that will insert a Google Adsense to our blog post.

From our WordPress installation directory, create a file named google_adds.php in the wp-contents/plugins/ directory and add these lines; 

<?php
/*
Plugin Name: Google Adsense
Version: 0.1
Plugin URI: www.mohdshakir.net
Description: This plugin will display Google Adsense in every post
Author: Mohd Shakir bin Zakaria
Author URI: www.mohdshakir.net
*/
?>

To see what it does, login to your WordPress admin panel and go to the Plugins section.

As what can be seen from the screenshot, the information of the plugins there were taken from the comments on our plugin code. We can now activate the plugin, but for now our plugin doesn’t really do anything useful.

Let’s go back to our PHP code, and add some lines to be as the following

<?php
/*
Plugin Name: Google Adsense
Version: 0.1
Plugin URI: http://www.mohdshakir.net
Description: This plugin will display Google Adsense in every post
Author: Mohd Shakir bin Zakaria
Author URI: http://www.mohdshakir.net
*/

function adsense($text){

        $adsense_code = <<<EOT
# Add your own Adsense code here
EOT;

        return $adsense_code . "<br \>"  .$text;
}

/*
  Add filter is a built in WordPress function, and the_content
  is a WordPress variable of a single post content.
*/
add_filter('the_content', 'adsense');

?>

Let’s see if it works

That’s it, our first fully functioning Google Adsense plugin :)

Interactive Python shell with IPython

Written on December 15th, 2007 by shakir
Categories: Information Insemination

One nice thing about Python as compared to some other scripting languages, is that it has a shell that enables us to test our Python script on the fly without needing to write a script file first

It’s nice and all that, but when you’re too used to IDEs which has features such as auto completion etcetera, you’ll feel like something is not right about the default python shell.

That changes when I come to IPython, one of the few other enhanced Python shell. The one feature I like most is the auto complete feature, which among other by using it I don’t need to remember module names, and at the same time would list and try Python modules that I’ve never heard of.

In Ubuntu, or debian in general, the package is called ipython, and to install it just;

shakir@herugrim ~ $ sudo apt-get install ipython

Can you spot the differences between this

shakir@herugrim ~ $ python
Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import command
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named command
>>> import commands
>>> status, output = commands.getoutputstatus ('ls')
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'getoutputstatus'
>>> status, output = commands.getstatusoutput ('ls')
>>> print output
Desktop
Documents
Music
Photos
>>>
shakir@herugrim ~ $

and this?

shakir@herugrim ~ $ ipython
Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32)
Type "copyright", "credits" or "license" for more information.

IPython 0.8.1 -- An enhanced Interactive Python.
?       -> Introduction to IPython's features.
%magic  -> Information about IPython's 'magic' % functions.
help    -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: import command
---------------------------------------------------------------------------
           Traceback (most recent call last)

/home/shakir/ in ()

: No module named command

In [2]: import commands

In [3]: status, output = commands.
commands.__all__           commands.__hash__          commands.__setattr__
commands.__class__         commands.__init__          commands.__str__
commands.__delattr__       commands.__name__          commands.getoutput
commands.__dict__          commands.__new__           commands.getstatus
commands.__doc__           commands.__reduce__        commands.getstatusoutput
commands.__file__          commands.__reduce_ex__     commands.mk2arg
commands.__getattribute__  commands.__repr__          commands.mkarg

In [3]: status, output = commands.gets
commands.getstatus        commands.getstatusoutput

In [3]: status, output = commands.getstatusoutput('ls')

In [4]: print output
Desktop
Documents
Music
Photos

In [5]:
Do you really want to exit ([y]/n)? y
shakir@herugrim ~ $

I hope you can guess when did I press the [tab] key :)

“‘BoundMetaData’ is not defined” error in SQLAlchemy

Written on December 13th, 2007 by shakir
Categories: Information Insemination

I get this error when creating a BoundMetaData object in SQLAlchemy 0.3.10. The fix is to just use MetaData instead of  BoundMetaData. BoundMetaData is deprecated and replaced with MetaData

Example code:

from sqlalchemy import *

db = create_engine('sqlite:///test.db')
metadata = MetaData(db)

"""
  Continue with the rest of your Python code
"""

Adding RSS / ATOM feed icon to Blogger / Blogspot

Written on December 13th, 2007 by shakir
Categories: Information Insemination

While reading the logs of people coming to my website, I found out that some came from Google, searching for how to add RSS feed icon to their blogspot blog. This is probably due to my previous post.

Though I've not been using blogspot as my blogging platform for quite a long time already, I'll just create a short howto here on to do just that.

From your blog's admin panel, go to Settings -> Template. From there click on the "Add a Page Element" link, and select  "Text" from the selections.

On the editor that pops out, fill up the title, and for the content, click "Edit HTML" and add this lines to the text area:

<a href="http://yourblog.blogspot.com/rss.xml">
    <img src="http://www.any-website.com/rss-icon.jpg" />Any text
</a>

You can change http://yourblog.blogspot.com/rss.xml to http://yourblog.blogspot.com/atom.xml, whichever that you prefer, and http://www.any-website.com/rss-icon.jpg is a URL to any RSS icon in the net that you prefer (or upload your own)

I hope this helps

Shift Happens

Written on December 12th, 2007 by shakir
Categories: Nerd Public Journal?

Was browsing some development blogs, and came to this slide;

A scary thought; at least to me..

Installing Voyage Linux in Wrap2c

Written on December 11th, 2007 by shakir
Categories: Information Insemination, Nerd Public Journal?

Basicly this is how my home network looks like;

As I’m upgrading the Linux I have in my Access Point, a.k.a. my home server, a.k.a. an SBC, I’ll just share it here in my blog.

Here’s what I need:

- SBC (Single Board Computer), wrap2c in my case
- Storage – I’m using 2GB CF card here
- Mini PCI wireless adapter – mine is CM9 (Atheros based)
- RS232 adapter
- USB – RS232 converter (optional, as my laptop doesnt have serial port)
- CF card reader
- and a camera to snap the photos in this post..

Instead of installing Debian or or Gentoo as what I described in my previous post, I’m gonna just install Voyage, a minimal Debian based distro customised for SBCs.

After downloading the tarball, extract it with sudo;

shakir@herugrim ~ $ sudo tar xvjf voyage-version.tar.bz2

Time to plug in the CF card, and let’s see where it is attached to:

shakir@herugrim ~ $ cat /proc/partitions
major minor  #blocks  name
   8     0  156290904 sda
   8     1   21494938 sda1
   8     2    8795587 sda2
   8     3    2939895 sda3
   8     4          1 sda4
   8     5    9775521 sda5
   8     6  113282316 sda6
   8    16    2014992 sdb
   8    17    2014721 sdb1

Here I can see my 2Gb CF is identified as /dev/sdb, and with this I run;

shakir@herugrim ~ $ cd voyage-version
shakir@herugrim ~/voyage-version $ less README

and follow the installation guide, which is in section 2 of the README :)

To see if things really work, I’m gonna need to make sure I can use my USB RS232 converter. Let’s plug it in and see what the kernel says;

usb 5-1: new full speed USB device using uhci_hcd and address 2
usb 5-1: configuration #1 chosen from 1 choice
usbcore: registered new interface driver usbserial
/build/buildd/linux-source-2.6.22-2.6.22/drivers/usb/serial/usb-serial.c: USB Serial support registered for generic
usbcore: registered new interface driver usbserial_generic
/build/buildd/linux-source-2.6.22-2.6.22/drivers/usb/serial/usb-serial.c: USB Serial Driver core
/build/buildd/linux-source-2.6.22-2.6.22/drivers/usb/serial/usb-serial.c: USB Serial support registered for pl2303
pl2303 5-1:1.0: pl2303 converter detected
usb 5-1: pl2303 converter now attached to ttyUSB0
usbcore: registered new interface driver pl2303
/build/buildd/linux-source-2.6.22-2.6.22/drivers/usb/serial/pl2303.c: Prolific PL2303 USB to serial adaptor driver

What’s important from the long message is that the kernel has recognized the converter, and attach it to /dev/ttyUSB0. If your kernel doesn’t recognize the converter, fom the log you can see that it requires pl2303 module, so go re-compile your kernel to have it supported..

After everything is connected, lets run minicom;

shakir@herugrim ~ $ sudo minicom -s

Select the Serial port setup option and change it to this;

exit;

power on the board, and hope for the best :)

We’ve got the login prompt, mission accomplished :D

Fishy Story

Written on December 10th, 2007 by shakir
Categories: Nerd Public Journal?

Get to see this from my office’s window today;

and on 12x zoom of my camera;

Reminds me when net casting was among my hobby :D

 

Installing Smiletag Shoutbox in WordPress

Written on December 10th, 2007 by shakir
Categories: Information Insemination

Smiletag Shoutbox is the shoutbox that I have on my website’s sidebar. I’ve been using it since I was using Joomla for my website, and now that I moved to WordPress, Smiletag is still my shoutbox of choice.

Installing it in Joomla is very straightforward, while it’s not really the case with WordPress, as it requires a little bit of coding.

The application can be downloaded from Hivemind’s website (requires free registration)  and it can then be installed as any other WordPress plugin. The shoutbox however doesn’t immediately show up in the page, and there’s no option in the admin console for that.

To add it to the page we must add some codes, and in our case, to the sidebar’s code. Edit it from the admin panel;

Presentation -> Theme Editor -> sidebar.php

or can be edited with any editor;

/path/to/current/template/sidebar.php

This is an example of a very simple WordPress template with widget support;


The sidebar uses <ul> for its items, and so we need to add <li> element to add our shoutbox (and probably any other static stuff) to it. We also need to add a header (<h2> probably) for the item’s title, and add the shoutbox’s code to it. The final sidebar should look something like this;


This might not exactly be the case with every template, but the idea is about the same. Happy blogging!