Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #13064 > unrolled thread
| Started by | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| First post | 2011-09-10 04:20 -0700 |
| Last post | 2011-09-11 12:37 -0700 |
| Articles | 5 on this page of 25 — 12 participants |
Back to article view | Back to comp.lang.python
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
Page 2 of 2 — ← Prev page 1 [2]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-09-10 22:49 +0200 |
| Message-ID | <mailman.950.1315687750.27778.python-list@python.org> |
| In reply to | #13064 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2011-09-10 16:25 -0700 |
| Message-ID | <mailman.953.1315697157.27778.python-list@python.org> |
| In reply to | #13064 |
On Sat, 10 Sep 2011 04:20:17 -0700 (PDT), Tigerstyle
<laddosingh@gmail.com> declaimed the following in
gmane.comp.python.general:
> title_split = title.strip().lower().split()
> for word in title_split:
> if title_split[0] in small_words:
> new_title.append(word.title())
title_split[0] will never change, so if it is one of the small
words, then all subsequent words will also be treated to this branch.
> elif word in small_words:
> new_title.append(word.lower())
You already lowercased all the words before splitting, so this is
applying lowercase to a known lowercase word
> else:
> new_title.append(word.title())
> return(' '.join(new_title))
>
I'd suggest looking up the definition of
enumerate()
in the language documentation... It will give you a simple way to know
if you are looking at the first word. Basically, you want to title-case
the word IF it is the first word OR the word is NOT in the list of
lowercase words; anything else goes through as lower case...
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2011-09-10 23:18 -0700 |
| Message-ID | <mailman.972.1315721914.27778.python-list@python.org> |
| In reply to | #13064 |
On Sat, 10 Sep 2011 16:25:42 -0700, Dennis Lee Bieber
<wlfraed@ix.netcom.com> declaimed the following in
gmane.comp.python.general:
>
> in the language documentation... It will give you a simple way to know
> if you are looking at the first word. Basically, you want to title-case
> the word IF it is the first word OR the word is NOT in the list of
> lowercase words; anything else goes through as lower case...
Of course, most of this can be done in a single line (including
taking into account that some words may have a punctuation mark which
would confuse the original).
>>> smalls = ['into', 'the', 'a', 'of', 'at', 'in', 'for', 'on' ]
>>> punct = ".,;:?!;'\"(){}[]"
>>> def recase(str = "physicist odd-affection, or how i was taught to stop fretting and adore the weapon of mass destruction"):
... return " ".join( w.title() if i == 0 or w.strip(punct) not in
smalls else w
... for i,w in enumerate(str.lower().strip().split()) )
...
>>> recase()
'Physicist Odd-Affection, Or How I Was Taught To Stop Fretting And Adore
the Weapon of Mass Destruction'
>>> recase("what? me worry?")
'What? Me Worry?'
>>> recase("the end of the matter is, to be blunt, a confusion")
'The End of the Matter Is, To Be Blunt, a Confusion'
>>> recase("the end of the matter is in, to be blunt, a confusion")
'The End of the Matter Is in, To Be Blunt, a Confusion'
>>> smalls = ['into', 'the', 'a', 'of', 'at', 'in', 'for', 'on', "and", "is", "to" ]
>>> recase()
'Physicist Odd-Affection, Or How I Was Taught To Stop Fretting and Adore
the Weapon of Mass Destruction'
>>> recase("the end of the matter is in, to be blunt, a confusion")
'The End of the Matter is in, to Be Blunt, a Confusion'
>>>
Of course, explaining what this construct is doing is the trick to
justifying it for a homework assignment.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2011-09-11 09:43 -0700 |
| Message-ID | <208f1e6b-66ca-4988-a41a-d83924cb8593@m18g2000vbe.googlegroups.com> |
| In reply to | #13117 |
On 11 Sep, 08:18, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
> On Sat, 10 Sep 2011 16:25:42 -0700, Dennis Lee Bieber
> <wlfr...@ix.netcom.com> declaimed the following in
> gmane.comp.python.general:
>
>
>
> > in the language documentation... It will give you a simple way to know
> > if you are looking at the first word. Basically, you want to title-case
> > the word IF it is the first word OR the word is NOT in the list of
> > lowercase words; anything else goes through as lower case...
>
> Of course, most of this can be done in a single line (including
> taking into account that some words may have a punctuation mark which
> would confuse the original).
>
> >>> smalls = ['into', 'the', 'a', 'of', 'at', 'in', 'for', 'on' ]
> >>> punct = ".,;:?!;'\"(){}[]"
> >>> def recase(str = "physicist odd-affection, or how i was taught to stop fretting and adore the weapon of mass destruction"):
>
> ... return " ".join( w.title() if i == 0 or w.strip(punct) not in
> smalls else w
> ... for i,w in enumerate(str.lower().strip().split()) )
> ...>>> recase()
>
> 'Physicist Odd-Affection, Or How I Was Taught To Stop Fretting And Adore
> the Weapon of Mass Destruction'>>> recase("what? me worry?")
> 'What? Me Worry?'
> >>> recase("the end of the matter is, to be blunt, a confusion")
>
> 'The End of the Matter Is, To Be Blunt, a Confusion'>>> recase("the end of the matter is in, to be blunt, a confusion")
>
> 'The End of the Matter Is in, To Be Blunt, a Confusion'>>> smalls = ['into', 'the', 'a', 'of', 'at', 'in', 'for', 'on', "and", "is", "to" ]
> >>> recase()
>
> 'Physicist Odd-Affection, Or How I Was Taught To Stop Fretting and Adore
> the Weapon of Mass Destruction'>>> recase("the end of the matter is in, to be blunt, a confusion")
>
> 'The End of the Matter is in, to Be Blunt, a Confusion'
>
>
>
> Of course, explaining what this construct is doing is the trick to
> justifying it for a homework assignment.
> --
> Wulfraed Dennis Lee Bieber AF6VN
> wlfr...@ix.netcom.com HTTP://wlfraed.home.netcom.com/
Too much destruction in this post man, and yeah I would not be able to
explain the code for my homework.
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2011-09-11 12:37 -0700 |
| Message-ID | <mailman.1003.1315769852.27778.python-list@python.org> |
| In reply to | #13135 |
On Sun, 11 Sep 2011 09:43:31 -0700 (PDT), Tigerstyle
<laddosingh@gmail.com> declaimed the following in
gmane.comp.python.general:
> Too much destruction in this post man, and yeah I would not be able to
> explain the code for my homework.
The whole plan in posting it... Something for independent study <G>
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.python
csiph-web