It's a shame that all this works flawlessly in Python, but the runtime makes everything prohibitively slow.
Have you tried using tools such as PyPy, Numba or other option mentioned at
https://pybenchmarks.org/ to increase performance? I rarely use those option for my use case, but take note those tool might cause weird bug.