The strategy to make a configuration object for your python project

本文探讨了组织项目配置的几种方法,包括使用字典、easydic对象及简单的Python对象。通过对比不同方法的特点,最终推荐使用Python对象进行配置管理,因为它能够将所有相关变量封装在一个单一的对象中,并且易于引用。
摘要由CSDN通过智能技术生成

Organizing the configurations of the project is in demand. Here are the methods to organize the configurations.

  • Initializing a bunch of variables seems to be the worst choice, for its weak organizing.
  • restoring all the variables in a dictionary organizes all the relating variable in a single object. But when referring the configuration items, there need be square brackets and quotation marks, and there is no auto-completion.
  • restoring all the variables in a easydic object is some how like restoring the variables to a dictionary. No need of square brackets and quotation marks. It also be able to auto-complete, when referring in the same script file. But when the easydic object is referred from the other file, the auto-completion items do not contain the user added attributes.
  • Finally, the best choice, to the best of my knowledge, a simple python object, which is able to wrap all related variables into a single object and easy to refer, simple . operator is enough and auto-completion is available when referring cross files.

Example 0

# @File    : config.py


class Config1:
    def __init__(self):
        self.conf0 = 0
        self.path0 = '/home'
        self.path1 = '/home/owyn'
        self.cnt = 0


config1 = Config1()

if __name__ == '__main__':
    print(config1.path0)
# @File    : config_use.py

from config import config1

print(
    config1.path0
)

这里写图片描述
# Example 1

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
# @Time    : 9/1/2018 12:49 PM
# @Author  : yusisc (yusisc@gmail.com)

import numpy as np


class PureClass:
    pass

config0 = PureClass()
config0.aa = 1
config0.bb = 2
print(config0.)

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值