有点问题待解决

main
roger 2 years ago
parent 429b216306
commit 528e5d426a
  1. 154
      orm/db.py
  2. 19
      orm/orm_db.py
  3. 100
      orm/real_index.py
  4. 38
      orm/red_forecast.py

@ -1,9 +1,9 @@
import json
from orm_db import History, BlueForecastFiveAll, BlueForecastThreeAll, RedForecastAll
from orm_db import History, BlueForecastFiveAll, BlueForecastThreeAll, RedForecastAll, IndexGroup
from sqlalchemy import engine
from sqlalchemy.orm import sessionmaker, declarative_base
from sqlalchemy import select, insert, desc, asc
from sqlalchemy import select, insert, update, desc, asc, and_
# from sqlalchemy import Column, Integer, String, DATE
@ -54,6 +54,18 @@ def db_history_get_data_in_ids(_ids):
return False
def db_blue_get_data_by_date_id(_date_id, _num):
try:
if _num == 3:
select_stmt = select(BlueForecastThreeAll).where(BlueForecastThreeAll.dateId == _date_id)
else:
select_stmt = select(BlueForecastFiveAll).where(BlueForecastFiveAll.dateId == _date_id)
select_result = db_session.execute(select_stmt).scalars()
return blue_scalars(select_result)
except Exception as e:
print(e)
def db_blue_insert(_data, _num):
try:
if _num == 5:
@ -73,15 +85,42 @@ def db_blue_insert(_data, _num):
print(e)
def db_red_get_index():
def db_blue_update(_date_id, _data, _id, _num):
try:
if _num == 5:
_table = BlueForecastFiveAll
elif _num == 3:
_table = BlueForecastThreeAll
else:
print('_num值错误')
blue_update_stmt = update(_table).where(and_(_table.dateId == _date_id, _table.id == _id)).values(_data)
update_result = db_session.execute(blue_update_stmt).rowcount
if update_result > 0:
db_session.commit()
else:
db_session.rollback()
return update_result
except Exception as e:
print(e)
# def db_red_get_index():
# try:
# index_stmt = select(RedForecastAll.real_red_index_group)
# index_result = db_session.execute(index_stmt).scalars()
# index_result_list = []
# for index in index_result:
# if index is not None:
# index_result_list.append(json.loads(index))
# return index_result_list
# except Exception as e:
# print(e)
def db_red_get_data_by_date_id(_date_id):
try:
index_stmt = select(RedForecastAll.real_red_index_group)
index_result = db_session.execute(index_stmt).scalars()
index_result_list = []
for index in index_result:
if index is not None:
index_result_list.append(json.loads(index))
return index_result_list
select_stmt = select(RedForecastAll).where(RedForecastAll.dateId == _date_id)
select_result = db_session.execute(select_stmt).scalars()
return red_scalars(select_result)
except Exception as e:
print(e)
@ -99,6 +138,62 @@ def db_red_insert(_data):
print(e)
def db_red_update(_id, _date_id, _data):
try:
update_stmt = update(RedForecastAll).where(
and_(RedForecastAll.dateId == _date_id, RedForecastAll.id == _id)).values(_data)
update_result = db_session.execute(update_stmt).rowcount
if update_result > 0:
db_session.commit()
else:
db_session.rollback()
return update_result
except Exception as e:
print(e)
def db_index_get_index_group(_type):
index_stmt = select(IndexGroup.index_group).where(IndexGroup.type == _type)
index_result = db_session.execute(index_stmt)
index_list = []
for index in index_result:
index_list.append(json.loads(index.index_group))
return index_list
def db_index_insert_index_group(_data):
try:
index_stmt = insert(IndexGroup).values(_data)
index_result = db_session.execute(index_stmt).rowcount
if index_result > 0:
db_session.commit()
else:
db_session.rollback()
except Exception as e:
print(e)
def db_history_get_last_open_id():
date_id_stmt = select(History.dateId).order_by(desc(History.id)).limit(2)
date_id_result = db_session.execute(date_id_stmt).scalars()
date_id_list = [date_id for date_id in date_id_result]
return date_id_list
def db_red_get_last_one_open_id():
date_id_stmt = select(RedForecastAll.dateId).order_by(desc(RedForecastAll.id)).limit(1)
date_id_result = db_session.execute(date_id_stmt).scalar()
return date_id_result
def db_blue_get_last_one_open_id():
date_id_three_stmt = select(BlueForecastThreeAll.dateId).order_by(desc(BlueForecastThreeAll.id)).limit(1)
date_id_three_result = db_session.execute(date_id_three_stmt).scalar()
date_id_five_stmt = select(BlueForecastThreeAll.dateId).order_by(desc(BlueForecastThreeAll.id)).limit(1)
date_id_five_result = db_session.execute(date_id_five_stmt).scalar()
return {'three': date_id_three_result, 'five': date_id_five_result}
def history_scalar(_scalar):
_data = dict()
_data['id'] = _scalar.id
@ -120,3 +215,42 @@ def history_scalars(_scalars):
_data['blue'] = row.blue
_list.append(_data)
return _list
def red_scalars(_scalars):
_list = []
for row in _scalars:
_data = dict()
_data['id'] = row.id
_data['dateId'] = row.dateId
_data['history'] = row.history
_data['history_random'] = row.history_random
_data['history_random_index'] = row.history_random_index
_data['history_random_index_group'] = row.history_random_index_group
_data['last_random'] = row.last_random
_data['last_random_index'] = row.last_random_index
_data['last_random_index_group'] = row.last_random_index_group
_data['real_red'] = row.real_red
_data['real_red_index_history'] = row.real_red_index_history
_data['real_red_index_group_history'] = row.real_red_index_group_history
_data['real_red_index_last'] = row.real_red_index_last
_data['real_red_index_group_last'] = row.real_red_index_group_last
_list.append(_data)
return _list
def blue_scalars(_scalars):
_list = []
for row in _scalars:
_data = dict()
_data['id'] = row.id
_data['dateId'] = row.dateId
_data['real_blue'] = row.real_blue
_data['prv'] = row.prv
_data['prv_index'] = row.prv_index
_data['post'] = row.post
_data['post_index'] = row.post_index
_data['history'] = row.history
_data['history_index'] = row.history_index
_list.append(_data)
return _list

@ -1,7 +1,7 @@
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy import Column, Integer, String, DATE
from sqlalchemy import Column, Integer, String, DATE, Enum
# 创建数据引擎
DB_URI = "mysql+pymysql://root:Sxzgx1209@home.rogersun.cn:3306/lottery"
@ -48,7 +48,7 @@ class BlueForecastThreeAll(Base):
prv = Column(String(500), nullable=False, comment='根据当前一期的蓝球,从历史数据中往前推5期,计算蓝球出现概率')
prv_index = Column(Integer, comment='根据实际结果计算蓝球的index位置')
post = Column(String(500), nullable=False,
comment='根据当前一期的蓝球,从历史数据中往后推5期,计算蓝球出现概率')
comment='根据当前一期的蓝球,从历史数据中往后推5期,计算蓝球出现概率')
post_index = Column(Integer, comment='根据实际结果计算蓝球的index位置')
history = Column(String(500), nullable=False, comment='根据当前一期的蓝球,从历史数据中计算每个蓝球出现的概率')
history_index = Column(Integer, comment='历史中的蓝球索引')
@ -69,8 +69,19 @@ class RedForecastAll(Base):
last_random_index = Column(String(256), comment='预测红球数据对应的index')
last_random_index_group = Column(String(256), comment='预测红球数据对应的index分布')
real_red = Column(String(256), comment='下一期的真实的红球数据')
real_red_index = Column(String(100), comment='真实红球在此次预测中的索引位置')
real_red_index_group = Column(String(100), comment='真实红球在此次预测中的索引位置的分布')
real_red_index_history = Column(String(100), comment='真实红球在此次预测中的索引位置')
real_red_index_group_history = Column(String(100), comment='真实红球在此次预测中的索引位置的分布')
real_red_index_last = Column(String(100), comment='真实红球在此次预测中的索引位置')
real_red_index_group_last = Column(String(100), comment='真实红球在此次预测中的索引位置的分布')
class IndexGroup(Base):
__tablename__ = 'index_group' # 定义表名
# 定义表结构
id = Column(Integer, primary_key=True, comment='主键')
type = Column(Enum('history', 'last'), default='history', nullable=False, comment='索引类型')
index_group = Column(String(100), nullable=False, comment='用于手机索引组数据')
# 创建数据表,下面部分没有联想

