-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.py
190 lines (137 loc) · 5.64 KB
/
Utilities.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""
**************************************************************************
| Utilities.py |
**************************************************************************
| Description: |
| |
| Contains support functions for the codebase. This code runs on python |
| 2.4 or later. |
**************************************************************************
| Author: Rob Lyon |
| Email : [email protected] |
| web : www.scienceguyrob.com |
**************************************************************************
"""
# Python 2.4 imports.
import traceback
import sys
import os
# ******************************************************************************************
#
# CLASS DEFINITION
#
# ******************************************************************************************
class Utilities(object):
"""
Provides utility functions used when computing features.
"""
# ******************************************************************************************
#
# Constructor.
#
# ******************************************************************************************
def __init__(self, debugFlag):
self.debug = debugFlag
# ******************************************************************************************
#
# Functions.
#
# ******************************************************************************************
def appendToFile(self, path, text):
"""
Appends the provided text to the file at the specified path.
Parameters:
path - the path to the file to append text to.
text - the text to append to the file.
Returns:
N/A
"""
destinationFile = open(path,'a')
destinationFile.write(str(text))
destinationFile.close()
# ******************************************************************************************
def clearFile(self, path):
"""
Clears the file at the specified path.
Parameters:
path - the path to the file to append text to.
Returns:
N/A
"""
open(path, 'w').close()
# ******************************************************************************************
def fileExists(self,path):
"""
Checks a file exists, returns true if it does, else false.
Parameters:
path - the path to the file to look for.
Returns:
True if the file exists, else false.
"""
try:
fh = open(path)
fh.close()
return True
except IOError:
return False
# ******************************************************************************************
def dirExists(self,path):
"""
Checks a directory exists, returns true if it does, else false.
Parameters:
path - the path to the directory to look for.
Returns:
True if the file exists, else false.
"""
try:
if(os.path.isdir(path)):
return True
else:
return False
except IOError:
return False
# ******************************************************************************************
def format_exception(self,e):
"""
Formats error messages.
Parameters:
e - the exception.
Returns:
The formatted exception string.
"""
exception_list = traceback.format_stack()
exception_list = exception_list[:-2]
exception_list.extend(traceback.format_tb(sys.exc_info()[2]))
exception_list.extend(traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1]))
exception_str = "\nTraceback (most recent call last):\n"
exception_str += "".join(exception_list)
# Removing the last \n
exception_str = exception_str[:-1]
return exception_str
# ******************************************************************************************
def out(self,message,parameter):
"""
Writes a debug statement out if the debug flag is set to true.
Parameters:
message - the string message to write out
parameter - an accompanying parameter to write out.
Returns:
N/A
"""
if(self.debug):
print(message , parameter)
# ******************************************************************************************
def outMutiple(self,parameters):
"""
Writes a debug statement out if the debug flag is set to true.
Parameters:
parameters - the values to write out.
Returns:
N/A
"""
if(self.debug):
output =""
for p in parameters:
output+=str(p)
print(output)
# ******************************************************************************************