On 09/15/2016 11:16 AM, Sascha Silbe wrote:

>>> +        if self.device_name is not None:
>>> +            result = self.vm.qmp('blockdev-change-medium',
>>> +                                 id=self.device_name, filename=new_img,
>>> +                                 format=iotests.imgfmt)
>>> +        else:
>>> +            result = self.vm.qmp('blockdev-change-medium',
>>> +                                 device='drive0', filename=new_img,
>>> +                                 format=iotests.imgfmt)
>>
>> I'm not enough of a python guru to know if there is any way to compress
>> this to a shorter format (I do know, however, that the lack of an
>> obvious ?: operator in python can indeed result in verbose if/else
>> clauses compared to other languages).
> 
> Actually there is a direct equivalent of "?:" (which unfortunately isn't
> in C11) in Python: "or". So you can use:
> 
>         result = self.vm.qmp('blockdev-change-medium',
>                              id=self.device_name or 'drive0', 
> filename=new_img,

Not what we want.  We want to use a different parameter, based on the
overall condition (so it is NOT as simple as a conditional assignment to
the 'id' parameter, but an actual swap between whether we have the 'id'
or the 'device' parameter).  It's not obvious whether:

result = foo(id=self.device_name,
             device='drive0' if not self.device_name else None)

would have the same semantics as:

if self.device_name:
    result = foo(id=self.device_name)
else
    result = foo(device='drive0')

and even if it does have the same semantics, it's not obvious if there's
a shorter way to write it.  So we end up with verbose duplication of all
the remaining parameters.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to