-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone.py
82 lines (59 loc) · 2.05 KB
/
one.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
import json
import yaml
import configparser
import os
from dotenv import load_dotenv
from config import drivers_config # Assuming drivers_config is defined in config.py
def python_configuration_files():
"""
Create config.py and store required values in a dictionary. Import it and use dictionary keys to access required values.
"""
print(drivers_config)
print(drivers_config.get('URL')) # Using .get() to avoid KeyError if 'URL' is not present
print(drivers_config.get('FileName'))
# Uncomment the following line to use the function
# python_configuration_files()
def json_method():
"""
Read values from a JSON config file.
"""
with open('config.json') as file:
data = json.load(file)
print(data.get('drivers_config', {}).get('URL')) # Using .get() to handle missing keys
# Uncomment the following line to use the function
# json_method()
def yml_data():
"""
Read values from a YAML config file.
"""
with open('config.yml', 'r') as file:
prime_service = yaml.safe_load(file)
print(prime_service, type(prime_service))
print(prime_service.get('URL'))
print(prime_service.get('rest', {}).get('url')) # Using .get() to handle missing keys
# Uncomment the following line to use the function
# yml_data()
def ini():
"""
Read values from an INI config file.
"""
file = open("config.ini", "r")
config = configparser.RawConfigParser(allow_no_value=True)
config.read_file(file)
print(config.get("drivers_config", "URL"))
# Uncomment the following line to use the function
# ini()
def env_method():
"""
Load environment variables from a .env file.
"""
load_dotenv()
# Use os.environ.get() with a default value to avoid KeyError
my_name = os.environ.get("myname", "DefaultName")
password = os.environ.get("password", "DefaultPassword")
logs_path = os.environ.get("LOGS_PATH", "DefaultLogsPath")
print(my_name)
print(password)
print(logs_path)
# Uncomment the following line to use the function
# env_method()