On Tue, Sep 14, 2021 at 11:31:43AM +0400, [email protected] wrote:
> Thus I have collection of options and some of them are empty or None.
> In order to get rendered command line options string I use “compress”
> in conjunction with “join":
>
> >>> opts = " ".join(compress(flags, flags))
Why do you (ab)use compress for that?
I understand that `compress(flags, flags)` has the effect of filtering
for non-empty flags. But that's an obfuscated way to write it. Either of
these would be more understandable:
* `filter(None, flags)` # could also use bool instead of None
* `(flag for flag in flags if flag)`
especially the last, although heavy users of functional languages may
prefer filter. But using compress with the same argument twice is just
weird.
And also fragile. You can't use an iterator for the flags.
>>> jin(iter(['a', 'b', '', 'c', 'd', 'e', 'f', '', 'g']))
'a d'
> I usually introduce alias for this:
>
> >>> def jin(a: str, sep=“ “):
> >>> return sep.join(compress(a, a))
>
> And I found that I use it quite frequently.
The signature is wrong. `flags` is a list of string options. If you pass
an actual string, you expand it with spaces:
>>> jin('abc def')
'a b c d e f'
--
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/RPY7HJ4VIUL5PXRBBTCFT6AALXBLUUGF/
Code of Conduct: http://python.org/psf/codeofconduct/