Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Automatic Attribute Assignment during Class Inheritance

1 view
Skip to first unread message

gizli

unread,
Sep 9, 2009, 11:14:33 PM9/9/09
to
Hi all,

I have been trying to do a programming trick with python and so far I
failed. I am using sqlalchemy in my code and writing a framework with
it. This framework uses something called polymorphic identity
attribute to define the do single table inheritance. Here's roughly
how it works:

1. Define a base ORM class in SQLAlchemy:
class Task:
....some params..
__mapper_args__ = dict(polymorphic_on = type)

2. Anyone who wants to extend this Task class would have to do this:
class MyTask(Task):
__mapper_args__ = dict(polymorphic_identity = 'MyTask')

I do not want to force the consumers of this framework to write this
obscure line of code. I was wondering if it is possible (at class
definition time) to capture the fact that MyTask extends Task and
automatically insert the polymorphic_identity key into the
__mapper_args__ class attribute (with value set to __name__).

So far, I have not been able to do anything because during class
definition time, there is no a lot of variables I can access. *self*
obviously does not work. __name__ and __class__ are also useless.

I am thinking, it could be possible if I could write a class wrapper.
I.e. the consumers would extend a wrapper instead of Task class. This
wrapper would return a class object with the __mapper_args__. I could
not find any examples on this topic.

I would really appreciate your help.

Steven D'Aprano

unread,
Sep 10, 2009, 12:00:21 AM9/10/09
to
On Wed, 09 Sep 2009 20:14:33 -0700, gizli wrote:

> I do not want to force the consumers of this framework to write this
> obscure line of code. I was wondering if it is possible (at class
> definition time) to capture the fact that MyTask extends Task and
> automatically insert the polymorphic_identity key into the
> __mapper_args__ class attribute (with value set to __name__).
>
> So far, I have not been able to do anything because during class
> definition time, there is no a lot of variables I can access. *self*
> obviously does not work. __name__ and __class__ are also useless.

Class definitions are controlled by the metaclass, which is fairly deep
magic but not entirely impenetrable. Once you've defined a metaclass to
use, the consumers will only need to say:

class MyTask(Task):
__metaclass__ = MyMetaclass

which is less obscure than the alternative. You may even be able to have
Task use the metaclass, in which case MyTask doesn't need to do anything
special at all. (I think.)


Another alternative is to use a class decorator:

# You write this and provide it in your API.
def mapper(cls):
cls.__mapper_args__ = dict(polymorphic_identity=cls.__name__)
return cls


# The consumer writes this.
@mapper
class MyTask(Task):
pass


Decorator syntax for classes only works for Python 2.6 or better. In 2.5,
the consumer would need to write:

class MyTask(Task):
pass
MyTask = mapper(MyTask)

--
Steven

gizli

unread,
Sep 10, 2009, 2:09:04 AM9/10/09
to
On Sep 9, 9:00 pm, Steven D'Aprano

Steven, thank you. __metaclass__ was exactly what I was looking for.
After a few documents later, I came up with this code:

class PolymorphicSetter(type):
def __new__(cls, name, bases, dictionary):
dictionary['__mapper_args__'] = name
return type.__new__(cls, name, bases, dictionary)

and set this in my Task class:

__metaclass__ = PolymorphicSetter

Also, thank you for the class decorator code. That is what I meant by
class wrapper :) I will keep that as a reference as well.

0 new messages