【问题标题】:Python assert is lying? 1 != 1Python 断言在撒谎? 1 != 1
【发布时间】:2014-05-03 10:50:25
【问题描述】:

我有一段代码

   assert (len(max_test_scores) != 1), \
          "Internal error - migration 0011 - module Programs." \
          " Please contact with developers - " + str(len(max_test_scores))

在执行这段代码的过程中,我遇到了一个断言错误:

AssertionError:内部错误 - 迁移 0011 - 模块程序。请与开发者联系 - 1

所以 1 != 1 ?

我一直在搜索、谷歌搜索并思考这是如何发生的,但我不知道。这是带有上下文的代码

def forwards(self, orm):
    problems_and_groups = orm.Test.objects \
            .values('problem', 'group').distinct()

    problems_instances_and_groups = []
    for pi in orm['contests.ProblemInstance'].objects.all():
        for pg in problems_and_groups:
            if pi.problem.pk == pg['problem']:
                problems_instances_and_groups \
                        .append({'problem_instance': pi.pk,
                                 'group': pg['group']})

    count = len(problems_instances_and_groups)
    num = 0
    update_every = max(1, count / 20)
    pb = ProgressBar(count)
    print "Migrating %d groups" % count
    for pig in problems_instances_and_groups:
        if num % update_every == 0:
            pb.update(num)
        num += 1

        submissions = orm['contests.Submission'].objects \
                .filter(problem_instance = pig['problem_instance']) \
                .values('pk')

        print submissions

        submission_reports = orm['contests.SubmissionReport'].objects \
                .filter(submission__in = submissions).values('pk')

        print submission_reports

        test_reports = orm.TestReport.objects \
                .filter(test_group = pig['group'],
                        submission_report__in = submission_reports)

        max_score = None

        max_test_scores = frozenset(test_report.test.max_score
                for test_report in test_reports)

        assert (len(max_test_scores) != 1), \
                "Internal error - migration 0011 - module Programs." \
                " Please contact with developers - " + str(len(max_test_scores))

        max_score = ScoreValue(list(max_test_scores)[0])

        GroupReport.filter(test_group=pig['group'],
                submission_report__in = submission_reports) \
                .update(max_score=max_score)

【问题讨论】:

  • 你在哪里证明len(max_test_scores)不等于1
  • @LukasGraf 在断言消息中。
  • @MatthewTrevor 是的,因为 str(len(max_test_scores)) 等于 1
  • @bdfhjk 这就是你的断言。它失败了。所以len(max_test_scores) 显然 等于1。但是您告诉我们它不是,因此“Python 在撒谎”。而且您的示例不包含以其他方式显示的必要数据。
  • 好吧,我知道问题出在哪里了 :)

标签: python django assertions


【解决方案1】:

当表达式不为真时,断言失败

>>> val = 1
>>> assert val != 1, 'Oops, val is 1'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError: Oops, val is 1

断言表达式的计算结果必须为真,断言才不会失败。

如果要断言设置的长度为 1,则测试 是否相等

您也不需要那么多反斜杠:

assert len(max_test_scores) == 1, (
    "Internal error - migration 0011 - module Programs. "
    "Please contact with developers - {}".format(len(max_test_scores)))

【讨论】:

    【解决方案2】:

    如果断言是Falseassert statement 会抛出一个AssertionError

    在这种情况下,显然是,因为在您的示例中,len(max_test_scores) 返回1(如assert 错误消息中所示,而1 != 1False

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多