Python metaprogramming help

Posted by Timmy on Stack Overflow See other posts from Stack Overflow or by Timmy
Published on 2010-05-07T14:25:55Z Indexed on 2010/05/07 14:28 UTC
Read the original article Hit count: 251

Filed under:
|

im looking into mongoengine, and i wanted to make a class an "EmbeddedDocument" dynamically, so i do this

def custom(cls):
    cls = type( cls.__name__, (EmbeddedDocument,), cls.__dict__.copy() )
    cls.a = FloatField(required=True)
    cls.b = FloatField(required=True)
    return cls

A = custom( A )

and tried it on some classes, but its not doing some of the base class's init or sumthing

in BaseDocument

def __init__(self, **values):
    self._data = {}
    # Assign initial values to instance
    for attr_name, attr_value in self._fields.items():
        if attr_name in values:
            setattr(self, attr_name, values.pop(attr_name))
        else:
            # Use default value if present
            value = getattr(self, attr_name, None)
            setattr(self, attr_name, value)

but this never gets used, thus never setting ._data, and giving me errors. how do i do this?

© Stack Overflow or respective owner

Related posts about python

Related posts about metaprogramming