试图用 Python 为医生和医院创建一个匹配算法,卡在条件上

根据单元测试文档doCleanups()是负责调用所有清理方法的方法。


一种方法是在 doCleanups 方法中检查测试用例的结果,如果测试用例失败则弹出所有清理方法并跳过其余测试用例


下面是代码:


import unittest


class SimpleTestCases(unittest.TestCase):

    FAILURE = False


    def setUp(self):

        print "\nmessage from function: setUp"

        if SimpleTestCases.FAILURE:

            self.skipTest("Test is skipped due to first failure")


            return super(SimpleTestCases, self).setUp()


        self.createResource()

        self.addCleanup(self.cleanResource)


    def createResource(self):

        msg = "\nmessage from function: createResource"


    def cleanResource(self):

        print "\nmessage from function: cleanResource"


    def test_func1(self):

        print "message from function: test_func1::start"

        print "message from function: test_func1::end"


    def test_func2(self):

        print "message from function: test_func2::start"


        self.assertTrue(False)


        print "message from function: test_func2::end"


    def test_func3(self):

        print "message from function: test_func3::start"

        print "message from function: test_func3:end"


    def doCleanups(self):

        print "message from function: doCleanups"

        if SimpleTestCases.FAILURE:

            return super(SimpleTestCases, self).doCleanups()


        result = getattr(self, '_outcomeForDoCleanups', self._resultForDoCleanups)


        ok_result = True

        exc_list = getattr(result, 'failures')


        if exc_list and exc_list[-1][0] is self:

            ok_result = ok_result and not exc_list[-1][1]


        if not ok_result:

            SimpleTestCases.FAILURE = True

            while self._cleanups:

                (func, args, kwargs) = self._cleanups.pop()


        return super(SimpleTestCases, self).doCleanups()


if __name__ == "__main__":

    result = unittest.main()


青春有我
浏览 281回答 1
1回答

吃鸡游戏

我将 First_round 更改为字典以使其更直接。我还会建议一些其他的结构更改,但之后会更多。一个解决方案是简单地输入一个 if 检查,看看是否指定了特定的医院,然后如果他有更好的分数就更换医生:def hospital_ranking_of_doctor(hospital, doctor):    return next(v for k, v in ranking_by_hospital[hospital].items() if v == doctor)Matches = {}Matches['Doctors'] = (doctors)First_round = {}# We loop through all the doctors one by onefor Doctor_ in ranking_by_doctors:    # Then we find which hospital the doctor prefers    favored_hospital = ranking_by_doctors[Doctor_].get(1.0)    # We test if that hospital has already had a doctor assigned    if favored_hospital not in First_round:        # If it has not, then we just assign the current doctor        First_round[favored_hospital] = Doctor_    else:        # If the hosptial was already assigned a doctor we compare         # that doctor with the new one and set the new one only if         # the hospital prefers him.        previously_assigned_doctor = First_round[favored_hospital]        if hospital_ranking_of_doctor(favored_hospital, previously_assigned_doctor) > hospital_ranking_of_doctor(favored_hospital, Doctor_):            First_round[favored_hospital] = Doctor_print(First_round)现在对于结构更改,我认为您还应该将排名结构从字典范围内的数字到医院/医生更改为只是偏好的有序列表或翻转它所以关键是医院/医生和优先级/score 是值。这将使检查医生的分数更自然,例如:ranking_by_hospital['Hospital_2']['Doctor_2'] > ranking_by_hospital['Hospital_2']['Doctor_3']因此,您可以将排名函数更新为:def hospital_ranking_of_doctor(hospital, doctor):    return ranking_by_hospital[hospital][doctor]或者简单地内联这些代码位。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python