[Tutor] Trivia

2011-06-24 Thread Vincent Balmori

I have to improve the trivia_challenge program so each question has a
different point value. I added a point into the text file after each
category line, so the next_block() can call it. Whenever the program
calculates the 'score = point' in main() it comes up with TypeError:
unsupported operand type(s) for +=: 'int' and 'str'  The problem is I am
having trouble changing point = next_line(the_file) in the next_block() to
be an int type. 

http://old.nabble.com/file/p31917610/trivia_challenge2.py
trivia_challenge2.py 

http://old.nabble.com/file/p31917610/trivia.txt trivia.txt 



-- 
View this message in context: 
http://old.nabble.com/Trivia-tp31917610p31917610.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trivia

2011-06-24 Thread Vincent Balmori

***ignore the point = point line in the next_block() function.
-- 
View this message in context: 
http://old.nabble.com/Trivia-tp31917610p31917637.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trivia

2011-06-24 Thread Vincent Balmori

try:

point = int(next_line(the_file))

If x is a string that can be interpreted as an integer number, int(x) is
that integer number; if the string is not the representation of an integer,
this will lead to a ValueError.

-- 
André Engels, andreeng...@gmail.com


It's working fine now with the scoring, but now at the end of the program
for some reason I get this error message:

Traceback (most recent call last):
  File
/Users/vincentbalmori/Desktop/Python/py3e_source/chapter07/trivia_challenge2.py,
line 83, in module
main()
  File
/Users/vincentbalmori/Desktop/Python/py3e_source/chapter07/trivia_challenge2.py,
line 76, in main
category, point, question, answers, correct, explanation =
next_block(trivia_file)
  File
/Users/vincentbalmori/Desktop/Python/py3e_source/chapter07/trivia_challenge2.py,
line 27, in next_block
point = int(next_line(the_file))
ValueError: invalid literal for int() with base 10: ''
-- 
View this message in context: 
http://old.nabble.com/Trivia-tp31917610p31917701.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trivia

2011-06-24 Thread Andre Engels
On Fri, Jun 24, 2011 at 8:56 AM, Vincent Balmori
vincentbalm...@yahoo.comwrote:


 try:

 point = int(next_line(the_file))

 If x is a string that can be interpreted as an integer number, int(x) is
 that integer number; if the string is not the representation of an integer,
 this will lead to a ValueError.

 --
 André Engels, andreeng...@gmail.com


 It's working fine now with the scoring, but now at the end of the program
 for some reason I get this error message:

 Traceback (most recent call last):
  File

 /Users/vincentbalmori/Desktop/Python/py3e_source/chapter07/trivia_challenge2.py,
 line 83, in module
main()
  File

 /Users/vincentbalmori/Desktop/Python/py3e_source/chapter07/trivia_challenge2.py,
 line 76, in main
category, point, question, answers, correct, explanation =
 next_block(trivia_file)
  File

 /Users/vincentbalmori/Desktop/Python/py3e_source/chapter07/trivia_challenge2.py,
 line 27, in next_block
 point = int(next_line(the_file))
 ValueError: invalid literal for int() with base 10: ''


The important thing in such a case is reading the error message and the last
line in the error trace. In this case the error message is:

ValueError: invalid literal for int() with base 10: ''

This means that the int() function is being called with something that is
not an integer - it even says what that something is, namely '' - that is,
the empty string. Apparently at some time you come to the line

point = int(next_line(the_file))

when next_line(the_file) is the empty string.

-- 
André Engels, andreeng...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trivia

2011-06-24 Thread Alan Gauld


Vincent Balmori vincentbalm...@yahoo.com wrote

It's working fine now with the scoring, but now at the end of the 
program

for some reason I get this error message:


/Users/vincentbalmori/Desktop/Python/py3e_source/chapter07/trivia_challenge2.py,

line 27, in next_block
   point = int(next_line(the_file))
ValueError: invalid literal for int() with base 10: ''


Thats because after the last question you try to read another
block and don't check anywhere whether you actually read
anything. Your next_block code just assumes there will
always be valid data there, but at the end of the file there
won't be. You get away with category being blank
because replace() doesn't complain. But int() does.

Your whole approach is very fragile in this respect, it only
takes one small mistake in the data to wreck your program,.
Somethjing like a config file format would be much more
robust (and readable) the config reader module would make
sure you got what you expected back.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trivia

