python refactor