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

Gedare Bloom gedare at rtems.org
Wed Feb 17 16:26:42 UTC 2021


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.

> Best regards
> André
>
> -----Ursprüngliche Nachricht-----
> Von: Chris Johns <chrisj at rtems.org>
> Gesendet: Dienstag, 16. Februar 2021 21:29
> An: Sommer, Jan <Jan.Sommer at dlr.de>; devel at rtems.org
> Cc: Nahrwold, Andre <Andre.Nahrwold at dlr.de>
> Betreff: Re: [PATCH 1/1] misc: tools: fix mkimage.py script type processing
>
> On 12/2/21 3:23 am, Jan Sommer wrote:
> > From: Andre Nahrwold <andre.nahrwold at dlr.de>
> >
> > ---
> >  misc/tools/mkimage.py | 10 ++++++++++
> >  1 file changed, 10 insertions(+)
> >
> > diff --git a/misc/tools/mkimage.py b/misc/tools/mkimage.py index
> > fd75f0a..111e224 100755
> > --- a/misc/tools/mkimage.py
> > +++ b/misc/tools/mkimage.py
> > @@ -121,6 +121,16 @@ outputfile.seek(struct.size);
> >
> >  inputcrc = 0;
> >
> > +if options.type in 'script':
>
> I saw this and thought it might be wrong and now I think it is. Should this line be:
>
>  > +if options.type in ['script']:
>
> ?
>
> Chris
> _______________________________________________
> devel mailing list
> devel at rtems.org
> http://lists.rtems.org/mailman/listinfo/devel


More information about the devel mailing list