From f59912acacc470614bad540a9c11b99b56379478 Mon Sep 17 00:00:00 2001 From: burivuh Date: Wed, 11 Sep 2019 12:17:53 +0300 Subject: [PATCH] added new base event type and a factory function to create right instance --- snippets/pyaloha/event.py | 93 ++++++++++++++++++++----------- snippets/pyaloha/event_factory.py | 4 +- 2 files changed, 64 insertions(+), 33 deletions(-) diff --git a/snippets/pyaloha/event.py b/snippets/pyaloha/event.py index 4eee183..5d35d36 100644 --- a/snippets/pyaloha/event.py +++ b/snippets/pyaloha/event.py @@ -1,11 +1,30 @@ +"""Base event classes are defined in this module.""" + + +def get_event(key, event_time, user_info, + data_list=None, data_list_len=0): + """Event factory function.""" + if data_list_len > 1: + return DictEvent( + key, event_time, user_info, + data_list, data_list_len + ) + elif data_list_len > 0: + return ValueEvent(key, event_time, user_info, data_list[0]) + return Event(key, event_time, user_info) + + class Event(object): - def __init__(self, - key, event_time, user_info, - data_list=None, data_list_len=0): - if data_list_len: - self.data_list = data_list - self.data_list_len = data_list_len + """ + Base event class. + Have only basic event info. + """ + + __slots__ = ('key', 'event_time', 'user_info') + + def __init__(self, key, event_time, user_info): + """Copy ctypes structures into a pure Python ones.""" self.event_time = event_time.make_object() self.user_info = user_info.make_object() self.key = key @@ -25,39 +44,51 @@ def __dumpdict__(self): } +class ValueEvent(Event): + """Add a single value to a base event.""" + + __slots__ = ('key', 'event_time', 'user_info', 'value') + + def __init__(self, key, event_time, user_info, value): + """Add self.value property to a base event info.""" + # No super constructor is used for performance reasons. + self.event_time = event_time.make_object() + self.user_info = user_info.make_object() + self.key = key + self.value = value + + def __dumpdict__(self): + d = super(ValueEvent, self).__dumpdict__() + d['value'] = self.value + return d + + class DictEvent(Event): """ - This is a simplified form of any Alohalytics pairs event -when all event params (except datetime and user/device identification) -are accumulated into a dict. - You can try to convert Event instance to it with a @method from_event. + This is a simplified form of any Alohalytics pairs event. + + All event params (except datetime and user/device identification) + are accumulated into a dict. """ - __slots__ = ('data', 'event_time', 'key', 'user_info') + + __slots__ = ('data',) def __init__(self, key, event_time, user_info, data_list, data_list_len): - super(DictEvent, self).__init__( - key, event_time, user_info - ) - - if data_list_len % 2 != 0: - raise ValueError( - "Event can't be casted to a dict without additional knowledge" - ) - - self.data = { - data_list[i]: data_list[i + 1] - for i in range(0, data_list_len, +2) - } + """Add event pairs to a dict self.data.""" + # No super constructor is used for performance reasons. + self.event_time = event_time.make_object() + self.user_info = user_info.make_object() + self.key = key - @classmethod - def from_event(cls, event): - return DictEvent( - event.key, - event.event_time, event.user_info, - event.data_list, event.data_list_len - ) + try: + self.data = { + data_list[i]: data_list[i + 1] + for i in range(0, data_list_len, +2) + } + except IndexError: + raise ValueError('Incorrect data_list') def __basic_dumpdict__(self): return super(DictEvent, self).__dumpdict__() diff --git a/snippets/pyaloha/event_factory.py b/snippets/pyaloha/event_factory.py index 5506a66..cb7e823 100644 --- a/snippets/pyaloha/event_factory.py +++ b/snippets/pyaloha/event_factory.py @@ -1,7 +1,7 @@ import itertools import traceback -from pyaloha.event import Event +from pyaloha.event import get_event class EventFactory(object): @@ -16,7 +16,7 @@ def __init__(self, custom_events): def make_event(self, key, *args, **kwargs): try: - return self.registered.get(key, Event)(key, *args, **kwargs) + return self.registered.get(key, get_event)(key, *args, **kwargs) except Exception as exc: raise Exception( 'Event "%s" creation failed: %s' % (