[libvirt] [PATCH 05/22] esx_vi_generator: Simplify generate_helper_source

Radostin Stoyanov posted 22 patches 7 years, 3 months ago
[libvirt] [PATCH 05/22] esx_vi_generator: Simplify generate_helper_source
Posted by Radostin Stoyanov 7 years, 3 months ago
The generate_helper_source() function returns a formatted string.
This could be achieved without the use of a local variable "source"
and string concatenation.

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

diff --git a/src/esx/esx_vi_generator.py b/src/esx/esx_vi_generator.py
index 8fbc8bef1..19c225384 100755
--- a/src/esx/esx_vi_generator.py
+++ b/src/esx/esx_vi_generator.py
@@ -1092,26 +1092,17 @@ class ManagedObject(GenericObject):
 
 
     def generate_helper_source(self):
-        source = ""
-
         # lookup
-        source += "/* esxVI_Lookup%s */\n" % self.name
-        source += "ESX_VI__TEMPLATE__LOOKUP(%s,\n" % self.name
-        source += "{\n"
-
-        source += self.generate_lookup_code1()
-
-        source += "},\n"
-        source += "{\n"
-
-        source += self.generate_lookup_code2()
-
-        source += "})\n\n"
-
-        source += "\n\n"
-
-        return source
-
+        return (
+            "/* esxVI_Lookup{name} */\n"
+            "ESX_VI__TEMPLATE__LOOKUP({name},\n"
+            "{{\n{lookup_code1}}},\n"
+            "{{\n{lookup_code2}}})\n\n\n\n"
+        ).format(
+            name=self.name,
+            lookup_code1=self.generate_lookup_code1(),
+            lookup_code2=self.generate_lookup_code2()
+        )
 
 
 class Enum(Type):
-- 
2.14.3

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH 05/22] esx_vi_generator: Simplify generate_helper_source
Posted by Daniel P. Berrangé 7 years, 3 months ago
On Sat, Mar 17, 2018 at 02:23:23PM +0000, Radostin Stoyanov wrote:
> The generate_helper_source() function returns a formatted string.
> This could be achieved without the use of a local variable "source"
> and string concatenation.
> 
> Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
> ---
>  src/esx/esx_vi_generator.py | 29 ++++++++++-------------------
>  1 file changed, 10 insertions(+), 19 deletions(-)
> 
> diff --git a/src/esx/esx_vi_generator.py b/src/esx/esx_vi_generator.py
> index 8fbc8bef1..19c225384 100755
> --- a/src/esx/esx_vi_generator.py
> +++ b/src/esx/esx_vi_generator.py
> @@ -1092,26 +1092,17 @@ class ManagedObject(GenericObject):
>  
>  
>      def generate_helper_source(self):
> -        source = ""
> -
>          # lookup
> -        source += "/* esxVI_Lookup%s */\n" % self.name
> -        source += "ESX_VI__TEMPLATE__LOOKUP(%s,\n" % self.name
> -        source += "{\n"
> -
> -        source += self.generate_lookup_code1()
> -
> -        source += "},\n"
> -        source += "{\n"
> -
> -        source += self.generate_lookup_code2()
> -
> -        source += "})\n\n"
> -
> -        source += "\n\n"
> -
> -        return source
> -
> +        return (
> +            "/* esxVI_Lookup{name} */\n"
> +            "ESX_VI__TEMPLATE__LOOKUP({name},\n"
> +            "{{\n{lookup_code1}}},\n"
> +            "{{\n{lookup_code2}}})\n\n\n\n"

Most of our code uses the traditional printf style format
variables, and I think that would be nicer here, because
of the usage of literal "{"  in places

It woukd be clear to break strings everywhere there is
a newline, to make it resemble the final generated code
structure better.

> +        ).format(
> +            name=self.name,
> +            lookup_code1=self.generate_lookup_code1(),
> +            lookup_code2=self.generate_lookup_code2()
> +        )
>  
>  
>  class Enum(Type):
> -- 
> 2.14.3
> 
> --
> libvir-list mailing list
> libvir-list@redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list

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 05/22] esx_vi_generator: Simplify generate_helper_source
Posted by Radostin Stoyanov 7 years, 3 months ago

On 19/03/18 10:42, Daniel P. Berrangé wrote:
> On Sat, Mar 17, 2018 at 02:23:23PM +0000, Radostin Stoyanov wrote:
>> The generate_helper_source() function returns a formatted string.
>> This could be achieved without the use of a local variable "source"
>> and string concatenation.
>>
>> Signed-off-by: Radostin Stoyanov <rstoyanov1@gmail.com>
>> ---
>>  src/esx/esx_vi_generator.py | 29 ++++++++++-------------------
>>  1 file changed, 10 insertions(+), 19 deletions(-)
>>
>> diff --git a/src/esx/esx_vi_generator.py b/src/esx/esx_vi_generator.py
>> index 8fbc8bef1..19c225384 100755
>> --- a/src/esx/esx_vi_generator.py
>> +++ b/src/esx/esx_vi_generator.py
>> @@ -1092,26 +1092,17 @@ class ManagedObject(GenericObject):
>>  
>>  
>>      def generate_helper_source(self):
>> -        source = ""
>> -
>>          # lookup
>> -        source += "/* esxVI_Lookup%s */\n" % self.name
>> -        source += "ESX_VI__TEMPLATE__LOOKUP(%s,\n" % self.name
>> -        source += "{\n"
>> -
>> -        source += self.generate_lookup_code1()
>> -
>> -        source += "},\n"
>> -        source += "{\n"
>> -
>> -        source += self.generate_lookup_code2()
>> -
>> -        source += "})\n\n"
>> -
>> -        source += "\n\n"
>> -
>> -        return source
>> -
>> +        return (
>> +            "/* esxVI_Lookup{name} */\n"
>> +            "ESX_VI__TEMPLATE__LOOKUP({name},\n"
>> +            "{{\n{lookup_code1}}},\n"
>> +            "{{\n{lookup_code2}}})\n\n\n\n"
> Most of our code uses the traditional printf style format
> variables, and I think that would be nicer here, because
> of the usage of literal "{"  in places
>
> It woukd be clear to break strings everywhere there is
> a newline, to make it resemble the final generated code
> structure better.
Thanks, I will resend this and the next patch with preserving the
traditional printf style format, and break strings everywhere there is a
newline.
>> +        ).format(
>> +            name=self.name,
>> +            lookup_code1=self.generate_lookup_code1(),
>> +            lookup_code2=self.generate_lookup_code2()
>> +        )
>>  
>>  
>>  class Enum(Type):
>> -- 
>> 2.14.3
>>
>> --
>> libvir-list mailing list
>> libvir-list@redhat.com
>> https://www.redhat.com/mailman/listinfo/libvir-list
> Regards,
> Daniel

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