Структура класса Python ... метод prep ()? - PullRequest
0 голосов
/ 02 апреля 2010

У нас есть метакласс, класс и дочерний класс для системы оповещений:

class AlertMeta(type):
"""
Metaclass for all alerts    
Reads attrs and organizes AlertMessageType data
"""
def __new__(cls, base, name, attrs):
    new_class = super(AlertMeta, cls).__new__(cls, base, name, attrs)
    # do stuff to new_class
    return new_class

class BaseAlert(object):
"""
BaseAlert objects should be instantiated
in order to create new AlertItems.
Alert objects have classmethods for dequeue (to batch AlertItems)
and register (for associated a user to an AlertType and AlertMessageType)

If the __init__ function recieves 'dequeue=True' as a kwarg, then all other
arguments will be ignored and the Alert will check for messages to send
"""

__metaclass__ = AlertMeta

def __init__(self, **kwargs):
    dequeue = kwargs.pop('dequeue',None)
    if kwargs:
        raise ValueError('Unexpected keyword arguments: %s' % kwargs)
    if dequeue:
        self.dequeue()
    else:
        # Do Normal init stuff

def dequeue(self):
    """
    Pop batched AlertItems
    """
    # Dequeue from a custom queue

class CustomAlert(BaseAlert):
    def __init__(self,**kwargs):
        # prepare custom init data
        super(BaseAlert, self).__init__(**kwargs)

Мы хотели бы иметь возможность создавать дочерние классы BaseAlert (CustomAlert), которые позволяют нам запускать очереди и иметь возможность запускать собственный код __init__. Мы думаем, что есть три способа сделать это:

  1. Добавить метод prep (), который возвращает True в BaseAlert и вызывается __init__. Дочерние классы могут определять свои собственные методы prep ().
  2. Сделайте dequeue () методом класса - однако, для того, чтобы dequeue () делала, требуются не-классовые методы - так что нам также придется создавать эти методы класса.
  3. Создать новый класс для работы с очередью. Будет ли этот класс расширять BaseAlert?

Существует ли стандартный способ обработки ситуаций такого типа?

1 Ответ

0 голосов
/ 02 апреля 2010

Для нашей конкретной проблемы мы делаем метод dequeue () классом.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...