473,410 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

how to do proper subclassing?

Hello there.

I'm new to this list (/me welcomes himself).

anyways, I'm having doubts about how to subclass
properly. The big picture is that I want to handle
permutations. As we all know, two popular notation
forms exist: products of disjoint cycles ("(0 1 3)(2
4)") and 2*length matrices with the identity
permutation as the first line ([0 1 2 3 4] over [1 3 4
0 2]). I find the latter more appropriate, since it's
easy to store as a list (one can skip the first line,
assuming it's _always_ the identity).

However, I want the permutation to act like a list, in
all those cases where I don't override the operations.
For example, assuming permfooation is the above
permutation [1 3 4 0 2], permfooation[1:3] should
return [3,4]. How is this done? Do I have to actually
implement a slice-mechanism in the permutation class?

thanks,

-- Jonas Kölker

__________________________________________________ _________ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com
Jul 18 '05 #1
4 1293
I think you want to have permfooation actually be
a list. If you do then permfooation[1:3} will in
fact return a list [3,4]. If the permutation is
actually stored as:

[0 1 2 3 4]
[1 3 4 0 2]

You can get them into lists with:

f=open(inputfile)
lines=f.readlines()
f.close()
l=0
for line in lines:
if l % 2 == 0: continue # skip first lines
l+=1
permfooation=[int(x) for x in line[1:-1].split()]

print permfooation[1:3]

Now you are probably going to want to wrap this into
a class for easier use.

HTH,
Larry Bates
Syscon, Inc.
"Jonas Koelker" <jo**********@yahoo.com> wrote in message
news:ma**************************************@pyth on.org...
Hello there.

I'm new to this list (/me welcomes himself).

anyways, I'm having doubts about how to subclass
properly. The big picture is that I want to handle
permutations. As we all know, two popular notation
forms exist: products of disjoint cycles ("(0 1 3)(2
4)") and 2*length matrices with the identity
permutation as the first line ([0 1 2 3 4] over [1 3 4
0 2]). I find the latter more appropriate, since it's
easy to store as a list (one can skip the first line,
assuming it's _always_ the identity).

However, I want the permutation to act like a list, in
all those cases where I don't override the operations.
For example, assuming permfooation is the above
permutation [1 3 4 0 2], permfooation[1:3] should
return [3,4]. How is this done? Do I have to actually
implement a slice-mechanism in the permutation class?

thanks,

-- Jonas Kölker

__________________________________________________ _________ALL-NEW Yahoo!

Messenger - all new features - even more fun! http://uk.messenger.yahoo.com
Jul 18 '05 #2
Jonas Koelker wrote:
Hello there.

I'm new to this list (/me welcomes himself).

anyways, I'm having doubts about how to subclass
properly. The big picture is that I want to handle
permutations. As we all know, two popular notation
forms exist: products of disjoint cycles ("(0 1 3)(2
4)") and 2*length matrices with the identity
permutation as the first line ([0 1 2 3 4] over [1 3 4
0 2]). I find the latter more appropriate, since it's
easy to store as a list (one can skip the first line,
assuming it's _always_ the identity).

However, I want the permutation to act like a list, in
all those cases where I don't override the operations.
For example, assuming permfooation is the above
permutation [1 3 4 0 2], permfooation[1:3] should
return [3,4]. How is this done? Do I have to actually
implement a slice-mechanism in the permutation class?


You already answered your question in the subject - you want a subclass of
list:
class Perm(list): .... def __init__(self, n):
.... list.__init__(self, range(n))
.... def __call__(self, i, k):
.... self[i], self[k] = self[k], self[i]
.... return self
.... p = Perm(5)
p [0, 1, 2, 3, 4] p(0,4)(2,4)(0,1)(2,3)(1,2) [1, 3, 4, 0, 2] p[1:2] [3] p[1:3] [3, 4]

Just in case you need it later, there is a special method for every
operator, e. g. __getitem__() for obj[sliceOrIndex], __mul__() for
multiplication etc. Write one for the class in question and it will grow
the corresponding operation. In the above example I have made the class
"callable" (i. e. its instances can behave like a function) by adding the
__call__() method. By returning self I ensure that you can chain these
calls. As you may know, this is not the way the list.sort() method behaves,
so this is probably bad style for methods mutating the original instance.

As to __getitem__(), I think the easiest way to see what happens inside is
provided by the following snippet:
class C: .... def __getitem__(self, i):
.... return i
.... C()[1] 1 C()[1:2] slice(1, 2, None) C()[1:2:3]

slice(1, 2, 3)

I. e. a typical minimal __getitem__() has to test whether it receives a
slice or an integer using isinstance().

Peter

Jul 18 '05 #3
On Tue, 3 Aug 2004, [iso-8859-1] Jonas Koelker wrote:
I'm new to this list (/me welcomes himself).
Welcome!
However, I want the permutation to act like a list, in
all those cases where I don't override the operations.
For example, assuming permfooation is the above
permutation [1 3 4 0 2], permfooation[1:3] should
return [3,4]. How is this done? Do I have to actually
implement a slice-mechanism in the permutation class?


I'm not sure exactly how your permutation class is implemented, so I can't
give you a precise answer, but in general, there are three ways to do
this, each method having benefits and drawbacks:

1) Implement your class as a subclass of list.

Using this method, your class would store its list data in itself (in
self, actually), since it is effectively a list. The __getitem__ method
needed to implement slicing will be inherited from list, and no further
work will need to be done. This method works best if you're only altering
the behaviour of a list slightly (e.g. transforming all items in a
particular way), but may not be well suited to objects that shouldn't
otherwise act like lists.

2) Store your data in a list, and delegate __getitem__.

Using this method, your class would store its list data in an attribute
(say self.data). To implement slicing, you can simply define your class's
__getitem__ method like this:

def __getitem__(self,i):
return self.data[i]

assuming self.data contains the list data to which you want to provide
access. This is the easiest method if all you want to implement is
slicing.

3) Implement __getitem__ yourself.

You should only need to do this if the data you want to slice is not
available in a readily slicable form (perhaps you generate it on the fly).
Implementing __getitem__ is easy; here is a general template you can use:

def __getitem__(self,i):
if isinstance(i,slice):
return self.make_a_slice_and_return_a_list(i.start,i.stop ,i.step)
else:
return self.get_an_item_at_index(i)

__getitem__ is passed either a slice object (in the case of slicing) or
an integer (in the case of indexing). The start, stop, and step
attributes of the slice object return what you'd expect, or None if they
weren't specified. The method slice.indices(length) will compute "actual"
slice indices for a sequence of the given length in the case of missing,
negative, or out-of-bounds indices, returning a tuple (start, stop,
stride).

Hope this helps.

Jul 18 '05 #4
Jonas Koelker wrote:
I'm new to this list (/me welcomes himself).
Jonas, I suggest that you post follow-ups vaguely on topic on c.l.py. That
gives you the chance for answers that I (for example) don't know about or
care to give as well as corrections or a second opinion.

[Jonas via email]
>> class Perm(list):


... def __init__(self, n):
... list.__init__(self, range(n))
... def __call__(self, i, k):
... self[i], self[k] = self[k], self[i]
... return self
...
>> p = Perm(5)
>> p


[0, 1, 2, 3, 4]
>> p(0,4)(2,4)(0,1)(2,3)(1,2)


[1, 3, 4, 0, 2]
>> p[1:2]


[3]
>> p[1:3]


[3, 4]


thanks, this looks about right (slaps himself on the
forehead - "you DO know how init works!")... :]

also, p(0,4)(2,4)(0,1)(2,3)(1,2) looks really neat and
math'ish :)
Just in case you need it later, there is a special
method for every operator, e. g. __getitem__() for
obj[sliceOrIndex], __mul__() for multiplication etc.


Yeah, I knew that. I might make __int__() convert the
perm to a ranking number in linear time. See


My mentioning of special methods doesn't mean that I think they are always
the best choice. I would prefer both p.rank() and rank(p) over int(p).
http://www.cs.uvic.ca/~ruskey/Public.../RankPerm.html

.. don't worry, I can implement it myself.
Don't worry, I won't :-)
btw, if I want to set the "list part" of the perm, how
do I do that?
p[:] = [1, 2, 3, 4, 5]
p[:2] = p[1], p[0]
p[2] = [99]

If you want to ensure that the permutation is still valid after assignment
you have to write your own __setitem__() method.
p = [4,0,3,2,1] would turn p into a list.
p = perm(5)(0,4)(2,4)(0,1)(2,3)(1,2) is valid (right?)
but time consuming.
That was just for fun... most likely faster than

while p != [4,0,3,2,1]: random.shuffle(p)
I'd say it'd be easier/better to implement it as:
class Perm(list):
... def __init__(self, n):
... list.__init__(self, n)
The good news: if __init__() does nothing but call the base class'
__init__(), you need not write one at all.
... def __call__(self, i, k):
... self[i], self[k] = self[k], self[i]
... return self
p = Perm([4, 0, 3, 2, 1])


... anyways, thanks :)


You're welcome.

Peter

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: WhiteRavenEye | last post by:
Why can't I subclass any window except mine in VB? Do I have to write dll for this? I've tried to subclass it with SetWindowLong but without success... Does anyone know how to subclass window...
4
by: GrelEns | last post by:
hello, i wonder if this possible to subclass a list or a tuple and add more attributes ? also does someone have a link to how well define is own iterable object ? what i was expecting was...
2
by: David Vaughan | last post by:
I'm using v2.3, and trying to write to text files, but with a maximum line length. So, if a line is getting too long, a suitable ' ' character is replaced by a new line. I'm subclassing the file...
2
by: BJörn Lindqvist | last post by:
A problem I have occured recently is that I want to subclass builtin types. Especially subclassing list is very troublesome to me. But I can't find the right syntax to use. Take for example this...
3
by: Peter Olsen | last post by:
I want to define a class "point" as a subclass of complex. When I create an instance sample = point(<arglist>) I want "sample" to "be" a complex number, but with its real and imaginary...
11
by: Brent | last post by:
I'd like to subclass the built-in str type. For example: -- class MyString(str): def __init__(self, txt, data): super(MyString,self).__init__(txt) self.data = data
3
by: alotcode | last post by:
Hello: What is 'interprocess subclassing'? To give more context, I am writing in reference to the following remark: With the advent of the Microsoft Win32 API, interprocess subclassing was...
5
by: Pieter Linden | last post by:
Hi, This question refers sort of to Rebecca Riordan's article on Access web about subclassing entities: http://www.mvps.org/access/tables/tbl0013.htm How practical is this? I am writing a...
16
by: manatlan | last post by:
I've got an instance of a class, ex : b=gtk.Button() I'd like to add methods and attributes to my instance "b". I know it's possible by hacking "b" with setattr() methods. But i'd like to do...
5
by: Ray | last post by:
Hi all, I am thinking of subclassing the standard string class so I can do something like: mystring str; .... str.toLower (); A quick search on this newsgroup has found messages by others
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.