Automatic Attribute Assignment during Class Inheritance

G

gizli

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

Steven D'Aprano

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)
 
G

gizli

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

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top