[Tutor] class methods as argument

2007-02-10 Thread thomas coopman
Hi,

I want to do something like this, don't know how to properly explain it,
so I just give you some example code

class Foo(object):
 def method(self, arg):
 print arg

def doSomething(object, func):
 object.func(test)

object = Foo()
doSomething(object, Foo.method)

I want to execute the class method given as argument,
but this obvious doesn't work, but I don't know how to
get it work,
Is it possible?
and how?

Thanks

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


Re: [Tutor] class methods as argument

2007-02-10 Thread thomas coopman
On Sat, 10 Feb 2007 22:10:52 +1000
Jonathan McManus [EMAIL PROTECTED] wrote:

 It's pretty easy to make this work, actually. The issue is in the
 doSomething method.
 
  class Foo(object):
   def method(self, arg):
   print arg
  
  def doSomething(object, func):
   object.func(test)
 
 Here, it's looking for a method of the Foo object (object) called
 func (AttributeError: 'Foo' object has no attribute 'func'), instead
 of replacing func with Foo.method. 
 
 What you need to do is:
 
  def doSomething (object, func):
func(object, test)
 
 This means that you are calling the method (explicity passed as being
 a method of Foo), as well as supplying the actual object as the first
 argument (the required self for classes).
 
  object = Foo()
  doSomething(object, Foo.method)
 test
 
 Hope that helps.
 


Thanks,
that I've didn't come up with that myself!
It's indeed easy!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] class methods as argument

2007-02-10 Thread thomas coopman
On Sat, 10 Feb 2007 07:55:54 -0500
Kent Johnson [EMAIL PROTECTED] wrote:

 thomas coopman wrote:
  Hi,
  
  I want to do something like this, don't know how to properly
  explain it, so I just give you some example code
  
  class Foo(object):
   def method(self, arg):
   print arg
  
  def doSomething(object, func):
   object.func(test)
  
  object = Foo()
  doSomething(object, Foo.method)
  
  I want to execute the class method given as argument,
  but this obvious doesn't work, but I don't know how to
  get it work,
 
 First a quick note - don't use object as a parameter or variable
 name, it will hide the definition of the built-in object class.
Yes, I know, it was a bad example
 
 You are very close. Foo.method is called an 'unbound method' of class 
 Foo. The syntax for calling an unbound method is to pass a class 
 instance as the first argument, followed by the actual argument list.
 It is actually the same argument list that you use when you declare
 the function (starting with self).
 
 So your example can be written this way:
 
 In [4]: class Foo(object):
 ...: def method(self, arg):
 ...: print arg
 
 In [6]: o=Foo()
 
 In [8]: def doSomething(obj, func):
 ...: func(obj, test)
 
 In [9]: doSomething(o, Foo.method)
 test
 
 
 A more common way to do this is to use a 'bound method'. That is what 
 you get when you refer to instance.method instead of Class.method. A 
 bound method includes a reference to the particular instance and can
 be called like an ordinary function. Rather than passing the instance
 and the unbound method to doSomething(), just pass the bound method
 as a single argument:
 

 In [10]: def doSomething(func):
 : func(test)
 
 In [12]: doSomething(o.method)
 test
 
 In summary:
 Class.method = unbound method, call with instance as first arg
 instance.method = bound method, call with normal argument list
 
 Kent
 

Thank you for the explanation of bound and unbound methods.
I understand that it is more common to use a bound method, but I don't
think that I can use this because at the time I save the method, I don't
know anything about the instances.

I use this for making a sorted list using any method you give as
argument when you create the list.  This class only gets to know it's
instances when you add them.

In [3]: class SortedList(object):
   ...: def __init__(self, compare):
   ...: self.compare = compare
   ...: def __add_(self, object):
   ...: for i in self.data:
   ...: self.compare(object, i)
   ...:

In [4]: class FooList(SortedList):
   ...: def __init__(self):
   ...: self.compare = Foo.compare


__add__ doesn't do anything here of course, it is just an example,
but I don't think that I can use a bound method in this case?

also,
Is it better to use super in FooList? and how should I use it then?

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


Re: [Tutor] class methods as argument

2007-02-10 Thread thomas coopman
On Sat, 10 Feb 2007 09:04:15 -0500
Kent Johnson [EMAIL PROTECTED] wrote:

 thomas coopman wrote:
  
  also,
  Is it better to use super in FooList? and how should I use it then?
 
 Actually I would say that FooList is not pulling its weight.
 SortedList already allows specialization by the compare function, so
 to create the equivalent of a FooList you just call
 SortedList(Foo.compare).
