-
Notifications
You must be signed in to change notification settings - Fork 2
/
bulletin.py
71 lines (56 loc) · 2.89 KB
/
bulletin.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
68
69
70
from datetime import datetime, timedelta
import json
class Bulletin:
def __init__(self, json_file):
self.input = self.load_json(json_file)
self.date = self.parse_date(self.input['bulletin_date'])
calendar = self.load_json(self.input['calendar_json'])
orgs = self.load_json(self.input['org_json'])
lessons = self.load_json(self.input['lessons_json'])
primary = self.load_json(self.input['primary_json'])
self.input['calendar'] = self.filter_calendar(calendar['events'])
self.input['orgs'] = self.filter_orgs(orgs['orgs'])
self.input['sunday_school'] = self.filter_lessons(lessons['lessons'], 'sunday_school')
self.input['priesthood_rs'] = self.filter_lessons(lessons['lessons'], 'priesthood_rs')
self.input['primary'] = self.filter_primary(primary['weeks'])
def load_json(self, filename):
f = open(filename, 'r')
input = json.load(f)
f.close()
return input
def get_parameters(self):
return self.input
def parse_date(self, date_to_parse):
return datetime.strptime(date_to_parse, "%d %B %Y")
def pretty_date(self, item):
# Added newlines for the 4-column format:
item['date'] = self.parse_date(item['date']).strftime("%A\n\n%d %B")
return item
def is_this_week(self, item):
date_to_test = self.parse_date(item['date'])
return date_to_test >= self.date and date_to_test < (self.date + timedelta(days=7))
def is_next_week(self, item):
date_to_test = self.parse_date(item['date'])
return date_to_test >= (self.date + timedelta(days=7)) and date_to_test < (self.date + timedelta(days=14))
def is_before_this_week(self, item):
date_to_test = self.parse_date(item['date'])
return date_to_test <= self.date
def filter_calendar(self, events):
calendar = {}
calendar['this_week'] = sorted(filter(self.is_this_week, events), key=lambda event: self.parse_date(event['date']))
calendar['next_week'] = sorted(filter(self.is_next_week, events), key=lambda event: self.parse_date(event['date']))
map(self.pretty_date, calendar['this_week'])
map(self.pretty_date, calendar['next_week'])
return calendar
def filter_orgs(self, orgs):
return sorted(filter(self.is_before_this_week, orgs), key=lambda org: self.parse_date(org['date']), reverse=True)
def filter_lessons(self, lessons, type):
filtered = {}
filtered['this_week'] = filter(self.is_this_week, lessons)[0][type]
filtered['next_week'] = filter(self.is_next_week, lessons)[0][type]
return filtered
def filter_primary(self, assignments):
primary = {}
primary['this_week'] = filter(self.is_this_week, assignments)[0]
primary['next_week'] = filter(self.is_next_week, assignments)[0]
return primary