Pattern matching or sort of in python

I must admit that pattern matching is not really new to me. The first time I looked at haskell I was impressed by the idea a lot. It wasn’t until late that I found about new languages on the JVM that support the same idiom.

For this to work properly – the language should know how to “destruct” an already constructed element. A simple example in haskell is if you have a list of elements and you want to capture the first element in a variable and the remaining part of the list in another variable. In that case you would write something like this:

f (x:xs) =  -- do something with "x" as the head of the list and "xs" as the tail

This is a definition of f as a function on one argument – if you then call the function with a list like [1, 2, 3] you will have a x = 1 and xs = [2, 3] in the body of the function.

So given this knowledge today I started coding a python script at work. And I found myself trying this pattern in the python interactive shell – the result was that python supports this for lists at least:

>>> p = [1, 2]
>>> [a, b] = p
>>> a 
>>> b

But continuing further I was given an error message:

>>> p = [1, 2, 3]
>>> [a, b] = p
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack

So while this works for full pattern on lists it won’t work if you provide less elements.

It is strange how exotic languages like haskell can open your mind for idioms that you can use in more “accepted” languages like python.

Implement properties in C plus plus

Here is some tricky code in C++ that I was watching recently and decided to post here for future references. Sometime the usage of operator overloading in C++ amuses me so much. Apperently there is a way to implement properties that look the same as normal properties and behave almost the same. The only problem as always is that when you get a compile time error – the messages are pretty confusing if you don’t know what’s going on. Enough said here is the code for that “beautiful” feature.

/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
using std::cout;
class Property
    PropertyType data;
    /* Use the cast type operator here */
    operator PropertyType()
        return Controller::get(data);
    PropertyType operator=(PropertyType a)
        Controller::set(data, a);
        return Controller::get(data);
class IntController
    static int get(int &a)
        return a;
    static void set(int &a, int &b)
        a = b;
class Man
    Property<int, IntController> HP;
void PropertyTest()
    Man man;
    man.HP = 7;
    man.HP = 7 + man.HP;
    cout << man.HP << "\n";
int main (int argc, char const* argv[])
    return 0;

Using SSH without password

The problem

Ok so you are using Linux or other Unix(Solaris, *BSD) favors to deploy your systems. If so you are most probably using ssh to log in on those servers. So to login to some external machine you will have to type something like this:

ssh -l nikolavp

and then when prompt for password, enter it. scp, rsync and friends will require almost the same amount of work.
This is bad for the following reasons:

  • It requires you to know the IP address or the DNS name whatsoever instead of doing an alias for yourself
  • You will have to remember the username for that host
  • You will have to remember the password for that host
  • You will have to break your fingers to type that over and over again

Well if you are like me you most probably don’t want to type all that much just do your work. Here I will provide a setup that will save your world :)

Create an alias for the host

SSH provides a convenient shortcuts for making internal aliases for the hosts. You can define a host alias for an IP address(or DNS resolvable name) and the username for that host. So to not use the password for our imaginary host from the example above, we have include the following in our ~/.ssh/config file(create it if it doesn’t exist)


Create a ssh key

You should forget about your password as authenticating method, it is far safer to use a key for that and some sort of cryptography like RSA. You can follow some guide from the internet on how to do that exactly. For example this one seems pretty simple and straightforward. Except that there is a far better command now for the last two steps – its called ssh-copy-id. You can man it if you want to know what it does.

Setting up a keymanager

To be able to unlock your key once on every session start you should be using a key manager. Likely most of the big desktop environments have done this for you – gnome has gnome-keyring and kde has the kde-wallet. On other more primitive environments you will be on your own. You can look at the guide from Archlinux for more information here.

Final thoughts

After following the steps from this simple and short guide you should be able to log into your servers without any password with just it’s alias. For our host from the example, I will just have to write

ssh foo

Another Skype instance may exist

Today I got this error message from skype in Linux while I was trying to login. After many attempts to get rid of the message by restarting skype, I finally googled the message and found out that there are two .lck files that need to be deleted. But after trying it and restarting it again, this didn’t solve it. If you happen to be one of those unlucky “souls”. Do the following:

cd ~/.Skype/
find -name '*.lck' -exec rm {} +
mv ${old_profile} ${old_profile}.back

This deletes all .lck files and makes a backup of your login history data, assuming ${old_profile} is the name of your skype login name.

Now open skype, login in with your account, quit skype and then to restore the history do:

rsync -av ${old_profile}.old/ ${old_profile}/

Moved from blogger to wordpress

The why

You are currently reading this post on my new blog, built on top of WordPress and OpenShift. My blog was on the blogger platform in the past, but i finally decided to move from there for the following reasons:

  • small screen/content width – this was the main reason that made me move. The width of the box content is so small that it’s really hard to post a snippet of code. When I struggled half an hour with the templates just to make them wider and I couldn’t, I just gave up… Which leads us to the next point
  • more control – I was feeling the blogging platform pretty limited for some things. I can remember spending 3 hours once just to make the damn code snippets work and highlight as I wanted them to be
  • not mine – the platform is pretty nice, but was always feeling the site is not really mine.

I am not really a fan of PHP, actually it’s the only language that I don’t like at all, but wordpress seems like the best option out there for blogging. Everything seems to be simple and at the end of the day nothings stops me to get my hands dirty and edit some code, which btw is really readable. It’s just not like other PHP code I have dealt with.

Step by step guide

So here is a step by step guide to the migration process.

Create a blogging openshift application

This assumes that you have the openshift rhc client and stuff installed. So here we go…

  1. create the application, named blog in my case(this assumes you have a domain already)
    rhc-create-app -a blog -t php-5.3
  2. create a mysql instance
    rhc-ctl-app -a blog -e add-mysql-5.1

    this will output some information about your current database name, user, password, url

Install wordpress

First we download the wordpress distribution

cd blog
wget -c
tar zxvf latest.tar.gz
mv wordpress/* php

Push the code to openshift

git add php
git commit -m 'Initial wordpress commit'
git push origin

wordpress should be now deployed on the server. Now

  1. go to to setup the database information then
  2. go to to setup the tables, etc.

Note that appname should be application name that we created in step 1 in openshift and the domain-name is your domain on openshift. That’s it, you now should have your wordpress ready to go. With this you are able to install plugins, create custom pages, etc, etc.

Setup redirection

There is only one last step until we are finally done. We want to notify our users somehow that the blog has moved.

  1. Go in blogger to Settings -> Publishing and specify the new domain for your blog there.
  2. Go in wordpress to Settings -> Permalinks, set the custom structure radio button and type

    there. This will make sure that links to post from the old blog go to the new blog content.

  3. Write a new post telling your users that you moved. This is only for feed purposes.

I will be glad if someone finds this guide helpful.

Moved from Ubuntu to Arch



After 3 years with Ubuntu on my laptop HP 6730s, I finally decided to switch to something faster and more reliable(I will talk about this some more)

Some history:
I was an old Gentoo user before and I was using it on my desktop machine. After I got my laptop though I knew that Gentoo is not so suitable for laptops(the compilation is killing them) so Ubuntu seemed like a nice choice. Something that just works out of the box is so important for laptops – i.e. I don’t want to configure my wireless for hours.

The problems I had with Ubuntu:
  • It tries to hide the system from me. This really annoys me – I want to be able to configure my system by hand sometimes and don’t want to find out that some service that I don’t know about, started by Ubuntu, is autoediting my config files every now and then :(
  • The performance is bad – as I am mostly working with java applications and I feel them slow. I am not sure who is to blame for this but the other computers I have deployed on are working better.
  • It’s not using a rolling release model which makes me pray and fix my machine every new release.
  • Some packages are old because of the “official” release model.
So after the latest problems which I had with NetworkManager i finally decided to switch to something better. I couldn’t find why, but my machine was booting like 2 minutes and messages poped up, telling me it’s because of the network manager.
So far Arch seems stable, far faster(you cannot imagine), boots in 30 seconds with all services up. It just feels right.

Installing jdk7 in Ubuntu

Today i wanted to try the new version of jdk and see how is the project going. Last time i checked it was too buggy to setup so i was a bit of disappointed. As I using ubuntu on my laptop i wanted to check if my distro is providing a development package for the new vm but that wasn’t the case :( . Luckily the jdk project provides some nice prepackaged binaries you can install yourself over here. After getting the binaries for my architecture(x64) and unpacking the archive i got a jdk1.7.0 directory. In ubuntu it pretty easy to set your platform default options and the tool for this is update-alternatives. Here are the commands if you have Ubuntu:

Move the directory to the jvm directory where it should reside:

sudo mv jdk1.7.0/ /usr/lib/jvm 

Check how many alternatives you have:

sudo update-alternatives --config java

Install the new alternative:

sudo update-alternatives --install /usr/bin/java java 
/usr/lib/jvm/jdk1.7.0/jre/bin/java 2

where 2 should be the next unused index from the last command. So if you have 2 java alternatives your new installation should have index 3

Set the platform default to be the new option:

update-alternatives --config java

Now all your java applications will use the new jdk. I cannot tell the difference but as I work more with eclipse and other java tools i will be make my opinions and might share them here :)

Happy coding

Installing flash 64bit on Kubuntu jaunty

Maybe some of you know that kubuntu/ubuntu is providing 32 bit flash player and is using nspluginwrapper if you are using an 64 OS. Here the nspluginwrapper is making problems, it is making my machine unresponsible if I am watching 3 flash sites at the time and so on. I am also suspecting that it is the reason firefox crashes sometimes on some flash movies.

So here is the workarround. On the official website of adobe there is no 64 option so we have to dig a little more. We push a search on google and here is what we get . It seems that there is some 64bit support so we scroll down the page and find a download link. Here is the whole procedure for installing the 64bit flash :) :

       wget -c
       tar zxvf
       mkdir -p ~/.mozilla/plugins
       mv ~/.mozilla/plugins

NB. Make sure that you uninstall any previous version of the flash player :)

Did you know movie and Kevin Kelly’s web 3.0

Have you already used twine, if not check it out. It is like a social bookmarking system and i am using it lately extensively for getting the latest information about the semantic technologies and the next generation web 3.0. After using it for about a week or two it starts to “know you” and recommend useful information based on your interest.

For example today i got this video with “Did you know” stuff which is mostly mentioning things about the internet but there are some nice facts about the population in USA, India. It is really interesting i think :) .

Check this video also which was in the web 3.0 twine section of mine. This is Kevin Kelly and he is talking about web 3.0 and how he sees the WWW 20 years from now. It is a must see :)

Reloading jars at Runtime

I haven’t blogged in a while but now i have some time :) . This week I had to dynamically reload some jars from a directory and i searched the net about the problem. I will show the implementation that I finally came up with. The problem with the dynamically loading external jars is that they should not be in the CLASSPATH because the system classloader is making some optimizations(cache) of the currently loaded classes and doesn’t search again in the CLASSPATH if it has already instantiated the class. So to make the jars dynamically loadable we have to make our own ClassLoader or we can use the URLClassLoader which is part of the JavaSE. The idea is that every time we load the class we should use a new object of the URLClassLoader so we don’t use the cache. And also we should have an interface so we can cast our instantiated object or the alternative is to use reflection and the Method class. But enough words here is the implementation:
1. The Interface :

package org.nikolavp;
public interface Reloadable{
    public void method();

2. The Class which will reload our class by a given name:

import org.nikolavp.Reloadable;
public class ClassReloader{ 
    public static Reloadable reload(String className) {
        Reloadable obj = null;
        URL jar = null;
        try {
            jar = new File("jars/somejar.jar").toURI().toURL();
        } catch (MalformedURLException e) {
        URLClassLoader loader = new URLClassLoader(new URL[] { jar },
        try {
            Class clazz = loader.loadClass(className);
            obj = (Reloadable) clazz.newInstance();
        } catch (ClassNotFoundException e) {
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        }  return obj;

3. The Main class to test our work:

import org.nikolavp.Reloadable;
public class Main{
    public static void main(String[] args) {
        while (true) {
            Reloadable object = ClassReloader.reload("MyClassFromSomeJar");
            try {
                Thread.sleep(10 * 1000);
            } catch (InterruptedException e) {

Assume that MyClassFromSomeJar exist in the jars/somejar.jar
you can now make changes to the
code in the external jar and see the results :)

Conclusion: This is one of the areas where java really shines. Try doing that with static languages like C/C++ :)