YAML FILE CONFIGURATION FOR PYTHON

                             Yaml file used for DB configuration.

The following example code access SQL -> dev  -> url

DataBase:
config:
Oracle:
url: example.oracle.com
port: 8080
user: scott
pwd: tiger
MySql:
url: mysql.sql.com
port: 8081
user: scott
pwd: leo123
SQL:
dev:
url: microsoft.sql.com
port: 4040
user: mike
pwd: adm123
prod:
url: microsoft.sql.prod
port: 4041
user: admin
pwd: adm123
 
# basics of python using yaml file for common features in application
# yaml files used for DB configurations, sql queries, constants in application.
# Understanding YAML file structure
import yaml, os


def load_file(myfile):
if os.path.isfile(myfile):
with open(myfile, 'r') as file:
value_yaml = yaml.safe_load(file)
return value_yaml


def read_data(value_yaml):
db_config = value_yaml['DataBase']['config']
oracle_config = db_config['Oracle']
mysql_config = db_config['MySql']
sql_config = db_config['SQL']
print('SQL-DEV-URL::', sql_config['dev']['url'])


if __name__ == '__main__':
myfile = "learning/db_conf.yaml"
value_yml = load_file(myfile)
read_data(value_yml)
 

 output:

SQL-DEV-URL:: microsoft.sql.com


Next Page

Comments

Most popular

Learn Python Basics