DynamicImplicitDomain
(class from pyomo.common.config
)
-
class pyomo.common.config.DynamicImplicitDomain(callback)[source]
Bases: object
Implicit domain that can return a custom domain based on the key.
This provides a mechanism for managing plugin-like systems, where
the key specifies a source for additional configuration information.
For example, given the plugin module,
pyomo/common/tests/config_plugin.py
:
from pyomo.common.config import ConfigDict, ConfigValue
def get_configuration(config):
ans = ConfigDict()
ans.declare('key1', ConfigValue(default=0, domain=int))
ans.declare('key2', ConfigValue(default=5, domain=str))
return ans(config)
Then we can declare a :class:``ConfigDict` that imports the domain
for specific keys from a module that matches the key name:
def _pluginImporter(name, config):
mod = importlib.import_module(name)
return mod.get_configuration(config)
config = ConfigDict()
config.declare('plugins', ConfigDict(
implicit=True,
implicit_domain=DynamicImplicitDomain(_pluginImporter)))
config.plugins['pyomo.common.tests.config_plugin'] = {'key1': 5}
config.display()
plugins:
pyomo.common.tests.config_plugin:
key1: 5
key2: '5'
Note
This initializer is only useful for the ConfigDict
implicit_domain
argument (and not for “regular” domain
arguments)
- Parameters:
callback (Callable[[str, object], ConfigBase]) – A callable (function) that is passed the ConfigDict key and
value, and is expected to return the appropriate Config object
(ConfigValue, ConfigList, or ConfigDict)
-
__init__(callback)[source]
Methods
Member Documentation