Posts

Showing posts from 2013

A Python script to execute Python code like "perl -ne"

I wrote a small utility script that can be used to run a small snippet of Python code like "perl -ne". I find it very useful for my needs. Hope it helps you too. Any suggestions welcome.
You can find the script as a gist here.

Setting the terminal window title - version 2

I had earlier written a small snippet about setting the window title from command line. Based on my experience, I felt that I could make it a lot simpler. Here is version of the same function.

function wtitle {
    if [ -z "$ORIG_PS1" ] ; then
        ORIG_PS1=$PS1
    fi
    export PS1="\[\033]0;$1 - \u@\h:\w\007\]$ORIG_PS1"
}

A rarely used but very useful option in grep

I usually do a lot of log analysis. I search for patterns in multiple files in multiple hosts. Then I collate the result and do processing on the result lines. A sample output looks like:
/tmp/input1.txt: line containing pattern /tmp/input2.txt: another line containing pattern  As it turns out, I don't need the file names in the grep result. I always used to remove the file name prefixes using a Perl one-liner. Silly me!

I was pretty sure that this was a common problem and it must have been solved already. When I referred to the man page of grep, I came across this gem: -h. If you specify this option, grep will not prefix each line with a the file name.

There is also -H option which will prefix each line with a filename even if you are searching in only one file.

Returning to Python

After a couple of years or so, I starting making use Python as my main programming language for one of the projects. This time I was making use of the virtualenv to install Python with various modules and just tar it up and copy to different machines. virtualenv saved me tons of time.


Zig-zag search

This is an interesting problem that I ran into. The problem definition goes like this.
You are given an array of integers. The array is considerably large (say 5 million elements). You are given two inputs: an index i in the array and an integer value v. Start searching the array for value v from the index i, and expand your search towards the two edges of the array. Return the index where the value v occurs closest to index i. If the value v occurs on both sides of index i at an equal distance, return the lower of the two indices. If the value v does not occur at all, return -1. It was very interesting to solve this problem. Give it a shot, you might also like it.

Installing Emacs 24.3.1

Earlier I blogged about a little hurdle that I ran into when I was trying to install Emacs 24.1. Since Emacs 24.3.1 was released recently, I thought I would give it a spin.

After downloading the source code, I unzipped the source code and ran configure command. I got a similar error that I was getting before.

checking for libXaw... configure: error: No X toolkit could be found. If you are sure you want Emacs compiled without an X toolkit, pass --with-x-toolkit=no to configure. Otherwise, install the development libraries for the toolkit that you want to use (e.g. Gtk+) and re-run configure.
I am trying to install Emacs on Mint 14. So to satisfy all the dependencies, I installed the missing packages.
sudo apt-get update sudo apt-get install libgtk2.0-dev libtiff4-dev libgif-dev libpng12-dev libxpm-dev libncurses-dev libjpeg-dev libjpeg8-dev libjpeg-turbo8-dev
After installing the dependencies, the usual three step process worked fine.
./configure ./make …

A late realization on embedding the code using gist

Off late I started using gist to embed source code in my blog entries. I was thinking about the consequences of doing this (or any form of embedding text using an external site). I realized the following:

First and foremost the search engines don't get to index the page based on the key words in the embedded source code. For e.g. consider an embedded gist that demonstrates ServerSocket. Unless I mention about ServerSocket in my blog somewhere, the serarch engine will not associate the blog entry with the ServerSocket as that word appears only in the embedded source code.When peopele search for a specific key word in the embedded source code, they will be taken directly to the gist page, instead of the blog entry. This is bad because the user will not get the context and explanation around the source code. To understand the depth of the problem, please search for alias-to-jumphosts-alist in Google. You will find that the gist is shown first and then the blog entry. Even in this cas…

Using dirtrack-mode inside shell mode

