美文网首页IT技术
2018-11-26 python 配置模块

2018-11-26 python 配置模块

作者: 昨天今天下雨天1 | 来源:发表于2018-11-26 15:00 被阅读2次
# -*- coding: utf-8 -*-
import os
from utils.reader import YamlReader

BASE_PATH = os.path.split(os.path.dirname(os.path.abspath(__file__)))[0]
CONFIG_FILE = os.path.join(BASE_PATH, 'config', 'config.yml')
# DATA_PATH = os.path.join(BASE_PATH, 'data')
LOG_PATH = os.path.join(BASE_PATH, 'log')
REPORT_PATH = os.path.join(BASE_PATH, 'report')


class Config(object):
    def __init__(self, config=CONFIG_FILE):
        self.config = YamlReader(config).data

    def get(self, element, index=0):
        """用YamlReader读取返回的是一个list,第一项是默认的节。如果有多个节,可以传入index来获取。"""
        return self.config[index].get(element)

    def get_db_conf(self):
        db_conf = self.get('database')
        return '{engine}://{user}:{pwd}@{host}:{port}/{db}?charset={charset}'.format(**db_conf)


conf = Config()  # default conf, parse config.yml


相关文章

网友评论

    本文标题:2018-11-26 python 配置模块

    本文链接:https://www.haomeiwen.com/subject/fpsbqqtx.html