port of lagerhythm from Perl to Python