#Python36
# coding:utf-8
# time:[2019/02/19]
# Python36
# coding:utf-8
# time:[2019/02/18]
# Python36
# coding:utf-8
# time:[2019/02/16]
# 年末2(12月29这一天需要取初始预测值这一天的90%)
# Python36
# coding:utf-8
# time:[2019/02/15]
# Python36
# coding:utf-8
# time:[2019/02/14]
import lightgbm as lgb
import pandas as pd

from dateutil import rrule
import pymssql
# import new_sun_funing as sf
import funing as sf
import datetime
import os
import numpy as np
from sklearn import preprocessing
import xgboost as xgb
import pickle
import time

# 返回当前日期
now_time = time.strftime("%Y-%m-%d")

def year_month_day(s):
    return s.strftime("%Y-%m-%d")

# 需要设置的参数
#  1.设置需要预测的截止的店铺id，比如，预测到372旧写372，预测到380就写380
pre_store_cd = 403
# 2. 取出预算数据时间时间设置，即取出该年该月1号前10天的数据，这儿的时间与pre_time_range要保持一致
beging_time = "201906"
# end_time = "201812"
# 预3.设置测时间区间设置，需要预测几个月就在下面list中写入相应格式的年月时间即可
pre_time_range = ["201906"]

# 四分位距倍数上限
IQR_MULTI_UP = 8
# 四分位距倍数下限
IQR_MULTI_LOW = 1.5
# 这些部门的数据有问题提
ABNORMAL_BUMON = [34, 43, 59, 74, 77, 97, 177, 180, 206]
# 一次平滑
# 特殊holiday日子不做处理
special_holiday = ["お盆_1日", "お盆_2日", "お盆_3日", "お盆_4日", "お盆_5日", "お盆_6日",
                   '創業祭_1日', '創業祭_2日', '創業祭_3日', '創業祭_4日', '創業祭_5日',
                   '年末_1', '年末_2', '年末_3', '年末_4', '年末_5',
                   "感謝祭_1日", "感謝祭_2日", "感謝祭_3日", "感謝祭_4日", "感謝祭_5日",
                   "ゴール1", "ゴール2"]
# 二次平滑

# 这些日子需要减
special_holiday_2 = ['年末_2', '創業祭_2日', '創業祭_3日', 'ゴール1', ]
# 这些日子需要加
special_holiday_3 = ['創業祭_1日', '創業祭_5日', ]
# 将不含有特殊日子2和3list中的其他日子取出
special_holiday_4 = ['年末_2', '創業祭_2日', '創業祭_3日', 'ゴール1', '創業祭_1日', '創業祭_5日']
# 三次平滑处理,初始预测值下调10%
special_holiday_5 = ["年末_2", "ゴール2"]
# 第四次平滑,"お盆_1日"这一天升高10%
special_holiday_6 = ["お盆_1日"]
# "お盆_3日"和"お盆_5日"升高5%
special_holiday_7 = ["お盆_3日", "お盆_5日","お盆_1日"]
# 'ゴール1'下调3%
special_holiday_8 = ["ゴール1","お盆_2日"]

def month_day(s):
    return s.strftime('%m%d')

def month(s):
    return s.strftime('%m')

def year_month(s):
    return s.strftime("%Y%m")

IBENTO_INF = 'holiday_inf.csv'
# 店铺情报
STORE_INF = 'store_inf.csv'
# 部门对应
BUMON_INF = 'bumon_inf.csv'
TRUE_PATH = os.getcwd() + os.sep

holiday_inf = pd.read_csv(TRUE_PATH + IBENTO_INF, sep=',', encoding="mbcs",engine = 'python')  # , encoding="Shift-JIS")  # 获取贩卖情况数据
holiday_inf['ds'] = pd.to_datetime(holiday_inf['ds'])
bumon_inf = pd.read_csv(TRUE_PATH + BUMON_INF, sep=',', encoding="mbcs",engine = 'python')
store_inf = pd.read_csv(TRUE_PATH + STORE_INF, sep=',', encoding="mbcs",engine = 'python')

# 获取店铺开店年限数据
store_time = store_inf  # [["オープン日"]]
store_time["オープン日"] = pd.to_datetime(store_time["オープン日"])
# store_time["now_time"] = now_time
store_time1 = store_time["オープン日"]
store_month_range = pd.DataFrame()
for time in store_time1:
    time = str(time)
    year_time = int(time[0:4])
    month_time = int(time[5:7])
    day_time = int(time[8:10])

    now_time = str(now_time)
    now_year_time = int(now_time[0:4])
    now_month_time = int(now_time[5:7])
    now_day_time = int(now_time[8:10])
    d1 = datetime.date(year_time, month_time, day_time)
    d2 = datetime.date(now_year_time, now_month_time, now_day_time)
    months = [[rrule.rrule(rrule.MONTHLY, dtstart=d1, until=d2).count()]]
    months = pd.DataFrame(months, columns=["month_range"])
    store_month_range = pd.concat([store_month_range, months], axis=0)
store_month_range = store_month_range.reset_index(drop=True)
new_store_inf = store_inf[["store_cd"]]
store_month_time = pd.concat([new_store_inf, store_month_range], axis=1)

# 增加地域：
new1_store_inf = store_inf
lb_store_region = preprocessing.LabelEncoder()
lb_store_region = lb_store_region.fit(list(new1_store_inf['フォーマット']) + ['0'])
new1_store_inf['フォーマット'] = lb_store_region.transform(list(new1_store_inf['フォーマット']))
store_region = new1_store_inf[["フォーマット"]]
store_region = pd.concat([new_store_inf, store_region], axis=1)
print(store_region)

new2_store_inf = store_inf
lb_store_yytime = preprocessing.LabelEncoder()
lb_store_yytime = lb_store_yytime.fit(list(new2_store_inf['営業時間']) + ['0'])
new2_store_inf['営業時間'] = lb_store_yytime.transform(list(new2_store_inf['営業時間']))
store_yytime = new1_store_inf[["営業時間"]]
store_yytime = pd.concat([new_store_inf, store_yytime], axis=1)

# 部门时候发生变化
changed_bumon = bumon_inf[bumon_inf['bumon_new'] != bumon_inf['bumon_old']].reset_index(drop=True)
# 获取祝日情报
red_day = sf.read_db_data("SELECT * FROM Python_Festival")
red_day.rename(columns={'日付': 'ds', '祝日': 'red_day'}, inplace=True)  # 将"日付"和"祝日"改为"ds"和"red_day"
red_day['ds'] = pd.to_datetime(red_day['ds'])

# 月别店别部门别预算数据取出
total_store_budget = pd.DataFrame()
for pre_time_range1 in pre_time_range:
    total_store_budget0 = sf.get_total_store_budget(pre_time_range1, pre_time_range1)
    total_store_budget = pd.concat([total_store_budget, total_store_budget0])

# 构造测试集
all_store_feature = pd.DataFrame()
# test_month_sales = pd.DataFrame()
for test_month in pre_time_range:

    test_month_yusuan = total_store_budget[total_store_budget["ds"] == int(test_month)]
    test_month_yusuan1 = test_month_yusuan[test_month_yusuan["store_cd"] <= pre_store_cd]
    test_month_yusuan2 = test_month_yusuan1[test_month_yusuan1["SALES_BUDGET_AMT"] > 0]
    store_bumon = test_month_yusuan2[["store_cd", "bumon_cd"]]
    store_bumon = store_bumon.drop_duplicates(subset=['store_cd', 'bumon_cd'], keep='first')
    # 构造预测会计月期间
    Period, Date_Range, begin_date, end_date = sf.get_pre_period(test_month)
    # 测试集验证销售数据抽出
    Date_Range_num = len(Date_Range)
    test_time_begin = Date_Range[0]
    test_time_end = Date_Range[Date_Range_num - 1]
    # test_month_inf = sf.read_db_data(
    # "select * from PYTHON_SALES_ACT_NEW where ds between '%s' and '%s' " % (test_time_begin, test_time_end))
    # 会计月销售数据合并
    # test_month_sales = pd.concat([test_month_sales,test_month_inf])
    # 构造店别部门别预测期间数据特战
    store_bumon["ds"] = ""
    xx = pd.DataFrame()
    for i in Date_Range:
        store_bumon["ds"] = i
        xx = pd.concat([xx, store_bumon])
    # 不同月的进行合并
    all_store_feature = pd.concat([all_store_feature, xx])

testx = sf.make_pre_data(all_store_feature, holiday_inf, red_day)
# 将开店年限特征追加进test
# testx = pd.merge(testx,store_month_time,how="left",on=["store_cd"])
# 将地域特战追加进test
testx = pd.merge(testx, store_region, how="left", on=["store_cd"])
# 将营业时间特征追加进test
testx = pd.merge(testx, store_yytime, how="left", on=["store_cd"])
testx_copy0 = testx
testx_copy = testx_copy0[
    ['ds', 'store_cd', 'bumon_cd', 'prior_scale', 'holiday', 'red_day', '年週_flg', '年月_flg', '曜日_flg', '月日_flg', '日_flg',
     'フォーマット', "営業時間"]]

z_lb_holiday = preprocessing.LabelEncoder()
z_lb_holiday = z_lb_holiday.fit(list(holiday_inf['holiday']) + ['0'])
z_lb_red_day = preprocessing.LabelEncoder()
z_lb_red_day = z_lb_red_day.fit(list(red_day['red_day']) + ['0'])
testx['holiday'] = z_lb_holiday.transform(list(testx['holiday']))
testx['red_day'] = z_lb_red_day.transform(list(testx['red_day']))
# 测试集特征与销售数据进行合并
# testx = pd.merge(testx,test_month_sales,how="left",left_on=["ds","store_cd","bumon_cd"],right_on=["ds","store_cd","bumon_cd"])
testx = testx[
    ['ds', 'store_cd', 'bumon_cd', 'prior_scale', 'holiday', 'red_day', '年週_flg', '年月_flg', '曜日_flg', '月日_flg', '日_flg',
     'フォーマット', "営業時間"]]

# 获取有需求预测的店铺以及部门

all_store_list = total_store_budget[total_store_budget["SALES_BUDGET_AMT"] > 0][
    "store_cd"].drop_duplicates().sort_values()
all_store_list = [x for x in all_store_list if x < pre_store_cd]
# 取出历史销售数据
get_history_time = str(beging_time[0:4]) + "/" + str(beging_time[4:]) + "/" + str(1)
get_history_time1 = datetime.datetime.strptime(get_history_time, '%Y/%m/%d')
begin_date0 = get_history_time1 + datetime.timedelta(days=-380)
end_date0 = get_history_time1 + datetime.timedelta(days=-20)

# 设置取历史数据时间段
begin_date = datetime.datetime.strftime(begin_date0, "%Y-%m-%d")
end_date = datetime.datetime.strftime(end_date0, "%Y-%m-%d")

train_data_2year1 = pd.DataFrame()

xxx_list = [iii for iii in range(4, pre_store_cd + 1)]
x_list1 = []
for j in range(0, len(xxx_list), 50):
    x_list1.append(xxx_list[j:j + 50])
for jj in x_list1:
    store_beg = min(jj)
    store_end = max(jj)
    print(store_beg, store_end)

    # x_list = [[4, 50], [51, 100], [101, 150], [151, 200], [201, 250], [251, 300], [301, 350], [351, pre_store_cd]]
    # train_data_2year1 = pd.DataFrame()
    # for x_store in x_list:
    # store_beg = x_store[0]
    # store_end = x_store[1]

    train_data_2year0 = sf.read_db_data(
        "select * from PYTHON_SALES_ACT_NEW where ds between '%s' and '%s' and store_cd between '%s' and '%s' " % (
            begin_date, end_date, store_beg, store_end))
    train_data_2year1 = pd.concat([train_data_2year1, train_data_2year0], axis=0)
train_data_2year1 = train_data_2year1.sort_values(by=["ds","store_cd"],ascending=True)
print(train_data_2year1)
#exit()
# 获取店铺基本数据
# 构造训练集、交叉验证集特征
# 训练集
train_data_copy_list = pd.DataFrame()
valid_data_copy_list = pd.DataFrame()
num = len(all_store_list)
ii = 1
train_data_list = pd.DataFrame()
# 交叉验证集
alid_data_list = pd.DataFrame()
for budget_cd in all_store_list:
    # 取出该店历史销售数据
    train_data_2year2 = train_data_2year1
    train_data_2year = train_data_2year1[train_data_2year2["store_cd"] == budget_cd]
    # 根据预算数据取出需要预算的部门，在历史数据中将该部门给清除
    total_store_budget_xx = total_store_budget
    store_total_store_budget = total_store_budget_xx[total_store_budget_xx["store_cd"] == budget_cd]
    store_total_store_budget1 = store_total_store_budget[store_total_store_budget["SALES_BUDGET_AMT"] > 0]
    store_bumon_bumon = list(store_total_store_budget1["bumon_cd"].drop_duplicates().sort_values())

    train_data_2year = train_data_2year[train_data_2year["bumon_cd"].isin(store_bumon_bumon)]

    train_data_2year["ds_1"] = train_data_2year["ds"].map(month).astype(int)

    train_data_2year['ds'] = pd.to_datetime(train_data_2year['ds'])
    ds_1 = train_data_2year.pop("ds_1")
    train_data_2year['sales'].fillna(100, inplace=True)  # inplace=为False填补缺失值，True为全部换为None(自行实验)，但这儿？
    # Open30天データ删除
    temp_date = min(train_data_2year['ds'])  # 获取数据集即中最早的时间，2016-10-13

    temp_date = temp_date + datetime.timedelta(days=30)  # 在temp_date往后推迟30天，即2016年11月12日，为什么要讲前30天的数据删除

    # train_data_2year = train_data_2year[train_data_2year['ds'] > temp_date]
    # 部門変更前2月対応20181210

    if changed_bumon.shape[0] > 0:  # 如果有部门变更，执行下面语句
        for i in range(changed_bumon.shape[0]):
            train_dt = train_data_2year[train_data_2year['bumon_cd'] == changed_bumon.iloc[i, 1]].reset_index(
                drop=True)

            train_dt['bumon_cd'] = changed_bumon.iloc[i, 0]
            train_data_2year = pd.concat([train_data_2year, train_dt], axis=0)

    train_data_2year['sales'] = train_data_2year['sales'].round(2).astype(int)

    train_data_2year['sales'] = train_data_2year['sales'].round(-2)

    where_are_nan = np.isnan(train_data_2year['sales'])

    where_are_inf = np.isinf(train_data_2year['sales'])  # np.isinf查找无穷值

    # ???????????  为什么要换为100，将这些样本
    train_data_2year.loc[where_are_nan, 'sales'] = 100  # ？   输出数据最下是100，
    train_data_2year.loc[where_are_inf, 'sales'] = 100
    train_data_2year.loc[train_data_2year['sales'] < 100, 'sales'] = 100

    # 训练数据（特征・异常值・空値・缺失值处理）
    # train_data_2year:相应商店及其相应部门2年的销售数据；ibento_inf：贩促数据；red_day：休息日；
    # ABNORMAL_BUMON“异常部门；ABNORMAL_BUMON：四分位倍数上限；IQR_MULTI_LOW：四分位倍数下限
    # 销售数据波动大的数据删除，依据标准差大小,bumon_cddd为符合标准差的部门

    # train_data_2year,bumon_fit = sf.get_std(train_data_2year)
    train_data, abnormal_data, test_data = sf.handle_abnormal_data(train_data_2year, holiday_inf, red_day,
                                                                   ABNORMAL_BUMON, IQR_MULTI_UP, IQR_MULTI_LOW,
                                                                   special_holiday)
    # 追加train、valid特征集中的开店时间和地域数据集和营业时间
    # train_data = pd.merge(train_data, store_month_time, how="left", on=["store_cd"])
    train_data = pd.merge(train_data, store_region, how="left", on=["store_cd"])
    train_data = pd.merge(train_data, store_yytime, how="left", on=["store_cd"])
    # test_data = pd.merge(test_data, store_month_time, how="left", on=["store_cd"])
    test_data = pd.merge(test_data, store_region, how="left", on=["store_cd"])
    test_data = pd.merge(test_data, store_yytime, how="left", on=["store_cd"])
    # 20181008单店原始数据深度备份
    train_data_copy = train_data.copy(deep=True)
    train_data_copy_list = pd.concat([train_data_copy_list, train_data_copy], axis=0)
    valid_data_copy = test_data.copy(deep=True)
    valid_data_copy_list = pd.concat([valid_data_copy_list, valid_data_copy], axis=0)
    # 单店训练对象做成
    # ？？？？  这一块的原因？
    # 将字符串换为数值型编码  ，空格用0替换
    lb_holiday = preprocessing.LabelEncoder()
    lb_holiday = lb_holiday.fit(list(holiday_inf['holiday']) + ['0'])
    lb_red_day = preprocessing.LabelEncoder()
    lb_red_day = lb_red_day.fit(list(red_day['red_day']) + ['0'])
    train_data = train_data[(train_data['store_cd'] == budget_cd)]
    test_data = test_data[(test_data['store_cd'] == budget_cd)]

    # 数据平滑处理log变换
    train_data['sales'] = np.log(train_data['sales'])  # 波动变平缓，即平滑处理
    where_are_nan = np.isnan(train_data['sales'])
    where_are_inf = np.isinf(train_data['sales'])
    train_data.loc[where_are_nan, 'sales'] = 4.605  # ln(100)  #为甚么要将这些改为4.605，不改成其他的
    train_data.loc[where_are_inf, 'sales'] = 4.605  # ln(100)
    train_data['sales'] = train_data['sales'].round(3)
    # 划分 测试训练数据集
    train_data['holiday'] = lb_holiday.transform(
        list(train_data['holiday']))  # 为什么要用lb_holiday.transform方法转换x中的holiday维
    train_data['red_day'] = lb_red_day.transform(list(train_data['red_day']))
    train_data = train_data  # .iloc[:,1:]

    train_data_list = pd.concat([train_data_list, train_data], axis=0)
    # 训练集、交叉验证集
    # 特征中重要度检验
    # 验证集
    # 数据平滑处理log变换
    test_data['sales'] = np.log(test_data['sales'])  # 波动变平缓，即平滑处理
    where_are_nan1 = np.isnan(test_data['sales'])
    where_are_inf1 = np.isinf(test_data['sales'])
    test_data.loc[where_are_nan1, 'sales'] = 4.605  # ln(100)  #为甚么要将这些改为4.605，不改成其他的
    test_data.loc[where_are_inf1, 'sales'] = 4.605  # ln(100)
    test_data['sales'] = test_data['sales'].round(3)
    test_data['holiday'] = lb_holiday.transform(list(test_data['holiday']))  # 为什么要用lb_holiday.transform方法转换x中的holiday维
    test_data['red_day'] = lb_red_day.transform(list(test_data['red_day']))
    test_data = test_data  # .iloc[:,1:]
    alid_data_list = pd.concat([alid_data_list, test_data], axis=0)

    print("训练集_验证集多部门特征构造完成:", str((ii / num) * 100)[0:5] + "%")
    ii = ii + 1

train_data_copy_list = pd.concat([train_data_copy_list, valid_data_copy_list], axis=0)
# 构造测试集
# with open("xg_train.pkl",'wb') as ff:
# pickle.dump(train_data_list,ff)
# train、valid、test 数据集

train = train_data_list
with open("test.pkl","wb") as f:
    pickle.dump(testx,f)
