Changing the comparison function to use keys for sorting

I have the following code written in Python 2.6, with a class Taskthat has an optional due_date:

class Task(models.Model):
    due_date = models.DateField(null = True, blank = True)

    @staticmethod
    def compare_by_due_date(t1, t2):
        return due_date_cmp(t1.due_date, t2.due_date)

def due_date_cmp(t1, t2):
    if t1 is None and t2 is None:
        return 0;
    if t1 is None:
        return 1
    if t2 is None:
        return -1
    return (t1 - t2).days

The reason I extracted the comparison function outside the class is because I wanted to be able to test it without creating instances Task. I use compare_by_due_datein the following code to organize tasks, increasing the execution time, with tasks that do not have an end date at the end of the list:

tasks = Task.objects.all()
tasks.sort(Task.compare_by_due_date)

I understood from this answer in the code review that I should use keys instead of the comparison function ? Can you show me how?

+3
source share
2 answers

(, datetime.date None), 2.x , 3.x, _ .

def due_date_key(t):
  return (t.due_date is None, t.due_date)

tasks.sort(key=due_date_key)

, , .

+4

, due_date_cmp , datetime.date None, None .

, None datetime.date:

( cmp):

import datetime as dt

def due_date_cmp(t1, t2):
    if t1 == None and t2 == None:
        return 0;
    if t1 == None:
        return 1
    if t2 == None:
        return -1
    return (t1 - t2).days


dates=[dt.date(2000,1,1),None,dt.date(1999,1,1),None,dt.date(2002,1,1)]
dates.sort(cmp=due_date_cmp)
print(dates)
# [datetime.date(1999, 1, 1), datetime.date(2000, 1, 1), datetime.date(2002, 1, 1), None, None]

( ):

def due_date_key(t):
    if t is None:
        return dt.date(dt.MAXYEAR,12,31)
    else:
        return t

dates=[dt.date(2000,1,1),None,dt.date(1999,1,1),None,dt.date(2002,1,1)]
dates.sort(key=due_date_key)
print(dates)

# [datetime.date(1999, 1, 1), datetime.date(2000, 1, 1), datetime.date(2002, 1, 1), None, None]

, due_date_key :

import operator
class Task(models.Model):
    @property
    def due_date_key(self):
        due_date=self.due_date
        if due_date is None:
            return dt.date(dt.MAXYEAR,12,31)
        else:
            return due_date

tasks = Task.objects.all()
tasks.sort(key=operator.attrgetter('due_date_key'))
+2
source

Source: https://habr.com/ru/post/1789583/


All Articles