Re: [Tutor] capturing error msg in exception

2010-06-28 Thread Adam Bark
On 28 June 2010 00:27, Steven D'Aprano  wrote:

> On Mon, 28 Jun 2010 03:12:39 am Adam Bark wrote:
>
> > I think the 'as' syntax is only available in Python 3.x
>
> You think wrong. It is available from Python 2.6 onwards.
>

I know, I corrected myself after Steve Willoughby pointed it out.


>
>
> > Question marks go at the end of the sentence where you would normally
> > put a full stop if it wasn't a question.
>
> That's a terribly unhelpful answer given the context of Payal's
> question. I'm sure he knows the grammatical rules for questions in
> ordinary English sentences, but he's asking specifically about a
> particular form of sentence where you have a question consisting of two
> or more alternatives or examples separated as paragraphs:
>
> Well it was a terribly unclear question, I just answered what I thought he
was asking.


> [example]
> Hello, which is better, a lambda:
>
> (1) lambda x: x+1
>
> or a function definition:
>
> (2) def f(x):
>return x+1?
> [end example]
>
> It is very reasonable to ask where to put the question mark in examples
> like this. Unfortunately there is no good answer. If you put it on the
> same line as the second example, as shown, certainly isn't correct
> because it makes the question mark part of the example. It's
> *especially* dangerous in a programming context, because it leads to a
> syntax error.
>
> Putting it on a line on it's own after the example looks silly.
> Re-writing the question to avoid the problem is often awkward, but can
> be done:
>
> [rewritten example]
> Hello, which of these two are better?
>
> (1) lambda x: x+1
>
> (2) def f(x):
>return x+1
> [end rewritten example]
>
> Since there is no One Right Answer, you can do whichever seems best in
> context.
>
I'm sure this "work it out yourself" answer is much more helpful.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] capturing error msg in exception

2010-06-28 Thread Steven D'Aprano
On Mon, 28 Jun 2010 06:04:31 pm Adam Bark wrote:
> I'm sure this "work it out yourself" answer is much more helpful.

Unfortunately, the only correct answer is that there is no One True 
Answer.

A bit like life, really.


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


[Tutor] What is super for?

2010-06-28 Thread Adam Bark
I can't figure out how super(C, self).__init__() is any better than
C.__init__(self), is there a preference? Does super do something special?

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


Re: [Tutor] What is super for?

2010-06-28 Thread Adam Bark
On 28 June 2010 17:15, Adam Bark  wrote:

> I can't figure out how super(C, self).__init__() is any better than
> C.__init__(self), is there a preference? Does super do something special?
>
> Thanks.
>

Whoops I should really think about these things for a minute first, you
don't have to know the superclass in advance is presumably the idea?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] What is super for?

2010-06-28 Thread Alan Gauld


"Adam Bark"  wrote


I can't figure out how super(C, self).__init__() is any better than
C.__init__(self), is there a preference? Does super do something 
special?


Whoops I should really think about these things for a minute first, 
you

don't have to know the superclass in advance is presumably the idea?


That's normally the idea behind super(eg in Smalltalk).
Unfortunately the Python version kind of negates that with the need
for the class in the super() call. I think Python v3 has improved this
but I admit is still use the parent.method(self...) style.
To me it is just more obvious and less error prone.

Alan G. 



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


[Tutor] Fwd: HELP!

2010-06-28 Thread Chris
My friend jacob had a question. I can't really answer it, but you guys 
can. Send you replies to him not me. Oh, and jacob, I'm forwarding you 
message to the Python Mailing list.


 Original Message 
Delivered-To:   g.nius...@gmail.com
Received: 	by 10.216.7.204 with SMTP id 54cs12253wep; Fri, 25 Jun 2010 
05:15:53 -0700 (PDT)

