我正在尝试读取 YAML 配置文件并将其显示到终端。现在我想尝试检查 YAML 文件中的数据库(db)是否不是 Sqlite 或 Postgres,然后会引发异常,但我不知道如何。我试过但失败了,我做错了什么?
我的test.yaml文件:
database:
dbopt:
host: bvcbvcbvcb.com
port: 5432
dbname: db1
user: username
password: 1234
client_encoding: utf-8
connect_timeout: 60
sslmode: none
query:
select * from manufacturing_product
我的代码:
# process_yaml.py file`
import yaml
with open(r'D:\Python\test.yaml') as file:
# The FullLoader parameter handles the conversion from YAML
# scalar values to Python the dictionary format
data = yaml.full_load(file)
for item, doc in data.items():
print(item, ":", doc)
def __init__(self, dbconf):
self._dbconf = dict(dbconf)
# checking for database type
dbtype = self.get_db_type()
if dbtype != 'sqlite' and dbtype != 'postgres':
raise exceptions.InvalidConfigError(
'E01001', 'Invalid database type, should be sqlite or postgres.')
else:
self.dbtype = dbtype
我的程序仍然无法捕获异常。我的终端:
database:
dbopt:
host:
port: 5432
dbname: db1
user: username
password: 1234
client_encoding: utf-8
connect_timeout: 60
sslmode: none
query:
select * from manufacturing_product
哔哔one
相关分类