0

I want to design specific class hierarchy consisting of three classes: Certificate, Organization, User. Both Certificate and Organization seem to me to be kinda "equal" (they are each others attributes and are dependent on each others):

class Certificate:
    def __init__(self,
                 certificate_id: int,
                 private_key_id: int,
                 organization: Organization = None, # Organization as an attribute!!!):
        self.organization = Organization(organization.name, organization.unit, organization.parent_id)
        self.organization.set_certificate_id(certificate_id)
        if organization.parent_id is not None:
            self.certificate_id: int = organization.parent_id
        else:
            self.certificate_id: int = certificate_id
        self.private_key_id: int = private_key_id
class Organization:
    def __init__(self,
                 name: str,
                 unit: str,
                 parent_id: int):
        self.name: str = name
        self.unit: str = unit
        self.parent_id: int = parent_id
        self.certificate_id = None

    def set_certificate_id(self, x):
        self.certificate_id = x

    def __get_certificate_id(self):
        return self.certificate_id

And then there is User class. User belongs to 1:N Organizations and has an access to their certificate ID's so it has Organization object as one of its attributes but I cannot access to its certificate - even though I defined a getter for Organization class:

class User:
    access = ("admin", "user", "manager"),

    def __init__(self,
                 name: str,
                 role: str,
                 **kwargs: Organization
                 # org: Organization 
                 # **kwargs
                 ):
        self.name = name,
        self.role = role,
        self.orgs = kwargs
        # for key, value in kwargs.iteritems():
        #     setattr(self, key, value)
        # self.orgs = {}

    def use_id(self):
        return self.orgs._Certificate__get_certificate_id()

Last line is kinda random, I tried accessing certificate in every way which came to my mind, ofc starting with self.orgs.certificate_id, what I get is (mostly) warning "Unresolved attribute reference '...' for class 'dict'" How am I supposed to access the ID of Certificate of given Organization which my User belongs to?

BTW, kwargs in init: I want to be able to set an undefined number of Organization objects as parameter, possibly as dictionary [Org: Certificate_id]

  • Janek, please provided a minimal reproducible code example. You have a lot of code that is not needed to reproduce your issue. Also, your provided code cannot run on its own. – Melon May 13 '22 at 14:09
  • Organizations *have* certificates; neither is a kind of the other. – chepner May 13 '22 at 14:11
  • @Melon fixed it a bit, I hope it's better now. Unfortunately, this is only a part of bigger project and I can't provide an independently running code – Janek Szpontyn May 13 '22 at 17:39
  • @chepner that's right, but also certificate should store data about which org owns it, that was requested – Janek Szpontyn May 13 '22 at 17:39
  • It looks like you're getting the error by trying to select the certificate id from the whole dictionary object and not just one member(i.e selecting orgs(the entire dictionary) instead of a particular member of the list). You could try `self.orgs[i].__get_certificate_id()` for each "i" in orgs. – Alias Cartellano May 13 '22 at 22:18

0 Answers0