trying to match a string

John Machin sjmachin at lexicon.net
Sun Jul 20 07:18:31 CEST 2008


On Jul 20, 11:14 am, Andrew Freeman <alif... at gmail.com> wrote:
> John Machin wrote:

> (4) I highly doubt that this code was actually to be used in an
> interactive session,

The offending code is a nonsense wherever it is used.

> the False/True output was truncated intentionally,

What meaning are you attaching to "truncated"?

> it's an obvious, but superfluous output (unless you were to rely on this
> by attaching it to a variable which might lead to sorting issues).
>
> I put together a generic matcher that returns either a list of True data
> (if the input is a list or tuple) or a boolean value:
>
> def match(ex, var):
>     "ex is the regular expression to match for, var the iterable or
> string to return a list of matching items or a boolean value respectively."
>     ex = re.compile(ex).match

You lose clarity by rebinding ex like that, and you gain nothing.


>     if isinstance(var, (list, tuple)):
>         return filter(ex, var)
>     else:
>         return bool(ex(var))
>
> I believe this is fairly clean and succinct code, it would help my
> learning immensely if you feel there is a more succinct, generic way of
> writing this function.

You have created a function which does two quite different things
depending on whether one of the arguments is one of only two of the
many kinds of iterables and which has a rather generic (match what?)
and misleading (something which filters matches is called "match"??)
name. The loss of clarity and ease of understanding caused by the
readers having to find the code for the function so that they can
puzzle through it means that the most succinct, generic and
*recommended* way of writing this function would be not to write it at
all.

Write a function which returns a MatchObject. In the unlikely event
that that anyone really wants to put bools in a list and sort them,
then they can wrap bool() around it. Give it a meaningful name e.g.
match_LRM.

You want to check if a single variable refers to a valid LRM string?
Use match_LRM(the_variable). Nice and clear.

You want to filter out of some iterable all the occurrences of valid
LRM strings? Use filter (whose name indicates its task) or a generator
or list comprehension ... what [x for x in some_iterable if
match_LRM(x)] does should be screamingly obvious i.e. have less chance
than filter of needing a trip to the manual.

HTH,
John



More information about the Python-list mailing list