I know that in this example it's rather useless to create FooList, but
the subclasses I have of SortedList have other specializations so I
will keep using them.

 
 If you do want to keep FooList then you should call 
 SortedList.__init__() to set the compare function.
 SortedList.__init__ is an unbound function so you call it like this:
SortedList.__init__(self, Foo.compare)
This works.
I think I'll need to reed some more about super because I thought I had
to use super in this case.

 
 Another design you might want to consider - if you will always be 
 sorting each type of list by the same compare method - is to define a 
 __cmp__() method in each class that will be part of a SortedList and
 use plain comparison operators (  etc) to do the compare.
That's the problem.  I define for most of my classes a __cmp__() method
but, some classes have another way of being sorted, and that's why I
give the compare method as an argument in SortedList

 
 Finally note that Python's sort() function is very fast and flexible
 and it might be better just to sort the list when you need it to be
 sorted, rather than keeping it sorted. Or maybe what you really need
 is a heap (see the heapq module). If you really want to keep a sorted
 list, you should look at the bisect module, it might help.
I now use sort() when there is some data initially.  After creation, I
insert data with my own bisect method, I also use it to search the data.

I mostly need the sorted list, because I want to detect when a object
is added with the same value of some item in the list and take
appropriate actions.

 
 Kent
 

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


[Tutor] comparing almost equal strings

2007-02-08 Thread thomas coopman
Hi,

I need a function that groups almost equal strings.  It seems most easy
to me, to do this with a hash function.

So I would write a hash function like this:
string = string.replace( , ).lower()[0:6]

and then hash the string chars, but it should detect minor typo's, so
words with one different char in the 6 chars, should have the same hash.

I think I once read something about it, but I can't find it, 
does somebody know how to do this?

Also do you think this is a good way, or do some of you have experience
with this and know a better way?

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


Re: [Tutor] comparing almost equal strings

2007-02-08 Thread thomas coopman
Hi,

On Thu, 08 Feb
2007 13:07:41 +0100
Christopher Arndt [EMAIL PROTECTED] wrote:

 thomas coopman schrieb:
  I need a function that groups almost equal strings.  It seems most
  easy to me, to do this with a hash function.
 
 What do you mean be almost equal? By which criterium? Spelling,
 Pronounciation? Semantics?
 
  I think I once read something about it, but I can't find it, 
  does somebody know how to do this?
 
 Maybe you mean the soundex algorithm? See, for example, here:
 
 http://diveintopython.org/performance_tuning/index.html

This is what I was looking for!
 
 Chris
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor

Thanks!

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


Re: [Tutor] import and unittest

2007-01-17 Thread thomas coopman
On Tue, 16 Jan 2007 10:06:37 -
Alan Gauld [EMAIL PROTECTED] wrote:

 
 Thomas Coopman [EMAIL PROTECTED] wrote
 .
  I wondered if it was possible to do something like this:
 
  src/
 -a_module/
 -sub_module/
  test/
 -a_module/
 -sub_module/
 
 
 I don;t see any reason why not although its slightly more work.
 Personally I tend to keep the tests with the code, but thats
 mainly because tools such as editors tend to remember the last
 folder opened and its a pain navigating between the two folders.
 
 The other system I have used(in C++ not Python) is to have
 a test folder inside each src folder like:
 
 src/
mod1/
f1.py
test/
   testf1.py
 mod2/
f2.py
f3.py
test/
testf1.py
testf2.py
 
 etc.
 
 This minimises navigation and keeps the tests separate.
 Its also relatively easy to filter out the tests when it comes
 time to package upp the code for distribution (assuming
 you want to lose them!)
 
  I have something like this but I don't know how to organize the 
  imports in
  the tests and I don't know if this is a good idea.  What do you 
  think?
 
 I think in Python you should create a package structure for
 your code so that import can find the modules more easily.
 But I've never tried this in Python, my Python projects are rarely
 big enough to warrant it.
 
 

well, I still have problems with import

suppose I have something like this:

M/
  __init__.py
  A/
__init__.py
One.py
  B/
__init__.py
Two.py

One.py
#!/usr/bin/python

from M.B import Two

Two.py
#!/usr/bin/python

