forked from MeItsLars/thabloid-sticker-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
117 lines (97 loc) · 3.17 KB
/
util.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
"""
File with utility functions
"""
import re
dutch_address_regex = re.compile(r"^(\d*[\wäöüß\d '/\\.\-]+)[,\s]+(\d+)[\s,]*([\wäöüß\d\-/]*)$")
def query_yes_no(question: str, default: str = None) -> bool:
"""
Ask a yes/no question to the user and return their answer.
Parameters
----------
question: str
The question that is presented to the user
default: str
Default response value (i.e. the value that is used when the user presses the 'enter' key)
Can be 'yes', 'ye', 'y', 'no', 'n' and None
Raises
------
ValueError
If an invalid default value is given
Returns
-------
bool:
True if the user response was 'yes', False if the user response was 'no'
"""
valid = {"yes": True, "y": True, "ye": True, "no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError(f"invalid default answer: '{default}'")
while True:
print(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
if choice in valid:
return valid[choice]
print("Please respond with 'yes' or 'no' (or 'y' or 'n')")
def entry_to_string(entry: dict) -> str:
"""
Converts an entry to a multi-line human-readable string
Parameters
----------
entry: dict
The entry, represented as a dictionary
Returns
-------
str:
A nicely formatted string representing this entry
"""
string = f" Name: {entry['first_name']} {entry['last_name']}\n" \
f" Address: {entry['address']}\n"
if len(entry['address_2']) > 0:
string += f" {entry['address_2']}\n"
string += f" Postal code: {entry['postal_code']}\n" \
f" City: {entry['city']}"
if entry['country'] != 'Netherlands':
string += f"\n Country: {entry['country']}"
return string
def format_dutch_address(address: str) -> (str, str, str):
"""
Formats a Dutch address into a street name, house number, and house number extra part,
using a regular expression.
Parameters
----------
address: str
The address, represented as a string
Returns
-------
str, str, str:
A tuple containing the address, house number and house number extra
None, None, None
If the pattern could not be matched
"""
pattern = dutch_address_regex.search(address.strip())
if pattern:
return pattern.group(1), pattern.group(2), pattern.group(3)
return None, None, None
def format_dutch_postal_code(postal_code: str) -> str:
"""
Formats a Dutch postal code to upper-case and with a space between the letters and digits.
Parameters
----------
postal_code: str
The Dutch postal code
Returns
-------
str:
The formatted Dutch postal code
"""
postal_code = postal_code.upper()
if len(postal_code) == 6:
return f"{postal_code[0:4]} {postal_code[4:6]}"
return postal_code