-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathex_notes.py
More file actions
54 lines (37 loc) · 1.31 KB
/
ex_notes.py
File metadata and controls
54 lines (37 loc) · 1.31 KB
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
import sys
from typing import Optional
class DbException(Exception):
pass
class DbConnectionException(DbException):
pass
def connect_to_db(conn_str, server: Optional[str] = None, port: Optional[int] = None):
if ("server=" in conn_str and server) or ("port=" in conn_str and port):
raise DbConnectionException("Connection string is malformed")
conn_str += f"&server={server}&port={port}"
print(f"Connecting to DB with {conn_str}")
def setup_app():
# conn_str = "mongo://user=mk&password=a&database=talkpython"
conn_str = "mongo://user=mk&password=a&database=talkpython&port=1000"
server = "localhost"
port = 27017
try:
connect_to_db(conn_str, server, port)
except DbConnectionException as dbe:
dbe.add_note('You cannot specify server or port in both the conn str and explicitly')
dbe.add_note('Amend the conn string and try again.')
raise
def main():
# setup_app()
try:
setup_app()
print("App ready")
except Exception as x:
print("Error starting app:")
print(f'{type(x).__name__}: {x}')
if x.__notes__:
print(f'There are {len(x.__notes__)} notes')
for n in x.__notes__:
print(f"Note: {n}")
sys.exit(7)
if __name__ == '__main__':
main()