Multiple equates

Cameron Laird claird at lairds.us
Tue Dec 2 20:23:53 EST 2008


In article <gh4e6n$pn$1 at lust.ihug.co.nz>,
Lawrence D'Oliveiro  <ldo at geek-central.gen.new_zealand> wrote:
>In message <j4svv5-gvp.ln1 at lairds.us>, Cameron Laird wrote:
>
>> In article <ggg3oe$vfe$2 at lust.ihug.co.nz>,
>> Lawrence D'Oliveiro  <ldo at geek-central.gen.new_zealand> wrote:
>>
>>>Cameron Laird wrote:
>>>
>>>> I've been trying to decide if there's any sober reason to advocate
>>>> the one-liner
>>>> 
>>>>     map(lambda i: a.__setitem__(i, False), [x1, x2, x3, ..., x1024])
>>>
>>>Are lambdas like the Dark Side of Python?
>>>
>>>:)
>> 
>> Enough so, apparently, that I'm reluctant even to touch that question.
>
>So how else would you express something like
>
>    def shell_escape(Arg) :
>        """returns Arg suitably escaped for use as a command-line argument
>        to Bash."""
>        return \
>            re.sub \
>              (
>                r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]",
>                lambda Match : "\\" + Match.group(0),
>                Arg
>              )
>              # Need to catch anything that might be meaningful to shell
>    #end shell_escape
>
>?

I suspect we're confusing each other.  I *like* lambdas--at least,
more than Guido does, which I recognize is a low standard.

When I take your question at face value, my response is

   def shell_escape(Arg) :
       """returns Arg suitably escaped for use as a command-line argument
       to Bash."""

       pattern = r"[\<\>\"\'\|\&\$\#\;\(\)\[\]\{\}\`\!\~\ \\]"
       def f1(Match):
	   return 
       return re.sub(pattern, f1, Arg)
             # Need to catch anything that might be meaningful to shell
   #end shell_escape

'cept that I'd hope to find a way to simplify pattern.  Was that
what you were asking?



More information about the Python-list mailing list