I'm writing an OOP style Python wrapper for FFmpeg. I've created the concept of multiple codec Classes, each with their own set of instance variables. For instance, the AACCodec class might have a cutoff_frequency instance variable like:
class AACCodec(Codec):
def __init__(self, **kwargs):
self.cutoff_frequency = 20000
where the FLAC Codec class might have compression_ratio instance variable, like:
class FLACCodec(Codec):
def __init__(self, **kwargs):
self.compression_ratio = 0.78
Basically, elsewhere in my code (in ffmpeg.py) I'd like to be able to create the correct codec object by making a single call such as:
import ffcodecs
newCodec = somefunctioncall('aac', 'key1'=value1, 'key2'=value2)
The way I considered implementing this was to use a function inside ffcodecs.py which returns the codec object based on input string:
def getCodec(name, **kwargs):
codecObjects = {
'aac' : AACCodec(**kwargs),
'ogg' : OGGCodec(**kwargs),
'flac': FLACCodec(**kwargs)
}
return codecObjects[name]
The problem with this is that whenever I call getCodec() it creates an instance of each codec when codecObjects is declared. I have logic in each codec __init__ to check if **kwargs matches the object's instance variables (self.cutoff_frequency, self.compression_ratio etc.) and error if it doesn't.
What's the most Pythonic way of instantiating the correct object and ONLY the correct object in a single call?
One solution would be to just have a set of if/else if statements to match each codec string and return each object accordingly, but if I end up maintaining a bunch of different codecs, I don't want a hundred if/else statements each time I instantiate a new object.
There's got to be a more elegant way of doing this.