-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselector.py
More file actions
30 lines (23 loc) · 860 Bytes
/
selector.py
File metadata and controls
30 lines (23 loc) · 860 Bytes
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
import re
from settings import SELECT_PATTERN
class RegexSelector:
def __init__(self, pattern):
self.compiled_pattern = re.compile(pattern)
def select(self, text):
matches = self.compiled_pattern.findall(text)
result = []
for match in matches:
if isinstance(match, tuple|list):
for elem in match:
if elem: result.append(elem)
else:
result.append(match)
return result
def select_from_file(self, path, encoding="ascii"):
with open(path, 'r') as f:
text = f.read()
return self.select(text)
class MissionEnigmaSelector(RegexSelector):
def __init__(self, *args, **kwargs):
super().__init__(SELECT_PATTERN, *args, **kwargs)
self.compiled_pattern = re.compile(SELECT_PATTERN)