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   yusisc  阅读(17)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示