Re: [Tutor] Calling super in __init__

2008-05-19 Thread wcyee
Kent and Paul, Thanks very much for your help!



On Mon, May 19, 2008 at 8:43 AM, Paul McGuire <[EMAIL PROTECTED]> wrote:

> >>
> Hi - I wrote a custom exception class as follows:
>
> class CustomError(Exception):
>def __init__(self, msg):
>super(CustomError, self).__init__(self, msg)
>
> But this doesn't work as expected:
> >>
>
> Correct use of super would be:
>
> class CustomError(Exception):
>def __init__(self, msg):
> super(CustomError, self).__init__(msg)
>
> (Don't add self as an argument.)
>
> Exception became a new-style class in Python 2.5.
>
> -- Paul
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timed functions? Timeouts?

2008-05-19 Thread W W
Those examples are really helpful and I'm pretty sure they'll do what
I need, I'll just have to play with them a little more.

I do have a question about one of the examples though!

Specifically this part:

  4 class Operation(threading._Timer):
  5 def __init__(self, *args, **kwargs):
  6 threading._Timer.__init__(self, *args, **kwargs)
  7 self.setDaemon(True)

So in trying to understand the class definition, I found this:

"In Python, the ancestor of a class is simply listed in parentheses
immediately after the class name."

So does that mean that Operation is similar or the same as say:

Operation = threading._Timer?

Then I'm a little confused by the * and ** - they look just like the
pointer and pointer to a pointer in C++, but do they perform the same
function in python?

TIA,
Wayne

On Mon, May 19, 2008 at 8:23 PM, Kent Johnson <[EMAIL PROTECTED]> wrote:
> On Mon, May 19, 2008 at 5:28 PM, W W <[EMAIL PROTECTED]> wrote:
>> Now, when I look at that example and try to translate the timeout to
>> what I think is going on "behind the scenes" I'm pretty sure it spawns
>> a thread that does something similar to this:
>>
>>   1 import time
>>  2
>>  3 def timer(end, exe):
>>  4 start = time.time()
>>  5 while True:
>>  6   now = time.time()
>>  7   if (now - start) >= end:
>>  8 break
>>  9 exe()
>>  10
>>  11 def execute_this():
>>  12for x in range(1, 10):
>>  13  print x
>>  14
>>  15 timer(5, execute_this)
>>
>> Am I correct? Is there a python built-in for this? Where should I go
>> to learn more? Is there anything "wrong" with using a function like
>> this?
>
> You are pretty much on the right track but you should put a
> time.sleep() in your loop or it will chew up all available CPU time.
> Even better, figure out how long you need to sleep.
>
> The sched module in the standard library might help though it doesn't
> seem to do any threading. Also look at these recipes:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496800
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/114644
>
> Kent
>



-- 
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pythoncom Tutorial

2008-05-19 Thread FT

> Hi!
>
> I noticed that when wanting to learn Pythoncom there is
> no real good
> accessible tutorial for it.
> Does anyone know where a good structured tutorial exists
> for the Com
> utilities so I can write my own screen reader program?
>

Python Programming on Win32 might help. There's also a couple of
articles on OnLamp.

http://www.oreilly.com/catalog/9781565926219/index.html

Mike

Hi!

A book is nice if you can see to read it. Still searching for the best
way to go on this. It was suggested to go to the API of windowes or ctypes.
I confress that I have not used ctypes when just hoping to find a python
format to get such things as the focus information when jumping from window
to window. Or tag along saying what is going on at the moment and location
you are in. The name fo the object, and the variable or data info at the
moment in time...

Bruce


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timed functions? Timeouts?

2008-05-19 Thread Kent Johnson
On Mon, May 19, 2008 at 5:28 PM, W W <[EMAIL PROTECTED]> wrote:
> Now, when I look at that example and try to translate the timeout to
> what I think is going on "behind the scenes" I'm pretty sure it spawns
> a thread that does something similar to this:
>
>   1 import time
>  2
>  3 def timer(end, exe):
>  4 start = time.time()
>  5 while True:
>  6   now = time.time()
>  7   if (now - start) >= end:
>  8 break
>  9 exe()
>  10
>  11 def execute_this():
>  12for x in range(1, 10):
>  13  print x
>  14
>  15 timer(5, execute_this)
>
> Am I correct? Is there a python built-in for this? Where should I go
> to learn more? Is there anything "wrong" with using a function like
> this?

You are pretty much on the right track but you should put a
time.sleep() in your loop or it will chew up all available CPU time.
Even better, figure out how long you need to sleep.

The sched module in the standard library might help though it doesn't
seem to do any threading. Also look at these recipes:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496800
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/114644

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] timed functions? Timeouts?

