2013-06-04 54 views
1

我有這個類由3個函數組成。每個功能都負責整個過程的一部分。從其他函數傳遞值和調用函數

​​加載兩個文件,重新格式化它們的內容並將它們寫入兩個新文件。

.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])) 

現在我的問題,我該如何使它所以每一個函數傳遞它的輸出文件作爲值到下一個功能,並調用它?

因此,對於一個例子:

運行​​應該輸出兩個文件,調用.compare()函數傳遞這兩個文件。

然後當.compare()完成後,它應該通過.final()輸出文件並調用它。

因此,.final()將打開從.compare()傳遞給它的任何文件,而不是如上所定義的"test123.txt"

我希望這一切都有道理。讓我知道你是否需要澄清。對於代碼本身,任何批評都是值得歡迎的。提前致謝。

回答

3

有幾種方法可以做到這一點,但是我會編寫一個主函數來依次調用其他三個函數。例如:

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) 

查看您的代碼,我認爲您在加載時遇到問題。你只聲明一個字典,然後將兩個文件的內容加載到它中,並將這些內容寫入兩個文件。由於每個文件具有相同的內容,比較不會做任何有意義的事情。

另外,你真的想寫出文件內容,然後重新讀入內存嗎?我會保持框架定義在內存中,以便在加載後使用,而不是將它們讀回來。

我真的不認爲這是一個類的原因,而不僅僅是三個函數,但也許如果你必須讀取多種格式不同的文件,你可以在繼承一般邏輯的同時使用類屬性來定義格式。

+0

是的,你對我的負載的問題是正確的,我修好了,謝謝!我唯一的問題是,我如何通過open()行將「result_file」傳遞給final?如果我這樣做:「打開(result_file +'。txt','r')爲fin:」它返回「沒有這樣的文件或目錄」。你有可能把我指向正確的方向嗎? – MaxPower

+1

那麼,那個錯誤意味着它沒有找到預期的文件 - 你需要確認它存在於你提供的位置。如果這意味着來自'compare'的輸出,那麼我可能會在master函數中構建我期望的文件名,並將它作爲參數傳遞給'compare'和'final',以確保它們。 –

3

你的意思是跟兩個文件的名字叫?那麼你定義了一個類,所以你可以這樣做:

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

依此類推。

1

我可能完全不在這裏,但爲什麼不按照你所說的去做呢?

只需撥打self.compare()load()方法。

您還可以將返回語句添加到load()並返回一個tuple與文件。

然後向你的班級添加第四種方法,然後收集返回的文件,並將它們傳送給compare()方法。

最好的問候!

1

Python的一個更強大的方面是你可以返回一個叫tuple的東西。要在更一般的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') 

每個函數有兩個命名的參數,但第一個返回的元組可以「解包」進入第二個的輸入參數通過添加*在它前面。