Python: how to create a container with elements that should reference their container

(The name, admittedly, is not that great. Please forgive my English, this is the best I could come up with.)

I am writing a python script that will manage email domains and their accounts, and I am also new to OOP design. My two problems (related?):

  • The domain class must do special work to add and remove accounts, such as adding / removing them to the base implementation
  • how to manage account operations that must go through their container

To solve the previous problem, I will add the factory method to the Domain class, which will create an instance of the account in this domain, and the remove (anti-factory?) Method to handle exceptions.

For the latter, it seems to me "anti-oop", because logically there can be an operation on the account (for example, change the password), which should always refer to the containing domain. It seems to me that I should add a link to the domain in my account and use it to get data (for example, domain name) or call methods in the domain class.

Sample code (the element uses data from the container) that controls the basic Vpopmail system :

class Account:
    def __init__(self, name, password, domain):
        self.name = name
        self.password = password
        self.domain = domain
    def set_password(self, password):
        os.system('vpasswd %s@%s %s' % (self.name, self.domain.name, password)
        self.password = password

class Domain:
    def __init__(self, domain_name):
        self.name = domain_name
        self.accounts = {}
    def create_account(self, name, password):
        os.system('vadduser %s@%s %s' % (name, self.name, password))
        account = Account(name, password, self)
        self.accounts[name] = account
    def delete_account(self, name):
        os.system('vdeluser %s@%s' % (name, self.name))
        del self.accounts[name]

Another option for Account.set_password is to call a domain method that will do the actual work - which sounds equally ugly to me.

( dict), ( " " ), .

: , , . , ( , , , ..).

, , , , , , , ( ) . , (), .

+3
4

, Account . , , Domain, - . Domain.accounts username: password .

, , .

, , , , , . Python , .

(, os.system, . . subprocess .)

+2

, / Domain. :

class Account:
    def __init__(self, name, password, domain):
        ...

    def activate(self):
        self.domain.add(self)
        os.system('vadduser %s@%s %s' % (name, self.domain.name, password))

    def deactivate(self):
        self.domain.remove(self)
        os.system('vdeluser %s@%s' % (name, self.domain.name)

, , . python - SQLAlchemy. ( ). , , , .

+1

Python , .

: , .

: , -, , ; .

+1

, , , , ...

"" "AccountManager" Python, ? , , , " ", .

class Manager(object):
    def set_password(self, domain, account, password):
        os.system('vpasswd %s@%s %s' % (account.name, domain.name, password)
        account.password = password

>>> m = Manager()
>>> d = Domain('google.com')
>>> a = Account('foouser')
>>> m.set_password(d, a, p)

, -, , , .

0

Source: https://habr.com/ru/post/1741300/


All Articles