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\n", 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

posted @ 2014-01-27 14:14  fendou999  阅读(1195)  评论(0编辑  收藏  举报