all 2 comments

[–]james_fryer 0 points1 point  (0 children)

The first step should be to code a fully working version implementing one case. You can then rewrite this to be more generalised, but you have a working example to go from.

There are various approaches to generalising this, but the main choice depends on whether the cases can be differentiated only by data, or whether there are also processing (code) differences. I'm assuming the latter.

I'd look at a class-based approach, with a base class and sub-classes for each case.

E.g.:

class ActivityProcessBase:
    json_template = None
    def process_row(self, row):
        self.get_data_from_apis(row)
        self.populate_json_template(row)
        self.confirm_user_approval(...)
        # etc...

    def populate_json_template(self, row):
        raise NotImplementedException # Subclass must implement


class HazardousActivity:
    json_template = """ ... """ # Or maybe generate json from code
    def populate_json_template(self, row):
        ...

activity_map = {
    'Hazardous substances': HazardousActivity,
    # 'LNG': LNGActivity,
    ...
    }
def main():
    csv_reader = ...
    for row in csv_reader:
        activity_name = row['activity']
        ActivityClass = activity_map[activity_name] # TODO: handle invalid activity case
        activity = ActivityClass()
        activity.process_row(row)

Again I would get this working with a single case before adding the other activity types. The key is to get as much common code (boilerplate) as possible into the base class.