crazy refactoring magic with python