2011-06-24 Thread Vincent Balmori

Your whole approach is very fragile in this respect, it only 
takes one small mistake in the data to wreck your program,. 
Somethjing like a config file format would be much more 
robust (and readable) the config reader module would make 
sure you got what you expected back. 

Can you please explain more on what you mean by this?


Alan Gauld wrote:
 
 
 Vincent Balmori vincentbalm...@yahoo.com wrote
 
 It's working fine now with the scoring, but now at the end of the 
 program
 for some reason I get this error message:

 /Users/vincentbalmori/Desktop/Python/py3e_source/chapter07/trivia_challenge2.py,
 line 27, in next_block
point = int(next_line(the_file))
 ValueError: invalid literal for int() with base 10: ''
 
 Thats because after the last question you try to read another
 block and don't check anywhere whether you actually read
 anything. Your next_block code just assumes there will
 always be valid data there, but at the end of the file there
 won't be. You get away with category being blank
 because replace() doesn't complain. But int() does.
 
 Your whole approach is very fragile in this respect, it only
 takes one small mistake in the data to wreck your program,.
 Somethjing like a config file format would be much more
 robust (and readable) the config reader module would make
 sure you got what you expected back.
 
 HTH,
 
 
 -- 
 Alan Gauld
 Author of the Learn to Program web site
 http://www.alan-g.me.uk/
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor
 
 

-- 
View this message in context: 
http://old.nabble.com/Trivia-tp31917610p31917979.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Trivia

2011-06-24 Thread Alan Gauld


Vincent Balmori vincentbalm...@yahoo.com wrote


Can you please explain more on what you mean by this?



Your whole approach is very fragile in this respect, it only
 takes one small mistake in the data to wreck your program,.


Your program relies on your data being exactly right.
It only takes one missed newline, or one extra newline
or a blank line or an extra question (maybe a cut n paste
error when editing) or whatever to completely mess your
program.


 Somethjing like a config file format would be much more
 robust (and readable) the config reader module would make
 sure you got what you expected back. 


Read the ConfigParser module documentation to see what I mean.
It treats your data like a Windows INI file and you can retrieve
values using section/tag values. So in your case
You could do, in pseudo code:

cp = ConfigParser(filename)
for section in cp.getSections():
question = cp.get(section, question)
 points = int( cp.getsection,points) )
 q1 = cp.get(section,q1)
 q2 = cp.get(section,q2)
 etc

And you can catch exceptions raised to detect errors.

That way, if a value doesn't exist you  find out about it and
the parser handles all the navigation of the file.

You could also use an XML format (or JSON or) but
Config Parser would be adequate for your needs.

The data file would also be easier to read because
the tags would make it clear:

[Section Title]
tag=value

Becomes, for example:

[On the Run With a Mammal]
points=1
question=Let's say you turn state's evidence and need to get on the 
lamb. If you wait /too long, what will happen?

q1=You'll end up on the sheep
q2=You'll end up on the cow
q3=You'll end up on the goat
q4=You'll end up on the emu
answer=1
explantion=A lamb is just a young sheep.[The Godfather Will Get Down 
With You Now]...HTH,-- Alan GauldAuthor of the Learn to Program web 
sitehttp://www.alan-g.me.uk/ 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] set the BETWEEN range in the SQL query using the python datetime function

2011-06-24 Thread Peter Otten
Norman Khine wrote:

 hello, i have this code http://pastie.org/2112997
 
 but i am not sure how to make the date range so that i get a list
 based on the daily totals not the results i am now getting:
 
 (2L, Decimal('173.958344'), Decimal('159.966349')) 2011-06-23 00:00:00
 2011-06-23 23:59:59
 (2L, Decimal('173.958344'), Decimal('159.966349')) 2011-06-22 00:00:00
 2011-06-23 23:59:59

What should the result look like? Can you give a simplified example with 
input and output data?

 the problem is that i am unsure how best to set the BETWEEN range in
 the SQL query using the python datetime function.

My guess is that you can move more of the problem from Python into SQL if 
you add a GROUP BY clause.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] set the BETWEEN range in the SQL query using the python datetime function

2011-06-24 Thread Norman Khine
thank you, it was simpler than what i was trying to do, here is the
revised version:

http://pastie.org/2115586

