[Tutor] Requesting restricted URL (further authentication requested)

2010-10-19 Thread Tim Johnson
I've written the following function which successfully gets an
authenticated URL:
def getRestrictedURL(authName,URL,log,pswd):
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(authName, URL,log,pswd)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
return opener.open(URL).read()
# But, alas, any links in content 'beneath' the URL
# require additional authentication. 
Any way around this?
Thanks
-- 
Tim 
tim at johnsons-web.com or akwebsoft.com
http://www.akwebsoft.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Requesting restricted URL (further authentication requested)

2010-10-19 Thread Vince Spicer
On Tue, Oct 19, 2010 at 1:56 PM, Tim Johnson  wrote:

> I've written the following function which successfully gets an
> authenticated URL:
> def getRestrictedURL(authName,URL,log,pswd):
>auth_handler = urllib2.HTTPBasicAuthHandler()
>auth_handler.add_password(authName, URL,log,pswd)
>opener = urllib2.build_opener(auth_handler)
>urllib2.install_opener(opener)
>return opener.open(URL).read()
> # But, alas, any links in content 'beneath' the URL
> # require additional authentication.
> Any way around this?
> Thanks
> --
> Tim
> tim at johnsons-web.com or akwebsoft.com
> http://www.akwebsoft.com
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Tim,

Unless you are tied to the standard library I would recommend looking at

httplib2  http://code.google.com/p/httplib2/

This handles your authentication and connection much better then the
standard urllib.

-- 
Vince Spicer

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


Re: [Tutor] Requesting restricted URL (further authentication requested)

2010-10-19 Thread Sander Sweers
On 19 October 2010 21:56, Tim Johnson  wrote:
> I've written the following function which successfully gets an
> authenticated URL:
> def getRestrictedURL(authName,URL,log,pswd):
>        auth_handler = urllib2.HTTPBasicAuthHandler()
>        auth_handler.add_password(authName, URL,log,pswd)
>        opener = urllib2.build_opener(auth_handler)
>        urllib2.install_opener(opener)
>        return opener.open(URL).read()

The above should be:
data = opener.open(URL).read() #does the initial authentication.
return opener #return the opener object not the data you read.

> # But, alas, any links in content 'beneath' the URL
> # require additional authentication.

You are almost there. In order to authenticate initially you indeed
need to read the page. But then what you want is to return the opener
object, not the page you read which you do currently. The opener
object holds the authentication data and this opener object you will
need to use for every new url you want to read, opener.open().

Depending on what you want to do with the data you might want to split
this up into 2 functions. One that will authenticate and another that
read the url and returns the data. The second function would need to
be passed the opener object which the first one returned.

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


Re: [Tutor] Requesting restricted URL (further authentication requested)

2010-10-19 Thread Tim Johnson
* Vince Spicer  [101019 12:25]:
> On Tue, Oct 19, 2010 at 1:56 PM, Tim Johnson  wrote:
> 
> 
> Tim,
> 
> Unless you are tied to the standard library I would recommend looking at
> 
> httplib2  http://code.google.com/p/httplib2/
> 
> This handles your authentication and connection much better then the
> standard urllib.
 
  Thanks Vince, I will give that a try. I'd like to keep things
  simpler than a global opener as per the other response. I'll let
  you all know how that goes.
  cheers
-- 
Tim 
tim at johnsons-web.com or akwebsoft.com
http://www.akwebsoft.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Problem with python

2010-10-19 Thread Matthew Nunes

To whom it may concern, 
   
   Hi, I've just started learning how to 
program in python using Allan B. Downy's book "How to think like a computer 
scientist" and it explained something in the recursion chapter which have still 
been unable to understand. It wrote a piece of code for the factorial function 
in math for example 3! is 3 * 2 * 1. I cannot understand the how it claimed the 
code executed, and  logically it makes no sense to me. Please explain it to me, 
as I have spent hours trying to get my head around it, as the book(in my 
opinion) did not explain it well. Here it is: 
 