from M.A import One


when I try to run One.py or Two.py I get import errors and I don't know
how to fix them.  I tried to set the __all__ variable in the __init__
files but nothing worked so far.

I would like to run the files from anywhere for example.
What am I doing wrong?
I read the information about modules on python.org
(http://www.python.org/doc/2.0.1/tut/node8.html) but I don't find a
solution

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


Re: [Tutor] import and unittest

2007-01-17 Thread Thomas Coopman

On 1/17/07, Kent Johnson [EMAIL PROTECTED] wrote:


thomas coopman wrote:
 On Tue, 16 Jan 2007 10:06:37 -
 Alan Gauld [EMAIL PROTECTED] wrote:

 Thomas Coopman [EMAIL PROTECTED] wrote
 .
 I wondered if it was possible to do something like this:

 src/
-a_module/
-sub_module/
 test/
-a_module/
-sub_module/

 I don;t see any reason why not although its slightly more work.
 Personally I tend to keep the tests with the code, but thats
 mainly because tools such as editors tend to remember the last
 folder opened and its a pain navigating between the two folders.

 The other system I have used(in C++ not Python) is to have
 a test folder inside each src folder like:

 src/
mod1/
f1.py
test/
   testf1.py
 mod2/
f2.py
f3.py
test/
testf1.py
testf2.py

 etc.

 This minimises navigation and keeps the tests separate.
 Its also relatively easy to filter out the tests when it comes
 time to package upp the code for distribution (assuming
 you want to lose them!)

 I have something like this but I don't know how to organize the
 imports in
 the tests and I don't know if this is a good idea.  What do you
 think?
 I think in Python you should create a package structure for
 your code so that import can find the modules more easily.
 But I've never tried this in Python, my Python projects are rarely
 big enough to warrant it.



 well, I still have problems with import

 suppose I have something like this:

 M/
   __init__.py
   A/
 __init__.py
 One.py
   B/
 __init__.py
 Two.py

 One.py
 #!/usr/bin/python

 from M.B import Two

 Two.py
 #!/usr/bin/python

 from M.A import One


 when I try to run One.py or Two.py I get import errors and I don't know
 how to fix them.  I tried to set the __all__ variable in the __init__
 files but nothing worked so far.

What error do you get?

It's better to avoid this kind of circular import if you can. Rather
than having One depend on Two and Two depend on One, look for some piece
you can factor out into a new module that both One and Two use (or it
may only be needed by One or Two).

There are several problems with circular imports, but the technical
problem you are having is probably like this:

module One starts to execute
One imports Two
Two starts to execute
Two imports One
-- Note that at this point, the body of One has not executed, so
anything defined in One after the import of Two is not yet defined.
Two tries to use something in One. Since One is not fully defined, it
gets an AttributeError.

Anyway, try to split up your modules or post the exact error you get.

Kent



Well I don't really
need the circular imports but I would like to know how to do the
imports correct.
Suppose that in the example that I showed only One needs Two.

So then we have this:

M/
__init__.py
A/
__init__.py
One.py
B/
__init__.py
Two.py

One.py
#!/usr/bin/python

from M.B import Two


when I run this I get this error:
ImportError: No Module named M.B


--
Thomas Coopman
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] import and unittest

2007-01-17 Thread Thomas Coopman

On 1/17/07, Kent Johnson [EMAIL PROTECTED] wrote:


Thomas Coopman wrote:
 Well I don't really
 need the circular imports but I would like to know how to do the imports
correct.

 Suppose that in the example that I showed only One needs Two.

 So then we have this:

 M/
 __init__.py
 A/
 __init__.py
 One.py
 B/
 __init__.py
 Two.py

 One.py
 #!/usr/bin/python

 from M.B import Two


 when I run this I get this error:
 ImportError: No Module named M.B

Is the directory containing M in sys.path?

Kent



When I run One.py from in the directory A, doesn't look python in it's
parent directory when it can't find the module?
And if that's not the case where and what should I add to the sys.pathvariable?


--
Thomas Coopman
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] import and unittest

2007-01-17 Thread Thomas Coopman

On 1/17/07, Kent Johnson [EMAIL PROTECTED] wrote:


Thomas Coopman wrote:
 When I run One.py from in the directory A, doesn't look python in it's
 parent directory when it can't find the module?
 And if that's not the case where and what should I add to the sys.path
 variable?

