Skip to content

Commit

Permalink
added new base event type and a factory function to create right inst…
Browse files Browse the repository at this point in the history
…ance
  • Loading branch information
burivuh authored and vicpopov committed Sep 11, 2019
1 parent c29d0e8 commit f59912a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 33 deletions.
93 changes: 62 additions & 31 deletions snippets/pyaloha/event.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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__()
Expand Down
4 changes: 2 additions & 2 deletions snippets/pyaloha/event_factory.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import itertools
import traceback

from pyaloha.event import Event
from pyaloha.event import get_event


class EventFactory(object):
Expand All @@ -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' % (
Expand Down

0 comments on commit f59912a

Please sign in to comment.