-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicMethods.py
More file actions
193 lines (151 loc) · 5.19 KB
/
BasicMethods.py
File metadata and controls
193 lines (151 loc) · 5.19 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
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
191
192
def isInt(value):
try:
int(value)
return True
except ValueError:
return False
def isfloat(value):
"""
Checks if the given value represent float
:param value:
:return: True if float
"""
try:
float(value)
return True
except:
return False
def getWriteOnly(file_name):
"""
Get the correct openning flag for files for write only.
:param file_name: the file name including the end.
:return: If the file ends with '.txt' will return "w", if its '.bin' return "wb"
"""
filename = ""
filename += file_name
splittedFileName = filename.split(".")
if splittedFileName[len(splittedFileName)-1] == "txt":
return "w"
elif splittedFileName[len(splittedFileName)-1] == "bin":
return "wb"
def getReadOnly(file_name):
"""
Get the correct openning flag for files for read only.
:param file_name: the file name including the end.
:return: If the file ends with '.txt' will return "r", if its '.bin' return "rb"
"""
filename = ""
filename += file_name
splittedFileName = filename.split(".")
if splittedFileName[len(splittedFileName)-1] == "txt":
return "r"
elif splittedFileName[len(splittedFileName)-1] == "bin":
return "rb"
def getAppendingOnly(file_name):
"""
Get the correct openning flag for files for appending only.
:param file_name: the file name including the end.
:return: If the file ends with '.txt' will return "a", if its '.bin' return "ab"
"""
filename = ""
filename += file_name
splittedFileName = filename.split(".")
if splittedFileName[len(splittedFileName)-1] == "txt":
return "a"
elif splittedFileName[len(splittedFileName)-1] == "bin":
return "ab"
def getWritePlusOnly(file_name):
"""
Get the correct openning flag for files for write and read only.
:param file_name: the file name including the end.
:return: If the file ends with '.txt' will return "w+", if its '.bin' return "wb+"
"""
filename = ""
filename += file_name
splittedFileName = filename.split(".")
if splittedFileName[len(splittedFileName)-1] == "txt":
return "w+"
elif splittedFileName[len(splittedFileName)-1] == "bin":
return "wb+"
def getReadPlusOnly(file_name):
"""
Get the correct openning flag for files for write and read only.
:param file_name: the file name including the end.
:return: If the file ends with '.txt' will return "r+", if its '.bin' return "rb+"
"""
filename = ""
filename += file_name
splittedFileName = filename.split(".")
if splittedFileName[len(splittedFileName)-1] == "txt":
return "r+"
elif splittedFileName[len(splittedFileName)-1] == "bin":
return "rb+"
def getAppendingPlusOnly(file_name):
"""
Get the correct openning flag for files for appending plus.
:param file_name: the file name including the end.
:return: If the file ends with '.txt' will return "a+", if its '.bin' return "ab+"
"""
filename = ""
filename += file_name
splittedFileName = filename.split(".")
if splittedFileName[len(splittedFileName)-1] == "txt":
return "a+"
elif splittedFileName[len(splittedFileName)-1] == "bin":
return "ab+"
def getStringSizeInBytes(string):
# String size in bytes
return len(string.encode('utf-8'))
def writeListToFile(path,fileName,listToWrite,useNewLine = True):
import os
if not os.path.exists(path) or len(listToWrite) == 0:
return
try:
myFile = open(path + '/' + fileName,'a')
for line in listToWrite:
if useNewLine:
myFile.write('|'.join(line) + '\n')
else: myFile.write('|'.join(line))
myFile.close()
except Exception as ex:
print(ex)
def get2DArrayFromFile(path, sep = '|'):
# returns a 2D array from file by sep
try:
myFile = open(path,'r')
with myFile:
lines = myFile.readlines()
myFile.close()
twoDArray = []
for line in lines:
line = line.rstrip('\n')
lineAsArray = line.split(sep)
twoDArray.append(lineAsArray)
return twoDArray
except Exception as ex:
print("Error while converting file to 2D array, E: ",ex)
def getDicFromFile(path, sep = '|'):
# returns a dic from file by sep
try:
myFile = open(path,'r')
with myFile:
lines = myFile.readlines()
myFile.close()
myDict = {}
for line in lines:
lineAsArray = line.split(sep)
myDict[lineAsArray[0]] = lineAsArray[1:]
return myDict
except Exception as ex:
print("Error while converting file to 2D array, E: ",ex)
def getTagFromText(textAsString,tag1,tag2='\n'):
find = textAsString.find(tag1)
if find == -1:
return ''
start = find + len(tag1)
end = textAsString[start:].find(tag2)
content = textAsString[start:start+end]
return content.strip(' ')
def getStringFormatForFloatValue(numOfDigits,valueAsFloat):
formatString = "{0:.%sf}" % (str(numOfDigits))
return str(formatString.format(round(valueAsFloat, numOfDigits)))