Finished Task S2 for PR3

main
Eren Saglam 2023-01-09 17:41:29 +01:00
parent dce0c0a2b0
commit 0b7b0ce07b
7 changed files with 207 additions and 0 deletions

2
Personen.txt 100644
View File

@ -0,0 +1,2 @@
Dr. Lucas Linus Kreutzer, 19.12.2022, Heidelberger Allee 18 68305 Mannheim, 0123456789
Ing. Birgid Oestrovsky, 2022-03-12, Heidelberger Allee 18 68305 Mannheim, 0123456789

62
PersonenNeu.json 100644
View File

@ -0,0 +1,62 @@
[
{
"Index": 1,
"Titel": [
"Dr."
],
"Vorname": [
"Lucas"
],
"Zweitname": [
"Linus"
],
"Nachname": [
"Kreutzer"
],
"Stra\u00dfe": [
"Heidelberger Allee"
],
"Hausnummer": [
"18"
],
"PLZ": [
"68305"
],
"Wohnort": [
"Mannheim"
],
"Geburtsdatum": [
"19.12.2022"
]
},
{
"Index": 2,
"Titel": [
"Ing."
],
"Vorname": [
"Birgid"
],
"Zweitname": [
null
],
"Nachname": [
"Oestrovsky"
],
"Stra\u00dfe": [
"Heidelberger Allee"
],
"Hausnummer": [
"18"
],
"PLZ": [
"68305"
],
"Wohnort": [
"Mannheim"
],
"Geburtsdatum": [
"12.03.2022"
]
}
]

26
s2_a1.py 100644
View File

@ -0,0 +1,26 @@
class X:
pass
class Y(X):
pass
class Z(X, Y):
pass
xyz = Z()
# L[X] = [X] + merge([X])
# L[X] = [X]
# L[Y] = [Y] + merge(L(X), [Y])
# L[Y] = [Y] + merge([X], [Y])
# L[Y] = [Y, X] + merge([Y])
# L[Y] = [Y, X]
# L[Z] = [Z] + merge(L(X), L(Y), [X, Y])
# L[Z] = [Z] + merge([X], [Y, X], [X, Y])
# >> cannot pick X
# >> cannot pick Y

10
s2_a2.py 100644
View File

@ -0,0 +1,10 @@
import re
text = "If the the problem is textual, use the the re module"
# \b Beschränkt den Match auf den Anfang/das Ende des Wortes
# \1 Referenz auf die erste Gruppe (In dem Fall das erste Vorkommen des Wortes)
# \w+ Matched mindestens ein Wort
text = re.sub(r'\b(\w+)( \1\b)+', r'\1', text)
print(text)

53
s2_a3.py 100644
View File

@ -0,0 +1,53 @@
import re
import json
index = 0
def parse_zeile(zeile):
global index
index = index + 1
elemente = zeile.split(", ")
name = elemente[0]
geburtsdatum = elemente[1]
adresse = elemente[2]
rufnummer = elemente[3]
print(name, adresse, geburtsdatum, rufnummer)
person = {}
person['Index'] = index
name_matches = re.search(r'([^ ]+) ([^ ]+) ([^ ]+)(?: ([^ ]+))?', name)
person['Titel'] = [name_matches.group(1)]
person['Vorname'] = [name_matches.group(2)]
if name_matches.group(4) is None:
person['Zweitname'] = [None]
person['Nachname'] = [name_matches.group(3)]
else:
person['Zweitname'] = [name_matches.group(3)]
person['Nachname'] = [name_matches.group(4)]
adress_matches = re.search(r'([^0-9]+) ([0-9]+) ([0-9]{5,10}) (.+)', adresse)
person['Straße'] = [adress_matches.group(1)]
person['Hausnummer'] = [adress_matches.group(2)]
person['PLZ'] = [adress_matches.group(3)]
person['Wohnort'] = [adress_matches.group(4)]
birth_matches = re.search(r'([0-9]{2,4})[ .-]([0-9]{2})[ .-]([0-9]{2,4})', geburtsdatum)
if len(birth_matches.group(1)) == 4:
person['Geburtsdatum'] = [birth_matches.group(3) + "." + birth_matches.group(2) + "." + birth_matches.group(1)]
else:
person['Geburtsdatum'] = [birth_matches.group(1) + "." + birth_matches.group(2) + "." + birth_matches.group(3)]
return person
personen = []
with open("Personen.txt", 'r') as file:
for zeile in file:
personen.append(parse_zeile(zeile))
with open("PersonenNeu.json", 'w') as file:
json.dump(personen, file)

21
s2_a4_a.py 100644
View File

@ -0,0 +1,21 @@
import re
def parse_number(number):
pattern = r'^(?:\+?1[- ])?(\(\d{3}\)|\d{3})[-. ](\d{3})[-. ](\d{4})$'
if not re.fullmatch(pattern, number):
raise ValueError("Ungültige Telefonnummer")
segments = re.search(pattern, number)
ortsvorwahl = segments.group(1).replace("(", "").replace(")", "")
amtskennziffer = segments.group(2)
vorwahl = segments.group(3)
if ortsvorwahl[0] <= '1' and ortsvorwahl[0] >= '0':
raise ValueError("Ungültige Telefonnummer")
if (vorwahl[0] <= '1' and vorwahl[0] >= '0') or vorwahl[1] == '9':
raise ValueError("Ungültige Telefonnummer")
return "1-" + ortsvorwahl + "-" + amtskennziffer + "-" + vorwahl

33
s2_a4_b.py 100644
View File

@ -0,0 +1,33 @@
import unittest
import s2_a4_a
test_numbers = [
("+1 223-456-7890", "1-223-456-7890"),
("1-223-456-7890", "1-223-456-7890"),
("+1 223 456-7890", "1-223-456-7890"),
("(223) 456-7890", "1-223-456-7890"),
("1 223 456 7890", "1-223-456-7890"),
("223.456.7890", "1-223-456-7890"),
("1-989-111-2222", "1-989-111-2222"),
]
test_numbers_invalid = [
"",
"+49 012 345 6821",
"+49 6821",
"1-182-324-4324",
"1-082-324-4324",
"1-682-624-1324",
"1-682-624-0324",
"1-682-624-5924",
]
class NumbersTests(unittest.TestCase):
def test_numbers(self):
for number, expected in test_numbers:
self.assertEqual(expected, s2_a4_a.parse_number(number))
def test_numbers_fails(self):
for number in test_numbers_invalid:
self.assertRaises(ValueError, lambda: s2_a4_a.parse_number(number))