python-3.x 当我调用某个方法时,它不能正常工作

4smxwvx5  于 2022-11-19  发布在  Python
关注(0)|答案(1)|浏览(136)

我需要在日志文件中进行两次检查并显示结果。单独的方法工作正常,但当我运行所有代码方法hit_unique_check时总是返回“PASS:所有命中都是唯一的."。对于三个**.log**文件中的两个,此结果不正确。

import os

class ReadFiles:

    def __init__(self):
        self.current_file = ""
        self.shoot_from = "Shoot from"
        self.hit_player = "Hit player"

    def equally_check(self):
        shoot_from_list = []
        hit_player_list = []
        for line in self.current_file:
            if self.shoot_from in line:
                shoot_from_list.append(line)
            elif self.hit_player in line:
                hit_player_list.append(line)
        if len(shoot_from_list) == len(hit_player_list):
            print(" PASS: Shoots and hits are equal.\n")
        else:
            print(" FAIL: Shoots and hits are NOT equal.\n")

    def hit_unique_check(self):
        unique_hit_list = []
        duplicates = []
        for line in self.current_file:
            if self.hit_player in line:
                unique_hit_list.append(line)
            else:
                continue
        for i in unique_hit_list:
            if unique_hit_list.count(i) > 1:
                duplicates.append(i)
                print(i)
            else:
                continue
        if len(duplicates) < 1:
            print(" PASS: All hits are unique.\n")
        else:
            print(" FAIL: This hits are duplicated.\n")

    def run(self):
        for file in os.listdir():
            if file.endswith(".log"):
                print(f"Log file - {file}")
                self.current_file = open(f"{file}", 'rt')
                print(self.current_file.readlines, f"")
                self.equally_check()
                self.hit_unique_check()
                self.current_file.close()

if __name__ == "__main__":
    run = ReadFiles()
    run.run()

我运行我的python代码,但结果总是一样:“* 通过:所有匹配项都是唯一的。"。对于某些文件,它必须是“ 失败:这个点击是重复的。*"。我不确定这个问题在方法hit_unique_check中,也不知道该怎么办。
你能不能给我解释一下,我怎样才能使这个方法不仅正确地工作,而且单独地工作?

bihw5rsg

bihw5rsg1#

考虑这种组织。每个函数都有一个任务,即计算并返回结果。调用者决定如何处理结果。还要注意,我使用的是计数器而不是列表,因为你并不真正关心列表包含什么。还要注意defaultdict的使用,以避免重复搜索命中列表。

import os
from collections import defaultdict

class ReadFiles:

    def __init__(self):
        self.shoot_from = "Shoot from"
        self.hit_player = "Hit player"

    def equally_check(self, lines):
        shoot_from = 0
        hit_player = 0
        for line in lines:
            if self.shoot_from in line:
                shoot_from += 1
            elif self.hit_player in line:
                hit_player += 1
        return shoot_from == hit_player

    def hit_unique_check(self, lines):
        unique_hit_list = defaultdict(int)
        for line in lines:
            if self.hit_player in line:
                unique_hit_list[line] += 1
        duplicates = 0
        for k,v in unique_hit_list.items()
            if v > 1:
                duplicates += 1
                print(k)
        return not duplicates

    def run(self):
        for filename in os.listdir():
            if filename.endswith(".log"):
                print(f"Log file - {filename}")
                lines = open(filename, 'rt').readlines()
                print(lines)
                if self.equally_check(lines):
                    print(" PASS: Shoots and hits are equal.\n")
                else:
                    print(" FAIL: Shoots and hits are NOT equal.\n")
                if self.hit_unique_check(lines):
                    print(" PASS: All hits are unique.\n")
                else:
                    print(" FAIL: This hits are duplicated.\n")

if __name__ == "__main__":
    run = ReadFiles()
    run.run()

您甚至可以用计数器替换hit_unique_check中的循环:

from collections import Counter
...
    def hit_unique_check(self,lines):
        unique_hit_list = Counter(lines)
        for k,v in unique_hit_list,items():
...

相关问题