python singleton

May 11, 2012   coding

A version of python singleton pattern, it is not very pretty and breaks a bunch of pep 20, but in some cases is useful.

Singleton Pattern

class Singleton(object):
    __instance = {}

    def __new__(cls, *args, **kwargs):
        if cls.__name__ not in cls.__instance:
            cls.__instance[cls.__name__] = super(cls.__class__, cls).__new__(cls, *args, **kwargs)
            setattr(cls.__instance[cls.__name__], '__init', True)
            setattr(cls.__instance[cls.__name__], '__init', False)
        return cls.__instance[cls.__name__]

    def __init__(self, *args, **kwargs):
        if getattr(self, '__init', False):
            # do initialization here

Alternatively consider the Borg Pattern below.

Borg Pattern

class Borg(object):
    __state = {}

    def __init__(self):
        self.__dict__ = self.__state
        if 'my_data' not in self.__dict__:
            # do initialization here, i.e.:
            self.my_data = 'initialize your data'

listening to posix style signals

Dec 8, 2011   coding

A quick self reminder

#include <stdio.h>
#include <string.h>
#include <signal.h>

void handler(int signal)
    printf("Signal: %s\n", strsignal(signal));
    if (signal == SIGABRT)
    raise(SIGKILL); // can't be caught or ignored

int main(int argc, char *argv[])
    signal(SIGABRT, handler); // Abort
    signal(SIGFPE, handler);  // Floating-Point Exception
    signal(SIGILL, handler);  // Illegal Instruction
    signal(SIGINT, handler);  // Interrupt
    signal(SIGSEGV, handler); // Segmentation Violation
    signal(SIGTERM, handler); // Terminate
    signal(SIGQUIT, handler); // Terminal quit

    printf("Hello Crazy World!\n");

    raise(SIGABRT); // raise Abort

    printf("I am never going to be printed...\n");

    return 0;

git memo

Mar 25, 2010   coding
create new repo with first commit and push

assuming you have code in a directory called hayate

  • cd into hayate directory cd /path/to/hayate
  • git init
  • git remote add origin
  • then git add .
  • git commit -a -m 'added hayate repo'
  • then for the first push: git push origin master:refs/heads/master which creates the master branch
git tag v1.0 -m 'my first tag'
git push --tags
git tag -l
clone into current directory
  • git clone . <- note the dot (hayate directory will not be created)
change working branch
  • git branch -l lists local branches
  • git checkout <branch-name> switches working branch to "branch-name"
remove an added file or directory (before it is committed)
  • git reset <file-or-directory-name>
add submodule (an external repository) within local source tree
  • git submodule add <repo-url> <local-path>
    i.e. git submodule add hayate
checkout a submodule
  • git submodule update --init
create a new remote branch tracking it locally
  • git checkout -b <branch_name>
  • git push -u origin <branch_name>
  • git checkout <branch_name> to switch to the new branch
start tracking a remote branch
  • git checkout --track -b <local name> origin/<remote name>
delete a remote branch
  • git push origin --delete <branch_name>
delete local branch
  • git branch -D <branch_name>
revert a merge
  • git revert -m 1 merge_hash
to suppress the following warning
warning: You did not specify any refspecs to push, and the current remote
warning: has not configured any push refspecs. The default action in this
warning: case is to push all matching refspecs, that is, all branches
warning: that exist both locally and remotely will be updated.  This may
warning: not necessarily be what you want to happen.
warning: You can specify what action you want to take in this case, and
warning: avoid seeing this message again, by configuring 'push.default' to:
warning:   'nothing'  : Do not push anything
warning:   'matching' : Push all matching branches (default)
warning:   'tracking' : Push the current branch to whatever it is tracking
warning:   'current'  : Push the current branch
  • git config [--global] push.default current (--global is optional)


tic text image converter

Apr 20, 2009   coding

This is a php5 library (two classes) I wrote to convert TrueType Font text into an image, using the GD library.

The project is hosted and can be downloaded from github: (it includes an example, documentation and some free fonts) or using git: git clone git://

Documentation is also available online here:

To extend the library to add support for other Font types just extend the TIC abstract class and implement the create and dimension methods.

See example below on how to use it:
setText('Hello World !')
    ->setBgColor(0x00, 0xff, 0xff)
    ->setFontColor(0x00, 0x00, 0x00)
Than in your html:
<img src="image.php" alt="TIC" />

That will output the following image:


The library is under the LGPL license, which means you can freely use it for commercial and non commercial applications, please post a comment for feedbacks, bugs, or links of projects where you have use it.

subversion propset svnignore

Apr 2, 2009   coding

This is a quick self-reminder on propset svn:ignore command.

To ignore all content in directory foo but not the directory:

svn propset svn:ignore '*' foo

To ignore the directory and its content:

svn propset svn:ignore 'foo' . 

web font tester

Jan 18, 2009   coding

The quick brown fox jumps over the lazy dog !

Font Name  (i.e. Arial, Courier etc.)
Font Size

linux pipe find rm

Sep 13, 2008   coding
find /path/to/dir -name name_of_file_to_match | xargs /bin/rm

I use often the above command to clean a directory from all the back up files created by emacs, so for example if I am developing a site at: /var/www/a-site/ then at the end of the day or at the beginning of a new one I clean up the directory as follow:

find /var/www/a-site -name '*~' | xargs rm

Be careful with the above command, specially if you run it as root, you could get the wrong regex and delete things you don't mean to.

compile emacs mode extensions

Jun 11, 2008   coding
M-x byte-compile-file [RET]

This is: press the "Esc" key (top left on my keyboard), then the letter x, then type "byte-compile-file" and press the return key.

Or to compile all .el files in a directory:

[ESC] x byte-recompile-directory [RET]