[libvirt] [PATCH 07/22] esx_vi_generator: Simplify get_occurrence_comment

Radostin Stoyanov posted 22 patches 7 years, 3 months ago
[libvirt] [PATCH 07/22] esx_vi_generator: Simplify get_occurrence_comment
Posted by Radostin Stoyanov 7 years, 3 months ago
Reduce the number of if-statements and use a single return.
Utilise a dictionary to map between occurrences and values.

Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
---
 src/esx/esx_vi_generator.py | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/esx/esx_vi_generator.py b/src/esx/esx_vi_generator.py
index 1641a2a1e..e4890c61b 100755
--- a/src/esx/esx_vi_generator.py
+++ b/src/esx/esx_vi_generator.py
@@ -75,16 +75,16 @@ 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 */"
-
-        raise ValueError("unknown occurrence value '%s'" % self.occurrence)
+        occurrence_map = {
+            OCCURRENCE__REQUIRED_ITEM: "/* required */",
+            OCCURRENCE__REQUIRED_LIST: "/* required, list */",
+            OCCURRENCE__OPTIONAL_ITEM: "/* optional */",
+            OCCURRENCE__OPTIONAL_LIST: "/* optional, list */"
+        }
+        if self.occurrence not in occurrence_map:
+            raise ValueError("unknown occurrence value '%s'" % self.occurrence)
+
+        return occurrence_map[self.occurrence]
 
 
 
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 07/22] esx_vi_generator: Simplify get_occurrence_comment
Posted by Daniel P. Berrangé 7 years, 3 months ago
On Sat, Mar 17, 2018 at 02:23:25PM +0000, Radostin Stoyanov wrote:
> Reduce the number of if-statements and use a single return.
> Utilise a dictionary to map between occurrences and values.
> 
> Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
> ---
>  src/esx/esx_vi_generator.py | 20 ++++++++++----------
>  1 file changed, 10 insertions(+), 10 deletions(-)

Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 07/22] esx_vi_generator: Simplify get_occurrence_comment
Posted by Marc Hartmayer 7 years, 3 months ago
On Sat, Mar 17, 2018 at 03:23 PM +0100, Radostin Stoyanov <rstoyanov1@gmail.com> wrote:
> Reduce the number of if-statements and use a single return.
> Utilise a dictionary to map between occurrences and values.
>
> Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
> ---
>  src/esx/esx_vi_generator.py | 20 ++++++++++----------
>  1 file changed, 10 insertions(+), 10 deletions(-)
>
> diff --git a/src/esx/esx_vi_generator.py b/src/esx/esx_vi_generator.py
> index 1641a2a1e..e4890c61b 100755
> --- a/src/esx/esx_vi_generator.py
> +++ b/src/esx/esx_vi_generator.py
> @@ -75,16 +75,16 @@ 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 */"
> -
> -        raise ValueError("unknown occurrence value '%s'" % self.occurrence)
> +        occurrence_map = {
> +            OCCURRENCE__REQUIRED_ITEM: "/* required */",
> +            OCCURRENCE__REQUIRED_LIST: "/* required, list */",
> +            OCCURRENCE__OPTIONAL_ITEM: "/* optional */",
> +            OCCURRENCE__OPTIONAL_LIST: "/* optional, list */"
> +        }
> +        if self.occurrence not in occurrence_map:
> +            raise ValueError("unknown occurrence value '%s'" % self.occurrence)
> +
> +        return occurrence_map[self.occurrence]

What do you think about this? (instead of an explicit check)

try:
   return occurrence_map[self.occurrence]
except KeyError:
   raise ValueError("unknown occurrence value '%s'" % self.occurrence)

In my opinion it’s more “pythonic”.

Beste Grüße / Kind regards
   Marc Hartmayer

IBM Deutschland Research & Development GmbH
Vorsitzende des Aufsichtsrats: Martina Koederitz
Geschäftsführung: Dirk Wittkopp
Sitz der Gesellschaft: Böblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294


--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 07/22] esx_vi_generator: Simplify get_occurrence_comment
Posted by Radostin Stoyanov 7 years, 3 months ago
On 19/03/18 11:40, Marc Hartmayer wrote:
> On Sat, Mar 17, 2018 at 03:23 PM +0100, Radostin Stoyanov <rstoyanov1@gmail.com> wrote:
>> Reduce the number of if-statements and use a single return.
>> Utilise a dictionary to map between occurrences and values.
>>
>> Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
>> ---
>>  src/esx/esx_vi_generator.py | 20 ++++++++++----------
>>  1 file changed, 10 insertions(+), 10 deletions(-)
>>
>> diff --git a/src/esx/esx_vi_generator.py b/src/esx/esx_vi_generator.py
>> index 1641a2a1e..e4890c61b 100755
>> --- a/src/esx/esx_vi_generator.py
>> +++ b/src/esx/esx_vi_generator.py
>> @@ -75,16 +75,16 @@ 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 */"
>> -
>> -        raise ValueError("unknown occurrence value '%s'" % self.occurrence)
>> +        occurrence_map = {
>> +            OCCURRENCE__REQUIRED_ITEM: "/* required */",
>> +            OCCURRENCE__REQUIRED_LIST: "/* required, list */",
>> +            OCCURRENCE__OPTIONAL_ITEM: "/* optional */",
>> +            OCCURRENCE__OPTIONAL_LIST: "/* optional, list */"
>> +        }
>> +        if self.occurrence not in occurrence_map:
>> +            raise ValueError("unknown occurrence value '%s'" % self.occurrence)
>> +
>> +        return occurrence_map[self.occurrence]
> What do you think about this? (instead of an explicit check)
>
> try:
>    return occurrence_map[self.occurrence]
> except KeyError:
>    raise ValueError("unknown occurrence value '%s'" % self.occurrence)
>
> In my opinion it’s more “pythonic”.
Thanks Marc,
Yes, I agree with you. Using try/except here would be better!
> Beste Grüße / Kind regards
>    Marc Hartmayer
>
> IBM Deutschland Research & Development GmbH
> Vorsitzende des Aufsichtsrats: Martina Koederitz
> Geschäftsführung: Dirk Wittkopp
> Sitz der Gesellschaft: Böblingen
> Registergericht: Amtsgericht Stuttgart, HRB 243294
>

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list