-
Notifications
You must be signed in to change notification settings - Fork 1
/
dash_utils.py
176 lines (98 loc) · 3.77 KB
/
dash_utils.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
Created on Tue Nov 28 20:59:55 2017
@author: abhiram haridas (abhiramharidas@gmail.com)
"""
"""
This file is imported in dashboard.py. This provides a set of utility functions used for input verification by the
main program. When run as a standalone program this serves as linebreak remover tool which takes a file name as input
and returns the file content without line breaks as output. This can be used while inputting HTML field values to the
console application.
Functions generally return two values - error_flag and error_msg. error_flag will be set if there is an error and corresponding error
message will be contained in error_msg
"""
import os.path
#================================================== HELPER FUNCTIONS FOR INPUT VALIDATION ====================================================
def file_exist(file_name):
error_flag = 0
error_msg = ""
if (not os.path.isfile(file_name)):
error_flag = 1
error_msg = "\nError! The specified file does not exist\n"
return (error_flag,error_msg)
def check_date(date_string):
error_flag = 0
error_msg = ""
dates = date_string.split('/');
if(len(dates) != 3):
error_flag = 1;
error_msg = "\nError! Invalid date string. Please follow the specified format.\n"
return (error_flag, error_msg)
t = dates[0]; # validating day
try:
t = int(t)
except ValueError:
error_flag = 1;
error_msg = "\nError! Invalid day in date string provided.\n"
return (error_flag, error_msg)
if(t < 1 or t > 31):
error_flag = 1;
error_msg = "\nError! Invalid day in date string provided.\n"
return (error_flag, error_msg)
t = dates[1] # validating month
try:
t = int(t)
except ValueError:
error_flag = 1;
error_msg = "\nError! Invalid month in date string provided.\n"
return (error_flag, error_msg)
if(t < 1 or t > 12):
error_flag = 1;
error_msg = "\nError! Invalid month in date string provided.\n"
return (error_flag,error_msg)
t = dates[2] # validating year
try:
t = int(t)
except ValueError:
error_flag = 1;
error_msg = "\nError! Invalid month in date string provided.\n"
return (error_flag, error_msg)
if(t < 1997 or t > 2100): # :) :P
error_flag = 1;
error_msg = "\nError! Invalid month in date string provided.\n"
return (error_flag, error_msg)
return (error_flag, error_msg) # valid date
def check_year(year_str):
error_flag = 0
error_msg = ""
try:
year = int(year_str)
except ValueError:
error_flag = 1
error_msg = "\nError! Invalid year provided.\n"
return (error_flag, error_msg)
if (year < 1997 or year > 2100):
error_flag = 1;
error_msg = "\nError! Invalid month in date string provided.\n"
return (error_flag, error_msg)
def check_name(name_str):
error_flag = 0
error_msg = ""
name = name_str.replace(' ','')
if not name.isalnum():
error_flag = 1
error_msg = "\nError! Name/title should have alphabets and spaces only.\n"
return (error_flag, error_msg)
#========================================== Functions executed when executed as main file ========================================
def remove_linebreaks(file_name):
error_flag, error_msg = file_exist(file_name)
if error_flag == 1:
print error_msg
return None
with open(file_name) as myfile:
data = myfile.read()
return data.replace('\n','').replace('\r','')
if __name__ == '__main__':
file_name = raw_input("Enter path to file : ")
data_str = remove_linebreaks(file_name)
if data_str is not None:
print data_str