2017-10-03 32 views
0

교수님이 과제에 사용할 일부 코드를 사용하면서 지침별로 변경할 수 없습니다. 나는 주위를 둘러 보았고 초기 글씨를 닫는 것을 추가했지만 시작 코드를 수정하는 것을 막기 위해 거기에 있는가?AttributeError : Wave_write 인스턴스에 '__exit__'속성이 없습니다.

코드에서 온 : 호출

코드 :

def run_menu(): 

    global CURRENT_TIME 

    # Provide a minimal indication that the program has started. 
    print(MINIMAL_HELP_STRING) 

    # Get the first keystroke. 
    c = readchar.readchar() 

    # Endless loop responding to the user's last keystroke. 
    # The loop breaks when the user hits the QUIT_MENU_KEY. 
    while True: 

     # Respond to the user's input. 
     if c == FORWARD_KEY: 

      # Advance the time, looping back around to the start. 
      CURRENT_TIME += 1 
      if CURRENT_TIME == len(NUMBERS_WAV): 
       CURRENT_TIME = 0 

      # Concatenate three audio files to generate the message. 
      sound.combine_wav_files(TMP_FILE_WAV, YOU_SELECTED_WAV, 
            NUMBERS_WAV[CURRENT_TIME], AM_WAV) 

      # Play the concatenated file. 
      sound.Play(TMP_FILE_WAV) 

기능 코드 :

def combine_wav_files(out_file, *files): 
    with wave.open(out_file, 'wb') as out: 
     with wave.open(files[0], 'rb') as first_in: 
      (nchannels, sampwidth, framerate, nframes, comptype, compname) =\ 
       first_in.getparams() 
      out.setparams(first_in.getparams()) 
     for filename in files: 
      with wave.open(filename, 'rb') as cur_in: 
       if (cur_in.getnchannels() != nchannels or 
        cur_in.getsampwidth() != sampwidth or 
        cur_in.getframerate() != framerate or 
        cur_in.getcomptype() != comptype or 
        cur_in.getcompname() != compname): 
        raise Exception('Mismatched file parameters: ' + filename) 
       out.writeframes(cur_in.readframes(cur_in.getnframes())) 

오류 메시지 :

Exception wave.Error: Error('# channels not specified',) in <bound method Wave_write.__del__ of <wave.Wave_write instance at 0x104029e60>> ignored 
Traceback (most recent call last): 
    File "sample_menu.py", line 144, in <module> 
     main() 
    File "sample_menu.py", line 25, in main 
     run_menu() 
    File "sample_menu.py", line 113, in run_menu 
     NUMBERS_WAV[CURRENT_TIME], AM_WAV) 
    File "/Users/jaredsmith/Desktop/443/P1 Starter Code 2017/sound.py", line 86, in combine_wav_files 
     with wave.open(out_file, 'wb') as out: 
AttributeError: Wave_write instance has no attribute '__exit__' 

나는 수입에서 수정을 넣어 그리고 그것은 작동합니다!

수정 (업데이트) : 그렇지가

#### 
# From http://web.mit.edu/jgross/Public/21M.065/sound.py 9-24-2017  
#### 

def _trivial__enter__(self): 
    return self 
def _self_close__exit__(self, exc_type, exc_value, traceback): 
    self.close() 

wave.Wave_read.__exit__ = wave.Wave_write.__exit__ = _self_close__exit__ 
wave.Wave_read.__enter__ = wave.Wave_write.__enter__ = _trivial__enter__ 

답변

0

. 제대로 작동하려면 Wave_write가 컨텍스트 관리자 여야합니다. 즉, __enter__()__exit__() 메서드를 모두 구현해야합니다. 여기서는 그렇지 않습니다.

Wave_write에 __exit__ 메서드를 추가하거나 with 문을 제거하고 수동으로 입력을 닫아야합니다 (필요한 경우). 예 : 그 코드가 wave.open()에 의해 반환되는 객체가 문맥 관리자로 사용할 수 있습니다 후 그는 python3를 사용하고 있었다 당신의 교수가 제공 한 경우

out = wave.open(out_file, 'wb'): 
    [do_stuff] 
out.close() # if Wave_write implements a closing method, use it. the with statement and __exit__() method would have handled that for you. 

https://docs.python.org/2/reference/compound_stmts.html#withhttps://docs.python.org/2/library/contextlib.html

+0

감사! 그것을 고치는 방법을 이해할 수 있도록 도와주었습니다. –

0

with wave.open(out_file, 'wb') as out 

wave.open 컨텍스트 매니저로 기록 된 것을 의미한다이 라인의 with 키워드,하지만. with을 가지고 대신 이렇게 : 당신은 with 문 내부 Wave_write 인스턴스를 사용하는

out = wave.open(out_file, 'wb') 
0

를 참조하십시오. 당신은 파이썬 2를 사용하고있는 것처럼 보입니다.

교수님과 같은 버전을 사용해야합니다. 그렇지 않으면 항상 이러한 문제가 발생합니다. 그래서 아마 python3으로 전환해야합니다.