Mercurial > repos > greg > gregs_filter
comparison filtering.py @ 3:48edb538c102 draft default tip
Uploaded 3.3.0
author | greg |
---|---|
date | Wed, 03 Oct 2012 13:22:53 -0400 |
parents | f28d5018f9cb |
children |
comparison
equal
deleted
inserted
replaced
2:24884dd48421 | 3:48edb538c102 |
---|---|
30 in_fname = sys.argv[1] | 30 in_fname = sys.argv[1] |
31 out_fname = sys.argv[2] | 31 out_fname = sys.argv[2] |
32 cond_text = sys.argv[3] | 32 cond_text = sys.argv[3] |
33 try: | 33 try: |
34 in_columns = int( sys.argv[4] ) | 34 in_columns = int( sys.argv[4] ) |
35 assert sys.argv[5] #check to see that the column types varaible isn't null | 35 assert sys.argv[5] #check to see that the column types variable isn't null |
36 in_column_types = sys.argv[5].split( ',' ) | 36 in_column_types = sys.argv[5].split( ',' ) |
37 except: | 37 except: |
38 stop_err( "Data does not appear to be tabular. This tool can only be used with tab-delimited data." ) | 38 stop_err( "Data does not appear to be tabular. This tool can only be used with tab-delimited data." ) |
39 | 39 |
40 # Unescape if input has been escaped | 40 # Unescape if input has been escaped |
59 check = int( operand ) | 59 check = int( operand ) |
60 except: | 60 except: |
61 if operand in secured: | 61 if operand in secured: |
62 stop_err( "Illegal value '%s' in condition '%s'" % ( operand, cond_text ) ) | 62 stop_err( "Illegal value '%s' in condition '%s'" % ( operand, cond_text ) ) |
63 | 63 |
64 # Prepare the column variable names and wrappers for column data types | 64 # Work out which columns are used in the filter (save using 1 based counting) |
65 used_cols = sorted(set(int(match.group()[1:]) \ | |
66 for match in re.finditer('c(\d)+', cond_text))) | |
67 largest_col_index = max(used_cols) | |
68 | |
69 # Prepare the column variable names and wrappers for column data types. Only | |
70 # cast columns used in the filter. | |
65 cols, type_casts = [], [] | 71 cols, type_casts = [], [] |
66 for col in range( 1, in_columns + 1 ): | 72 for col in range( 1, largest_col_index + 1 ): |
67 col_name = "c%d" % col | 73 col_name = "c%d" % col |
68 cols.append( col_name ) | 74 cols.append( col_name ) |
69 col_type = in_column_types[ col - 1 ] | 75 col_type = in_column_types[ col - 1 ] |
70 type_cast = "%s(%s)" % ( col_type, col_name ) | 76 if col in used_cols: |
77 type_cast = "%s(%s)" % ( col_type, col_name ) | |
78 else: | |
79 #If we don't use this column, don't cast it. | |
80 #Otherwise we get errors on things like optional integer columns. | |
81 type_cast = col_name | |
71 type_casts.append( type_cast ) | 82 type_casts.append( type_cast ) |
72 | 83 |
73 col_str = ', '.join( cols ) # 'c1, c2, c3, c4' | 84 col_str = ', '.join( cols ) # 'c1, c2, c3, c4' |
74 type_cast_str = ', '.join( type_casts ) # 'str(c1), int(c2), int(c3), str(c4)' | 85 type_cast_str = ', '.join( type_casts ) # 'str(c1), int(c2), int(c3), str(c4)' |
75 assign = "%s = line.split( '\\t' )" % col_str | 86 assign = "%s, = line.split( '\\t' )[:%i]" % ( col_str, largest_col_index ) |
76 wrap = "%s = %s" % ( col_str, type_cast_str ) | 87 wrap = "%s = %s" % ( col_str, type_cast_str ) |
77 skipped_lines = 0 | 88 skipped_lines = 0 |
89 invalid_lines = 0 | |
78 first_invalid_line = 0 | 90 first_invalid_line = 0 |
79 invalid_line = None | 91 invalid_line = None |
80 lines_kept = 0 | 92 lines_kept = 0 |
81 total_lines = 0 | 93 total_lines = 0 |
82 out = open( out_fname, 'wt' ) | 94 out = open( out_fname, 'wt' ) |
86 for i, line in enumerate( file( in_fname ) ): | 98 for i, line in enumerate( file( in_fname ) ): |
87 total_lines += 1 | 99 total_lines += 1 |
88 line = line.rstrip( '\\r\\n' ) | 100 line = line.rstrip( '\\r\\n' ) |
89 if not line or line.startswith( '#' ): | 101 if not line or line.startswith( '#' ): |
90 skipped_lines += 1 | 102 skipped_lines += 1 |
91 if not invalid_line: | |
92 first_invalid_line = i + 1 | |
93 invalid_line = line | |
94 continue | 103 continue |
95 try: | 104 try: |
96 %s | 105 %s |
97 %s | 106 %s |
98 if %s: | 107 if %s: |
99 lines_kept += 1 | 108 lines_kept += 1 |
100 print >> out, line | 109 print >> out, line |
101 except: | 110 except: |
102 skipped_lines += 1 | 111 invalid_lines += 1 |
103 if not invalid_line: | 112 if not invalid_line: |
104 first_invalid_line = i + 1 | 113 first_invalid_line = i + 1 |
105 invalid_line = line | 114 invalid_line = line |
106 ''' % ( assign, wrap, cond_text ) | 115 ''' % ( assign, wrap, cond_text ) |
107 | 116 |
119 if valid_filter: | 128 if valid_filter: |
120 out.close() | 129 out.close() |
121 valid_lines = total_lines - skipped_lines | 130 valid_lines = total_lines - skipped_lines |
122 print 'Filtering with %s, ' % cond_text | 131 print 'Filtering with %s, ' % cond_text |
123 if valid_lines > 0: | 132 if valid_lines > 0: |
124 print 'kept %4.2f%% of %d lines.' % ( 100.0*lines_kept/valid_lines, total_lines ) | 133 print 'kept %4.2f%% of %d valid lines (%d total lines).' % ( 100.0*lines_kept/valid_lines, valid_lines, total_lines ) |
125 else: | 134 else: |
126 print 'Possible invalid filter condition "%s" or non-existent column referenced. See tool tips, syntax and examples.' % cond_text | 135 print 'Possible invalid filter condition "%s" or non-existent column referenced. See tool tips, syntax and examples.' % cond_text |
127 if skipped_lines > 0: | 136 if invalid_lines: |
128 print 'Skipped %d invalid lines starting at line #%d: "%s"' % ( skipped_lines, first_invalid_line, invalid_line ) | 137 print 'Skipped %d invalid line(s) starting at line #%d: "%s"' % ( invalid_lines, first_invalid_line, invalid_line ) |
138 if skipped_lines: | |
139 print 'Skipped %i comment (starting with #) or blank line(s)' % skipped_lines |