I have a class that accepts a different number of inputs but achieves the same goal (Same output). It's a complex class and is used heavily for an API. I built a class handler that accepts inputs and maps them into the function based on the API request.
I'm not sure if this type of design is maintainable or if it's going to present other issues. So I have these inquiries:
- Is this design maintainable and scalable?
- Is this error-tolerant?
- Can I improve it?
- Am I following a design similar to any accepted design pattern?
- Any advice?
Please take a look at this very simplified code design:
Handler:
def class_handler(class_usage_type, class_object, kwargs):
# Method inputs and procedures are different but output is the same
Usage_FUNC_MAP = {
'use_method_x': class_object.method_x,
'use_method_y': class_object.method_y,
'use_method_z': class_object.method_z,
# ...
}
try:
result = Usage_FUNC_MAP[class_usage_type](**kwargs)
except Exception as e:
result = {
'output_elem1': kwargs
'output_elem2': f'Error: {e}',
'output_elem3': f'Error {e}'
}
finally:
return result
Class
class some_class():
# ...init
def method_x(self, param1_for_x, param2_for_x):
# ...procedure for x
return same_output
def method_y(self, param1_for_y):
# ...procedure for y
return same_output
def method_z(self, param1_for_z, param2_for_z, param3_for_z, param4_for_z):
# ...procedure for z
return same_output
# ...
API
# Main is in a different file as an API
# Route 1 for x
# ...
params_for_x = {"param1_for_x": '...', "param2_for_x": '...'}
result_for_x = class_handler(class_usage_type='use_method_x', class_object=some_class, kwargs=params)
#...
# Route 2 for y
# ...
params_for_y = {"param1_for_y": '...'}
result_for_y = class_handler(class_usage_type='use_method_y', class_object=some_class, kwargs=params)
# ...
# Route 3 for z
# ...
params_for_z = {"param1_for_z": '...', "param2_for_z": '...', "param3_for_z": '...', "param4_for_z": '...'}
result_for_z = class_handler(class_usage_type='use_method_z', class_object=some_class, kwargs=params)
# ...
# ...
Aucun commentaire:
Enregistrer un commentaire