python: Remove duplicate code