def from_config(cls, config_dict, merge_default=True): """ Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments. This method should not be called via super unless and instance of the class is desired. :param config_dict: JSON compliant dictionary encapsulating a configuration. :type config_dict: dict :param merge_default: Merge the given configuration on top of the default provided by ``get_default_config``. :type merge_default: bool :return: Constructed instance from the provided config. :rtype: DescriptorElementFactory """ if merge_default: merged_config = cls.get_default_config() merge_dict(merged_config, config_dict) config_dict = merged_config return DescriptorElementFactory( get_descriptor_element_impls()[config_dict['type']], config_dict[config_dict['type']] )
def from_config(cls, config_dict, merge_default=True): """ Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments. This method should not be called via super unless and instance of the class is desired. :param config_dict: JSON compliant dictionary encapsulating a configuration. :type config_dict: dict :param merge_default: Merge the given configuration on top of the default provided by ``get_default_config``. :type merge_default: bool :return: Constructed instance from the provided config. :rtype: DescriptorElementFactory """ if merge_default: merged_config = cls.get_default_config() merge_dict(merged_config, config_dict) config_dict = merged_config return DescriptorElementFactory( get_descriptor_element_impls()[config_dict['type']], config_dict[config_dict['type']])
def get_default_config(cls): """ Generate and return a default configuration dictionary for this class. This will be primarily used for generating what the configuration dictionary would look like for this class without instantiating it. It is not be guaranteed that the configuration dictionary returned from this method is valid for construction of an instance of this class. :return: Default configuration dictionary for the class. :rtype: dict """ return make_config(get_descriptor_element_impls())
def get_default_config(cls): """ Generate and return a default configuration dictionary for this class. This will be primarily used for generating what the configuration dictionary would look like for this class without instantiating it. By default, we observe what this class's constructor takes as arguments, aside from the first two assumed positional arguments, turning those argument names into configuration dictionary keys. If any of those arguments have defaults, we will add those values into the configuration dictionary appropriately. The dictionary returned should only contain JSON compliant value types. It is not be guaranteed that the configuration dictionary returned from this method is valid for construction of an instance of this class. :return: Default configuration dictionary for the class. :rtype: dict """ c = super(CachingDescriptorElement, cls).get_default_config() # Nested DescriptorElementFactory configuration if c['wrapped_element_factory'] is None: # Have to make this configuration in such a way that we don't # include ourselves in the list of nestable classes else an infinite # recursion will occur. de_impls = get_descriptor_element_impls() # Remove ourselves del de_impls[cls.__name__] # Construct config block DescriptorElementFactory wants c_def = {"type": None} for label, de_cls in de_impls.iteritems(): # noinspection PyUnresolvedReferences c_def[label] = de_cls.get_default_config() c['wrapped_element_factory'] = c_def else: c['wrapped_element_factory'] = \ c['wrapped_element_factory'].get_config() return c
def get_default_config(cls): """ Generate and return a default configuration dictionary for this class. This will be primarily used for generating what the configuration dictionary would look like for this class without instantiating it. By default, we observe what this class's constructor takes as arguments, aside from the first two assumed positional arguments, turning those argument names into configuration dictionary keys. If any of those arguments have defaults, we will add those values into the configuration dictionary appropriately. The dictionary returned should only contain JSON compliant value types. It is not be guaranteed that the configuration dictionary returned from this method is valid for construction of an instance of this class. :return: Default configuration dictionary for the class. :rtype: dict """ c = super(CachingDescriptorElement, cls).get_default_config() # Nested DescriptorElementFactory configuration if c['wrapped_element_factory'] is None: # Have to make this configuration in such a way that we don't # include ourselves in the list of nestable classes else an infinite # recursion will occur. de_impls = get_descriptor_element_impls() # Remove ourselves del de_impls[cls.__name__] # Construct config block DescriptorElementFactory wants c_def = {"type": None} for label, de_cls in six.iteritems(de_impls): # noinspection PyUnresolvedReferences c_def[label] = de_cls.get_default_config() c['wrapped_element_factory'] = c_def else: c['wrapped_element_factory'] = \ c['wrapped_element_factory'].get_config() return c
def from_config(cls, config_dict): """ Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments. This method should not be called via super unless and instance of the class is desired. :param config_dict: JSON compliant dictionary encapsulating a configuration. :type config_dict: dict :return: Constructed instance from the provided config. :rtype: DescriptorElementFactory """ return DescriptorElementFactory( get_descriptor_element_impls()[config_dict['type']], config_dict[config_dict['type']] )
def from_config(cls, config_dict): """ Instantiate a new instance of this class given the configuration JSON-compliant dictionary encapsulating initialization arguments. This method should not be called via super unless and instance of the class is desired. :param config_dict: JSON compliant dictionary encapsulating a configuration. :type config_dict: dict :return: Constructed instance from the provided config. :rtype: DescriptorElementFactory """ merged_config = cls.get_default_config() merged_config.update(config_dict) return DescriptorElementFactory( get_descriptor_element_impls()[merged_config['type']], merged_config[merged_config['type']])