annotate micropita_format_input_selector.py @ 32:041787cd0d31 draft default tip

Modified from StringIO import StringIO ## for Python 2 to from io import StringIO ## for Python 3
author george-weingart
date Wed, 23 Jun 2021 20:52:58 +0000
parents d9862a9a4d84
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
1 #!/usr/bin/env python
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
2
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
3 """
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
4 Author: George Weingart
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
5 Description: Dynamically read columns from input file for UI
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
6 """
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
7
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
8 #####################################################################################
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
9 #Copyright (C) <2012>
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
10 #
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
11 #Permission is hereby granted, free of charge, to any person obtaining a copy of
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
12 #this software and associated documentation files (the "Software"), to deal in the
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
13 #Software without restriction, including without limitation the rights to use, copy,
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
14 #modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
15 #and to permit persons to whom the Software is furnished to do so, subject to
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
16 #the following conditions:
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
17 #
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
18 #The above copyright notice and this permission notice shall be included in all copies
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
19 #or substantial portions of the Software.
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
20 #
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
21 #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
22 #INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
23 #PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
24 #HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
25 #OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
26 #SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
27 #####################################################################################
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
28
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
29 __author__ = "George Weingart"
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
30 __copyright__ = "Copyright 2012"
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
31 __credits__ = ["George Weingart"]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
32 __license__ = "MIT"
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
33 __maintainer__ = "George Weingart"
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
34 __email__ = "george.weingart@gmail.com"
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
35 __status__ = "Development"
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
36
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
37 import sys,string,time
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
38 from pprint import pprint
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
39
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
40 def red(st,l):
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
41 if len(st) <= l: return st
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
42 l1,l2 = l/2,l/2
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
43 return st[:l1]+".."+st[len(st)-l2:]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
44
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
45 def get_cols(data,full_names):
27
d9862a9a4d84 Uploaded micropita_format_input_Selector.py - fixed spaces tabs inconsistencies
george-weingart
parents: 0
diff changeset
46 if data == "":
d9862a9a4d84 Uploaded micropita_format_input_Selector.py - fixed spaces tabs inconsistencies
george-weingart
parents: 0
diff changeset
47 return []
0
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
48 max_len =32
27
d9862a9a4d84 Uploaded micropita_format_input_Selector.py - fixed spaces tabs inconsistencies
george-weingart
parents: 0
diff changeset
49 fname = data.dataset.file_name
0
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
50 input_file = open(fname,'rU')
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
51 input_lines = input_file.readlines()
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
52 input_file.close()
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
53 table_lines = []
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
54 for x in input_lines:
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
55 first_column = x.split('\t')[0]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
56 table_lines.append(first_column)
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
57
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
58 opt = []
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
59 rc = ''
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
60 lines = []
27
d9862a9a4d84 Uploaded micropita_format_input_Selector.py - fixed spaces tabs inconsistencies
george-weingart
parents: 0
diff changeset
61 try:
0
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
62 lines = [(red((rc+v.split()[0]),max_len),'%d' % (i+1),False) for i,v in enumerate(table_lines) if v]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
63
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
64 except:
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
65 l1 = '*ALL*'
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
66 l2 = 1
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
67 l3 = False
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
68 MyList = [l1,l2,l3]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
69 lines.append(MyList)
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
70 return opt+lines
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
71
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
72 def get_cols_add_line(data,full_names,lastmeta):
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
73 if data == "": return []
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
74 display_to = 1
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
75 try:
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
76 display_to = int(lastmeta)
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
77 except:
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
78 pass
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
79
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
80 max_len = 32
27
d9862a9a4d84 Uploaded micropita_format_input_Selector.py - fixed spaces tabs inconsistencies
george-weingart
parents: 0
diff changeset
81 fname = data.dataset.file_name
0
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
82 input_file = open(fname,'rU')
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
83 input_lines = input_file.readlines()
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
84 input_file.close()
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
85 table_lines = []
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
86 for x in input_lines:
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
87 first_column = x.split('\t')[0]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
88 table_lines.append(first_column)
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
89 table_lines.insert(0,'-')
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
90 if not display_to == 1:
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
91 del table_lines[display_to + 1:]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
92
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
93
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
94 opt = []
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
95 rc = ''
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
96 lines = []
27
d9862a9a4d84 Uploaded micropita_format_input_Selector.py - fixed spaces tabs inconsistencies
george-weingart
parents: 0
diff changeset
97 try:
0
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
98 lines = [(red((rc+v.split()[0]),max_len),'%d' % (i+1),False) for i,v in enumerate(table_lines) if v]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
99
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
100 except:
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
101 l1 = '*ALL*'
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
102 l2 = 1
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
103 l3 = False
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
104 MyList = [l1,l2,l3]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
105 lines.append(MyList)
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
106 return opt+lines
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
107
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
108 def get_cols_features(data,full_names,lastmeta):
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
109 if data == "": return []
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
110 display_from = 1
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
111 try:
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
112 display_from = int(lastmeta)
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
113 except:
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
114 pass
27
d9862a9a4d84 Uploaded micropita_format_input_Selector.py - fixed spaces tabs inconsistencies
george-weingart
parents: 0
diff changeset
115 fname = data.dataset.file_name
0
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
116 input_file = open(fname,'rU')
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
117
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
118 input_lines = input_file.readlines()
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
119 input_file.close()
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
120 table_lines = []
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
121 for x in input_lines:
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
122 first_column = x.split('\t')[0]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
123 table_lines.append(first_column)
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
124
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
125 opt = []
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
126 rc = ''
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
127 del table_lines[:display_from]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
128 lines = []
27
d9862a9a4d84 Uploaded micropita_format_input_Selector.py - fixed spaces tabs inconsistencies
george-weingart
parents: 0
diff changeset
129 try:
0
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
130 lines = [(red((rc+v.split()[0]),max_len),'%d' % (i+1),False) for i,v in enumerate(table_lines) if v]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
131
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
132 except:
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
133 l1 = '*ALL*'
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
134 l2 = 1
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
135 l3 = False
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
136 MyList = [l1,l2,l3]
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
137 lines.append(MyList)
d589875b8125 First version of micropita in this repository
george-weingart
parents:
diff changeset
138 return opt+lines