-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathkl8_cash_plus.py
227 lines (215 loc) · 10.4 KB
/
kl8_cash_plus.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# -*- coding:utf-8 -*-
"""
Author: KittenCN
"""
import pandas as pd
import argparse
# import subprocess
# import threading
from multiprocessing import Process
from tqdm import tqdm
from config import *
from itertools import combinations
# from loguru import logger
# from concurrent.futures import ThreadPoolExecutor, as_completed
parser = argparse.ArgumentParser()
parser.add_argument('--name', default="kl8", type=str, help="lottery name")
parser.add_argument('--download', default=1, type=int, help="download data")
parser.add_argument('--cash_file_name', default="-1", type=str, help='cash_file_name')
parser.add_argument('--current_nums', default=-1, type=int, help='current nums')
parser.add_argument('--path', default="", type=str, help='path')
parser.add_argument('--simple_mode', default=0, type=int, help='simple mode')
parser.add_argument('--random_mode', default=0, type=int, help='random mode')
parser.add_argument('--cal_nums', default=10, type=int, help='cal_nums')
#--------------------------------------------------------------------------------------------------#
parser.add_argument('--limit_line', default=0, type=int, help='useless')
parser.add_argument('--total_create', default=50, type=int, help='useless')
parser.add_argument('--multiple', default=1, type=int, help='useless')
parser.add_argument('--multiple_ratio', default="1,0", type=str, help='useless')
parser.add_argument('--repeat', default=1, type=int, help='useless')
parser.add_argument('--calculate_rate', default=0, type=int, help='useless')
parser.add_argument('--calculate_rate_list', default="5", type=str, help='useless')
parser.add_argument('--max_workers', default=4, type=int, help='useless')
args = parser.parse_args()
if args.random_mode == 0:
if args.path == "":
file_path = "./results/"
else:
file_path = "./results_" + args.path + "/"
elif args.random_mode == 1:
if args.path == "":
file_path = "./random/"
else:
file_path = "./random_" + args.path + "/"
endstring = ["csv"]
name = args.name
nums_index = 0
cal_nums = int(args.cal_nums)
content = []
if args.download == 1:
from common import get_data_run
get_data_run(name=name, cq=0)
ori_data = pd.read_csv("{}{}".format(name_path[name]["path"], data_file_name))
ori_numpy = ori_data.drop(ori_data.columns[0], axis=1).to_numpy()[0][1:]
# if args.current_nums >= 0:
# index = ori_data.drop(ori_data.columns[0], axis=1).to_numpy()[0][0] - (args.current_nums + 1)
# if index >= 0:
# ori_numpy = ori_data.drop(ori_data.columns[0], axis=1).to_numpy()[index][1:]
cash_select_list = []
for i in range(0, 11):
_t = [element for element in range(i, -1, -1)]
cash_select_list.append(_t)
cash_price_list = [[5000000, 8000, 800, 80, 5, 3, 0, 0, 0, 0, 2], \
[300000, 2000, 200, 20, 5, 3, 0, 0, 0, 2], \
[50000, 800, 88, 10, 3, 0, 0, 0, 2], \
[10000, 288, 28, 4, 0, 0, 0, 2], \
[3000, 30, 10, 3, 0, 0, 0], \
[1000, 21, 3, 0, 0, 0], \
[100, 5, 3, 0, 0], \
[53, 3, 0, 0], \
[19, 0, 0], \
[4.6, 0]]
def sub_check_lottery(item, cash_select, cash_price, cash_list):
for index in range(len(cash_select)):
ori_split = list(combinations(ori_numpy, cash_select[index]))
cash_split = list(combinations(item, cash_select[index]))
cash_set = set(ori_split) & set(cash_split)
if cash_select[index] != 0:
cash_list[index] += len(cash_set)
if cash_price[index] != 0 and len(cash_set) != 0:
return cash_list
elif cash_select[index] == 0 and len(cash_set) == 0:
cash_list[index] += 1
return cash_list
def check_lottery(file_path, filename, args):
global ori_numpy, nums_index, all_cash, all_lucky, content, cal_nums
cash_file_name = file_path + filename
filename_split = filename.split('_')
if len(filename_split) == 4:
if int(filename_split[-1].split('.')[0]) > 0:
args.current_nums = int(filename_split[-1].split('.')[0])
nums_index += 1
if args.current_nums >= ori_data.drop(ori_data.columns[0], axis=1).to_numpy()[-1][0] and args.current_nums <= ori_data.drop(ori_data.columns[0], axis=1).to_numpy()[0][0]:
index = ori_data.drop(ori_data.columns[0], axis=1).to_numpy()[0][0] - args.current_nums
if index >= 0:
ori_numpy = ori_data.drop(ori_data.columns[0], axis=1).to_numpy()[index][1:]
cash_data = pd.read_csv(cash_file_name)
cash_numpy = cash_data.to_numpy()
if cal_nums >= 0:
cal_nums = cash_numpy.shape[1]
else:
cal_nums = abs(cal_nums)
cash_select = cash_select_list[cal_nums]
cash_price = cash_price_list[10 - (cal_nums)]
cash_list = [0] * len(cash_select)
# for j in tqdm(range(len(cash_numpy)), desc='subCashThread {}'.format(args.path), leave=False):
for item in cash_numpy:
# item = cash_numpy[j]
for index in range(len(cash_select)):
ori_split = list(combinations(ori_numpy, cash_select[index]))
cash_split = list(combinations(item, cash_select[index]))
cash_set = set(ori_split) & set(cash_split)
if cash_select[index] != 0:
cash_list[index] += len(cash_set)
if cash_price[index] != 0 and len(cash_set) != 0:
break
elif cash_select[index] == 0 and len(cash_set) == 0:
cash_list[index] += 1
break
# with ThreadPoolExecutor(max_workers=int(args.max_workers)) as executor:
# future_to_url = {executor.submit(sub_check_lottery, item, cash_select, cash_price, cash_list): item for item in cash_numpy}
# for future in as_completed(future_to_url):
# data = future.result()
# if data != None:
# cash_list = data
total_cash = 0
for i in range(len(cash_select)):
total_cash += cash_list[i] * cash_price[i]
if args.simple_mode == 0 or (args.simple_mode == 2 and total_cash / (len(cash_numpy) * 2) * 100 >= 100):
# logger.info("{}, 第{}期,本期共投入{}元,总奖金为{}元,返奖率{:.2f}%。".format(args.path, nums_index, len(cash_numpy) * 2, total_cash, total_cash / (len(cash_numpy) * 2) * 100))
content.append("{}, 第{}张,本期共投入{}元,总奖金为{}元,返奖率{:.2f}%。".format(args.path, nums_index, len(cash_numpy) * 2, total_cash, total_cash / (len(cash_numpy) * 2) * 100))
all_cash += len(cash_numpy) * 2
all_lucky += total_cash
return all_cash, all_lucky, content, args
## 判断文件是否存在
def check_file(_file_name):
if os.path.exists(_file_name):
return True
else:
return False
## 多线程调用写入文件
def write_file(_content,_file_name="./kl8_runnint_results.txt"):
# t = threading.Thread(target=write_file_core, args=(_content, _file_name))
t = Process(target=write_file_core, args=(_content, _file_name))
t.start()
## 写入文件
def write_file_core(_content,_file_name="./kl8_runnint_results.txt"):
if check_file(_file_name):
write_mode = "a"
else:
write_mode = "w"
with open(_file_name, write_mode) as f:
for item in _content:
f.write(item + "\n")
if __name__ == "__main__":
nums_index = 0
if args.path == "" or args.cash_file_name != "-1":
if args.random_mode == 0:
if args.path == "":
file_path = "./results/"
else:
file_path = "./results_" + args.path + "/"
elif args.random_mode == 1:
if args.path == "":
file_path = "./random/"
else:
file_path = "./random_" + args.path + "/"
if args.cash_file_name != "-1":
cash_file_name = file_path + args.cash_file_name + ".csv"
else:
## 寻找目录下最新的文件
import os
file_list = [_ for _ in os.listdir(file_path) if _.split('.')[1] in endstring]
file_list.sort(key=lambda fn: os.path.getmtime(file_path + fn))
cash_file_name = file_path + file_list[-1]
filename_split = file_list[-1].split('_')
if len(filename_split) == 4:
if int(filename_split[-1].split('.')[0]) > 0:
args.current_nums = int(filename_split[-1].split('.')[0])
check_lottery(cash_file_name=cash_file_name, args=args, path_mode=0)
else:
if args.random_mode == 0:
if args.path == "":
file_path = "./results/"
else:
file_path = "./results_" + args.path + "/"
elif args.random_mode == 1:
if args.path == "":
file_path = "./random/"
else:
file_path = "./random_" + args.path + "/"
all_cash, all_lucky = 0, 0
import os
file_list = [_ for _ in os.listdir(file_path) if _.split('.')[1] in endstring]
file_list.sort(key=lambda fn: os.path.getmtime(file_path + fn))
threads = []
# for j in tqdm(range(len(file_list)), desc='CashThread {}'.format(args.path), leave=False):
for j in range(len(file_list)):
filename = file_list[j]
# t = threading.Thread(target=check_lottery, args=(file_path, filename, args))
t = Process(target=check_lottery, args=(file_path, filename, args))
threads.append(t)
t.start()
# for t in threads:
for t_index in tqdm(range(len(threads)), desc='CashThread {}'.format(args.path), leave=False):
t = threads[t_index]
t.join()
# with ThreadPoolExecutor(max_workers=int(args.max_workers)) as executor:
# future_to_url = {executor.submit(check_lottery, file_path, file_list[filename_index], args): file_list[filename_index] for filename_index in tqdm(range(len(file_list)), desc='CashThread {}'.format(args.path), leave=False)}
# for future in as_completed(future_to_url):
# data = future.result()
# if data != None:
# all_cash, all_lucky, content, args = data
# logger.info("{}, 总投入{}元,总奖金为{}元,返奖率{:.2f}%。".format(args.path, all_cash, all_lucky, all_lucky / all_cash * 100))
content.append("{}, 总投入{}元,总奖金为{}元,返奖率{:.2f}%。".format(args.path, all_cash, all_lucky, all_lucky / all_cash * 100))
write_file(content)