7 Commits
teil23 ... 1.00

Author SHA1 Message Date
b655926202 teil25 vom develop 2024-03-12 15:54:48 +01:00
0d63af7bbc endgültige Versionen. 2024-03-12 15:53:19 +01:00
504013d890 phone.py 2024-02-07 07:24:08 +00:00
65eb1436d6 tungen für RegEx.e Bitte geben Sie eine Commit-Beschreibung für Ihre Änderungen ein. Zeilen, 2024-02-07 08:21:15 +01:00
e2676beee0 Demoprogramme 2024-02-02 13:47:29 +01:00
3963f98baa Dateien umbenannt. 2024-02-02 11:48:10 +01:00
Olli Graf
c01833f411 teil24 angelegt. 2024-02-02 09:55:56 +01:00
17 changed files with 2937 additions and 2 deletions

View File

@@ -26,7 +26,9 @@ CC-BY-SA Olli Graf
|19 | Webseiten (Flask)|
|20 | virtuelle Umgebungen|
|22 | Numpy|
|21 | Matplotlib|
|23 | Pandas|
|23 | Matplotlib|
|24 | match|
|25 | Pandas|
|26 | Pygame|

6
teil24/.gitignore vendored Normal file
View File

@@ -0,0 +1,6 @@
# created by virtualenv automatically
__pycache__
bin
lib
share
*.csv

19
teil24/match_list.py Normal file
View File

@@ -0,0 +1,19 @@
def print_match(wert):
print(f'wert={wert}')
match wert:
case 1|2:
print('1 oder 2')
case 3|4:
print('3 oder 4')
case _:
print('unbekannt')
print_match(1)
print_match(3)
print_match(5)

11
teil24/match_structure.py Normal file
View File

@@ -0,0 +1,11 @@
values= ['John']
# Matching structure in Python switch-case
match values:
case [a]:
print(f'Only one item: {a}')
case [a, b]:
print(f'Two items: {a}, {b}')
case [a, b, c]:
print(f'Three items: {a}, {b}, and {c}')
case [a, b, c, *rest]:
print(f'More than three items: {a}, {b}, {c}, as well as: {rest}')

19
teil24/print_class.py Normal file
View File

@@ -0,0 +1,19 @@
def print_match(name):
print(f'name={name}')
match name:
case 'Lisa'|'Ralph'|'Janey':
print('zweite Klasse bei Ms. Hoover')
case 'Bart'|'Milhouse'|'Nelson':
print('vierte Klasse bei Ms. Krabappel')
case _:
print('unbekannt')
print_match('Lisa')
print_match('Milhouse')
print_match('Homer')

23
teil24/print_tag_elif.py Normal file
View File

@@ -0,0 +1,23 @@
import sys
num=5
def print_wochentag(num):
if num == 0:
print('Montag')
elif num == 1:
print('Dienstag')
elif num == 2:
print('Mittwoch')
elif num == 3:
print('Donnerstag')
elif num == 4:
print('Freitag')
elif num == 5:
print('Samstag')
elif num == 6:
print('Sonntag')
if __name__ == '__main__':
print_wochentag(int(sys.argv[1]))

28
teil24/print_tag_match.py Normal file
View File

@@ -0,0 +1,28 @@
import sys
num=5
def print_wochentag(num):
match num:
case 0:
print('Montag')
case 1:
print('Dienstag')
case 2:
print('Mittwoch')
case 3:
print('Donnerstag')
case 4:
print('Freitag')
case 5:
print('Samstag')
case 6:
print('Sonntag')
case _:
print('unbekannte Eingabe')
if __name__ == '__main__':
print_wochentag(int(sys.argv[1]))

View File

@@ -0,0 +1,28 @@
import sys
num=5
def print_wochentag(num):
match num:
case '0':
print('Montag')
case '1':
print('Dienstag')
case '2':
print('Mittwoch')
case '3':
print('Donnerstag')
case '4':
print('Freitag')
case '5':
print('Samstag')
case '6':
print('Sonntag')
case _:
print('unbekannte Eingabe')
if __name__ == '__main__':
print_wochentag(sys.argv[1])

23
teil24/print_type.py Normal file
View File

@@ -0,0 +1,23 @@
#encoding:utf-8
def print_type(value):
match value:
case int():
print('Integer')
case str():
print('String')
case list():
print('Liste')
case dict() as d:
print(f'Dictionary mit {len(d)} Einträgen')
case _:
print('unbekannter Datentyp')
print_type(1) #Integer
print_type('The Simpsons') # String
print_type([1, 2, 3]) # Liste
print_type({'a': 1, 'b': 2, 'c': 3}) # Dictionary
print_type(1.0) #float ist nicht implentiert

8
teil24/pyvenv.cfg Normal file
View File

@@ -0,0 +1,8 @@
home = /usr/bin
implementation = CPython
version_info = 3.11.2.final.0
virtualenv = 20.17.1+ds
include-system-site-packages = false
base-prefix = /usr
base-exec-prefix = /usr
base-executable = /usr/bin/python3

6
teil25/.gitignore vendored Normal file
View File

@@ -0,0 +1,6 @@
# created by virtualenv automatically
bin/
lib/
__pycache__
pyenv.cfg
*.bak

File diff suppressed because it is too large Load Diff

21
teil25/filter_vornamen.py Normal file
View File

@@ -0,0 +1,21 @@
from vornamen_reader import read_vornamen
import re
def print_vornamen_simple(vornamen):
for vorname in vornamen:
print(vorname)
def print_vornamen_filtered(vornamen,regex):
pattern = re.compile(regex)
for vorname in vornamen:
# print(f"{type(r)}: {r}")
# s = re.findall(regex,vorname)
# if s[0] != '':
is_match = pattern.match(vorname).span()[1] >0
if is_match:
print(f'{is_match}: {vorname}')
vornamen = read_vornamen('./Vornamen_Wuppertal_2020.csv')
#print_vornamen_simple(vornamen)
print_vornamen_filtered(vornamen,'O*')