Return-Path:
Received-SPF: 	pass (google.com: domain of benderjaco...@gmail.com 
designates 10.231.166.146 as permitted sender) client-ip=10.231.166.146;
Authentication-Results: 	mr.google.com; spf=pass (google.com: domain of 
benderjaco...@gmail.com designates 10.231.166.146 as permitted sender) 
smtp.mail=benderjaco...@gmail.com; dkim=pass 
header.i=benderjaco...@gmail.com
Received: 	from mr.google.com ([10.231.166.146]) by 10.231.166.146 with 
SMTP id m18mr672493iby.109.1277468152247 (num_hops = 1); Fri, 25 Jun 
2010 05:15:52 -0700 (PDT)
DKIM-Signature: 	v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; 
s=gamma; 
h=domainkey-signature:mime-version:received:received:date:message-id 
:subject:from:to:content-type; 
bh=fkkLSup/Zm9O5pNVp/JD3Z8YGVub7MdiKrBLK15hCss=; 
b=RPMHV1heyBvD25Ds95k3tdMD25KKr4fszAGSkoIoRhRNC43pOpx08JXSaHBmuO9lLK 
AXh56ZUgujS2c7MZR7LonTbHf2lmH+n4iS4j9zDWnhRVQYZp0PLzXNiy6rsmm9BZ8Ssx 
4MuLHSQu5ycP7RzyC1UKLo++EvMHNRXzIbJqQ=
DomainKey-Signature: 	a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; 
h=mime-version:date:message-id:subject:from:to:content-type; 
b=pEuD6hxgEDoy+BypQZeQyfam769gn9/T5P0/kTVorTYbDZX7OU+bkODyEleXmmXUQC 
PxuzC0rVUbzqDhswUQKv0W890MXtEBXCLMucrl8SqzlUo2Do2LuxeE9x7k7xhm6J9fSr 
3pa/VxDGgj0WH9Gelflxw9XjgSPd8mVRkbuK4=

MIME-Version:   1.0
Received: 	by 10.231.166.146 with SMTP id 
m18mr672493iby.109.1277468152240; Fri, 25 Jun 2010 05:15:52 -0700 (PDT)
Received: 	by 10.231.191.71 with HTTP; Fri, 25 Jun 2010 05:15:52 -0700 
(PDT)

Date:   Fri, 25 Jun 2010 08:15:52 -0400
Message-ID: 
Subject:HELP!
From:   Jacob Bender 
To: Chris 
Content-Type:   multipart/alternative; boundary=001636e0b4036d19880489d9bb56



Chris,
I tried the py2exe thing again, but things are getting confusing... It 
tells me to enter this line to run a setup that will make it a .exe.

"C: \Tutorial>python setup.py install"
Now, the REALLY BIG problem with this line is that the, I guess you 
could say action, of install always comes back with an invalid token 
error. This is straight from the website.
Now, that was in different programs and on the command line. If you 
write it in the setup.py, it comes back with a syntax error after the 
install... Please help

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


Re: [Tutor] Fwd: HELP!

2010-06-28 Thread Walter Prins
On 28 June 2010 21:36, Chris  wrote:

>  My friend jacob had a question. I can't really answer it, but you guys
> can. Send you replies to him not me. Oh, and jacob, I'm forwarding you
> message to the Python Mailing list.
>
> Hi Chris/Jacob,

I'm not sure if it's just me, but I'd have been slightly more interested in
helping and have found it a bit more polite if your friend came to this list
direct with his problem, rather than you basically appearing to be palming
off a support request for something that you started onto strangers on the
Python language tutoring list.  (This isn't exactly a python language
problem either.)

That said: Jacob can you please post *exactly* what you enter, and what
output you get?  (Copy and paste please, don't paraphrase/second guess what
we might want to know.) Suffice it to say that you're likely somehow
entering the command incorrectly.  (I can only speculate at this point that
you're incorrectly including part of the command prompt in your command,
and/or possibly inserting spaces (or omitting them) where needed. )

Regards

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


Re: [Tutor] Fwd: HELP!

2010-06-28 Thread Chris

On 06/28/2010 05:17 PM, Walter Prins wrote:
On 28 June 2010 21:36, Chris http://g.nius.ck>@gmail.com 
> wrote:


My friend jacob had a question. I can't really answer it, but you
guys can. Send you replies to him not me. Oh, and jacob, I'm
forwarding you message to the Python Mailing list.

Hi Chris/Jacob,

