Skip to main content
 首页 » 编程设计

python之从其他函数传递值和调用函数

2025年05月04日13虾米姐

我有一个包含 3 个函数的类。每个功能负责整个过程的一部分。

.load() 加载两个文件,重新格式化它们的内容并将它们写入两个新文件。

.compare() 获取两个文件并以特定格式打印出它们的差异。

.final() 获取 .compare() 的结果并为每组值创建一个文件。

请忽略逻辑中科学怪人的本质,因为这不是我目前主要关心的问题。我知道它可以写得好一千倍,这对我来说很好,因为我对 Python 和一般编程还是陌生的。我确实有一些理论经验,但技术实践非常有限,这是我正在努力的事情。

代码如下:

from collections import defaultdict 
from operator import itemgetter 
from itertools import groupby 
from collections import deque 
import os 
 
 
class avs_auto: 
 
 
    def load(self, fileIn1, fileIn2, fileOut1, fileOut2): 
        with open(fileIn1+'.txt') as fin1, open(fileIn2+'.txt') as fin2: 
            frame_rects = defaultdict(list) 
            for row in (map(str, line.split()) for line in fin1): 
                id, frame, rect = row[0], row[2], [row[3],row[4],row[5],row[6]] 
                frame_rects[frame].append(id) 
                frame_rects[frame].append(rect) 
            for row in (map(str, line.split()) for line in fin2): 
                id, frame, rect = row[0], row[2], [row[3],row[4],row[5],row[6]] 
                frame_rects[frame].append(id) 
                frame_rects[frame].append(rect) 
 
        with open(fileOut1+'.txt', 'w') as fout1, open(fileOut2+'.txt', 'w') as fout2: 
            for frame, rects in sorted(frame_rects.iteritems()): 
                fout1.write('{{{}:{}}}\n'.format(frame, rects)) 
                fout2.write('{{{}:{}}}\n'.format(frame, rects)) 
 
    def compare(self, f1, f2): 
        with open(f1+'.txt', 'r') as fin1: 
            with open(f2+'.txt', 'r') as fin2: 
                lines1 = fin1.readlines() 
                lines2 = fin2.readlines() 
                diff_lines = [l.strip() for l in lines1 if l not in lines2] 
                diffs = defaultdict(list) 
                with open(f1+'x'+f2+'Result.txt', 'w') as fout: 
                    for line in diff_lines: 
                        d = eval(line) 
                        for k in d: 
                            list_ids = d[k] 
                            for i in range(0, len(d[k]), 2): 
                                diffs[d[k][i]].append(k) 
                    for id_ in diffs: 
                        diffs[id_].sort() 
                        for k, g in groupby(enumerate(diffs[id_]), lambda (i, x): i - x): 
                            group = map(itemgetter(1), g) 
                            fout.write('{0} {1} {2}\n'.format(id_, group[0], group[-1])) 
 
    def final(self): 
        with open('hw1load3xhw1load2Result.txt', 'r') as fin: 
            lines = (line.split() for line in fin) 
            for k, g in groupby(lines, itemgetter(0)): 
                fst = next(g) 
                lst = next(iter(deque(g, 1)), fst) 
                with open('final/{}.avs'.format(k), 'w') as fout: 
                    fout.write('video0=ImageSource("MovieName\original\%06d.jpeg", {}, {}, 15)\n'.format(fst[1], lst[2])) 

现在回答我的问题,我如何才能让每个函数都将其输出文件作为值传递给下一个函数并调用它?

举个例子:

运行 .load() 应该输出两个文件,调用 .compare() 函数将这两个文件传递给它。

然后当 .compare() 完成后,它应该将输出文件传递给 .final() 并调用它。

因此 .final() 将打开从 .compare() 而不是 "test123.txt" 传递给它的任何文件它在上面定义。

我希望这一切都有意义。如果您需要澄清,请告诉我。欢迎对代码本身提出任何批评。提前致谢。

请您参考如下方法:

有几种方法可以做到这一点,但我会编写一个主函数来依次调用其他三个函数。像这样的东西:

def load_and_compare(self, input_file1, input_file2, output_file1, output_file2, result_file): 
    self.load(input_file1, input_file2, output_file1, output_file2) 
    self.compare(output_file1, output_file2) 
    self.final(result_file) 

查看您的代码,我认为您的负载有问题。您只需声明一个字典,然后将两个文件的内容加载到其中并将这些相同的内容写到两个文件中。因为每个文件都有相同的内容,所以比较不会做任何有意义的事情。

还有,你真的要写出文件内容,然后重新读入内存吗?我会将框架定义保存在内存中,以便在加载后进行比较,而不是将它们读回。

我真的不认为这是一个类而不仅仅是三个函数的原因,但也许如果你必须读取格式略有不同的多个文件,你可以使用类属性获得一些好处在继承通用逻辑的同时定义格式。