Python creating dynamic global variable from list

霸气de小男生 提交于 2019-12-12 03:55:30

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/15947221/python-creating-dynamic-global-variable-from-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!