Skip to content

Commit 196104e

Browse files
committed
refactoring example, to create readable reusable code
1 parent f4ce6c7 commit 196104e

12 files changed

+187
-12
lines changed

function_pointers/switch_alternative.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ int main(){
2929

3030
std::cout << "an example of using fuction pointers in an array, as order of execution" << std::endl;
3131
int ins[] = {1, 1, 2, 0, 1};
32-
for(int i=0; i<sizeof(ins)/sizeof(ins[0]); i++)
33-
(*p[ ins[i] ])(i);
32+
for(int i=0; i<sizeof(ins)/sizeof(ins[0]); i++) (*p[ ins[i] ])(i);
3433

3534
//declare a map (hash) of 3 function pointers that take int as argument. C++ as std::function which would be a cleaner more c++ approach for prototyping def.
3635
std::map<std::string, void (*)(int)> t;

modulo_2_must_go/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
CC = g++ # compiler to use
88

99
LINKERFLAG =
10-
CXXFLAGS = -g -O3 -std=c++17
10+
CXXFLAGS = -O3 -g -std=c++17
1111

1212
SRCS := $(wildcard *.cpp)
1313
BINS := $(SRCS:%.cpp=%)

modulo_2_must_go/fast_odd_even.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void moduloMethod(const int ITS) {
2525
temp++;
2626
}
2727
}
28+
2829
std::cout << temp << endl;
2930
}
3031

