Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #13083

Re: Doctest failing

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'terry': 0.07; 'first:': 0.09; 'loop.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'am,': 0.12; 'stored': 0.13; 'def': 0.15; '*before*': 0.16; 'idioms': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'reedy': 0.16; 'titlecased': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'helper': 0.23; 'testing': 0.24; 'variable': 0.24; "i'm": 0.27; 'skip:" 30': 0.28; 'yield': 0.29; 'lines': 0.30; 'from:addr:web.de': 0.30; "skip:' 10": 0.30; 'usually': 0.32; 'too': 0.33; 'to:addr:python-list': 0.33; 'be.': 0.34; 'flag': 0.34; 'header:X-Complaints-To:1': 0.35; 'assuming': 0.37; 'checks': 0.37; 'thread': 0.37; 'response': 0.37; 'put': 0.37; 'but': 0.37; 'think': 0.38; 'received:org': 0.38; 'subject:: ': 0.39; 'header:Mime-Version:1': 0.39; 'else': 0.39; 'to:addr:python.org': 0.39; 'more': 0.60; 'alternative': 0.64; 'here': 0.65; 'making': 0.67; 'special': 0.67; 'namely': 0.84; 'otten': 0.84; 'treatment': 0.96
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Doctest failing
Date Sat, 10 Sep 2011 22:49:13 +0200
Organization None
References <678b89fd-82f4-4080-8b36-e4d0ba30cdbf@o9g2000vbo.googlegroups.com> <j4fin9$ibm$2@dough.gmane.org> <j4gea8$m3b$2@dough.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084ac98.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.950.1315687750.27778.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1315687750 news.xs4all.nl 2453 [2001:888:2000:d::a6]:57348
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:13083

Show key headers only | View raw


Terry Reedy wrote:

> On 9/10/2011 7:47 AM, Peter Otten wrote:
> 
>> You can work around that with a
>> flag along these lines
>>
>> first = True
>> for word in title_split:
>>      if first:
>>          # special treatment for the first word
>>          first = False
>>      else:
>>          # put checks for all words but the first here
>>      new_title.append(fixed_word) # assuming you have stored the
>>      titlecased
>>                                   # or lowercased word in the fixed_word
>>                                   # variable
> 
> An alternative to a flag and testing every item is to remove and process
> the first item *before* the loop. See my response on this thread or my
> new thread
> Idioms combining 'next(items)' and 'for item in items:'

I reckoned the approach with the flag the most beginner-friendly because you 
don't have to think too hard about the corner-cases, namely

>>> book_title("")
''

When I use the "process first item before the loop" approach I usually end 
up with a helper generator

def _words(words, small_words={w.title(): w for w in small_words}):
    yield next(words)
    for word in words:
        yield small_words[word] if word in small_words else word

def book_title(s):
    return " ".join(_words(iter(s.title().split())))

and the nagging thought that I'm making it more complex than need be.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-10 04:20 -0700
  Re: Doctest failing Mel <mwilson@the-wire.com> - 2011-09-10 07:43 -0400
    Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:36 -0700
  Re: Doctest failing Peter Otten <__peter__@web.de> - 2011-09-10 13:47 +0200
    Re: Doctest failing ting@thsu.org - 2011-09-10 19:12 -0700
      Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:42 -0700
  Re: Doctest failing Thomas Jollans <t@jollybox.de> - 2011-09-10 13:50 +0200
    Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:39 -0700
  Re: Doctest failing Alister Ware <alister.ware@ntlworld.com> - 2011-09-10 12:24 +0000
    Re: Doctest failing Chris Angelico <rosuav@gmail.com> - 2011-09-11 01:56 +1000
      Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:40 -0700
    Re: Doctest failing Ethan Furman <ethan@stoneleaf.us> - 2011-09-11 11:43 -0700
    Re: Doctest failing Chris Angelico <rosuav@gmail.com> - 2011-09-12 11:03 +1000
      Re: Doctest failing Ben Finney <ben+python@benfinney.id.au> - 2011-09-12 11:37 +1000
        Re: Doctest failing Chris Angelico <rosuav@gmail.com> - 2011-09-12 13:06 +1000
          Re: Doctest failing Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-09-12 14:29 +1000
  Re: Doctest failing Terry Reedy <tjreedy@udel.edu> - 2011-09-10 13:59 -0400
    Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 04:46 -0700
      Re: Doctest failing Terry Reedy <tjreedy@udel.edu> - 2011-09-11 13:03 -0400
  Re: Doctest failing Terry Reedy <tjreedy@udel.edu> - 2011-09-10 15:36 -0400
  Re: Doctest failing Peter Otten <__peter__@web.de> - 2011-09-10 22:49 +0200
  Re: Doctest failing Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-09-10 16:25 -0700
  Re: Doctest failing Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-09-10 23:18 -0700
    Re: Doctest failing Tigerstyle <laddosingh@gmail.com> - 2011-09-11 09:43 -0700
      Re: Doctest failing Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2011-09-11 12:37 -0700

csiph-web