Re: [Tutor] Overloading comparisons

2005-11-15 Thread Hugo González Monteverde
Hi Danny,

That's nice. I do think I'm going this way

I don't want the container to grow too big in memory, not bigger than 1k 
elements(it is a cache), but when deleting an element I need it to be 
the oldest files (again, because it is a cache)

Thanks for the advice, I'm going this way. My OOP skills *are* sloppy...

Hugo

Danny Yoo wrote:
> 
>>I defined an object wich describes a video clip, like this
>>
>>class VideoSegment:
>> def __init__(self, filename):
>> # Attributes that have to be present
>> self.filename = filename
> 
> 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Overloading comparisons

2005-11-15 Thread Danny Yoo


> I defined an object wich describes a video clip, like this
>
> class VideoSegment:
>  def __init__(self, filename):
>  # Attributes that have to be present
>  self.filename = filename

[some text cut]

> I can define the following for sorting the array:

[some code cut]

> But then I want to test for existance doing
>
> if 'lala.avi' in myarray:

[some text cut]


Hi Hugo,

Rather than directly use a list to hold those VideoSegments, you may want
to make a separate VideoContainer.  A quick and dirty approach would look
something like this:

#
class VideoContainer:
def __init__(self):
self.segments = []

def addSegment(self, segment):
self.segments.append(segment)
self.segments.sort()

def deleteOldest(self): ...

def __contains__(self, segmentName): ...
#

The idea is to squirrel away the sorted list in this VideoContainer, and
not expose that implementation detail directly to people.  Why do we use a
list?  Why not a hash?  Why does the list have to be sorted?

Those are implementation details that we want to hide, just in case we
have to change our mind on how to effectively hold those Segments.  As far
as the world's concerned, a VideoContainer is something that we can put
VideoSegments in, and where we can drop old segments off.

(And if your operations are limited to this, you might even use something
like the 'heapq' module if you're concerned about efficiency.  See:
http://www.python.org/doc/lib/module-heapq.html for details.)


It should be the container's responsibility to keep the segments sorted
any time we add a new segment in.  The container can also know how to
check for segments by name (with the __contains__() function.)

Trying to have VideoSegment.__cmp__() to somehow juggle both the sorting
role and the existence checking role might not be a good idea.  You're
doing two distinct things, and I think they should be two different
methods.  And from a design perspective, I think that existence-checking
method should go in a class like VideoContainer.


Best of wishes!

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


Re: [Tutor] Overloading comparisons

2005-11-15 Thread Hugo González Monteverde
I know it is rude to reply to myself. But I found the answer and wanted 
to share it.

I did the following test:

 >>> class Lala:
... def __eq__(self, other):
... if self.name == other.name:
... return True
...
... def __cmp__(self, other):
... if self.time < other.time:
... return -1
... elif self.time > other.time:
... return 1
... else:
... return 0
...
 >>> ins0 = Lala()
 >>> ins0.time = 11
 >>> ins0.name = 'myname'
 >>>
 >>>
 >>> ins1 = Lala()
 >>> ins1.time = 10
 >>> ins1.name = 'myname'
 >>>
 >>> ins1 == ins0
True
 >>>
 >>> ins0>> ins1>>
 >>> #See? They test correctly for greater and lesser than (time)
... #but they test equal as to time.
...
 >>> #I even tried sort, it works fine.



The thing is to use the rich comparison functions.

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