0

I'm refactoring spaghetti code, and it has a piece like this:

template_dict = {
    "value": "",
    "isIncreased": False,
    "isDecreased": False
}
my_dict = {
    "current_half_result": {
        "home": template_dict,
        "draw": template_dict,
        "away": template_dict
    },
    "full_time_result": {
        "home": template_dict,
        "draw": template_dict,
        "away": template_dict
    },
    "current_half_over_under": {
        "$1_5": {
            "over": template_dict,
            "under": template_dict
        },
        "handicap": ""
    },
    "full_time_over_under": {
        "$2_5": {
            "over": template_dict,
            "under": template_dict
        },
        "handicap": ""
    },
    "next_goal": {
        "home": template_dict,
        "no_goal": template_dict,
        "away": template_dict
    }
}

As you see my_dict variable and has the same value in all leaf keys - template_dict. How can I remove repeating from code in a way that code doesn't get slower than the current example and also increase readability and cleanliness of the code. Speed is an important factor, because, this piece of code gets 3-6 hundred times a second in my server. And I don't wont to increase number of lines much or create additional function etc.

P.S. I didn't write that code, so don't judge me. Due to strong coupling in code, I can't make a big change at once. For full code, check this link

Alihaydar Gubatov
  • 988
  • 1
  • 12
  • 27
  • This dictionary is static a needs only to be defined once. Defined on a module level, the definition won't get executed twice. Are you sure it's being run hunderds of times ? You can move it to module level at least. – smido Apr 10 '19 at 12:09
  • @smido, yes, I'm sure. It is just a part of the code. It gets changed in execution of next lines. All of the values of "value", "isIncreased", "isDecreased" keys are changing. Full code of class is in this link: https://pastebin.com/CLQ7SF9g – Alihaydar Gubatov Apr 10 '19 at 12:14
  • 2
    It's not perfectly clear what you're up to, but maybe check `defaultdict` from `collections`, which could allow you to NOT define the structure at all, just set the values. You will probably need infinitely nested dict for this, named `InfiniteDict` [here](https://stackoverflow.com/questions/4178249/is-there-a-standard-class-for-an-infinitely-nested-defaultdict) – smido Apr 10 '19 at 12:30
  • @smido I thought that too, and my own implementation uses defaultdict. Just wanted to check if there is something better. Thank you for your energy and time. – Alihaydar Gubatov Apr 10 '19 at 12:44

1 Answers1

0

Here's my own solution.

def_dict = defaultdict(lambda: {"value": "", "isIncreased": False, "isDecreased": False})
my_dict = defaultdict(lambda: def_dict)
# PyCharm raises warning for two lines below, but it works
my_dict['current_half_over_under'] = defaultdict(lambda: def_dict) 
my_dict['full_time_over_under'] = defaultdict(lambda: def_dict)

And to set "handicap" keys in "full_time_over_under" and "current_half_over_under" dictionaries we should access it via my_dict.setdefault('handicap', some_value)

Alihaydar Gubatov
  • 988
  • 1
  • 12
  • 27