challenging (?) metaclass problem

A

alainpoint

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
 
A

alainpoint

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
 
P

Peter Otten

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.

Don't trust code posted in a newsgroup -- it may sometimes be
quality-challenged :)
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.

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
 
A

alainpoint

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
 
A

alainpoint

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
 
P

Peter Otten

I don't know if you noticed but i changed my mind and removed the post

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

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.
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.

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.
Viele dank.

Gern geschehen.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top