(x, y)
for a given COBservation, do the following.
    1: If it's an addendum/reconciliation trumps all
    2: If it's direct, and other is not direct, the direct wins
    3: If both direct, do by earliest date
    4: If neither direct, do by earliest encounter_date regardless of method.

    < -1
    = 0
    > 1

    Assumes that x and y have the same observation date (cell in the DOT json array)
    Encounter date is the date in which the date cell is observed.
for a given COBservation, do the following.
    1: If it's an addendum/reconciliation trumps all
    2: If it's direct, and other is not direct, the direct wins
    3: If both direct, do by earliest date
    4: If neither direct, do by earliest encounter_date regardless of method.
for a given COBservation do the following . 1 : If it s an addendum / reconciliation trumps all 2 : If it s direct and other is not direct the direct wins 3 : If both direct do by earliest date 4 : If neither direct do by earliest encounter_date regardless of method .
def cmp_observation(x, y):
    """
    for a given COBservation, do the following.
    1: If it's an addendum/reconciliation trumps all
    2: If it's direct, and other is not direct, the direct wins
    3: If both direct, do by earliest date
    4: If neither direct, do by earliest encounter_date regardless of method.

    < -1
    = 0
    > 1

    Assumes that x and y have the same observation date (cell in the DOT json array)
    Encounter date is the date in which the date cell is observed.
    """

    assert x.observed_date.date() == y.observed_date.date()
    x_is_reconciliation = getattr(x, 'is_reconciliation', None)
    y_is_reconciliation = getattr(y, 'is_reconciliation', None)
    # Reconciliation handling
    # reconciliations always win.
    if x_is_reconciliation and y_is_reconciliation:
        # sort by earlier date, so flip x,y
        return cmp(y.submitted_date, x.submitted_date)
    elif x_is_reconciliation and not y_is_reconciliation:
        # result: x > y
        return 1
    elif not x_is_reconciliation and y_is_reconciliation:
        # result: x < y
        return -1
    elif x.method == DOT_OBSERVATION_DIRECT and y.method == DOT_OBSERVATION_DIRECT:
        # Direct observations win next
        # sort by earlier date, so flip x,y
        return cmp(y.encounter_date, x.encounter_date)
    elif x.method == DOT_OBSERVATION_DIRECT and y.method != DOT_OBSERVATION_DIRECT:
        # result: x > y
        return 1
    elif x.method != DOT_OBSERVATION_DIRECT and y.method == DOT_OBSERVATION_DIRECT:
        # result: x < y
        return -1
    elif (x.adherence, x.method) == DOT_UNCHECKED_CELL and (y.adherence, y.method) != DOT_UNCHECKED_CELL:
        # unchecked should always lose
        return -1
    elif (x.adherence, x.method) != DOT_UNCHECKED_CELL and (y.adherence, y.method) == DOT_UNCHECKED_CELL:
        return 1
    else:
        return cmp(y.encounter_date, x.encounter_date)
def cmp_observation ( x , y ) : assert x . observed_date . date ( ) == y . observed_date . date ( ) x_is_reconciliation = getattr ( x , 'is_reconciliation' , None ) y_is_reconciliation = getattr ( y , 'is_reconciliation' , None ) # Reconciliation handling # reconciliations always win. if x_is_reconciliation and y_is_reconciliation : # sort by earlier date, so flip x,y return cmp ( y . submitted_date , x . submitted_date ) elif x_is_reconciliation and not y_is_reconciliation : # result: x > y return 1 elif not x_is_reconciliation and y_is_reconciliation : # result: x < y return - 1 elif x . method == DOT_OBSERVATION_DIRECT and y . method == DOT_OBSERVATION_DIRECT : # Direct observations win next # sort by earlier date, so flip x,y return cmp ( y . encounter_date , x . encounter_date ) elif x . method == DOT_OBSERVATION_DIRECT and y . method != DOT_OBSERVATION_DIRECT : # result: x > y return 1 elif x . method != DOT_OBSERVATION_DIRECT and y . method == DOT_OBSERVATION_DIRECT : # result: x < y return - 1 elif ( x . adherence , x . method ) == DOT_UNCHECKED_CELL and ( y . adherence , y . method ) != DOT_UNCHECKED_CELL : # unchecked should always lose return - 1 elif ( x . adherence , x . method ) != DOT_UNCHECKED_CELL and ( y . adherence , y . method ) == DOT_UNCHECKED_CELL : return 1 else : return cmp ( y . encounter_date , x . encounter_date )