valid = alid_data_list
test = testx
all_bumon_list = test["bumon_cd"].drop_duplicates().sort_values()
all_store_bumon_pre_sales = pd.DataFrame()
for bumon in all_bumon_list:
    if  bumon >=0:
        print("部门开始:",bumon)
        bumon_train0 = train[train["bumon_cd"] == bumon]
        y_train = bumon_train0.pop("sales")
        bumon_train = bumon_train0.iloc[:, 1:]
        x_train = bumon_train
        bumon_valid0 = valid[valid["bumon_cd"] == bumon]
        y_valid = bumon_valid0.pop("sales")
        bumon_valid = bumon_valid0.iloc[:, 1:]
        x_valid = bumon_valid

        bumon_test00 = testx_copy[testx_copy["bumon_cd"] == bumon]

        bumon_test0 = test[test["bumon_cd"] == bumon]
        # y_test = bumon_test0.pop("sales")
        bumon_test = bumon_test0.iloc[:, 1:]

        x_test = bumon_test
        # 构建模型并训练模型
        # model = xgb.XGBRegressor(max_depth=6, learning_rate=0.06, n_estimators=600, silent=True,
        # objective='reg:linear')
        # model.fit(x_train, y_train)
        params = {'num_leaves': 150,
                  'objective': 'regression',
                  'max_depth': 6,
                  'learning_rate': .05,
                  'max_bin': 200,
                  'verbose': -1,
                  "seed": 2100}
        numRound = 20000
        print("训练集数量：",x_train.shape,y_train.shape)
        print("验证集数量:",x_valid.shape,y_valid.shape)

        dataTrain = lgb.Dataset(x_train, label=y_train)
        dataVali = lgb.Dataset(x_valid, label=y_valid, reference=dataTrain)
        # forest111 = ensemble.RandomForestRegressor(n_estimators=118,max_depth=20,random_state=118,n_jobs=3,oob_score =True)
        watchList = [dataTrain, dataVali]
        model = lgb.train(params, dataTrain, numRound, verbose_eval=True, valid_sets=watchList,
                          early_stopping_rounds=100)

        forecasty = np.expm1(model.predict(x_test))
        pre_sales = pd.DataFrame(np.round(forecasty), columns=["pre_sales"])

        # bumon_test00 = bumon_test00.reset_index(drop=True)
        # bumon_test00 = bumon_test00.as_matrix()
        # print("22:\n",bumon_test00)

        # bumon_test11 = pd.DataFrame(bumon_test00,columns=['ds', 'store_cd', 'bumon_cd', 'prior_scale', 'holiday',
        # 'red_day', '年週_flg', '年月_flg', '曜日_flg', '月日_flg', '日_flg','フォーマット','営業時間'])

        # bumon_test11["ds"] = pd.to_datetime(bumon_test11["ds"])
        bumon_test11 = bumon_test00.reset_index(drop=True)
        pre_result0 = pd.concat([bumon_test11, pre_sales], axis=1)
        print("pre_result0000:\n", pre_result0)

        # 将含有holiday中特殊日子的数据集取出
        sps_pre_result = pre_result0[pre_result0["holiday"].isin(special_holiday)]

        # 将train中该部门的数据取出
        sps_train_data0 = train_data_copy_list[train_data_copy_list["bumon_cd"] == bumon]
        sps_train_data = sps_train_data0[sps_train_data0["holiday"].isin(special_holiday)]
        sps_train_data = sps_train_data[["store_cd", "bumon_cd", "holiday", "sales"]]

        # 将holiday中特殊日子的数据进行合并
        sps_pre_result = pd.merge(sps_pre_result, sps_train_data, how="left", left_on=["store_cd", "bumon_cd", "holiday"],
                                  right_on=["store_cd", "bumon_cd", "holiday"])
        sps_where = np.isnan(sps_pre_result["sales"])
        sps_pre_result.loc[sps_where, ["sales"]] = 0

        # 取出sales为0的数据
        sps_pre_result0 = sps_pre_result[sps_pre_result["sales"].isin(['0'])]

        # 取出sales为非0的数据
        sps_pre_result1 = sps_pre_result[~sps_pre_result["sales"].isin(['0'])]

        sps_pre_result1["pre_sales"] = sps_pre_result1["pre_sales"] * 0.3 + sps_pre_result1["sales"] * 0.7

        sps_pre_result2 = pd.concat([sps_pre_result1, sps_pre_result0], axis=0)

        # 特殊日子二次处理
        # 需要降低的日子 special_holiday_2,不能降太多
        sps_pre_result3 = sps_pre_result2[sps_pre_result2["holiday"].isin(special_holiday_2)]
        sps_pre_result3_mean = np.mean(sps_pre_result3["sales"])
        sps_pre_result30 = sps_pre_result3[sps_pre_result3["sales"].isin(["0"])]
        sps_pre_result30["sales"] = sps_pre_result3_mean
        sps_pre_result31 = sps_pre_result3[~sps_pre_result3["sales"].isin(["0"])]
        sps_pre_result32 = pd.concat([sps_pre_result31, sps_pre_result30], axis=0)
        sps_pre_result32["pre_sales"] = sps_pre_result32["pre_sales"] * 0.6 + sps_pre_result32["sales"] * 0.4
        # 需要升高的日子 special_holiday_3，多升高一点
        sps_pre_result4 = sps_pre_result2[sps_pre_result2["holiday"].isin(special_holiday_3)]
        sps_pre_result4_mean = np.mean(sps_pre_result4["sales"])
        sps_pre_result40 = sps_pre_result4[sps_pre_result4["sales"].isin(["0"])]
        sps_pre_result40["sales"] = sps_pre_result4_mean
        sps_pre_result41 = sps_pre_result4[~sps_pre_result4["sales"].isin(["0"])]
        sps_pre_result42 = pd.concat([sps_pre_result41, sps_pre_result40], axis=0)
        sps_pre_result42["pre_sales"] = sps_pre_result42["pre_sales"] * 0.35 + sps_pre_result42["sales"] * 0.65
        # 取出不存在二次特殊日子的数据
        sps_pre_result5 = sps_pre_result2[~sps_pre_result2["holiday"].isin(special_holiday_4)]
        # 将三个数据集合并
        sps_pre_result6 = pd.concat([sps_pre_result32, sps_pre_result42, sps_pre_result5], axis=0)
        sps_pre_result7 = sps_pre_result6.reset_index(drop=True)

        # 进行第三次平滑
        # 将第三次平滑的数据取出
        sps_pre_result80 = sps_pre_result7[sps_pre_result7["holiday"].isin(special_holiday_5)]
        sps_pre_result80["pre_sales"] = sps_pre_result80["pre_sales"] * 0.9
        # 将不需进行第三次平滑的数据取出
        sps_pre_result81 = sps_pre_result7[~sps_pre_result7["holiday"].isin(special_holiday_5)]
        # 第三次平滑数据合并
        sps_pre_result9 = pd.concat([sps_pre_result81, sps_pre_result80], axis=0)
        sps_pre_result9 = sps_pre_result9.reset_index(drop=True)

        # 第四次平滑数据合并
        sps_pre_result100 = sps_pre_result9[sps_pre_result9["holiday"].isin(special_holiday_6)]
        sps_pre_result100["pre_sales"] = sps_pre_result100["pre_sales"] * 1.1
        # 将不需进行第三次平滑的数据取出
        sps_pre_result101 = sps_pre_result9[~sps_pre_result9["holiday"].isin(special_holiday_6)]
        # 第四次平滑数据合并
        sps_pre_result11 = pd.concat([sps_pre_result101, sps_pre_result100], axis=0)
        sps_pre_result11 = sps_pre_result11.reset_index(drop=True)

        # 第五次平滑数据合并
        sps_pre_result110 = sps_pre_result11[sps_pre_result11["holiday"].isin(special_holiday_7)]

        sps_pre_result110["pre_sales"] = sps_pre_result110["pre_sales"] * 1.05

        # 将不需进行第三次平滑的数据取出
        sps_pre_result111 = sps_pre_result11[~sps_pre_result11["holiday"].isin(special_holiday_7)]
        # 第三次平滑数据合并
        sps_pre_result12 = pd.concat([sps_pre_result111, sps_pre_result110], axis=0)
        sps_pre_result12 = sps_pre_result12.reset_index(drop=True)

        # 第六次平滑数据合并
        sps_pre_result120 = sps_pre_result12[sps_pre_result12["holiday"].isin(special_holiday_8)]

        sps_pre_result120["pre_sales"] = sps_pre_result120["pre_sales"] * 0.96

        # 将不需进行第三次平滑的数据取出
        sps_pre_result121 = sps_pre_result12[~sps_pre_result12["holiday"].isin(special_holiday_8)]
        # 第三次平滑数据合并
        sps_pre_result13 = pd.concat([sps_pre_result121, sps_pre_result120], axis=0)
        sps_pre_result13 = sps_pre_result13.reset_index(drop=True)

        sps_pre_result10 = sps_pre_result13[['ds', 'store_cd', 'bumon_cd', 'prior_scale', 'holiday', 'red_day',
                                             '年週_flg', '年月_flg', '曜日_flg', '月日_flg', '日_flg', 'フォーマット', '営業時間',
                                             'pre_sales']]

        # 没含有特殊日子的数据集
        pre_result = pre_result0[~pre_result0["holiday"].isin(special_holiday)]

        pre_result = pd.concat([pre_result, sps_pre_result10], axis=0)
        pre_result = pre_result.reset_index(drop=True)

        # pre_result.to_csv("E:\\改善日割预测\\test\\"""+str(bumon)+".csv")

        # 构成比计算
        # 预测为时间段
        # 先取出部门数据集中店铺预测数据
        all_test_pre_store_list = pre_result["store_cd"].drop_duplicates().sort_values()
        for test_store in all_test_pre_store_list:
            # print("store_Cd",test_store)
            store_pre_sales = pre_result[pre_result["store_cd"] == test_store]

            # 在店铺数据集中取出该店每月的预测数据集
            # 预测时间段
            pre_time_range = pre_time_range
            num_pre_time_range = len(pre_time_range)
            month_time_list = []
            for i in range(num_pre_time_range):
                # 配置会计月截止时间段，每月20
                month_pre_time = pre_time_range[i][0:4] + "-" + str(pre_time_range[i][4:]) + "-" + str(20)
                if i == 0:
                    month_pre_sales = store_pre_sales[store_pre_sales["ds"] <= month_pre_time]
                    month_time_list.append(month_pre_time)
                else:
                    month_pre_sales = store_pre_sales[
                        (store_pre_sales["ds"] <= month_pre_time) & (store_pre_sales["ds"] > month_time_list[i - 1])]
                    month_time_list.append(month_pre_time)
                month_shape = month_pre_sales.shape[0]
                if month_shape > 0:
                    month_pre_sales["pre_rate"] = month_pre_sales["pre_sales"] / np.sum(month_pre_sales["pre_sales"])

                    # 预测数据与预算数据合并
                    total_store_budget0 = total_store_budget
                    month_total_store_budget0 = total_store_budget0[
                        (total_store_budget0["bumon_cd"] == bumon) & (total_store_budget0["store_cd"] == test_store)]
                    # print("month_total_store_budget:\n",month_total_store_budget0)
                    month_total_store_budget1 = month_total_store_budget0[
                        month_total_store_budget0["ds"] == int(pre_time_range[i])]
                    # print("month_total_store_budget1\n",month_total_store_budget1)
                    # print("month_total_store_budget2\n",list(month_total_store_budget1["SALES_BUDGET_AMT"]))
                    month_pre_sales["SALES_BUDGET_AMT"] = list(month_total_store_budget1["SALES_BUDGET_AMT"])[0]
                    # 根据构成比分配目标金额
                    month_pre_sales["pre_rate_sales"] = month_pre_sales["pre_rate"] * month_pre_sales["SALES_BUDGET_AMT"]

                    # 会计100单位修正
                    month_begin_time = min(month_pre_sales["ds"])
                    month_end_time = max(month_pre_sales["ds"])
                    month_range_time = pd.date_range(start=month_begin_time, end=month_end_time)
                    month_range_time_list = []
                    for month_time in month_range_time:
                        month_time1 = datetime.datetime.strftime(month_time, "%Y-%m-%d")
                        month_range_time_list.append(month_time1)
                    # 进入会计100单位修正函数并返回修修正后的预测数据集
                    month_pre_sales = sf.pre_xiuzheng(month_pre_sales, test_store, month_range_time_list)
                    all_store_bumon_pre_sales = pd.concat([all_store_bumon_pre_sales, month_pre_sales])
        print("第" + str(bumon) + "部门完成")
# with open("xg_pre_jion_new_feature.pkl",'wb') as f:
# pickle.dump(all_store_bumon_pre_sales,f)

print("寄存店预测完毕")
print("开始预测新店......")
#新店预测
#计算新店寄存店之间的相似度
def get_ous_dis(new_store,all_store_inf,pre_store_cd):
    #将新店的特征取出
    new_store_inf = all_store_inf[all_store_inf["store_cd"] == new_store]
    new_store_inf0 = new_store_inf.as_matrix()[0]
    #将寄存店的数据取出，
    obs_store_inf = all_store_inf[all_store_inf["store_cd"] <= pre_store_cd]
    obs_store_inf0 = obs_store_inf.as_matrix()
    obs_store_inf0 = np.array(obs_store_inf0)
    obs_store_dis = []

    for obs_store in obs_store_inf0:

        #将店铺id取出
        obs_store_index = int(obs_store[0])
        #去除店铺id之外的特征
        obs_store_feature = obs_store[1:]
        #取出寄存店的特征
        new_store_feature = new_store_inf0[1:]

        #计算欧式距离
        ou_dis = np.sqrt(np.sum(np.square(obs_store_feature -  new_store_feature)))

        store_dis = ou_dis,obs_store_index,
        obs_store_dis.append(store_dis)
    obs_store_dis.sort(reverse=False)
    store_lsit = obs_store_dis[0:4]
    store_list1 = []
    for store in store_lsit:
        store0 = store[1]
        store_list1.append(store0)

    return store_list1

#读取店铺基本信息
STORE_INF = 'store_inf.csv'
store_inff = pd.read_table(TRUE_PATH + STORE_INF, sep=',', encoding="Shift-JIS")
#获取寄存店和新店的各自的list  id
store_id_cd = list(store_inff["store_cd"].drop_duplicates().sort_values())
new_store_id_list = [ y for y in store_id_cd if y > pre_store_cd]

#将所有新店的月别预算数据取出
total_store_budget = total_store_budget
#total_store_budget = pd.DataFrame()
#for pre_time_range1 in pre_time_range:
    #total_store_budget0 = sf.get_total_store_budget(pre_time_range1,pre_time_range1)
    #total_store_budget = pd.concat([total_store_budget,total_store_budget0])

#取店铺基本信息计算寄存店和新店新店之间的相似度
#取相关的信息以及编码化
all_store_inf = store_inff[['store_cd','立地法','レジ台数','地域','県名','到達圏別人口数5分','到達圏別人口数10分','到達圏別人口数15分','フォーマット','営業時間']]
#地域编码化
region_lb_all_store_inf = preprocessing.LabelEncoder()
region_lb_lb_all_store_inf = region_lb_all_store_inf.fit(list(store_inff['地域']) + ['0'])
all_store_inf['地域'] = region_lb_lb_all_store_inf.transform(list(all_store_inf['地域']))

#県名编码化
county_lb_all_store_inf = preprocessing.LabelEncoder()
county_lb_lb_all_store_inf = county_lb_all_store_inf.fit(list(store_inff['県名']) + ['0'])
all_store_inf['県名'] = county_lb_lb_all_store_inf.transform(list(all_store_inf['県名']))

#店铺类型编码化
type_lb_all_store_inf = preprocessing.LabelEncoder()
type_lb_lb_all_store_inf = type_lb_all_store_inf.fit(list(store_inff['フォーマット']) + ['0'])
all_store_inf['フォーマット'] = type_lb_lb_all_store_inf.transform(list(all_store_inf['フォーマット']))

#店铺营业时间编码化
yytime_lb_all_store_inf = preprocessing.LabelEncoder()
yytime_lb_lb_all_store_inf = yytime_lb_all_store_inf.fit(list(store_inff['営業時間']) + ['0'])
all_store_inf['営業時間'] = yytime_lb_lb_all_store_inf.transform(list(all_store_inf['営業時間']))

#读取新店与既存店相应的数据
new_used_store_inf = pd.read_csv(TRUE_PATH+'new_store_data.csv',encoding="Shift-JIS")

#将所有新店数据进行封装
all_new_store_pre_sales = pd.DataFrame()