2008-05-19 Thread Alan Gauld


"W W" <[EMAIL PROTECTED]> wrote


The specific bit I'm looking at is this line

  the_timeout = setTimeout('writeTime();',500);

basically it calls the function again in 500ms, but rather than
pausing, I guess it spawns a new thread? I'm not really sure.


You might be able to use select() for this.

Or if in a GUI environment just create a timer.

Or create a thread and use sleep() within the thread.

Just some ideas at 01:10am. So they are probably mince!

Night all,

Alan G

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New Style Classes, __getitem__ and iteration

2008-05-19 Thread Kent Johnson
On Mon, May 19, 2008 at 5:21 PM, Marilyn Davis <[EMAIL PROTECTED]> wrote:
> Hi Tutors and Tutees,
>
> I've been teaching Python quite a while and a brilliant student asked a
> question that made me realize a big hole in my understanding.
>
> I think it is really magical that, when I define __getitem__ on classic
> classes, the built-in iterator uses it.
>
> But, when I make the same class as a new style class, I lose this behavior.

There are a couple of possible explanations, I'm not sure which is
right without looking into the implementation of sorted().

Wesley may be right, that sorted() sees that its argument is a list
and uses a lower-level access than __getitem__() to copy the list for
sorting.

In general, list operations are not implemented in terms of
__getitem__(). In other words, you can't subclass list(), override
__getitem__() and expect to change how all list operations work.
__getitem__() is not a bottleneck procedure.

It's also possible that sorted() just expects a sequence and uses the
iterator protocol to access the values of its argument. This is
certainly the case with your Circle class. This class doesn't define
an __iter__() method so Python uses the older iteration protocol based
on __getitem__().

With NewCircle, even if sorted() is accessing it with an iterator,
your __getitem__() will not be called because list implements
__iter__() so iteration will use the modern iteration protocol.

--

OK, I looked at the source a bit.

builtin_sorted(), in bltinmodule.c, calls PySequence_List() to create
a list to sort.
PySequence_List(), in abstract.c, creates a new list, then calls
_PyList_Extend() to add the new items.
_PyList_Extend(), in listobject.c, calls listextend() which is the
implementation of list.extend()
listextend() is clearly optimized for lists and tuples. I didn't dig
farther than that. For other data types it iterates over the argument.

A good discussion of the iterator protocol is here:
http://www.python.org/doc/2.2.3/whatsnew/node4.html

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] env variable

2008-05-19 Thread Alan Gauld


"james collins" <[EMAIL PROTECTED]> wrote


just wondering how to set up my env variable?
can't import scripts because my IDLE MacPython 2.5, can't find the 
module scripts.


You should try setting your PYTHONPATH environment vaqriable
in your bash shell.

.profile
or
.login

are the two files that you can use and I've forgotten which is
recommended for environment variables. Both work but one
can get called multiple times if I remember correctly.

i created the file with textwrangler and stored them in my home 
folder  in a folder i called python, on a macbook pro running mac os 
10.5.2


In bash the line should look like:

export PYTHONPATH=~/python

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] converting all files in a directory with subprocess

2008-05-19 Thread Alan Gauld


"Tim Michelsen" <[EMAIL PROTECTED]> wrote in 


below is some code that works

### convert all t2t docs in a directory.



for file in os.listdir(documentation_directory):
if fnmatch.fnmatch(file, '*.t2t'):


You might be able to do this more succinctly using 
the glob.glob() function...


Just a thought,

