Refactor the "py_search" function
Update tests