Advertisement
InbarRose

max_file_utils

Jun 4th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.89 KB | None | 0 0
  1. from collections import defaultdict
  2. from operator import itemgetter
  3. from itertools import groupby
  4. from collections import deque
  5. from ast import literal_eval
  6. import os
  7.  
  8. class my_file_utils:
  9.  
  10.     def __init__(self, file_a=None, file_b=None):
  11.         ''' create an instance of my_file_utils, with or without pre-set files '''
  12.         self.f1 = file_a
  13.         self.f2 = file_b
  14.  
  15.     def set_file_a(self, file_a):
  16.         ''' set file_a '''
  17.         self.f1 = file_a
  18.  
  19.     def set_file_b(self, file_b):
  20.         ''' set file_b '''
  21.         self.f2 = file_b
  22.  
  23.     @property
  24.     def files_set(self):
  25.         ''' checks if file_a and file_b are set '''
  26.         return (self.f1 and self.f2)
  27.    
  28.     def load(self, file_in=None, file_out=None):
  29.         '''
  30.        Reads file_in (file_a)
  31.        Parses lines.
  32.        Writes to file_out (file_b)
  33.        '''
  34.         if not file_in and self.f1:
  35.             file_in = self.f1
  36.         elif not file_in and not self.f1:
  37.             raise ValueError('no input file specified, and none pre-set')
  38.         if not file_out and self.f2:
  39.             file_out = self.f2
  40.         elif not file_out and not self.f2:
  41.             raise ValueError('no output file specified, and none pre-set')            
  42.  
  43.         with open(file_in) as fin, open(fileOut, 'w') as fout:
  44.             for line in fin:
  45.                 rows = line.split()
  46.                 frame, rect = row[2], row[3:7]
  47.                 fout.write('{{{}:{}}}\n'.format(frame, rect))
  48.  
  49.     def compare(self, output_file='my_file_compare_results.txt', first_file=None, second_file=None):
  50.         '''
  51.        Reads two files and compares them
  52.        Keeps all lines in first_file not in second_file
  53.        Does something to them ?
  54.        If no output specified, it is created in CWD with the name:
  55.            my_file_compare_results.txt
  56.        '''
  57.         if not first_file and self.f1:
  58.             first_file = self.f1
  59.         elif not first_file and not self.f1:
  60.             raise ValueError('no first_file specified, and none pre-set')
  61.         if not second_file and self.f2:
  62.             second_file = self.f2
  63.         elif not second_file and not self.f2:
  64.             raise ValueError('no second_file specified, and none pre-set')  
  65.  
  66.         diffs = defaultdict(list)
  67.         with open(first_file) as f1, open(second_file) as f2:
  68.             lines_1 = [l.strip() for l in f1]
  69.             lines_2 = [l.strip() for l in f2]
  70.             diff_lines = [l for l in lines_1 if l not in lines_2]
  71.        
  72.         for line in diff_ines:
  73.             d = literal_eval(line)
  74.             for k, v in d.items():
  75.                 for i in range(0, len(d[k]), 2):
  76.                     diffs[d[k][i]].append(k)                
  77.                
  78.         with open(output_file, 'w') as fout:
  79.             for k, v in diffs.items():
  80.                 for _, g in groupby(enumerate(sorted(v)), lambda (i, x): i - x):
  81.                     group = map(itemgetter(1), g)
  82.                     fout.write('{0} {1} {2}\n'.format(k, group[0], group[-1]))
  83.  
  84.     def final(self, file_in=None, dir_out=None):
  85.         '''
  86.        Reads from file_in (file_a)
  87.        Does something ?
  88.        Writes to file_out (file_b)
  89.        '''
  90.         if not file_in and self.f1:
  91.             file_in = self.f1
  92.         elif not file_in and not self.f1:
  93.             raise ValueError('no input file specified, and none pre-set')
  94.         if not dir_out:
  95.             dir_out = os.getcwd()
  96.         else:
  97.             raise ValueError('no output dir specified')          
  98.        
  99.         with open(file_in) as fin:
  100.             lines = (line.split() for line in fin)
  101.             for k, g in groupby(lines, itemgetter(0)):
  102.                 fst = next(g)
  103.                 lst = next(iter(deque(g, 1)), fst)
  104.                
  105.                 with open(dir_out + '/final_{}.txt'.format(k), 'w') as fout:
  106.                     fout.write('{} {}'.format(fst[1], lst[2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement