I think this is an extension to another question
Background
Working with python 2.7, I have a class
class Report(object):
def translate_report(self, report_entity):
... # Process report_entity dictionary and return results
# But there is a legacy report class too
class Legacy_Report(object):
def translate_report(self, legacy_report_entity):
... # Process report_entity dictionary in different way and return results
The report_entity
is a dictionary dumped as string in a text file. The dictionary objects vary depending upon Report
and Legacy_Report
.
Requirement
I'll recreate the object instance with this dictionary and call translate_report
. The calling method would be agnostic whether the entity is legacy version or the new one.
My approach
class Legacy_Report(object):
@staticmethod
def _legacy_translation(legacy_report_entry):
... # Process
return legacy_report_entry
def __call__(self, underlying_function):
def wrapper_func(self, report_object):
if 'legacy' in report_object.keys():
return Decorator._legacy_translation(report_object)
else:
return underlying_function(self, report_object)
return wrapper_func
class Report(object):
@Legacy_Report()
def translate_report(self, report_entity):
... # Process report_entity
return report_entity
The Question
While this works, is this the right way to achieve this ?
Maybe decorator at some other level or some better pythonic way ?