I'm not sure if it's just me, but I'd have been slightly more 
interested in helping and have found it a bit more polite if your 
friend came to this list direct with his problem, rather than you 
basically appearing to be palming off a support request for something 
that you started onto strangers on the Python language tutoring list. 
 (This isn't exactly a python language problem either.)


That said: Jacob can you please post *exactly* what you enter, and 
what output you get?  (Copy and paste please, don't paraphrase/second 
guess what we might want to know.) Suffice it to say that you're 
likely somehow entering the command incorrectly.  (I can only 
speculate at this point that you're incorrectly including part of the 
command prompt in your command, and/or possibly inserting spaces (or 
omitting them) where needed. )


Regards

Walter

Well I was just showing him the list.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fwd: HELP!

2010-06-28 Thread Lang Hurst

On 06/28/2010 01:36 PM, Chris wrote:
My friend jacob had a question. I can't really answer it, but you guys 
can. Send you replies to him not me. Oh, and jacob, I'm forwarding you 
message to the Python Mailing list.




Chris,
I tried the py2exe thing again, but things are getting confusing... It 
tells me to enter this line to run a setup that will make it a .exe.

"C: \Tutorial>python setup.py install"
Now, the REALLY BIG problem with this line is that the, I guess you 
could say action, of install always comes back with an invalid token 
error. This is straight from the website.
Now, that was in different programs and on the command line. If you 
write it in the setup.py, it comes back with a syntax error after the 
install... Please help


For what it's worth, py2exe never really worked for me the one time I 
was trying to bundle up a program.  I ended up using pyinstaller ( 
http://www.pyinstaller.org/ ) and it worked pretty much without a 
hitch.  Good luck, and next time don't be scared to ask yourself.  I 
don't think this is a python problem, so I doubt this list is the 
correct place...


-Lang


Jake


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



--
There are no stupid questions, just stupid people.

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


Re: [Tutor] What is super for?

2010-06-28 Thread Steven D'Aprano
On Tue, 29 Jun 2010 02:15:00 am Adam Bark wrote:

> I can't figure out how super(C, self).__init__() is any better than
> C.__init__(self), is there a preference? Does super do something
> special?

You've written that slightly wrong. You write the name of the *parent* 
class in the direct call to the method, and the name of the *current* 
class in the call to super:

class MyClass(C):
def __init__(self):
super(MyClass, self).__init__()
# or C.__init__(self)


super() does a lot, but remember it only works with new-style classes.


Consider a class with multiple inheritance:

class MyClass(C, D):
def f(self, *args):
do_something()
# now need to inherit behaviour from the super-classes

The question is, how do we write method f()?

def f(self, *args):
do_something()
C.f(self, *args)
D.f(self, *args)

But that's not right, because C might not have method f. Or D might not 
have it. Or they might both have it. 

Or neither C nor D have the method, and *both* inherit it from the same 
parent class higher up the hierarchy, B. If you manually call C.f and 
D.f, you end up calling B.f *twice*. Oops.

Even if C and D don't share the same parent class, they might inherit 
from a chain of classes, any of which defines f. Working out which 
method f to call can be complicated.

super() handles all that complication for you. You just write:

def f(self, *args):
do_something()
super(MyClass, self).f(*args)

and it works out which parent classes to call in which order.

Now, right about now you're probably thinking that since you don't use 
multiple inheritance, you don't care about super. Fair enough. But 
consider somebody who imports your class to inherit from them. They 
might be using multiple inheritance, or they might not be. If you use 
super(), then your class will work correctly for them regardless of 
whether they have multiple or single inheritance. But if you call the 
parent class manually, your class will *only* work with single 
inheritance.

You can find lots more about super and multiple inheritance on the web, 
both for and against. Mostly against in the Python world. I have read 
them, so you don't have to, and my summary is:

* Don't use multiple inheritance.
* If you have to use multiple inheritance, make sure you *really* 
  have to.
* Whether you use multiple inheritance or not, use super() so that 
  those brave or foolish souls who do can inherit from you.
* Use super() consistently, and document that you use it as part of
  your class' external interface.
* Never call super() with anything but the exact arguments you 
  received, unless you're a guru.

There is a provocative article called "Python's Super Considered 
Harmful". Despite the title, if you read the whole article you discover 
that the author doesn't actually recommend *against* super at all.

http://fuhm.net/super-harmful/

This thread on the Python-Dev mailing list discusses the above article:

http://mail.python.org/pipermail/python-dev/2005-January/050656.html

And here are some links from Michele Simionato on why mixins and 
multiple inheritance should be avoided unless you really, really need 
it, and how to avoid needing it:

http://www.artima.com/weblogs/viewpost.jsp?thread=246341
http://www.artima.com/weblogs/viewpost.jsp?thread=246483
http://www.artima.com/forums/flat.jsp?forum=106&thread=237121
http://www.artima.com/forums/flat.jsp?forum=106&thread=246488


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


Re: [Tutor] OT: need computer advice from wise Tutors

2010-06-28 Thread Steven D'Aprano
On Mon, 28 Jun 2010 10:52:03 am Richard D. Moores wrote:
> On Sun, Jun 27, 2010 at 16:25, Steven D'Aprano  
wrote:
> > On Mon, 28 Jun 2010 03:07:47 am Richard D. Moores wrote:
> >> A "feature" very important to me
> >> is that with Gmail, my mail is just always THERE, with no need to
> >> download it
> >
> > You see your email without downloading it? You don't understand how
> > the Internet works, do you?
>
> I do, and I also know that you know what I meant.

No, I'm afraid that I don't. You log into Gmail and your browser 
downloads the Gmail page; you click on an email, and your browser 
downloads the contents of the email in order to display it. I'm afraid 
I have no idea what you mean by not downloading your email. Perhaps you 
should try reading a 50MB email over dial-up to drive home the fact 
that you *are* downloading?

The difference is that, with Gmail (or Hotmail, or Yahoo mail), you have 
to download it each time you read the email instead of just once.

Particularly as this is a programming mailing list, I think it is very 
important to remember that fetching information over the Internet *is* 
downloading, and not just gloss over it as some sort of magic. There 
are Python libraries specifically for dealing with all this, and apart 
from the ability to execute Javascript, Python can do pretty much 
everything your browser does.

There are two sorts of people in the world: those who think that (e.g.) 
watching a streaming video in your browser over the Internet is 
fundamentally different from "downloading", and those who know that the 
only difference is that with streaming, the browser deletes the video 
after you've watched it. I would think that, as programmers, we should 
be in the second group rather than the first.


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


Re: [Tutor] OT: need computer advice from wise Tutors

2010-06-28 Thread David Hutto
On Mon, Jun 28, 2010 at 8:32 PM, Steven D'Aprano  wrote:
> On Mon, 28 Jun 2010 10:52:03 am Richard D. Moores wrote:
>> On Sun, Jun 27, 2010 at 16:25, Steven D'Aprano 
> wrote:
>> > On Mon, 28 Jun 2010 03:07:47 am Richard D. Moores wrote:
>> >> A "feature" very important to me
>> >> is that with Gmail, my mail is just always THERE, with no need to
>> >> download it
>> >
>> > You see your email without downloading it? You don't understand how
>> > the Internet works, do you?
>>
>> I do, and I also know that you know what I meant.
>
> No, I'm afraid that I don't. You log into Gmail and your browser
> downloads the Gmail page; you click on an email, and your browser
> downloads the contents of the email in order to display it. I'm afraid
> I have no idea what you mean by not downloading your email. Perhaps you
> should try reading a 50MB email over dial-up to drive home the fact
> that you *are* downloading?
>
> The difference is that, with Gmail (or Hotmail, or Yahoo mail), you have
> to download it each time you read the email instead of just once.
>
> Particularly as this is a programming mailing list, I think it is very
> important to remember that fetching information over the Internet *is*
> downloading, and not just gloss over it as some sort of magic. There
> are Python libraries specifically for dealing with all this, and apart
> from the ability to execute Javascript, Python can do pretty much
> everything your browser does.
>
> There are two sorts of people in the world: those who think that (e.g.)
> watching a streaming video in your browser over the Internet is
> fundamentally different from "downloading", and those who know that the
> only difference is that with streaming, the browser deletes the video
> after you've watched it.

But this only matters if a)you're paying for it, not the boss b) that
there are unlimited plans available for a single monthly price, or c)
you have an 'egotistical'(meaning a professional ego/rep to maintain)
perspective on minimizing your code.

 I would think that, as programmers, we should
