Passing values and calling functions from other functions
Question: I have this class that consists of 3 functions. Each function is in charge of
one part of the whole process.

`.load()` loads up two files, re-formats their content and writes them to two
new files.

`.compare()` takes two files and prints out their differences in a specific
format.

`.final()` takes the result of `.compare()` and creates a file for every set
of values.

Please ignore the Frankenstein nature of the logic as it is not my main
concern at the moment. I know it can be written a thousand times better and
that's fine by me for now as i am still new to Python and programing in
general. I do have some theoretical experience but very limited technical
practice and that is something i am working on.

Here is the code:

    
    
    from collections import defaultdict
    from operator import itemgetter
    from itertools import groupby
    from collections import deque
    import os
    
    
    class avs_auto:
    
    
        def load(self, fileIn1, fileIn2, fileOut1, fileOut2):
            with open(fileIn1+'.txt') as fin1, open(fileIn2+'.txt') as fin2:
                frame_rects = defaultdict(list)
                for row in (map(str, line.split()) for line in fin1):
                    id, frame, rect = row[0], row[2], [row[3],row[4],row[5],row[6]]
                    frame_rects[frame].append(id)
                    frame_rects[frame].append(rect)
                for row in (map(str, line.split()) for line in fin2):
                    id, frame, rect = row[0], row[2], [row[3],row[4],row[5],row[6]]
                    frame_rects[frame].append(id)
                    frame_rects[frame].append(rect)
    
            with open(fileOut1+'.txt', 'w') as fout1, open(fileOut2+'.txt', 'w') as fout2:
                for frame, rects in sorted(frame_rects.iteritems()):
                    fout1.write('{{{}:{}}}\n'.format(frame, rects))
                    fout2.write('{{{}:{}}}\n'.format(frame, rects))
    
        def compare(self, f1, f2):
            with open(f1+'.txt', 'r') as fin1:
                with open(f2+'.txt', 'r') as fin2:
                    lines1 = fin1.readlines()
                    lines2 = fin2.readlines()
                    diff_lines = [l.strip() for l in lines1 if l not in lines2]
                    diffs = defaultdict(list)
                    with open(f1+'x'+f2+'Result.txt', 'w') as fout:
                        for line in diff_lines:
                            d = eval(line)
                            for k in d:
                                list_ids = d[k]
                                for i in range(0, len(d[k]), 2):
                                    diffs[d[k][i]].append(k)
                        for id_ in diffs:
                            diffs[id_].sort()
                            for k, g in groupby(enumerate(diffs[id_]), lambda (i, x): i - x):
                                group = map(itemgetter(1), g)
                                fout.write('{0} {1} {2}\n'.format(id_, group[0], group[-1]))
    
        def final(self):
            with open('hw1load3xhw1load2Result.txt', 'r') as fin:
                lines = (line.split() for line in fin)
                for k, g in groupby(lines, itemgetter(0)):
                    fst = next(g)
                    lst = next(iter(deque(g, 1)), fst)
                    with open('final/{}.avs'.format(k), 'w') as fout:
                        fout.write('video0=ImageSource("MovieName\original\%06d.jpeg", {}, {}, 15)\n'.format(fst[1], lst[2]))
    

Now to my question, how do i make it so each of the functions passes it's
output files as values to the next function and calls it?

So for an example:

running `.load()` should output two files, call the `.compare()` function
passing it those two files.

Then when `.compare()` is done, it should pass `.final()` the output file and
calls it.

So `.final()` will open whatever file is passed to it from `.compare()` and
not `"test123.txt"` as it is defined above.

I hope this all makes sense. Let me know if you need clarification. Any
criticism is welcome concerning the code itself. Thanks in advance.


Answer: Do you mean call with the name of the two files? Well you defined a class, so
you can just do:

    
    
    def load(self, fileIn1, fileIn2, fileOut1, fileOut2):
        ... // do stuff here
        // when done
        self.compare( fileOut1, fileOut2 )
    

And so on.

