-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
67 lines (58 loc) · 1.93 KB
/
util.py
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
# -*- coding: utf-8 -*-
import re
import logging
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
def makeArray2D(data_list, length=2):
return [data_list[i:i+length] for i in range(0, len(data_list), length)]
def distributeElementMaxSize(seq, maxSize=5):
lines = len(seq) / maxSize
if len(seq) % maxSize > 0:
lines += 1
avg = len(seq) / float(lines)
out = []
last = 0.0
while last < len(seq):
out.append(seq[int(last):int(last + avg)])
last += avg
return out
def segmentArrayOnMaxChars(array, maxChar=20, ignoreString=None):
#logging.debug('selected_tokens: ' + str(selected_tokens))
result = []
lineCharCount = 0
currentLine = []
for t in array:
t_strip = t.replace(ignoreString, '') if ignoreString and ignoreString in t else t
t_strip_size = len(t_strip.decode('utf-8'))
newLineCharCount = lineCharCount + t_strip_size
if not currentLine:
currentLine.append(t)
lineCharCount = newLineCharCount
elif newLineCharCount > maxChar:
#logging.debug('Line ' + str(len(result)+1) + " " + str(currentLine) + " tot char: " + str(lineCharCount))
result.append(currentLine)
currentLine = [t]
lineCharCount = t_strip_size
else:
lineCharCount = newLineCharCount
currentLine.append(t)
if currentLine:
#logging.debug('Line ' + str(len(result) + 1) + " " + str(currentLine) + " tot char: " + str(lineCharCount))
result.append(currentLine)
return result
reSplitSpace = re.compile("\s")
def splitTextOnSpaces(text):
return reSplitSpace.split(text)
def escapeMarkdown(text):
for char in '*_`[':
text = text.replace(char, '\\'+char)
return text
def containsMarkdown(text):
for char in '*_`[':
if char in text:
return True
return False