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_freeze_params():
params = freeze_params({"target_dialogue_state": "some-state"})
assert params.__class__ == FrozenParams
input_params = Params()
input_params.target_dialogue_state = "some-state-2"
params = freeze_params(input_params)
assert params.__class__ == FrozenParams
params = freeze_params(params)
assert params.__class__ == FrozenParams
with pytest.raises(TypeError):
freeze_params([1, 2, 3])
def test_convo_params_are_cleared(kwik_e_mart_nlp, kwik_e_mart_app_path):
"""Tests that the params are cleared in one trip from app to mm."""
convo = Conversation(nlp=kwik_e_mart_nlp, app_path=kwik_e_mart_app_path)
convo.params = Params(
allowed_intents=["store_info.find_nearest_store"],
target_dialogue_state="greeting",
)
convo.say("close door")
assert convo.params == Params()
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"
def to_json(instance):
"""Convert the responder into a JSON representation.
Args:
instance (DialogueResponder): The responder object.
Returns:
(dict): The JSON representation.
"""
serialized_obj = {}
for attribute, value in vars(instance).items():
if isinstance(value, (Params, Request, FrozenParams)):
serialized_obj[attribute] = DialogueResponder.to_json(value)
elif isinstance(value, immutables.Map):
serialized_obj[attribute] = dict(value)
else:
serialized_obj[attribute] = value
return serialized_obj
def _pre_dm(self, processed_query, context, params, frame, history):
# We pass in the previous turn's responder's params to the current request
request = self.request_class(
context=context,
history=history,
frame=frame,
params=params,
**processed_query
)
# We reset the current turn's responder's params
response = self.responder_class(
frame=frame,
params=Params(),
slots={},
history=history,
request=request,
directives=[],
)
return request, response
dict: Mapping from parameter name to bool depending on validation.
"""
return {
param: self.validate_param(param)
for param in (
"time_zone",
"timestamp",
"dynamic_resource",
"language",
"locale",
)
}
@attr.s(frozen=True, kw_only=True)
class FrozenParams(Params):
"""
An immutable version of the Params object.
Attributes:
allowed_intents (list, str): A list of intents that you can set to force the language
processor to choose from.
target_dialogue_state (str): The name of the dialogue handler that you want to reach in
the next turn.
time_zone (str): The name of an IANA time zone, such as 'America/Los_Angeles', or
'Asia/Kolkata'.
language (str): The language code representing ISO 639-1/2 language codes
locale (str, optional): The locale representing the ISO 639-1/2 language code and
ISO3166 alpha 2 country code separated by an underscore character.
timestamp (long): A unix time stamp for the request accurate to the nearest second.
dynamic_resource (dict): A dictionary containing data used to influence the language
classifiers by adding resource data for the given turn.
):
"""
Initializes a dialogue responder.
Args:
frame (dict): The frame object.
params (Params): The params object.
history (list): The history of the responder.
slots (dict): The slots of the responder.
request (Request): The request object associated with the responder.
dialogue_state (str): The dialogue state.
directives (list): The directives of the responder.
"""
self.directives = directives or []
self.frame = frame or {}
self.params = params or Params()
self.dialogue_state = dialogue_state
self.slots = slots or {}
self.history = history or []
self.request = request or Request()
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