diff options
author | R David Murray <rdmurray@bitdance.com> | 2014-10-09 20:59:30 (GMT) |
---|---|---|
committer | R David Murray <rdmurray@bitdance.com> | 2014-10-09 20:59:30 (GMT) |
commit | 4487dd0ed5482d619279aa45099a22d0abc9615d (patch) | |
tree | 9e0919a9825e4a064ecd54209ec51bea3d49cbd9 /Lib | |
parent | aad627f2f94c426fab6d8341ffd60ca36cc005b7 (diff) | |
download | cpython-4487dd0ed5482d619279aa45099a22d0abc9615d.zip cpython-4487dd0ed5482d619279aa45099a22d0abc9615d.tar.gz cpython-4487dd0ed5482d619279aa45099a22d0abc9615d.tar.bz2 |
#18615: Make sndhdr return namedtuples.
Patch by Claudiu Popa.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/sndhdr.py | 7 | ||||
-rw-r--r-- | Lib/test/test_sndhdr.py | 13 |
2 files changed, 19 insertions, 1 deletions
diff --git a/Lib/sndhdr.py b/Lib/sndhdr.py index 240e507..e5901ec 100644 --- a/Lib/sndhdr.py +++ b/Lib/sndhdr.py @@ -32,6 +32,11 @@ explicitly given directories. __all__ = ['what', 'whathdr'] +from collections import namedtuple + +SndHeaders = namedtuple('SndHeaders', + 'filetype framerate nchannels nframes sampwidth') + def what(filename): """Guess the type of a sound file.""" res = whathdr(filename) @@ -45,7 +50,7 @@ def whathdr(filename): for tf in tests: res = tf(h, f) if res: - return res + return SndHeaders(*res) return None diff --git a/Lib/test/test_sndhdr.py b/Lib/test/test_sndhdr.py index 5e0abe0..361f70f 100644 --- a/Lib/test/test_sndhdr.py +++ b/Lib/test/test_sndhdr.py @@ -1,4 +1,5 @@ import sndhdr +import pickle import unittest from test.support import findfile @@ -18,6 +19,18 @@ class TestFormats(unittest.TestCase): what = sndhdr.what(filename) self.assertNotEqual(what, None, filename) self.assertSequenceEqual(what, expected) + self.assertEqual(what.filetype, expected[0]) + self.assertEqual(what.framerate, expected[1]) + self.assertEqual(what.nchannels, expected[2]) + self.assertEqual(what.nframes, expected[3]) + self.assertEqual(what.sampwidth, expected[4]) + + def test_pickleable(self): + filename = findfile('sndhdr.aifc', subdir="sndhdrdata") + what = sndhdr.what(filename) + dump = pickle.dumps(what) + self.assertEqual(pickle.loads(dump), what) + if __name__ == '__main__': unittest.main() |