2017-10-03 39 views
0

私の教授から割り当てられたコードを使用していますが、指示ごとに変更することはできません。私は周りを見回して、最初の書き込みの周りに閉鎖を追加しましたが、スターターコードの変更を防ぐためにそこにありますか?私は輸入品の下に修正を入れAttributeError:Wave_writeインスタンスに属性 '__exit__'がありません

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__' 

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())) 

エラーメッセージコードを呼び出す

コードから来ましたそれは動作します!

修正(更新):

#### 
# 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 

この行のwithキーワードはwave.openコンテキストマネージャとして書き込まれていることを意味し、それはしていません。 withを取り出し、代わりにこれを行う:あなたはwith文の中Wave_writeインスタンスを使用している

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

を参照してください。あなたはpython2を使っているようですが、そうではありませんでした。

教授と同じバージョンを使用する必要があります。そうしないと、常にこのような問題が発生します。だから、おそらくpython3に切り替えるべきです。

関連する問題