Python Forum
Read Yaml configuration file in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read Yaml configuration file in Python
#1
I wrote a program to read a Yaml configuration file and display it to the terminal. Now I want to try something like checking if the database (db) in the yaml file is not Sqlite or Postgres then exception will raise
Quote: Invalid database type, should be sqlite or postgres
. I tried but still couldn't catch the exception. Can anyone give me suggestion?
My test.yaml file :
Quote:db: mysql
dbopt:
host: foobar.baz.qux.com
port: 6311
dbname: spam_eggs
user: hamburger
password: example_password
client_encoding: utf-8
connect_timeout: 60
sslmode: none
query: select * from manufacturing_product

My code:
# 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
My program still cannot catch the exception. My terminal :
Output:
db : mysql dbopt : {'host': 'foobar.baz.qux.com', 'port': 6311, 'dbname': 'spam_eggs', 'user': 'hamburger', 'password': 'example_password', 'client_encoding': 'utf-8', 'connect_timeout': 60, 'sslmode': 'none'} query : select * from manufacturing_product
Reply
#2
Is that all your code? Some things I notice:

- You have declared an __init__ function outside of a class. Why?
- Said function isn't actually called anywhere.
- There's no code to catch any exceptions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace values in Yaml file with value in dictionary PelleH 1 2,080 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 996 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Read TXT file in Pandas and save to Parquet zinho 2 1,204 Sep-15-2024, 06:14 PM
Last Post: zinho
  Endpoint Configuration Issues in Python Script on AWS EC2 zaharul 0 625 Aug-31-2024, 10:22 AM
Last Post: zaharul
  Pycharm can't read file Genericgamemaker 5 1,536 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,545 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,182 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 4,577 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,733 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 2,539 Jul-06-2023, 01:52 AM
Last Post: Tupa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020