Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- from operator import itemgetter
- from itertools import groupby
- from collections import deque
- from ast import literal_eval
- import os
- class my_file_utils:
- def __init__(self, file_a=None, file_b=None):
- ''' create an instance of my_file_utils, with or without pre-set files '''
- self.f1 = file_a
- self.f2 = file_b
- def set_file_a(self, file_a):
- ''' set file_a '''
- self.f1 = file_a
- def set_file_b(self, file_b):
- ''' set file_b '''
- self.f2 = file_b
- @property
- def files_set(self):
- ''' checks if file_a and file_b are set '''
- return (self.f1 and self.f2)
- def load(self, file_in=None, file_out=None):
- '''
- Reads file_in (file_a)
- Parses lines.
- Writes to file_out (file_b)
- '''
- if not file_in and self.f1:
- file_in = self.f1
- elif not file_in and not self.f1:
- raise ValueError('no input file specified, and none pre-set')
- if not file_out and self.f2:
- file_out = self.f2
- elif not file_out and not self.f2:
- raise ValueError('no output file specified, and none pre-set')
- with open(file_in) as fin, open(fileOut, 'w') as fout:
- for line in fin:
- rows = line.split()
- frame, rect = row[2], row[3:7]
- fout.write('{{{}:{}}}\n'.format(frame, rect))
- def compare(self, output_file='my_file_compare_results.txt', first_file=None, second_file=None):
- '''
- Reads two files and compares them
- Keeps all lines in first_file not in second_file
- Does something to them ?
- If no output specified, it is created in CWD with the name:
- my_file_compare_results.txt
- '''
- if not first_file and self.f1:
- first_file = self.f1
- elif not first_file and not self.f1:
- raise ValueError('no first_file specified, and none pre-set')
- if not second_file and self.f2:
- second_file = self.f2
- elif not second_file and not self.f2:
- raise ValueError('no second_file specified, and none pre-set')
- diffs = defaultdict(list)
- with open(first_file) as f1, open(second_file) as f2:
- lines_1 = [l.strip() for l in f1]
- lines_2 = [l.strip() for l in f2]
- diff_lines = [l for l in lines_1 if l not in lines_2]
- for line in diff_ines:
- d = literal_eval(line)
- for k, v in d.items():
- for i in range(0, len(d[k]), 2):
- diffs[d[k][i]].append(k)
- with open(output_file, 'w') as fout:
- for k, v in diffs.items():
- for _, g in groupby(enumerate(sorted(v)), lambda (i, x): i - x):
- group = map(itemgetter(1), g)
- fout.write('{0} {1} {2}\n'.format(k, group[0], group[-1]))
- def final(self, file_in=None, dir_out=None):
- '''
- Reads from file_in (file_a)
- Does something ?
- Writes to file_out (file_b)
- '''
- if not file_in and self.f1:
- file_in = self.f1
- elif not file_in and not self.f1:
- raise ValueError('no input file specified, and none pre-set')
- if not dir_out:
- dir_out = os.getcwd()
- else:
- raise ValueError('no output dir specified')
- with open(file_in) 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(dir_out + '/final_{}.txt'.format(k), 'w') as fout:
- fout.write('{} {}'.format(fst[1], lst[2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement