-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal_helpers.py
65 lines (53 loc) · 1.73 KB
/
global_helpers.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
# Library to exit code when error occurs
import sys
def error(msg, line_num):
"""
Shows error message in red color and exits the current process
Params
======
msg (string) = The message to be shown as error message
line_num (int) = Line number
"""
# Prints the error to screen in red color and then exits tokenizer
print("\033[91m[Line %d] Error: %s" % (line_num, msg))
sys.exit()
def is_digit(char):
"""
Checks if character is digit or not
Params
======
char (string) = Single character mostly part of a longer string
Returns
=======
bool: Checks whether the character is number or not, since '.' is not considered an alphabet by the
standard isdigit function
"""
return char in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."]
def is_alpha(char):
"""
Checks if character is alphabet or not
Params
======
char (string) = Single character mostly part of a longer string
Returns
=======
bool: Checks whether the character is alphabet or not, since '_' is not considered a digit by the
standard isalpha function
"""
return char.isalpha() or char == "_"
def is_alnum(char):
"""
Checks if character is alphabet or digit or none
Params
======
char (string) = Single character mostly part of a longer string
Returns
=======
bool: Checks whether the character is alphabet/digit not, since '_' is not considered an alphabet by the
isalpha function, and '.' is not considered a digit by the standard isdigit function
"""
return (
char.isalpha()
or char == "_"
or char in ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."]
)