@@ -45,19 +46,19 @@ int main() {
4546
vector<long> aC;
4647
vector<long> bC;
4748

48-
std::chrono::time_point<std::chrono::system_clock> startTime;
49-
std::chrono::time_point<std::chrono::system_clock> endTime;
49+
std::chrono::time_point<std::chrono::system_clock> sTime;
50+
std::chrono::time_point<std::chrono::system_clock> eTime;
5051

5152
for (int i = 0; i < 100; i++) {
52-
startTime = high_resolution_clock::now();
53+
sTime = high_resolution_clock::now();
5354
andMethod(ITS);
54-
endTime = high_resolution_clock::now();
55-
aC.push_back(duration_cast<microseconds>(endTime - startTime).count());
55+
eTime = high_resolution_clock::now();
56+
aC.push_back(duration_cast<microseconds>(eTime - sTime).count());
5657

57-
startTime = high_resolution_clock::now();
58+
sTime = high_resolution_clock::now();
5859
moduloMethod(ITS);
59-
endTime = high_resolution_clock::now();
60-
bC.push_back(duration_cast<microseconds>(endTime - startTime).count());
60+
eTime = high_resolution_clock::now();
61+
bC.push_back(duration_cast<microseconds>(eTime - sTime).count());
6162
}
6263

6364
float aCA = accumulate(aC.begin(), aC.end(), 0.0) / aC.size();

modulo_2_must_go/fast_odd_even.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def Average(lst):
2828

2929
ITS = 10000000
3030

31-
for i in range(0, 3):
31+
for i in range(0, 100):
3232
start = time.time_ns()
3333
andMethod(ITS)
3434
stop = time.time_ns()
12 KB
Binary file not shown.

reusable_code/okay.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hi write me to the file please

reusable_code/write_to_file.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
import os
3+
4+
def writeDataToFile(fileName, data):
5+
try:
6+
f = open(fileName, "a+")
7+
f.write(data)
8+
f.close()
9+
except IOError:
10+
raise IOError
11+
12+
#Main program
13+
data = "Hi write me to the file please"
14+
okayFile = "okay.txt"
15+
16+
try:
17+
writeDataToFile(okayFile, data)
18+
except IOError as e:
19+
print("Error %s occured, program halted" % {e.strerror})
20+
exit(1)
21+

reusable_code/write_to_file_v1.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python3
2+
import os
3+
4+
def writeDataToFile(fileName, data):
5+
try:
6+
f = open(fileName, "w+")
7+
f.write(data)
8+
except IOError:
9+
raise IOError
10+
finally:
11+
f.close()
12+
13+
#Main program
14+
data = "Hi write me to the file please"
15+
okayFile = "okay.txt"
16+
17+
try:
18+
writeDataToFile(okayFile, data)
19+
except IOError as e:
20+
print("Error %s occured, program halted" % {e.strerror})
21+
exit(1)
22+

reusable_code/write_to_file_v2.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
import os
3+
4+
def writeDataToFile(fileName, data):
5+
try:
6+
f = open(fileName, "w+")
7+
if f is None:
8+
raise TypeError
9+
f.write(data)
10+
except IOError:
11+
raise IOError
12+
finally:
13+
f.close()
14+
15+
#Main program
16+
data = "Hi write me to the file please"
17+
okayFile = "okay.txt"
18+
19+
try:
20+
writeDataToFile(okayFile, data)
21+
except IOError as e:
22+
print("Error %s occured, program halted" % {e.strerror})
23+
exit(1)
24+

reusable_code/write_to_file_v3.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3
2+
import os
3+
4+
def ThrowErrorIfObjectIsNull(obj):
5+
if not obj:
6+
raise TypeError("Object is set to Null")
7+
8+
def writeDataToFile(fileName, data):
9+
try:
10+
f = open(fileName, "w+")
11+
f = None
12+
ThrowErrorIfObjectIsNull(f)
13+
f.write(data)
14+
f.close()
15+
except Exception as e:
16+
raise e
17+
18+
#Main program
19+
data = "Hi write me to the file please"
20+
okayFile = "okay.txt"
21+
22+
try:
23+
writeDataToFile(okayFile, data)
24+
except Exception as e:
25+
print("Uncaught exception %s" % (e))
26+
exit(1)
27+

reusable_code/write_to_file_v4.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python3
2+
import os
3+
4+
def ThrowErrorIfObjectIsNull(obj):
5+
if not obj:
6+
raise TypeError("Object is set to null")
7+
8+
def writeToFileAndClose(fileDesc, data):
9+
ThrowErrorIfObjectIsNull(fileDesc)
10+
fileDesc.write(data)
11+
fileDesc.close()
12+
13+
def writeDataToFile(fileName, data):
14+
try:
15+
f = open(fileName, "w+")
16+
writeToFileAndClose(f, data)
17+
except IOError as e:
18+
raise e
19+
20+
#Main program
21+
data = "Hi write me to the file please"
22+
okayFile = "okay.txt"
23+
24+
try:
25+
writeDataToFile(okayFile, data)
26+
except Exception as e:
27+
print("Uncaught exception %s" % (e))
28+
exit(1)
29+

reusable_code/write_to_file_v5.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python3
2+
import os
3+
4+
class ErrorCheck:
5+
@staticmethod
6+
def ThrowTypeErrorIfObjectIsNull(obj):
7+
if not obj:
8+
raise TypeError("ErrorCheck object is set to null ")
9+
10+
class File:
11+
@staticmethod
12+
def __writeDataToFileObjectAndClose(fileDesc, data):
13+
ErrorCheck.ThrowTypeErrorIfObjectIsNull(fileDesc)
14+
fileDesc.write(data)
15+
fileDesc.close()
16+
17+
@staticmethod
18+
def writeText(fileName, text, mode):
19+
try:
20+
f = open(fileName, mode)
21+
ErrorCheck.ThrowTypeErrorIfObjectIsNull(f)
22+
File.__writeDataToFileObjectAndClose(f, text)
23+
except TypeError as e:
24+
raise TypeError("the file %s could not be open, fileObject is None" % (fileName) )
25+
except IOError as e:
26+
raise e
27+
28+
@staticmethod
29+
def createFileOrAppendWithText(fileName, text):
30+
try:
31+
File.writeText(fileName,text, "a+")
32+
except IOError as e:
33+
raise e
34+
35+
@staticmethod
36+
def createFileWithText(fileName, text):
37+
try:
38+
File.writeText(fileName,text, "w+")
39+
except IOError as e:
40+
raise e
41+
42+
#Main program
43+
textToSave = "Hi write me to the file please\n"
44+
okayFile = "okay.txt"
45+
46+
try:
47+
File.createFileOrAppendWithText(okayFile, textToSave)
48+
except Exception as e:
49+
print("Uncaught exception %s" % (e))
50+
exit(1)
51+

0 commit comments

Comments
 (0)