Repository navigation
Expand file tree
/
Copy pathCustomStringOps.py
More file actions
executable file
·333 lines (293 loc) · 8.28 KB
/
Copy pathCustomStringOps.py
File metadata and controls
executable file
·333 lines (293 loc) · 8.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env python
"""General String Operations
"""
#####################
# IMPORT OPERATIONS #
#####################
from __future__ import absolute_import
from __future__ import print_function
from six.moves import range
from six.moves import zip
from functools import reduce
import random
import re
import string
import sys
###############
# AUTHOR INFO #
###############
__author__ = "Michael Gruenstaeudl, PhD"
__copyright__ = "Copyright (C) 2015 Michael Gruenstaeudl"
__email__ = "mi.gruenstaeudl@gmail.com"
__version__ = "2015.12.15.1100"
#############
# DEBUGGING #
#############
#import pdb
#pdb.set_trace()
###########
# CLASSES #
###########
class afterFind:
'''
Args:
string <a>, string <b>
Returns:
integer
'''
def __init__(self, a, b):
self.inStr = a
self.kw = b
def go(self):
pos1 = self.inStr.find(self.kw) + len(self.kw)
return pos1
class clearSplit:
''' Splits string without removing delim.
Args:
inStr <a>, delim <b>, rflag <c>
Returns:
outlist
'''
def __init__(self, a, b, c):
self.inStr = a
self.delim = b
self.rflag = c
def go(self):
splitL = self.inStr.split(self.delim)
if len(splitL) < 2:
print(" ERROR: ", self.__class__.__name__)
print(" Less than two elements after split.")
outlist = []
if len(splitL) == 2:
if not self.rflag:
outlist = [splitL[0]+self.delim, splitL[1]]
if self.rflag:
outlist = [splitL[0], self.delim+splitL[1]]
else: # Do NOT comment out the else statement, which is important for ASR_Mesquite.py
delim_list = list(numpy.repeat(numpy.array("<"), len(splitL)))
for a, b in zip(delim_list, splitL):
outlist.append(a+b)
return outlist
class exciseStr:
''' Excises string via kw delimitation.
Args:
inStr <a>, kw1 <b>, kw2 <c>
Returns:
outstring
'''
def __init__(self, a, b, c):
self.inStr = a
self.kw1 = b
self.kw2 = c
def afterKw(self):
pos1 = afterFind(self.inStr, self.kw1).go()
pos2 = self.inStr.find(self.kw2, pos1)
return self.inStr[pos1:pos2]
def keepKw1(self):
pos1 = self.inStr.find(self.kw1)
pos2 = self.inStr.find(self.kw2, pos1)
return self.inStr[pos1:pos2]
class findAll:
''' class to find all instances of a substring in string
Args:
substring <a>, string <b>
Returns:
list
'''
def __init__(self, a, b):
self.substring = a
self.astring = b
def go(self):
return [elem.start() for elem in re.finditer(self.substring, self.astring)]
class isEven:
''' Checking if number is even.
Args:
integer <a>
Returns:
boolean
'''
def __init__(self, a):
self.inInt = a
def go(self):
return self.inInt % 2 == 0
class makePrettyTable:
''' Generate a table from an array
Args:
array <a>
Returns:
string
'''
def __init__(self, a):
self.inArray = a
def go(self):
longest_str = max(self.inArray[:,0], key=len)
outStr = ""
for row in self.inArray:
whitespaces = len(longest_str)-len(row[0])
outStr += row[0].ljust(whitespaces) + " " + row[1] + "\n"
return outStr
class makePairwise:
''' Converts a list into pairs of two.
Args:
list <a>
Returns:
list: list of pairs
'''
def __init__(self, a):
self.inL = a
def go(self):
from itertools import tee, izip
a, b = tee(self.inL)
next(b, None)
return izip(a, b)
class randomWord:
''' Generate a random word of defined length
Args:
integer <a>
Returns:
string
'''
def __init__(self, a):
self.inInt = a
def go(self):
return ''.join(random.choice(string.lowercase) for i in range(self.inInt))
class removeExt:
''' Returns inStr without extension (i.e. ".txt" or ".trees").
Args:
string <a>
Returns:
string
'''
def __init__(self, a):
self.inStr = a
def go(self):
sep = "."
return self.inStr[:self.inStr.rfind(sep)]
class removePath:
''' Returns inStr without the prepending file path.
Args:
string <a>
Returns:
string
'''
def __init__(self, a):
self.inStr = a
def go(self):
if sys.platform == "linux" or sys.platform == "linux2":
sep = "/"
elif sys.platform == "darwin":
sep = "/"
elif sys.platform == "win32":
sep = "\\"
return self.inStr[self.inStr.rfind(sep)+len(sep):]
class replaceStr:
''' Replaces a string via kw delimitation.
Args:
inStr <a>, kw1 <b>, kw2 <c>, replc <d>
Returns:
original string containing replc(s)
Note:
currently requires that kws are only found once;
otherwise use TFL:
return self.inStr.replace(self.inStr[pos1:pos2],self.replc)
'''
def __init__(self, a, b, c, d):
self.inStr = a
self.kw1 = b
self.kw2 = c
self.replc = d
def go(self):
pos1 = afterFind(self.inStr, self.kw1).go()
pos2 = self.inStr.find(self.kw2, pos1)
return self.inStr[:pos1] + self.replc + self.inStr[pos2:]
class rmBlankLns:
''' Removes blank lines from file object
Args:
file object <a>
Returns:
string
'''
def __init__(self, a):
self.inFile = a
def go(self):
aList = self.inFile.splitlines()
aList = [_f for _f in aList if _f]
return "\n".join(aList)
def go2(self): # Faster alternative that go()
import os
aList = self.inFile.splitlines()
return os.linesep.join([s for s in aList if s])
class splitKeepSep:
''' Split a string by separator, but keeps separator
Args:
string <a>
Returns:
list
'''
def __init__(self, a, b):
self.inStr = a
self.sep = b
def go(self):
outL = reduce(lambda acc, elem: acc +
[elem] if elem == self.sep else acc[:-1] +
[acc[-1] + elem],
re.split("(%s)" % re.escape(self.sep), self.inStr)[1:], [])
return outL
class splitKeepSep2: # Problem: Difficult to know, if separator should be attached to left or right of element; currently to right
''' Split a string by separator, but keeps separator
Args:
string <a>
Returns:
list
'''
def __init__(self, a, b):
self.inStr = a
self.sep = b
def go(self):
return [e + self.sep for e in self.inStr.split(self.sep) if e != ""]
class sublistInList:
''' Checks is sublist is in list
Args:
list <a>, list <b>
Returns:
list
'''
def __init__(self, a, b):
self.alist = a
self.sublist = a
def go(self):
n = len(self.sublist)
return any((self.sublist == self.alist[i:i+n]) for i in range(len(self.alist)-n+1))
###############
# DEFINITIONS #
###############
def afind(inStr, kw):
return afterFind(inStr, kw).go()
def csplit(inStr, delim, rflag=False):
return clearSplit(inStr, delim, rflag).go()
def findall(substring, astring):
return findAll(substring, astring).go()
def exstr(inStr, kw1, kw2):
return exciseStr(inStr, kw1, kw2).afterKw()
def exstrkeepkw(inStr, kw1, kw2):
return exciseStr(inStr, kw1, kw2).keepKw1()
def iseven(inInt):
return isEven(inInt).go()
def makeprettytable(inArray):
return makePrettyTable(inArray).go()
def randomword(length):
return randomWord(length).go()
def replstr(inStr, kw1, kw2, replc):
return replaceStr(inStr, kw1, kw2, replc).go()
def rmblanklns(inFile):
return rmBlankLns(inFile).go()
def rmext(inStr):
return removeExt(inStr).go()
def rmpath(inStr):
return removePath(inStr).go()
def splitkeepsep(inStr, sep):
return splitKeepSep(inStr, sep).go()
def splitkeepsep2(inStr, sep):
return splitKeepSep2(inStr, sep).go()
def sublistinlist(alist, sublist):
return sublistInList(alist, sublist).go()