one thing i am getting an error is on like 103, in that if, i change
(http://pastie.org/2115615):

-plt.legend(('Income - GBP', 'Discounts', 'Google AdWords - GBP',
'Commission - %s GBP' % (total_commission)),
+plt.legend(('Income - GBP', 'Discounts', 'Google AdWords - %s GBP',
'Commission - %s GBP' % (total_adwords, total_commission)),
'upper right', shadow=True)

i get the following traceback:

Traceback (most recent call last):
  File commission.py, line 119, in module
plt.legend(('Income - GBP', 'Discounts', 'Google AdWords - %s
GBP', 'Commission - %s GBP' % (total_adwords, total_commission)),
TypeError: not all arguments converted during string formatting

what am i missing?

thanks

norman

On Fri, Jun 24, 2011 at 12:18 PM, Peter Otten __pete...@web.de wrote:
 Norman Khine wrote:

 hello, i have this code http://pastie.org/2112997

 but i am not sure how to make the date range so that i get a list
 based on the daily totals not the results i am now getting:

 (2L, Decimal('173.958344'), Decimal('159.966349')) 2011-06-23 00:00:00
 2011-06-23 23:59:59
 (2L, Decimal('173.958344'), Decimal('159.966349')) 2011-06-22 00:00:00
 2011-06-23 23:59:59

 What should the result look like? Can you give a simplified example with
 input and output data?

 the problem is that i am unsure how best to set the BETWEEN range in
 the SQL query using the python datetime function.

 My guess is that you can move more of the problem from Python into SQL if
 you add a GROUP BY clause.


 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor




--
˙ʇı ɹoɟ ƃuıʎɐd ǝɹ,noʎ ʍou puɐ ǝɔıoɥɔ ɐ ʞooʇ ı ʇɐɥʇ sı 'ʇlnɔıɟɟıp sı ʇɐɥʍ
˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ
ǝq s,ʇǝן ʇǝʎ
% .join( [ {'*':'@','^':'.'}.get(c,None) or
chr(97+(ord(c)-83)%26) for c in ,adym,*)uzq^zqf ] )
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] set the BETWEEN range in the SQL query using the python datetime function

2011-06-24 Thread Peter Otten
Norman Khine wrote:

 thank you, it was simpler than what i was trying to do, here is the
 revised version:
 
 http://pastie.org/2115586
 
 one thing i am getting an error is on like 103, in that if, i change
 (http://pastie.org/2115615):
 
 -plt.legend(('Income - GBP', 'Discounts', 'Google AdWords - GBP',
 'Commission - %s GBP' % (total_commission)),
 +plt.legend(('Income - GBP', 'Discounts', 'Google AdWords - %s GBP',
 'Commission - %s GBP' % (total_adwords, total_commission)),
 'upper right', shadow=True)
 
 i get the following traceback:
 
 Traceback (most recent call last):
   File commission.py, line 119, in module
 plt.legend(('Income - GBP', 'Discounts', 'Google AdWords - %s
 GBP', 'Commission - %s GBP' % (total_adwords, total_commission)),
 TypeError: not all arguments converted during string formatting
 
 what am i missing?

'Commission - %s GBP' % (total_adwords, total_commission))

Two items in the tuple but only one '%s' in the format string. You probably 
wanted

... 'Google AdWords - %s GBP' % total_adwords, 'Commission - %s GBP' % 
total_commission ...

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] set the BETWEEN range in the SQL query using the python datetime function

2011-06-24 Thread Christian Witts

On 2011/06/24 01:19 PM, Norman Khine wrote:

thank you, it was simpler than what i was trying to do, here is the
revised version:

http://pastie.org/2115586

one thing i am getting an error is on like 103, in that if, i change
(http://pastie.org/2115615):

-plt.legend(('Income - GBP', 'Discounts', 'Google AdWords - GBP',
'Commission - %s GBP' % (total_commission)),
+plt.legend(('Income - GBP', 'Discounts', 'Google AdWords - %s GBP',
'Commission - %s GBP' % (total_adwords, total_commission)),
 'upper right', shadow=True)

i get the following traceback:

Traceback (most recent call last):
   File commission.py, line 119, inmodule
 plt.legend(('Income - GBP', 'Discounts', 'Google AdWords - %s
GBP', 'Commission - %s GBP' % (total_adwords, total_commission)),
TypeError: not all arguments converted during string formatting

what am i missing?

thanks

norman


You've got 'Google AdWords - %s GBP' with no arguments and 'Commission - 
%s GBP' with 2 arguments.


--

Christian Witts
Python Developer

//

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] set the BETWEEN range in the SQL query using the python datetime function

2011-06-24 Thread Norman Khine
On Fri, Jun 24, 2011 at 1:31 PM, Christian Witts cwi...@compuscan.co.za wrote:
 On 2011/06/24 01:19 PM, Norman Khine wrote:

 thank you, it was simpler than what i was trying to do, here is the
 revised version:

 http://pastie.org/2115586

 one thing i am getting an error is on like 103, in that if, i change
 (http://pastie.org/2115615):

 -plt.legend(('Income - GBP', 'Discounts', 'Google AdWords - GBP',
 'Commission - %s GBP' % (total_commission)),
 +plt.legend(('Income - GBP', 'Discounts', 'Google AdWords - %s GBP',
 'Commission - %s GBP' % (total_adwords, total_commission)),
 'upper right', shadow=True)

 i get the following traceback:

 Traceback (most recent call last):
   File commission.py, line 119, in module
 plt.legend(('Income - GBP', 'Discounts', 'Google AdWords - %s
 GBP', 'Commission - %s GBP' % (total_adwords, total_commission)),
 TypeError: not all arguments converted during string formatting

 what am i missing?

 thanks

 norman

 You've got 'Google AdWords - %s GBP' with no arguments and 'Commission - %s
 GBP' with 2 arguments.

thanks


 --

 Christian Witts
 Python Developer





-- 
˙ʇı ɹoɟ ƃuıʎɐd ǝɹ,noʎ ʍou puɐ ǝɔıoɥɔ ɐ ʞooʇ ı ʇɐɥʇ sı 'ʇlnɔıɟɟıp sı ʇɐɥʍ
˙uʍop ǝpısdn p,uɹnʇ pןɹoʍ ǝɥʇ ǝǝs noʎ 'ʇuǝɯɐן sǝɯıʇ ǝɥʇ puɐ 'ʇuǝʇuoɔ
ǝq s,ʇǝן ʇǝʎ
% .join( [ {'*':'@','^':'.'}.get(c,None) or
chr(97+(ord(c)-83)%26) for c in ,adym,*)uzq^zqf ] )
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decorators

2011-06-24 Thread Prasad, Ramit
Excellent explanation Steven; I understood the mechanical basics but this has 
made the reason behind it a lot clearer. 

If test_argument is only being passed the function how does it have access to 
the arguments? Or is that more syntactic sugar / abbreviation for explanation?

def test_argument(func):
 def inner(arg):
 if not (isinstance(arg, int) and arg  0):
 raise ValueError('bad argument')
 arg = min(arg, 1000)
 return func(arg)
 return inner


@test_argument
def func(arg):
 do stuff

This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase  Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase 
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decorators

2011-06-24 Thread python
Steven,

 Here's my cheap introduction to decorators...

Beautifully explained!

Thank you,

Malcolm (not the OP)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] decorators

2011-06-24 Thread Steven D'Aprano

Prasad, Ramit wrote:
Excellent explanation Steven; I understood the mechanical basics but this has made the reason behind it a lot clearer. 

If test_argument is only being passed the function how does it have access to the arguments? 


It doesn't. There are three functions involved. The first is the 
decorator, test_argument. It only receives one argument, the function func.


The second is func itself, the original function before it is wrapped.

The third is the inner function of the decorator, which wraps func. Only 
the last two functions, func and the wrapped func, ever see the function 
arguments.


An example may make it more clear:

def decorator(f):
# Wrap function f.
print(decorator is called with argument %s % f)
print(creating inner function...)
def inner(x):
print(wrapper function called with argument %s % x)
return f(x+100)
print(now returning the wrapped function)
return inner

@decorator
def func(x):
print(original function called with argument %s % x)

func(1)
func(2)
func(3)


When I execute that code, I get this output printed:

decorator is called with argument function func at 0x922a16c
creating inner function...
now returning the wrapped function
wrapper function called with argument 1
original function called with argument 101
wrapper function called with argument 2
original function called with argument 102
wrapper function called with argument 3
original function called with argument 103


--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor