Source code for schrodinger.models.adapters.abstractadapter

[docs]class AbstractAdapter: """ Abstract class for copying data from compound params to other data classes. Subclasses must implement `_createDefaultObj` and `_copyItem`. Behavior can be customized by overriding the virtual methods: `_paramToDict`: to customize param keywords or values `_convertKeyword`: to customize how all keywords are transformed """
[docs] @classmethod def convert(cls, param, destination_obj=None): """ Copy data from the compound param to the destination object. If no destination object is supplied, an empty one will be created. :param param: Compound param instance :type param: schrodinger.models.parameters.CompoundParam :param destination_obj: Destination object or None to create empty :type destination_obj: object or NoneType :return: Destination object containing data from the compound param :rtype: object """ if destination_obj is None: destination_obj = cls._createDefaultObj() param_dict = cls._paramToDict(param) for param_key, value in param_dict.items(): if isinstance(value, dict): # TODO add nested support raise ValueError("Nested params are not supported") config_key = cls._convertKeyword(param_key) cls._copyItem(config_key, value, destination_obj) return destination_obj
######################################### # Pure virtual methods ######################################### @classmethod def _createDefaultObj(cls): """ Create a default destination object to be used by `convert` if none is supplied :return: Default destination object :rtype: object """ raise NotImplementedError() @classmethod def _copyItem(cls, key, value, destination_obj): """ Copy the specified item onto the destination object. Override this method with logic to copy the compound param `value` named `key` to the destination object in the appropriate format. :param key: Item key (already converted and transformed) :param value: Item value (already transformed but not copied) """ raise NotImplementedError() ######################################### # Virtual methods ######################################### @classmethod def _paramToDict(cls, param): """ Convert the compound param into a dictionary. Subclasses that need to transform keys or values should do it here. """ return param.toDict() @classmethod def _convertKeyword(cls, param_key): """ Convert a keyword from params conventions to destination obj conventions :param param_key: Keyword from parameters object :type param_key: str :return: Converted key appropriate for destination obj :rtype: str """ return param_key