Python从配置文件取值

配置文件Config.py格式如下:


numNature = 9                       # Integer
                                    # NOTE!
padding = True                      # BOOLEAN
numProduct = None                   # Integer / None
prefix = "ST"                       # String / None
numUnit = 6                         # Integer / None
paddingForNumUnicite = False        # BOOLEAN
numExploitantEmetteur = None        # Integer / None
numExploitantDest = None            # Integer / None

useLogFileTimeStamp = False         # BOOLEAN: Option to set DATE_FABRICATION from last modification date of all the log files inside XML.

#### IF ALL THREE OPTIONS ABOVE ARE SET TO None/False,THEN DATE IS RETRIEVED FROM USER SYSTEM CLOCK NO MATTER WHAT #####

useSystemDateForXMLFileName = False    # Operator can choose whether to print the current system date for Xml filename
                                       # If False, we use config, header or log time stamp values for file name instead.
                                       # If system time is used in dateFabrication, it will print the same date,
                                       # regardless of the choice here. Only affects DATE_FABRICATION in xml IF all 
                                       # three options above are set to false.

limitXMLSize = True                    # BOOLEAN: Controls whether the XML number of lines are limited.
                                       # When False, max value does not matter.
                                       # Default/Normal is 'True'

maxXMLSizeInEntries = 100000           # You can adjust this variable to control the maximum number of lines printed
                                       # onto a file. The program will pre-emptively switch to new xml file
                                       # when value exceeds maxXMLSizeInEntries subtracted by last detected line count
                                       # in the latest log file. Only active when limitXMLsize is True.

test.py中获取配置文件的值:


import re
import struct
import xml.etree.ElementTree as ET
import getopt
import time
import datetime
import fnmatch
import sys

from ctypes import *

import os
#from types import SimpleNamespace #Only for python 3, replace with class SimpleNamespace for python 27

#Python 27 equivalent implementation of SimpleNamespace
class SimpleNamespace:
    def __init__(self, **kwargs):
        self.__dict__.update(kwargs)

    def __repr__(self):
        keys = sorted(self.__dict__)
        items = ("{}={!r}".format(k, self.__dict__[k]) for k in keys)
        return "{}({})".format(type(self).__name__, ", ".join(items))

    def __eq__(self, other):
        return self.__dict__ == other.__dict__

# 读取配置文件
LotItem_dict = {}
lot_item_globals = {}
exec(compile(open(r'Config.py', "rb").read(), r'Config.py', 'exec'), lot_item_globals, LotItem_dict)
CF = SimpleNamespace(**LotItem_dict)


def main(argv):
    print(CF.limitXMLSize);    # 可以将配置项作为属性使用
    if (CF.limitXMLSize):
        print(CF.maxXMLSizeInEntries);


if __name__ == "__main__":
    main(sys.argv[1:])
    pass

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值