> be in the second group rather than the first.
>
>
> --
> Steven D'Aprano
> ___
> Tutor maillist  -  tu...@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] OT: need computer advice from wise Tutors

2010-06-28 Thread Richard D. Moores
On Mon, Jun 28, 2010 at 17:32, Steven D'Aprano  wrote:
> On Mon, 28 Jun 2010 10:52:03 am Richard D. Moores wrote:
>> On Sun, Jun 27, 2010 at 16:25, Steven D'Aprano 
> wrote:
>> > On Mon, 28 Jun 2010 03:07:47 am Richard D. Moores wrote:
>> >> A "feature" very important to me
>> >> is that with Gmail, my mail is just always THERE, with no need to
>> >> download it
>> >
>> > You see your email without downloading it? You don't understand how
>> > the Internet works, do you?
>>
>> I do, and I also know that you know what I meant.
>
> No, I'm afraid that I don't.

I think you should have.

> You log into Gmail and your browser downloads the Gmail page;

Yes, of course. But I'm always logged into Gmail.  With Eudora I would
have to manually *download* new email to see what was new (as I
recall, there was a way to set Eudora to check for new mail at an
interval I could set -- but I often found this an annoying
interruption); with Gmail this is done for me (with no annoyance).
That's what I meant by "my mail is just always THERE", and because you
know the difference between OE and Gmail you knew what I meant, even
if I may have expressed it incorrectly. I really don't need your
lecture on this. I'm sure there's plenty for me to learn from you, but
not this.

