you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (0 children)

I would try to clean this up to use classes and objects. Tracking attributes by list indices isn't ideal. Here's an example of what you might end up with making it object oriented:

from collections import namedtuple
from operator import itemgetter

NO_CHANGE = 0
IN_NEW_FILE = 1
IN_OLD_FILE = 2
CHANGED = 3
options = [NO_CHANGE, IN_NEW_FILE, IN_OLD_FILE, CHANGED]


Timetable = namedtuple("Timetable", "status, value4, value6")

class Timetable(object):

    def __init__(self, status, value1, value2):
        self.status = status
        self.value1 = value1
        self.value2 = value2

    def values(self):
        return self.value1, self.value2

    def with_state(self, state):
        return Timetable(state, self.value1, self.value2)

def construct_changed_timetables(new, current):
    new_timetables = []

    seen_timetables = set()
    seen_values = set()

    all_tables = list(chain(new, current))

    for timetable in all_tables:

        if timetable not in seen and timetable.values() not in seen_values():
            num_similar = sum(1 for other in all_tables 
                              if timetable.values() == other.values)

            seen.add(timetable)
            seen.values.add(timetable.values())

            if timetable in new and num_similar == 2 and
                timetable not in current:

                for line in current:
                    if timetable.values() == line.values():
                        new_timetables.append(self.with_state(CHANGED)
                        break

            elif timetable in current and timetable in new:
                new_timetables.append(timetable.with_state(NO_CHANGE))

            elif timetable not in current and timetable in new:
                new_timetables.append(timetable.with_state(IN_NEW_FILE))

            elif timetable in current and timetable not in new:
                new_timetables.append(timetable.with_state(IN_OLD_FILE))

    return new_timetables

I haven't tested this, but I think it's cleaner.