Skip to content

Commit 889b800

Browse files
author
Andreas Kugel
committed
update
1 parent faabf5f commit 889b800

5 files changed

Lines changed: 150 additions & 60 deletions

File tree

src/1-basicData.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
import sys
66
import pygame
77
import time
8-
import os
98
import random
109
from mpl_toolkits.mplot3d import Axes3D
1110

1211

13-
#playing and plotting data from a wave file
12+
# playing and plotting data from a wave file
1413

1514
# pgame stuff related to playing sound
1615
pygame.mixer.pre_init(24000, size=-16, channels=1)
@@ -48,7 +47,7 @@
4847
signal = "dqpinfqfmmdqwpdqwpdqwodmopmodpwndeniqwqwnfojqnwd"
4948
print("Using ", signal)
5049

51-
txtSignal = signal # make a copy of the text
50+
txtSignal = signal # make a copy of the text
5251
signal = np.fromstring(signal.upper(), "uint8")
5352
sigMin = min(signal)
5453
signal -= min(signal)
@@ -67,7 +66,7 @@
6766
break
6867
max3d = min(len(signal), min3d + 200 )
6968
print("min ",min3d,", max ",max3d)
70-
69+
7170
#x3d = [i for i in range(min3d,max3d)]
7271
#y3d = [signal[i] % 10 for i in range(min3d,max3d)]
7372
#z3d = [signal[i] % 100 for i in range(min3d,max3d)]
@@ -85,7 +84,7 @@
8584

8685
for i in range(len(x3d)):
8786
ax.text(x3d[i],y3d[i],z3d[i], label3d[i], size=20)
88-
87+
8988
plt.axis("off")
9089
plt.title('A pile of numbers ...')
9190
plt.get_current_fig_manager().full_screen_toggle()
@@ -106,7 +105,7 @@
106105
plt.figure()
107106
plt.title('Sequential numbers: A Wave display/timeseries...')
108107
plt.plot(signal)
109-
plt.get_current_fig_manager().full_screen_toggle()
108+
plt.get_current_fig_manager().full_screen_toggle()
110109
plt.show()
111110

112111
# we can also create a 2d plot in the following way
@@ -116,7 +115,7 @@
116115
signal2d = np.reshape(signal, (-1, int(ncols))) # reshape
117116
plt.imshow(signal2d) # create image
118117
plt.title('2D arranged numbers: An Image ...')
119-
plt.get_current_fig_manager().full_screen_toggle()
118+
plt.get_current_fig_manager().full_screen_toggle()
120119
plt.show()
121120

122121
# recalculate 3d PARMS
@@ -138,21 +137,21 @@
138137
ha.plot_surface(x,y, signal2d, color='b')
139138

140139
plt.title('3D-stlye arranged numbers: A Surface Plot ...')
141-
plt.get_current_fig_manager().full_screen_toggle()
140+
plt.get_current_fig_manager().full_screen_toggle()
142141
plt.show()
143142

144143

145144
##### text display if not wav
146145
if not useWav:
147146
label3d = ["%c" % (signal[i]+sigMin) for i in range(min3d,max3d)]
148-
pntSize = 0
147+
pntSize = 0
149148
fig = plt.figure()
150149
ax = Axes3D(fig)
151150
ax.scatter(x3d,y3d,z3d, s= pntSize)
152151

153152
for i in range(len(x3d)):
154153
ax.text(x3d[i],y3d[i],z3d[i], label3d[i], size=20)
155-
154+
156155
plt.axis("off")
157156
plt.title('A pile of numbers with encoding: characters ...')
158157
plt.get_current_fig_manager().full_screen_toggle()
@@ -203,7 +202,7 @@
203202
sd = pygame.mixer.Sound(longSignal[:sl])
204203
sd.play()
205204

206-
#important to wait until sound finished
205+
#important to wait until sound finished
207206
while pygame.mixer.get_busy():
208207
pass
209208

@@ -212,7 +211,7 @@
212211

213212
##########################################
214213
# initialize speech output
215-
#for windows speech, we need win32 stuff
214+
#for windows speech, we need win32 stuff
216215
import platform
217216
if "windows" in platform.system().lower():
218217
import win32api
@@ -246,15 +245,15 @@
246245
plt.title('Ottos Mops hops ...')
247246