for new_store in new_store_id_list:
    if new_store > pre_store_cd:
        # 返回相应新店与最相似寄存店之间的相似度
       # new_obs_store_list = get_ous_dis(new_store, all_store_inf, pre_store_cd)
        new_obs_store_list0 = new_used_store_inf[new_used_store_inf["store_cd"] == new_store][["ref_store1","ref_store2","ref_store3"]]
        new_obs_store_list = [ list(new_obs_store_list0["ref_store1"])[0],list(new_obs_store_list0["ref_store2"])[0],list(new_obs_store_list0["ref_store3"])[0]]
        for mongth in pre_time_range:
            #将该新店的最相似的3个既存店的数据取出
            data_lujin = TRUE_PATH + str(mongth) + os.sep
            month_pre_sales1 = pd.read_csv(data_lujin + '%s.csv' % str(new_obs_store_list[0]))
            month_pre_sales1 = month_pre_sales1.iloc[:,0:4]
            month_pre_sales1.columns = ["ds","store_cd","bumon_cd","pre_rate_sales1"]
            month_pre_sales1["store_cd"] =  new_store
            month_pre_sales2 = pd.read_csv(data_lujin + '%s.csv' % str(new_obs_store_list[1]))
            month_pre_sales2 = month_pre_sales2.iloc[:, 0:4]
            month_pre_sales2.columns = ["ds", "store_cd", "bumon_cd", "pre_rate_sales2"]
            month_pre_sales2["store_cd"] = new_store
            month_pre_sales3 = pd.read_csv(data_lujin + '%s.csv' % str(new_obs_store_list[2]))
            month_pre_sales3 = month_pre_sales3.iloc[:, 0:4]
            month_pre_sales3.columns = ["ds", "store_cd", "bumon_cd", "pre_rate_sales3"]
            month_pre_sales3["store_cd"] = new_store
            #将三个店铺的数据进行合并
            month_pre_sales1_2 = pd.merge(month_pre_sales1,month_pre_sales2,how="left",left_on=["ds","store_cd","bumon_cd"],right_on=["ds","store_cd","bumon_cd"])
            month_pre_sales1_2_3 = pd.merge(month_pre_sales1_2,month_pre_sales3,how="left",left_on=["ds","store_cd","bumon_cd"],right_on=["ds","store_cd","bumon_cd"])
            month_pre_sales1_2_3["mean"] = np.mean(month_pre_sales1_2_3[["pre_rate_sales1","pre_rate_sales2","pre_rate_sales3"]],axis=1)
            #使用指定列的值去填充相应列的nan值
            '''
            month_pre_sales1_2_3.loc[month_pre_sales1_2_3['pre_rate_sales1'].isnull(), 'pre_rate_sales1'] = month_pre_sales1_2_3[month_pre_sales1_2_3['pre_rate_sales1'].isnull()]['nan_mean']
            month_pre_sales1_2_3.loc[month_pre_sales1_2_3['pre_rate_sales2'].isnull(), 'pre_rate_sales2'] = month_pre_sales1_2_3[month_pre_sales1_2_3['pre_rate_sales2'].isnull()]['nan_mean']
            month_pre_sales1_2_3.loc[month_pre_sales1_2_3['pre_rate_sales3'].isnull(), 'pre_rate_sales3'] = month_pre_sales1_2_3[month_pre_sales1_2_3['pre_rate_sales3'].isnull()]['nan_mean']
            month_pre_sales1_2_3["mean"] = np.mean(month_pre_sales1_2_3[["pre_rate_sales1","pre_rate_sales2","pre_rate_sales3"]],axis=1)
            '''
            two_store_pre_sales =  month_pre_sales1_2_3[["ds", "store_cd", "bumon_cd", "mean"]]
            two_store_pre_sales.columns = ["ds", "store_cd", "bumon_cd", "pre_rate_sales_mean"]
            # 将该店的预算数据取出
            new_store_total = total_store_budget[
                (total_store_budget["store_cd"] == new_store) & (total_store_budget["ds"] == int(mongth))]
            new_store_total = new_store_total[new_store_total["SALES_BUDGET_AMT"] > 0]
            new_store_num = len(new_store_total)
            if new_store_num > 0:
                # 根据预算数据集构造特征
                # 构造预测月的时间
                new_pre_time_range = list(two_store_pre_sales["ds"].drop_duplicates().sort_values())

                new_store_bumon_feature = new_store_total[["store_cd", "bumon_cd", "SALES_BUDGET_AMT"]]

                new_feature = pd.DataFrame()
                for new_pre_time in new_pre_time_range:
                    new_prt = new_store_bumon_feature
                    new_prt["ds"] = new_pre_time
                    new_feature = pd.concat([new_feature, new_prt])
                new_feature = new_feature.reset_index(drop=True)
                new_feature = new_feature[["ds", "store_cd", "bumon_cd", "SALES_BUDGET_AMT"]]

                # 将新店特征集与相似店的预测数据集合并
                new_pre_sales = pd.merge(new_feature, two_store_pre_sales, how="left",
                                         left_on=["ds", "store_cd", "bumon_cd"],
                                         right_on=["ds", "store_cd", "bumon_cd"])
                # 求合并后数据集预测值的均值
                new_pre_sales_mean = np.mean(new_pre_sales["pre_rate_sales_mean"])
                # 查找nan之并用均值替换
                where_nan = np.isnan(new_pre_sales["pre_rate_sales_mean"])
                new_pre_sales.loc[where_nan, ["pre_rate_sales_mean"]] = new_pre_sales_mean
                new_pre_sales.rename(columns={"pre_rate_sales_mean": "pre_sales"}, inplace=True)
                new_bumon_list = new_pre_sales["bumon_cd"].drop_duplicates().sort_values()
                month_new_store_pre_sales = pd.DataFrame()

                for new_bumon in new_bumon_list:
                    new_bumon_pre_sales = new_pre_sales[new_pre_sales["bumon_cd"] == new_bumon]
                    new_bumon_pre_sales["pre_rate"] = new_bumon_pre_sales["pre_sales"] / np.sum(
                        new_bumon_pre_sales["pre_sales"])
                    new_bumon_pre_sales["pre_rate_sales"] = new_bumon_pre_sales["pre_rate"] * new_bumon_pre_sales[
                        "SALES_BUDGET_AMT"]
                    new_store_cd = list(new_bumon_pre_sales["store_cd"].drop_duplicates().sort_values())[0]
                    # 会计100单位修正
                    new_store_cd = sf.pre_xiuzheng(new_bumon_pre_sales, new_store_cd, new_pre_time_range)
                    month_new_store_pre_sales = pd.concat([month_new_store_pre_sales, new_store_cd], axis=0)
                month_new_store_pre_sales = month_new_store_pre_sales[
                    ["ds", "store_cd", "bumon_cd", "pre_rate_sales", "SALES_BUDGET_AMT"]]
                month_new_store_pre_sales.to_csv(TRUE_PATH + str(mongth) + os.sep + '%s.csv' % str(new_store),
                                                 index=False, header=True)
                all_new_store_pre_sales = pd.concat([all_new_store_pre_sales,month_new_store_pre_sales],axis=0)
        print("第" + str(new_store) + "店铺完成")
all_store_bumon_pre_sales = all_store_bumon_pre_sales
all_store_bumon_pre_sales = all_store_bumon_pre_sales.reset_index(drop=True)
all_new_store_pre_sales =  all_new_store_pre_sales.reset_index(drop=True)
all_new_used_store_pre_sales = pd.concat([all_store_bumon_pre_sales, all_new_store_pre_sales],axis=0)
all_new_used_store_pre_sales = all_new_used_store_pre_sales.reset_index(drop=True)
#将本次所有预测数据写进excel中
# 创建文件路径
for pre_month in pre_time_range:
    if not os.path.exists(r'./' + pre_month):  # 文件是否存在
        os.makedirs(r'./' + pre_month)  # 如果不存在该文件，就创建一个该文件
        # print(' Folder Set Up Done')
    result_path = TRUE_PATH + pre_month + os.sep
# 预测总数据集分店铺
iii = 0
time_range = []
for time in pre_time_range:
    time1 = time[0:4] + "-" + str(time[4:]) + "-" + str(20)
    if iii == 0:
        data = all_new_used_store_pre_sales[all_new_used_store_pre_sales["ds"] <= str(time1)]
        time_range.append(time1)
    else:
        data = all_new_used_store_pre_sales[(all_new_used_store_pre_sales["ds"] <= str(time1)) & (
            all_new_used_store_pre_sales["ds"] > str(time_range[iii - 1]))]
        time_range.append(time1)
    data = data[["ds", "store_cd", "bumon_cd", "pre_rate_sales", "SALES_BUDGET_AMT"]]
    data.to_csv(TRUE_PATH + os.sep + '%s.csv' % str(time), index=False, header=True)
    iii = iii + 1
    test_pre_store_list = data["store_cd"].drop_duplicates().sort_values()
    for store_id in test_pre_store_list:
        print("time:", time)
        store_in_sales = data[data["store_cd"] == store_id]
        store_in_sales.to_csv(TRUE_PATH + str(time) + os.sep + '%s.csv' % str(store_id), index=False, header=True)
print("本次预测全部完成")


import logging
# logging.basicConfig(level=logging.INFO, datefmt='%Y/%m/%d %H:%M',format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# 创建日志器,每一个日志器都是唯一的
logger1 = logging.getLogger(__name__) # __name__代表的是__main__
logger2 = logging.getLogger('logger2')
logger3 = logging.getLogger('logger3')

# 创建记录
logger1.info('This is a log info',exc_info=True)
logger2.warning('Warning exists',)
logger3.error('%s - error %s ',stack_info=True,extra={'user':'tom','ip':'47.98.53.222'})


import os

import torch
from torch.nn import DataParallel
from torch.optim.lr_scheduler import LambdaLR

from torch.utils.data import DataLoader
from transformers import AdamW

from data_utils.FakedditDatasetWithComment import FakedditDatasetWithComment
from model.PureBert import PureBert
from data_utils.FakedditDataset import FakedditDataset
from trainer import Train
from model.util import load_parallel_save_model

from sklearn.metrics import *

import logging

# log format
C_LogFormat = '%(asctime)s - %(levelname)s - %(message)s'
# setting log format
logging.basicConfig(level=logging.INFO, format=C_LogFormat)

# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
# os.environ['CUDA_VISIBLE_DEVICES'] = '0,1,2,3'
# os.environ['CUDA_VISIBLE_DEVICES'] = '0,1,2,3,4,5,6,7'

BERT_PATH = '/home/tanghengzhu/yjs/model/bert-base-uncased_L-24_H-1024_A-16'

BATCH_SIZE_PER_GPU = 7
GPU_COUNT = torch.cuda.device_count()

def get_linear_schedule_with_warmup(optimizer, num_warmup_steps, num_training_steps, last_epoch=-1):
    def lr_lambda(current_step):
        if current_step < num_warmup_steps:
            return float(current_step) / float(max(1, num_warmup_steps))
        return max(0.0, float(num_training_steps - current_step) / float(max(1, num_training_steps - num_warmup_steps)))

    return LambdaLR(optimizer, lr_lambda, last_epoch)

if __name__ == '__main__':
    train_dataset = FakedditDatasetWithComment('/home/tanghengzhu/yjs/data/Fakeddit/fakeddit_v1.0/',
                                    bert_path=BERT_PATH,
                                    max_sequence_length=510,
                                    num_class=2)
    test_dataset = FakedditDatasetWithComment('/home/tanghengzhu/yjs/data/Fakeddit/fakeddit_v1.0/', isTest=True,
                                   bert_path=BERT_PATH,
                                   max_sequence_length=510,
                                   num_class=2)
    val_dataset = FakedditDatasetWithComment('/home/tanghengzhu/yjs/data/Fakeddit/fakeddit_v1.0/', isVal=True,
                                  bert_path=BERT_PATH,
                                  max_sequence_length=510,
                                  num_class=2)

    train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE_PER_GPU * GPU_COUNT, shuffle=True)
    test_loader = DataLoader(test_dataset, batch_size=int(BATCH_SIZE_PER_GPU * GPU_COUNT * 15), shuffle=True)
    val_loader = DataLoader(val_dataset, batch_size=int(BATCH_SIZE_PER_GPU * GPU_COUNT * 15), shuffle=True)

    logging.info(f"train data all steps: {len(train_loader)}, "
                 f"validate data all steps : {len(val_loader)},"
                 f"test data all steps : {len(test_loader)}")

    model = PureBert(bert_path=BERT_PATH)

    model = DataParallel(model)

    model = model.cuda()

    # Prepare  optimizer and schedule(linear warmup and decay)
    no_decay = ['bias', 'LayerNorm.weight']
    optimizer_grouped_parameters = [
        {'params': [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay)],
         'weight_decay': 0.0},
        {'params': [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
    ]

    optimizer = AdamW(optimizer_grouped_parameters, lr=2e-5, eps=1e-8)
    scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=0, num_training_steps=len(train_loader))
    crit = torch.nn.CrossEntropyLoss()

    trainer = Train(model_name='pure-bert-with-comment',
                    train_loader=train_loader,
                    val_loader=val_loader,
                    test_loader=test_loader,
                    model=model,
                    optimizer=optimizer,
                    loss_fn=crit,
                    epochs=5,
                    print_step=10,
                    early_stop_patience=2,
                    save_model_path='./save_model/pure-bert-with-comment',
                    save_model_every_epoch=True,
                    metric=accuracy_score,
                    num_class=2,
                    tensorboard_path='./tensorboard_log')

    trainer.train()
    print(f"Testing result :{trainer.test()}")


from threading import currentThread
from weakref import WeakKeyDictionary

from cloudshell.shell.core import driver_context

_CONTEXT_CONTAINER = WeakKeyDictionary()

def get_context():
    if currentThread() in _CONTEXT_CONTAINER:
        return _CONTEXT_CONTAINER[currentThread()]
    return None

def get_attribute_by_name_wrapper(attribute):
    """Wrapper uses to closure get_attribute_by_name func."""  # noqa: D202

    def attribute_func():
        return get_attribute_by_name(attribute)

    return attribute_func

def build_suitable_context(context_obj):
    module = driver_context
    context_class = context_obj.__class__.__name__
    if context_class in dir(module):
        classobject = getattr(module, context_class)
    else:
        raise Exception("build_suitable_context", "Cannot find suitable context class")
    obj = classobject()
    for attribute in filter(
        lambda x: not str(x).startswith("__") and not x == "ATTRIBUTE_MAP",
        dir(context_obj),
    ):
        value = getattr(context_obj, attribute)
        if (
            value
            and hasattr(value, "__class__")
            and value.__class__.__name__ in dir(module)
        ):
            value = build_suitable_context(value)

        if attribute in obj.ATTRIBUTE_MAP:
            obj_attr = obj.ATTRIBUTE_MAP[attribute]
        else:
            obj_attr = attribute

        setattr(obj, obj_attr, value)
    return obj

def put_context(context):
    if context:
        _CONTEXT_CONTAINER[currentThread()] = context
    else:
        raise Exception("put_context", "Context is None")

def get_resource_context_details(context=None):
    """Helps to get resource context details."""
    if not context:
        _CONTEXT_CONTAINER.get(currentThread(), None)
    if context and hasattr(context, "resource"):
        return context.resource
    else:
        raise Exception(
            "get_resource_context_details", "Context does not have resource attribute"
        )

def is_instance_of(context, type_name):
    context_type = context.__class__.__name__
    return context_type == type_name

def get_resource_context_attribute(attribute, context=None):
    """Helps to get resource context attribute."""
    if not context:
        _CONTEXT_CONTAINER.get(currentThread(), None)
    resource = get_resource_context_details(context)
    if resource and hasattr(resource, attribute):
        return getattr(resource, attribute)
    else:
        raise Exception(
            "get_resource_context_attribute",
            "Resource context does not have attribute {0}".format(attribute),
        )

def get_attribute_by_name(attribute_name, context=None):
    """Return attribute from attributes or resource context."""
    attributes = get_resource_context_attribute(context=context, attribute="attributes")
    resolved_attribute = None
    if attribute_name in attributes:
        resolved_attribute = attributes[attribute_name]
    return resolved_attribute

def get_resource_address(context=None):
    """Returns resource address."""
    return get_resource_context_attribute(context=context, attribute="address")

def get_resource_name(context=None):
    """Returns resource name."""
    return get_resource_context_attribute(context=context, attribute="name")

def get_reservation_context_details(context=None):
    """Helps to get reservation context details."""
    if not context:
        _CONTEXT_CONTAINER.get(currentThread(), None)
    if context and hasattr(context, "reservation"):
        reservation = context.reservation
    elif context and hasattr(context, "remote_reservation"):
        reservation = context.remote_reservation
    else:
        raise Exception(
            "get_reservation_context_details",
            "Context does not have reservation or remote_reservation attribute",
        )
    return reservation

def get_reservation_context_attribute(attribute, context=None):
    """Helps to get reservation context attribute."""
    reservation = get_reservation_context_details(context)
    if reservation and hasattr(reservation, attribute):
        return getattr(reservation, attribute)
    else:
        raise Exception(
            "get_reservation_context_attribute",
            "Reservation context does not have attribute {0}".format(attribute),
        )

def get_connectivity_context_details(context=None):
    """Helps to get connectivity context details."""
    if not context:
        _CONTEXT_CONTAINER.get(currentThread(), None)
    if context and hasattr(context, "connectivity"):
        return context.connectivity
    else:
        raise Exception(
            "get_connectivity_context_details",
            "Context does not have connectivity attribute",
        )

def get_connectivity_context_attribute(attribute, context=None):
    """Helps to get connectivity context attribute."""
    connectivity = get_connectivity_context_details(context)
    if connectivity and hasattr(connectivity, attribute):
        return getattr(connectivity, attribute)
    else:
        raise Exception(
            "get_connectivity_context_attribute",
            "Connectivity context does not have attribute {0}".format(attribute),
        )


##################################################
## Final Project - Implementing a Simple Router ##
##################################################
## Ashwin Chidambaram                           ##
## 03/06/2020                                   ##
##################################################
## final.py ######################################
##################################################

#!/usr/bin/python

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel
from mininet.cli import CLI
from mininet.node import RemoteController

class final_topo(Topo):
    def build(self):

        #### Set up Hosts ######################################################
        h1 = self.addHost('h1',mac='00:00:00:00:00:01',ip='10.0.1.101/24',defaultRoute="h1-eth0")               ## Host 1
        h2 = self.addHost('h2',mac='00:00:00:00:00:02',ip='10.0.2.102/24',defaultRoute="h2-eth0")               ## Host 2
        h3 = self.addHost('h3',mac='00:00:00:00:00:03',ip='10.0.3.103/24',defaultRoute="h3-eth0")               ## Host 3
        h4 = self.addHost('h4',mac='00:00:00:00:00:04',ip='128.114.50.10/24',defaultRoute="h4-eth0")            ## Untrusted Host
        h5 = self.addHost('h5',mac='00:00:00:00:00:04',ip='10.0.4.104/24',defaultRoute="h5-eth0")               ## Server

        #### Set up Switches ###################################################
        s1 = self.addSwitch('s1')                                                                               ## Adds Floor 1 Switch
        s2 = self.addSwitch('s2')                                                                               ## Adds Floor 2 Switch
        s3 = self.addSwitch('s3')                                                                               ## Adds Floor 3 Switch
        s4 = self.addSwitch('s4')                                                                               ## Adds Core Switch
        s5 = self.addSwitch('s5')                                                                               ## Adds Data Center Switch

        #### Link Switches & Hosts #############################################

        # Link the Hosts to their relevent switches
        self.addLink(s1, h1, port1=8, port2=0)                                                                  ## Link Switch 1 & Host 1   -   [Floor Switch 1 <--> Host 1]
        self.addLink(s2, h2, port1=8, port2=0)                                                                  ## Link Switch 2 & Host 2   -   [Floor Switch 2 <--> Host 2]
        self.addLink(s3, h3, port1=8, port2=0)                                                                  ## Link Switch 3 & Host 3   -   [Floor Switch 3 <--> Host 3]
        self.addLink(s4, h4, port1=8, port2=0)                                                                  ## Link Switch 4 & Host 4   -   [Core Switch <--> Untrusted Host]
        self.addLink(s5, h5, port1=8, port2=0)                                                                  ## Link Switch 4 & Host 4   -   [Data Center Switch <--> Server]

        # Link to Core Switch
        self.addLink(s1, s4, port1=3, port2=1)                                                                  ## Link Switch 1 & Switch 4 -   [Floor Switch 1 <--> Core Switch]
        self.addLink(s2, s4, port1=3, port2=2)                                                                  ## Link Switch 2 & Switch 4 -   [Floor Switch 2 <--> Core Switch]
        self.addLink(s3, s4, port1=3, port2=3)                                                                  ## Link Switch 3 & Switch 4 -   [Floor Switch 3 <--> Core Switch]
        self.addLink(s5, s4, port1=3, port2=4)                                                                  ## Link Switch 5 & Switch 4 -   [Data Center Switch <--> Core Switch]

def configure():
    topo = final_topo()
    net = Mininet(topo=topo, controller=RemoteController)
    net.start()

    CLI(net)

    net.stop()

if __name__ == '__main__':
    configure()


import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):							#example class derived from the base class QWidge, so 2 construtors
	def __init__(self):							#constructor method for the example class
		super(Example,self).__init__()					#super method provides parental control to  Example class
		self.initUI()							#object to the function

	def initUI(self):							#function
		'''btn = QtGui.QPushButton('Quit',self)
		btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
		btn.resize(btn.sizeHint())
		btn.move(50,50)'''
		self.setGeometry(300,300,500,700)				#combines move nad resize
		self.setWindowTitle('Attendance Auto-logging System')		#name
		self.setWindowIcon(QtGui.QIcon('index.jpeg'))			#add image as an icon
		self.show()							#display

def main():									#main function
	app = QtGui.QApplication(sys.argv)					#creating application object
	ex = Example()								#call to the class example
	sys.exit(app.exec_())							#clean exit

if __name__ == '__main__':
	main()


from tictactoe.test_base import BaseTestCase

import unittest
from flask import json, url_for

class IndexTestCase(BaseTestCase):

    def setUp(self):
        BaseTestCase.setUp(self)
        self.data = dict(firstPlayer='dan')
        self.app.post(url_for("index"),
                      data=json.dumps(self.data), content_type="application/json")

    def test_get_on_index(self):
        response = self.app.get(url_for('index'))
        self.assertEqual(response.status_code, 200)

    def test_posts_to_index_with_empty_json(self):
        response = self.app.post(url_for('index'),
                data=json.dumps(dict()), content_type='application/json')
        self.assertEqual(response.status_code, 200)

    def test_posts_to_index_with_all_fields(self):
        data = dict(firstPlayer='alice', secondPlayer='bob', board='_,1,0,_,_,0,_,_,_')
        response = self.app.post(url_for('index'),
                data=json.dumps(data), content_type='application/json')
        self.assertEqual(response.status_code, 200)

    def test_index_multiple_posts_get(self):
        first = dict(firstPlayer='alice')
        self.app.post(url_for('index'), data=json.dumps(first), content_type='application/json')
        second = dict(firstPlayer='bob')
        self.app.post(url_for('index'), data=json.dumps(second), content_type='application/json')
        third = dict(firstPlayer='charlize')
        self.app.post(url_for('index'), data=json.dumps(third), content_type='application/json')
        response = self.app.get(url_for('index'))
        response_data = json.loads(response.data.decode('utf-8'))
        self.assertEqual(response_data[0]['firstPlayer'], 'dan') # from setup
        self.assertEqual(response_data[1]['firstPlayer'], first['firstPlayer'])
        self.assertEqual(response_data[2]['firstPlayer'], second['firstPlayer'])
        self.assertEqual(response_data[3]['firstPlayer'], third['firstPlayer'])

    def test_game_allows_get(self):
        response = self.app.get(url_for("game", game_id=1))
        self.assertEqual(response.status_code, 200)

    def test_game_404_on_unfound_id(self):
        response = self.app.get(url_for("game", game_id=9))
        self.assertEqual(response.status_code, 404)

    def test_game_allows_post(self):
        first = dict(firstPlayer='alice')
        self.app.post(url_for("game", game_id=1), data=json.dumps(first), content_type='application/json')
        response = self.app.get(url_for("game", game_id=1))
        response_data = json.loads(response.data.decode('utf-8'))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response_data['firstPlayer'], first['firstPlayer'])

    def test_game_allows_delete(self):
        response = self.app.delete(url_for('game',game_id=1)) # id 1 is in setup
        self.assertEqual(response.status_code, 200)

if __name__ == '__main__':
    unittest.main()


import tensorflow as tf
import numpy as np