No, Python won't look in parent directories for imports. Try running
One.py from the parent dir of M by typing
 python M/A/One.py

or add the parent dir of M to sys.path.

Kent

when I try to run it from the parent dir of M, I still get the error,

where and when should I add the the parent dir to sys.path?

when I try from M.A import One in a shell executed in the parent dir of M
than it works.

The thing is that I would like to run One.py from anywhere and not just from
that one directory.
--
Thomas Coopman
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] import and unittest

2007-01-16 Thread Thomas Coopman

Hi,

the documentation of pyunit
say that it is good to place the unittest in a seperate module.
I wondered if it was possible to do something like this:

src/
   -a_module/
   -sub_module/
test/
   -a_module/
   -sub_module/

So the test are in a complete different directory than the code and the test
dir is a complete copy
of the src dir but with unittest instead of code.

I have something like this but I don't know how to organize the imports in
the tests and I don't know
if this is a good idea.  What do you think?

Thanks

--
Thomas Coopman
[EMAIL PROTECTED]
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] import and unittest

2007-01-16 Thread thomas coopman
On Tue, 16 Jan 2007 10:06:37 -
Alan Gauld [EMAIL PROTECTED] wrote:

 
 Thomas Coopman [EMAIL PROTECTED] wrote
 .
  I wondered if it was possible to do something like this:
 
  src/
 -a_module/
 -sub_module/
  test/
 -a_module/
 -sub_module/
 
 
 I don;t see any reason why not although its slightly more work.
 Personally I tend to keep the tests with the code, but thats
 mainly because tools such as editors tend to remember the last
 folder opened and its a pain navigating between the two folders.
 
 The other system I have used(in C++ not Python) is to have
 a test folder inside each src folder like:
 
 src/
mod1/
f1.py
test/
   testf1.py
 mod2/
f2.py
f3.py
test/
testf1.py
testf2.py
 
 etc.
 
 This minimises navigation and keeps the tests separate.
 Its also relatively easy to filter out the tests when it comes
 time to package upp the code for distribution (assuming
 you want to lose them!)

I think I will use something like this.

 
  I have something like this but I don't know how to organize the 
  imports in
  the tests and I don't know if this is a good idea.  What do you 
  think?
 
 I think in Python you should create a package structure for
 your code so that import can find the modules more easily.
 But I've never tried this in Python, my Python projects are rarely
 big enough to warrant it.
 
 
Thanks.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Problems with a Class

2007-01-13 Thread thomas coopman
On Sat, 13 Jan 2007 01:08:53 +0100
Carlos [EMAIL PROTECTED] wrote:

 Hello,
 
 I'm just about to finish my project, but something is causing me 
 trouble. I need to find out how to get some information out of the 
 genetic algorithm that Im using. This algorithm is generating some 
 coordinates for a collection of objects, until now I needed only the 
 final result of the process. But I discovered that the final
 information is not enough, I need to get some intermediate data to
 actualize the positions of the objects and then evaluate those
 positions.
 
 This is the area of the algorith that I think holds the info that I
 need:
 
 def evolve(self, generations = 100):
   
 self.halt_reached = 0

 for gen in range(generations):
 
 #Changed.
 print 'GEN:',gen, self.entities

 self.do_mutation()
 self.do_crossover()
 print Average fitness generation  + str(gen) + :  + 
 str(self.avg_fitness())
 if self.debug == 1:
 self.echo_fitness()
 if self.halt = 0:
 max_entity = self.get_max_fitness()
 fit = self.calc_fitness(max_entity)
 if fit = halt:
 self.halt_reached = 1
 return [max_entity]
 
 The line marked as #Changed is what I changed,  with this I was able
 to print the info, but I'm sorry to say that I have no clue at how to
 apply something like return() that would give me this self.entities
 list. As you can see there is a for loop and I need the info every
 time that it gets updated by the loop.
 
 Hopefully I'm making myself clear and the info is enough.
 
 Thanks for your help in advance
 Carlos
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor


Hi,

when you call return, you return from the function so
when you have something like this:

def loop():
for i in range(10):
return i

print loop()

This will print just 0.

when you want to get all the numbers, you can't use return in the loop
you could save them in a list or something, and after the loop return
the list.

def loop():
x = []
for i in range(10):
x.append(i)
return x

print loop()

this will print a list with all the numbers.

I hope this is what you mean.


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