Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Open Source > BOOK: Python Projects
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Python Projects
This is the forum to discuss the Wrox book Python Projects Laura Cassell, Alan Gauld; ISBN: 978-1-118-90866-2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Python Projects section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 4th, 2015, 04:40 PM
jc. jc. is offline
Registered User
Points: 53, Level: 1
Points: 53, Level: 1 Points: 53, Level: 1 Points: 53, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2015
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default P304, Appendix A, Answer 7

I believe

Code:
class RotatingCounter:
   def __init__(self, start = 0)
should read:

Code:
class RotatingCounter:
   def __init__(self, start = 0):
And should:

Code:
if 0 < value < 9:
read?

Code:
if 0 < current_value < 9:
Reply With Quote
  #2 (permalink)  
Old August 4th, 2015, 06:00 PM
Wrox Author
Points: 119, Level: 2
Points: 119, Level: 2 Points: 119, Level: 2 Points: 119, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2015
Location: Stirling, Scotland
Posts: 25
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Quote:
Originally Posted by jc. View Post
I believe

Code:
class RotatingCounter:
   def __init__(self, start = 0)
should read:

Code:
class RotatingCounter:
   def __init__(self, start = 0):
Quite right. We should have had you as a proof reader!

Quote:
Originally Posted by jc. View Post
And should:

Code:
if 0 < value < 9:
read?

Code:
if 0 < current_value < 9:
But not for this one, although there is a slight error in the text. Where it says "the latter returns the current count and then sets the count back to zero" it should say sets the count to value if value is valid and otherwise leaves it as-is. (the default behaviour is of course to set it to zero.) So the code is what I intended but the description is inadequate.

Thanks again for highlighting it. I'm trying to get these things onto the book's errata page.

Alan G.
Reply With Quote
  #3 (permalink)  
Old August 5th, 2015, 03:48 AM
jc. jc. is offline
Registered User
Points: 53, Level: 1
Points: 53, Level: 1 Points: 53, Level: 1 Points: 53, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2015
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Cheers Alan.

[warning: I suspect I'm way off base here]

So calling reset() with a 'value' is optional, right? If it's not provided, zero is used, yes?

A value is provided, but not valid

I don't see how the default value is then used to reset self.counter, as we just (in the else block) raise an exception and return the current value of self.counter, don't we?

No value is provided, and zero is used

Doesn't the if block fail, as zero is not less than zero, so self.counter is not set to zero?

Value is provided, and is valid

Assuming the code does work as intended (likely given my understanding), why return the old value of self.counter (held in current_value), why not just return self.counter in both situations (and dispense with using current_value)?

Thanks for your patience!
Reply With Quote
  #4 (permalink)  
Old August 5th, 2015, 05:33 AM
Wrox Author
Points: 119, Level: 2
Points: 119, Level: 2 Points: 119, Level: 2 Points: 119, Level: 2
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2015
Location: Stirling, Scotland
Posts: 25
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Groan! I obviously had a bad day at the office with this one.

Yes value is optional, if not given the default zero should be used.

You are also right that the test for value should be

if 0<= value <=9:

Finally the reason to return the old value is mainly an arbitrary
choice, but its a practical one. Since reset() now allows you to
set the counter at any value you choose(within the range) in
practice you may want to remember the old value, change the
counter temporarily, then later go back to how it was. You could
do that in multiple steps but having reset give you the old value
for free saves a step. If you didn't need the old value you just
ignore it!As i say it was an arbitrary decision intended to make
the users life slightly easier And you should always try to design
a class (or a program for that matter) from the users perspective..

One thing to remember with the Appendix answers is that they
are *example* solutions. Any answer that meets the spec as given
is correct. In this case i tried to be a bit (too?) clever and provide
some added value (the original spec didn't say anything about setting
the value, it just said reset to zero...)

And another thing that will be obvious by now is that the sample
answers didn't get tested as thoroughly as the code in the main
text. That was probably a mistake....!
Reply With Quote
  #5 (permalink)  
Old August 5th, 2015, 06:13 AM
jc. jc. is offline
Registered User
Points: 53, Level: 1
Points: 53, Level: 1 Points: 53, Level: 1 Points: 53, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2015
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ahh. Thank you.

re: user perspective / examples only. Good advice / yes, of course.

Much appreciated.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
P304, Appendix A, Answer 3 jc. BOOK: Python Projects 5 August 5th, 2015 03:52 AM
Where is Appendix A? RKevinBurton Book: Professional Microsoft SQL Server Analysis Services 2008 with MDX: 978-0-470-24798-3 1 September 12th, 2010 10:47 AM
A New Appendix B DanM BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 12 August 14th, 2010 04:53 AM
Chapter 9 - Validating user Imput (p304) SouthendSupporter BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 March 9th, 2010 05:24 AM



All times are GMT -4. The time now is 07:52 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.