@ -1,2 +1,100 @@
from orm_db import *
from db import *
import json
def general_red_index(_real_red, _forecast_data):
index_list = []
for _red in _forecast_data.keys():
if _red in _real_red:
index_list.append(_forecast_data.keys().index(_red) + 1)
index_group = [0, 0, 0]
for index in index_group:
if index <= 11:
index_group[0] += 1
elif 11 < index <= 22:
index_group[1] += 1
elif 22 < index <= 33:
index_group[2] += 1
else:
print(f'索引值存在错误{index_list}')
return index_list, index_group
def general_blue_index(_real_blue, _forecast_data):
return list(_forecast_data.keys()).index(_real_blue) + 1
def update_real_data():
new_history_date_id, old_history_date_id = db_history_get_last_open_id()
blue_date_id = db_blue_get_last_one_open_id()
red_date_id = db_red_get_last_one_open_id()
all_data = db_history_get_all_data()
# 如果 历史里面有新数据: 需要计算真实的index,然后预测新数据
# 如果 历史记录中是老数据: 需要预测新数据
if red_date_id == old_history_date_id:
# 插入 RedForecastAll real_red, real_red_index, real_red_index_group
real_red = json.loads(all_data[-1]['red'])
forecast_red_data = db_red_get_data_by_date_id(red_date_id)
for data in forecast_red_data:
_id = data['id']
history_index_list, history_index_group = general_red_index(real_red, json.loads(data['history']))
last_index_list, last_index_group = general_red_index(real_red, json.loads(data['last']))
update_data = {
'real_red': real_red,
'real_red_index_history': history_index_list,
'real_red_index_group_history': history_index_group,
'real_red_index_last': last_index_list,
'real_red_index_group_last': last_index_group
}
db_red_update(_id, red_date_id, update_data)
# 插入 IndexGroup index_group
db_index_insert_index_group({'type': 'history', 'index_group': json.dumps(history_index_group)})
db_index_insert_index_group({'type': 'last', 'index_group': json.dumps(last_index_group)})
if blue_date_id['five'] == old_history_date_id:
# 插入 real_blue, prv_index, post_index, history_index
real_blue = all_data[-1]['blue']
forecast_blue_data = db_blue_get_data_by_date_id(blue_date_id['five'], 5)
for data in forecast_blue_data:
print(data)
_id = data['id']
prv_index = general_blue_index(real_blue, json.loads(data['prv']))
post_index = general_blue_index(real_blue, json.loads(data['post']))
history_index = general_blue_index(real_blue, json.loads(data['history']))
print(prv_index)
print(post_index)
print(history_index)
update_data = {
'real_blue': real_blue,
'prv_index': prv_index,
'post_index': post_index,
'history_index': history_index
}
print(update_data)
db_blue_update(blue_date_id['five'], update_data, _id, 5)
if blue_date_id['three'] == old_history_date_id:
real_blue = all_data[-1]['blue']
forecast_blue_data = db_blue_get_data_by_date_id(blue_date_id['three'], 3)
for data in forecast_blue_data:
_id = data['id']
prv_index = general_blue_index(real_blue, json.loads(data['prv']))
post_index = general_blue_index(real_blue, json.loads(data['post']))
history_index = general_blue_index(real_blue, json.loads(data['history']))
update_data = {
'real_blue': real_blue,
'prv_index': prv_index,
'post_index': post_index,
'history_index': history_index
}
db_blue_update(blue_date_id['three'], update_data, _id, 3)
# 插入预测数据
def general_forecast_data():
pass
if __name__ == "__main__":
update_real_data()
general_forecast_data()

@ -56,14 +56,27 @@ def get_last_red_delta(_last_red):
return dict_sort(get_analysis_data(_last_red), 'val', True)
def get_random_index():
# 产生随机数
def get_index_from_db(_index_type):
index_list = db_index_get_index_group(_index_type)
return random.choice(index_list)
def get_random_index_group():
random_high, random_middle, random_low = -1, -1, -1
while random_high < 0 or random_middle < 0 or random_low < 0:
random_high = random.randint(0, 2)
random_low = random.randint(1, 6 - random_high)
random_middle = 6 - random_high - random_low
print('分布区间(高概率区,中概率区,低概率区):', random_high, random_middle, random_low)
return [random_high, random_middle, random_low]
def get_random_index(_mode='history', _index_type='history'):
# 产生随机数
if _mode == 'random':
random_high, random_middle, random_low = get_random_index_group()
else:
random_high, random_middle, random_low = get_index_from_db(_index_type)
print('分布区间(高概率区,中概率区,低概率区):', random_high, random_middle, random_low)
red_index_group = [int(random_high), int(random_middle), int(random_low)]
high_red_index_list = [(n[0] - 1) for n in new_num(1, 11, random_high)[0:random_high]]
middle_red_index_list = [(n[0] - 1) for n in new_num(12, 22, random_middle)[0:random_middle]]
@ -78,13 +91,8 @@ def get_random_index():
return red_index_group, red_index_list
def get_index_from_db():
index_list = db_red_get_index()
print(index_list)
def get_forecast_red(_red_data):
index_group, index_list = get_random_index()
def get_forecast_red(_red_data, _index_type):
index_group, index_list = get_random_index('history', _index_type)
forecast_red_list = []
_red_data_list = list(_red_data.items())
for i in index_list:
@ -95,10 +103,6 @@ def get_forecast_red(_red_data):
return {'forecast': forecast_red_list, 'group': index_group, 'index': index_list}
def get_forecast_red_by_db_index(_red_data, _mode='history'):
pass
def insert_red_data(_all_data, _history, _history_random, _last, _last_random):
print(_history_random)
print(_last_random)
@ -127,7 +131,7 @@ if __name__ == "__main__":
last_red_delta = get_last_red_delta(get_all_red_in_last(all_data))
# print(last_red_delta)
print_data([history_red_delta, last_red_delta])
forecast_red_history_data = get_forecast_red(history_red_delta)
forecast_red_last_data = get_forecast_red(last_red_delta)
forecast_red_history_data = get_forecast_red(history_red_delta, 'history')
forecast_red_last_data = get_forecast_red(last_red_delta, 'last')
insert_red_data(all_data, history_red_delta, forecast_red_history_data, last_red_delta, forecast_red_last_data)
get_index_from_db()

Loading…
Cancel
Save