248247
if not useWav:
249-
tfig = plt.subplot(1,3,2)
248+
tfig = plt.subplot(1,3,2)
250249
tfig.axis("off")
251250
tfig.annotate(txtSignal,\
252251
xy=(1, 1), xytext=(.4,.8), xycoords="data", \
253252
textcoords="axes fraction",ha="left", va="top", size=14)
254253

255254
import wordcloud
256255
wc = wordcloud.WordCloud().generate_from_text(txtSignal)
257-
wfig = plt.subplot(1,3,3)
256+
wfig = plt.subplot(1,3,3)
258257
wfig.axis("off")
259258
wfig.imshow(wc)
260259

@@ -276,5 +275,4 @@
276275
if i < len(lines):
277276
engine.say(lines[i])
278277
engine.runAndWait()
279-
plt.pause(.1)
280-
278+
plt.pause(.1)

src/2a-chessPrint.py

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,83 @@
11
""" Demonstrate fundamental programming elements
2-
by creating a checker board, 8 by 8 fields,
2+
by creating a checker board, N by N fields,
33
field size is configurable, same width and height
4+
5+
The following programming constructs are demonstrated:
6+
7+
Comment
8+
# The "#" starts a comment until the end of the line
9+
# We can write whatever we want
10+
11+
Variable
12+
Normally, we have to remember values sometime in order to perform
13+
operations on or with them later on. A variable is an object which
14+
has a name and a value, like so:
15+
var1 = "some text"
16+
or
17+
var2 = 123
18+
or
19+
var3 = True
20+
We can change the value of variables any time
21+
22+
Constant
23+
A constant is basically a variable were the value cannot be changed
24+
(immutable). We don't have this here but we use some variables in this
25+
style
26+
27+
Operation
28+
We have a large number of operator available to do math, logic and other
29+
things like:
30+
a = 5 + 3
31+
b = a * 4
32+
c = a % 3 # The is the modulo operation (reminder)
33+
d = 5 == 6 # d is False. The double "=" means "has equal value"
34+
35+
Branch
36+
We test a condition and do one thing when it is true and another thing
37+
when it is false using IF ... ELSE ... statements like:
38+
if a == 4 then:
39+
do thing 1
40+
else:
41+
do thing 2
42+
43+
Iteration (loop)
44+
We can repeat sections of the code several times in different ways, for
45+
example for a (fixed) number of times using a
46+
FOR LOOP like so:
47+
for i in range(10):
48+
statements ...
49+
of while a condition is true like so:
50+
while a < b:
51+
statements ...
52+
53+
Block
54+
Every language has it's own syntax to form group of statements. Python
55+
uses indentation. All statements in sequence with the same indent level
56+
are in the same block, like so:
57+
if a == 4 then:
58+
thing1a() # call function thing1a in the IF branch
59+
thing1b() # same identation => same block => do also thing1b
60+
else: # same indent as the "if", previous block terminated
61+
thing2() # new block, do thing2 in the ELSE branch
62+
thing3() # not indented, previous block terminated. Thing3 is
63+
# outside the IF-ELSE block
64+
65+
Function
66+
we use functions to execute code which is (re)used several times
67+
we can provide parameters to the functions, so the same code can
68+
actually perform (slightly) different depending on the parameters
69+
Function can return "objects" (we don't know what exactly that is yet,
70+
but in Python more or less everything is an object)
71+
Functions have to be declared before we can use them like so:
72+
def <name>([paramters]):
73+
... function code ...
74+
[return]
75+
476
"""
577

78+
########################################################################
79+
# this is our first function declaration
80+
# We have to decalre the function before we use it
681
# function to make one linear segment of a pattern
782
def segment(width, color):
883
""" print <width> items of a color """
@@ -11,39 +86,39 @@ def segment(width, color):
1186
# print(color,end="") # simple verion
1287
print("{0:2d}".format(color),end="") # this looks better
1388

14-
# the main function
89+
########################################################################
90+
# our second function is the main function
1591
def main():
1692
"""create a checker board pattern using a function"""
1793

18-
# constants
94+
# constants (actually just variables we do not change any more)
1995
fields = 8
2096
fieldWidth = 4
21-
97+
2298
# variables
2399
row = 0
24100
column = 0
25101
# set color to 0
26102
color = 0
103+
# we iterate until we are done ...
27104
while row < fields*fieldWidth and column < fields*fieldWidth:
28105
# print segment
29106
segment(fieldWidth,color)
30-
# invert color
107+
# invert color after every segment
31108
color = 1 if color == 0 else 0
32-
# increment column
109+
# increment column. go back to 0 after 8 fields
33110
column = column + fieldWidth if column < fieldWidth*(fields-1) else 0
34111
# new line and increment row if column = 0
35112
if column == 0:
36113
print("") # prints a new line
37-
# reset line
114+
# next row
38115
row = row + 1
39116
# invert color again if new row and end of field
40117
if column == 0 and row % fieldWidth == 0 :
41118
color = 1 if color == 0 else 0
42-
43119

