Reduce Your Python Code Complexity with This Simple Trick
Author:
ran-isenbergLink:
https://isenberg-ran.medium.com/reduce-your-python-code-complexity-with-this-simple-trick-7046b7c54e7aPublish Date:
2021.07.01Reviewed Date:
30
- A customer management system receives requests.
- Each request type is a Python dictionary and all 4 are handled uniquely
request1 = {
"action": "create",
"customer": "customer1",
}
request2 = {
"action": "activate",
"customer": "customer2",
}
request3 = {
"action": "suspend",
"customer": "customer2",
}
request4 = {
"action": "delete",
"customer": "customer2",
}
def function_handler(request: Dict[str, Any]) -> None:
# handle request
90e9dc
from typing import Any, Dict
def function_handler(request: Dict[str, Any]) -> None:
action = request.get("action")
customer_name = request.get("customer")
# validate input
if action == "create":
_handle_create_request(customer_name)
elif action == "activate":
_handle_activate_request(customer_name)
elif action == "suspend":
_handle_suspend_suspend(customer_name)
elif action == "delete":
_handle_delete_request(customer_name)
def _handle_create_request(customer_name: str) -> None:
# do something related to create
return
def _handle_activate_request(customer_name: str) -> None:
# do something related to activate
return
def _handle_suspend_suspend(customer_name: str) -> None:
# do something related to suspend
return
def _handle_delete_request(customer_name: str) -> None:
# do something related to delete
return
df2a07
from typing import Any, Dict
ACTION_MAPPING = {
"create": _handle_create_request,
"activate": _handle_activate_request,
"suspend": _handle_suspend_suspend,
"delete": _handle_delete_request,
}
def function_handler(request: Dict[str, Any]) -> None:
action = request.get("action")
customer_name = request.get("customer")
# validate input
_handle_request(action, customer_name)
def _handle_request(action: str, customer_name: str) -> None:
action_handler = ACTION_MAPPING.get(action)
# handle action
action_handler(customer_name)
def _handle_create_request(customer_name: str) -> None:
# do something related to create
return
def _handle_activate_request(customer_name: str) -> None:
# do something related to activate
return
def _handle_suspend_suspend(customer_name: str) -> None:
# do something related to suspend
return
def _handle_delete_request(customer_name: str) -> None:
# do something related to delete
return
c90001
Backlinks