Hacker News new | ask | show | jobs
by fdomingues 1630 days ago
Is useful to do code generation/transformation, for example in this utility[1] it uses the information provided on the declaration of the class to generate the methods required by the base class.

The user of the utility write this:

    class Book(objects.Object):
        title: Optional[str]
And the generated class code is:

    class Book(Base):
        __slots__ = ('title', )
        title: typing.Optional[str]
    
        def __init__(self, title=None):
            self.title = title
    
        @classmethod
        def from_data(cls, data):
            title = data.get('title', None)
            return cls(title=title)
    
        def to_data(self):
            data = {}
            if self.title is not None:
                data['title'] = self.title
            return data
    
        @classmethod
        def from_oracle_object(cls, obj):
            return cls(title=obj.title)
    
        def to_oracle_object(self, connection):
            obj = connection.gettype('Book').newobject()
            obj.title = self.title
            return obj
[1]https://github.com/domingues/oracle-object-mapping