Skip to content
Snippets Groups Projects
Commit ccf5d258 authored by Samuel GAIST's avatar Samuel GAIST
Browse files

[experiments][api] Fix sorting of experiments for Python 3

Python 3 has brought several modifications regarding sorting.
This patch refactors the code so it can be properly used with
both versions of Python.
parent 4ed962c5
No related branches found
No related tags found
2 merge requests!2551.4.x,!249Web fixes
...@@ -29,6 +29,7 @@ import re ...@@ -29,6 +29,7 @@ import re
import uuid import uuid
import simplejson import simplejson
import functools
from django.conf import settings from django.conf import settings
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
...@@ -104,6 +105,15 @@ class ListCreateExperimentsView(ListCreateContributionView): ...@@ -104,6 +105,15 @@ class ListCreateExperimentsView(ListCreateContributionView):
def _getStatusLabel(status): def _getStatusLabel(status):
return [s for s in Experiment.STATUS if s[0] == status][0][1] return [s for s in Experiment.STATUS if s[0] == status][0][1]
def _cmp(a, b):
"""
cmp is not available anymore in Python 3. This method is implemetend
as recommended in the documentation for this kind of use case.
Based on:
https://docs.python.org/3.0/whatsnew/3.0.html#ordering-comparisons
"""
return (a > b) - (a < b)
def _custom_compare(exp1, exp2): def _custom_compare(exp1, exp2):
PENDING = _getStatusLabel(Experiment.PENDING) PENDING = _getStatusLabel(Experiment.PENDING)
...@@ -113,7 +123,7 @@ class ListCreateExperimentsView(ListCreateContributionView): ...@@ -113,7 +123,7 @@ class ListCreateExperimentsView(ListCreateContributionView):
if exp2['status'] != PENDING: if exp2['status'] != PENDING:
return -1 return -1
elif exp1['creation_date'] != exp2['creation_date']: elif exp1['creation_date'] != exp2['creation_date']:
return cmp(exp2['creation_date'], exp1['creation_date']) return _cmp(exp1['creation_date'], exp2['creation_date'])
else: else:
return 1 return 1
elif exp2['status'] == PENDING: elif exp2['status'] == PENDING:
...@@ -123,13 +133,13 @@ class ListCreateExperimentsView(ListCreateContributionView): ...@@ -123,13 +133,13 @@ class ListCreateExperimentsView(ListCreateContributionView):
tier3_states = [_getStatusLabel(Experiment.DONE), _getStatusLabel(Experiment.FAILED)] tier3_states = [_getStatusLabel(Experiment.DONE), _getStatusLabel(Experiment.FAILED)]
if (exp1['status'] in tier3_states) and (exp2['status'] in tier3_states): if (exp1['status'] in tier3_states) and (exp2['status'] in tier3_states):
return cmp(exp2['end_date'], exp1['end_date']) return _cmp(exp2['end_date'], exp1['end_date'])
elif (exp1['status'] in tier3_states) and (exp2['status'] not in tier3_states): elif (exp1['status'] in tier3_states) and (exp2['status'] not in tier3_states):
return -1 return -1
elif (exp1['status'] not in tier3_states) and (exp2['status'] in tier3_states): elif (exp1['status'] not in tier3_states) and (exp2['status'] in tier3_states):
return 1 return 1
return cmp(exp1['start_date'], exp2['start_date']) return _cmp(exp1['start_date'], exp2['start_date'])
# Retrieve the experiments # Retrieve the experiments
fields_to_return_original = self.get_serializer_fields(request) fields_to_return_original = self.get_serializer_fields(request)
...@@ -140,8 +150,10 @@ class ListCreateExperimentsView(ListCreateContributionView): ...@@ -140,8 +150,10 @@ class ListCreateExperimentsView(ListCreateContributionView):
experiments = self.model.objects.from_author_and_public(request.user, author_name).select_related() experiments = self.model.objects.from_author_and_public(request.user, author_name).select_related()
serializer = self.get_serializer(experiments, many=True, fields=fields_to_return) serializer = self.get_serializer(experiments, many=True, fields=fields_to_return)
result = serializer.data if six.PY2:
result.sort(_custom_compare) result = sorted(serializer.data, cmp=_custom_compare)
else:
result = sorted(serializer.data, key=functools.cmp_to_key(_custom_compare))
processed_result = [] processed_result = []
for experiment in result: for experiment in result:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment