Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old April 10th, 2006, 09:15 AM
alainpoint@yahoo.fr
Guest
 
Posts: n/a
Default challenging (?) metaclass problem

Hi,

I have what in my eyes seems a challenging problem.
Thanks to Peter Otten, i got the following code to work. It is a sort
of named tuple.
from operator import itemgetter
def constgetter(value):
def get(self): return value
return get
def createTuple(*names):
class TupleType(type):
pass
class T(tuple):
__metaclass__ = TupleType
def __new__(cls, *args):
if len(names) != len(args):
raise TypeError
return tuple.__new__(cls, args)
for index, name in enumerate(names):
setattr(T, name, property(itemgetter(index)))
setattr(TupleType, name, property(constgetter(index)))
return T
if __name__ == '__main__':
Point=makeTuple('x','y')
p=Point(4,7)
assert p.x==p[0]
assert p.y==p[1]
assert Point.x==0
assert Point.y==1

Now my problem is the following. I want to write a function called
createDerivedTuple in order to create a class derived from the one
created with the function createTuple, taking the parent class as first
argument:
def createDerivedTuple(parentclass,*names):
....... code yet to be figured out ....

The usage should be as follows:

DerivedPoint=makeDerivedTuple(Point,'z')
p=DerivedPoint(4,7,9)
assert p.x==p[0]
assert p.y==p[1]
assert p.z==p[2]
assert DerivedPoint.x==0
assert DerivedPoint.y==1
assert DerivedPoint.z==2

I am still a newbie on metaclasses but i am convinced there are elegant
solutions to this challenging problem.

Best regards

Alain

  #2  
Old April 10th, 2006, 02:15 PM
alainpoint@yahoo.fr
Guest
 
Posts: n/a
Default Re: challenging (?) metaclass problem

Now, a tab-free version of my previous post. (Sorry for the
inconvenience)
Hi,


I have what in my eyes seems a challenging problem.
Thanks to Peter Otten, i got the following code to work. It is a sort
of named tuple.
from operator import itemgetter
def constgetter(value):
def get(self): return value
return get
def createTuple(*names):
class TupleType(type):
pass
class T(tuple):
__metaclass__ = TupleType
def __new__(cls, *args):
if len(names) != len(args):
raise TypeError
return tuple.__new__(cls, args)
for index, name in enumerate(names):
setattr(T, name, property(itemgetter(index)))
setattr(TupleType, name, property(constgetter(index)))
return T
if __name__ == '__main__':
Point=makeTuple('x','y')
p=Point(4,7)
assert p.x==p[0]
assert p.y==p[1]
assert Point.x==0
assert Point.y==1


Now my problem is the following. I want to write a function called
createDerivedTuple in order to create a class derived from the one
created with the function createTuple, taking the parent class as first

argument:
def createDerivedTuple(parentclass,*names):
....... code yet to be figured out ....


The usage should be as follows:


DerivedPoint=makeDerivedTuple(Point,'z')
p=DerivedPoint(4,7,9)
assert p.x==p[0]
assert p.y==p[1]
assert p.z==p[2]
assert DerivedPoint.x==0
assert DerivedPoint.y==1
assert DerivedPoint.z==2


I am still a newbie on metaclasses but i am convinced there are elegant

solutions to this challenging problem.


Best regards


Alain

  #3  
Old April 10th, 2006, 10:25 PM
Peter Otten
Guest
 
Posts: n/a
Default Re: challenging (?) metaclass problem

alainpoint@yahoo.fr wrote:
[color=blue]
> I have what in my eyes seems a challenging problem.
> Thanks to Peter Otten, i got the following code to work. It is a sort
> of named tuple.[/color]

Don't trust code posted in a newsgroup -- it may sometimes be
quality-challenged :-)
[color=blue]
> Now my problem is the following. I want to write a function called
> createDerivedTuple in order to create a class derived from the one
> created with the function createTuple, taking the parent class as first
> argument:
> def createDerivedTuple(parentclass,*names):
> ....... code yet to be figured out ....
>
> The usage should be as follows:
>
> DerivedPoint=makeDerivedTuple(Point,'z')
> p=DerivedPoint(4,7,9)
> assert p.x==p[0]
> assert p.y==p[1]
> assert p.z==p[2]
> assert DerivedPoint.x==0
> assert DerivedPoint.y==1
> assert DerivedPoint.z==2
>
> I am still a newbie on metaclasses but i am convinced there are elegant
> solutions to this challenging problem.[/color]

Hey, if you don't try to solve it yourself we'll end up with me learning
more than you...

I didn't find a way to extend the approach with a per-class metaclass to
support inheritance. I think you would have to subclass both class and
metaclass. So I had to start all over again:

class IndexProperty(object):
def __init__(self, index):
self.index = index
def __get__(self, inst, cls):
index = self.index
if inst is None:
return index
return inst[index]

class TupleType(type):
def __new__(mcl, name, bases, classdict):
names = classdict.get("_names")
if names is not None:
for base in bases:
base_names = getattr(base, "_names", None)
if base_names is not None:
offset = len(base_names)
break
else:
offset = 0
for i, n in enumerate(names):
classdict[n] = IndexProperty(offset + i)
if offset:
names[:0] = base_names
return type.__new__(mcl, name, bases, classdict)

class Tuple(tuple):
__metaclass__ = TupleType
def __new__(cls, values):
self = tuple.__new__(cls, values)
if len(self) != len(cls._names):
raise TypeError
return self

import unittest

class P2(Tuple):
_names = ["x", "y"]

class P3(P2):
_names = ["z"]

def yield_them(seq):
"""Helper to ensure the implementation cannot rely on len()."""
for item in seq:
yield item

class Test(unittest.TestCase):
def test_P2(self):
self.assertEquals(P2.x, 0)
self.assertEquals(P2.y, 1)
self.assertRaises(AttributeError, lambda: P2.z)

def test_P3(self):
self.assertEquals(P3.x, 0)
self.assertEquals(P3.y, 1)
self.assertEquals(P3.z, 2)

def test_P2_inst(self):
self.assertRaises(TypeError, P2, yield_them("A"))
self.assertRaises(TypeError, P2, yield_them("ABC"))

p = P2(yield_them("AB"))
self.assertEquals(p.x, "A")
self.assertEquals(p.y, "B")
self.assertRaises(AttributeError, lambda: p.z)

def test_P3_inst(self):
self.assertRaises(TypeError, P3, yield_them("A"))
self.assertRaises(TypeError, P3, yield_them("ABCD"))

p = P3(yield_them("ABC"))
self.assertEquals(p.x, "A")
self.assertEquals(p.y, "B")
self.assertEquals(p.z, "C")

self.assertRaises(AttributeError, lambda: p.t)


if __name__ == "__main__":
unittest.main()

I did not perform any tests other than the unittest given above.

Peter
  #4  
Old April 11th, 2006, 10:55 AM
alainpoint@yahoo.fr
Guest
 
Posts: n/a
Default Re: challenging (?) metaclass problem


Hi Peter,

I don't know if you noticed but i changed my mind and removed the post
as i realised that people seemed to have much more interest in how
relevant c code still is than in solving an interesting problem.
I only speak French and Dutch and my knowledge of Belgium's third
official language (German) is only passive. Otherwise i would have
posted on the german newsgroup (the French newsgroup does not seem to
be so interesting and there are no belgian or dutch newsgroups either).
Anyway, thank you for accepting the challenge. I'll try out your code
and get back if i have any problem.

Viele dank.

Alain

  #5  
Old April 11th, 2006, 11:05 AM
alainpoint@yahoo.fr
Guest
 
Posts: n/a
Default Re: challenging (?) metaclass problem


Hi Peter,

I don't know if you noticed but i changed my mind and removed the post
as i realised that people seemed to have much more interest in how
relevant c code still is than in solving an interesting problem.
I only speak French and Dutch and my knowledge of Belgium's third
official language (German) is only passive. Otherwise i would have
posted on the german newsgroup (the French newsgroup does not seem to
be so interesting and there are no belgian or dutch newsgroups either).
Anyway, thank you for accepting the challenge. I'll try out your code
and get back if i have any problem.

Viele dank.

Alain

  #6  
Old April 11th, 2006, 05:25 PM
Peter Otten
Guest
 
Posts: n/a
Default [OT] Re: challenging (?) metaclass problem

alainpoint@yahoo.fr wrote:
[color=blue]
> I don't know if you noticed but i changed my mind and removed the post[/color]

No, I didn't. It seems to be kept in the cache of my newsreader.
[color=blue]
> as i realised that people seemed to have much more interest in how
> relevant c code still is than in solving an interesting problem.[/color]

Oh the defeatism. The chance that you might get an answer is a perfect
reason to ask. That you might get no answer is no reason not to.
[color=blue]
> I only speak French and Dutch and my knowledge of Belgium's third
> official language (German) is only passive. Otherwise i would have
> posted on the german newsgroup (the French newsgroup does not seem to
> be so interesting and there are no belgian or dutch newsgroups either).
> Anyway, thank you for accepting the challenge. I'll try out your code
> and get back if i have any problem.[/color]

I don't think you are more likely to get an answer on the national
newsgroups. Python addicts frequent groups in both English and vernacular.
[color=blue]
> Viele dank.[/color]

Gern geschehen.

Peter
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles