Change open mode and seek() behaviour#21
Change open mode and seek() behaviour#21bastibe merged 2 commits intobastibe:masterfrom mgeier:mode-seek
Conversation
Now the integer mode constants are called READ, WRITE and RDWR. They are still in the module namespace, but now they have their own type _ModeType (derived from int). Alternatively, 'r'/'w'/'rw' and 'READ'/'WRITE'/'RDWR' (case-insensitive) can be used in the SoundFile constructor. This includes the strings suggested by @bastibe in #14. The strings 'r+'/'w+'/'a'/'a+'/... from the standard Python file objects are not used, because their behaviour is different from the modes in libsndfile which would lead to confusion. The mode is now the second constructor argument, right after the file name. This way, 'rw' can be used as positional parameter: f = SoundFile('myfile.wav', 'rw')
|
This is awesome! I think we finally arrived at a very good solution for this! Thank you so much for you continued work on these projects! If I'm not missing anything, you don't seem to define |
|
They're defined in the dictionary I'm planning to use the same method for The reason why I'm not assigning them directly in the module namespace is to be able to use the dict Another advantage of the type classes is that they help checking for the wrong argument order. Without the check, this would create a 3-channel file with subtype The only drawback I see to this solution (except it being von hinten durch die Brust ins Auge), is that I'm glad you like the PR! |
|
Oh, I get it. Great! I'm not sure I like that |
Change open mode and seek() behaviour
|
Thanks for merging! I didn't know the rule, I think it's a good one.
If you know a better way to achieve these, please tell me! |
|
You could just write Or you could define the variables that way to begin with, like and not have an |
|
The first example wouldn't work because Your second example looks promising, I think it could achieve both goals mentioned above. I guess we could live with this (although I think it's not nicer than the current solution), but the really bad thing would be that the class couldn't be used anymore within the code! No object of the type could be constructed from an integer (without specifying the string as well). |
|
You could define a type like then, you could replace all the numeric constants with something like I haven't thought this through yet. But something like this might actually be a better solution than all those dicts. |
|
I finally came up with a solution based on your suggestions and I implemented it in 1a0c137. For the open mode I'm using a classmethod to set up the id/string mapping. For the formats I'm searching in the whole module namespace via I'm not sure, however, if this is really better than before ... |
|
I really think that having those constants explicitly defined in the module scope makes it much more readable. Having less "magic" is a good thing. You seem to be using autocompletion a lot. I usually read the code instead--not seeing those constants would confuse me a lot. |
|
I agree that it is better if we can easily see where the constants are defined. And I'm using auto-completion only since recently, when I started working with IPython. |
See #10