Awesome window manager: Missing icons in Control Center

If you’re running the awesome window manager and are using the gnome-session or gnome-settings-daemon as well as the gnome-control-center you might have noticed it is missing some icons (see screenshot on the right).
This is due to an entry in the “gnome*panel*.desktop” files located in “/usr/share/applications”: 

OnlyShowIn=GNOME;

You can quickly fix this by changing it to:

OnlyShowIn=GNOME;Awesome;

Of course you do not have to do this by hand. Simply use this handy command:

sudo find /usr/share/applications/ -name "gnome*panel*desktop" -print 
 | xargs sudo sed -i 's/OnlyShowIn=GNOME;/OnlyShowIn=GNOME;Awesome;/g'

Python 3.4 and Django on Ubuntu 13.10

Why bother about Python versions? 

I recently started a new project creating a web application. As I have a lot of Python programming experience I chose Python with Django over Ruby on Rails. At the beginning of a new project I prefer using the latest versions of the frameworks the application will depend on. Starting now with Python 2.7 would mean that sooner or later there would be additional work porting the codebase to Python 3. Yesterday, Python 3.4 was released. One of the biggest improvements is that it has pip already included which makes handling virtual environments and installing the latest release of Django really easy.

Building Python from source

The downside is, that Linux distributions do not include the latest Python release yet. Most of them still ship with Python 2.7 as default version. The next Fedora and Ubuntu releases might change that, but for now you need to compile it from source. Luckily that is not a hard task. Go to the download page and grab the latest Python release (recommended if you read the post later and a newer version was released) or past the following command into a terminal.

First make sure you have everything installed to compile Python from source.

sudo apt-get install build-essential

Before downloading create a temporary directory to make the cleanup easier. At the end you can just delete “tmpPython”.

mkdir tmpPython
cd tmpPython
wget --no-check-certificate https://www.python.org/ftp/python/3.4.0/Python-3.4.0.tgz
tar xvf Python-3.4.0.tgz

After the archive is extracted, cd into the source directory. Create a directory to install to, then run configure, build and install.

cd Python-3.4.0
sudo mkdir /opt/Python34
./configure --prefix=/opt/Python34 && make -j4
sudo make install

Now you have Python 3.4 installed on your system.
Add the the path containing the executable to your environment.

export PATH=/opt/Python34/bin:$PATH

Also make sure to add this line to your .bashrc file (or .zshrc if you’re using zsh).

echo "export PATH=/opt/Python3.4/bin:$PATH" >> $HOME/.bashrc

Creating a virtual environment

Go to the directory where you want to create the virtual environment. I recommend /opt if you collaborate with others within the environment (you have to create everything with sudo) or your home directory if you work alone. Then run pyvenv to create it.
pyvenv-3.4 djangoEnv
source djangoEnv/bin/activate

The bash prompt changes to

(djangoEnv) mpei@earth /opt

and that means that you are now within this virtual environment.
This command shows you what you have installed:

pip freeze

Installing Django

Just use pip to install the latest version of Django and its extension:
sudo pip install django django-extensions

And you’re done! You can check the installed versions by running “pip freeze” again. Maybe another blog post on Django and databases? Or the first steps in Django? We’ll see… bye bye!

Organizing C/C++ includes

After starting my new job programming in a big software project I spent some thought on organizing includes and give a recommendation. Here’s what I’ve come up with. As always, some things are obvious, some are not…

  1. You should only include what is necessary to compile the source code. Adding unnecessary
    includes means a longer compilation time, especially in large projects.
  2. Each header and corresponding source file should compile cleanly on its own. That
    means, if you have a source file that includes only the corresponding header, it
    should compile without errors. The header file should include not more
    than what is necessary for that.
  3. Try to use forward declarations as much as possible. If you’re using a
    class, but the header file only deals with pointers/references to
    objects of that class, then there’s no need to include the definition of
    the class. That is what forward declarations are designed for!

    // Forward declaration

    class MyClass;

  4. Note that some system headers might include others. But apart from a few
    exceptions, there is no requirement. So if you need both
    <iostream> and <string> include both, even if you can
    compile only with one of them.
  5. To prevent multiple-inclusions, with loops and all such attendant horrors is having an #ifndef guard.

    #ifndef _FOO_H
    #define _FOO_H
      …contents of header file…
    #endif

  6. The order in which the includes appear (system includes and user includes) is up to the coding standard you follow.
  7. If you have the choice of picking a coding standard regarding the order at the beginning of a new project, I recommend to go from local to global, each
    subsection in alphabetical order. That way you can avoid introducing
    hidden dependencies. If you reverse the order and i.e. myclass.cpp
    includes <string> then <myclass.h>, there is no way to catch
    at build time that myclass.h may itself depend on string. So if later someone includes myclass.h but does not need string, he’ll
    get an error that needs to be fixed either in the cpp or in the header
    itself. 
  8. So the recommended order would be:
    • header file corresponding to its .cpp file
    • headers from the same component
    • headers from other components
    • system headers

If you use the Eclipse IDE (which I highly recommend), you can use a very nice feature that helps you organizing includes (“Source -> Organize Includes”).