From c5b36a252752c072bae951e316554587e819d17b Mon Sep 17 00:00:00 2001 From: Philipp Zettl Date: Fri, 4 Sep 2020 16:24:45 +0200 Subject: [PATCH] add structure for objects --- models/channel.py | 18 ++++++++++++++++++ models/programme.py | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 models/channel.py create mode 100644 models/programme.py diff --git a/models/channel.py b/models/channel.py new file mode 100644 index 0000000..ffe41a0 --- /dev/null +++ b/models/channel.py @@ -0,0 +1,18 @@ +class Channel: + def __init__(self, name: str) -> None: + self.name = name + + +class _ChannelManager: + def __init__(self): + self.channels = {} + + def get_or_create(self, name: str): + if name in self.channels.keys(): + return self.channels[name] + new_channel = Channel(name) + self.channels[name] = new_channel + return new_channel + + +ChannelManager = _ChannelManager() diff --git a/models/programme.py b/models/programme.py new file mode 100644 index 0000000..5a383c4 --- /dev/null +++ b/models/programme.py @@ -0,0 +1,20 @@ +from datetime import datetime +from typing import Dict + +from .channel import Channel, ChannelManager + + +class Programme: + def __init__(self, channel: Channel, start: datetime, stop: datetime): + self.channel: Channel = channel + self.start: datetime = start + self.stop: datetime = stop + + @classmethod + def from_dict(cls, input_dict: Dict) -> 'Programme': + date_format = "%Y%m%d%H%M%S %z" + return cls( + ChannelManager.get_or_create(input_dict['channel']), + datetime.strptime(input_dict['start'], date_format), + datetime.strptime(input_dict['stop'], date_format) + )