Alan G.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pythoncom Tutorial

2008-05-19 Thread Alan Gauld


"FT" <[EMAIL PROTECTED]> wrote

   I noticed that when wanting to learn Pythoncom there is no real 
good

accessible tutorial for it.
   Does anyone know where a good structured tutorial exists for the 
Com

utilities so I can write my own screen reader program?


Assuming you mean Pythonwin, the libraries for accessing the
Windows API that come as part of winall then the best tutorial
is the one in Mark Hammond's book "Python Progeramming
on Win32"

But it helps if you have a basic understanding of COM generally,
for example using it from within VBScript. Also familiarity with
the vanilla Windows API documentation helps and that is not
really part of the Pytthon access library. You should find plenty
of tutorials on the raw API on the net.

the objects and its methods so I can use a python program to read 
and
verbalize the objects as you focus on them and such. The only 
explanations I
found either have the descriptions scattered and no examples or 
structure on

how to use any of the com stuff.


You might find ctypes is better for that kind of thing. COM
is better when you want to remotely control another application
but for interceprting low level Windows messages ctypes
seems like a better approach - although I confess to not
having tried it!


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




   How to use the Makepy and when not to use it. How to get the 
objects and
its list of methods to be able to work with it for screen reading 
purposes.


   Thanks in advance.

   Bruce

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor




___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] New Style Classes, __getitem__ and iteration

2008-05-19 Thread wesley chun
>  I think it is really magical that, when I define __getitem__ on classic
>  classes, the built-in iterator uses it.
>
>  But, when I make the same class as a new style class, I lose this behavior.
>
>  I didn't realize that something was lost in defining a new style class.
>  Maybe it's something else that I'm missing.


marilyn,

the problem isn't related to you using a new-style class.  if you
ported the classic class directly, it works just fine:

class Circle2(object):
   def __init__(self, data, times):
   """Put the 'data' in a circle that goes around 'times' times."""
   self.data = data
   self.times = times

   def __getitem__(self, i):
   """circle[i] --> Circle.__getitem__(circle, i)."""
   l_self = len(self)
   print 'i, self.times, l_self:', i, self.times, l_self
   if i >= self.times * l_self:
   raise IndexError, \
 "Error raised: Circle object only goes around %d times"\
 % self.times
   return self.data[i % l_self]

   def __len__(self):
   return len(self.data)

$ ./circle_question.py
['a', 'a', 'a', 'd', 'd', 'd', 'n', 'n', 'n', 'o', 'o', 'o', 'r', 'r',
'r', 'u', 'u', 'u']

to really start to debug your code, change the call from sorted() to
list(), as in:

circle = Circle2("around", 3)
print list(circle)

the output will change to:

$ ./circle_question.py
['a', 'r', 'o', 'u', 'n', 'd', 'a', 'r', 'o', 'u', 'n', 'd', 'a', 'r',
'o', 'u', 'n', 'd']

when you call sorted() or list() on your object, it attempts to build
a new list consisting of the return values of each getitem() call.
the difference is that with your original class, it doesn't "end"
until the count has surpassed the number of times you want it (3) and
the number of elements total (6), meaning it stops when you go beyond
18.

i put in a tiny print statement in the __getitem__() code in all 3
classes. in the original classic class and my port to new-style
classes, that print statement told me everything i just said above.
the thing that is different about your new-style class is that it
subclasses list.   in your class that derives from list, it did not
execute your __getitem__() method even once.

that clues me in on my belief that the reason why it doesn't work is
because it is *already* a list.  calling list() or sorted() on it
attempts to create a new list, but since the original data structure
is a list, it merely just copies all of its references without using
getitem().

without looking at the source code, i can't confirm this.  can anyone
else "finish the sentence i started?"

hope this partially helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] New Style Classes, __getitem__ and iteration

2008-05-19 Thread Marilyn Davis
Hi Tutors and Tutees,

I've been teaching Python quite a while and a brilliant student asked a
question that made me realize a big hole in my understanding.

I think it is really magical that, when I define __getitem__ on classic
classes, the built-in iterator uses it.

