replace PLY legacy interface of module-level functions by recommended object methods

because the legacy interface may not be supported in the future and using objects is explicit (local variables better than global ones).