0

In a piece of code I'm writing, a function is used a lot and takes a lot of total time using the profiler cProfile. I followed the advice I got in a previous answer (How do I export part of an XML file to a multi-level DataFrame in pandas?) to optimize my code here below.

from functools import lru_cache
import json

# library used in the object_id2name function
#name_dict = json.loads( open(r"path_to_json\dictionnary.json","r").read() )

# dictionary provided as an example
exemple_dict = {"1":"Apple", "2":"Yoghurt", "3":"Banana", "4":"Burrito"}

@lru_cache(maxsize=3000)
def object_id2name(object_id, name_Dict):
    """ 
    This function uses a dictionary to translate the object ID into its name.
    input: object_id
    output: name
     """

    if  not object_id.isnumeric():
        return ""
    else:
        partial_id = object_id[:-1]
        
        if partial_id not in name_Dict.keys():
            return object_id

        if object_id[-1] == "0":
            return  name_Dict.get(partial_id)
        else:
            return  name_Dict.get(partial_id)+"X"+object_id[-1]

object_id2name("1", other_dict)

The function used to call a new instance of the dictionary for every loop which took a lot of unnecessary time. The function was also called a lot for pretty redundant object_ids (a little more than 3000 differents IDS for 12 000 000 loops) so I decided to use the decorator @lru_cache to store the results in cache and speed up the process.

The problem is that using the decorator makes it impossible to use the dictionary outside the function, resulting in the infamous TypeError: unhashable type: 'dict' on line 30 (last line). This seems to be somewhat a redundant question, but I was unable to fix my problem by creating a hashable dict class for example (https://stackoverflow.com/a/16162138/16117705)

I would like to be able to store the results in cache using the decorator, but also loading the dictionary outside the function for clarity and speed's sake.

tripleee
  • 158,107
  • 27
  • 234
  • 292
Bobbert
  • 73
  • 7

0 Answers0