reimplemented grep as python code