Python 从其他函数传递值和调用函数

Python 从其他函数传递值和调用函数,python,python-2.7,Python,Python 2.7,我有一个由3个函数组成的类。每个职能部门负责整个流程的一部分 .load()加载两个文件,重新格式化其内容并将其写入两个新文件 .compare()获取两个文件,并以特定格式打印它们之间的差异 .final()获取.compare()的结果,并为每组值创建一个文件 请忽略逻辑的弗兰肯斯坦本质,因为这不是我目前主要关心的问题。我知道它可以写得好上千倍,这对我来说现在还不错,因为我对Python和编程还是个新手。我确实有一些理论经验,但技术实践非常有限,这是我正在努力的 代码如下: from col

我有一个由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(self, fileIn1, fileIn2, fileOut1, fileOut2):
    ... // do stuff here
    // when done
    self.compare( fileOut1, fileOut2 )

等等。

你是说用这两个文件的名称来调用吗?您定义了一个类,因此可以执行以下操作:

def load(self, fileIn1, fileIn2, fileOut1, fileOut2):
    ... // do stuff here
    // when done
    self.compare( fileOut1, fileOut2 )

等等。

我可能完全不在这里,但你为什么不完全按照你说的去做呢

只需从
load()
方法中调用
self.compare()

您还可以将返回语句添加到
load()
中,并返回带有文件的
元组

然后向类中添加第四个方法,该方法将收集返回的文件并将它们传递到
compare()
方法


致以最良好的祝愿

我可能完全不在这里,但你为什么不照你说的做呢

只需从
load()
方法中调用
self.compare()

您还可以将返回语句添加到
load()
中,并返回带有文件的
元组

然后向类中添加第四个方法,该方法将收集返回的文件并将它们传递到
compare()
方法


致以最良好的祝愿

有两种方法可以做到这一点,但我会编写一个主函数,按顺序调用其他三个。比如:

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

另外,您真的想写出文件内容,然后将其重新读入内存吗?我会将帧定义保存在内存中,以便在加载后用于比较,而不是将它们读回内存


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

有几种方法可以做到这一点,但我会编写一个主函数,按顺序调用其他三个。比如:

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

另外,您真的想写出文件内容,然后将其重新读入内存吗?我会将帧定义保存在内存中,以便在加载后用于比较,而不是将它们读回内存


我真的不认为这是一个类,而不仅仅是三个函数,但是,如果您必须读取格式略有不同的多个文件,那么在继承常规逻辑的同时使用类属性来定义格式可能会带来一些好处。

Python更强大的一个方面是,您可以返回一个名为。要用更一般的Python意义来回答这个问题,请考虑这个代码:

>>> def load(file1, file2):
        return file1+'.txt',file2+'.txt'

>>> def convert(file1, file2):
        return 'converted_'+file1,'converted_'+file2

>>> convert(*load("Java", "C#"))
('converted_Java.txt', 'converted_C#.txt')

每个函数都有两个命名参数,但是第一个函数返回的元组可以通过在前面添加一个
*
来“解包”到第二个函数的输入参数中。

Python更强大的一个方面是,可以返回一个名为。要用更一般的Python意义来回答这个问题,请考虑这个代码:

>>> def load(file1, file2):
        return file1+'.txt',file2+'.txt'

>>> def convert(file1, file2):
        return 'converted_'+file1,'converted_'+file2

>>> convert(*load("Java", "C#"))
('converted_Java.txt', 'converted_C#.txt')

每个函数都有两个命名参数,但是第一个函数返回的元组可以通过在前面添加一个
*
来“解包”到第二个函数的输入参数中。

是的,我的加载中的问题你是对的,我修复了它,谢谢!我唯一的问题是如何在with open()行中将“result_file”传递给final?如果我这样做:“将open(result_file+'.txt',r')作为fin:”它返回“没有这样的文件或目录”。你能给我指出正确的方向吗?这个错误意味着它找不到预期的文件-你需要确认它存在于你提供它的位置。如果这是
compare
的输出,我可能会在主函数中构造我期望的文件名,并将其作为参数传递给
compare
final
以确保。是的,我的加载中的问题你是对的,我已经解决了,谢谢!我唯一的问题是