def AlexNet_Model(X,y,is_training):
    """
    build a convolutional network with the architecture of AlexNet.
    The architecture is
    conv1->max pool1->norm1->conv2->max pool2->norm2->conv3->conv4->conv5->max pool3
    ->fc6->fc7->fc8
    :param X:cifar-10-data
    :param y:label names
    :param is_training:
    :return:10 classes scores for N input classes
    """
    #initialize the parameters
    conv_W1=tf.get_variable('conv_W1',shape=[4,4,3,48])     #we use 96 filters at the conv1 layer
    conv_b1=tf.get_variable('conv_b1',shape=[48])
    conv_W2=tf.get_variable('conv_W2',shape=[4,4,48,128])   #we use 256 filters at the conv2 layer
    conv_b2=tf.get_variable('conv_b2',shape=[128])
    conv_W3=tf.get_variable('conv_W3',shape=[3,3,128,128])  #we use 384 filters at the conv3 layer
    conv_b3=tf.get_variable('conv_b3',shape=[128])
    conv_W4=tf.get_variable('conv_W4',shape=[3,3,128,192])  #we use 384 filters at the conv4 layer
    conv_b4=tf.get_variable('conv_b4',shape=[192])
    conv_W5=tf.get_variable('conv_W5',shape=[3,3,192,128])  #we use 384 filters at the conv5 layer
    conv_b5=tf.get_variable('conv_b5',shape=[128])
    fc_W1=tf.get_variable('fc_W1',shape=[6272,3136])
    fc_b1=tf.get_variable('fc_b1',shape=[3136])
    fc_W2=tf.get_variable('fc_W2',shape=[3136,630])
    fc_b2=tf.get_variable('fc_b2',shape=[630])
    fc_W3=tf.get_variable('fc_W3',shape=[630,10])
    fc_b3=tf.get_variable('fc_b3',shape=[10])

    #create the layer model
    conv1=tf.nn.conv2d(X,conv_W1,strides=[1,1,1,1],padding='VALID')+conv_b1
    relu1=tf.nn.relu(conv1)
    bn1=tf.layers.batch_normalization(relu1,training=is_training)

    conv2=tf.nn.conv2d(bn1,conv_W2,strides=[1,1,1,1],padding='VALID')+conv_b2
    relu2=tf.nn.relu(conv2)
    pool1=tf.nn.max_pool(relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='VALID')
    bn2=tf.layers.batch_normalization(pool1,training=is_training)

    conv3=tf.nn.conv2d(bn2,conv_W3,strides=[1,1,1,1],padding='VALID')+conv_b3
    relu3=tf.nn.relu(conv3)
    conv4=tf.nn.conv2d(relu3,conv_W4,strides=[1,1,1,1],padding='VALID')+conv_b4
    relu4=tf.nn.relu(conv4)
    conv5=tf.nn.conv2d(relu4,conv_W5,strides=[1,1,1,1],padding='VALID')+conv_b5
    relu5=tf.nn.relu(conv5)
    conv5_new=tf.reshape(relu5,[-1,6272])

    affine1=tf.matmul(conv5_new,fc_W1)+fc_b1
    relu6=tf.nn.relu(affine1)
    affine2=tf.matmul(relu6,fc_W2)+fc_b2
    relu7=tf.nn.relu(affine2)
    out=tf.matmul(relu7,fc_W3)+fc_b3
    return out


# Turtle - Circle Demo
# Written by: Jeff Brusoe
# Last Updated: February 4, 2019

import turtle

x_start = 0
y_start = 0
circle_radius = 100

turtle.showturtle()

turtle.penup()
turtle.goto(x_start,y_start)
turtle.pendown()

turtle.circle(circle_radius)


import time
import unittest

from nokia import NokiaMeasureGroup

class TestNokiaMeasureGroup(unittest.TestCase):
    def test_attributes(self):
        """
        Check that attributes get set as expected when creating a
        NokiaMeasureGroup object
        """
        data = {
            'attrib': 2,
            'measures': [
                {'unit': -1, 'type': 1, 'value': 860}
            ],
            'date': 1409361740,
            'category': 1,
            'grpid': 111111111
        }
        group = NokiaMeasureGroup(data)
        self.assertEqual(group.data, data)
        self.assertEqual(group.grpid, data['grpid'])
        self.assertEqual(group.attrib, data['attrib'])
        self.assertEqual(group.category, data['category'])
        self.assertEqual(group.date.timestamp, 1409361740)
        self.assertEqual(group.measures, data['measures'])
        for _type, type_id in NokiaMeasureGroup.MEASURE_TYPES:
            assert hasattr(group, _type)
            self.assertEqual(getattr(group, _type),
                             86.0 if _type == 'weight' else None)

    def test_types(self):
        """
        Check that all the different measure types are working as expected
        """
        for _, type_id in NokiaMeasureGroup.MEASURE_TYPES:
            data = {
                'attrib': 2,
                'measures': [
                    {'unit': -1, 'type': type_id, 'value': 860}
                ],
                'date': 1409361740,
                'category': 1,
                'grpid': 111111111
            }
            group = NokiaMeasureGroup(data)
            for _type, type_id2 in NokiaMeasureGroup.MEASURE_TYPES:
                assert hasattr(group, _type)
                self.assertEqual(getattr(group, _type),
                                 86.0 if type_id == type_id2 else None)

    def test_multigroup_types(self):
        """
        Check that measure typse with multiple measurements in the group are
        working as expected
        """
        data = {
            'attrib': 2,
            'measures': [
                {'unit': -1, 'type': 9, 'value': 800},
                {'unit': -1, 'type': 10, 'value': 1200},
                {'unit': -1, 'type': 11, 'value': 860}
            ],
            'date': 1409361740,
            'category': 1,
            'grpid': 111111111
        }
        group = NokiaMeasureGroup(data)
        for _type, type_id in NokiaMeasureGroup.MEASURE_TYPES:
            assert hasattr(group, _type)
            if _type == 'diastolic_blood_pressure':
                self.assertEqual(getattr(group, _type), 80.0)
            elif _type == 'systolic_blood_pressure':
                self.assertEqual(getattr(group, _type), 120.0)
            elif _type == 'heart_pulse':
                self.assertEqual(getattr(group, _type), 86.0)
            else:
                self.assertEqual(getattr(group, _type), None)

    def test_is_ambiguous(self):
        """ Test the is_ambiguous method """
        data = {'attrib': 0, 'measures': [], 'date': 1409361740, 'category': 1,
                'grpid': 111111111}
        self.assertEqual(NokiaMeasureGroup(data).is_ambiguous(), False)
        data['attrib'] = 1
        assert NokiaMeasureGroup(data).is_ambiguous()
        data['attrib'] = 2
        self.assertEqual(NokiaMeasureGroup(data).is_ambiguous(), False)
        data['attrib'] = 4
        assert NokiaMeasureGroup(data).is_ambiguous()

    def test_is_measure(self):
        """ Test the is_measure method """
        data = {'attrib': 0, 'measures': [], 'date': 1409361740, 'category': 1,
                'grpid': 111111111}
        assert NokiaMeasureGroup(data).is_measure()
        data['category'] = 2
        self.assertEqual(NokiaMeasureGroup(data).is_measure(), False)

    def test_is_target(self):
        """ Test the is_target method """
        data = {'attrib': 0, 'measures': [], 'date': 1409361740, 'category': 1,
                'grpid': 111111111}
        self.assertEqual(NokiaMeasureGroup(data).is_target(), False)
        data['category'] = 2
        assert NokiaMeasureGroup(data).is_target()

    def test_get_measure(self):
        """
        Check that the get_measure function is working as expected
        """
        data = {
            'attrib': 2,
            'measures': [
                {'unit': -2, 'type': 9, 'value': 8000},
                {'unit': 1, 'type': 10, 'value': 12},
                {'unit': 0, 'type': 11, 'value': 86}
            ],
            'date': 1409361740,
            'category': 1,
            'grpid': 111111111
        }
        group = NokiaMeasureGroup(data)
        self.assertEqual(group.get_measure(9), 80.0)
        self.assertEqual(group.get_measure(10), 120.0)
        self.assertEqual(group.get_measure(11), 86.0)
        self.assertEqual(group.get_measure(12), None)


import flask
from flask import request, jsonify
import pandas as pd
import  random
app = flask.Flask(__name__)
app.config["DEBUG"] = True

# Create some test data for our catalog in the form of a list of dictionaries.
original = pd.read_csv('arts_craftss.csv')

# set first column to index
df = pd.read_csv('arts_crafts_result.csv', index_col=0)
# set columns and index names
df.columns.name = df.index.name
df.index.name = df.index[0]
# remove first row of data
df = df.iloc[1:]

@app.route('/', methods=['GET'])
def home():
    return '''
Distant Reading Archive

A prototype API for distant reading of science fiction novels.
'''

@app.route('/antique/api/user', methods=['GET'])
def api_id():
    # Check if an ID was provided as part of the URL.
    # If ID is provided, assign it to a variable.
    # If no ID is provided, display an error in the browser.
    if 'id' in request.args:
        userId = request.args['id']
    else:
        return "Error: No id field provided. Please specify an id."

    # check if user exists
    if userId not in original['userId'].values:
        return jsonify(random.choices(original['title'].to_list(),k=10))

    # get already purchased, rated items
    rated = original[original['userId'] == userId]['title'].to_list()

    # get recommended items for users by sorting items Descending by rating
    recommended = df.loc[userId].sort_values(ascending=False).index

    # filter the recommended items and remove already rated items
    recommended_filtered = [i for i in recommended if i not in rated][:3]

    # return filtered recommendation
    return jsonify(recommended_filtered)

app.run()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
from bson.json_util import dumps
from django.shortcuts import render_to_response
from django.template import RequestContext
from mpcontribs.rest.views import get_endpoint
from mpcontribs.io.core.recdict import render_dict
from mpcontribs.io.core.components import render_plot
from test_site.settings import STATIC_URL

def index(request):
    ctx = RequestContext(request)
    if request.user.is_authenticated():
        if request.user.groups.filter(name='slac_mose2').exists():
            API_KEY = request.user.api_key
            ENDPOINT = request.build_absolute_uri(get_endpoint())
            from ..rest.rester import SlacMose2Rester
            with SlacMose2Rester(API_KEY, endpoint=ENDPOINT) as mpr:
                try:
                    prov = mpr.get_provenance()
                    ctx['title'] = prov.pop('title')
                    ctx['provenance'] = render_dict(prov, webapp=True)

                    contribs = mpr.get_contributions()
                    ctx['graphs'], ctx['uuids'] = [], []
                    for plot in contribs['graphs']:
                        rplot = render_plot(plot, webapp=True)
                        ctx['graphs'].append(rplot[0])
                        ctx['uuids'].append(str(rplot[1]))

                    ctx['traces'] = dumps(contribs['traces'])
                    ctx['trace_names'] = [trace['name'] for trace in contribs['traces']]
                    mod = os.path.dirname(__file__).split(os.sep)[-2]
                    ctx['static_url'] = '_'.join([STATIC_URL[:-1], mod])
                except Exception as ex:
                    ctx.update({'alert': str(ex)})
        else:
            ctx.update({'alert': 'access restricted to "slac_mose2" user group!'})
    else:
        ctx.update({'alert': 'Please log in!'})
    return render_to_response("slac_mose2_explorer_index.html", ctx)


"""
gazellescraper.scraper
~~~~~~~~~~~~~~~~~~~~~~

Module containing functions that scrape data from a connected gazelle api

~~~~~~~~~~~~~~~

Copyright Michael Adkins 2017
Distributed under the MIT License.
See accompanying file LICENSE.md file or copy at http://opensource.org/licenses/MIT

"""
import whatapi
import pickle
import os

from .utils import format_data
from .models import ScrapeMode, ScrapeType

from tqdm import tqdm
from datetime import datetime

#################
#### Config #####
#################

what_server = 'https://passtheheadphones.me'

what_credentials = {
        'username':     'bleh',
        'password':     'meh',
        'server':       what_server
    }

global_cookie_path = 'cookies.dat'
scrape_path = 'scraped.dat'

#################
#### SCRAPER ####
#################

log = []

def connect(credentials, cookie_path=None):
    c = None
    if cookie_path:
        try:
            c = pickle.load((open(cookie_path, 'rb')))
        except:
            print("Failed to load cookies.")
    handle = whatapi.WhatAPI(**credentials, cookies=c)
    if cookie_path:
        pickle.dump(handle.session.cookies, open(cookie_path, 'wb'))
    return handle

