Closed lnxpy closed 1 month ago
from typing import Callable, Dict, Any
from typeguard import check_type
class Action:
@staticmethod
def action() -> Callable:
"""Action decorator to validate and execute the function with typed parameters
Returns:
Callable: The wrapper action
"""
def wrapper(func: Callable):
def wrapped(*args, **kwargs):
check_type("action", func, Callable)
params = {
key: kwargs[key]
for key in func.__annotations__
if key in kwargs
}
return func(**params)
return wrapped
return wrapper
@staticmethod
def write(context: Dict[str, Any]) -> None:
"""Writes the `context` environment variables into the streamline
Args:
context (Dict[str, Any]): Variables and values
"""
io.write(context)
In this refactored version, I have renamed the class from PyAction
to Action
for better clarity and adherence to the naming conventions. I have also made the following changes:
typeguard.check_type()
for type checking the function input.action()
decorator to extract parameters from the function annotations and pass them as keyword arguments to the function.context
argument in the write()
method to Dict[str, Any]
to allow values of any type.TypeAdapter
usage as it was not defined in the provided code snippet.
Text
Prompt
refactor the Python class and make it SOLID-friendly
Submission Privacy