Python Forum

Full Version: Pythonic way to handle/spread alerts class in multiple modules
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Can you explain what you mean by Core -> Handler -> Class A -> Class B | Class C | .. ?

I don't see a problem in defining a module
# spamish.py

class Spam:
    def __init__(self):
        self.eggs = 'eggs'

the_spam = Spam() # <--- our unique instance
In other modules, we can simply import the unique instance
from spamish import the_spam
print(the_spam.eggs)
Well,

I think I found the perfect solution to me (Didn't know this use). Just define them as class variables and classmethods decorator and the variables have the same value across all the instances:

class AlertsHandler:
    """
    Defines all the operations that are required to process all the NIMFty alerts.
    This handler uses class variables that have the same value across all class instances
    """
    alert_1 = []
    alert_2 = []
    alert_3 = []
    ...

    @classmethod
    def send_alerts(cls):
        """
        Sends alerts (emails) about the process execution

        :return: None
        """
        ...
And in my code I just:

AlertsHandler.alert_1.append(x)
AlertsHandler.send_alerts()
when I need :)
Pages: 1 2