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 | 20 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 1 of 2 [1] 2 Next page →
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2011-09-10 04:20 -0700 |
| Subject | Doctest failing |
| Message-ID | <678b89fd-82f4-4080-8b36-e4d0ba30cdbf@o9g2000vbo.googlegroups.com> |
Hi guys.
I'm strugglin with some homework stuff and am hoping you can help me
out here.
This is the code:
small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
def book_title(title):
""" Takes a string and returns a title-case string.
All words EXCEPT for small words are made title case
unless the string starts with a preposition, in which
case the word is correctly capitalized.
>>> book_title('DIVE Into python')
'Dive into Python'
>>> book_title('the great gatsby')
'The Great Gatsby'
>>> book_title('the WORKS OF AleXANDer dumas')
'The Works of Alexander Dumas'
"""
new_title = []
title_split = title.strip().lower().split()
for word in title_split:
if title_split[0] in small_words:
new_title.append(word.title())
elif word in small_words:
new_title.append(word.lower())
else:
new_title.append(word.title())
return(' '.join(new_title))
def _test():
import doctest, refactory
return doctest.testmod(refactory)
if __name__ == "__main__":
_test()
All tests are failing even though I am getting the correct output on
the first two tests. And the last test still gives me "Of" instead of
"of"
Any help is appreciated.
Rgds
T
[toc] | [next] | [standalone]
| From | Mel <mwilson@the-wire.com> |
|---|---|
| Date | 2011-09-10 07:43 -0400 |
| Message-ID | <j4fihi$5ao$1@speranza.aioe.org> |
| In reply to | #13064 |
Tigerstyle wrote:
> Hi guys.
>
> I'm strugglin with some homework stuff and am hoping you can help me
> out here.
>
> This is the code:
>
> small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
>
> def book_title(title):
> """ Takes a string and returns a title-case string.
> All words EXCEPT for small words are made title case
> unless the string starts with a preposition, in which
> case the word is correctly capitalized.
> >>> book_title('DIVE Into python')
> 'Dive into Python'
> >>> book_title('the great gatsby')
> 'The Great Gatsby'
> >>> book_title('the WORKS OF AleXANDer dumas')
> 'The Works of Alexander Dumas'
> """
> new_title = []
> title_split = title.strip().lower().split()
> for word in title_split:
> if title_split[0] in small_words:
> new_title.append(word.title())
> elif word in small_words:
> new_title.append(word.lower())
> else:
> new_title.append(word.title())
> return(' '.join(new_title))
>
> def _test():
> import doctest, refactory
> return doctest.testmod(refactory)
> if __name__ == "__main__":
> _test()
>
> All tests are failing even though I am getting the correct output on
> the first two tests. And the last test still gives me "Of" instead of
> "of"
>
> Any help is appreciated.
I don't know about doctest -- I suspect it wants a structured docstring to
specify the tests -- but this
if title_split[0] in small_words:
new_title.append(word.title())
can't be what you want.
Mel.
[toc] | [prev] | [next] | [standalone]
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2011-09-11 09:36 -0700 |
| Message-ID | <e86efe59-25ee-48d4-a201-4a67053a51c2@bl1g2000vbb.googlegroups.com> |
| In reply to | #13065 |
On 10 Sep, 13:43, Mel <mwil...@the-wire.com> wrote:
> Tigerstyle wrote:
> > Hi guys.
>
> > I'm strugglin with some homework stuff and am hoping you can help me
> > out here.
>
> > This is the code:
>
> > small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
>
> > def book_title(title):
> > """ Takes a string and returns a title-case string.
> > All words EXCEPT for small words are made title case
> > unless the string starts with a preposition, in which
> > case the word is correctly capitalized.
> > >>> book_title('DIVE Into python')
> > 'Dive into Python'
> > >>> book_title('the great gatsby')
> > 'The Great Gatsby'
> > >>> book_title('the WORKS OF AleXANDer dumas')
> > 'The Works of Alexander Dumas'
> > """
> > new_title = []
> > title_split = title.strip().lower().split()
> > for word in title_split:
> > if title_split[0] in small_words:
> > new_title.append(word.title())
> > elif word in small_words:
> > new_title.append(word.lower())
> > else:
> > new_title.append(word.title())
> > return(' '.join(new_title))
>
> > def _test():
> > import doctest, refactory
> > return doctest.testmod(refactory)
> > if __name__ == "__main__":
> > _test()
>
> > All tests are failing even though I am getting the correct output on
> > the first two tests. And the last test still gives me "Of" instead of
> > "of"
>
> > Any help is appreciated.
>
> I don't know about doctest -- I suspect it wants a structured docstring to
> specify the tests -- but this
>
> if title_split[0] in small_words:
> new_title.append(word.title())
>
> can't be what you want.
>
> Mel.
Agreed. Not what I need.
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2011-09-10 13:47 +0200 |
| Message-ID | <mailman.933.1315655225.27778.python-list@python.org> |
| In reply to | #13064 |
Tigerstyle wrote:
> I'm strugglin with some homework stuff and am hoping you can help me
> out here.
>
> This is the code:
>
> small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
> new_title = []
> title_split = title.strip().lower().split()
> for word in title_split:
> if title_split[0] in small_words:
> new_title.append(word.title())
> elif word in small_words:
> new_title.append(word.lower())
> else:
> new_title.append(word.title())
The logic of the for-loop is flawed; the expression
title_split[0] in small_words
will always evaluate to True if the first word is a "small word", even when
the loop is already past the first word. 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
[toc] | [prev] | [next] | [standalone]
| From | ting@thsu.org |
|---|---|
| Date | 2011-09-10 19:12 -0700 |
| Message-ID | <bc13c81c-11f3-485b-ae17-f31ed6828be3@et6g2000vbb.googlegroups.com> |
| In reply to | #13066 |
On Sep 10, 7:47 am, Peter Otten <__pete...@web.de> wrote:
> Tigerstyle wrote:
> > I'm strugglin with some homework stuff and am hoping you can help me
> > out here.
>
> > This is the code:
>
> > small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
> > new_title = []
> > title_split = title.strip().lower().split()
> > for word in title_split:
> > if title_split[0] in small_words:
> > new_title.append(word.title())
> > elif word in small_words:
> > new_title.append(word.lower())
> > else:
> > new_title.append(word.title())
>
> The logic of the for-loop is flawed; the expression
>
> title_split[0] in small_words
>
> will always evaluate to True if the first word is a "small word", even when
> the loop is already past the first word. 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
Another way to tackle this is to just capitalize the entire sentence
before returning it.
new_title = []
title_split = title.strip().lower().split()
for word in title_split:
if word in small_words:
new_title.append(word.lower())
else:
new_title.append(word.title())
return(''.join(new_title).capitalize())
However, I'm only helping with your homework, because I want to point
out that doctest comments should be *readable*. Don't just throw them
in, take the extra 10-15 seconds to make them easier on the eyes. In
the workplace, months will pass before you review this code again, so
you want to make it easier for an older version of yourself to
remember it.
And it takes very little effort to make it readable. Here's your
doctest string, reformatted with just a tiny bit more whitespace. I
even cut out a sentence.
def book_title(title):
"""
All words EXCEPT for small words are made title case
unless the string starts with a preposition, in which
case the word is correctly capitalized.
>>> book_title('DIVE Into python')
'Dive into Python'
>>> book_title('the great gatsby')
'The Great Gatsby'
>>> book_title('the WORKS OF AleXANDer dumas')
'The Works of Alexander Dumas'
"""
--
// T.Hsu
[toc] | [prev] | [next] | [standalone]
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2011-09-11 09:42 -0700 |
| Message-ID | <8fc2dea1-a5ef-4d7f-a0f2-e0be56e21e22@h7g2000yqm.googlegroups.com> |
| In reply to | #13100 |
On 11 Sep, 04:12, t...@thsu.org wrote:
> On Sep 10, 7:47 am, Peter Otten <__pete...@web.de> wrote:
>
>
>
>
>
>
>
>
>
> > Tigerstyle wrote:
> > > I'm strugglin with some homework stuff and am hoping you can help me
> > > out here.
>
> > > This is the code:
>
> > > small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
> > > new_title = []
> > > title_split = title.strip().lower().split()
> > > for word in title_split:
> > > if title_split[0] in small_words:
> > > new_title.append(word.title())
> > > elif word in small_words:
> > > new_title.append(word.lower())
> > > else:
> > > new_title.append(word.title())
>
> > The logic of the for-loop is flawed; the expression
>
> > title_split[0] in small_words
>
> > will always evaluate to True if the first word is a "small word", even when
> > the loop is already past the first word. 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
>
> Another way to tackle this is to just capitalize the entire sentence
> before returning it.
>
> new_title = []
> title_split = title.strip().lower().split()
> for word in title_split:
> if word in small_words:
> new_title.append(word.lower())
> else:
> new_title.append(word.title())
> return(''.join(new_title).capitalize())
>
> However, I'm only helping with your homework, because I want to point
> out that doctest comments should be *readable*. Don't just throw them
> in, take the extra 10-15 seconds to make them easier on the eyes. In
> the workplace, months will pass before you review this code again, so
> you want to make it easier for an older version of yourself to
> remember it.
>
> And it takes very little effort to make it readable. Here's your
> doctest string, reformatted with just a tiny bit more whitespace. I
> even cut out a sentence.
>
> def book_title(title):
> """
> All words EXCEPT for small words are made title case
> unless the string starts with a preposition, in which
> case the word is correctly capitalized.
>
> >>> book_title('DIVE Into python')
> 'Dive into Python'
> >>> book_title('the great gatsby')
> 'The Great Gatsby'
> >>> book_title('the WORKS OF AleXANDer dumas')
> 'The Works of Alexander Dumas'
> """
> --
> // T.Hsu
It capitalises the phrase, but still the rest of the phrase is in
lower case.
[toc] | [prev] | [next] | [standalone]
| From | Thomas Jollans <t@jollybox.de> |
|---|---|
| Date | 2011-09-10 13:50 +0200 |
| Message-ID | <mailman.934.1315655395.27778.python-list@python.org> |
| In reply to | #13064 |
On 10/09/11 13:20, Tigerstyle wrote:
> Hi guys.
>
> I'm strugglin with some homework stuff and am hoping you can help me
> out here.
>
> All tests are failing even though I am getting the correct output on
> the first two tests. And the last test still gives me "Of" instead of
> "of"
Cannot reproduce. I only get the one, expected, failure.
% python -m doctest books.py
**********************************************************************
File "books.py", line 12, in books.book_title
Failed example:
book_title('the WORKS OF AleXANDer dumas')
Expected:
'The Works of Alexander Dumas'
Got:
'The Works Of Alexander Dumas'
**********************************************************************
1 items had failures:
1 of 3 in books.book_title
***Test Failed*** 1 failures.
>
> def _test():
> import doctest, refactory
> return doctest.testmod(refactory)
> if __name__ == "__main__":
> _test()
>
What is this "refactory"? Are you testing the right code? What is the
output of your test - does it make sense for the module?
Thomas
[toc] | [prev] | [next] | [standalone]
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2011-09-11 09:39 -0700 |
| Message-ID | <d8154628-4307-4ec9-b3ee-cb9767b1355a@o9g2000vbo.googlegroups.com> |
| In reply to | #13067 |
On 10 Sep, 13:50, Thomas Jollans <t...@jollybox.de> wrote:
> On 10/09/11 13:20, Tigerstyle wrote:
>
> > Hi guys.
>
> > I'm strugglin with some homework stuff and am hoping you can help me
> > out here.
>
> > All tests are failing even though I am getting the correct output on
> > the first two tests. And the last test still gives me "Of" instead of
> > "of"
>
> Cannot reproduce. I only get the one, expected, failure.
>
> % python -m doctest books.py
> **********************************************************************
> File "books.py", line 12, in books.book_title
> Failed example:
> book_title('the WORKS OF AleXANDer dumas')
> Expected:
> 'The Works of Alexander Dumas'
> Got:
> 'The Works Of Alexander Dumas'
> **********************************************************************
> 1 items had failures:
> 1 of 3 in books.book_title
> ***Test Failed*** 1 failures.
>
>
>
> > def _test():
> > import doctest, refactory
> > return doctest.testmod(refactory)
> > if __name__ == "__main__":
> > _test()
>
> What is this "refactory"? Are you testing the right code? What is the
> output of your test - does it make sense for the module?
>
> Thomas
Still struggling with my test failing. All 3 tests fail. I'm using
Ecplipse and I think Eclipse is what causing this.
[toc] | [prev] | [next] | [standalone]
| From | Alister Ware <alister.ware@ntlworld.com> |
|---|---|
| Date | 2011-09-10 12:24 +0000 |
| Message-ID | <ZFIaq.3398$GZ.577@newsfe03.ams2> |
| In reply to | #13064 |
On Sat, 10 Sep 2011 04:20:17 -0700, Tigerstyle wrote:
> Hi guys.
>
> I'm strugglin with some homework stuff and am hoping you can help me out
> here.
>
> This is the code:
>
> small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
>
> def book_title(title):
> """ Takes a string and returns a title-case string. All words EXCEPT
> for small words are made title case unless the string starts with a
> preposition, in which case the word is correctly capitalized.
> >>> book_title('DIVE Into python')
> 'Dive into Python'
> >>> book_title('the great gatsby')
> 'The Great Gatsby'
> >>> book_title('the WORKS OF AleXANDer dumas')
> 'The Works of Alexander Dumas'
> """
> new_title = []
> title_split = title.strip().lower().split()
> for word in title_split:
> if title_split[0] in small_words:
> new_title.append(word.title())
> elif word in small_words:
> new_title.append(word.lower())
> else:
> new_title.append(word.title())
> return(' '.join(new_title))
>
> def _test():
> import doctest, refactory return doctest.testmod(refactory)
> if __name__ == "__main__":
> _test()
>
> All tests are failing even though I am getting the correct output on the
> first two tests. And the last test still gives me "Of" instead of "of"
>
> Any help is appreciated.
>
> Rgds
>
> T
Ignoring the docttests my process would be to process each word & then
manually capitalize he 1st word, .I would als0 use a comprehension as
makes for cleaner code:-
small_words=('into','the','a','of','at','in','for','on')
def capitalize(word):
if word in small_words:
return word
else:
return word.title()
def set_title(phrase):
result=[ capitalize(x.lower()) for x in phrase.split(' ') ]
result[0]=result[0].title()
return " ".join(result)
print set_title('the works of alexander dumas')
--
... I don't like FRANK SINATRA or his CHILDREN.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-09-11 01:56 +1000 |
| Message-ID | <mailman.940.1315670200.27778.python-list@python.org> |
| In reply to | #13068 |
On Sat, Sep 10, 2011 at 10:24 PM, Alister Ware <alister.ware@ntlworld.com> wrote: > Ignoring the docttests my process would be to process each word & then > manually capitalize he 1st word, .I would als0 use a comprehension as > makes for cleaner code:- > > def capitalize(word): > if word in small_words: > return word > else: > return word.title() And I'd do this with a lambda, but that's just me. Of course, if your logic is more complicated, it makes more sense to keep it in a named function, but a single conditional call can fit nicely into a lambda. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2011-09-11 09:40 -0700 |
| Message-ID | <bf53d530-0d06-4de3-8c14-7d70ff6428ed@b20g2000vbz.googlegroups.com> |
| In reply to | #13075 |
On 10 Sep, 17:56, Chris Angelico <ros...@gmail.com> wrote: > On Sat, Sep 10, 2011 at 10:24 PM, Alister Ware > > <alister.w...@ntlworld.com> wrote: > > Ignoring the docttests my process would be to process each word & then > > manually capitalize he 1st word, .I would als0 use a comprehension as > > makes for cleaner code:- > > > def capitalize(word): > > if word in small_words: > > return word > > else: > > return word.title() > > And I'd do this with a lambda, but that's just me. Of course, if your > logic is more complicated, it makes more sense to keep it in a named > function, but a single conditional call can fit nicely into a lambda. > > ChrisA Lambda is too complicated for me to understand yet. Will get there after a little while. Where would you put this piece of code?
[toc] | [prev] | [next] | [standalone]
| From | Ethan Furman <ethan@stoneleaf.us> |
|---|---|
| Date | 2011-09-11 11:43 -0700 |
| Message-ID | <mailman.999.1315766676.27778.python-list@python.org> |
| In reply to | #13068 |
Chris Angelico wrote: > On Sat, Sep 10, 2011 at 10:24 PM, Alister Ware > <alister.ware@ntlworld.com> wrote: >> Ignoring the docttests my process would be to process each word & then >> manually capitalize he 1st word, .I would als0 use a comprehension as >> makes for cleaner code:- >> >> def capitalize(word): >> if word in small_words: >> return word >> else: >> return word.title() > > And I'd do this with a lambda, but that's just me. Of course, if your > logic is more complicated, it makes more sense to keep it in a named > function, but a single conditional call can fit nicely into a lambda. Lambdas are great when needed, but if don't *need* it, and you have more than a few, debugging can be a nightmare... "Okay, so this is function <lambda>... and that is function <lambda>... and over here we also have function <lambda>... ARGH!" ~Ethan~
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-09-12 11:03 +1000 |
| Message-ID | <mailman.1011.1315789407.27778.python-list@python.org> |
| In reply to | #13068 |
On Mon, Sep 12, 2011 at 4:43 AM, Ethan Furman <ethan@stoneleaf.us> wrote: > Chris Angelico wrote: >> >> And I'd do this with a lambda, but that's just me. Of course, if your >> logic is more complicated, it makes more sense to keep it in a named >> function, but a single conditional call can fit nicely into a lambda. > > Lambdas are great when needed, but if don't *need* it, and you have more > than a few, debugging can be a nightmare... "Okay, so this is function > <lambda>... and that is function <lambda>... and over here we also have > function <lambda>... ARGH!" Yeah, they can be like the Bruces sketch at times. A lambda is basically a function defined in an expression. For instance: def add_one(x): return x+1 is (practically) the same as: add_one = lambda x: x+1 In each case, you can call that function and will get back a value. The advantage of lambdas is that, in a list comprehension or map call, the code is right there instead of being elsewhere in a def statement. But as you can see, they quickly become hard to read: [j+2 for i in [[1,2,3],[4,5,6],[7,8,9]] for j in (lambda x: [q+10 for q in x])(i)] Their main advantage isn't in list comps, where you can already use arbitrary expressions, but in calls that require a function as an argument. The map() function is very similar to a generator expression, but it can iterate over multiple iterables at once: >>> list(map(lambda x,y: x+y,[1,2,3],[40,50,60])) [41, 52, 63] Note how the lambda keeps the code right there, whereas a def would separate it out. It's obvious from this one expression that it's adding successive corresponding pairs. Hope that helps! ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2011-09-12 11:37 +1000 |
| Message-ID | <87sjo2lewp.fsf@benfinney.id.au> |
| In reply to | #13153 |
Chris Angelico <rosuav@gmail.com> writes: > A lambda is basically a function defined in an expression. For instance: > > def add_one(x): > return x+1 > > is (practically) the same as: > > add_one = lambda x: x+1 Those are only practically the same if you ignore the practical worth of a function knowing the name it was defined with. The latter does not have that, hence I don't see it as practically the same as the former. -- \ “The Vatican is not a state.… a state must have territory. This | `\ is a palace with gardens, about as big as an average golf | _o__) course.” —Geoffrey Robertson, 2010-09-18 | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-09-12 13:06 +1000 |
| Message-ID | <mailman.1012.1315796817.27778.python-list@python.org> |
| In reply to | #13154 |
On Mon, Sep 12, 2011 at 11:37 AM, Ben Finney <ben+python@benfinney.id.au> wrote: > Those are only practically the same if you ignore the practical worth of > a function knowing the name it was defined with. The latter does not > have that, hence I don't see it as practically the same as the former. > I know, but in the context of explaining what "lambda" means, it's practically the same. Lambdas can't (afaik) have docstrings either, etc, etc, but in terms of defining lambda, the two are more-or-less equivalent. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-09-12 14:29 +1000 |
| Message-ID | <4e6d8a92$0$29972$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #13155 |
On Mon, 12 Sep 2011 01:06 pm Chris Angelico wrote:
> On Mon, Sep 12, 2011 at 11:37 AM, Ben Finney <ben+python@benfinney.id.au>
> wrote:
>> Those are only practically the same if you ignore the practical worth of
>> a function knowing the name it was defined with. The latter does not
>> have that, hence I don't see it as practically the same as the former.
>>
>
> I know, but in the context of explaining what "lambda" means, it's
> practically the same. Lambdas can't (afaik) have docstrings either,
> etc, etc, but in terms of defining lambda, the two are more-or-less
> equivalent.
>>> f = lambda x: x+1
>>> f.__name__
'<lambda>'
>>> f.__doc__ = "Return x+1"
>>> f.__name__ = "add_one"
>>> help(f)
Help on function add_one in module __main__:
add_one(x)
Return x+1
Lambdas are functions, no more, no less. The only difference is that the
*syntax* of the lambda statement only allows a single expression rather
that the full block of a def, and no name. But because it returns an
ordinary function object, you can post-process it just like any other
function.
Unfortunately, tracebacks ignore the function.__name__ and print the code
object name:
>>> f
<function add_one at 0xb7eb425c>
>>> f("x")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
TypeError: cannot concatenate 'str' and 'int' objects
and the code object name is read-only:
>>> f.func_code.co_name
'<lambda>'
>>> f.func_code.co_name = "add_one"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: readonly attribute
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2011-09-10 13:59 -0400 |
| Message-ID | <mailman.943.1315677614.27778.python-list@python.org> |
| In reply to | #13064 |
On 9/10/2011 7:20 AM, Tigerstyle wrote:
> Hi guys.
>
> I'm strugglin with some homework stuff and am hoping you can help me
> out here.
We appreciate you saying so instead of hiding that this is homework.
> small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
>
> def book_title(title):
> """ Takes a string and returns a title-case string.
> All words EXCEPT for small words are made title case
> unless the string starts with a preposition, in which
> case the word is correctly capitalized.
> >>> book_title('DIVE Into python')
> 'Dive into Python'
> >>> book_title('the great gatsby')
> 'The Great Gatsby'
> >>> book_title('the WORKS OF AleXANDer dumas')
> 'The Works of Alexander Dumas'
> """
> new_title = []
> title_split = title.strip().lower().split()
> for word in title_split:
> if title_split[0] in small_words:
> new_title.append(word.title())
> elif word in small_words:
> new_title.append(word.lower())
> else:
> new_title.append(word.title())
The key issue is that you want to treat the first word one way (.title
it) and the rest differently (conditionally .title or not) . So
immediately separate the first from the rest and then process each.
There are at least three ways to do the split. Perhaps I should stop
with this hint, and certainly you should think a bit before reading
further, but here is what I consider to be the most elegant 3.2 code.
.
,
,
,
.
.
.
first, *rest = title.strip().lower().split()
new_title = [first.title()]
for word in rest:
if word not in small_words:
word = word.title()
new_title.append(word)
> return(' '.join(new_title))
doctest.testmod() now passes (there is no 'refactory' here)
> def _test():
> import doctest, refactory
> return doctest.testmod(refactory)
> if __name__ == "__main__":
> _test()
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Tigerstyle <laddosingh@gmail.com> |
|---|---|
| Date | 2011-09-11 04:46 -0700 |
| Message-ID | <fceb391d-8ac5-4eef-b89e-72c7327fc266@o26g2000vbi.googlegroups.com> |
| In reply to | #13077 |
On 10 Sep, 19:59, Terry Reedy <tjre...@udel.edu> wrote:
> On 9/10/2011 7:20 AM, Tigerstyle wrote:
>
> > Hi guys.
>
> > I'm strugglin with some homework stuff and am hoping you can help me
> > out here.
>
> We appreciate you saying so instead of hiding that this is homework.
>
>
>
>
>
>
>
>
>
> > small_words = ('into', 'the', 'a', 'of', 'at', 'in', 'for', 'on')
>
> > def book_title(title):
> > """ Takes a string and returns a title-case string.
> > All words EXCEPT for small words are made title case
> > unless the string starts with a preposition, in which
> > case the word is correctly capitalized.
> > >>> book_title('DIVE Into python')
> > 'Dive into Python'
> > >>> book_title('the great gatsby')
> > 'The Great Gatsby'
> > >>> book_title('the WORKS OF AleXANDer dumas')
> > 'The Works of Alexander Dumas'
> > """
> > new_title = []
> > title_split = title.strip().lower().split()
> > for word in title_split:
> > if title_split[0] in small_words:
> > new_title.append(word.title())
> > elif word in small_words:
> > new_title.append(word.lower())
> > else:
> > new_title.append(word.title())
>
> The key issue is that you want to treat the first word one way (.title
> it) and the rest differently (conditionally .title or not) . So
> immediately separate the first from the rest and then process each.
> There are at least three ways to do the split. Perhaps I should stop
> with this hint, and certainly you should think a bit before reading
> further, but here is what I consider to be the most elegant 3.2 code.
> .
> ,
> ,
> ,
> .
> .
> .
> first, *rest = title.strip().lower().split()
> new_title = [first.title()]
> for word in rest:
> if word not in small_words:
> word = word.title()
> new_title.append(word)
>
> > return(' '.join(new_title))
>
> doctest.testmod() now passes (there is no 'refactory' here)
>
> > def _test():
> > import doctest, refactory
> > return doctest.testmod(refactory)
> > if __name__ == "__main__":
> > _test()
>
> --
> Terry Jan Reedy
Thank you Terry,
I went for this solution as it was the easiest for me to understand
and comment myself keeping in mind what level I am at right now.
Thanks a ton to everyone for sharing so much information and making it
easy to read and understand your thoughts. This was surely very very
educating read the replies from so many talented people.
Thanks and looking forward to hanging around here more :)
T
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2011-09-11 13:03 -0400 |
| Message-ID | <mailman.995.1315760626.27778.python-list@python.org> |
| In reply to | #13124 |
On 9/11/2011 7:46 AM, Tigerstyle wrote: > Thank you Terry, > I went for this solution as it was the easiest for me to understand > and comment myself keeping in mind what level I am at right now. > Thanks a ton to everyone for sharing so much information and making it > easy to read and understand your thoughts. This was surely very very > educating read the replies from so many talented people. You are welcome. For more, see my thread "Idioms combining 'next(items)' and 'for item in items:'" and the responses. -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2011-09-10 15:36 -0400 |
| Message-ID | <mailman.945.1315683610.27778.python-list@python.org> |
| In reply to | #13064 |
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:' -- Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web