Neo Yion
Neo Yion

Reputation: 11

Consume instance of existing class same as the parent by the child class

I can't directly overwrite all of the parent attributes from a child with an already instantiated class that is of the same type as the parent the child inherits from.

What I'm trying to achieve is in a child class directly overwrite all its parent attributes by an already instantiated class that is the same as the parent.

The answer in this question describes good what I want: Create child class instance from parent class instance

If you mean (as @Barmar suggested) that you want a way to copy all the attributes of an instance of Foo over to an instance of Bar, that is another story. In that case, you still need to be careful to define, what exactly you mean by "all attributes".

With "all attributes" I mean just copy over mine defined ones. Like the same way as pydantic see and grab them for its typing context. And in an automatic way, because most classes don't have just 10-20 attrs, but 50-200. And set each one by one in init by hand to copy its value over would be overkill, as they are already once written down in the respective class.

The design problem is, the policy must do the first request to the database to get the case_id of the records ID it refers to, so with that gathered knowledge it knows how to load the details of that policy from the database.
That's because I load a python file ({case_id}.py) where I store the specific infos on what tablename to lookup and what fields there are. So it is clean seperated.

main.py

from Models.Policy import Policy
from Models.PolicyDetails import PolicyDetails
policy = Policy()
policy.findById(policy_id)
details = PolicyDetails()
details.loadFrom( policy )

Policy.py

from pydantic import BaseModel
from .ActiveRecord import ActiveRecord

class Policy(BaseModel, ActiveRecord):
    id: int = None
    case_id: str = None
    v_number: str = None
    g_id: str = None

    def __init__(self):
        BaseModel.__init__(self)
        ActiveRecord.__init__(Policy)
    
    def tableName(self):
        return 'gn_policy'
    
    def idName(self):
        return 'identifier'
    
    def getTableColumnMap(self):
        return {
            "identifier": "id",
            "case_description": "case_id",
            "pol_doc_number": "v_number",
            "grow_val_identifier": "g_id"
        }

PolicyDetails.py
In this class I load dynamically {case_id}.py files and instanciate them and want to consume the child.

import importlib
from pydantic import BaseModel
from .Policy import Policy

class PolicyDetails(BaseModel):

    details: object = None
    
    def __init__(self):
        pass
    
    def loadFrom(self, policy: Policy):
        case_id = policy.case_id
        case_class = self.load_class(case_id)
        case_instance = case_class(policy)
        
    def load_class(self, id: str):
        case_module = importlib.import_module("."+id, "Models")
        case_class = getattr(case_module, id)
        return case_class

Two {case_id}.py file looks like this:
GERM.py

class GERM(Policy):
    def tableName():
        return 'gn_GERM'
    
    address: str = None
    def getTableColumnMap(self):
        return {
            "adr": "address"
        }

IMP.py

class IMP(Policy):
    def tableName():
        return 'gn_IMP'
    
    firstname: str = None
    lastname: str = None
    def getTableColumnMap(self):
        return {
            "FI": "firstname",
            "LA": "lastname"
        }

The main goal is to populate/hydrate the policy instance with the delayed gathered details of it.

Is inheritance intended for solving this or moreover done by composition?

Thanks in advance.

Upvotes: 0

Views: 33

Answers (0)

Related Questions