-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser.py
More file actions
46 lines (39 loc) · 1.28 KB
/
parser.py
File metadata and controls
46 lines (39 loc) · 1.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
"""
Module with different parsers.
- List, the element is recovered in position
- Delimiter, it is split and element is recovered in position
- Regex retrieves the group x from the given regex
"""
import re
def parser_list(position):
"""
Returns function that retrieves the element in position `position`
:param position: position to return
:return: element
"""
return lambda x: x[position]
def parser_delimiter(delimiter, position):
"""
Separate the event with the separator and return the element in the position
position
:param delimiter: delimiter
:param position: position
:return: the element in the given position
"""
return lambda x: x.split(delimiter)[position]
def parser_regex(regex, group, ignorecase=False):
"""
Recovers the group from a regex and returns it
:param regex: regex
:param group: the group to recover
:param ignorecase: False by default
:return: the group obtained.
"""
if group == 0:
if ignorecase:
return lambda x: re.search(regex, x, re.I).groups()[group]
return lambda x: re.search(regex, x).groups()[group]
else:
if ignorecase:
return lambda x: re.findall(regex, x, re.I)[group]
return lambda x: re.findall(regex, x)[group]