-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_json_object.py
More file actions
executable file
·58 lines (43 loc) · 1.21 KB
/
Copy pathexample_json_object.py
File metadata and controls
executable file
·58 lines (43 loc) · 1.21 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
55
#!/usr/bin/env python3
from json import loads, dumps
from pathlib import Path
input_path = Path('example.json')
text = input_path.read_text(encoding='utf8')
class Database:
def __init__(self, user, password, server, port):
self.user = user
self.password = password
self.server = server
self.port = port
class Config:
def __init__(self, database, api):
self.database = database
self.api = api
class Factory:
@staticmethod
def create_database(obj):
return Database(
user=obj['user'],
password=obj['password'],
server=obj['server'],
port=obj['port'],
)
@staticmethod
def create_config(obj):
return Config(
database=Factory.create_database(obj['database']),
api=obj['api']['url']
)
print('Content from file:')
print(text)
print('\n\n')
data = Factory.create_config(loads(text))
print('Python structure')
print(data)
print('')
print('Individual fields accessed like objects ... IDEs can auto-complete and check fields:')
print(data.database.user)
print(data.database.password)
print(data.database.server)
print(data.database.port)
print(data.api)