2
\$\begingroup\$

I have a set of classes that have similar funcionality but very different interfaces. I created two interface classes with polymorphic methods so I can operate over then. When creating calling the methods, I have to pass a set of parameters, in this case a JSON file, that is also different for each class. Is there a way to create a generic interface without having to do ifs to select which parameter to pass to each class?

import library_A
import library_B
import json

class ApproachA():

    def __init__(self, params):
        self.params = params
        self.entity = library_A.Model(self.params)

    def run(self, data):
        return self.entity(data)


class ApproachB():

    def __init__(self, params):
        self.params = parse_params(params)
        self.entity = library_B.Model(self.params)

    def run(self, data):
        real_data = do_something(data)
        return self.entity(data)


if __name__ == "__main__":
    with open('params.json') as f:
        params = json.loads(f)

    entity_list = []
    # here lies the problem
    for key, param in params.items():
        if key == "model_a":
            entity_list.append(Approach_A(param))
        elif key == "model_b":
            entity_list.append(Approach_B(param))

while the JSON parameter file is as follows:

{
    "model_a": {"param_1":10, "param_2":22},
    "model_b": {"thing":"strudel", "param_cc": 102, "talk":False}
}

I definitely want to avoid these ifs sequences and couplings, as things similar to these tests have to be done in several places in the code. I also would like to avoid an abstract base class. I´m trying to keep things as functional as possible, without heavy OO concepts.

\$\endgroup\$

closed as off-topic by Graipher, Toby Speight, Mathias Ettinger, Heslacher, Austin Hastings Apr 4 at 22:31

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – Graipher, Mathias Ettinger, Austin Hastings
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ It looks like you're calling transpose on a dict. I'm confused - why is this expected to work? \$\endgroup\$ – Reinderien Apr 2 at 0:30
  • \$\begingroup\$ Does the JSON file contain both model_a and model_b data always? Or are you combining the data further up/earlier in the procedure? \$\endgroup\$ – C. Harley Apr 2 at 0:51
  • \$\begingroup\$ I´ll be combining the data further, this is just a demonstration of my process. \$\endgroup\$ – Ivan Apr 2 at 12:06
  • \$\begingroup\$ transpose here was supposed to represent an operation done on the data, to show that the two interface classes are distinct. I´ll edit the code to be more realistic. \$\endgroup\$ – Ivan Apr 2 at 12:06
  • \$\begingroup\$ Why don't you simply define params = {ApproachA: {"param_1": 10, …}, …}? \$\endgroup\$ – Mathias Ettinger Apr 2 at 13:51