PythonTip >> 博文 >> python

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

zihua 2014-01-27 15:01:32 点击: 940 | 收藏


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

原文链接:http://www.tuicool.com/articles/qI7Rb2

作者:zihua | 分类: python | 标签: python | 阅读: 940 | 发布于: 2014-01-27 15时 |