Tuesday, 1 September 2020

Stay SOLID and DRY with coroutines and functions as methods in python

I have that Code example:

from time import sleep
import asyncio

class bird:

    def __init__(self, sleeptime=1):
        self.var = sleeptime

    def wait_meep(self):
        sleep(self.var)
        print("Meep")

    def do_sth(self):
        print("Dop Dop Do do ...")


class bird_async:

    def __init__(self, sleeptime=1):
        self.var = sleeptime

    async def wait_meep(self):
        await asyncio.sleep(self.var)
        print("Meep")

    def do_sth(self):
        print("Dop Dop Do do ...")

As you can see, both clients are mostly identical and shall contain the same name (so that everyone knows what to expect). Now I want to be DRY and write bird_async(bird). Because every extension in bird shall be used in bird_async as well. This is possible, but my coworker says, it is not SOLID, because I have overwritten wait_meep. Now I am looking for different soultions and found abstract classes. What I don't know is, if creating an abstract class birdBase(ABC) would be SOLID as well. I would overwrite there as well, because first it was a function and then a couroutine, or am I wrong here?

What could be a SOLID and DRY solution to get those both classes together, without renaming the methods?



from Stay SOLID and DRY with coroutines and functions as methods in python

No comments:

Post a Comment