I can recommend Architecture Patterns with Python [0] to learn how to refactor code in this way. I don't know Python but I found the examples to be relevant to many other languages as well!
[0]: https://learning.oreilly.com/library/view/architecture-patte...