I usually try to automate the things that I repeatedly perform. One the things that I recently automated was:
Create a directory with a prefix and today's date or time as suffix.Chdir to the most recently created directory. The source code for these utility functions is given below:
When I use these utility functions inside the Emacs' shell-mode, the shell-mode was unable to keep track of the current directory. The issue is that I am changing directories without making use of the cd command.

Then came the dirtrack-mode for the rescue. The idea is very simple: you specify the regular expression where the path appears in your shell prompt. The dirtrack-mode figures out the rest of the stuff.

Now my .emacs file contains the following two lines:

Thats it. Works like a charm. Please note that you might have to modify the regular expression to match where the full path appears in your prompt.

One sequence per table or one sequence per database

Let us assume that you are implementing RESTful APIs for your service. Your service has the following two models: users and tweets. While inserting new entities, you should genertate IDs. The IDs for each of these models can be generated in two ways:
You can use a sequence (if you are using Oracle) or autoincrement feature (if you are using MySQL) to have per model IDs.You can use a global sequence for all the models. If you are following the first strategy, you will have user IDs 1, 2, 3, 4, etc. and tweet IDs 1, 2, 3, 4, etc. If you are following the second strategy, you will have user IDs 1, 3, 4, 8, etc. and the tweet IDs 2, 5, 6, 7, etc. 
Thinking about it, I felt that using the first strategy will leak information. Consider some one doing a tweet every day at 10:00 AM and get the tweet ID. If first day the tweet ID is 100, and the second day the tweet ID is 200, etc. That person can infer that your system is receiving 100 tweets/day. I don't know how sensitive this informati…

The issue with Emacs "cd" function and CDPATH

Recently I wasted sometime trying to figure out an issue with "cd" function and autocompletion in Emacs, while I had a value set for CDPATH environment variable. I hope it saves some time for someone who might bump into the same issue.

I had CDPATH environment variable set and it didn't have "." (current directory) as one of the components. When I tried autocompletion while running "cd" function, it didn't show any of the subdirectories under the current directory. The directories it showed for completion were from somewhere else. As soon as I added ".", I was able to see all the subdirectories of current directory in the autocompletion list.

Hope this saves some time for you as well.

Enabling multiple cores in VirtualBox

Image
This post is a follow up to my earlier post Moving from VMWare Player to VirtualBox.

Once you have set up your VirtualBox instance, the first challenge you will face is that the guest OS may possibly be very slow due to the fact that the number of cores allowed is only one. Hence you will have to increase the number of cores that your guest OS can make use of. Open the VirtualBox manager, click on your VM, then click on the Settings button. In the Settings window, click on the System on the left pane and select the Processor tab on the right.
In this page, you can increase the number of processors. If you haven't already enabled the virtualization in your processor, you are likely to get an error when you click on OK. The error message will look like this:


Failed to open a session for the virtual machine Lubuntu.
VD: error VERR_NOT_SUPPORTED opening image file 'C:\VMs\Lubuntu.vmdk' (VERR_NOT_SUPPORTED).
Result Code: E_FAIL (0x80004005)Component: ConsoleInterface: IConsole {d…

Moving from VMWare Player to VirtualBox

This is my first post this year. Wishing you a happy New Year.

Until a week back, I was using VMWare Player for my personal use to learn different OSes. Using VMWare Player, I was trying out various Linux distros. I have been a fan of VMWare Player for six years now. Offlate I started seeing theese limitations:

I bought a new i7 quad core laptop. Even though I have 8 cores, my VMWare Player will not allow me to create a VM with more than 2 cores.I used to enjoy the vmnetcfg command line tool which used to come with VMWare Player. But it has been removed from recent installations. Due to this, I was unable to configure NAT port-forwarding.I used to be able perform packet sniffing using vnetsniffer command line tool. That also has been removed in the recent distributions.You cannot run a 64bit guest OS in VMWare Player. Hence I could not run my guest Linux OS distros with more than 3 GB of RAM. Before I started using VMWare Player primarily, I was evaluating VirtualBox. At that time, it…