简体   繁体   English

我如何在 python 中实现它?

[英]How can I implement this in python?

I have string list as below,我有如下字符串列表,

flag_list = ['-Werror=unused-but-set-variable', '-Wall', '-D FIRMWARE_MAJOR_VERSON=0', '-D FIRMWARE_MINOR_VERSION=1', '-D _TASK_STD_FUNCTION', '-D DEBUG_ENABLE=1', '-D
ENABLE_DEBUG_MAIN=1', '-D ENABLE_DEBUG_OLED_UI=1', '-D ENABLE_TEST_FEATURES=1', '-D LED=1', '-DSERIAL_NUMBER=1234', '-DREQUIRESNEW']

I am trying to implement a function that helps me get the value of the defined flag if available.我正在尝试实现一个函数,该函数可帮助我获取已定义标志的值(如果可用)。

Here is what I have tried, I am sure that this is not optimal way of doing this.这是我尝试过的,我确信这不是这样做的最佳方式。

def get_flag_value(flags_list, flag_name):
    flag = [s for s in flags if flag_name + "=" in s]
    flag_new = filter(lambda str: str.startswith("-D"), flag)
    print flag_new
    if (len(flag) == 1) :
        print flag_name + " found."
        print flag
        # TODO: Check for the = sign.
        # TODO: get value of parameter if available.
        # return (result, value)
    elif (len(flag) > 1) :
        print "Multiple enteries found"
        print flag
        return (false, 0)
    else:
        print flag_name + " not found"
        return (false, 0)   

please note that sometimes -D may not have the tailing space all the time.请注意,有时-D可能不会一直有拖尾空间。

I have implemented a code.我已经实现了一个代码。 You can try it.你可以试试看。

Code:代码:

input_list = [
    "-Werror=unused-but-set-variable",
    "-Wall",
    "-D FIRMWARE_MAJOR_VERSON=0",
    "-D FIRMWARE_MINOR_VERSION=1",
    "-D _TASK_STD_FUNCTION",
    "-D DEBUG_ENABLE=1",
    "-D ENABLE_DEBUG_MAIN=1",
    "-D ENABLE_DEBUG_OLED_UI=1",
    "-D ENABLE_TEST_FEATURES=1",
    "-D LED=1",
    "-DSERIAL_NUMBER=1234",
    "-DREQUIRESNEW",
]


def get_flag_value(flag_name):
    for item in input_list:
        if flag_name in item:
            return True, item.split("=")[-1]
    return False, False


print(get_flag_value("ENABLE_DEBUG_OLED_UI"))
print(get_flag_value("SERIAL_NUMBER"))
print(get_flag_value("Wall"))
print(get_flag_value("Werror"))
print(get_flag_value("Fake_flag"))

Output:输出:

>>> python3 test.py 
(True, '1')
(True, '1234')
(True, '-Wall')
(True, 'unused-but-set-variable')
(False, False)

EDIT:编辑:

Make the function to more robust.使函数更加健壮。

Code:代码:

def get_flag_value(flag_name):
    for item in input_list:
        item = item.replace("-D", "").strip()
        if flag_name.lower() == item.split("=")[0].lower():
            return True, item.split("=")[-1]
    return False, False


print(get_flag_value("ENABLE_DEBUG_OLED_UI"))
print(get_flag_value("SERIAL_NUMBER"))
print(get_flag_value("-Wall"))
print(get_flag_value("-Werror"))
print(get_flag_value("Fake_flag"))
print(get_flag_value("LED"))
print(get_flag_value("REQUIRESNEW"))

Output:输出:

>>> python3 test.py 
(True, '1')
(True, '1234')
(True, '-Wall')
(True, 'unused-but-set-variable')
(False, False)
(True, '1')
(True, 'REQUIRESNEW')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM