[Tutor] Operator Overloading

2005-04-03 Thread Kevin Reeder
Following an example from a book, I'm getting an unexpected outcome.
The point of exercise is to extend operator overloading methods from
a superclass and track the method calls. Here's the code,

class MyList:
def __init__(self, start):
self.wrapped = [ ]
for x in start: self.wrapped.append(x)

def __add__(self, other):
return MyList(self.wrapped + other)

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

=

from module import MyList

class MyListSub(MyList):

calls = 0

def __init__(self, start):
self.adds = 0
MyList.__init__(self, start)

def __add__(self, other):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1
return MyList.__add__(self, other)

def __len__(self):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1
return MyList.__len__(self)

def stats(self):
return self.calls, self.adds

This is not my code but is taken from the book I'm working with. My
problem is that whenever I call to the __add__ method the counters
increase by 2 while calls the __len__ method increase the counters
by 1 as expected. What I've concluded is that the method which
overloads and arithmetic operations executes the counter statements
twice while the method which overloads a sequencing operation
executes the counter statements only once. Otherwise, I can see no
difference between the two methods.

Here's an example,

>>> A = MyListSub([1, 2, 3])
>>> A.stats()
(0, 0)
>>> len(A)
3
>>> A.stats()
(1, 1)
>>> A + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
>>> A.stats()
(3, 3)

I'm stumped and and would appreciate any help.

Kevin


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


Re: [Tutor] Operator Overloading

2005-04-04 Thread Kevin Reeder
On Mon, 04 Apr 2005 21:14:21 +1200
John Fouhy <[EMAIL PROTECTED]> wrote:

> Are you sure you've giving us all the code?

No, I was trying to keep it simple. Anyway, here it is,

class MyList:
def __init__(self, start):
self.wrapped = [ ]
for x in start: self.wrapped.append(x)

def __add__(self, other):
return MyList(self.wrapped + other)

def __mul__(self, time):
return MyList(self.wrapped * time)

def __getitem__(self, offset):
return self.wrapped[offset]

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

def __getslice__(self, low, high):
return MyList(self.wrapped[low:high])

def __setitem__(self, index, value):
self.wrapped[index] = value
print self.wrapped

def __getattr__(self, name):
return getattr(self.wrapped, name)

def __repr__(self):
return `self.wrapped`
  

=

from mylist import MyList

class MyListSub(MyList):

calls = 0

def __init__(self, start):
self.adds = 0
MyList.__init__(self, start)

def __add__(self, other):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1
return MyList.__add__(self, other)

def __mul__(self, time):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1
return MyList.__mul__(self, time)

def __getitem__(self, offset):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1
return MyList.__getitem__(self, offset)

def __len__(self):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1
return MyList.__len__(self)

def __getslice__(self, low, high):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1  
return MyList.__getslice__(self, low, high)

def __setitem__(self, index, value):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1
return MyList.__setitem__(self, index, value)
  
def __getattr__(self, name):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1
return MyList.__getattr__(self, name)

 def __sub__(self, index):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1
return MyList.__sub__(self, index)

def __and__(self, value):
MyListSub.calls = MyListSub.calls + 1
self.adds = self.adds + 1
MyList.__and__(self, value)

def stats(self):
return self.calls, self.adds


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


Re: [Tutor] Operator Overloading

2005-04-04 Thread Kevin Reeder
On Tue, 05 Apr 2005 10:56:40 +1200 (NZST)
[EMAIL PROTECTED] wrote:

> Fine, but always test the simplified version, unless you're
> absolutely certain what you're throwing out!

Point taken.

> When you do 'A + [4, 5, 6]', python first calls
> A.__getattr__('__coerce__').

Everything's working fine now.

> pdb is useful :-)

Thanks,

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


[Tutor] Calling a function

2005-06-08 Thread Kevin Reeder
I'm having trouble with this code which is meant to run a time
comparison between two similar functions. The first module is
makezeros.py

def lots_of_appends():
zeros = []
for i in range(1):  
zeros.append(0)

def one_multiply():
zeros = [0] * 1


The second module is timings.py.

import time, makezeros

def do_timing(num_times, *funcs):
totals = {}
for func in funcs: totals[func] = 0.0
for x in range(num_times):
for func in funcs:
starttime = time.time()
apply(func)
stoptime = time.time()
elapsed = stoptime-starttime
totals[func] = totals[func] + elapsed
 for func in funcs:
 print "Running %s %d times took %.3f seconds" %
(func.__name__, num_times, totals[func])

do_timing(100, (makezeros.lots_of_appends, makezeros.one_multiply))


Here's the outcome I get:

$ python ./Python/timings.py
Traceback (most recent call last):
  File "./Python/timings.py", line 17, in ?
do_timing(100, (lots_of_appends, one_multiply))
  File "./Python/timings.py", line 10, in do_timing
apply(func)
TypeError: 'tuple' object is not callable


BTW, the code is taken straight out of Learning Python, but I've
been banging my head on it for awhile. Any ideas??

Kevin


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


Re: [Tutor] Calling a function

2005-06-09 Thread Kevin Reeder
Ewald & John,

thanks for the help. i'll work on it some more with your ideas in
mind (after getting some sleep).

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


[Tutor] fileinput problem

2005-06-24 Thread Kevin Reeder
I'm getting unexpected behaviour from this module. My script is
meant to count the number of occurences of a term in one or more
files.

---

import sys, string, fileinput

searchterm, sys.argv[1:] = sys.argv[1], sys.argv[2:]

for line in fileinput.input():
num_matches = string.count(line, searchterm)
if num_matches:
print "found '%s' %i times in %s on line %i." % (searchterm,
num_matches, fileinput.filename(), 
fileinput.filelineno())

---

When I run at the command line, this error message appears:

$ python ./Python/fileinput.py for ./Python/*.py

Traceback (most recent call last):
  File "./Python/fileinput.py", line 1, in ?
import sys, string, fileinput
  File "./Python/fileinput.py", line 6, in ?
for line in fileinput.input():
AttributeError: 'module' object has no attribute 'input'

---

I'm stumped. Any ideas?

Kevin

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


Re: [Tutor] fileinput problem

2005-06-25 Thread Kevin Reeder
On Sat, 25 Jun 2005 06:41:01 -0400
Kent Johnson <[EMAIL PROTECTED]> wrote:

> You named your program fileinput.py, so when you import fileinput
> you are getting your own program again instead of the library
> module. Change the name of your program and try again.

Doh! I do remember reading that somewhere before. Lesson learnt.

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


[Tutor] Single Underscore

2005-07-10 Thread Kevin Reeder
What's the significance of naming a variable with a single
underscore as its first character? For example, I'm looking at
find.py in the standard library and it has variables named _debug
and _prune.

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


Re: [Tutor] Single Underscore

2005-07-10 Thread Kevin Reeder
Thanks for the info. I'll look into the links provided.

> http://jaynes.colorado.edu/PythonGuidelines.html
> http://www.python.org/peps/pep-0008.html

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