But, when I make the same class as a new style class, I lose this behavior.

I didn't realize that something was lost in defining a new style class. 
Maybe it's something else that I'm missing.

Thank you for your insights.

Marilyn

p.s.

Here's some code that demonstrates my confusion:

#!/usr/bin/env python

class Circle:

def __init__(self, data, times):
"""Put the 'data' in a circle that goes around 'times' times."""
self.data = data
self.times = times

def __getitem__(self, i):
"""circle[i] --> Circle.__getitem__(circle, i)."""
l_self = len(self)
if i >= self.times * l_self:
raise IndexError, \
  "Error raised: Circle object only goes around %d times"\
  % self.times
return self.data[i % l_self]

def __len__(self):
return len(self.data)

class NewCircle(list):

def __init__(self, data, times):
list.__init__(self, data)
self.times = times

def __getitem__(self, i):
l_self = len(self)
if i >= self.times * l_self:
raise IndexError, \
  "Error raised: NewCircle object only goes around %d times"\
  % self.times
return list.__getitem__(self, i % l_self)

def main():
circle = Circle("around", 3)
print sorted(circle)
new_circle = NewCircle("around", 3)
print sorted(new_circle)

main()
"""
OUTPUT:

$ ./circle_question.py
['a', 'a', 'a', 'd', 'd', 'd', 'n', 'n', 'n', 'o', 'o', 'o', 'r', 'r',
'r', 'u', 'u', 'u']
['a', 'd', 'n', 'o', 'r', 'u']
$

"""


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] timed functions? Timeouts?

2008-05-19 Thread W W
Hi, I'm having trouble finding any information (or if it's possible)
on this particular topic.

Specifically, there's a feature in javascript that I want to emulate.

this is an example I pulled from a webmonkey tutorial

--
function writeTime() {

   // get a date object
   var today = new Date();

   // ask the object for some information
   var hours = today.getHours();
   var minutes = today.getMinutes();
   var seconds = today.getSeconds();

   // fixTime makes the minutes and seconds look right
   // it just sticks a zero in front of numbers less than 10
   minutes = fixTime(minutes);
   seconds = fixTime(seconds);

   // put together the time string and write it out
   var the_time = hours + ":" + minutes + ":" + seconds;
   window.document.the_form.the_text.value = the_time;

   // run this function again in half a second
   the_timeout= setTimeout('writeTime();',500);

 }

 function fixTime(the_time) {

if (the_time <10)
{
the_time = "0" + the_time;
}
return the_time;
 }
---

The specific bit I'm looking at is this line

   the_timeout = setTimeout('writeTime();',500);

basically it calls the function again in 500ms, but rather than
pausing, I guess it spawns a new thread? I'm not really sure.
>From the little experience I have in JS it appears that multiple
"timers" can be running at a time, all the code "in between" the
timeouts will execute, and when the timeout is finished it will
execute whatever the command is (in this case, 'writeTime();').

So I'm trying to find out a pythonic equivalent (if there is one). So
far Googling for "python timeout" gives me no helpful results, and
"python threading" gives me some examples that are close to what I
think I'm looking for.

Now, when I look at that example and try to translate the timeout to
what I think is going on "behind the scenes" I'm pretty sure it spawns
a thread that does something similar to this:

   1 import time
  2
  3 def timer(end, exe):
  4 start = time.time()
  5 while True:
  6   now = time.time()
  7   if (now - start) >= end:
  8 break
  9 exe()
 10
 11 def execute_this():
 12for x in range(1, 10):
 13  print x
 14
 15 timer(5, execute_this)

Am I correct? Is there a python built-in for this? Where should I go
to learn more? Is there anything "wrong" with using a function like
this?

Thanks in advance,
Wayne
-- 
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] env variable

2008-05-19 Thread james collins

just wondering how to set up my env variable?
can't import scripts because my IDLE MacPython 2.5, can't find the  
module scripts.
i created the file with textwrangler and stored them in my home folder  
in a folder i called python, on a macbook pro running mac os 10.5.2

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Pythoncom Tutorial

2008-05-19 Thread Hansen, Mike
 

> -Original Message-
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of FT
> Sent: Monday, May 19, 2008 12:04 PM
> To: Tutor Python ORG
> Subject: [Tutor] Pythoncom Tutorial
> 
> 
> Hi!
> 
> I noticed that when wanting to learn Pythoncom there is 
> no real good
> accessible tutorial for it.
> Does anyone know where a good structured tutorial exists 
> for the Com
> utilities so I can write my own screen reader program?
> 
> When trying to understand all the needed variables and 
> such for the
> windows commands and variables it gets confusing. I want to 
> learn how to get
> the objects and its methods so I can use a python program to read and
> verbalize the objects as you focus on them and such. The only 
> explanations I
> found either have the descriptions scattered and no examples 
> or structure on
> how to use any of the com stuff.
> 
> How to use the Makepy and when not to use it. How to get 
> the objects and
> its list of methods to be able to work with it for screen 
> reading purposes.
> 
> Thanks in advance.
> 
> Bruce
> 

Python Programming on Win32 might help. There's also a couple of
articles on OnLamp.

http://www.oreilly.com/catalog/9781565926219/index.html

Mike
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] converting all files in a directory with subprocess

2008-05-19 Thread Tim Michelsen

Hello,
just for the records:
below is some code that works

### convert all t2t docs in a directory.


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import subprocess
import fnmatch

documentation_directory = './doc/'

for file in os.listdir(documentation_directory):
if fnmatch.fnmatch(file, '*.t2t'):

### assemble file name
filepath = documentation_directory+file

### run txt2tags
#subprocess.call(['txt2tags', '--target=html', '--toc', 
documentation_directory+file])

subprocess.call(['txt2tags', '--target=html', '--toc', filepath])

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Open a directory in the default file manager

2008-05-19 Thread Tim Michelsen

Hi!



 > is there any function/module that allows me to open a directory in the
 > default file manager of a operating system?

On Windows you can use os.startfile().
On "pure" Unices there's no such thing as filetype associations
However, if you use a desktop environment, you can spawn xdg-open (from 
xdg-utils) from Python. This will autodetect gnome, kde and xfce and use 
their tools (gnome-open, kfmclient, exo-open).

I think on OS X/Darwin there's a similar utility called `open'.

You can use sys.platform to determine which system you're running on.

I wasn't able to start explorer with os.startfile()

I then used
subprocess.call(['explorer', my_directory])

And it works.

Kind regards,
Timmie

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python and Plone Boot Camps in Chapel Hill, NC

2008-05-19 Thread Chris Calloway
Triangle (NC) Zope and Python Users Group (TriZPUG) is proud to open 
registration for our fourth annual ultra-low cost Plone and Python 
training camps, BootCampArama 2008:


http://trizpug.org/boot-camp/2008/

Registration is now open for:

PyCamp: Python Boot Camp, August 4 - 8

Plone Boot Camp: Customizing Plone, July 28 - August 1

Advanced Plone Boot Camp: Plone 3 Techniques, August 4 - 8

All of these take place on the campus of the University of North 
Carolina at Chapel Hill in state of the art high tech classrooms, with 
free mass transit, low-cost accommodations with free wireless, and 
convenient dining options.


Plone Boot Camp is taught by Joel Burton, twice chair of the Plone 
Foundation. Joel has logged more the 200 days at the head of Plone 
classrooms on four continents. See plonebootcamps.com for dozens of 
testimonials from Joel's students.


PyCamp is taught by Chris Calloway, facilitator for TriZPUG and 
application analyst for the Southeast Coastal Ocean Observing System. 
Chris has developed PyCamp for over 1500 hours on behalf of Python user 
groups. Early bird registration runs through June 30. So register today!


PyCamp is TriZPUG's Python Boot Camp, which takes a programmer familiar 
with basic programming concepts to the status of Python developer with 
one week of training. If you have previous scripting or programming 
experience and want to step into Python programming as quickly and 
painlessly as possible, this boot camp is for you. PyCamp is also the 
perfect follow-on to Plone Boot Camp: Customizing Plone the previous week.


At Plone Boot Camp: Customizing Plone you will learn the essentials you 
need to build your Plone site and deploy it. This course is the most 
popular in the Plone world--for a good reason: it teaches you practical 
skills in a friendly, hands-on format. This bootcamp is aimed at:

* people with HTML or web design experience
* people with some or no Python experience
* people with some or no Zope/Plone experience
It covers using Plone, customizing, and deploying Plone sites.

At Advanced Plone Boot Camp: Plone 3 Techniques you will learn to build 
a site using the best practices of Plone 3 as well as advance your 
skills in scripting and developing for Plone. The course covers the new 
technologies in Plone 3.0 and 3.1 intended for site integrators and 
developers: our new portlet infrastructure, viewlets, versioning, and a 
friendly introduction to Zope 3 component architecture. Now, updated for 
Plone 3.1! The course is intended for people who have experience with 
the basics of Plone site development and HTML/CSS. It will cover what 
you need to know to take advantage of these new technologies in Plone 3.


For more information contact: [EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Pythoncom Tutorial

2008-05-19 Thread FT

Hi!

I noticed that when wanting to learn Pythoncom there is no real good
accessible tutorial for it.
Does anyone know where a good structured tutorial exists for the Com
utilities so I can write my own screen reader program?

When trying to understand all the needed variables and such for the
windows commands and variables it gets confusing. I want to learn how to get
the objects and its methods so I can use a python program to read and
verbalize the objects as you focus on them and such. The only explanations I
found either have the descriptions scattered and no examples or structure on
how to use any of the com stuff.

How to use the Makepy and when not to use it. How to get the objects and
its list of methods to be able to work with it for screen reading purposes.

Thanks in advance.

Bruce

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calling super in __init__

2008-05-19 Thread Paul McGuire
>>
Hi - I wrote a custom exception class as follows:

class CustomError(Exception):
def __init__(self, msg):
super(CustomError, self).__init__(self, msg)

But this doesn't work as expected:
>>

Correct use of super would be:

class CustomError(Exception):
def __init__(self, msg):
super(CustomError, self).__init__(msg)

(Don't add self as an argument.)

Exception became a new-style class in Python 2.5.

-- Paul

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calling super in __init__

2008-05-19 Thread Kent Johnson
On Mon, May 19, 2008 at 3:27 AM, wcyee <[EMAIL PROTECTED]> wrote:
> Hi - I wrote a custom exception class as follows:
>
> class CustomError(Exception):
> def __init__(self, msg):
> super(CustomError, self).__init__(self, msg)

Should be
  super(CustomError, self).__init__(msg)
i.e. don't repeat 'self'.

> According to the documentation, "super() only works with new-style classes".
> Is the problem that Exception is an "old style" class (I'm still learning
> what this means)?

No

> If so, how can I tell when I'm up against an "old style"
> class when using python?

The type of a new-style class is 'type' (unless it has a custom
metaclass). The type of an old-style class is 'classobj':
In [20]: type(Exception)
Out[20]: 

In [21]: class Foo: pass
   :

In [22]: type(Foo)
Out[22]: 

New-style classes have a __getattribute__() special method which is
missing in old-style classes, so another way to tell is to look for
__getattribute__.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Calling super in __init__

2008-05-19 Thread wcyee
Hi - I wrote a custom exception class as follows:

class CustomError(Exception):
def __init__(self, msg):
super(CustomError, self).__init__(self, msg)

But this doesn't work as expected:

try:
raise CustomError('something bad')
except CustomError, err:
print err.message

err.message is a blank string. I fixed this eventually by rewriting the call
to the super constructor:

class CustomError(Exception):
def __init__(self, msg):
Exception.__init__(self, msg)

Now everything works, but I'm not sure I understand why
super(...).__init__(...) behaves differently from Exception.__init__(...).

According to the documentation, "super() only works with new-style classes".
Is the problem that Exception is an "old style" class (I'm still learning
what this means)? If so, how can I tell when I'm up against an "old style"
class when using python? Why does super() fail silently, if this is the
case?

Thanks in advance for any pointers or help in understanding this!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor