-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessing-themes.py
64 lines (54 loc) · 2.6 KB
/
processing-themes.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
import logging
from datetime import date
from constants import DEMOGRAPHIC_TYPE_TO_LIST_MAP, DEMOGRAPHIC_TYPES, PARTIES
from models import Ad
from themes import Theme
from utils import recursive_round, render_template
logging.basicConfig(
format="[%(asctime)s] %(levelname)s: %(message)s",
level=logging.INFO,
datefmt="%Y-%m-%d %H:%M:%S",
)
SEPT_1 = date(year=2020, month=9, day=1)
if __name__ == "__main__":
theme_data = {
"impressions-demographics-theme": {t: {dt: [] for dt in DEMOGRAPHIC_TYPES} for t in Theme.titles()},
"impressions-demographics-theme-party": {
p: {t: {dt: [] for dt in DEMOGRAPHIC_TYPES} for t in Theme.titles()} for p in PARTIES
},
"impressions-theme-party": {p: [] for p in PARTIES},
"number-of-ads-theme-party": {p: [] for p in PARTIES},
"matched": {
p: [
Ad.ads_in_time_range(first_date=SEPT_1).where(Ad.themes != 0).where(Ad.party == p).count(),
Ad.ads_in_time_range(first_date=SEPT_1).where(Ad.themes == 0).where(Ad.party == p).count(),
]
for p in PARTIES
},
}
for theme in Theme.all():
logging.info(f"Processing {theme.title}.")
ads = Ad.ads_in_time_range(first_date=SEPT_1).where(Ad.themes.bin_and(theme.value) == theme.value)
for demographic_type in DEMOGRAPHIC_TYPES:
for demographic in DEMOGRAPHIC_TYPE_TO_LIST_MAP[demographic_type]:
theme_data["impressions-demographics-theme"][theme.title][demographic_type].append(
sum(a.rank_to_data("impressions", demographic) for a in ads)
)
for party in PARTIES:
ads = (
Ad.ads_in_time_range(first_date=SEPT_1)
.where(Ad.party == party)
.where(Ad.themes.bin_and(theme.value) == theme.value)
)
theme_data["number-of-ads-theme-party"][party].append(ads.count())
theme_data["impressions-theme-party"][party].append(
sum(a.rank_to_data("impressions", "total") for a in ads)
)
for demographic_type in DEMOGRAPHIC_TYPES:
for demographic in DEMOGRAPHIC_TYPE_TO_LIST_MAP[demographic_type]:
theme_data["impressions-demographics-theme-party"][party][theme.title][demographic_type].append(
sum(a.rank_to_data("impressions", demographic) for a in ads)
)
logging.debug("Writing templates.")
recursive_round(theme_data)
render_template("themes.html", "themes.html", theme_data=theme_data, THEMES=Theme.titles())