The strategy to make a configuration object for your python project

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.)

这里写图片描述

posted on 2018-08-06 13:39  yusisc  阅读(17)  评论(0编辑  收藏  举报

导航