> you click on an email, and your browser
> downloads the contents of the email in order to display it.

Of course. Just like anything else which has to get from a Gmail
server to me. If text, that's a small fraction of a second for me. So
small that it appears to be instantaneous.  If there are images, it's
still a small fraction of a second, and images are usually there by
the time I can scroll down to them.

> I'm afraid
> I have no idea what you mean by not downloading your email. Perhaps you
> should try reading a 50MB email over dial-up to drive home the fact
> that you *are* downloading?

Sure, but I have broadband access, as do many. My fault for not
mentioning this -- but you should not pretend to not have inferred
that I did have such access.

> The difference is that, with Gmail (or Hotmail, or Yahoo mail), you have
> to download it each time you read the email instead of just once.

Not a problem. See above.

> Particularly as this is a programming mailing list, I think it is very
> important to remember that fetching information over the Internet *is*
> downloading, and not just gloss over it as some sort of magic. There
> are Python libraries specifically for dealing with all this, and apart
> from the ability to execute Javascript, Python can do pretty much
> everything your browser does.

NOW you're talking about stuff I'd like to learn here.

> There are two sorts of people in the world: those who think that (e.g.)
> watching a streaming video in your browser over the Internet is
> fundamentally different from "downloading", and those who know that the
> only difference is that with streaming, the browser deletes the video
> after you've watched it. I would think that, as programmers, we should
> be in the second group rather than the first.

Hear, hear! But also to not be so quick when classifying others.

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


Re: [Tutor] retrieve URLs and text from web pages

2010-06-28 Thread Tino Dai
On Sun, Jun 27, 2010 at 12:15 PM, Khawla Al-Wehaibi wrote:

> Hi,
>
> I’m new to programming. I’m currently learning python to write a web
> crawler to extract all text from a web page, in addition to, crawling to
> further URLs and collecting the text there. The idea is to place all the
> extracted text in a .txt file with each word in a single line. So the text
> has to be tokenized. All punctuation marks, duplicate words and non-stop
> words have to be removed.
>

Welcome to Python! What you are doing is best done in a multi step process
so that you can understand everything that you are doing. To really
leverage Python, there are a couple of things that you need to read right
off the bat.

http://docs.python.org/library/stdtypes.html   (Stuff about strings). In
Python, everything is an object so everything will have methods or functions
related to it. For instance, the String object has a find method that will
return position of the string. Pretty handy if you ask me.

Also, I would read up on sets for python. That will reduce the size of your
code significantly.

>
> The program should crawl the web to a certain depth and collect the URLs
> and text from each depth (level). I decided to choose a depth of 3. I
> divided the code to two parts. Part one to collect the URLs and part two to
> extract the text. Here is my problem:
>
> 1.The program is extremely slow.
>

The best way to go about this is to use a profiler:

 http://docs.python.org/library/profile.html

2.I'm not sure if it functions properly.
>

To debug your code, you may want to read up on the python debugger.
 http://docs.python.org/library/pdb.html

3.Is there a better way to extract text?
>

See the strings and the lists. I think that you will be pleasantly surprised


> 4.Are there any available modules to help clean the text i.e. removing
> duplicates, non-stop words ...
>

Read up on sets and the string functions/method. They are your friend

> 5.Any suggestions or feedback is appreciated.
>
>
-Tino

PS: Please don't send html ladden emails, it makes it harder to work with.
Thanks
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor