Re: Customizing sequence types



On Sun, Nov 16, 2008 at 7:15 PM, Arnaud Delobelle
<arnodel@xxxxxxxxxxxxxx> wrote:
Mr.SpOOn <mr.spoon21@xxxxxxxxx> writes:

Hi,
I'm trying to create a class which inherit a list to change some behavior.
This list should contain other instance objects and has to manage
these instances in a particular way.

1) I need to sort this elements in this list, but they must be sorted
using an instance variable.

I don't understand what this means.

Well, inside the elements I need to order there is a variable that
stores a numeric value. I need to sort the elements using this value.

Or sort them according to a key.

Ok, knowing what to search for I cleared my ideas. I think I just have
to choose between redefining __cmp__() or using the key argument.

2) I need to have just unique elements. Maybe this is related to first
question (or maybe not). Again, to estabilish the uniqueness of an
element I have to use an instance variable. I think I have to rewrite
both the methods append() and __contains__(), but I'm not sure how.

For the first one I did:

def append(self, element):
if element in self:
pass
else:
list.append(self, element)

It seems right to me, but I have no idea what to do with __contains__()

Why do you need to redefine __contains__?

Well, to check the presence of an element according to an attribute.

If I were you, I wouldn't inherit from list because it seems that your
list-like object quite different from a plain list.

The most obvious choice seems to be stes. But the order is really
important. Maybe the best solution is using sets and convert them to
list when I need to access them in the correct order.

You should probably use the `bisect` module
(http://docs.python.org/library/bisect.html) for searching and
inserting into the list as it takes advantage of and ensures that the
list keeps sorted. It also means that __contains__ and some other
operations become O(log N) rather than O(N).

This seems good too, but I'd always have to check the presence of an
element before inserting a new one and it's still not clear to me how
to do it.

Thanks everybody.
.



Relevant Pages

  • Re: BG remaining stable.
    ... The groove comes partially from lowering my anxiety about blindness or expecting some high reading at any moment. ... The other part is that I keep feeling some sort of "presence" that I cannot explain. ... Logically I know there is no god, but this presence has begun to make me feel good and happy about our life here. ... This group is one of the safer groups that exist on the internet. ...
    (alt.support.diabetes)
  • Re: That feeling you get when you know someone looks at you...
    ... another person who is in the car driving ... your presence all along and knew you were looking at them. ... have had the same sort of thing happen, ... it could be something like the eyes send out some kind of light, ...
    (sci.med.vision)
  • Re: How do I view merge fields?
    ... it inserts the company name *twice* (on separate lines). ... sort in some kind of sensible order when I sort the address book by name? ... But why is a it inserting a name at all if the merge field is just ... I'm merging data from what Word calls the "Office Address Book". ...
    (microsoft.public.mac.office.word)
  • Re: Customizing sequence types
    ... I'm trying to create a class which inherit a list to change some behavior. ... This list should contain other instance objects and has to manage ... Comparison operators are always used but you can sort elements according ...
    (comp.lang.python)
  • Re: How do I view merge fields?
    ... sort in some kind of sensible order when I sort the address book by name? ... But why is a it inserting a name at all if the merge field is just ... Instead of using the Address field, try using the separate Street / City / State/ Zip fields. ... Personally, I keep the address book sorted by nickname, which also allows me to do things like sort all the people I know from a particular context together by prefacing the nickname with an equals ...
    (microsoft.public.mac.office.word)