Python switch-case语句的实现 -- 字典模拟实现

static void
print_asru_status(int status, char *label)
{
    char *msg = NULL;

    switch (status) {
    case 0:
        msg = dgettext("FMD", "ok and in service");
        break;
    case FM_SUSPECT_DEGRADED:
        msg = dgettext("FMD", "service degraded, "
            "but associated components no longer faulty");
        break;
    case FM_SUSPECT_FAULTY | FM_SUSPECT_DEGRADED:
        msg = dgettext("FMD", "faulted but still "
            "providing degraded service");
        break;
    case FM_SUSPECT_FAULTY:
        msg = dgettext("FMD", "faulted but still in service");
        break;
    case FM_SUSPECT_UNUSABLE:
        msg = dgettext("FMD", "out of service, "
            "but associated components no longer faulty");
        break;
    case FM_SUSPECT_FAULTY | FM_SUSPECT_UNUSABLE:
        msg = dgettext("FMD", "faulted and taken out of service");
        break;
    default:
        break;
    }
    if (msg) {
        (void) printf("%s     %s
", label, msg);
    }
}

以上代码是我做项目中遇到的一段代码,要求用Python语言实现,实现代码如下所示:

def print_asru_status(status, label):
    dic = {0:"ok and in service",
        FM_SUSPECT_DEGRADED:"service degraded, but associated components no longer faulty",
        FM_SUSPECT_FAULTY | FM_SUSPECT_DEGRADED:"service degraded, but associated components no longer faulty",
        FM_SUSPECT_FAULTY:"faulted but still in service",
        FM_SUSPECT_UNUSABLE:"out of service, but associated components no longer faulty",
        FM_SUSPECT_FAULTY | FM_SUSPECT_UNUSABLE:"faulted and taken out of service"}
    if status not in dic:
        msg = ""
    else:
        msg = dic[status]
    if msg :
        print(label,"     ", msg)

学习文章:http://www.cnblogs.com/russellluo/p/3299725.html

原文地址:https://www.cnblogs.com/fendou-999/p/3534839.html