I am working on a data analysis system that looks over a set of data then writes some basic observations on the data based on some preset rules. For example, consider columns a to e, output would have to be like column comment:
a b c d e comment
1 2 5 8 9 'observation 1, observation 3 and observation 4'
This algorithm can easily be done with tones of if statements, for example:
def get_comments(row)
comments_bag = []
if row[1] > 0:
comments_bag.append('observation 1')
if (row[1]+row[4] + 7) > 0:
comments_bag.append('some other observation 1')
...
return comments_bag
However, as in the future, more comments will have to be implemented depending on multiple variable checks, updating the code will be a pain in the neck. Therefore, I am looking for a design pattern, algorithm that can solve my problem and be easy to update in the future. Moreover, the comments will also have to follow some logical order. For now, the only solution I have is a comment to validation dictionary as so:
is_observation_1 = True if row[1]> 0 else False
is_observation_2 = True if if (row[1]+row[4] + 7) > 0 else False
comments_dict = { 'observation 1': is_observation_1
'observation 2': is_observation_2
'observation 3': is_observation_3
...
'some observation': is_observation_some
}
return keys where values are True from comments_dict
I was also thinking of implementing the composite pattern, however I think that might be an overkill for my problem. I was also thinking of a decision tree type of pattern.
Aucun commentaire:
Enregistrer un commentaire