Re: [Tutor] loop questions

2013-04-07 Thread Alan Gauld

On 06/04/13 23:00, Soliman, Yasmin wrote:

I have two questions on these simple programs:

1st why does this loop keep repeating after I enter 'Quit'?

import calendar
m = raw_input(“Enter a year: “)
while m != “Quit”:
  if calendar.isleap(int(m)):
   print “%d is a leap year” % (int(m))
  else:
   print “%d is not a leap year” % (int(m))



I don't understand why it even starts repeating since
the only user input is before the loop. However you
need to enter exactly Quit and not QUIT or quit
or any other combination... It usually better to
force the user input to upper or lower case, eg:

while m.lower() != quit:


2nd  How can I make this program not crash when a user enters a non integer?

m = raw_input(“Enter an integer: “)
while not m.isdigit():
  m = raw_input(“Enter an integer: “)
  num = int(m)


Moving the last line out of the loop will help.
But there are other things to check in addition to isdigit().
That's why Python tends towards exception handling rather
than pre-validation.

eg.

while True:
   try: m = int(raw_input())
   except ValueError,TypeError: continue

HTH
--
Alan G
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


[Tutor] urllib.urlretrieve ?

2013-04-07 Thread Matthew Ngaha
Im following a tutorial on what i believe is using Python 2 and i'm a
bit confused. I use Python 3 on windows vista. I believe
urlretrieve() is a python 2 function that isnt in 3, but why is the
tutorial using urllib instead of urllib2? Also when i run the program
on Python 3, it says:

AttributeError: 'module' object has no attribute 'urlretrieve'

so Python 3 doesnt recognize this function, i want to ask if there is
an equivalent function to 'urlretrieve' in Python 3? the program uses
this function to download a file as so:

urllib.urlretrieve(self._url, self._filename, reporthook)

the 3rd argument is a function that is called to track progress of the
current download status. i dont really need help with the program, i
understand its code, but i need to know if this function has one
similar in Python 3? or do you have any examples of how i could add
its functionality to Python 3.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urllib.urlretrieve ?

2013-04-07 Thread eryksun
On Sun, Apr 7, 2013 at 11:07 AM, Matthew Ngaha chigga...@gmail.com wrote:
 so Python 3 doesnt recognize this function, i want to ask if there is
 an equivalent function to 'urlretrieve' in Python 3?

The documentation is indexed:

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


Re: [Tutor] urllib.urlretrieve ?

2013-04-07 Thread Alan Gauld

On 07/04/13 16:07, Matthew Ngaha wrote:


an equivalent function to 'urlretrieve' in Python 3? the program uses
this function to download a file as so:

urllib.urlretrieve(self._url, self._filename, reporthook)



Looking in the V3 documents on the Python web site it says:

==
The following functions and classes are ported from the Python 2 module 
urllib (as opposed to urllib2). They might become deprecated at some 
point in the future.


urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)

Copy a network object denoted by a URL to a local file...

===

Look in the module docs for urlib.request.


HTH

--
Alan G
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] urllib.urlretrieve ?

2013-04-07 Thread Matthew Ngaha
the whole program depends on the urlretrieve 3rd argument, the
function to be called.. is there no options to include a callback?

def _download(self):

def reporthook(pos, block, total):
if self.size != total:
self._size = total
self.on_size.emit()
self.progress = float(pos*block)/float(total)

urllib.urlretrieve(self._url, self._filename, reporthook)
##3rd arg calls inner function
self.running = False
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urllib.urlretrieve ?

2013-04-07 Thread Matthew Ngaha
 Look in the module docs for urlib.request.


Hi. Ive never used the urllib before unless it was typing in something
from an unrelated tutorial. I've also never done anything related to
web programming. I'm looking at the urllib.request page and i really
don't understand what i'm looking at. I'm confused as to what i should
be looking for.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urllib.urlretrieve ?

2013-04-07 Thread Joel Goldstick
On Sun, Apr 7, 2013 at 11:32 AM, Matthew Ngaha chigga...@gmail.com wrote:

 the whole program depends on the urlretrieve 3rd argument, the
 function to be called.. is there no options to include a callback?

 def _download(self):

 def reporthook(pos, block, total):
 if self.size != total:
 self._size = total
 self.on_size.emit()
 self.progress = float(pos*block)/float(total)

 urllib.urlretrieve(self._url, self._filename, reporthook)
 ##3rd arg calls inner function
 self.running = False


You might want to look at requests module:
http://docs.python-requests.org/en/latest/
Many people think it is easier to use than the native python support.


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




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


Re: [Tutor] urllib.urlretrieve ?

2013-04-07 Thread Matthew Ngaha

 You might want to look at requests module:
 http://docs.python-requests.org/en/latest/
 Many people think it is easier to use than the native python support.


