-
Notifications
You must be signed in to change notification settings - Fork 0
/
news.py
69 lines (48 loc) · 1.75 KB
/
news.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from datetime import datetime
import json
class News:
def __init__(self, topic, author, datetime, text):
self.topic = topic
self.author = author
self.datetime = datetime
self.text = text
def __str__(self):
return 'topic:{topic} , author:{author} , datetime:{datetime} , text:{text} '.format(
topic = self.topic,
author = self.author,
datetime = self.datetime,
text = self.text
)
def toJson(self):
return {'topic': self.topic, 'author': self.author, 'datetime': str(self.datetime), 'text': self.text} #json not datetime, so use str()
class List_news():
def __init__(self, default = None):
if default is None:
self.news = []
else:
self.news = default
def append(self, data):
#data must not be None, so use try ... except deal with
if type(data) == News :
self.news.append(data)
return self.news
else:
raise BaseException
def __len__(self):
return len(self.news)
def __iter__(self):
return self
def __next__(self):
i = -1
if i < len(self.news):
i +=1
return self.news[i]
def filter(self, func):
for item in self.news:
if func(item): yield item #self.filter() is list( so save only 1 floor)
def search_author(self, goal):
return self.filter(lambda item: goal in item.author)
def search_time(self, first_goal_time, end_goal_time):
return self.filter(lambda item: first_goal_time <= item.datetime.hour < end_goal_time)
def search_topic(self, keyword):
return self.filter(lambda item: keyword in item.topic)