[PATCH 1/1] misc: tools: fix mkimage.py script type processing

Chris Johns chrisj at rtems.org
Wed Feb 17 19:15:02 UTC 2021


On 18/2/21 3:26 am, Gedare Bloom wrote:
> On Wed, Feb 17, 2021 at 12:35 AM <Andre.Nahrwold at dlr.de> wrote:
>>
>> Hi Chris,
>>
>> as far as my understanding of python goes this does not make any difference.
>> Strings are essentially arrays in python which would make the parenthesis obsolete.
>>
>> When this condition should catch another type in the future it would be good practice to do something like this:
>>
>> If options.type in ['script', '<other-type>']:
>>
>> Even though the following would be functional the same but obviously much less readable and logical:
>>
>> If options.type in 'script<other-type>':
>>
> I don't quite agree.
> 
> $ python3
> Python 3.8.5 (default, Jul 28 2020, 12:59:40)
> [GCC 9.3.0] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> ff='s'
>>>> if ff in 'script':
> ...   print("blah")
> ...
> blah
>>>> if ff in ['script']:
> ...   print("blah")
> ...
>>>>
> 
> You need the list, or else the "in" operator iterates the string as a
> character checking for membership, which can match on single
> characters. Only if you can guarantee options.type is also a string
> can you omit the list tokens. It is also more future-proof for adding
> other options.

Thanks for this. The match also covers substrings so characters match and
strings like `cr` or `ipt` also match and this is not what we want.

>>> s = 'c'






>>> if s in 'script':
...     print('blah')






...
blah
>>> s = 'cr'






>>> if s in 'script':
...     print('blah')






...
blah
>>> s = 'ipt'






>>> if s in 'script':
...     print('blah')






...
blah

Chris


More information about the devel mailing list