22
teil25/phone.py Normal file
View File

@@ -0,0 +1,22 @@
import re
import unittest
class RegEx(unittest.TestCase):
pattern_phone = re.compile(r'\D')
pattern_digits_only = re.compile(r'\d')
def test_phone(self):
m = self.pattern_phone.sub('','+1 (212) 555-6832')
self.assertIsNotNone(self.pattern_digits_only.match(m))
print(f'm={m}')
if __name__ == '__main__':
unittest.main()

8
teil25/pyvenv.cfg Normal file
View File

@@ -0,0 +1,8 @@
home = /usr/bin
implementation = CPython
version_info = 3.11.2.final.0
virtualenv = 20.17.1+ds
include-system-site-packages = false
base-prefix = /usr
base-exec-prefix = /usr
base-executable = /usr/bin/python3

101
teil25/regex.py Normal file
View File

@@ -0,0 +1,101 @@
import re
import unittest
import logging
pattern_char_only = re.compile('^[A-Z][a-z]+$')
pattern_digits_only = re.compile(r'\d+')
pattern_no_digits = re.compile(r'\D+')
pattern_plz = re.compile('^[0-9]{5}$')
pattern_plz2 = re.compile('^\d{5}$')
pattern_quote= re.compile(':')
pattern_tab = re.compile(r'\t')
pattern_simpson = re.compile('Simpson')
pattern_simple = re.compile('O*')
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
# Unittest für die Demonstration von regular expressions (re)
class TestRegEgEx(unittest.TestCase):
# match: Passt der String auf die re
def test_match(self):
m = pattern_char_only.match('Olli')
logging.debug(f'm={m}')
self.assertTrue(m.span()[1] >0)
# Sucht alle Vorkommnisse der re im String
def test_findall(self):
s = '5 Äpfel, 10 Bananen, 42 Erdbeeren'
logging.debug(f' nur Ziffern: {pattern_digits_only.findall(s)}')
logging.debug(f' keine Ziffern: {pattern_no_digits.findall(s)}')
m = pattern_char_only.findall('Olli')
logging.debug(f'm={m}')
self.assertIsNotNone(m)
# Prüft die Gültigkeit einer Postleitzahl ('^\d{5}$' genau 5 Ziffern)
def test_plz(self):
patterns = [pattern_plz,pattern_plz2]
plz = ['42275','5600']
for pattern in patterns:
m = pattern.findall(plz[0])
logging.debug(f'm={m}')
self.assertNotEqual('',m[0])
m = pattern.findall(plz[1])
logging.debug(f'm={m}')
self.assertEqual([],m)
# Test für Suche
def test_search(self):
m = pattern_char_only.search('Olli')
logging.debug(f'm={m}')
#String passt auf die regular expression
self.assertIsNotNone(m)
m = pattern_char_only.search('123456')
logging.debug(f'm={m}')
#Dieser String passt nicht.
self.assertIsNone(m)
# Test zum Splitting (der Satz wird am ':' in die Worte zerlegt)
def test_split(self):
m = pattern_quote.split(' Die:Würde:des:Menschen:ist:unantastbar.')
logging.debug(f'm={m}')
self.assertIsNotNone(m)
#Die einzelnen Worte ausgeben
for word in m:
logging.debug(f' Wort: {word}')
# Test für Ersetzung (substitution von Tab mit Space)
def test_sub(self):
m = pattern_tab.sub(' ','Satz\tmit\tTabulator.')
logging.debug(f'm={m}')
# Test für einfache regular Expression ('O*')
def test_simple_re(self):
m = pattern_simple.search('Olli')
logging.debug(f'm={m.span()}')
self.assertTrue(m.end() > 0)
m = pattern_simple.search('Luana')
logging.debug(f'm={m.span()}')
self.assertFalse(m.end() >0)
# Test für den Iterator ('*Simpson')
def test_finditer(self):
m = pattern_simpson.finditer('Homer Simpson, Marge Simpson, Bart Simpson, Lisa Simpson, Maggie Simpson')
logging.debug(f'm={m}')
# Die einzelnen gefunden Teilstrings ausgeben.
for name in m:
logging.debug(f'Name={name},Start bei: {name.start()}')
if __name__ == '__main__':
unittest.main()

50
teil25/vornamen_reader.py Normal file
View File

@@ -0,0 +1,50 @@
#encoding: utf-8
import re
def read_vornamen(filename):
dict = {}
# Der Zähler dient nur dazu, die erste Zeile zu überspringen
count = 0
pattern_csv = re.compile(';')
dict['mädchen'] = 0
dict['jungs'] = 0
dict['divers'] = 0
with open(filename,'r') as f:
for zeile in f:
if count >0:
# einzelne Zeile in seine Bestandteile zerlegen
splitted = pattern_csv.split(zeile.strip())
anzahl = splitted[0]
vorname = splitted[1]
geschlecht= splitted[2]
position = splitted[3]
if geschlecht == 'w':
dict['mädchen'] += 1
elif geschlecht == 'm':
dict['jungs'] += 1
else:
dict['divers'] += 1
data = {}
data['vorname'] = vorname
data['anzahl'] = int(anzahl)
data['geschlecht'] = geschlecht
data['position'] = position
if vorname not in dict:
dict[vorname] = data
else:
# doppelte Einträge summieren wir auf.
e = dict[vorname]
e['anzahl'] = int(anzahl) + (e['anzahl'])
count +=1
gesamt = dict['jungs'] + dict['mädchen'] + dict['divers']
dict['gesamt'] = gesamt
return dict