为什么Python配置库的公共变量没有改变,因为我将私有变量提供给它们

时间:2016-07-19 17:08:30

标签: python private public

我打算创建一个Python库作为配置文件。

我希望让用户在配置中更改私有变量,然后当配置文件运行其load_input()函数时,将验证这些私有变量,并且这些对应变量将具有其值。

但是当我导入这个配置文件时,它的公共变量似乎没有被更改。你能告诉我原因吗?

非常感谢你们。这是下面的配置文件。

    # config.py

    __level1_fullpath = '.../level_1a.csv'
    __boundary_fullpath = '.../TLS_adm0.shp'
    __output_polygon_fullpath = 'TEST_hightlight_USA.shp'
    __output_point_fullpath = 'Test_highlight_USA_point.shp'

    __attribute_csv_fullpath = 'default'
    __dict_filter = {}
    __dict_filter['donors_iso3'] = 'USA'
    __time_filter = {}
    # __time_filter['example'] = {'start':2006, 'end':2010}
    __voronoi_mode = 1
    __allow_empty_time_record = False
    __comparing_country_iso = ''

    level1_fullpath = None
    boundary_fullpath = None
    output_polygon_fullpath = None
    output_point_fullpath = None
    attribute_csv_fullpath = None
    # for time filter:
    # boolean
    allow_empty_time_record = None
    # voronoi mode
    voronoi_mode = None
    # two kinds of filters
    dict_filter = None
    time_filter = None
    # pairwise mode must have this input below
    comparing_country_iso = None

    def load_input():
    try:
        level1_fullpath = str(__level1_fullpath)
        boundary_fullpath = str(__boundary_fullpath)
        output_point_fullpath = str(__output_point_fullpath)
        output_polygon_fullpath = str(__output_polygon_fullpath)
        attribute_csv_fullpath = str(__attribute_csv_fullpath)
    except:
        raise ImportError('Please check all input fullpath parameters are writen correctly in Input Configuration file')

    try:
        dict_filter = dict(__dict_filter)
        time_filter = dict(__time_filter)
    except:
        raise ImportError('Please check filter dictionray has been inputted properly in input configuration file.')

    try:
        voronoi_mode = int(__voronoi_mode)
    except:
        raise ImportError('Please check __voronoi_mode has been inputted properly under instruction.')

    try:
         allow_empty_time_record = bool(__allow_empty_time_record)
    execpt:
        raise ImportError('Please check __allow_empty_time_record has been inputted properly under instruction.')

    try:
        if voronoi_mode == 2:
            comparing_country_iso = str(__comparing_country_iso)
    except:
        raise ImportError('Please check __comparing_counties_iso has been inputted properly under instruction.')

    load_input()

当我尝试导入它并使用配置变量时。我会这样做

    import config

    inputdata = config.level1_fullpath
    voronoi_mode = config.voronoi_mode
    ...

但是这些公共变量只显示为None,是NoneType。

0 个答案:

没有答案