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

2010-06-27 Thread Marc Tompkins
On Sat, Jun 26, 2010 at 4:46 PM, Steven D'Aprano wrote:

> Apart from:
>
 The OP was asking about upgrading from Vista to 7, so let me answer your
objections here...

 having no default email client (what sort of two-bit operating system
> doesn't have an email client in 2010?);
>
Jesus, you _miss_ Outlook Express?  Seriously:  the new default is webmail.
Like it, don't like it, but it's really not as if you can't get your mail.


> the automatic upgrades that run silently in the background with no easy
> way to turn them on or off

Just like XP and Vista, you're asked during installation whether you want to
allow or disallow automatic updates.  If you breeze past that question, then
- just like in XP and Vista - you can right-click on the little icon that
appears in your system tray (oops, I mean "notification area.")  True,
Windows 7 is proactive about hiding stuff in the tray... but it does still
appear there.

A more legitimate gripe is that, after downloading those updates, Windows
insists on applying them when you shut down the computer.  If you're in a
hurry, that can be a PITA.


> (always fun when your Internet download cap
> is completely used up TWO DAYS into the month -- especially when you
> don't know because you can't read the email from your ISP due to not
> having an email client,

To paraphrase, "what sort of two-bit ISP doesn't have a webmail site in
2010?"

the gratuitous UI changes (am I missing something, or does Internet
> Explorer no longer have a menubar?);
>
That's IE, not Windows.  Windows 7 comes with IE 8; Vista came with IE 7; XP
came with IE 6.  I don't care for the look myself, but then I never use IE
if I can avoid it (Firefox for mail and searching, 'cause I love me some
plugins; Chrome for everything else, 'cause it's fast as hell.  IE when I'm
on a client machine and there's nothing better.)  IE 8 is being rolled out
to all versions of Windows in any case, so that's not a reason not to
upgrade - unless you were planning to turn off Windows Update in Vista,
which is a bad idea for security reasons...

the use of third-party applications like Adobe Acrobat Reader which have
> become overloaded with *stupid* security vulnerabilities *by design*
> (years after Microsoft themselves got burnt, time and time again, by
> allowing the web-browser and mail client to execute random code found
> on the internet, somebody at Adobe apparently thought it would be a
> good idea for the PDF reader to do the same thing *facepalms*); and
>
Microsoft doesn't provide a PDF reader of its own - I'm not sure, but I
suspect that's for legal reasons - so whatever PDF reader you use, it HAS to
be third-party.  Why Adobe?  Probably because it's called "_Adobe_ Acrobat?"

Now, I hate Adobe products for the same reasons you mentioned, so I use and
recommend the free Foxit Reader (www.foxitsoftware.com).  But that was the
same under XP and Vista.


> consequently the proliferation of adware and spyware (even
> the "legitimate" anti-malware companies fill your browser with ad-laden
> toolbars and try terrifying the user with "your computer is
> unprotected" warnings -- no wonder the average user can't tell the
> difference between legitimate anti-malware and trojan horses).
>

 Again, not new in 7 - and, as a matter of fact, possibly somewhat better
than in Vista.


> On the other hand, it is quite pretty.
>
Screw pretty.  Pretty don't pay the rent.  First thing I do on any machine I
get my hands on is turn off the $%^&* Aero Glass - why would I take that
kind of performance hit for the dubious pleasure of a translucent titlebar?
But guess what?  MS introduced that piece of idiotism in - wait for it -
Vista.

My reasons FOR upgrading:

- Better UAC.  UAC is never going to feel natural to users (like me - I
freely admit it) who came up from DOS, and are stuck in a single-user
mindset.  Users raised on *nixes, on the other hand, find UAC to be a
laughable baby step on the way to a real least-privilege security model.
That said, 7 does a much more natural job (than Vista) of asking for
elevation only when needed, and staying out of the way most of the rest of
the time.

- Faster sleep/hibernation and wakeup.  I have no idea what they did under
the covers, but on my laptop under Vista 64-bit, it took a minute or so to
hibernate, and 30 seconds or so to wake up.  7 takes 30 seconds or so to
hibernate, and 15 seconds to wake up.  Not astronomical, but coupled with
the next point, it's HUGE.

- Better wireless networking.  Coming out of sleep or hibernation, it used
to take up to a minute and a half to connect to a known wireless network (in
other words, a network I'd previously connected to and saved settings for.)
Now, I'm generally connected even before I can see the desktop - 3 to 5
seconds, maybe.  I carry my laptop everywhere, and connect to wireless
networks at my clients (and Starbucks!) all day long, so this is a MAJOR
deal for me.

- Seriously improved multi-monitor support.  I've loved using dual m

Re: [Tutor] Advice on math

2010-06-27 Thread Modulok
>> Hello, I've enjoying learning python for the first few years, and
>> appreciate all the help I have received from this list. I had some
>> interest in programming and am very glad to have made python my choice
>> of programming language. Since I didn't go to college for computer
>> studies, I feel my math is kind of rusty in some areas, and looks like
>> improving/regaining math skills would help a lot in programming,
>> logic, algorithms, etc.
>> Right now I think I need a better understanding of logarithm, arrays
>> (matrixes), and even probability. What links would you all recommend
>> that would be useful for me to learn these things in a newbie-friendly
>> way?
>>
>>
>>
> If you want to code+learn, nothing beats ProjectEuler (
> http://projecteuler.net/ ). However I assume you have basic understanding of
> maths.

Not to hijack the thread, but that's awesome! Only when you solve the
problems and then read the forum thread, do you realize how crude your
own solutions are, compared to some very clever fellows. Far better
than a crossword to keep things stimulated.

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


Re: [Tutor] class questions

2010-06-27 Thread Hugo Arts
On Sun, Jun 27, 2010 at 2:09 AM, Steven D'Aprano  wrote:
> On Sun, 27 Jun 2010 03:05:16 am Payal wrote:
>>
>> Can you give any simple example where this simple mro will work
>> incorrectly?
>
> Probably not... it's quite complicated, which is why it's rare. I'll
> have a think about it and see what I can come up with.
>

Here's my attempt. Consider this simple Diamond hierarchy:

class A:
def x(self): return "in A"

class B(A): pass
class C(A):
def x(self): return "in C"

class D(B, C): pass

   A
  / \
 /   \
B   C
 \/
  \  /
   D

Now, with this diagram the following code probably doesn't do what you expect:

>>> obj = D()
>>> obj.x()
'in A'

D inherits from C, which overrides the x method. But this is seemingly
completely ignored by python, which produces the A.x method! So why
does this happen?

well, the old MRO uses a simple depth-first, left-to-right search.
This means that to find a method, python first looks into the leftmost
parent, then its leftmost parent, et cetera et cetera. For our
diamond, that means this MRO:

D, B, A, C, A

so, when you try to access D.x, it will look first in D (not found), B
(not found), then A, producing the A.x method. And this probably isn't
what you want (we inherited from C for a reason, you know).

For new style classes, the MRO is actually D, B, C, A. That produces
the correct results. The algorithm is a little complicated, and if you
would like to know, the link you posted earlier has all the details.

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


Re: [Tutor] class questions

2010-06-27 Thread Payal
Hi Hugo,

On Sun, Jun 27, 2010 at 01:27:37PM +0200, Hugo Arts wrote:
> Here's my attempt. Consider this simple Diamond hierarchy:
[...]
> Now, with this diagram the following code probably doesn't do what you expect:

Actually, it does what is expected. The old mro specifically says,
bottom-top, left-right. So we expected D-B-A-C-A.
Steven says in some cases even this simple logic of bottom-top,
left-right search will not work. As he says, a simple example of it
failing is diffclt to write, so meanwhile I will take his word for it.

Thanks for the help.
With warm regards,
-Payal
-- 

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


Re: [Tutor] class questions

2010-06-27 Thread Payal
Hi Steven,

Thanks a lot for patiently explaining the concepts. I uderstood most of
it.

With warm regards,
-Payal
-- 


On Sun, Jun 27, 2010 at 10:09:38AM +1000, Steven D'Aprano wrote:
> Probably not... it's quite complicated, which is why it's rare. I'll 
> have a think about it and see what I can come up with.
___
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-27 Thread Alan Gauld


"Steven D'Aprano"  wrote

having no default email client (what sort of two-bit operating 
system

doesn't have an email client in 2010?);


To be fair to MS - and it pains me to do so - they have been beat up
so much by the lawyers that its hardlly surprising. After all they
have been forced to remove the browser from the OS in Europe
(and elsewhere?). Why not the email client too? After all, Linux 
doesn't

tecchnically have a mail client (unless you count command line mail),
it's the distros that add that.

the automatic upgrades that run silently in the background with no 
easy

way to turn them on or off


Yes I really hate that!


the gratuitous UI changes (am I missing something, or does Internet
Explorer no longer have a menubar?);


None of the new MS applications do, the menu bar has been
merged into the new look toolbar. Thus making something small
and unobtrusive large and extremely obtrusive and requiring a
relearning of the whole command structure! I really don't like the
new MS UI thing, even after 6 montrhs of use it still drives me nuts!
(I use Win7 on my laptop at work)

the use of third-party applications like Adobe Acrobat Reader which 
have

become overloaded with *stupid* security vulnerabilities *by design*


But that applies to all OS that Reader runs on doesn't it?
Can't blame Windows 7 for that.


consequently the proliferation of adware and spyware (even


Yep, agree with this too.


On the other hand, it is quite pretty.


Hmm, I'm stlll not sure about that...
But then, I srtill run Windows XP in Classic mode with Windows95
style menu and start button etc.

Alan G.


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


Re: [Tutor] class questions

2010-06-27 Thread Payal
Thanks a lot Eike for the code snippet. I got the idea now.

With warm regards,
-Payal
-- 


On Sat, Jun 26, 2010 at 10:41:59PM +0200, Eike Welk wrote:
> Hello Payal!
> 
> On Saturday June 26 2010 19:05:16 Payal wrote:
> > Can we say that our own exception classes have only maybe a doc-string
> > and pass, nothing more?
> 
> No, you let the exception transport the information that you need for 
> handling 
> the error. This is an exception that I use to transport user visible error 
> messages in a compiler, that I write as a hobby:
> 
> 
> class UserException(Exception):
> '''Exception that transports user visible error messages.'''
> def __init__(self, message, loc=None, errno=None):
> Exception.__init__(self)
> self.msg = message
> self.loc = loc
> self.errno = errno
> 
> def __str__(self):
> if self.errno is None:
> num_str = ''
> else:
> num_str = '(#%s) ' % str(self.errno) 
> return 'Error! ' + num_str + self.msg + '\n' + str(self.loc) + '\n'
> 
> def __repr__(self):
> return self.__class__.__name__ + str((self.msg, self.loc, 
>   self.errno))
> 
> It contains:
> self.msg : The error message
> self.loc : An object encoding the location of the error in the program's
>text. Together with the file name and the text.
> self.errno : An integer to identify the error, for the test framework.
> 
> That said; the expression's type and the error message are often sufficient 
> to 
> handle the error.
> 
> 
> Eike.
___
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-27 Thread Alan Gauld

"Marc Tompkins"  wrote

having no default email client (what sort of two-bit operating 
system

doesn't have an email client in 2010?);

Jesus, you _miss_ Outlook Express?  Seriously:  the new default is 
webmail.
Like it, don't like it, but it's really not as if you can't get your 
mail.


I will miss OE. I actually quite like it, its simple but has all the 
bits

I need for both email and newrgroups. I tried thunderbird and use it
on my Linux box but on windows I usually revert to OE.

And its lots better than webmail which is only useful for occasional
browsing. But I get around 200 plus emails a day (and sometimes the
same again in news messages) and trying to manage that on webmail
is a nightmare - and you can't read it while offline. I really need an
offline mail client.

Just like XP and Vista, you're asked during installation whether you 
want to
allow or disallow automatic updates.  If you breeze past that 
question, then
- just like in XP and Vista - you can right-click on the little icon 
that

appears in your system tray (oops, I mean "notification area.")


OOh. I've never noticed the icon - what does it look like? I didn't
do the install so had no say in the decision for work, but for my
home PC I'd much rather decide if/when I do "upgrades" - I've had
too mamy Windows upgrades kill my PC to the point of needing
rebuiilds!

To paraphrase, "what sort of two-bit ISP doesn't have a webmail site 
in

2010?"


But webmail is no good if you've used up your bandwidth.
A background client might just have received the warning
before the quota went bang...

- Better wireless networking.  Coming out of sleep or hibernation, 
it used
to take up to a minute and a half to connect to a known wireless 
network


I'll need to check that - I've just gotten used to going for a coffee 
when I

boot up - it usually takes me around 5 minutes for everything to get
started so I've never noticed the WiFi changes.

- Seriously improved multi-monitor support.  I've loved using dual 
monitors


I only user this when doing powerpoint presentations but I'll need
to take a closer look.

Thanks for sharing your comments, even if this thread is seriously
off topic!

Alan G. 



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


Re: [Tutor] socket

2010-06-27 Thread Alan Gauld


"Christopher King"  wrote

process. In other words, I want to connect two computers. I've 
looked up

socket but the explanations are too complex. I think I need a 2-way
conversation with a expert to understand it. Or even better, point 
me to a

easier module.


You can read the networking topic in my tutorial;(V2 only) and see
a simple explanation with examples. It might be helpful to read the
previous two topics as well as a general background to communicating
between processes.

--
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


[Tutor] retrieve URLs and text from web pages

2010-06-27 Thread Khawla Al-Wehaibi
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.

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. 
2.    I'm not sure if it functions properly.
3.    Is there a better way to extract text?
4.    Are there any available modules to help clean the text i.e. removing 
duplicates, non-stop words ...
5.    Any suggestions or feedback is appreciated.

(Please Note: the majority of the code (the first part) is written by “James 
Mills”. I found the code online and it looks helpful so I used it. I just 
modified it and added my code to it)

Thanks,
Kal

import sys
import urllib2
import urlparse
from BeautifulSoup import BeautifulSoup,NavigableString

__version__ = "0.1"
__copyright__ = "CopyRight (C) 2008 by James Mills"
__license__ = "GPL"
__author__ = "James Mills"
__author_email__ = "James Mills, James dot Mills st dotred dot com dot au"

USAGE = "%prog [options] "
VERSION = "%prog v" + __version__

AGENT = "%s/%s" % (__name__, __version__)

def encodeHTML(s=""):
    """encodeHTML(s) -> str

    Encode HTML special characters from their ASCII form to
    HTML entities.
    """

    return s.replace("&", "&") \
    .replace("<", "<") \
    .replace(">", ">") \
    .replace("\"", """) \
    .replace("'", "'") \
    .replace("--", "&mdash")

class Fetcher(object):

    def __init__(self, url):
    self.url = url
    self.urls = []

    def __contains__(self, x):
    return x in self.urls

    def __getitem__(self, x):
    return self.urls[x]

    def _addHeaders(self, request):
    request.add_header("User-Agent", AGENT)

    def open(self):
    url = self.url
    #print "\nFollowing %s" % url
    try:
    request = urllib2.Request(url)
    handle = urllib2.build_opener()
    except IOError:
    return None
    return (request, handle)

    def fetch(self):
    request, handle = self.open()
    self._addHeaders(request)
    if handle:
    soup = BeautifulSoup()
    try:
    content = unicode(handle.open(request).read(), errors="ignore")
    soup.feed(content)
    #soup = BeautifulSoup(content)
    tags = soup('a')
    except urllib2.HTTPError, error:
    if error.code == 404:
    print >> sys.stderr, "ERROR: %s -> %s" % (error, error.url)
    else:
    print >> sys.stderr, "ERROR: %s" % error
    tags = []
    except urllib2.URLError, error:
    print >> sys.stderr, "ERROR: %s" % error
    tags = []
    for tag in tags:
    try:
    href = tag["href"]
    if href is not None:
    url = urlparse.urljoin(self.url, encodeHTML(href))
    if url not in self:
    #print " Found: %s" % url
    self.urls.append(url)
    except KeyError:
    pass


# I created 3 lists (root, level2 and level3). #
# Each list saves the URLs of that level i.e. depth. I choose to create 3  #
# lists so I can have the flexibility of testing the text in each level. Also, #
# the 3 lists can be easily combined into one list.    #


# Level1: 
root = Fetcher('http://www.wantasimplewebsite.co.uk/index.html')
root.fetch()
for url in root:
    if url not in root: # Avoid duplicate links 
   root.append(url)
   
print "\nRoot URLs are:"
for i, url in enumerate(root):
   print "%d. %s" % (i+1, url)

# Level2: 
level2 = []
for url in root: # Traverse every element(i.e URL) in root and fetch the URLs 
from it
    temp = Fetcher(url)
    temp.fetch()
    for url in temp:
    if url not in level2: # Avoid duplicate links 
   level2.append(url)   
   
print "\nLevel2 URLs are:"
for i, url in enumerate(level2):
   print "%d. %s" % (i+1, url)
 
# Level3: 
level3 = []
for url in level2: # Traverse every element(i.e URL) in level2 and fetch the 
URLs from it
    temp = Fetcher(url)
    temp.fetch()
    for url in temp:
    if url not in level3: # Avoid duplicate links
   leve

Re: [Tutor] class questions

2010-06-27 Thread Hugo Arts
On Sun, Jun 27, 2010 at 4:13 PM, Payal  wrote:
> Hi Hugo,
>
> On Sun, Jun 27, 2010 at 01:27:37PM +0200, Hugo Arts wrote:
>> Here's my attempt. Consider this simple Diamond hierarchy:
> [...]
>> Now, with this diagram the following code probably doesn't do what you 
>> expect:
>
> Actually, it does what is expected. The old mro specifically says,
> bottom-top, left-right. So we expected D-B-A-C-A.
> Steven says in some cases even this simple logic of bottom-top,
> left-right search will not work. As he says, a simple example of it
> failing is diffclt to write, so meanwhile I will take his word for it.
>

The problem of the MRO isn't that it doesn't work, it's that it causes
behavior that is unintuitive. In my example, We would expect D.x to be
equal to C.x (since D inherits from C, and C overrides the x method).
However, this is not the case. This is what the problem is with the
old MRO system, not so much that it doesn't work at all, but the
results aren't consistent with what you'd expect from multiple
inheritance.

I just found this link, where Guido explains why he changed the MRO
system for new-style classes. He uses the same example I do.

http://www.python.org/download/releases/2.2.2/descrintro/#mro

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


[Tutor] capturing error msg in exception

2010-06-27 Thread Payal
Hi,
Again a few queries regarding exceptions,

a. What is the difference between,

except ZeroDivisionError as e: print 'Msg : ' , e
except ZeroDivisionError ,e: print 'Msg : ' , e

Both show,
Msg :  integer division or modulo by zero

b. What is portable and correct way of writing,

except (NameError, ZeroDivisionError) as e: print 'Msg : ' , e

c. What is the correct Python of writing,
except  as e: print 'Msg : ' , e# Capturing all exceptions

Thanks a lot for the help in advance.
With warm regards,
-Payal
-- 

p.s. I am always confused where does one put the "?" when I am framing the
questions like I have done above (a, b and c)? 
This is completely OT query for native english speaking people :-)

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


Re: [Tutor] class questions

2010-06-27 Thread Shashwat Anand


> The problem of the MRO isn't that it doesn't work, it's that it causes
> behavior that is unintuitive. In my example, We would expect D.x to be
> equal to C.x (since D inherits from C, and C overrides the x method).
> However, this is not the case. This is what the problem is with the
> old MRO system, not so much that it doesn't work at all, but the
> results aren't consistent with what you'd expect from multiple
> inheritance.
>
>
I tried your example in python2.6

>>> class A:
...def x(self): return "in A"
...
>>> class B(A): pass
...
>>> class C(A):
...def x(self): return "in C"
...
>>> class D(B, C): pass
...
>>> o = D()
>>> o.x()
'in A'

If MRO was in python2.3, the output of o.x() should be 'in C'. It works fine
on 3.2alpha though.


> I just found this link, where Guido explains why he changed the MRO
> system for new-style classes. He uses the same example I do.
>
> http://www.python.org/download/releases/2.2.2/descrintro/#mro
>
>
This was the only useful link I found upon googling.
Thanks for your effort though.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] statements vs. expression and types

2010-06-27 Thread Payal
Hello,
Please forgive for such a silly questions. I have never taken a course on
computers so am rough with some of basic technical terminology.

a. I read in a book that "lambdas are expressions ... so they can go
where functions are not allowed". What are expressions and what are
statements? A simple example of both in Python will suffice.

b. I read on web that Python has in new releases done "unifications of
types and classes". I know what classes are, what are types in simple
language.

Again please excuse for simple questions and thanks a lot in advance.

With warm regards,
-Payal
-- 



___
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-27 Thread Richard D. Moores
On Sun, Jun 27, 2010 at 07:41, Alan Gauld  wrote:
> "Marc Tompkins"  wrote

> I will miss OE. I actually quite like it, its simple but has all the bits
> I need for both email and newrgroups. I tried thunderbird and use it
> on my Linux box but on windows I usually revert to OE.
>
> And its lots better than webmail which is only useful for occasional
> browsing. But I get around 200 plus emails a day (and sometimes the
> same again in news messages) and trying to manage that on webmail
> is a nightmare - and you can't read it while offline. I really need an
> offline mail client.

I get 200-300 emails per weekday, and Gmail handles them with aplomb.
I have over 300 labels (it's now, finally, possible to nest them).
(Tip: use [(From:address) OR (From:me AND ((To: OR Cc:)address))] --
delete the square brackets -- in the "Has the words" textbox when
making a filter that will apply to all mail from yourself to: or cc'd
to an address OR from an address). Labels can be nested and color
coded. Be sure to check out the numerous other features, including the
ones on the "Labs" tab. BTW I used Eudora for a decade, and also OE
because I had to teach it to my wife. A "feature" very important to me
is that with Gmail, my mail is just always THERE, with no need to
download it -- and often INSTANTLY there because there are so many
other Gmail users. I'll stop my praise of Gmail here because this is
an OT of an OT thread :) .

Dick
___
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-27 Thread Adam Bark
On 27 June 2010 17:47, Payal  wrote:

> Hi,
> Again a few queries regarding exceptions,
>
> a. What is the difference between,
>
> except ZeroDivisionError as e: print 'Msg : ' , e
> except ZeroDivisionError ,e: print 'Msg : ' , e
>
> Both show,
> Msg :  integer division or modulo by zero
>
> b. What is portable and correct way of writing,
>
> except (NameError, ZeroDivisionError) as e: print 'Msg : ' , e
>
> c. What is the correct Python of writing,
> except  as e: print 'Msg : ' , e# Capturing all exceptions
>
> Thanks a lot for the help in advance.
> With warm regards,
> -Payal
> --
>
> p.s. I am always confused where does one put the "?" when I am framing the
> questions like I have done above (a, b and c)?
> This is completely OT query for native english speaking people :-)
>
>
I think the 'as' syntax is only available in Python 3.x


Question marks go at the end of the sentence where you would normally put a
full stop if it wasn't a question.
___
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-27 Thread Steve Willoughby

On 27-Jun-10 10:12, Adam Bark wrote:

On 27 June 2010 17:47, Payal 


c. What is the correct Python of writing,
except  as e: print 'Msg : ' , e# Capturing all exceptions


Since exceptions are (should be?) subclasses of Exception, you can do:

except Exception as e:


I think the 'as' syntax is only available in Python 3.x


It's in Python 2 as well.  At least I see it in 2.6.4, probably earlier 
too (but I'm not sure ATM how early it showed up).


___
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-27 Thread Adam Bark
On 27 June 2010 18:22, Steve Willoughby  wrote:

> On 27-Jun-10 10:12, Adam Bark wrote:
>
>> On 27 June 2010 17:47, Payal >
>
> c. What is the correct Python of writing,
>>except  as e: print 'Msg : ' , e# Capturing all exceptions
>>
>
> Since exceptions are (should be?) subclasses of Exception, you can do:
>
> except Exception as e:
>
>
>  I think the 'as' syntax is only available in Python 3.x
>>
>
> It's in Python 2 as well.  At least I see it in 2.6.4, probably earlier too
> (but I'm not sure ATM how early it showed up).
>
>
Ah yeah sorry I got confused, it's the comma version that is gone from 3.x
isn't it? I assume that means 'as' is the preferred syntax.
___
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-27 Thread Marc Tompkins
On Sun, Jun 27, 2010 at 7:41 AM, Alan Gauld wrote:

> "Marc Tompkins"  wrote
>
>  having no default email client (what sort of two-bit operating system
>>
>>> doesn't have an email client in 2010?);
>>>
>>>  Jesus, you _miss_ Outlook Express?  Seriously:  the new default is
>> webmail.
>> Like it, don't like it, but it's really not as if you can't get your mail.
>>
>
> I will miss OE. I actually quite like it, its simple but has all the bits
> I need for both email and newrgroups. I tried thunderbird and use it
> on my Linux box but on windows I usually revert to OE.
>
I always hated OE, but I suppose it does have its uses. Backing up OE was
always problematic...



> And its lots better than webmail which is only useful for occasional
> browsing. But I get around 200 plus emails a day (and sometimes the
> same again in news messages) and trying to manage that on webmail
> is a nightmare - and you can't read it while offline. I really need an
> offline mail client.

As Richard mentioned and I will second, GMail handles that volume easily;
Gears enables offline reading, and if you've drunk the Kool-Aid like I have,
integration with Android is seamless.  Also it's instantly portable from
machine to machine.

In any case, I only meant to whole-heartedly endorse upgrading from Vista to
7, and dismiss the loss of OE as a reason not to.  Embracing the GMail way
appears to be another one of those dreaded religious topics - I certainly
can feel myself morphing into a ranting fanatic...


>  Just like XP and Vista, you're asked during installation whether you want
>> to
>> allow or disallow automatic updates.  If you breeze past that question,
>> then
>> - just like in XP and Vista - you can right-click on the little icon that
>> appears in your system tray (oops, I mean "notification area.")
>>
>
> OOh. I've never noticed the icon - what does it look like? I didn't
> do the install so had no say in the decision for work, but for my
> home PC I'd much rather decide if/when I do "upgrades" - I've had
> too mamy Windows upgrades kill my PC to the point of needing
> rebuiilds!

It looks like a rectangular pane of glass (a Window, maybe?) with an orange
halo orbiting it.  I don't see it on my machine at the moment (because I -
wait for it - turned off automatic updates when I installed), so I'm a
little fuzzy on whether it's a left-click, double-click, or right-click that
gets you the option to turn off the auto updates.
Regardless, here's a more direct way:
   Control Panel\System and Security\Windows Update\Change settings


 - Better wireless networking.  Coming out of sleep or hibernation, it used
> to take up to a minute and a half to connect to a known wireless network
>

I'll need to check that - I've just gotten used to going for a coffee when I
> boot up - it usually takes me around 5 minutes for everything to get
> started so I've never noticed the WiFi changes.


I was driving on the freeway a while back, talking to my sister on the phone
(hands-free!) when she mentioned that her computer was giving her trouble.
I exited and pulled into a Starbucks I'd used before (so my machine already
knew "attwifi7" or whatever it is).  I opened my laptop, signed in, and used
Chrome to sign into the AT&T captive portal; within less than a minute of my
butt hitting the banquette I was in Copilot.  She and I commiserated about
the good old days when at least I could have ordered my coffee before I had
to get down to work... 8-)


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


Re: [Tutor] class questions

2010-06-27 Thread Hugo Arts
On Sun, Jun 27, 2010 at 6:47 PM, Shashwat Anand
 wrote:
> 
>>
>> The problem of the MRO isn't that it doesn't work, it's that it causes
>> behavior that is unintuitive. In my example, We would expect D.x to be
>> equal to C.x (since D inherits from C, and C overrides the x method).
>> However, this is not the case. This is what the problem is with the
>> old MRO system, not so much that it doesn't work at all, but the
>> results aren't consistent with what you'd expect from multiple
>> inheritance.
>>
>
> I tried your example in python2.6
 class A:
> ...    def x(self): return "in A"
> ...
 class B(A): pass
> ...
 class C(A):
> ...    def x(self): return "in C"
> ...
 class D(B, C): pass
> ...
 o = D()
 o.x()
> 'in A'
> If MRO was in python2.3, the output of o.x() should be 'in C'. It works fine
> on 3.2alpha though.
>

You forgot that the example as is still uses the old MRO, even in 2.3.
This is because these classes are still classic-style classes. To use
the new MRO you will have to use new-style classes (A must inherit
from object). In 3.x it will of course work fine, since everything
without explicit inheritance inherits from object.

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


Re: [Tutor] statements vs. expression and types

2010-06-27 Thread Hugo Arts
On Sun, Jun 27, 2010 at 6:57 PM, Payal  wrote:
> Hello,
> Please forgive for such a silly questions. I have never taken a course on
> computers so am rough with some of basic technical terminology.
>
> a. I read in a book that "lambdas are expressions ... so they can go
> where functions are not allowed". What are expressions and what are
> statements? A simple example of both in Python will suffice.
>

an expression is anything that yields a value. Simple expressions are
1
1 + 1
a
[1, 2, 3]
True and False
lambda: 5

but anything that has a value is an expression. Expressions can be on
the right side of an assignment statement.
A statement is kind of the opposite. It does not have a value.
Examples of statements:

return 5
def x(): pass
if True: sys.exit()
while x < 4: pass

> b. I read on web that Python has in new releases done "unifications of
> types and classes". I know what classes are, what are types in simple
> language.
>

a 'type,' in python before 2.2, was simply any of the builtin types,
that is, int, str, list, dict, and so on. The unification that
happened in 2.2 means that there are no more differences between a
builtin 'type' and a user-defined 'class.' Nowadays a type is just any
instance of the type class.

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


Re: [Tutor] TypeError when io.open is used

2010-06-27 Thread petkovas
Can you send the full error text please? I'm not sure which 
is line 55 and Python normally displays the faulty line as 
part of the error trace.


As it is I can't see any reason for it to fail but I'd like to 
be sure I'm looking at the right place!


Also is there any reason why you explicitly call io.open() 
instead of just using the built-in open() directly? I know 
they are the same function but its quite unusual to use 
the io version explicitly...


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


The full error message is:

Traceback :
   File "insert_into_db_v9.py", line 55, in 
  WHERE testtable_n = %s""", data1, 
str(os.path.splitext(file)[0]))

TypeError: an integer is required

And i would want to add that str() is not the problem. I have tried 
without it and the problem persisted.


I have tried the following, too:

from pg8000 import DBAPI
import os
import os.path
import sys

# !!! that is test data. It must be changed
conn=DBAPI.connect(host="localhost", database="postgres", 
user="postgres", password="test")


#conn.cursor will return a cursor oject, you can use this cursor to 
perform queries

cursor = conn.cursor()

file = open( 
"C:\\Blender_Library\\BlenderLib\\objectLib\\Faqns\\Osaka2\\faqns_osaka_2.jpg", 
"rb" )

data1 = DBAPI.Binary(file.read())
data2 = 'faqns_osaka_2'

# execute our Query
cursor.execute("UPDATE testtable SET jpeg = %s WHERE testtable_n = 
%s", data1, data2)

sys.stdout.flush()  

# Save (commit) the changes
conn.commit()

# We can also close the cursor if we are done with it
cursor.close()

The problem this time was:
Traceback :
   File "insertdb_pg8000.py", line 19, in 
  cursor.execute("UPDATE testtable SET jpeg = %s WHERE 
testtable_n = %s", data1, data2)

   File "build\bdist.win32\egg\pg8000\dbapi.py", line 243, in _fn
TypeError: execute() takes at most 3 arguments (4 given)


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


Re: [Tutor] TypeError when io.open is used

2010-06-27 Thread Marc Tompkins
On Sun, Jun 27, 2010 at 3:20 PM,  wrote:

> I have tried the following, too:
>
> from pg8000 import DBAPI
> import os
> import os.path
> import sys
>
> # !!! that is test data. It must be changed
> conn=DBAPI.connect(host="localhost", database="postgres", user="postgres",
> password="test")
>
> #conn.cursor will return a cursor oject, you can use this cursor to perform
> queries
> cursor = conn.cursor()
>
> file = open(
> "C:\\Blender_Library\\BlenderLib\\objectLib\\Faqns\\Osaka2\\faqns_osaka_2.jpg",
> "rb" )
> data1 = DBAPI.Binary(file.read())
> data2 = 'faqns_osaka_2'
>
> # execute our Query
> cursor.execute("UPDATE testtable SET jpeg = %s WHERE testtable_n = %s",
> data1, data2)
> sys.stdout.flush()
>
> # Save (commit) the changes
> conn.commit()
>
> # We can also close the cursor if we are done with it
> cursor.close()
>
> The problem this time was:
> Traceback :
>   File "insertdb_pg8000.py", line 19, in 
>  cursor.execute("UPDATE testtable SET jpeg = %s WHERE testtable_n =
> %s", data1, data2)
>   File "build\bdist.win32\egg\pg8000\dbapi.py", line 243, in _fn
> TypeError: execute() takes at most 3 arguments (4 given)
>
>
I don't have any insight into your other piece of code, but here I think you
just need another set of parentheses - so that the string interpolation is
done first, and the result of it becomes the argument to cursor.execute().
I can't really test it at the moment, but try changing it to:
cursor.execute( ("UPDATE testtable SET jpeg = %s WHERE testtable_n = %s",
data1, data2) )

Either that, or break it into two lines:

myQuery = "UPDATE testtable SET jpeg = %s WHERE testtable_n = %s", data1,
data2
cursor.execute(myQuery)

-- 
www.fsrtechnologies.com
___
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-27 Thread Steven D'Aprano
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?

*wry grin*


-- 
Steven D'Aprano
___
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-27 Thread Steven D'Aprano
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.


> 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:

[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.


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


Re: [Tutor] TypeError when io.open is used

2010-06-27 Thread Steven D'Aprano
On Mon, 28 Jun 2010 08:20:01 am petko...@dir.bg wrote:

> The full error message is:
>
> Traceback :
> File "insert_into_db_v9.py", line 55, in 
>WHERE testtable_n = %s""", data1,
> str(os.path.splitext(file)[0]))
> TypeError: an integer is required

Unfortunately this isn't helpful, because we're only seeing part of the 
offending line of code. This is because Python tracebacks only show the 
*physical* line which fails, but a logical line of code can be split 
over more than one physical line.

E.g. if I have a module like this:

# test.py
x = 23
y = 23-x
z = (42/
  y)

and then import it:

>>> import test
Traceback (most recent call last):
  File "", line 1, in 
  File "test.py", line 5, in 
y)
ZeroDivisionError: integer division or modulo by zero

it only shows me the last part of the expression (42/y).

So you will need to look at your source code and manually copy and paste 
the rest of the logical line, that is, the line or lines immediately 
before line 55.


> And i would want to add that str() is not the problem. I have tried
> without it and the problem persisted.

The call to str() is totally pointless, since os.path.splitext(file)[0] 
is already a string.


[...]
> file = open(
> "C:\\Blender_Library\\BlenderLib\\objectLib\\Faqns\\Osaka2\\faqns_osa
>ka_2.jpg", "rb" )

Windows accepts forwards slashes for pathnames too, so the above can 
more simply be written:

"C:/Blender_Library/BlenderLib/objectLib/Faqns/Osaka2/faqns_osaka_2.jpg"

with less chance of error.


> The problem this time was:
> Traceback :
> File "insertdb_pg8000.py", line 19, in 
>cursor.execute("UPDATE testtable SET jpeg = %s WHERE
> testtable_n = %s", data1, data2)
> File "build\bdist.win32\egg\pg8000\dbapi.py", line 243, in _fn
> TypeError: execute() takes at most 3 arguments (4 given)


I'm afraid this is an example of a *slightly* misleading error message 
in Python. the cursor.execute method takes at most three arguments:

# Something like this.
class Cursor:
def execute(self, a, b):
pass


*but* one of those arguments, self, is automatically filled in by 
Python. So when you call execute, you can only supply TWO arguments:

cursor.execute(a, b)

If you provide three:

cmd = "UPDATE testtable SET jpeg = %s WHERE testtable_n = %s"
cursor.execute(cmd, data1, data2)

then Python fills in self and gives an error message that execute takes 
only three arguments, not four.

So you need to read the documentation for execute to find out what 
arguments it takes, because you can't pass all three of cmd, data1 and 
data2.



-- 
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-27 Thread Richard D. Moores
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.

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


Re: [Tutor] TypeError when io.open is used

2010-06-27 Thread petkovas

On Sun, 27 Jun 2010 15:56:23 -0700
 Marc Tompkins  wrote:

On Sun, Jun 27, 2010 at 3:20 PM,  wrote:

I don't have any insight into your other piece of code, but here 
I think you
just need another set of parentheses - so that the string 
interpolation is
done first, and the result of it becomes the argument to 
cursor.execute().

I can't really test it at the moment, but try changing it to:
cursor.execute( ("UPDATE testtable SET jpeg = %s WHERE 
testtable_n = %s",

data1, data2) )

Either that, or break it into two lines:

myQuery = "UPDATE testtable SET jpeg = %s WHERE testtable_n = 
%s", data1,

data2
cursor.execute(myQuery)

-- www.fsrtechnologies.com


Thank you for the suggestion that i should enforce the parantheses. At 
least that changed the error. Unfortunately that is wierd one, too:


Traceback :
   File "insertdb_pg8000.py", line 20, in 
  cursor.execute( ("UPDATE testtable SET jpeg = %s WHERE 
testtable_n = %s", data1, data2) )

   File "build\bdist.win32\egg\pg8000\dbapi.py", line 243, in _fn
   File "build\bdist.win32\egg\pg8000\dbapi.py", line 314, in execute
   File "build\bdist.win32\egg\pg8000\dbapi.py", line 319, in 
_execute
   File "build\bdist.win32\egg\pg8000\interface.py", line 303, in 
execute
   File "build\bdist.win32\egg\pg8000\interface.py", line 108, in 
_init_

   File "build\bdist.win32\egg\pg8000\protocol.py", line 913, in _fn
   File "build\bdist.win32\egg\pg8000\protocol.py", line 1048, in 
parse
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 
49: ordinal not in range()


I mean, as far as i know, for binary files i do not need to set 
encoding when i open them.


PS: sorry for the direct E-Mail to Marc.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] TypeError when io.open is used

2010-06-27 Thread Marc Tompkins
On Sun, Jun 27, 2010 at 5:13 PM,  wrote:

> On Sun, 27 Jun 2010 15:56:23 -0700
>  Marc Tompkins  wrote:
>
>> On Sun, Jun 27, 2010 at 3:20 PM,  wrote:
>>
>> I don't have any insight into your other piece of code, but here I think
>> you
>> just need another set of parentheses - so that the string interpolation is
>> done first, and the result of it becomes the argument to cursor.execute().
>> I can't really test it at the moment, but try changing it to:
>> cursor.execute( ("UPDATE testtable SET jpeg = %s WHERE testtable_n = %s",
>> data1, data2) )
>>
>> Either that, or break it into two lines:
>>
>> myQuery = "UPDATE testtable SET jpeg = %s WHERE testtable_n = %s", data1,
>> data2
>> cursor.execute(myQuery)
>>
>> --
>> www.fsrtechnologies.com
>>
>
> Thank you for the suggestion that i should enforce the parantheses. At
> least that changed the error. Unfortunately that is wierd one, too:
>
> Traceback :
>   File "insertdb_pg8000.py", line 20, in 
>
>  cursor.execute( ("UPDATE testtable SET jpeg = %s WHERE testtable_n =
> %s", data1, data2) )
>   File "build\bdist.win32\egg\pg8000\dbapi.py", line 243, in _fn
>   File "build\bdist.win32\egg\pg8000\dbapi.py", line 314, in execute
>   File "build\bdist.win32\egg\pg8000\dbapi.py", line 319, in _execute
>   File "build\bdist.win32\egg\pg8000\interface.py", line 303, in execute
>   File "build\bdist.win32\egg\pg8000\interface.py", line 108, in _init_
>   File "build\bdist.win32\egg\pg8000\protocol.py", line 913, in _fn
>   File "build\bdist.win32\egg\pg8000\protocol.py", line 1048, in parse
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 49:
> ordinal not in range()
>
> I mean, as far as i know, for binary files i do not need to set encoding
> when i open them.
>

OK - when I answered before, I was only looking at the error message and the
actual line of code that generated it; I hadn't really looked at what you're
trying to do.

Now that I've stepped back a bit, here's what I think you're trying to
achieve:
- open the file "faqns_osaka_2.jpg"
- read its contents
- write the contents to a BLOB field (or whatever the Postgres equivalent is
- anyway, a field that can hold an arbitrary chunk of data)
- write the name 'faqns_osaka_2' to a label field.

Unfortunately, you're trying to build the SQL command to do that using
string interpolation (the %s notation.)  So your program gets as far as
reading the contents of the file, and then tries to decode them into a
string  - and that's why you get this error.  Be happy you got the error -
if it had succeeded, the resulting binary data would have been unreadable as
an image, and you wouldn't have known why.

I'm certain that there are proper ways to pass chunks of binary data for
insertion into BLOB fields - people must do this every day - but I don't
actually know them.

Here's a possibility:
http://book.opensourceproject.org.cn/lamp/python/pythoncook2/opensource/0596007973/pythoncook2-chp-7-sect-11.html

Or try searching for "python postgres blob".



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