Home:ALL Converter>Most pythonic way to find matching string

Most pythonic way to find matching string

Ask Time:2018-06-08T21:53:32         Author:hikamare

Json Formatter

Having code posted below how can I improve it's implementation? I am trying no to have to much if-logic in my code and I am pretty certain that there are much faster mechanisms in Python to deal with such tasks:

      is_sat = re.search('sat', destination, re.IGNORECASE) 
      if is_sat is not None:
            temp_zone = self.determine_zone(value, self.sat_zone_dict)
            data_type = 'SAT' + str(temp_zone)
            self.log_handler.update(destination+" "+data_type+"\n")
        else:
            if destination.find("All") > 0:
                temp_zone = self.determine_zone(value, self.mn_zone_dict)
                data_type = 'ALL_' + str(temp_zone)
                self.log_handler.update("ALL destination: "+destination + " " + data_type + "\n")
            elif destination.find("Mobile") > 0:
                temp_zone = self.determine_zone(value, self.mn_zone_dict)
                data_type = 'MOB_'+str(temp_zone)
            elif destination.find("Fixed") > 0:
                temp_zone = self.determine_zone(value, self.mn_zone_dict)
                data_type = 'FIX_'+str(temp_zone)
            elif destination.find("NGN") > 0:
                temp_zone = self.determine_zone(value, self.sat_zone_dict)
                data_type = 'NGN_' + str(temp_zone)

determine_zone() method allows me to match with key: value dict, but while I first have to search for the pattern in the destination field it makes no sense to me to add another operation which would exactly extract position of the "All', 'Mobile' etc. in my string.

@EDIT As @zwer asked here is determine_zone() implementation:

 def determine_zone(price: int, zone_dict: dict) -> int:

     zone = None

     try:
         for (key, value) in zone_dict.items():
             if price < value:
                 zone = key
                 return zone

         if zone is None:
             zone = zone_dict.__len__() + 1
             return zone

     except TypeError:
         raise TypeError

Author:hikamare,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50762211/most-pythonic-way-to-find-matching-string
9000 :

Factor out the common code. Wherever you see any copy-pasted code (and posted a snipped with a lot of it), it's a valid target for factoring out — not necessarily to a separate function, but in a way that prevents it from being repeated.\n\nis_sat = ...\n# temp_zone is calculated only once, and turned to string immediately.\ntemp_zone = str(self.determine_zone(value, self.sat_zone_dict))\nif is_sat is None: \n for prefix in ('ALL', 'MOB', 'FIX', 'NGN'):\n if prefix in destination:\n data_type = prefix + '_' + temp_zone # Only once.\n if prefix == 'ALL':\n log(\"ALL destination: \"+destination + \" \" + data_type + \"\\n\") \n # You _might_ have a case where none of the prefix values were found.\n # Think about handling it, or make sure it cannot happen. \nelse:\n data_type = 'SAT_' + temp_zone\n self.log_handler.update(destination + \" \" + data_type + \"\\n\")\n\n\nThis is basically it. ",
2018-06-08T14:18:32
yy