virtual_knitting_machine.machine_state_violation_handling.machine_state_violation_policy module

Module containing classes related to setting a policy for how a knitting machine handles violations of the machine state’s requirements.

class Machine_State_With_Policy(*args, **kwargs)[source]

Bases: Protocol

Protocol for objects with policies for handling machine state errors.

property violation_policy: Knitting_Machine_Error_Policy

Returns: Knitting_Machine_Error_Policy: The policy for handling machine state errors.

set_response_for(violation, response=None)[source]

Sets the response for the given violation to the given response.

Parameters:
  • violation (Violation) – The violation to set the response for.

  • response (ViolationResponse, optional) – The response to set for the given violation. Defaults to the default response of this policy.

handle_violations(response_policy=None, handler=None)[source]

Context manager that catches exceptions and routes them through the violation policy.

Parameters:
  • response_policy (ViolationResponse | Violation, optional) – The ViolationResponse policy for this code block. If given a Violation, the response will be the policy assigned to that violation in the violation policy. Defaults to the response will be the default response of the violation policy.

  • handler (Callable[..., Any], optional) – Optional handler to attempt before falling back to the policy response.

classmethod __class_getitem__(params)

Parameterizes a generic class.

At least, parameterizing a generic class is the main thing this method does. For example, for some generic class Foo, this is called when we do Foo[int] - there, with cls=Foo and params=int.

However, note that this method is also called when defining generic classes in the first place with class Foo(Generic[T]): ….

__init__(*args, **kwargs)
checked_operation(method)[source]

Decorator for methods that check machines tate violations against a Knitting_Machine_Error_Policy. The violation_policy will be set to proceed after completion of the wrapped method, regardless of code ignored based on violations of the policy. :param method: A method of a Machine_State_With_Policy class that executes the operation being validated.

Returns: The wrapped method.

class Knitting_Machine_Error_Policy(_responses=<factory>, default=<factory>, proceed=True)[source]

Bases: object

Controls how the virtual knitting machine responds to constraint violations.

default: ViolationResponse
proceed: bool = True
set_response_for(violation, response=None)[source]

Sets the response for the given violation to the given response.

Parameters:
  • violation (Violation) – The violation to set the response for.

  • response (ViolationResponse, optional) – The response to set for the given violation. Defaults to the default response of this policy.

response_for(violation)[source]
Parameters:

violation (Violation) – The violation type to get a response to.

Returns:

The response for the given violation.

Return type:

ViolationResponse

check_violation(response, violation_exception, handler=None, **handler_kwargs)[source]

Check the given violation against this policy and act accordingly.

Parameters:
  • response (ViolationResponse) – The response pattern to the violation.

  • violation_exception (Exception) – The exception to raised with the given violation.

  • handler (Callable[[Machine_State_With_Policy], Any], optional) – The handler for this specific check of a violation. The handler must take one positional argument that is a Machine_State_With_Policy handler and can have any return value that is ignored. Defaults to having no handler for the violation.

  • handler_kwargs (Any) – Optional keyword arguments to pass to the handler method. Note that self passed by default for methods of the calling class.

Raises:

Exception – The given violation exception if the policy for the violation is to raise the error.

Warns:

Passed_Machine_Error_Warning – A warning raised in place of the given exception if the violation’s policy is to warn the user.

__init__(_responses=<factory>, default=<factory>, proceed=True)