Python从列表创建动态全局变量 [英] Python creating dynamic global variable from list

查看:707
本文介绍了Python从列表创建动态全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使通用配置,从而配置解析器。有两个配置文件说A和B.我想解析部分,并根据硬编码列表从其中创建全局值。



以下是一个示例:



在配置文件中:

  [section] 
var1 = value1
var2 = value2

在全局范围内:

  some_global_list = [[var1,var2], [var3,var4]] 

在功能中解压缩此值,由ConfigParser: / p>

  configparser = ConfigParser.RawConfigParser()
configparser.read(some_config_filename)

in some_global_list:
globals()[par] = dict()
在configparser.items(section)中的元素:
globals()[section] .update({element [0]:元素[1]})

这个工作很好... 然而。全局变量的范围()似乎仅限于功能,这显然不是我的意图。我可以在那个函数中访问变量。



可能有人分享更好,简单的想法吗?
我知道我可能会移动代码主要而不用担心,但我不认为这是一个好主意。
我也想到制作一些生成器(对于伪代码在这里):



在全局范围内:

  for some_global_list:
globals()[x] = x

还尝试将其添加到函数中:

  for some_global_list [0]:
全球x

但无处可寻。



编辑:



经过以下讨论,这里是:



问题解决如下:


  1. 从主脚本中删除整个配置到模块

  2. 从模块导入(从模块导入somefunction)配置

  3. 删除全局变量()实际上不需要它们,因为函数有点像这样改变:

功能:

  def somefunction:
#(...)

configparser = ConfigParser .RawConfigParser()
configparser.read(some_config_filename)

temp_list = []
for some_global_list中的变量:
tmp = dict()
在configparser.items(section)中的元素:
tmp.update({element [0]:element [1] })
temp_list.append(tmp)
return temp_list #this是一个文件的包。

现在在主脚本中

  tmp = [] 
for i,枚举中的conf_file([args.conf1,args.conf2,args.conf3]):
如果conf_file:
尝试:
tmp.append([function(params ...)])
除了:
#handling here
#but,因为我需要这些配置名称作为全局变量
对于j,枚举中的variable_set(known_variable_names [i]):
globals()[variable_set] = tmp [i] [j]

所以不幸的黑客presists。但似乎工作。对于你的帮助家伙来说,Thx。

我接受(如果可能的话)下面回答,因为它给了我好主意:)

解决方案

解决这个问题的一个简单的方法是在你的应用程序包中的 __ init __。py 你可以做类似的事情以下内容:

  app_config = read_config()

def read_config():
configparser = ConfigParser.RawConfigParser()
configparser.read(some_config_filename)
return configparser.as_dict()#An将val返回为dict的imagine方法。

app_config变量可以导入应用程序中的任何其他模块。


I am trying to make generic config, and thus config parser. There are two config files say A and B. I want to parse sections and make global values from them according to hardcoded list.

Here is an example:

in config file:

[section]
var1 = value1
var2 = value2

In global scope:

some_global_list = [ ["var1","var2"], ["var3","var4"] ] 

in function to unpack this values, by ConfigParser:

configparser = ConfigParser.RawConfigParser()
configparser.read(some_config_filename)

for variables in some_global_list:
    globals()[section]=dict()
    for element in configparser.items(section):
        globals()[section].update({element[0]:element[1]})

This works nicely...however. Scope of globals() seem to be limited to function which is obviously not what I intended. I can access variable only while in that function.

Could someone share better yet simple idea? I know that i might move code to main and not to worry, but I don't think it is a good idea. I thought also about making some generator (sorry for pseudocode here):

in global scope:

for x in some_global_list:
    globals()[x] = x

also tried adding this to function:

for x in some_global_list[0]:
    global x

but got nowhere.

Edit :

After discussion below, here it is:

Problem solved like this:

  1. removed whole configuration from main script to module
  2. imported (from module import somefunction) config from module
  3. removed globals() in fact didnt need them, since function was changed a little like so:

in function:

def somefunction:
#(...)

configparser = ConfigParser.RawConfigParser()
configparser.read(some_config_filename)

temp_list=[]
for variables in some_global_list:
    tmp=dict()
    for element in configparser.items(section):
    tmp.update({element[0]:element[1]})
temp_list.append (tmp)
return temp_list #this is pack for one file.   

now in main script

tmp=[]
for i,conf_file in enumerate([args.conf1,args.conf2,args.conf3]):
    if conf_file:
        try:
            tmp.append([function(params...)])
        except:
            #handling here
        #but since i needed those config names as global variables 
    for j,variable_set in enumerate(known_variable_names[i]):
        globals()[variable_set] = tmp[i][j]

so unfortunate hack presists. But seems to work. Thx for Your help guys.

I'm accepting (if thats possible) below answer since it gave me good idea :)

解决方案

A simple way to solve this issue is in your application package within the __init__.py you can do something similar to the following:

app_config = read_config()

def read_config():
    configparser = ConfigParser.RawConfigParser()
    configparser.read(some_config_filename)
    return configparser.as_dict() #An imaginery method which returns the vals as dict.

The "app_config" variable can be imported into any other module within the application.

这篇关于Python从列表创建动态全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