-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringHelper.py
35 lines (31 loc) · 1.07 KB
/
stringHelper.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
import preferences as p
def strip_song_message(message):
tokens = message.split(" ");
i = 0
t = len(tokens)
while i < t and not is_a_song(tokens[i]):
i = i + 1
if i < t:
return tokens[i]
return False
def is_a_song(message):
for a in p.stringBank.songMatchTokens:
if(a in message):
return True
def trim_comments_and_newlines(filename):
trimmed_lines = []
f = open(filename,'r')
for line in f:
if not line.startswith("#"):
trimmed_lines.append(line[:-1]) #Last character of a line is always \n, so we remove it
return trimmed_lines
#Parse a text file into an array of lines, removing comments and optionally filtering on an ending
def parse_pairings_file(filename,end_code):
pairs = [];
f = open(filename,'r')
for line in f:
tokens = line.split(':',1)
if not tokens[0].startswith("#") and len(tokens) == 2 and tokens[0].endswith(end_code):
if(tokens[1].endswith("\n")): tokens[1] = tokens[1][:-1]
pairs.append(tokens)
return pairs;