def scrape(handle, scrape_type, data, save_path, mode=ScrapeMode.UPDATEAPPEND):

    count_skipped = 0
    count_old = 0
    count_new = 0

    # Pull information from old data file if specified
    old_data = None
    if mode == ScrapeMode.UPDATEAPPEND or mode == ScrapeMode.FASTUPDATE:
        if not os.path.isfile(save_path):
            print("Warning! '{}' does not exist, a new file will be written".format(save_path))
            mode = ScrapeMode.OVERWRITE
        else:
            old_data = pickle.load(open(save_path, 'rb'))
            if not 'scrapeType' in old_data:
                raise ValueException("'{}' does not appear to be a scrape file".format(save_path))
            if not old_data['scrapeType'] == scrape_type:
                raise ValueException("Scrape type '{}' does not match file"
                "scrape type of '{}'".format(scrape_type, old_data['scrapeType']))
                return
            count_old = len(old_data['json'])

    if scrape_type == ScrapeType.MANUAL:
        # Construct from data
        raise NotImplementedError()

    elif scrape_type == ScrapeType.ARTIST:
        # Handle multi and single artist by always pushing into a list
        if type(data) == str:
            data = [data]
        # Split & or and separated artists
        for a in data:
            if ' & ' in a:
                data = data + a.split(' & ')
            if ' and ' in a:
                data = data + a.split(' and ')
        if not type(data) == list:
            raise ValueException("Scrape type 'ARTIST' requires data as a string"
                "or list")
        if mode == ScrapeMode.FASTUPDATE and not old_data == None:
            new_data = {}
            for d in tqdm(data):
                if d not in old_data['json']:
                    new_data[d] = scrape_artist(handle, d)
                else:
                    count_skipped += 1
        else:
            new_data = {d: scrape_artist(handle, d) for d in tqdm(data)}

    elif scrape_type == ScrapeType.ARTIST_ALBUM:
        if not type(data) == dict:
            raise ValueException("Scrape type 'ARTIST_ALBUM' requires the format"
                "data = {'Artist': [Album, Album, Album]}")
        new_data = {art: scrape_albums(handle, art, alb)
                        for (art, albs) in tqdm(data.items())}

    elif scrape_type == ScrapeType.TOP10:
        new_data = scrape_top10(handle)

    elif scrape_type == ScrapeType.REQUESTS:
        new_data = scrape_requests(handle)

    else:
        raise ValueException("Invalid scrape type '{}'".format(scrape_type))

    count_new = len(new_data)

    # Combine data
    if mode == ScrapeMode.OVERWRITE:
        full_data = new_data
    elif (not old_data is None) and (mode == ScrapeMode.UPDATEAPPEND or mode == ScrapeMode.FASTUPDATE):
        full_data = {**old_data['json'], **new_data}

    # Finalize and save
    final_data = format_data(full_data, scrape_type)
    pickle.dump(final_data, open(save_path, 'wb'))
    print("Scrape completed with the addition of {} items for a total of {} with {} skipped.", count_new, count_new + count_old, count_skipped)
    return final_data

def scrape_artist(handle, artist):
    try:
        r = handle.request("artist", artistname=artist)
    except Exception as e:
        # Fuck exceptions in whatapi
        log.append("An exception occured on the request for '{}'. Details: {}".format(artist, e.args[0]))
        return None
    else:
        if not r['status'] == 'success':
            log.append("The request for {} failed.".format(artist))
            return None
        return r['response']

def scrape_albums(handle, artist, albums):
    artist_data = scrape_artist(handle, artist)
    if artist_data == None:
        log.append("Couldn't find artist '{}' on {}".format(artist, handle.server))
        return None

def scrape_top10(handle):
    raise NotImplementedError()

def scrape_requests(handle):
    raise NotImplementedError()

def printlog():
    global log
    for l in log:
        print(l)
    log = []


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 27 10:18:32 2020

