-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
121 lines (93 loc) · 4.61 KB
/
main.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
from collections import defaultdict
from datetime import datetime
from itertools import islice
from create_revision import CreateRevisionService
from csv_parser import CsvParser
from csv_reader import CsvReader
from deal_serializer import DealSerializer
from file_save_helper import FileSaveHelper
from option_serializer import OptionSerializer
from policy_serializer import PolicySerializer
from src.model.raw_csv import RawCsvGoodsOption, RawCsvPolicy, RawCsvDeal
'''
{option_sno: [(deal, option_binlog)]}
option_sno/deal.txt
option_sno/option.txt
output:
"option_sno, correct_price, wrong_price, consumer_origin, price_origin, thumbnail_price, deal_sno, policy_sno, is_enabled, started_at, ended_at, dt, transaction_time"
1. read csv file
2. form the columns
3. group by {goods_option_sno: Rev(consumer_origin, price_origin)}
'''
print('program starts..')
def dict_slice(dictionary, start=0, stop=None) -> dict:
return dict(islice(dictionary.items(), start, stop))
def process_options(limit: int | None) -> None:
# 1. parse the csv file
options: list[RawCsvGoodsOption] = [
CsvParser.parse_raw_option(line=line) for line in
CsvReader.read(filepath='data/options_250107_250113.csv', limit=limit)
]
# 2. group by goods_sno
option_map: dict[int, list[RawCsvGoodsOption]] = defaultdict(list)
for opt in options:
option_map[opt.goods_sno].append(opt)
# 3. save
serialized_data: bytes = OptionSerializer.serialize_option(raw_map=option_map)
FileSaveHelper.save(data=serialized_data, filepath='data/processed/option_map.msgpack')
# serialized_data: bytes = FileSaveHelper.read(filepath='data/processed/option_map.msgpack')
# deserialized_map: dict[int, list[RawCsvGoodsOption]] = OptionSerializer.deserialize_option(data=serialized_data)
# print('deserialized_map: ', len(deserialized_map))
def process_policies(limit: int | None) -> None:
# 1. parse the csv file
policies: list[RawCsvPolicy] = [
CsvParser.parse_raw_policy(line=line) for line in
CsvReader.read(filepath='data/policies_250107_250113.csv', limit=limit)
]
policy_map: dict[int, list[RawCsvPolicy]] = defaultdict(list)
for policy in policies:
policy_map[policy.goods_sno].append(policy)
# 3. save
serialized_data: bytes = PolicySerializer.serialize(raw_map=policy_map)
FileSaveHelper.save(data=serialized_data, filepath='data/processed/policy_map.msgpack')
# serialized_data: bytes = FileSaveHelper.read(filepath='data/processed/policy_map.msgpack')
# deserialized_map: dict[int, list[RawCsvPolicy]] = PolicySerializer.deserialize(data=serialized_data)
# for k, v in deserialized_map.items():
# for policy in v:
# print('policy ', policy)
def process_deals(limit: int | None) -> None:
# 1. parse the csv file
deals: list[RawCsvDeal] = [
CsvParser.parse_raw_deal(line=line) for line in
CsvReader.read(filepath='data/deals_250107_250113.csv', limit=limit)
]
deal_map: dict[int, list[RawCsvDeal]] = defaultdict(list)
for deal in deals:
deal_map[deal.goods_sno].append(deal)
# 3. save
serialized_data: bytes = DealSerializer.serialize(raw_map=deal_map)
FileSaveHelper.save(data=serialized_data, filepath='data/processed/deal_map.msgpack')
# serialized_data: bytes = FileSaveHelper.read(filepath='data/processed/deal_map.msgpack')
# deserialized_map: dict[int, list[RawCsvPolicy]] = DealSerializer.deserialize(data=serialized_data)
# for k, v in deserialized_map.items():
# for policy in v:
# print('policy ', policy)
LIMIT: int | None = None
begin_time: datetime = datetime.now()
# process_options(limit=LIMIT)
# process_policies(limit=LIMIT)
# process_deals(limit=LIMIT)
serialized_data: bytes = FileSaveHelper.read(filepath='data/processed/option_map.msgpack')
option_map: dict[int, list[RawCsvGoodsOption]] = OptionSerializer.deserialize_option(data=serialized_data)
print('option_map: ', len(option_map))
serialized_data: bytes = FileSaveHelper.read(filepath='data/processed/deal_map.msgpack')
deal_map: dict[int, list[RawCsvDeal]] = DealSerializer.deserialize(data=serialized_data)
deal_map: dict[int, list[RawCsvDeal]] = dict_slice(deal_map, start=0, stop=10)
goods_sno_list: list[int] = list(deal_map.keys())
selected_option_map: dict[int, list[RawCsvGoodsOption]] = {}
for goods_sno in goods_sno_list:
selected_option_map[goods_sno] = option_map[goods_sno]
revisions = CreateRevisionService.create_revision(deal_map=deal_map, option_map=selected_option_map)
print('revisions: ', revisions)
end_time: datetime = datetime.now()
print('elapsed time: ', end_time - begin_time)