Commit feef62e170734a82145c568caa93fb6ca3ba4246

Authored by Jürgen Knödlseder
1 parent f52a79f5

Fix again

Showing 1 changed file with 39 additions and 8 deletions
modules/comscripts/comgendb.py
... ... @@ -160,6 +160,37 @@ class comgendb(ctools.cscript):
160 160 # Return relative path
161 161 return relpath
162 162  
  163 + def _sorted(self, elements):
  164 + """
  165 + Sort list of dict according to 'tjd_start'
  166 +
  167 + Parameters
  168 + ----------
  169 + elements : list of dict
  170 + List of dictionary
  171 +
  172 + Returns
  173 + -------
  174 + sorted_elements : list of dict
  175 + Sorted list of dictionary
  176 + """
  177 + # Set Python version that has sorted function and current
  178 + # Python version
  179 + req_version = (2,4)
  180 + cur_version = sys.version_info
  181 +
  182 + # If Python version has sorted the use sorted function
  183 + if cur_version >= req_version:
  184 + sorted_elements = sorted(elements, key=lambda k: k['tjd_start'])
  185 +
  186 + # ... otherwise use sort() with cmp
  187 + else:
  188 + sorted_elements = elements
  189 + sorted_elements.sort(lambda x,y : cmp(x['tjd_start'], y['tjd_start']))
  190 +
  191 + # Return sorted elements
  192 + return sorted_elements
  193 +
163 194 def _get_evp_list(self):
164 195 """
165 196 Get EVP list
... ... @@ -192,7 +223,7 @@ class comgendb(ctools.cscript):
192 223 evp_list.extend(evps)
193 224  
194 225 # Sort EVP list by start day
195   - evp_list.sort(lambda x,y : cmp(x['tjd_start'], y['tjd_start']))
  226 + evp_list = self._sorted(evp_list)
196 227  
197 228 # Return EVP list
198 229 return evp_list
... ... @@ -274,7 +305,7 @@ class comgendb(ctools.cscript):
274 305 evp_list.append(evp_dict)
275 306  
276 307 # Sort EVP list by start day
277   - evp_list.sort(lambda x,y : cmp(x['tjd_start'], y['tjd_start']))
  308 + evp_list = self._sorted(evp_list)
278 309  
279 310 # Return EVP list
280 311 return evp_list
... ... @@ -426,7 +457,7 @@ class comgendb(ctools.cscript):
426 457 oad_list.extend(oads)
427 458  
428 459 # Sort OAD list by start day
429   - oad_list.sort(lambda x,y : cmp(x['tjd_start'], y['tjd_start']))
  460 + oad_list = self._sorted(oad_list)
430 461  
431 462 # Return OAD list
432 463 return oad_list
... ... @@ -488,7 +519,7 @@ class comgendb(ctools.cscript):
488 519 oad_list.append(oad_dict)
489 520  
490 521 # Sort OAD list by start day
491   - oad_list.sort(lambda x,y : cmp(x['tjd_start'], y['tjd_start']))
  522 + oad_list = self._sorted(oad_list)
492 523  
493 524 # Return OAD list
494 525 return oad_list
... ... @@ -613,7 +644,7 @@ class comgendb(ctools.cscript):
613 644 tim_list.extend(tims)
614 645  
615 646 # Sort TIM list by start day
616   - tim_list.sort(lambda x,y : cmp(x['tjd_start'], y['tjd_start']))
  647 + tim_list = self._sorted(tim_list)
617 648  
618 649 # Return TIM list
619 650 return tim_list
... ... @@ -675,7 +706,7 @@ class comgendb(ctools.cscript):
675 706 tim_list.append(tim_dict)
676 707  
677 708 # Sort TIM list by start day
678   - tim_list.sort(lambda x,y : cmp(x['tjd_start'], y['tjd_start']))
  709 + tim_list = self._sorted(tim_list)
679 710  
680 711 # Return TIM list
681 712 return tim_list
... ... @@ -824,7 +855,7 @@ class comgendb(ctools.cscript):
824 855 xml_list.append(xml)
825 856  
826 857 # Sort XML list by start day
827   - xml_list.sort(lambda x,y : cmp(x['tjd_start'], y['tjd_start']))
  858 + xml_list = self._sorted(xml_list)
828 859  
829 860 # Return XML list
830 861 return xml_list
... ... @@ -1211,7 +1242,7 @@ class comgendb(ctools.cscript):
1211 1242 oads.append(oad)
1212 1243  
1213 1244 # Sort OAD list by start day
1214   - oads.sort(lambda x,y : cmp(x['tjd_start'], y['tjd_start']))
  1245 + oads = self._sorted(oads)
1215 1246  
1216 1247 # Return OADs
1217 1248 return oads
... ...