Source code for MPF.commonHelpers.options

from collections import namedtuple

[docs]def checkUpdateDict(optDict, **kwargs): """Returns a dict containing the options from optDict, updated with kwargs. Raises typeError if an option in kwargs doesn't exist in optDict""" opt = dict(optDict) for key, value in optDict.items(): opt[key] = kwargs.pop(key, value) if kwargs: raise TypeError("Got unexpected kwargs: {} - Try {}".format(list(kwargs.keys()), optDict.keys())) return opt
[docs]def popUpdateDict(optDict, **kwargs): """Returns a dict containing the options from optDict, updated with kwargs and the remaining kwargs that don't exist in optDict""" opt = dict(optDict) for key, value in optDict.items(): opt[key] = kwargs.pop(key, value) return opt, kwargs
[docs]def checkUpdateOpt(optDict, **kwargs): """Returns a namedtuple containing the options from optDict, updated with kwargs. Raises typeError if an option in kwargs doesn't exist in optDict""" Opt = namedtuple("Opt", optDict.keys()) return Opt(**dict(optDict, **kwargs))
[docs]def setAttributes(obj, optDict, **kwargs): for key, value in optDict.items(): setattr(obj, key, kwargs.pop(key, value)) if kwargs: raise TypeError("Got unexpected kwargs: {} - Try {}".format(list(kwargs.keys()), dir(obj)))
[docs]def updateAttributes(obj, optDict, **kwargs): for key, value in kwargs.items(): if key in optDict: setattr(obj, key, value) else: raise KeyError("Got an unexpected kwarg: {} - Try {}".format(key, list(optDict.keys())))