def test_get_serializer_byname(name): filename = 'foo.file' if name == 'kaldi': with pytest.raises(ValueError) as err: serializers.get_serializer(FeaturesCollection, 'foo.file', name) assert 'the file extension must be ".ark", it is ".file"' in str(err) filename = 'foo.ark' h = serializers.get_serializer(FeaturesCollection, filename, name) assert not os.path.isfile(filename) assert isinstance(h, serializers.supported_serializers()[name])
def test_save_not_collection(tmpdir, mfcc): f = str(tmpdir.join('foo.json')) h = serializers.get_serializer(FeaturesCollection, f, None) with pytest.raises(ValueError) as err: h.save(mfcc) assert 'features must be FeaturesCollection but are Features' in str( err.value)
def test_save_exists(tmpdir, mfcc_col): f = str(tmpdir.join('foo.json')) open(f, 'w').write('something') h = serializers.get_serializer(FeaturesCollection, f, None) with pytest.raises(IOError) as err: h.save(mfcc_col) assert 'file already exists' in str(err.value)
def load(cls, filename, serializer=None): """Loads a FeaturesCollection from a `filename` Parameters ---------- filename : str The file to load serializer : str, optional The file serializer to use for loading, if not specified guess the serializer from the `filename` extension Returns ------- features : :class:`~shennong.features.FeaturesCollection` The features loaded from the `filename` Raises ------ IOError If the `filename` cannot be read ValueError If the `serializer` or the file extension is not supported, if the features loading fails. """ return get_serializer(cls, filename, serializer).load()
def test_load_noreadable(tmpdir): f = str(tmpdir.join('foo.json')) h = serializers.get_serializer(FeaturesCollection, f, None) open(f, 'w').write('spam a lot') os.chmod(f, 0o222) # write-only with pytest.raises(IOError) as err: h.load() assert 'file not readable' in str(err.value)
def test_save_invalid(tmpdir, mfcc): f = str(tmpdir.join('foo.json')) h = serializers.get_serializer(FeaturesCollection, f, None) feats = FeaturesCollection( mfcc=Features(data=mfcc.data, times=0, validate=False)) with pytest.raises(ValueError) as err: h.save(feats) assert 'features are not valid' in str(err.value)
def test_load_invalid(tmpdir, mfcc_col): f = str(tmpdir.join('foo.json')) h = serializers.get_serializer(FeaturesCollection, f, None) h.save(mfcc_col) # remove 2 lines in the times array to corrupt the file data = json.load(open(f, 'r')) data['mfcc']['attributes']['_times']['__ndarray__'] = ( data['mfcc']['attributes']['_times']['__ndarray__'][2:]) open(f, 'w').write(json.dumps(data)) with pytest.raises(ValueError) as err: h.load() assert 'features not valid in file' in str(err.value)
def test_get_serializer_bad(): with pytest.raises(ValueError) as err: serializers.get_serializer(int, 'foo', None) assert 'must be shennong.features.FeaturesCollection' in str(err.value) with pytest.raises(ValueError) as err: serializers.get_serializer(FeaturesCollection, 'foo.spam', None) assert 'invalid extension .spam' in str(err.value) with pytest.raises(ValueError) as err: serializers.get_serializer(FeaturesCollection, 'foo', None) assert 'no extension nor serializer name specified' in str(err.value) with pytest.raises(ValueError) as err: serializers.get_serializer(FeaturesCollection, 'foo.spam', 'spam') assert 'invalid serializer spam' in str(err.value)
def test_load_nofile(): h = serializers.get_serializer(FeaturesCollection, 'foo.json', None) with pytest.raises(IOError) as err: h.load() assert 'file not found' in str(err.value)
def test_get_serializer_byext(ext): h = serializers.get_serializer(FeaturesCollection, 'foo' + ext, None) assert not os.path.isfile('foo' + ext) assert isinstance(h, serializers.supported_extensions()[ext])
def save(self, filename, serializer=None, **kwargs): get_serializer(self.__class__, filename, serializer).save(self, **kwargs)