@author: marcus
"""

# This file will define a script to correlate bursting time with position in a video.
# To ensure self completeness bursting times will be found from the raw intensity data in this script

import pandas as pd
import sys
import glob
from getBurstingTime import get_bursting_time
import numpy as np
from matplotlib import pyplot as plt

if __name__ == '__main__':
    # we use glob to get a list of file paths for the trap positions files. The name pattern is defined here.

    positions_names = 'trap_positions_*.csv'

    debug = True

    if len(sys.argv) > 1:
        dpath = sys.argv[1]

    else:
        print("Haven't entered directory path as Command Line option")
        sys.exit()

    if len(sys.argv) > 2:
        opath = sys.argv[2]
        if debug:
            print(opath)
    else:
        print("Haven't given an output path name pattern as Command Line option")

        sys.exit()

TPfile_list = glob.glob(dpath+'/'+positions_names)

Ifile_list = []

for file in TPfile_list:
    Ifile_list.append(dpath+'/'+'detected_at_beginning_non_filtered_Intensities_'+file[file.find('tions')+6:])

if debug:

    print(TPfile_list[2])
    print(Ifile_list[2])

for i in range(0,len(TPfile_list)):

    # select matching file paths
    TPfile = TPfile_list[i]
    Ifile = Ifile_list[i]

    #load data
    positions =  np.loadtxt(TPfile,delimiter = ',')
    dfI = pd.read_csv(Ifile)

    #create a new data frame with bursting times

    dfBT = get_bursting_time(dfI)

    # ensure that the data frame is ordered same as the positions.

    BTarray = []
    missing_labels = []
    for i in range(0,len(positions[:,0])):
        #label = str(int(positions[:,0][i]))
        #need to force label to be in right format
        label = str(int(positions[:,0][i]))
        if len(label) == 1:
            label = '00'+label
        elif len(label) ==2:
            label = '0'+label

        try:
            BTarray.append(dfBT[label][0])
        except KeyError:
            print(label)
            missing_labels.append(i)
            continue

    BTarray  = np.array(BTarray)

    print('Bursting time numpy array ', BTarray)
    offset = 0
    print(missing_labels)
    for j in missing_labels:
        j -= offset
        print(positions.shape)
        positions = np.vstack((positions[:j,:], positions[j+1:,:]))

        offset +=1
    plt.scatter(positions[:,2],positions[:,1],s=10,c=BTarray)

    plt.show()


#
# 17. Letter Combinations of a Phone Number
#
# Given a string containing digits from 2-9 inclusive, return all possible
# letter combinations that the number could represent.
#
# A mapping of digit to letters (just like on the telephone buttons) is given
# below. Notethat 1 does not map to any letters.
#
# Example:
#     Input: "23"
#     Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]
#
# Note:
#     Although the above answer is in lexicographical order, your answer could
#     be in any order you want.
#

class Solution(object):
    # 递归
    def letterCombinations(self, digits):
        """
        type digits: str
        rtype: List[str]
        """
        self.res = []
        if digits:
            self.backtrack('', digits)
        return self.res

    def backtrack(self, combination, next_digits):
        """
        type combination: str
        type next_digits: str
        rtype: List[str]
        """
        alphas = {
            '2': 'abc', '3': 'def', '4': 'ghi', '5': 'jkl', '6': 'mno',
            '7': 'pqrs', '8': 'tuv', '9': 'wxyz'
        }
        if not next_digits:
            self.res.append(combination)
        else:
            for a in alphas[next_digits[0]]:
                self.backtrack(combination + a, next_digits[1:])


import bs4
import csv
import requests
import sys

url = "http://planetpython.org"
source = requests.get(url)

soup = bs4.BeautifulSoup(source.content, 'lxml')

def get_content():
    links = [link.get('href') for link in soup.find_all('a')]

    return links

def content_to_csv():
    writer = csv.writer(sys.stdout)
    links = get_content()
    writer.writerow(['Link'])
    for link in links:
        writer.writerow([link])
    writer.writerow([])


import gym
import random
import re
import time
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from collections import deque
from omegaconf import OmegaConf
from torch import distributions as pyd
from torch.distributions.utils import _standard_normal

class eval_mode:
    def __init__(self, *models):
        self.models = models

    def __enter__(self):
        self.prev_states = []
        for model in self.models:
            self.prev_states.append(model.training)
            model.train(False)

    def __exit__(self, *args):
        for model, state in zip(self.models, self.prev_states):
            model.train(state)
        return False

def set_seed_everywhere(seed):
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(seed)
    np.random.seed(seed)
    random.seed(seed)

def soft_update_params(net, target_net, tau):
    for param, target_param in zip(net.parameters(), target_net.parameters()):
        target_param.data.copy_(tau * param.data +
                                (1 - tau) * target_param.data)

def to_torch(xs, device):
    return tuple(torch.as_tensor(x, device=device) for x in xs)

def weight_init(m):
    if isinstance(m, nn.Linear):
        nn.init.orthogonal_(m.weight.data)
        if hasattr(m.bias, 'data'):
            m.bias.data.fill_(0.0)
    elif isinstance(m, nn.Conv2d) or isinstance(m, nn.ConvTranspose2d):
        gain = nn.init.calculate_gain('relu')
        nn.init.orthogonal_(m.weight.data, gain)
        if hasattr(m.bias, 'data'):
            m.bias.data.fill_(0.0)

class Until:
    def __init__(self, until, action_repeat=1):
        self._until = until
        self._action_repeat = action_repeat

    def __call__(self, step):
        if self._until is None:
            return True
        until = self._until // self._action_repeat
        return step < until

class Every:
    def __init__(self, every, action_repeat=1):
        self._every = every
        self._action_repeat = action_repeat

    def __call__(self, step):
        if self._every is None:
            return False
        every = self._every // self._action_repeat
        if step % every == 0:
            return True
        return False

class Timer:
    def __init__(self):
        self._start_time = time.time()
        self._last_time = time.time()

    def reset(self):
        elapsed_time = time.time() - self._last_time
        self._last_time = time.time()
        total_time = time.time() - self._start_time
        return elapsed_time, total_time

    def total_time(self):
        return time.time() - self._start_time

class TruncatedNormal(pyd.Normal):
    def __init__(self, loc, scale, low=-1.0, high=1.0, eps=1e-6):
        super().__init__(loc, scale, validate_args=False)
        self.low = low
        self.high = high
        self.eps = eps

    def _clamp(self, x):
        clamped_x = torch.clamp(x, self.low + self.eps, self.high - self.eps)
        x = x - x.detach() + clamped_x.detach()
        return x

    def sample(self, clip=None, sample_shape=torch.Size()):
        shape = self._extended_shape(sample_shape)
        eps = _standard_normal(shape,
                               dtype=self.loc.dtype,
                               device=self.loc.device)
        eps *= self.scale
        if clip is not None:
            eps = torch.clamp(eps, -clip, clip)
        x = self.loc + eps
        return self._clamp(x)

def schedule(schdl, step):
    try:
        return float(schdl)
    except ValueError:
        match = re.match(r'linear\((.+),(.+),(.+)\)', schdl)
        if match:
            init, final, duration = [float(g) for g in match.groups()]
            mix = np.clip(step / duration, 0.0, 1.0)
            return (1.0 - mix) * init + mix * final
        match = re.match(r'step_linear\((.+),(.+),(.+),(.+),(.+)\)', schdl)
        if match:
            init, final1, duration1, final2, duration2 = [
                float(g) for g in match.groups()
            ]
            if step <= duration1:
                mix = np.clip(step / duration1, 0.0, 1.0)
                return (1.0 - mix) * init + mix * final1
            else:
                mix = np.clip((step - duration1) / duration2, 0.0, 1.0)
                return (1.0 - mix) * final1 + mix * final2
    raise NotImplementedError(schdl)

class ActionRepeatWrapper(gym.Wrapper):
    """Gym wrapper for repeating actions."""
    def __init__(self, env, action_repeat, discount):
        gym.Wrapper.__init__(self, env)
        self._env = env
        self._action_repeat = action_repeat
        self._discount = discount

    def reset(self, seed=None):
        return self._env.reset(seed=seed)

    def step(self, action):
        total_reward = 0.0
        discount = 1.0
        for _ in range(self._action_repeat):
            obs, reward, done, info = self._env.step(action)
            total_reward += reward * discount
            discount *= self._discount
            if done:
                break
        return obs, total_reward, done, info

    def render(self, **kwargs):
        return self._env.render(**kwargs)

class FrameStackWrapper(gym.Wrapper):
    """Gym wrapper for stacking image observations."""
    def __init__(self, view, env, k):
        self.view = view
        self._env = env
        gym.Wrapper.__init__(self, env)
        self._k = k
        if str(self.view) == 'both' or self.view == 'double_view_3':
            self._frames1 = deque([], maxlen=k)
            self._frames3 = deque([], maxlen=k)
        else:
            self._frames = deque([], maxlen=k)
        self._proprio_obs_stack = deque([], maxlen=k)
        shp = env.observation_space['im_rgb'].shape
        self.observation_space = gym.spaces.Box(
            low=0,
            high=1,
            shape=((shp[0] * k,) + shp[1:]),
            dtype=env.observation_space['im_rgb'].dtype)
        self._max_episode_steps = env.max_path_length

    def reset(self, seed=None):
        obs = self._env.reset(seed=seed)
        for _ in range(self._k):
            if str(self.view) == 'both' or self.view == 'double_view_3':
                self._frames1.append(obs['im_rgb1'])
                self._frames3.append(obs['im_rgb3'])
            else:
                self._frames.append(obs['im_rgb'])
            self._proprio_obs_stack.append(obs['proprio'])
        return self._get_obs()

    def step(self, action):
        obs, reward, done, info = self._env.step(action)
        if str(self.view) == 'both' or self.view == 'double_view_3':
            self._frames1.append(obs['im_rgb1'])
            self._frames3.append(obs['im_rgb3'])
        else:
            self._frames.append(obs['im_rgb'])
        self._proprio_obs_stack.append(obs['proprio'])
        return self._get_obs(), reward, done, info

    def _get_obs(self):
        assert len(self._proprio_obs_stack) == self._k
        proprio_obs = np.concatenate(list(self._proprio_obs_stack), axis=0)
        if str(self.view) == 'both' or self.view == 'double_view_3':
            assert len(self._frames1) == self._k
            assert len(self._frames3) == self._k
            img_obs1 = np.concatenate(list(self._frames1), axis=0)
            img_obs3 = np.concatenate(list(self._frames3), axis=0)
            return img_obs1, img_obs3, proprio_obs
        else:
            assert len(self._frames) == self._k
            img_obs = np.concatenate(list(self._frames), axis=0)
            return img_obs, proprio_obs

    def render(self, **kwargs):
        return self._env.render(**kwargs)


#<ImportModules>
import ShareYourSystem as SYS
#</ImportModules>

#<DefineClass>
class FileClass(SYS.ObjectClass):

	#<DefineHookMethods>
	def initAfter(self):

		#<DefineSpecificDict>
		self.FilePathString=""
		self.FolderPathString=""
		self.ExtensionString=""
		self.FileString=""
		self.ModeString="r"
		#</DefineSpecificDict>

	def bindAfter(self,_LocalCallVariable,_LocalCallingVariablesList):

		#Bind with FilePathString setting
		_FilePathString=self.FolderPathString+self.FileString
		if self.ExtensionString!="":
			_FilePathString+=self.ExtensionString
		self['FilePathString']=_FilePathString
	#</DefineHookMethods>

#</DefineClass>


from socket import *
import json
import time
import sys

def server_param():
    if sys.argv == 3:
        for param1, param2 in sys.argv:
            port = param1
            host = param2
    elif sys.argv == 2:
        for param in sys.argv:
            port = param
            host = ''
    else:
        host = ''
        port = 7777
    return host, port

def client_data_to_dict(a):
    data_client = a
    objs = json.loads(data_client)
    client_responce = dict()
    for section, commands in objs.items():
        client_responce[section] = commands
    return client_responce

def server_responce_200(account_name):
    server_answer = {
        "response": 200,
        "alert": "Имя {}! Время: {}".format(account_name, time.time())
    }
    print(server_answer)
    return server_answer

def server_responce(client_responce):
    if client_responce['action'] == 'presence':
        account_name = client_responce['user']['account_name']
        answer = server_responce_200(account_name)
        return answer
    else:
        return SERVER_RESPONSE_ERROR

SERVER_RESPONSE_200 = {
    "response": 200,
    "alert": "Необязательное сообщение/уведомление"}

SERVER_RESPONSE_ERROR = {
    "response": '4xx / 5xx',
    "time": '<unix timestamp>',
    "error": "error message (optional)"}

host, port = server_param()
s = socket(AF_INET, SOCK_STREAM)
s.bind((host, port))
s.listen(5)

while True:
    client, addr = s.accept()
    data = client.recv(1024)
    print("Запрос от: {}".format(str(client)))
    data_client = data.decode('UTF-8')
    server_responce1 = client_data_to_dict(data_client)
    server_responce2 = server_responce(server_responce1)
    server_responce_json = str(server_responce2).encode('UTF-8')
    client.send(server_responce_json)
    client.close()


import gym
import sys
sys.path.append("algs")
sys.path.append("envs")
from DQN import DeepQNetwork
from six_legged_env import SixLeggedEnv
from two_legged_env_dqn import TwoLeggedEnv
import numpy as np
import matplotlib.pyplot as plt

class DynamicPlot():
    #Class used for plotting
    #__init__ for start a new plot
    #update for updating the plot
    #close to close the current plot
    plt.ion()
    def __init__(self):

        #Initialize data
        self.xdata = []
        self.ydata = []

        #Set up plot
        self.figure, self.ax = plt.subplots()
        self.lines, = self.ax.plot([],[], '-')

        #Autoscale on unknown axis and known lims on the other
        self.ax.set_autoscaley_on(True)

        #Other stuff
        self.ax.grid()

    def update(self, iteration, reward):
        self.xdata.append(iteration)
        self.ydata.append(reward)

        #Update data (with the new _and_ the old points)
        self.lines.set_xdata(self.xdata)
        self.lines.set_ydata(self.ydata)

        #Need both of these in order to rescale
        self.ax.relim()
        self.ax.autoscale_view()

        #We need to draw *and* flush
        self.figure.canvas.draw()
        plt.xlabel('iteration')
        plt.ylabel('reward')
        plt.xlim((0, iteration+1))
        plt.ylim((-5, 20))
        plt.title('Learning Curve - two_legged with DQN')
        self.figure.canvas.flush_events()

    def close(self):
        plt.close(self.figure)

MAX_EPISODES = 200
MAX_EP_STEPS = 2000
isTrain = True
def run_ant(rl_agent):
    step = 0
    d = DynamicPlot()
    for episode in range(MAX_EPISODES):
        # initial observation
        observation = env.reset()
        print("reset")
        for i in range(MAX_EP_STEPS):
            # fresh env
            if (not isTrain) or episode >= MAX_EPISODES/6*5:
                env.render()
            # RL choose action based on observation
            action, action_idx = rl_agent.choose_action(observation)
            #action = env.action_space.sample()

            # RL take action and get next observation and reward

            observation_, reward, done, info = env.step(action)

            # Plot reward here
            d.update(i, reward)

            if isTrain:
                rl_agent.store_transition(observation, action_idx, reward, observation_)

            if isTrain and (step > 200) and (step % 5 == 0):
                rl_agent.learn()

            # swap observation
            observation = observation_

            # break while loop when end of this episode
            if done:
                break
            step += 1
            #print(info)
            if (step % 300 == 0):
                print("reward:",reward, "info:", info)

        d.close()
        d.__init__()

    # end
    print('over')
    sys.exit()
if __name__ == "__main__":
    ###get environment
    #env = gym.make('HalfCheetah-v2')##HalfCheetah, Ant, Humanoid
    env = TwoLeggedEnv()#SixLeggedEnv()
    #env = myEnv() #self-defined enviornment

    ###initialize rl_agent
    state_dim = env.observation_space.shape[0]
    action_dim = env.action_space.shape[0]
    print(action_dim)
    if (isTrain):
        model_path = "models/dqn_two_legged"
    else:
        model_path = "models/dqn_two_legged_final"
    rl_agent = DeepQNetwork(model_path, action_dim, state_dim,
                      learning_rate=0.01,
                      reward_decay=0.9,
                      e_greedy=0.9,
                      replace_target_iter=200,
                      memory_size=2000,
                      output_graph=True
                      )
    #parse rl_agent to run the environment
    run_ant(rl_agent)


# coding:utf-8
# /usr/bin/python

# creator = wangkai
# creation time = 2018/4/22 22:08

from ctypes import *
import time

lib = cdll.LoadLibrary("./speed.so")
lib.main()

lastCount = 0
while 1:
    count = lib.returnCount()
    print("count:", count)
    speed = count - lastCount
    print("speed:", speed)
    lastCount = count
    time.sleep(0.5)


__author__ = 'licheng'

class Solution(object):
    def longestCommonPrefix(self, strs):
        """
        :type strs: List[str]
        :rtype: str
        """
        if len(strs) == 0:
            return ''
        if len(strs) == 1:
            return strs[0]

        cand = strs[0]
        pre = ''
        for cid, c in enumerate(cand):
            for st in strs[1:]:
                if cid > len(st)-1 or st[cid] != c:
                    return pre
            pre += c

        return pre


from flask_restful import Resource
from flask import request

from blueprints.tenants.arg_parser import (tenant_parser,
                                           tenant_parser_delete)
from blueprints.tenants.tenant_object_serlzer import marshal_with_decor
from blueprints.tenants.tenants_db import tenants_storage, Tenant

class Tenants(Resource):
    @marshal_with_decor
    def get(self):
        tenant_filters = tenant_parser.parse_args()
        response = tenants_storage
        if any(tuple(tenant_filters.values())):
            response = [tenant for tenant in tenants_storage
                        for tenant_filter in tenant_filters.items()
                        if getattr(tenant, tenant_filter[0]) == tenant_filter[1]]
        return response

    def post(self):
        new_tenant_values = request.get_json()
        tenants_storage.append(Tenant(**new_tenant_values))
        return 'New tenant was successfully added!'

    def patch(self):
        tenant_info_to_update = request.get_json()
        tenant_to_update = None
        for tenant in tenants_storage:
            if tenant.name == tenant_info_to_update.get('name'):
                tenant_to_update = tenant
        for key, value in tenant_info_to_update.items():
            setattr(tenant_to_update, key, value)
        return f' Info about tenant - {tenant_to_update.name} was successfully updated!'

    def delete(self):
        tenant_to_delete = tenant_parser_delete.parse_args().get('name')
        for tenant in tenants_storage:
            if tenant.name == tenant_to_delete:
                tenants_storage.remove(tenant)
                return f'The tenant - {tenant.name} successfully deleted from database!'


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings
import gestionAsignaturas.models

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('gestionPersonal', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Archivo',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('nombre', models.CharField(max_length=50)),
                ('fichero', models.FileField(upload_to=gestionAsignaturas.models.ruta_archivo)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Asignatura',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('nombre', models.CharField(max_length=50)),
                ('descripcion', models.TextField(default=b'---')),
                ('contrasena', models.CharField(max_length=50, null=True, blank=True)),
                ('alumnos', models.ManyToManyField(to='gestionPersonal.Alumno', null=True, blank=True)),
            ],
            options={
                'verbose_name_plural': 'Asignaturas',
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Curso',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('nombre', models.CharField(max_length=50, choices=[(b'Primer curso', b'Primer curso'), (b'Segundo curso', b'Segundo curso'), (b'Tercer curso', b'Tercer curso'), (b'Cuarto curso', b'Cuarto curso'), (b'Quinto curso', b'Quinto curso'), (b'Sexto curso', b'Sexto curso')])),
                ('descripcion', models.TextField(null=True, blank=True)),
            ],
            options={
                'verbose_name_plural': 'Cursos',
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Foro',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('titulo', models.CharField(max_length=60)),
                ('asignatura', models.OneToOneField(to='gestionAsignaturas.Asignatura')),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Grado',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('nombre', models.CharField(max_length=50)),
                ('descripcion', models.TextField(default=b'---')),
            ],
            options={
                'verbose_name_plural': 'Grados',
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Hilo',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('titulo', models.CharField(max_length=60)),
                ('texto', models.TextField(max_length=10000)),
                ('fecha_creacion', models.DateTimeField(auto_now_add=True)),
                ('creador', models.ForeignKey(blank=True, to=settings.AUTH_USER_MODEL, null=True)),
                ('foro', models.ForeignKey(to='gestionAsignaturas.Foro')),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Respuesta',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('titulo', models.CharField(max_length=60)),
                ('texto', models.TextField(max_length=10000)),
                ('fecha_creacion', models.DateTimeField(auto_now_add=True)),
                ('creador', models.ForeignKey(blank=True, to=settings.AUTH_USER_MODEL, null=True)),
                ('hilo', models.ForeignKey(to='gestionAsignaturas.Hilo')),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.CreateModel(
            name='Seccion',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('nombre', models.CharField(max_length=50)),
                ('asignatura', models.ForeignKey(to='gestionAsignaturas.Asignatura')),
            ],
            options={
                'verbose_name_plural': 'Secciones',
            },
            bases=(models.Model,),
        ),
        migrations.AddField(
            model_name='curso',
            name='grado',
            field=models.ForeignKey(to='gestionAsignaturas.Grado'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='asignatura',
            name='curso',
            field=models.ForeignKey(to='gestionAsignaturas.Curso'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='asignatura',
            name='profesores',
            field=models.ManyToManyField(to='gestionPersonal.Profesor'),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='archivo',
            name='seccion',
            field=models.ForeignKey(blank=True, to='gestionAsignaturas.Seccion', null=True),
            preserve_default=True,
        ),
    ]