2014-02-07 61 views
0

爲什麼這些方法不能像我期望的那樣工作?我可以在 這個方法後打印一行,但是,其他的功能似乎都不起作用。調用類中方法的方法的問題

我想

  1. 指定目錄
  2. 迭代在文件中使用的glob
  3. 指定文件處理程序目錄
  4. 找到的每個文件使用的方法彙總,然後調用一系列相互呼叫的其他方法。
  5. 排序和解析文件
  6. 打印排序和解析的結果。

代碼:

#! /usr/bin/env python3 
#This utility attempts to parse the output text files generated by dcp_inspect 
#for faster inspection and when attending to multiple DCP inspections. 
#dcp_inspect_parse runs on Python3.x 
import logging 
import os 
import re 
import sys 

from glob import glob 

logging.basicConfig(level=logging.DEBUG) 

class DcpParser: 
    """Parses DCP Inspector output files, and writes summary files to a 
    directory whose path is supplied at construction.""" 



    def parse(self, dir): 
     """Parses all files in the current directory.""" 
     cpl     = [] 
     content    = [] 
     contentkind   = [] 
     container   = [] 
     errors    = [] 
     package    = [] 
     summary    = [] 
     mainsound   = [] 
     mainpicture   = [] 
     encryption   = [] 
     duration    = [] 
     fsp     = [] 
     dimensiontype  = [] 
     aspect    = [] 
     filesize    = [2] 
     audio    = [] 
     cpltype    = [] 
     os.chdir(dir) 
     files = glob('*') 
     logging.debug("Found files: %s", files) 
     for file in files: 
      try: 
       with open(file) as data: 
        logging.debug("Opening file: %s", data) 
        print("Scanning...\t\t\t", file) 
        for each_line in data: 
         #self.prints() 
         self.file_summary() 
         #self.audio() 

      except: 
       pass 



    def prints(self): 

      print('Container:    \t\t',container.strip())   
      print('CPL Type:    \t\t',cpltype) 
      print('Duration:    \t\t',duration.strip(',')) 
      print('Frame Rate:   \t\t',fps) 
      print('Aspect Ratio:   \t\t',aspect.strip(',')) 
      print('Dimension:    \t\t',dimensiontype.strip(',')) 
      print('Content Title:   \t\t',content.strip(',').strip('""')) 
      print('Audio for DCP:   \t\t', audio) 
      print('Package size:   \t\t',filesize) 
      print('Encryption status:  \t\t',encryption.upper().strip())  
      print('CPL ID:    \t\t', cpl[2]) 
      print('Content Kind:   \t\t',contentkind.upper()) 
      print('\n') 
      print('There are',errors[1],'Errors and',errors[3],'hints for', content.strip(',')) 
      if errors[1] != '0': 
       print('could be issues\n') 
      else: 
       print('This DCP appears to be OK\n')  


    def file_summary(self): 
     print('made it to summary') 
     self.file_audio() 
     for each_line in data: 
      if 'summary'in each_line: 
       summary.extend(each_line.split()) 
       print(summary.extend(each_line.split())) 
       for x in range(len(summary)): 
        if 'fps' in summary[x]: 
         fps = summary[(x-1)] 
         duration = summary[(x-2)].strip() 

        elif 'summary:' in summary[x]: 
         content = summary[(x+1)] 
         content.strip(',') 

        elif '2D' in summary[x]: 
         dimensiontype = summary[(x)] 
         aspect = summary[(x+1)].strip(',') 

        elif '3D' in summary[x]: 
         dimensiontype = summary[(x)] 
         aspect = summary[(x+1)] 

      elif 'Errors' in each_line: 
       errors=(each_line.split()) 
      elif 'Package with total size'in each_line: 
       if 'Bytes 0' in each_line: 
        pass 
       else: 
        temp =[] 
        temp.extend(each_line.split()) 
        filesize=(temp.pop()+' '+temp.pop()) 


    def file_audio(self): 
     print('made it to audio') 
     self.file_picture() 
     for each_line in data: 

      if 'MainSound'in each_line: 

       if 'audio' in each_line: 
        m = re.search('([0-9]+)bps', each_line) 
        if m is None: 
         bps = 'bps error' 
        else: 
         bps = int(m.group(1)) 

        m = re.search('([0-9]+)ch', each_line) 
        if m is None: 
         channels = 'channel error' 
        else: 
         channels = int(m.group(1)) 

        m = re.search('([0-9]+)kHz', each_line) 
        if m is None: 
         bandwidth = 'bandwidth error' 
        else: 
         bandwidth = int(m.group(1)) 

        audio = '%ich %ikHz %ibps' % (channels, bandwidth, bps) 


    def file_picture(self): 
     print('made it to picture') 
     for each_line in data: 

      if 'MainPicture'in each_line: 
       if 'pictures' in each_line: 
        temp = [] 
        temp.extend(each_line.split(',')) 
        container = temp[-2] 
        encryption= temp[-3] 




    def file_cplId(self): 
     print('cpl id') 
     if 'CPL Id:'in each_line: 
      cpl=(each_line.split()) 


    def file_cplType(self): 
     if 'CPL type: 'in each_line: 
      cpltype=(each_line.split()) 
      cpltype = cpltype[2] 

    def file_contentKind(self): 
     if 'ContentKind: 'in each_line: 
      contentkind=(each_line.split()) 
      contentkind = contentkind[1] 

    def file_contentTitleText(self): 
     if 'ContentTitleText:'in each_line: 
      content=(each_line.split(':')) 
      content = content[1] 










if __name__ == '__main__': 
    print("Input directory name: " + os.getcwd()) 
    default_dir = os.getenv('HOME') + '/Desktop/dcp_output' 
    prompt = "Please enter the output directory name [" + default_dir + "]: " 
    dir = input(prompt) or default_dir 
    print("Output directory name: " + dir) 

    parser = DcpParser() 

    parser.parse(dir) 
+2

你有可能把它縮小一點嗎?嘗試閱讀http://sscce.org – jonrsharpe

+2

「爲什麼這些方法不按我期望的那樣工作?」你能解釋一下你的期望,以及實際發生了什麼? – SethMMorton

回答

2

我覺得奇怪,我沒有你的變量開始self.,這意味着你沒有任何類成員,所有的變量都在各自的方法本地。

同樣方便您在代碼的主要部分周圍使用try except pass,它將忽略發生的任何錯誤。其中一個錯誤是您可以通過幾種方法訪問data,這些方法既不是全局變量也不是以前在該方法中分配的。

刪除try except pass用於調試目的並報告發生了什麼。

+0

你說得對。數據讓我感到悲傷。那麼我只需要聲明數據? – inbinder

+0

@ user1124541我想它應該是一個類成員,在這種情況下,您需要使用'self.data'而不是'data'。 – Nabla

+0

我如何讓所有功能/方法都可以訪問類成員? – inbinder