-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpython-classes.py
156 lines (132 loc) · 4.09 KB
/
python-classes.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
print(
'-----------------------------------------\n'\
'Python Education || Documentation - Classes\n'\
'-----------------------------------------\n'
)
print(
'Task:\n'\
'-----------------------------------------\n'\
'Write a Python program to test all available functionalities and code samples from the official documentation.\n'
)
print(
'Solution:\n'\
'-----------------------------------------'\
)
import doctest
import unittest
#Default function for handling execution loop:
def execution_loop():
data = int(input("Do you want to try again ? Enter [1] - for continue / [0] - for quit :"))
if data == 1:
return True
elif data == 0:
return False
else:
print("Error: your entered incorrect command. Please, try again...")
execution_loop()
#Function for testing definition of function outside of the class:
def fl(self, x, y):
return min(x, x+y)
class C:
f = fl
def g(self):
return 'I\'m a function "g".'
h = g
#Initializing a new Reverse class:
class Reverse:
def __init__(self, data):
self.data = data
self.index = len(data)
def __iter__(self):
return self
def __next__(self):
if self.index == 0:
raise StopIteration
self.index = self.index - 1
return self.data[self.index]
#Initializing a new test statistical function for unit testing of the code:
class TestStatisticalFunctions(unittest.TestCase):
def test_average(self):
self.assertEqual(average([20, 30, 70]), 40.0)
self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
with self.assertRaises(ZeroDivisionError):
average([])
with self.assertRaises(TypeError):
average(20, 30, 70)
#Function for testing iterators:
def iterators_func():
print('Iterators function was called:')
print('Testing for loop statement with iterators:')
for element in [1, 2, 3]:
print(element)
for element in (4, 5, 6):
print(element)
for key in {'one': 1, 'two': 2, 'three': 3}:
print(key)
for char in "789":
print(char)
for line in open("./python-classes.txt"):
print(line, end='')
print('\nLooping over a backward sequence:\n')
test_string = 'vr player'
it = iter(test_string)
print(it)
print(next(it))
print(next(it))
print(next(it))
print('\nDisplaying a backward sequence:\n')
rev = Reverse('spam')
iter(rev)
for char in rev:
print(char)
#Function for handling reverse displaying:
def handleReverseDisplay(data):
print('Using generators for displaying reverse data:')
for char in reverse(data):
print(char)
#Function for reverse displaying:
def reverse(data):
for index in range(len(data)-1, -1, -1):
yield data[index]
#Function for testing generator expressions:
def generator_expressions():
from math import pi, sin
print('Sum of squares:\n')
print(sum(i*i for i in range(10)))
xvec = [10, 20, 30]
yvec = [7, 5, 3]
print('Dot product:\n')
print(sum(x*y for x,y in zip(xvec, yvec)))
sine_table = { x: sin(x*pi/180) for x in range(0, 91) }
data = input("Enter your custom text for testing generator expressions:\n>>> ")
data_output = list(data[i] for i in range(len(data)-1, -1, -1))
print(data_output)
#Function for testing quality of the code using 'doctest' module:
def doctest_quality_control(values):
return sum(values) / len(values)
#Default parameter for handling execution loop:
again_exec = True
counter_exec = 0
#Default loop for handling execution:
while again_exec:
iterators_func()
user_input = input('Please, enter your custom text for testing reverse displaying:\n>>> ')
handleReverseDisplay(user_input)
generator_expressions()
print('The arithmetic mean of a list of numbers 20, 30 ,70: ')
doctest_quality_control([20, 30, 70])
#doctest the code:
doctest.testmod()
#unittest the code:
unittest.main()
again_exec = execution_loop()
counter_exec = counter_exec + 1
#The end of execution:
if again_exec == False:
print("Program was executed: ",counter_exec, ' times.')
break
print(
'\n-----------------------------------------\n'\
'Copyright 2019 Vladimir Pavlov. All Rights Reserved.\n'\
'-----------------------------------------'
)