在python中设置许多变量的最干净的方法
我想知道是否有一种更干净的方法来设置许多配置方法和变量。这可以用一种更干净、更像蟒蛇的方式来完成吗?我想买本字典,但不确定这是否是最好的做法在python中设置许多变量的最干净的方法,python,Python,我想知道是否有一种更干净的方法来设置许多配置方法和变量。这可以用一种更干净、更像蟒蛇的方式来完成吗?我想买本字典,但不确定这是否是最好的做法 script_name = sys.argv[0].split('.')[0] now = datetime.datetime.today() today = datetime.
script_name = sys.argv[0].split('.')[0]
now = datetime.datetime.today()
today = datetime.date.today()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
config_file = '%s/%s_config.ini' % (ini_path, target_os)
config = configparser.ConfigParser()
config.read(config_file)
config.sections()
cwd = os.getcwd()
base_dir = config.get('root_dir', 'dir')
file_name = 'fakeappname.%s.%s.log' % (target_os, today)
log_file = os.path.join(cwd, file_name)
log_key = log_file.split('/')[-1]
handler = logging.FileHandler(log_file, 'w', 'utf-8')
handler.setLevel(logging.INFO)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s -'
' {0}'.format(socket.gethostname()))
handler.setFormatter(formatter)
logger.addHandler(handler)
您有很多代码可以内联执行,比如:
cwd=os.getcwd()
log\u file=os.path.join(cwd,文件名)
log_key=log_file.split('/')[-1]
你能行
log\u key=os.path.join(os.getcwd(),文件名).split('/')[-1]
诸如此类的事情需要您添加注释进行解释,您可能无论如何都应该这样做。您能更具体一些吗?当然,我想我只是不喜欢每行一个变量,我想知道是否有更好的方法来管理所有这些设置。也许我忽略了一个模块?如果我还有30个,那么最好的做法是每行设置一个吗?除非您分配的数据和变量遵循相同的常规模式,否则您就无能为力了。这里没有足够的一致性来概括。其中一些东西是相关的,但很多都不是。我仍然不确定我是否理解这个问题。你可以用大写字母写常量。您还可以将变量等分类,如
#脚本名称的一般定义
和基本目录
,#日志定义
记录器等,我认为这更容易阅读。