试图将一个大的(可能非常)不可拾取的对象渲染到文件中,以供以后使用。

dill.dump(file)方面无投诉:

In [1]: import echonest.remix.audio as audio

In [2]: import dill

In [3]: audiofile = audio.LocalAudioFile("/Users/path/Track01.mp3")
en-ffmpeg -i "/Users/path/audio/Track01.mp3" -y -ac 2 -ar 44100 "/var/folders/X2/X2KGhecyG0aQhzRDohJqtU+++TI/-Tmp-/tmpWbonbH.wav"
Computed MD5 of file is b3820c166a014b7fb8abe15f42bbf26e
Probing for existing analysis

In [4]: with open('audio_object_dill.pkl', 'wb') as f:
   ...:     dill.dump(audiofile, f)
   ...:

In [5]:


但是尝试加载.pkl文件:

In [1]: import dill

In [2]: with open('audio_object_dill.pkl', 'rb') as f:
   ...:     audio_object = dill.load(f)
   ...:


返回以下错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-203b696a7d73> in <module>()
      1 with open('audio_object_dill.pkl', 'rb') as f:
----> 2     audio_object = dill.load(f)
      3

/Users/mikekilmer/Envs/GLITCH/lib/python2.7/site-packages/dill-0.2.2.dev-py2.7.egg/dill/dill.pyc in load(file)
    185     pik = Unpickler(file)
    186     pik._main_module = _main_module
--> 187     obj = pik.load()
    188     if type(obj).__module__ == _main_module.__name__: # point obj class to main
    189         try: obj.__class__ == getattr(pik._main_module, type(obj).__name__)

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.pyc in load(self)
    856             while 1:
    857                 key = read(1)
--> 858                 dispatch[key](self)
    859         except _Stop, stopinst:
    860             return stopinst.value

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.pyc in load_newobj(self)
   1081         args = self.stack.pop()
   1082         cls = self.stack[-1]
-> 1083         obj = cls.__new__(cls, *args)
   1084         self.stack[-1] = obj
   1085     dispatch[NEWOBJ] = load_newobj

TypeError: __new__() takes at least 2 arguments (1 given)


AudioObject比(从SO answer)进行上述调用的class object复杂得多(并且很大),我不清楚是否需要通过dill发送第二个参数,以及是否因此,该参数将是什么,或者如何判断对该特定对象是否可行的酸洗方法。

检查一下对象本身:

In [4]: for k, v in vars(audiofile).items():
...:     print k, v
...:


返回:

is_local False
defer False
numChannels 2
verbose True
endindex 13627008
analysis <echonest.remix.audio.AudioAnalysis object at 0x103c61bd0>
filename /Users/mikekilmer/Envs/GLITCH/glitcher/audio/Track01.mp3
convertedfile /var/folders/X2/X2KGhecyG0aQhzRDohJqtU+++TI/-Tmp-/tmp9ADD_Z.wav
sampleRate 44100
data [[0 0]
 [0 0]
 [0 0]
 ...,
 [0 0]
 [0 0]
 [0 0]]


并且audiofile.analysis似乎包含一个称为audiofile.analysis.source的属性,该属性包含(或显然指向)

最佳答案

在这种情况下,答案就在模块本身之内。

LocalAudioFile类提供(并且每个实例都可以利用)它自己的save方法,该方法通过LocalAudioFile.save或更可能是the_audio_object_instance.save调用。

如果是.mp3文件,则LocalAudioFile实例包括指向临时.wav文件(该文件是.mp3的解压缩版本)的指针,以及从其中返回的所有分析数据。与基于Internet的Echonest API接口后的初始音频文件。

LocalAudioFile.save调用shutil.copyfile(path_to_wave, wav_path)以与链接到音频对象的原始文件相同的名称和路径保存.wav文件,如果该文件已经存在,则返回错误。它调用pickle.dump(self, f)将分析数据保存到一个文件中,该文件也位于调用原始音频对象文件的目录中。

可以简单地通过LocalAudioFile重新引入pickle.load()对象。

这是我使用iPythondill会话,这是一个非常有用的包装器或接口,它提供大多数标准的pickle方法以及更多其他方法:

audiofile = audio.LocalAudioFile("/Users/mikekilmer/Envs/GLITCH/glitcher/audio/Track01.mp3")

In [1]: import echonest.remix.audio as audio

In [2]: import dill
# create the audio_file object
In [3]: audiofile = audio.LocalAudioFile("/Users/mikekilmer/Envs/GLITCH/glitcher/audio/Track01.mp3")
en-ffmpeg -i "/Users/path/audio/Track01.mp3" -y -ac 2 -ar 44100 "/var/folders/X2/X2KGhecyG0aQhzRDohJqtU+++TI/-Tmp-/tmp_3Ei0_.wav"
Computed MD5 of file is b3820c166a014b7fb8abe15f42bbf26e
Probing for existing analysis
#call the LocalAudioFile save method
In [4]: audiofile.save()
Saving analysis to local file /Users/path/audio/Track01.mp3.analysis.en
#confirm the object is valid by calling it's duration method
In [5]: audiofile.duration
Out[5]: 308.96
#delete the object - there's probably a "correct" way to do this
in [6]: audiofile = 0
#confirm it's no longer an audio_object
In [7]: audiofile.duration
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-04baaeda53a4> in <module>()
----> 1 audiofile2.duration

AttributeError: 'int' object has no attribute 'duration'


#open the pickled version (using dill)
In [8]: with open('/Users/path/audio/Track01.mp3.analysis.en') as f:
   ....:     audiofile = dill.load(f)
   ....:
#confirm it's a valid LocalAudioFile object
In [8]: audiofile.duration
Out[8]: 308.96


Echonest是非常健壮的API,并且remix包提供了大量功能。在here汇编中有一小部分相关链接。

关于python - 使用Dill在Load Object上执行Python TypeError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25538812/

10-12 21:00