Seebs <usenet-nos...@seebs.net> writes:

> On 2010-11-09, Ben Finney <ben+pyt...@benfinney.id.au> wrote:
> > The regex is less clear for the purpose than I'd prefer. For a
> > simple ???is it a member of this small set???, I'd find it more
> > readable to use a simple list of the actual strings::
>
> >     ' '.join(
> >         x for x in target_cflags.split()
> >         if x in ['-D', '-I', '-i', '-U'])
>
> The regex is intentionally not anchored with a $, because I'm looking
> for "starts with", not "is".

Ah, okay. Strings have a built-in ‘startswith’ method, but it may be
less convenient for the current case compared to a regex. Here's an
attempt::

    ' '.join(
        x for x in target_cflags.split()
        if any(
            x.startswith(opt_name)
            for opt_name in ['-D', '-I', '-i', '-U']))

I still find that more readable than the version using a regex, but
it's pushing the boundaries of verbosity.

Better would be to use the fact that ‘str.startswith’ can do the same as
above with a tuple of prefix strings::

    ' '.join(
        x for x in target_cflags.split()
        if x.startswith(('-D', '-I', '-i', '-U')))

> I think we're stuck with backwards compatibility at least as far as
> 2.4.

Then you don't yet have the ‘any’ and ‘all’ built-in functions, or the
tuple-of-prefixes feature of ‘str.startswith’ either. Bummer.

At which point, the Pythonic thing to do is to convince your
organisation to use a version of Python that's at least officially
supported by the PSF :-)

-- 
 \     “Teach a man to make fire, and he will be warm for a day. Set a |
  `\       man on fire, and he will be warm for the rest of his life.” |
_o__)                                                 —John A. Hrastar |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to