From: Radostin Stoyanov Date: Tue, 20 Mar 2018 06:48:50 +0000 (+0000) Subject: esx_vi_generator: Simplify get_occurrence_comment X-Git-Tag: v4.2.0-rc1~83 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b89111afed3484ef9092e38656e68011a3247285;p=thirdparty%2Flibvirt.git esx_vi_generator: Simplify get_occurrence_comment Reduce the number of if-statements and use a single return. Utilise a dictionary to map between occurrences and values. Reviewed-by: Daniel P. Berrangé Signed-off-by: Radostin Stoyanov --- diff --git a/src/esx/esx_vi_generator.py b/src/esx/esx_vi_generator.py index 95521fa1e5..df913d8915 100755 --- a/src/esx/esx_vi_generator.py +++ b/src/esx/esx_vi_generator.py @@ -75,16 +75,17 @@ class Member: def get_occurrence_comment(self): - if self.occurrence == OCCURRENCE__REQUIRED_ITEM: - return "/* required */" - elif self.occurrence == OCCURRENCE__REQUIRED_LIST: - return "/* required, list */" - elif self.occurrence == OCCURRENCE__OPTIONAL_ITEM: - return "/* optional */" - elif self.occurrence == OCCURRENCE__OPTIONAL_LIST: - return "/* optional, list */" + occurrence_map = { + OCCURRENCE__REQUIRED_ITEM: "/* required */", + OCCURRENCE__REQUIRED_LIST: "/* required, list */", + OCCURRENCE__OPTIONAL_ITEM: "/* optional */", + OCCURRENCE__OPTIONAL_LIST: "/* optional, list */" + } + try: + return occurrence_map[self.occurrence] + except KeyError: + raise ValueError("unknown occurrence value '%s'" % self.occurrence) - raise ValueError("unknown occurrence value '%s'" % self.occurrence)