import MapReduce
import sys

mr = MapReduce.MapReduce()

def mapper(record):
    key = record[1]
    value = record
    mr.emit_intermediate(key, value)

def reducer(key, list_of_values):
    for i in list_of_values:
        if i[0] == "order":
            for j in list_of_values:
                if j[0] == "line_item":
                    partial = []
                    for l in i:
                        partial.append(l)
                    for l in j:
                        partial.append(l)
                    mr.emit(partial)

if __name__ == '__main__':
    inputdata = open(sys.argv[1])
    mr.execute(inputdata, mapper, reducer)
