lundi 28 août 2017

How to create generic method for methods that have the same structure?

I have the following method in CommentsService class:

async def background_job_auto_approve(self):
    while True:
        new = get_comments_by_status(CommentStatus.NEW.value)
        pending = get_comments_by_status(CommentStatus.PENDING.value)
        all_comments = new + pending
        for comment in all_comments:
            if check_it_auto_approve(item=comment):
                await self.auto_approve(comment_id=comment['comment_id'],
                                        alert_id=comment['alert_id'])
                yield comment
        await asyncio.sleep(self.check_expire_seconds)

But I have exactly the same method in my AlertsService:

async def background_job_auto_approve(self):
    while True:
        new = get_alerts_by_status(AlertStatus.NEW.value)
        pending = get_alerts_by_status(AlertStatus.PENDING.value)
        all = new + pending
        for alert in alerts:
            if check_it_auto_approve(item=alert):
                await self.auto_approve(alert_id=alert['alert_id'])
                yield alert
        await asyncio.sleep(self.check_expire_seconds)

How to avoid code duplication?

Aucun commentaire:

Enregistrer un commentaire