Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Stephen Hansen Newsgroups: comp.lang.python Subject: Re: Whittle it on down Date: Thu, 05 May 2016 00:34:29 -0700 Lines: 49 Message-ID: References: <572ae25f$0$2821$c3e8da3$76491128@news.astraweb.com> <1462430766.25079.598726825.1B90C7A1@webmail.messagingengine.com> <572af09d$0$1508$c3e8da3$5496439d@news.astraweb.com> <1462433669.32454.598751585.63AF2D12@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de rlajCNNylCOZkMsSNhJu5Qi9flHVQkeLw224PDP67FRg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'correct.': 0.07; 'logic': 0.09; 'received:internal': 0.09; 'splitting': 0.09; 'python': 0.10; 'wed,': 0.15; 'thu,': 0.15; '12:04': 0.16; '2016': 0.16; 'cryptic': 0.16; 'enough.': 0.16; 'meh.': 0.16; 'message- id:@webmail.messagingengine.com': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:psf.io': 0.16; 'recognizing': 0.16; 'rejects': 0.16; 'said.': 0.16; 'wrongly': 0.16; 'wrote:': 0.16; 'string': 0.17; 'instance,': 0.18; "shouldn't": 0.18; 'version.': 0.18; 'simpler': 0.22; 'stephen': 0.22; 'am,': 0.23; 'bit': 0.23; 'seems': 0.23; 'matching': 0.23; "python's": 0.23; "haven't": 0.24; 'header:In-Reply-To:1': 0.24; 'otherwise.': 0.27; 'function': 0.28; 'idea': 0.28; 'regular': 0.29; 'accepts': 0.29; 'faster,': 0.29; 'strings,': 0.29; "i'm": 0.30; 'call.': 0.30; 'version,': 0.30; 'extend': 0.31; 'probably': 0.31; 'language.': 0.32; 'problem': 0.33; "d'aprano": 0.33; 'steven': 0.33; "i'll": 0.33; 'definition': 0.34; 'exist': 0.35; 'important.': 0.35; "isn't": 0.35; 'problem.': 0.35; 'sometimes': 0.35; 'but': 0.36; 'smaller': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'really': 0.37; 'received:66': 0.38; 'sure': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'still': 0.40; 'easy': 0.60; 'care': 0.60; 'your': 0.60; 'skip:u 10': 0.61; 'header :Message-Id:1': 0.61; 'more': 0.63; 'thursday': 0.66; 'account': 0.66; 'situation': 0.67; 'customers': 0.70; '95%': 0.84; 'compact,': 0.84; 'distinguish': 0.84; 'greek': 0.84; 'nonsense.': 0.84; 'subject:down': 0.84; 'good,': 0.93 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=ixokai.io; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=2ZNsZyaYRoTAAFJb2ILdN8N1hms=; b=Jykw7h yJJ0ou13BrLjDt8Cx/oL+QHCY0z/UZ/J0y1qlx3l6/CPZumQEgyfVqnSG+jptpEk MeKWSzg8ev0Iizu4rfVq3Fp2gKy+2BJNuYU/IU146XDm4hmjHYRU6kQpewT5cntZ +0cEi44vKZx2tmnpWHNxEC2z/fQlQcP62Gb9g= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=2ZNsZyaYRoTAAFJ b2ILdN8N1hms=; b=XZhgpxajEXh10Ojg6gf8XUVr7et3h/TpzBJTwac5dOCqDK4 mUtchAZ0Zdylv1BUIqhkHiy6Jo2CjogPnWCwvkILippLieoBcWx5dbe+mEJKoNZO nqyBAhYgzz71iQ3jpVbm6u/hypTvfl7+ux5pAs/1zNBOzNxOjh2tkNnsFexw= X-Sasl-Enc: eT+7ALtDTXhfFhhvfoEQ4IjrlCTX62C9hAQGDGko0uuK 1462433669 X-Mailer: MessagingEngine.com Webmail Interface - ajax-9f40a94e In-Reply-To: <572af09d$0$1508$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <1462433669.32454.598751585.63AF2D12@webmail.messagingengine.com> X-Mailman-Original-References: <572ae25f$0$2821$c3e8da3$76491128@news.astraweb.com> <1462430766.25079.598726825.1B90C7A1@webmail.messagingengine.com> <572af09d$0$1508$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:108165 On Thu, May 5, 2016, at 12:04 AM, Steven D'Aprano wrote: > On Thursday 05 May 2016 16:46, Stephen Hansen wrote: > > > On Wed, May 4, 2016, at 11:04 PM, Steven D'Aprano wrote: > >> Start by writing a function or a regex that will distinguish strings t= hat > >> match your conditions from those that don't. A regex might be faster, = but > >> here's a function version. > >> ... snip ... > >=20 > > Yikes. I'm all for the idea that one shouldn't go to regex when Python's > > powerful string type can answer the problem more clearly, but this seems > > to go out of its way to do otherwise. > >=20 > > I don't even care about faster: Its overly complicated. Sometimes a > > regular expression really is the clearest way to solve a problem. >=20 > You're probably right, but I find it easier to reason about matching in=20 > Python rather than the overly terse, cryptic regular expression mini- > language. >=20 > I haven't tested my function version, but I'm 95% sure that it is > correct.=20 > It trickiest part of it is the logic about splitting around ampersands. > And=20 > I'll cheerfully admit that it isn't easy to extend to (say) "ampersand, > or=20 > at signs". But your regex solution: >=20 > r"^[A-Z\s&]+$" >=20 > is much smaller and more compact, but *wrong*. For instance, your regex=20 > wrongly accepts both "&&&&&" and " " as valid strings, and wrongly=20 > rejects "=CE=94=CE=A3=CE=98=CE=9B". Your Greek customers will be sad... Meh. You have a pedantic definition of wrong. Given the inputs, it produced right output. Very often that's enough. Perfect is the enemy of good, it's said.=20 There's no situation where "&&&&&" and " " will exist in the given dataset, and recognizing that is important. You don't have to account for every bit of nonsense.=20 If the OP needs a unicode-aware solution that redefines "A-Z" as perhaps "\w" with an isupper call. Its still far simpler then you're suggesting. --=20 Stephen Hansen m e @ i x o k a i . i o