are all of these modules related to web programming like clients and
(HTTP)servers? I've come across urllib a lot of times but only
followed along, maybe its best i just learn about HTTP and its
concepts etc so i can understand the request module better then come
back to my problem.. Thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urllib.urlretrieve ?

2013-04-07 Thread Alan Gauld

On 07/04/13 17:20, Matthew Ngaha wrote:

Look in the module docs for urlib.request.



Hi. Ive never used the urllib before


In Python 3 urllib combines (the best) features of
urllib and urllib2 from Python 2.


web programming. I'm looking at the urllib.request page and i really
don't understand what i'm looking at. I'm confused as to what i should
be looking for.


You were asking about urllib.urlretrieve.
It's documented on that page.

Now, beyond that, what else are you confused about?
The more specific the question the more specific the answer.

--
Alan G
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] urllib.urlretrieve ?

2013-04-07 Thread Matthew Ngaha
yeah, sadly urlretrieve doesnt exist on my version of python. i just
need a function to put in the same line that replaces this one with
the same arguments in order to see the results of my tutorial.

urllib.urlretrieve(self._url, self._filename, reporthook)

On Sun, Apr 7, 2013 at 7:10 PM, Alan Gauld alan.ga...@btinternet.com wrote:
 On 07/04/13 17:20, Matthew Ngaha wrote:

 Look in the module docs for urlib.request.


 Hi. Ive never used the urllib before


 In Python 3 urllib combines (the best) features of
 urllib and urllib2 from Python 2.


 web programming. I'm looking at the urllib.request page and i really
 don't understand what i'm looking at. I'm confused as to what i should
 be looking for.


 You were asking about urllib.urlretrieve.
 It's documented on that page.

 Now, beyond that, what else are you confused about?
 The more specific the question the more specific the answer.


 --
 Alan G
 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
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urllib.urlretrieve ?

2013-04-07 Thread Mark Lawrence

On 07/04/2013 19:49, Matthew Ngaha wrote:

Please don't top post.


yeah, sadly urlretrieve doesnt exist on my version of python. i just
need a function to put in the same line that replaces this one with
the same arguments in order to see the results of my tutorial.

urllib.urlretrieve(self._url, self._filename, reporthook)

On Sun, Apr 7, 2013 at 7:10 PM, Alan Gauld alan.ga...@btinternet.com wrote:

On 07/04/13 17:20, Matthew Ngaha wrote:


Look in the module docs for urlib.request.



Hi. Ive never used the urllib before


In Python 3 urllib combines (the best) features of
urllib and urllib2 from Python 2.


web programming. I'm looking at the urllib.request page and i really
don't understand what i'm looking at. I'm confused as to what i should
be looking for.


You were asking about urllib.urlretrieve.
It's documented on that page.

Now, beyond that, what else are you confused about?
The more specific the question the more specific the answer.

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



See either 
http://docs.python.org/2/library/urllib.html#urllib.urlretrieve or

http://docs.python.org/3/library/urllib.request.html#urllib.request.urlretrieve


--
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.


Mark Lawrence

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


Re: [Tutor] urllib.urlretrieve ?

2013-04-07 Thread Matthew Ngaha
org/2/library/urllib.html#urllib.urlretrieve
 or
 http://docs.python.org/3/library/urllib.request.html#urllib.request.urlretrieve
sorry about the top posting gmail has changed the way their messages
are done, it gave me a blank field i didnt realize i was writing above
the message.

i must have missed it but your 2nd link was exactly what i needed. ive
got the program to run without the module error. However i'm out of
luck as i now get this error:

urllib.error.HTTPError: HTTP Error 404: Not Found

i guess the tutorial is very old that the site doesnt exist anymore:(
Thanks for the help
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] urllib.urlretrieve ?

2013-04-07 Thread Bod Soutar
On 7 April 2013 20:59, Matthew Ngaha chigga...@gmail.com wrote:
org/2/library/urllib.html#urllib.urlretrieve
 or
 http://docs.python.org/3/library/urllib.request.html#urllib.request.urlretrieve
 sorry about the top posting gmail has changed the way their messages
 are done, it gave me a blank field i didnt realize i was writing above
 the message.

 i must have missed it but your 2nd link was exactly what i needed. ive
 got the program to run without the module error. However i'm out of
 luck as i now get this error:

 urllib.error.HTTPError: HTTP Error 404: Not Found

 i guess the tutorial is very old that the site doesnt exist anymore:(
 Thanks for the help

Unless you're looking for something specific from the URL that the
tutorial uses, then you could use any address

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


Re: [Tutor] urllib.urlretrieve ?

2013-04-07 Thread Matthew Ngaha
 Unless you're looking for something specific from the URL that the
 tutorial uses, then you could use any address

 Bodsda

thanks i did that and to my suprise an extra file showed up in my
folder, of the page i just visited:) thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor