Cosimulate SystemVerilog and python

You may be interested in an open source project called Cocotb which abstracts VPI to make it easy to interact with a simulation from Python.

It works in a similar manner to the code you’ve posted above, by embedding the Python interpreter in the simulator. On elaboration complete it loads a Python module (defined by an environment variable) and calls a function with a handle to the root of the simulation. From Python you can call your model, read signal values from the simulator or drive directly onto signals.

If you want to interface Python to a simulation it might save you some time.