Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_immutability_of_request_and_params():
"""Test the immutability of the request and params objects"""
with pytest.raises(FrozenInstanceError):
params = FrozenParams()
params.allowed_intents = []
with pytest.raises(TypeError):
params = FrozenParams()
params.dynamic_resource["a"] = "b"
with pytest.raises(FrozenInstanceError):
request = Request()
request.params = Params()
with pytest.raises(TypeError):
request = Request()
request.frame["a"] = "b"
default_params (Params, optional): The default params to use with each turn. These
defaults will be overridden by params passed for each turn.
force_sync (bool, optional): Force synchronous return for `say()` and `process()`
even when app is in async mode.
"""
app = app or path.get_app(app_path)
app.lazy_init(nlp)
self._app_manager = app.app_manager
if not self._app_manager.ready:
self._app_manager.load()
self.context = context or {}
self.history = []
self.frame = {}
self.default_params = default_params or Params()
self.force_sync = force_sync
self.params = FrozenParams()
def freeze_params(params):
"""
If params is a dictionary or Params we convert it into FrozenParams.
Otherwise we raise a TypeError.
Args:
params (dict, Params): The input params to convert
Returns:
FrozenParams: The converted params object
"""
params = params or FrozenParams()
if isinstance(params, dict):
params = FrozenParams(**params)
elif params.__class__ == Params:
params = FrozenParams(**DialogueResponder.to_json(params))
elif not isinstance(params, FrozenParams):
raise TypeError(
"Invalid type for params argument. "
"Should be dict or {}".format(FrozenParams.__name__)
)
return params
Returns:
(dict): The dictionary response.
"""
if self._app_manager.async_mode:
res = self._process_async(text, params=params)
if self.force_sync or force_sync:
return asyncio.get_event_loop().run_until_complete(res)
return res
if not self._app_manager.ready:
self._app_manager.load()
internal_params = copy.deepcopy(self.params)
if isinstance(params, dict):
params = FrozenParams(**params)
if isinstance(internal_params, dict):
internal_params = FrozenParams(**internal_params)
if params:
# If the params arg is explicitly set, overight the internal params
for k, v in vars(params).items():
vars(internal_params)[k] = v
response = self._app_manager.parse(
text,
params=internal_params,
context=self.context,
frame=self.frame,
history=self.history,
)
Args:
text (str): The text of a message.
params (dict): The params to use with this message, overriding any defaults
which may have been set.
Returns:
(dict): The dictionary Response.
"""
if not self._app_manager.ready:
await self._app_manager.load()
internal_params = copy.deepcopy(self.params)
if isinstance(params, dict):
params = FrozenParams(**params)
if isinstance(internal_params, dict):
internal_params = FrozenParams(**internal_params)
if params:
# If the params arg is explicitly set, overight the internal params
for k, v in vars(params).items():
vars(internal_params)[k] = v
response = await self._app_manager.parse(
text,
params=internal_params,
context=self.context,
frame=self.frame,
history=self.history,
)
if self._app_manager.async_mode:
res = self._process_async(text, params=params)
if self.force_sync or force_sync:
return asyncio.get_event_loop().run_until_complete(res)
return res
if not self._app_manager.ready:
self._app_manager.load()
internal_params = copy.deepcopy(self.params)
if isinstance(params, dict):
params = FrozenParams(**params)
if isinstance(internal_params, dict):
internal_params = FrozenParams(**internal_params)
if params:
# If the params arg is explicitly set, overight the internal params
for k, v in vars(params).items():
vars(internal_params)[k] = v
response = self._app_manager.parse(
text,
params=internal_params,
context=self.context,
frame=self.frame,
history=self.history,
)
self.history = response.history
self.frame = response.frame
self.params = response.params
def freeze_params(params):
"""
If params is a dictionary or Params we convert it into FrozenParams.
Otherwise we raise a TypeError.
Args:
params (dict, Params): The input params to convert
Returns:
FrozenParams: The converted params object
"""
params = params or FrozenParams()
if isinstance(params, dict):
params = FrozenParams(**params)
elif params.__class__ == Params:
params = FrozenParams(**DialogueResponder.to_json(params))
elif not isinstance(params, FrozenParams):
raise TypeError(
"Invalid type for params argument. "
"Should be dict or {}".format(FrozenParams.__name__)
)
return params
confidences (): Immutables Map of keys ``domains``, ``intents``, ``entities`` and ``roles``
containing confidence probabilities across all labels for each classifier.
nbest_transcripts_text (tuple): List of alternate n-best transcripts from an ASR system
nbest_transcripts_entities (tuple): List of lists of extracted entities for each of the
n-best transcripts.
nbest_aligned_entities (tuple): List of lists of aligned entities for each of the n-best
transcripts.
"""
domain = attr.ib(default=None)
intent = attr.ib(default=None)
entities = attr.ib(default=attr.Factory(tuple), converter=tuple)
history = attr.ib(default=attr.Factory(tuple), converter=tuple)
text = attr.ib(default=None)
frame = attr.ib(default=immutables.Map(), converter=immutables.Map)
params = attr.ib(default=FrozenParams())
context = attr.ib(default=immutables.Map(), converter=immutables.Map)
confidences = attr.ib(default=immutables.Map(), converter=immutables.Map)
nbest_transcripts_text = attr.ib(default=attr.Factory(tuple), converter=tuple)
nbest_transcripts_entities = attr.ib(default=attr.Factory(tuple), converter=tuple)
nbest_aligned_entities = attr.ib(default=attr.Factory(tuple), converter=tuple)