120+
121+
########################################################################
44122
# call the main function
45123
if __name__ == "__main__":
46124
main()
47-
48-
49-

src/2b-chessList.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
11
""" Demonstrate fundamental programming elements
2-
by creating a checker board, 8 by 8 fields,
2+
by creating a checker board, 8 by 8 fields,
33
field size is configurable, same width and height
4+
5+
This is basically the same as in the previous example, but instead
6+
of printing the segment immediately we create a data structure
7+
based upon lists ([]) and append the segment to the line
8+
49
"""
510

611
# function to make one linear segment of a pattern
712
def segment(width, color):
8-
""" put <width> items of color in a row starting at position row,col (= y,x)"""
9-
# initialize a new list as a numpy array
13+
""" put <width> items of color in a row"""
14+
# initialize a new list
1015
segment = []
1116
# add items with a FOR LOOP
1217
for i in range(width):
1318
segment.append(color)
1419
# return the list
1520
return segment
1621

17-
# the main function
22+
# the main function
1823
def main():
1924
"""create a checker board pattern using a function"""
2025

2126
# constants
2227
fields = 8
2328
fieldWidth = 2
24-
29+
2530
# variables
2631
row = 0
2732
column = 0
@@ -38,25 +43,22 @@ def main():
3843
color = 1 if color == 0 else 0
3944
# increment column
4045
column = column + fieldWidth if column < fieldWidth*(fields-1) else 0
41-
# add line to board and increment row if column = 0
46+
# if column == 0 then previous line is complete.
47+
# add line to board, increment row and start a new line
4248
if column == 0:
4349
board.append(line)
44-
# reset line
45-
line = []
4650
row = row + 1
51+
line = []
4752
# invert color again if new row and end of field
4853
if column == 0 and row % fieldWidth == 0 :
4954
color = 1 if color == 0 else 0
50-
55+
5156

5257
print("Checker board")
5358
for b in board:
5459
print(b)
55-
60+
5661

5762
# call the main function
5863
if __name__ == "__main__":
5964
main()
60-
61-
62-

src/2c-chessNp.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
11
""" Demonstrate fundamental programming elements
2-
by creating a checker board, 8 by 8 fields,
2+
by creating a checker board, 8 by 8 fields,
33
field size is configurable, same width and height
4+
5+
Again, the basic algorith is like before, but this time
6+
we use a more advanced data structure provided by a external library
7+
The library "numerical python" (numpy) is imported with the statement
8+
#import numpy as np
9+
and it's functions are called as "np.function_name()"
10+
11+
We use the "array" data type to realize a 2D matrix, representing
12+
the checker board. The 2D matrix is similar to the list of lists but
13+
offers additional operations e.g. on the entire dataset which we use
14+
in the future
15+
416
"""
517

618
# numpy is a helper library
719
import numpy as np
820

921
# function to make one linear segment of a pattern
1022
def segment(width, color):
11-
""" put <width> items of color in a row starting at position row,col (= y,x)"""
23+
""" put <width> items of color in a row"""
1224
# initialize a new list as a numpy array
13-
segment = np.empty(width) #
25+
segment = np.empty(width) #
1426
# add items with a FOR LOOP
1527
for i in range(width):
1628
segment[i] = color
1729
# return the list
1830
return segment
1931

20-
# the main function
32+
# the main function
2133
def main():
2234
"""create a checker board pattern using a function"""
2335

2436
# constants
2537
fields = 8
2638
fieldWidth = 2
27-
39+
2840
# variables
2941
row = 0
3042
column = 0
@@ -44,15 +56,12 @@ def main():
4456
# invert color again if new row and end of field
4557
if column == 0 and row % fieldWidth == 0 :
4658
color = 1 if color == 0 else 0
47-
59+
4860

4961
print("Checker board")
5062
print(board)
51-
63+
5264

5365
# call the main function
5466
if __name__ == "__main__":
5567
main()
56-
57-
58-

0 commit comments

Comments
 (0)