def factorial(n):
if n == 0:
return 1
else:
recurse = factorial(n-1)
result = n * recurse
return result
 
If there is and easier piece of code that you know of that can be used for 
factorial, if you could please also tell me that.
 
Thank you for your time.  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problem with python

2010-10-19 Thread Alan Gauld


"Matthew Nunes"  wrote...


...something in the recursion chapter which have still been unable
to understand. It wrote a piece of code for the factorial function
 Please explain it to me, as I have spent hours trying to get
my head around it,


Recursion is one of the more baffling topics when you first
encounter it so don't be surprised if it takes a couple of goes
to "get it". It even trips up people who have been using it for years.

For an alternative explanation of recursion try my tutorial
(in the Advanced Topics section, under recursion(!)). I walk
through factorial cycle by cycle, see if that explanation helps.
If it does you can read a bit more more about it and its uses
in the Functional Programming topic.

If not, try forming some more specific questions around the
bits that puzzle you most. The more specific the question the
greater the chance of an answer...

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] Problem with python

2010-10-19 Thread Emmanuel Ruellan
On Tue, Oct 19, 2010 at 11:02 PM, Matthew Nunes wrote:

>
> It wrote a piece of code for the factorial function in math for example 3!
> is 3 * 2 * 1. I cannot understand the how it claimed the code executed, and
> logically it makes no sense to me.
>
>
I suggest you follow the algorithm yourself, with a pencil and a sheet of
paper. Substitute various numerical values for n, starting with zero.

For example:

For n=0, the body of the function becomes:

if 0 == 0:
return 1
else:
recurse = factorial(0-1)
result = 0 * recurse
return result

What result do you get?

For n=1, it gets a little bit tricky, because the function calls itself:

if 1 == 0:
return 1
else:
recurse = factorial(1-1)
result = 1 * recurse
return result

You'd like an easier method to calculate factorials?

>>> from math import factorial
>>> print factorial(4)
24

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


Re: [Tutor] Problem with python

2010-10-19 Thread Wayne Werner
On Tue, Oct 19, 2010 at 4:02 PM, Matthew Nunes wrote:

>  To whom it may concern,
>
>Hi, I've just started learning how to
> program in python using Allan B. Downy's book "How to think like a computer
> scientist" and it explained something in the recursion chapter which have
> still been unable to understand. It wrote a piece of code for the factorial
> function in math for example 3! is 3 * 2 * 1. I cannot understand the how it
> claimed the code executed, and  logically it makes no sense to me. Please
> explain it to me, as I have spent hours trying to get my head around it, as
> the book(in my opinion) did not explain it well. Here it is:
>

That's a pretty solid book. This example uses recursion - for a great
example of recursion, search Google for recursion

But we can examine the code:

This is a function that takes one parameter named n

> def factorial(n):
>
If n is 0 then return 1 (since 0! = 1)

> if n == 0:
>
> return 1
>
> else:
>
If n > 0 then the definition of a factorial is n * factorial(n-1) - which we
do here in two steps

>  recurse = factorial(n-1)
>
> result = n * recurse
>
And then we return the result


> return result
>
> If there is and easier piece of code that you know of that can be used for
> factorial, if you could please also tell me that.
>

This is probably the very easiest recursive example to get your head around
because the definition of factorial is recursive. However, it's a *terribly*
inefficient way to compute the factorial of a number.

The most efficient way for something like 3 is this:

import math
math.factorial(3)

Using the built-in functions are always better.

However, if you're wanting a code example you can do this one:

def factorial(n):
if n == 0:
n = 1
fact = 1
for x in xrange(1, n+1):
fact = fact * x # This could be replaced by fact *= x
return fact

Or you could do something a little more advanced:

def factorial(n):
if n == 0:
n = 1
return reduce(lambda x,y: x*y, xrange(1,n+1))

But that's probably a little beyond your comprehension level at this point.

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