# -*- coding: utf-8 -*-

import json
import time
import pymongo
import pymongo.errors as pymongo_err
from bson import json_util
import mysql.connector
import decimal
# sql json格式查询接口
from app.api_1_0.api_functions.SqlPack.SQLModel import qureResultAsJson
# 获取连接信息
from app.api_1_0.api_functions.SqlPack.SqlLinkInfo import getFlowerQueryFunction as Sql

# 获取新架构卡资源数据库连接信息
sql_info = Sql

def timestamp_datetime(value):
    format1 = '%Y-%m-%d %H:%M:%S'
    format2 = '%Y-%m-%d'
    # value为传入的值为时间戳(整形)，如：1332888820
    structFormate = time.gmtime(value)
    # 经过localtime转换后变成结构型时间
    # 最后再经过strftime函数转换为字符型正常日期格式。
    try:
        dt = time.strftime(format1, structFormate)
    except ValueError:
        dt = time.strftime(format2, structFormate)

    return dt

def getJsonData(sys_str, data_base, query_str):
    """
    qureResultAsJson 自带链接断开操作
    :param sys_str:
    :param data_base:
    :param query_str:
    :return:
    """
    jsonResults = qureResultAsJson(sysStr=sys_str,
                                   Database=data_base,
                                   query_str=query_str,
                                   where=[])
    return jsonResults

def getSimPackageStatic(dic_param):
    """

    :param dic_param:
    :return:
    """
    errInfo = ''
    sim_package_info_data = []
    country_str = ''
    org_str = ''
    sim_type_str = ''
    package_type_name_str = ''
    ava_status_str = ''
    business_status_str = ''
    package_status_str = ''
    slot_status_str = ''
    bam_status_str = ''
    country = ''
    org = ''
    sim_type = ''
    package_type_name = ''
    ava_status = ''
    business_status = ''
    package_status = ''
    slot_status = ''
    bam_status = ''
    try:
        country = dic_param['country']
        org = dic_param['orgName']
        sim_type = dic_param['simType']
        package_type_name = dic_param['packageTypeName']
        ava_status = dic_param['avaStatus']
        business_status = dic_param['businessStatus']
        package_status = dic_param['packageStatus']
        slot_status = dic_param['slotStatus']
        bam_status = dic_param['bamStatus']
    except KeyError:
        errInfo = 'api simPackageInfo static param keyErr!'
    if errInfo:
        dic_data = []
        dic_results = {'info': {'err': True, 'errInfo': errInfo}, 'data': dic_data}
        return dic_results
    else:
        if country:
            country_str = "AND a.`iso2` = '" + country + "'"
        if org:
            if org == 'all':
                org_str = ''
            else:
                org_str = " AND e.`org_name` = '" + org + "' "
        if sim_type:
            sim_type_str = " AND a.`vsim_type` = '" + sim_type + "' "
        if package_type_name:
            package_type_name_str = " AND c.`name` LIKE  '" + package_type_name + "%' "
        if ava_status:
            ava_status_str = " AND a.`available_status` IN (" + ava_status + ") "
        if business_status:
            business_status_str = " AND a.business_status IN (" + business_status + ") "
        if package_status:
            package_status_str = " AND b.`package_status`  IN (" + package_status + ") "
        if slot_status:
            slot_status_str = " AND a.`slot_status` IN (" + slot_status + ") "
        if bam_status:
            bam_status_str = " AND a.`bam_status` IN (" + bam_status + ") "

        str_query = (
            "( "
            "SELECT  "
            "a.`iso2`              AS 'Country',  "
            "CASE WHEN 1 THEN CONCAT('1-合计-',a.`iso2`,'-',e.`org_name` ) END  AS 'PackageName',  "
            "CASE WHEN 1 THEN '' END  AS 'NextUpdateTime',  "
            "CASE WHEN 1 THEN '' END  AS 'LastUpdateTime', "
            "COUNT(DISTINCT a.`imsi`) AS 'all_num',  "
            "CAST((SUM(b.`total_use_flow`)/SUM(CASE WHEN `activate_status` = 0  "
            "                                       THEN b.`init_flow` "
            "                                       ELSE 0 END ))*100  AS DECIMAL(64,1)) AS 'Percentage'  "
            "FROM `t_css_vsim` AS a  "
            "LEFT  JOIN `t_css_vsim_packages` AS b  ON a.`imsi`= b.`imsi`  "
            "LEFT  JOIN `t_css_group` AS e          ON a.`group_id`= e.`id`  "
            "LEFT  JOIN `t_css_package_type`  AS c  ON c.`id` = b.`package_type_id`  "
            "WHERE  c.`name` IS NOT NULL "
            "       AND b.`init_flow` IS NOT NULL  " + country_str + org_str + sim_type_str + package_type_name_str +
            ava_status_str + business_status_str + package_status_str + slot_status_str + bam_status_str + " "
            "GROUP BY a.`iso2`,e.`org_name`  "
            ") "
            "UNION "
            "( "
            "SELECT  "
            "a.`iso2` AS 'Country',  "
            "c.`name` AS 'PackageName',  "
            "DATE_FORMAT(b.`next_update_time`,'%Y-%m-%d %H')  AS 'NextUpdateTime', "
            "DATE_FORMAT(b.`last_update_time`,'%Y-%m-%d %H')  AS 'LastUpdateTime', "
            "COUNT(DISTINCT a.`imsi`) AS 'all_num',  "
            "CAST((SUM(b.`total_use_flow`)/SUM(CASE WHEN `activate_status` = 0 "
            "                                       THEN b.`init_flow` "
            "                                       ELSE 0 END ))*100  AS DECIMAL(64,1)) AS 'Percentage'  "
            "FROM `t_css_vsim` AS a  "
            "LEFT  JOIN `t_css_vsim_packages` AS b  ON a.`imsi`= b.`imsi`  "
            "LEFT  JOIN `t_css_group`         AS e  ON a.`group_id`= e.`id`  "
            "LEFT  JOIN `t_css_package_type`  AS c  ON c.`id` = b.`package_type_id`  "
            "WHERE  c.`name` IS NOT NULL "
            "       AND b.`init_flow` IS NOT NULL  " + country_str + org_str + sim_type_str + package_type_name_str +
            ava_status_str + business_status_str + package_status_str + slot_status_str + bam_status_str + " "
            "GROUP BY a.`iso2`,c.`name`,DATE_FORMAT(b.`next_update_time`,'%Y-%m-%d %H'), e.`org_name`  "
            ") "
            "ORDER BY Country,NextUpdateTime,PackageName DESC "
        )
        try:
            sim_package_info_data = getJsonData(sys_str=sql_info['src_on_sys']['db'],
                                                data_base=sql_info['src_on_sys']['database'],
                                                query_str=str_query)
        except KeyError as keyerr:
            errInfo = ("KeyError:{}".format(keyerr))
        except mysql.connector.Error as err:
            errInfo = ("Something went wrong: {}".format(err))
        if errInfo != '':
            dic_results = {'info': {'err': True, 'errInfo': errInfo}, 'data': []}
            return dic_results
        else:
            if not sim_package_info_data:
                dic_results = {'info': {'err': False, 'errInfo': "No Query Data"}, 'data': []}
                return dic_results
            else:
                for cs in sim_package_info_data:
                    if type(cs['Percentage']) is decimal.Decimal:
                        cs['Percentage'] = float(cs['Percentage'])
                dic_results = {'info': {'err': True, 'errInfo': errInfo}, 'data': sim_package_info_data}
                return dic_results

def getSimPackageFlowerAPI(sim_package_param):
    """

    :param sim_package_param:
    :return:
    """

    SimPackageInfo = getSimPackageStatic(sim_package_param)

    return json.dumps(SimPackageInfo, sort_keys=True, indent=4, default=json_util.default)

def mongo_agg_hour(db_str, pip_line, oriage_db):
    """

    :param db_str:
    :param pip_line:
    :param oriage_db:
    :return:
    """
    agg_data = []
    errInfo = ''
    connection = pymongo.MongoClient(sql_info[db_str]['uri'])
    try:
        agg_data = list(connection.get_database(sql_info[db_str]['db']
                                                ).get_collection(sql_info[db_str]['collection']
                                                                 ).aggregate(pip_line)
                        )
    except KeyError as k_err:
        errInfo = 'erro:' + str(k_err)
    except pymongo_err.OperationFailure:
        errInfo = "DataBase Authentication failed!"
    except pymongo_err.NetworkTimeout:
        errInfo = "DataBase Connection Exceeded SocketTimeoutMS!"
    except pymongo_err.InvalidStringData as isd:
        errInfo = 'err:' + str(isd)
    except:
        errInfo = "Unexpected error"
    connection.close()
    if errInfo:
        dic_results = {'info': {'err': True, 'errInfo': errInfo}, 'data': []}
        return dic_results
    else:
        if agg_data:
            for fd in agg_data:
                agg_id_temp = fd.pop('_id')  # {‘_id’:{}}转换成标准json数据
                fd.update(agg_id_temp)
                # 获得MB 单位流量
                fd['Flower'] = round((fd['Flower'] / 1024 / 1024), 2)
            # 流量合并至套餐表单中
            for pd in oriage_db['data']:
                pd_flower_if = False
                for fd in agg_data:
                    if str(pd['imsi']) == fd['imsi']:
                        pd['flower'] = fd['Flower']
                        pd_flower_if = True
                        break
                if not pd_flower_if:
                    pd['flower'] = 0
            dic_results = {'info': {'err': False, 'errInfo': errInfo}, 'data': oriage_db}
            return dic_results
        else:
            dic_results = {'info': {'err': False, 'errInfo': '无查询结果'}, 'data': []}
            return dic_results

def mongo_agg_day(db_str, pip_line_hour, pip_line_day, oriage_db):
    """

    :param db_str:
    :param pip_line_hour:
    :param pip_line_day:
    :param oriage_db:
    :return:
    """
    agg_data_hour = []
    agg_data_day = []
    errInfo = ''

    connection_hour = pymongo.MongoClient(sql_info[db_str['hour']]['uri'])
    connection_day = pymongo.MongoClient(sql_info[db_str['day']]['uri'])
    try:
        agg_data_hour = list(connection_hour.get_database(
            sql_info[db_str['hour']]['db']).get_collection(
            sql_info[db_str['hour']]['collection']).aggregate(pip_line_hour)
                             )
        agg_data_day = list(connection_day.get_database(
            sql_info[db_str['day']]['db']).get_collection(
            sql_info[db_str['day']]['collection']).aggregate(pip_line_day)
                            )
    except KeyError as k_err:
        errInfo = 'erro:' + str(k_err)
    except pymongo_err.OperationFailure:
        errInfo = "DataBase Authentication failed!"
    except pymongo_err.NetworkTimeout:
        errInfo = "DataBase Connection Exceeded SocketTimeoutMS!"
    except pymongo_err.InvalidStringData as isd:
        errInfo = 'err:' + str(isd)
    except:
        errInfo = "Unexpected error"
    connection_hour.close()
    connection_day.close()
    if errInfo:
        dic_results = {'info': {'err': True, 'errInfo': errInfo}, 'data': []}
        return dic_results
    else:
        if agg_data_day or agg_data_hour:
            try:
                # 天流量整形
                for i in range(len(agg_data_day)):
                    agg_id_temp = agg_data_day[i].pop('_id')  # {‘_id’:{}}转换成标准json数据
                    agg_data_day[i].update(agg_id_temp)
                    agg_data_day[i]['Flower'] = round(((agg_data_day[i]['Flower']) / 1024 / 1024),
                                                      2)  # 流量输出为MB
                # 小时流量整形
                for i in range(len(agg_data_hour)):
                    agg_id_temp = agg_data_hour[i].pop('_id')  # {‘_id’:{}}转换成标准json数据
                    agg_data_hour[i].update(agg_id_temp)
                    agg_data_hour[i]['Flower'] = round(((agg_data_hour[i]['Flower']) / 1024 / 1024),
                                                       2)  # 流量输出为MB
                # 天套餐流量合并至信息表
                for pd in oriage_db['data']:
                    pd_flower_if = False
                    for fd in agg_data_day:
                        if str(pd['imsi']) == fd['imsi']:
                            pd['flower'] = fd['Flower']
                            pd_flower_if = True
                            break
                    if not pd_flower_if:
                        pd['flower'] = 0
                # 小时套餐流量何必至信息表--因前一次天操作已经补全所有imsi的flower值，故每个imsi都有
                for fd in agg_data_hour:
                    for pd in oriage_db['data']:
                        if str(pd['imsi']) == fd['imsi']:
                            pd['flower'] = pd['flower'] + fd['Flower']
                            break
                for pd in oriage_db['data']:
                    pd['percentage_f'] = round((pd['flower']/(pd['init_flow']/1024/1024))*100, 2)
            # 错误标记-keyerr
            except KeyError as ke:
                errInfo = '完成流量查询，天流量整形出现错误： ' + str(ke)

            if errInfo:
                dic_results = {'info': {'err': True, 'errInfo': errInfo}, 'data': []}
                return dic_results
            else:
                dic_results = {'info': {'err': False, 'errInfo': errInfo}, 'data': oriage_db}
                return dic_results
        else:
            errInfo = '完成流量查询, 无对应套餐流量使用记录！'
            dic_results = {'info': {'err': False, 'errInfo': errInfo}, 'data': []}
            return dic_results

def getPackageFlower(flower_param, package_info):
    """

    :param flower_param:
    :param package_info:
    :return:
    """
    agg_data = []
    list_str_imsi = []
    query_type = ''
    list_time = []
    add_group_key = []
    errInfo = ''
    group_id = {'imsi': "$imsi"}
    try:
        query_type = flower_param['query_type']
        list_time = flower_param['list_time']
        add_group_key = flower_param['add_group_key']
    except KeyError as kerr:
        errInfo = '参数设置错误-1：' + str(kerr)
    if errInfo:
        dic_results = {'info': {'err': True, 'errInfo': errInfo}, 'data': []}
        return dic_results
    else:
        for pi in package_info['data']:
            list_str_imsi.append(str(pi['imsi']))

        if query_type == 'hour':
            if list_time:
                begin_time_unix = int(list_time[0]['begin']) * 1000
                end_time_unix = int(list_time[0]['end']) * 1000
                matchStages = {'createtime': {'$gte': begin_time_unix, '$lt': end_time_unix},
                               'imsi': {'$in': list_str_imsi}
                               }
                pipeline = [
                    {"$match": matchStages
                     },
                    {"$group": {
                        "_id": group_id,
                        'Flower': {'$sum': {'$add': ["$userFlower", "$sysFlower"]}}
                    }
                    }]
                flower_data_hour = mongo_agg_hour(db_str='queryhourFlower',
                                                  pip_line=pipeline,
                                                  oriage_db=package_info)
                return flower_data_hour
            else:
                errInfo = '小时查询的时间参数为空!'
                dic_results = {'info': {'err': True, 'errInfo': errInfo}, 'data': []}
                return dic_results
        elif query_type == 'day':
            or_hour_match = []
            day_match = {}
            if list_time:
                for i in range(len(list_time)):
                    if i != 1:
                        or_hour_match.append({'createtime': {'$gte': int(list_time[i]['begin']) * 1000,
                                                             '$lt': int(list_time[i]['end']) * 1000
                                                             }
                                              })
                    else:
                        day_match = {'createtime': {'$gte': int(list_time[i]['begin']) * 1000,
                                                    '$lt': int(list_time[i]['end']) * 1000
                                                    }
                                     }
                hour_match_stages = {'$or': or_hour_match,
                                     'imsi': {'$in': list_str_imsi}}
                day_match_stages = {'createtime': day_match['createtime'],
                                    'imsi': {'$in': list_str_imsi}}
                pipeline_hour = [
                    {"$match": hour_match_stages
                     },
                    {"$group": {
                        "_id": group_id,
                        'Flower': {'$sum': {'$add': ["$userFlower", "$sysFlower"]}}
                    }
                    }]
                pipeline_day = [
                    {"$match": day_match_stages
                     },
                    {"$group": {
                        "_id": group_id,
                        'Flower': {'$sum': {'$add': ["$userFlower", "$sysFlower"]}}
                    }
                    }]
                db_str = {
                    'hour': 'queryhourFlower',
                    'day': 'querydayFlower'
                }
                flower_data_day = mongo_agg_day(db_str=db_str,
                                                pip_line_hour=pipeline_hour,
                                                pip_line_day=pipeline_day,
                                                oriage_db=package_info)
                return flower_data_day
            else:
                errInfo = '天时间设置列表为空列表！'
                dic_results = {'info': {'err': True, 'errInfo': errInfo}, 'data': []}
                return dic_results
        else:
            errInfo = '存在不合法的查询类型：hour , day！'
            dic_results = {'info': {'err': True, 'errInfo': errInfo}, 'data': []}
            return dic_results

def getPackageInfo(package_set_param):
    """

    :param package_set_param:
    :return:
    """
    packageInfoData = []
    add_key = []
    country = ''
    org = ''
    sim_type = ''
    package_type_name = ''
    next_update_time = ''
    ava_status = ''
    business_status = ''
    package_status = ''
    slot_status = ''
    bam_status = ''
    errInfo = ''
    org_str = ''
    country_str = ''
    sim_type_str = ''
    package_type_name_str = ''
    next_update_time_str = ''
    ava_status_str = ''
    business_status_str = ''
    package_status_str = ''
    slot_status_str = ''
    bam_status_str = ''
    sim_agg_str = ''
    last_update_time_str = ''
    try:
        country = package_set_param['country']
        org = package_set_param['org']
        sim_type = package_set_param['sim_type']
        package_type_name = package_set_param['package_type_name']
        next_update_time = package_set_param['next_update_time']
        ava_status = package_set_param['ava_status']
        business_status = package_set_param['business_status']
        package_status = package_set_param['package_status']
        slot_status = package_set_param['slot_status']
        bam_status = package_set_param['bam_status']
        add_key = package_set_param['add_group_key']

    except KeyError as kerr:
        errInfo = 'erro:' + str(kerr)
    if errInfo:
        dic_results = {'info': {'err': True, 'errInfo': errInfo}, 'data': packageInfoData}

        return dic_results

    else:
        if country:
            country_str = "AND a.`iso2` = '" + country + "'"
        if org:
            if org == 'all':
                org_str = ''
            else:
                org_str = " AND e.`org_name` = '" + org + "' "
        if sim_type:
            sim_type_str = " AND a.`vsim_type` = '" + sim_type + "' "
        if package_type_name:
            package_type_name_str = " AND c.`name` =  '" + package_type_name + "' "
        if next_update_time:
            next_update_time_str = " AND DATE_FORMAT(b.`next_update_time`,'%Y-%m-%d %H') = '" + next_update_time + "'"
        if ava_status:
            ava_status_str = " AND a.`available_status` IN (" + ava_status + ") "
        if business_status:
            business_status_str = " AND a.business_status IN (" + business_status + ") "
        if package_status:
            package_status_str = " AND b.`package_status`  IN (" + package_status + ") "
        if slot_status:
            slot_status_str = " AND a.`slot_status` IN (" + slot_status + ") "
        if bam_status:
            bam_status_str = " AND a.`bam_status` IN (" + bam_status + ") "
        if add_key:
            if 'sim_agg' in add_key:
                sim_agg_str = "p.`name` AS 'sim_agg',  "
            if 'last_update_time' in add_key:
                last_update_time_str = "DATE_FORMAT(b.`last_update_time`,'%Y-%m-%d %H')  AS 'last_update_time', "
        query_str = (
            "SELECT  "
            "a.`iso2` AS 'country', "
            "a.`imsi` AS 'imsi',  "
            "c.`name` AS 'package_name',  "
            "b.`init_flow`,  "
            "CAST((b.`total_use_flow`/ b.`init_flow`)*100  AS DECIMAL(64,1)) AS 'percentage_fs',  "
            "a.`iccid` AS 'iccid', " + sim_agg_str + last_update_time_str + " "
            "DATE_FORMAT(b.`next_update_time`,'%Y-%m-%d %H')  AS 'next_update_time' "
            "FROM `t_css_vsim` AS a  "
            "LEFT  JOIN `t_css_vsim_packages` AS b  ON a.`imsi`= b.`imsi`  "
            "LEFT  JOIN `t_css_group`         AS e  ON a.`group_id`= e.`id`  "
            "LEFT  JOIN `t_css_package_type`  AS c  ON c.`id` = b.`package_type_id`  "
            "LEFT JOIN `t_css_plmnset` AS p ON a.`plmnset_id`=p.`id`  "
            "WHERE   c.`name` IS NOT NULL "
            "        AND b.`init_flow` IS NOT NULL " + org_str + country_str + sim_type_str + package_type_name_str +
            next_update_time_str + ava_status_str + business_status_str + package_status_str + slot_status_str +
            bam_status_str
        )
        try:
            packageInfoData = getJsonData(sys_str=sql_info['src_on_sys']['db'],
                                          data_base=sql_info['src_on_sys']['database'],
                                          query_str=query_str)
        except KeyError as keyerr:
            errInfo = ("KeyError:{}".format(keyerr))
        except mysql.connector.Error as err:
            errInfo = ("Something went wrong: {}".format(err))
        if errInfo != '':
            dic_results = {'info': {'err': True, 'errInfo': errInfo}, 'data': []}
            return dic_results
        else:
            if not packageInfoData:
                dic_results = {'info': {'err': False, 'errInfo': "无改套餐对应资源信息！"}, 'data': []}

                return dic_results
            else:
                for cs in packageInfoData:
                    if type(cs['percentage_fs']) is decimal.Decimal:
                        cs['percentage_fs'] = float(cs['percentage_fs'])
                dic_results = {'info': {'err': False, 'errInfo': errInfo}, 'data': packageInfoData}

                return dic_results

def getSimPackageFlowerNextAPI(package_data, flower_data):
    """

    :param package_data:
    :param flower_data:
    :return:
    """
    package_info = getPackageInfo(package_set_param=package_data)
    if not package_info['data']:
        return json.dumps(package_info, sort_keys=True, indent=4, default=json_util.default)
    else:
        package_flower_data = getPackageFlower(flower_param=flower_data, package_info=package_info)

        return json.dumps(package_flower_data, sort_keys=True, indent=4, default=json_util.default)


# -*- coding: utf-8 -*-
import urllib.request, json
from bs4 import BeautifulSoup
from hashlib import md5
import pandas as pd

def make_md5(s):
    return md5(s.encode('utf-8')).hexdigest()

def get_list(path):
    df = pd.read_excel(path)

    data = {}

    for index, row in df.iterrows():
        id = row["12目録番号"]
        meta = {}
        data[id] = meta
        meta["和暦"] = row["和暦"]
        meta["西暦コード"] = row["3西暦コード"]
        meta["タイトル"] = row["タイトル"]
        meta["版種類"] = row["5版種類"]
        meta["数量"] = row["数量"]
        meta["書誌事項"] = row["7書誌事項"]
        meta["形態分類"] = row["形態分類"]
        meta["内容分類"] = row["9内容分類"]
        meta["帖数"] = row["volume"]
        meta["地名"] = row["地名"]

    return data

def getInfoFromManifest(url, collection_manifest, members):
    response = urllib.request.urlopen(url)
    response_body = response.read().decode("utf-8")
    data = json.loads(response_body.split('\n')[0])

    anno_list_url = data["sequences"][0]["canvases"][0]["otherContent"][0]["@id"];

    image_url = data["sequences"][0]["canvases"][0]["images"][0]["resource"]["service"]["@id"];

    response = urllib.request.urlopen(anno_list_url)
    response_body = response.read().decode("utf-8")
    data = json.loads(response_body.split('\n')[0])

    resources = data["resources"]

    for i in range(len(resources)):
        resource = resources[i]
        text = resource["resource"][0]["chars"]
        text = BeautifulSoup(text, "lxml").text

        o_name = text

        selector = resource["on"][0]["selector"]["default"]["value"]

        canvas_id = resource["on"][0]["full"]

        tmp = o_name.split("-")
        if len(tmp) == 6:
            o_name = tmp[0] + "-" + tmp[1] + "-" + tmp[2] + "-" + tmp[3] + "-" + tmp[4] + "_" + tmp[5]

        o_name = o_name.split("_")[0]

        n = o_name.split("-")[4]
        if not n.isdigit():
            print(text)
            continue

        if o_name not in members:
            members[o_name] = {}

        tmp = {}
        members[o_name][int(canvas_id.split("canvas/p")[1])] = tmp
        tmp["selection"] = canvas_id + "#" + selector
        tmp["label"] = o_name

        tmp["thumbnail"] = image_url + "/" + selector.split("=")[1] + "/,300/0/default.jpg"

uuid_map = {}
manifests = []

def exec2collection(org_canvas, org_label, org_manifest):
    flg = True
    page = 1

    members = {}

    while flg:
        url = endpoint + "/items?item_type=18&search=" + org_canvas + "&page=" + str(page)
        print("url:\t" + url)

        page += 1

        response = urllib.request.urlopen(url)
        response_body = response.read().decode("utf-8")
        data = json.loads(response_body.split('\n')[0])

        if len(data) > 0:
            for i in range(len(data)):

                if i % 10 == 0:
                    print(i)

                # 各アイテム
                obj = data[i]

                element_texts = obj["element_texts"]
                for e in element_texts:

                    if e["element"]["name"] == "On Canvas":
                        uuid = e["text"]

                        if uuid not in uuid_map:
                            tmp_url = endpoint + "/items?search=" + uuid

                            # print("tmp_url:\t" + tmp_url)

                            response = urllib.request.urlopen(tmp_url)
                            response_body = response.read().decode("utf-8")
                            data_t = json.loads(response_body.split('\n')[0])

                            uuid_map[uuid] = data_t[0]

                        obj_t = uuid_map[uuid]

                        id = obj_t["id"]
                        collection_id = obj_t["collection"]["id"]

                manifest = endpoint.replace("/api", "") + "/oa/items/" + str(id) + "/manifest.json"

                collection_manifest = endpoint.replace("/api", "") + "/oa/collections/" + str(
                    collection_id) + "/manifest.json"

                if manifest not in manifests:
                    getInfoFromManifest(manifest, collection_manifest, members)
                    manifests.append(manifest)

        else:
            flg = False

    # print(members)

    for o_name in members:

        # print("***\t"+o_name)

        with open(data_dir + '/template.json') as f:
            df = json.load(f)

        df["selections"] = []

        selection = {}
        df["selections"].append(selection)

        df["@id"] = "https://kunshujo.dl.itc.u-tokyo.ac.jp/data/curation/" + o_name + ".json"
        selection["@id"] = df["@id"] + "/range" + str(1)

        selection["@type"] = "sc:Range"
        selection["label"] = "Manual curation by IIIF Curation Viewer"

        selection["members"] = []

        manifest = {}
        selection["within"] = manifest
        manifest["@id"] = org_manifest
        manifest["@type"] = "sc:Manifest"
        manifest["@label"] = org_label

        count = 1

        for canvas in sorted(members[o_name]):

            member = {}

            selection["members"].append(member)

            tmp = members[o_name][canvas]

            member["@id"] = tmp["selection"]
            member["@type"] = "sc:Canvas"
            member["label"] = tmp["label"]
            member["thumbnail"] = tmp["thumbnail"]

            meta = list[member["label"]]

            metadata = []
            member["metadata"] = metadata

            description = "
"

            for key in meta:
                if meta[key] != "" and not pd.isnull(meta[key]):
                    obj = {}
                    metadata.append(obj)
                    obj["label"] = key
                    if key == "帖数":
                        obj["value"] = "『捃拾帖』第" + str(meta[key]) + "帖"
                    else:
                        obj["value"] = meta[key]

                    description += "
" + key + "	"
                    if key == "帖数":

                        tmp = member["@id"].split("/")
                        tmp_id = tmp[5]
                        page = tmp[7].split("#")[0].split("p")[1]
                        page = int(page) - 1
                        tmp_url = "https://iiif.dl.itc.u-tokyo.ac.jp/repo/s/tanaka/document/" + tmp_id + "#?cv=" + str(
                            page)

                        description += "" + "『捃拾帖』第" + str(
                            meta[key]) + "帖" + "
"
                    else:
                        description += "" + str(meta[key]) + "
"

                if key == "帖数":
                    page = "『捃拾帖』第" + str(meta[key]) + "帖"
                if key == "タイトル":
                    title = meta[key]

            df["label"] = title + "（" + page + "のうち）"

            description += "
"
            member["description"] = description

            count += 1

        with open("../../docs/data/curation/" + o_name + ".json", 'w') as outfile:
            json.dump(df, outfile, ensure_ascii=False, indent=4, sort_keys=True, separators=(',', ': '))

cflg = True
cpage = 1

data_dir = "data"

list = get_list("../" + data_dir + "/metadata.xlsx")

while cflg:

    endpoint = "https://diyhistory.org/public/omekac/api"

    curl = endpoint + "/collections?page=" + str(cpage)
    print("curl:\t" + curl)

    cpage += 1

    cresponse = urllib.request.urlopen(curl)
    cresponse_body = cresponse.read().decode("utf-8")
    cdata = json.loads(cresponse_body.split('\n')[0])

    if len(cdata) > 0:
        for i in range(len(cdata)):
            # 各アイテム
            obj = cdata[i]

            org_label = ""
            org_canvas = ""
            org_manifest = ""

            flg = False

            element_texts = obj["element_texts"]
            for e in element_texts:

                if e["element"]["id"] == 50:
                    org_label = e["text"]
                elif e["element"]["id"] == 48:
                    org_manifest = e["text"]
                    org_canvas = org_manifest.replace("manifest", "canvas")
                elif e["element"]["id"] == 38:
                    vol = int(e["text"])
                    if vol >= 16:
                        flg = True

            if flg:
                exec2collection(org_canvas, org_label, org_manifest)

    else:
        cflg = False


'''Server for communication between two or more zmq type clients.

    Working with Router-Dealer connection.
    Sending multipart messages in custom json format.
'''
import json
import sys
import sqlite3
import time
import traceback
from threading import Thread
import zmq
from enums_server import Host, Intervals
from login_server import LoginServer

class Server:
    '''Server Class where all communication is proccessed'''

    def __init__(self):
        self.context = zmq.Context.instance()
        self.recv_socket = None
        self.database = sqlite3.connect(Host.DATABASE)  # database of users and their tokens

    # Bind server socket to port and setting identity for server main socket
    def server_bind(self):
        '''Binding zmq socket to context in Server class'''
        self.recv_socket = self.context.socket(zmq.ROUTER)
        self.recv_socket.setsockopt(zmq.IDENTITY, Host.HOST)
        bind_address_rcv = 'tcp://{}:{}'.format(Host.ADDRESS, Host.PORT)
        self.recv_socket.bind(bind_address_rcv)
        print('Receiving socket bound!')

    # Receive and process the message
    def receive_message(self):
        '''Receiving message from recv_socket'''
        id_, data_raw = self.recv_socket.recv_multipart()  # recv id and raw message
        data = json.loads(data_raw)  # that needs to be processed
        to_ = data['to']
        token = data['token']
        message = data['message']
        can_do = False

        # Inspect token, if token is active you can send message
        cursor = self.database.cursor()
        cursor.execute("SELECT token FROM tokens")
        temp = cursor.fetchall()
        if any(token == row[0] for row in temp):
            print('{} sent to {}: {} token({})'.format(
                id_.decode('utf-8'), to_, message, token))
            cursor.execute(
                'UPDATE tokens SET timestamp = ? WHERE username = ? ',
                (str(round(time.time())), id_.decode('utf-8')))
            self.database.commit()
            return id_.decode('utf-8'), to_.encode(), message, True
        print('{} sent to {}: {} token({} expired)'.format(id_.decode('utf-8'),
                                                           to_, message, token))
        return id_.decode('utf-8'), to_.encode(), message, False

    # send message that has been formatted to be read on client

    def send_message(self, client_id, client_to, client_message):
        '''Sending message to target client'''
        data = {'id': client_id, 'message': client_message}
        message = json.dumps(data).encode()
        send_data = [client_to, message]
        self.recv_socket.send_multipart(send_data)

    @staticmethod
    def delete_token():
        '''Token deletion method'''
        db_name = Host.DATABASE
        while True:
            database = sqlite3.connect(db_name)
            cursor = database.cursor()
            cursor.execute("SELECT * FROM tokens")
            tokens_select = cursor.fetchall()
            for tmp in tokens_select:
                user = tmp[0]
                token_time = tmp[2]
                if round(time.time()) - int(
                        token_time) >= Intervals.TOKEN_EXPIRATION:
                    cursor.execute("DELETE FROM tokens WHERE username = ?",
                                   (user,))
                    database.commit()
            cursor.close()
            database.close()
            time.sleep(Intervals.TOKEN_CHECK_INTERVAL)
            continue

    def save_message_to_base(self, client_id, client_to, client_message):
        '''Saving currently received message to database'''
        cursor = self.database.cursor()
        cursor.execute("INSERT INTO history VALUES (?,?,?,?)",
                       (str(time.asctime(time.localtime(time.time()))),
                        client_id, client_to.decode(), client_message))
        self.database.commit()
        cursor.close()

    def server_run(self):
        '''Main server program, running all functionalities'''
        try:
            self.server_bind()
            poller = zmq.Poller()
            poller.register(self.recv_socket, zmq.POLLIN)

            login_server = LoginServer()  # login server object on port 5557
            login_server.start()
            time.sleep(1)  # wait for login server to finish creating database tables

            delete_token = Thread(target=self.delete_token, daemon=True)
            delete_token.start()
            # if message is received process it, if not, try again

            while True:
                events = dict(poller.poll(timeout=Intervals.POLL_IN_TIME))
                while True:
                    if self.recv_socket in events:
                        id_, to_, new_message, send = self.receive_message()
                        # if token is OK, send message to targeted receiver
                        if not new_message:
                            break
                        elif send:
                            self.send_message(id_, to_, new_message)
                            self.save_message_to_base(id_, to_, new_message)
                        # if token isn't OK, return token_expired message to sender
                        else:
                            self.send_message(id_, id_.encode(), 'Your token expired!')
                    else:
                        break

        except(KeyboardInterrupt, SystemExit) as ex:
            # if we get KeyboardInterupt,SystemExit or some ZMQError we delete tokens table
            cursor = self.database.cursor()
            cursor.execute('DROP TABLE IF EXISTS tokens')
            cursor.close()
            self.database.close()
            self.context.destroy()
            print('\nKeyboard Interupt ! Main Server closing...')
            sys.exit(0)
        except (zmq.ZMQError):
            cursor = self.database.cursor()
            cursor.execute('DROP TABLE IF EXISTS tokens')
            cursor.close()
            self.database.close()
            self.context.destroy()
            print('ZMQError occured, Closing Main Server...')
            sys.exit(0)
        except Exception as ex:
            print("Not handled: ", type(ex).__name__)
            print(traceback.format_exc())


import os.path
import docker
DOCKER_CLIENT = docker.from_env()
ROOT_PATH = os.path.dirname(os.path.abspath(__file__))  # this is where the constants.py file should be
DOCKER_IMAGE_PATH = ROOT_PATH
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
MIN_SUBMISSION_INTERVAL_SECONDS = 10  # the minimum time a user must wait between submissions
MAX_TEST_RUN_SECONDS = 5  # the maximum time a Submission can run
TESTS_FOLDER_NAME = 'challenge_tests'  # the name of the folder which holds tests for challenges

CHALLENGES_APP_FOLDER_NAME = 'challenges'
GRADER_FILE_NAME = 'grader.py'  # the file which is sent over to the docker container and executed there

BASE_USER_ROLE_NAME = 'User'

RUSTLANG_NAME = 'Rust'
PYTHONLANG_NAME = 'Python'
CPPLANG_NAME = 'C++'
GOLANG_NAME = 'Go'
KOTLIN_NAME = 'Kotlin'
RUBY_NAME = 'Ruby'

SUBMISSION_MINIMUM_TIMED_OUT_PERCENTAGE = 40  # the minimum percentage of timed out tests a submission needs to have for the whole submission to be considered 'timed_out'

# Keys for the object returned by the grader's AsyncResult function
GRADER_TEST_RESULTS_RESULTS_KEY = 'results'
GRADER_TEST_RESULT_SUCCESS_KEY = 'success'
GRADER_TEST_RESULT_TIME_KEY = 'elapsed_seconds'
GRADER_TEST_RESULT_DESCRIPTION_KEY = 'description'
GRADER_TEST_RESULT_TRACEBACK_KEY = 'traceback'
GRADER_TEST_RESULT_ERROR_MESSAGE_KEY = 'error_message'
GRADER_COMPILE_FAILURE = 'COMPILATION FAILED'
GRADER_TEST_RESULT_TIMED_OUT_KEY = 'timed_out'

RUSTLANG_TIMEOUT_SECONDS = 5
RUSTLANG_ERROR_MESSAGE_SNIPPET = 'error: aborting due to previous error'
RUSTLANG_ERROR_MESSAGE_SNIPPET_2 = 'error: incorrect close delimiter'
RUSTLANG_UNFRIENDLY_ERROR_MESSAGE = "note: Run with `RUST_BACKTRACE=1`"  # error message that is of no interest to the user
RUSTLANG_FILE_EXTENSION = '.rs'
RUSTLANG_COMPILE_ARGS = ['rustc']
RUSTLANG_RUN_ARGS = []

CPP_TIMEOUT_SECONDS = 4
CPP_FILE_EXTENSION = '.cpp'
CPP_COMPILE_ARGS = ['g++', '-std=c++11', '-o']
CPP_RUN_ARGS = []

PYTHON_TIMEOUT_SECONDS = 5
PYTHON_FILE_EXTENSION = '.py'
PYTHON_RUN_COMMAND = 'python3'

RUBY_TIMEOUT_SECONDS = 5
RUBY_FILE_EXTENSION = '.rb'
RUBY_RUN_COMMAND = 'ruby'

GO_TIMEOUT_SECONDS = 4
GO_FILE_EXTENSION = '.go'
GO_COMPILE_ARGS = ['go', 'build']
GO_RUN_ARGS = []

KOTLIN_TIMEOUT_SECONDS = 4
KOTLIN_COMPILE_ARGS = ['kotlinc']
KOTLIN_FILE_EXTENSION = '.kt'
KOTLIN_RUN_ARGS = ['java', '-jar']


#Importing all the necessary files
import requests
from bs4 import BeautifulSoup
from nltk.tokenize import word_tokenize
import HKF

def logic(product,budget):

    #defining necessary variables
    page=1
    i=1
    max_pages=1

    #defining path and standard url pattern
    path='D:/urvesh/Project/Program/2.csv'
    url='https://www.flipkart.com/search?as=off&as-show=on&otracker=start&page='+str(page)+'&q='+str(product)+'&viewType=grid'

    #scraping url into text format
    source_code=requests.get(url)
    text=source_code.text
    soup=BeautifulSoup(text,'lxml')

    #for finding no. of pages each search may consist
    for max_p in soup.findAll(['span','a'], {'class': "_3v8VuN"}):
        pages=max_p.text
        p=word_tokenize(pages)
        p1=str(p[-1])
        max_pages=int(p1.replace(',',''))

    #main loop starts
    while page<=max_pages:

        #printing oage no. and incrementing page no.
        print('Page no.: ' + str(page))
        url='https://www.flipkart.com/search?as=off&as-show=on&otracker=start&page='+str(page)+'&q='+product+'&viewType=grid'
        page+=1

        #scraping url
        source_code=requests.get(url)
        text=source_code.text
        soup=BeautifulSoup(text,'lxml')

        #outer loop for finding products
        for prod_details in soup.findAll('div',{'class':'_3liAhj'}):
##            for img in prod_details.findAll('img',{'class' : '_1Nyybr _30XEf0'}):
####                image=img.get('src')
##
            #this loop extracts titles and links from the web page
            for details in prod_details.findAll('a',{'class' : '_2cLu-l'}):

                title=details.get('title')
                link=details.get('href')
                link='https://www.flipkart.com' + str(link)

                #this loop is extracts all types of price details in the web page
                for price in prod_details.findAll('div',{'class' : '_1uv9Cb'}):
                    for dp in price.findAll('div',{'class' : ['_1vC4OE','_1vC4OE _2rQ-NK']}):
                            #replacing unnecessary things from deal_price like rupee symbol and comma
                            deal_price=int(dp.text.replace(u'\u20b9','').replace(',',''))

                            #checking necessary condition
                            if(deal_price <= budget):

                                for np in price.findAll('div',{'class' : '_3auQ3N'}):
                                    for dis in price.findAll('div',{'class' : 'VGWI6T'}):

                                        #replacing unnecessary things from norm_price like rupee symbol and comma
                                        norm_price=np.text.replace(u'\u20b9','').replace(',','')
                                        ##replacing unnecessary things from discount like off and %
                                        discount=word_tokenize(dis.text)
                                        discount=discount[0]

                                        #printing everything
                                        print('\nProduct id :' + str(i))
                                        print('Name of the product : ' + title)
                                        print('Discount : '+ str(discount) +'%')
                                        print('Deal Price : Rs. ' + str(deal_price))
                                        print('Normal Price : Rs. ' + str(norm_price))
                                        print('Product link : '+ link+'\n')

                                        #merging every variable and converting into excel sheet (.csv format)
                                        info=str(str(i)+','+str(title)+','+str(deal_price)+','+str(norm_price)+',')
                                        HKF.write_file(path,link,info)

                                        #Incrementing product id
                                        i+=1
                            else:
                            #do nothing
                               pass


import numpy as np
from pandas import read_pickle

EXAMPLES_PATH = 'examples.pkl'
EXAMPLES2_PATH = 'examples2.pkl'
MAX_ITERS = 20000
DEBUG_MODE = False

class Example:
    def __init__(self, S: np.ndarray, K: np.ndarray, k: int):
        self.k = k
        self.S = S
        self.K = K

def ones_k_band_lower(n: int, k: int):
    return np.triu(np.tril(np.ones((n, n)), 0), -k)

def target(S: np.ndarray, K: np.ndarray) -> float:
    """
    Original target function for minimization
    :param S: The given S matrix
    :param K: a k-banded pd matrix
    :return: the target value (the one we should minimize)
    """
    return np.trace(S @ K) - np.log(np.linalg.det(K))

def triangular_target(S: np.ndarray, L: np.ndarray) -> float:
    """
    Returns the target value for the L triangular matrix (where K = L @ L.T)
    :param S: The given S matrix
    :param L: Our triangular L k-band matrix
    :return: the target value
    """
    return (L.T * (S @ L).T).sum() - np.sum(np.log(np.power(np.diag(L), 2)))

def target_gradient(S: np.ndarray, L: np.ndarray) -> float:
    """
    C alcultes the gradient of the target problem (in it's cholesky form)
    :param S: The given S matrix
    :param L: Our triangular L k-band matrix
    :return: the target gradient value
    """
    log_mat = np.diag(1 / (np.diag(L)))
    return 2 * (S @ L - log_mat)

def solve(S: np.ndarray, k: int):
    """
    The main function of the program - this will be the function that the tests run
    :param S: The given S matrix
    :param k: The k-band parameter
    :return: A k-banded pd matrix K that minimizes the target function
    """
    return solve_problem_with_parameters(S, k, alpha=0.4, beta=0.5)

def solve_problem_with_parameters(S, k, alpha, beta):
    """
    Gives a solution to the target problem with given parameters for the gradient decent
    :param S: The given S matrix
    :param k: The k-band parameter
    :param alpha: the alpha parameter for the gradient decent method
    :param beta: the beta parameter for the gradient decent method
    :return:
    """
    sub_L_array = []
    sub_problem_size = k + 1
    n = S.shape[0]
    for i in range(0, n):
        if i + k >= n:
            break
        sub_S = S[i: min(n, i + sub_problem_size), i: min(n, i + sub_problem_size)]
        sub_L = solve_helper(sub_S, k, alpha, beta)
        sub_L_array.append(sub_L)

    return concat_sub_L_array(sub_L_array, sub_problem_size, n)

def concat_sub_L_array(sub_L_array, size, n):
    """
    Gets an array of sub_solution matrix's and concat them to one K solution -
    The sub-solutions are k-banded and pd and so is the concatenated K.
    :param sub_L_array:
    :param size:
    :param n:
    :return: K
    """
    concatenated_L = np.zeros((n, n))
    for i, sub_L in enumerate(sub_L_array):
        concatenated_L[i: min(n, i + size), i: min(n, i + size)] = sub_L

    return concatenated_L @ concatenated_L.T

def solve_helper(sub_S: np.ndarray, k: int, alpha: float, beta: float) -> np.ndarray:
    """
    Solves each given sub-problem using initial guess and gradient descent.
    :param sub_S: The given S matrix
    :param k: The k-band parameter
    :param alpha: the alpha parameter for the gradient decent method
    :param beta: the beta parameter for the gradient decent method
    :return: a triangular L matrix such that L @ L.T is the optimal K matrix (for the sub matrix of S)
    """
    try:
        # Best guess if sub_S is non-singular
        initial_L = np.linalg.cholesky(np.linalg.inv(sub_S)) * ones_k_band_lower(sub_S.shape[0], k)
    except:
        # Good guess if sub_S is singular
        initial_L = (1 / sub_S) * ones_k_band_lower(sub_S.shape[0], k)

    return gradient_decent(sub_S, initial_L, k, alpha, beta)

def gradient_decent(sub_S: np.ndarray, sub_L: np.ndarray, k: int, alpha: float, beta: float, max_iterations=MAX_ITERS):
    """
    Tries to find global minimum with a gradient decent implementation
    :param sub_S: The given sub_S
    :param sub_L: The triangular pd k-band sub_L s.t sub_L @ sub_L.T = sub_K
    :param k: k-band parameter
    :param alpha: determines how much the norma of the gradient will get considered
    :param beta:  determines how fast the step-size will reduce
    :param max_iterations: maximal number of iterations we will run
    :return: approximation for the arg min of the sub-problem
    """
    ones_k_band_lower_mat = ones_k_band_lower(sub_S.shape[0], k)
    threshold = 0.001
    prev_value = triangular_target(sub_S, sub_L)
    for i in range(max_iterations):
        grad = target_gradient(sub_S, sub_L) * ones_k_band_lower_mat
        step_size = line_search(triangular_target, sub_S, sub_L, grad, alpha, beta)
        sub_L -= step_size * grad
        if i % 100 == 0 or i == 1:
            current_value = triangular_target(sub_S, sub_L)
            if abs(current_value - prev_value) < threshold:
                if DEBUG_MODE:
                    print(f'number of iterations was {i + 1}')
                break
            prev_value = current_value

    else:
        if DEBUG_MODE:
            print(f'number of iterations was {max_iterations}')

    return sub_L

def line_search(f, S, L, grad, alpha, beta):
    t = 1
    cur_target_val = f(S, L)
    grad_norm = np.linalg.norm(grad) ** 2
    while f(S, L - t * grad) > cur_target_val - alpha * t * grad_norm:
        t *= beta
    return t

def run_solve_on_given_exmaples(examples_path):
    examples = read_pickle(examples_path)
    for i in range(len(examples)):
        S, K, k = examples[i].S, examples[i].K, examples[i].k
        our_K = solve(S, k)
        print(f"error {i}: {np.linalg.norm(our_K - K) / (k * S.shape[0])}")
        print("Opt: ", target(S, K))
        print("Our: ", target(S, our_K))

if __name__ == '__main__':
    run_solve_on_given_exmaples(EXAMPLES2_PATH)


from django.conf.urls import patterns, include, url

from django.contrib import admin
admin.autodiscover()

from rest_framework import routers
from cursos import views

router = routers.DefaultRouter()
router.register(r'clases', views.ClaseViewSet)

urlpatterns = patterns('',
	# Examples:
	url(r'^$', 'cursos.views.home', name='home'),
	url(r'^home/$', 'cursos.views.home_user', name='home_user'),
	url(r'^login/$', 'cursos.views.loginView', name='login'),
	url(r'^logout/$', 'cursos.views.logoutView', name='logout'),
	# url(r'^blog/', include('blog.urls')),
	url(r'^cursos/$', 'cursos.views.cursos', name="cursos"),
	url(r'^estadisticas/$', 'cursos.views.estadisticas', name="estadisticas"),
	url(r'^cursos/(?P<materia_id>\d+)/$', 'cursos.views.materia', name='materia'),
	url(r'^cursos/(?P<materia_id>\d+)/clase/(?P<clase_id>\d+)/$', 'cursos.views.clase', name='clase'),
	url(r'^cursos/(?P<materia_id>\d+)/foro/$', 'cursos.views.foro', name='foro'),
	url(r'^cursos/(?P<materia_id>\d+)/material/$', 'cursos.views.material', name='material'),
	url(r'^configuraciones/$', 'cursos.views.configuraciones', name='configuraciones'),
	url(r'^cursos/(?P<materia_id>\d+)/foro/(?P<pregunta_id>\d+)/$', 'cursos.views.pregunta', name='pregunta'),
	url(r'^cursos/(?P<materia_id>\d+)/clase/(?P<clase_id>\d+)/actividades/$', 'cursos.views.actividades', name='actividades'),
	url(r'^cursos/(?P<materia_id>\d+)/clase/(?P<clase_id>\d+)/material_de_clase/$', 'cursos.views.material_de_clase', name='material_de_clase'),
	url(r'^cursos/(?P<materia_id>\d+)/clase/(?P<clase_id>\d+)/tareas/$', 'cursos.views.tareas', name='tareas'),
	url(r'^perfil/(?P<user_id>\d+)/$','cursos.views.perfil', name='perfil'),
	url(r'^admin/', include(admin.site.urls)),
    url(r'^api/', include(router.urls)),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
)

from django.conf import settings

if settings.DEBUG:
    urlpatterns += patterns('',
        url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),
    )

handler404 = 'cursos.views.handler404'


"""
Inserts metadata and figures into the report template.
"""
import logging
import subprocess

from bokeh.embed import components
from bokeh.layouts import gridplot
from jinja2 import (Environment, DebugUndefined, PackageLoader,
                    select_autoescape, Template)

from solarforecastarbiter.reports import figures

logger = logging.getLogger(__name__)

def template_report(report, metadata, metrics,
                    processed_forecasts_observations):
    """
    Render the markdown report template. Figures are left untemplated.

    Parameters
    ----------
    report : solarforecastarbiter.datamodel.Report
        Metadata describing report
    metadata : solarforecastarbiter.datamodel.ReportMetadata
        Describes the pre-report
    metrics : tuple of dict
    processed_forecasts_observations : tuple of solarforecastarbiter.datamodel.ProcessedForecastObservation

    Returns
    -------
    markdown
    """  # noqa
    # By default, jinja removes undefined variables from the rendered string.
    # DebugUndefined leaves undefined variables in the string so that they
    # can be used in the full report template process.
    env = Environment(
        loader=PackageLoader('solarforecastarbiter.reports', 'templates'),
        autoescape=select_autoescape(['html', 'xml']),
        undefined=DebugUndefined)

    template = env.get_template('template.md')

    script_metrics, data_table_div, figures_dict = _metrics_script_divs(
        report, metrics)

    strftime = '%Y-%m-%d %H:%M:%S %z'

    rendered = template.render(
        name=metadata.name,
        start=metadata.start.strftime(strftime),
        end=metadata.end.strftime(strftime),
        now=metadata.now.strftime(strftime),
        proc_fx_obs=processed_forecasts_observations,
        validation_issues=metadata.validation_issues,
        versions=metadata.versions,
        script_metrics=script_metrics,
        tables=data_table_div,
        **figures_dict)
    return rendered

def _metrics_script_divs(report, metrics):
    cds = figures.construct_metrics_cds(metrics, 'total', index='forecast')
    data_table = figures.metrics_table(cds)

    figures_bar = []
    for num, metric in enumerate(report.metrics):
        fig = figures.bar(cds, metric)
        figures_bar.append(fig)

    script, (data_table_div, *figures_bar_divs) = components((data_table,
                                                              *figures_bar))

    script_month, figures_bar_month_divs = _loop_over_metrics(report, metrics,
                                                              'month')

    script_day, figures_bar_day_divs = _loop_over_metrics(report, metrics,
                                                          'day')

    script_hour, figures_bar_hour_divs = _loop_over_metrics(report, metrics,
                                                            'hour')

    script_metrics = script + script_month + script_day + script_hour

    figures_dict = dict(
        figures_bar=figures_bar_divs,
        figures_bar_month=figures_bar_month_divs,
        figures_bar_day=figures_bar_day_divs,
        figures_bar_hour=figures_bar_hour_divs)

    return script_metrics, data_table_div, figures_dict

def _loop_over_metrics(report, metrics, kind):
    figs = []
    # series with MultiIndex of metric, forecast, day
    # JSON serialization issues if we don't drop or fill na.
    # fillna ensures 0 - 24 hrs on hourly plots.
    metrics_series = figures.construct_metrics_series(metrics, kind).fillna(0)
    for num, metric in enumerate(report.metrics):
        cds = figures.construct_metrics_cds2(metrics_series, metric)
        # one figure with a subfig for each forecast
        fig = figures.bar_subdivisions(cds, kind, metric)
        figs.append(gridplot(fig, ncols=1))
    script, divs = components(figs)
    return script, divs

# not all args currently used, but expect they will eventually be used
def add_figures_to_report_template(fx_obs_cds, metadata, report_template,
                                   html=True):
    """
    Add figures to the report_template

    Parameters
    ----------
    fx_obs_cds : list
        List of (forecast, observation, ColumnDataSource) tuples to
        pass to bokeh plotting objects.
    report : solarforecastarbiter.datamodel.Report
        Metadata describing report
    report_template : str, markdown
        The templated report
    html : bool
        Indicates if the template will be rendered into html or pdf.

    Returns
    -------
    body : str, markdown
    """
    body_template = Template(report_template)

    ts_fig = figures.timeseries(fx_obs_cds, metadata.start, metadata.end,
                                timezone=metadata.timezone)
    scat_fig = figures.scatter(fx_obs_cds)
    try:
        script, div = components(gridplot((ts_fig, scat_fig), ncols=1))
    except Exception:
        logger.exception(
            'Failed to make Bokeh items for timeseries and scatterplot')
        script = ''
        div = """
::: warning
Failed to make timeseries and scatter figure from stored data. Try
generating report again.
:::
"""

    body = body_template.render(
        script_data=script,
        html=html,
        figures_timeseries_scatter=div)
    return body

def report_md_to_html(report_md):
    """
    Render markdown into simple html using pandoc.

    Parameters
    ----------
    report_md : str, markdown

    Returns
    -------
    str, html
    """
    try:
        out = subprocess.run(args=['pandoc', '--from', 'markdown+pipe_tables'],
                             input=report_md.encode(), capture_output=True)
    except (FileNotFoundError, subprocess.CalledProcessError) as e:
        raise OSError('Error converting prereport to html using pandoc') from e
    return out.stdout.decode()

def full_html(body):
    """Create full html file.

    The Solar Forecast Arbiter dashboard will likely use its own
    templates for rendering the full html.

    Parameters
    ----------
    body : html

    Returns
    -------
        head : str, html
        Header for the full report.
    """
    env = Environment(
        loader=PackageLoader('solarforecastarbiter.reports', 'templates'),
        autoescape=select_autoescape(['html', 'xml']))
    base_template = env.get_template('base.html')

    base = base_template.render(body=body)

    return base


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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('podcasts', '0005_auto_20160305_1300'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='podcast',
            name='pdf',
        ),
        migrations.AddField(
            model_name='podcast',
            name='file_download',
            field=models.CharField(max_length=255, verbose_name='Link de descarga', blank=True),
        ),
    ]


# Python 3, Tkinter 8.6
# Display Entry in a Label

from tkinter import *

root = Tk()

def returnEntry(arg=None):
    """Gets the result from Entry and return it to the Label"""

    result = myEntry.get()
    resultLabel.config(text=result)
    myEntry.delete(0, END)

# Create the Entry widget
myEntry = Entry(root, width=20)
myEntry.focus()
myEntry.bind("<Return>", returnEntry)
myEntry.pack()

# Create the Enter button
enterEntry = Button(root, text="Enter", command=returnEntry)
enterEntry.pack(fill=X)

# Create and emplty Label to put the result in
resultLabel = Label(root, text="")
resultLabel.pack(fill=X)

root.geometry("+750+400")

root.mainloop()


import numpy as np

from .base import MultivariateOutlierGenerator

class MultivariateShiftProportionShiftGenerator(MultivariateOutlierGenerator):
    def __init__(self, timestamps=None, factor_range=(-.995, -0.98)):
        if factor_range[0] >= factor_range[1]:
            raise ValueError('First value must be less than second value')
        timestamps = timestamps or []
        self.timestamps = timestamps
        self.min_factor = factor_range[0]
        self.factor_width = factor_range[1] - factor_range[0]

    def add_outliers(self, timeseries):
        additional_values = np.zeros(timeseries.size)
        for start, end in self.timestamps:
            random_factor = np.random.random() * self.factor_width + self.min_factor
            additional_values[list(range(start, end))] += (random_factor * timeseries[list(range(start, end))])
        return additional_values


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.parameter import Parameter

class Tree(nn.Module):

    def __init__(self, depth, num_classes=10):
        super(Tree, self).__init__()

        self.depth = depth
        self.num_classes = num_classes

        self.num_leaf = 2 ** (depth - 1)
        self.num_split = self.num_leaf - 1

        # leaf node distributions
        self.pi = Parameter(torch.rand((self.num_leaf, self.num_classes)))

    def forward(self, x):

        assert x.ndimension() == 2
        assert x.min() >= 0 and x.max() <= 1
        bs = x.size(0)
        assert x.size(1) == self.num_split, "%d vs %d" % (x.size(1), self.num_split)

        x = x.unsqueeze(dim=2)
        probabilities = torch.cat((x, 1-x), dim=2) # [bs, num_split, 2]

        start, end = 0, 1
        if x.is_cuda:
            tmp = torch.ones((bs, 1, 1)).cuda()
        else:
            tmp = torch.ones((bs, 1, 1))

        for d in range(self.depth-1):
            tmp = tmp.view(bs, -1, 1).repeat(1, 1, 2)
            prob = probabilities[:, start:end, :]
            tmp = tmp * prob
            start = end
            end = start + 2 ** (d + 1)

        tmp = tmp.view(bs, self.num_leaf)

        return torch.mm(tmp, F.softmax(self.pi, dim=1))


import sys

def main(argv):
    if ( len(argv) != 2 ):
        print ("Usage: python config.py --routefile=<name of route file> --sessionfile=<name of session file>")
        return 1

    routefile = ""
    sessionfile = ""
    for arg in argv:
        if ( arg.find("--routefile=") != -1 ):
            routefile = arg.split("--routefile=")[1]
        elif ( arg.find("--sessionfile=") != -1 ):
            sessionfile = arg.split("--sessionfile=")[1]

    # Check that files are found
    if ( routefile == "" ):
        print ("Did not find any routefile...")
    if ( sessionfile == "" ):
        print ("Did not find any sessionfile...")
    out = open("globalParameters.py", 'w')
    out.write("ROUTE_FILE=\"%s\"\n"%(routefile))
    out.write("SESSION_FILE=\"%s\"\n"%(sessionfile))
    out.close()

if __name__ == "__main__":
    main(sys.argv[1:])


""" Bits and pieces for checking alpha """

# imports

import numpy as np

from ulmo import io as ulmo_io
from ulmo.plotting import plotting
from ulmo.utils import image_utils
from ulmo.analysis import fft

def gen_image(U0, U1, outfile):
    # Load
    tbl_file = '/data/Projects/Oceanography/AI/OOD/SST/MODIS_L2/Tables/MODIS_SSL_96clear_v4_DT1.parquet'
    modis_tbl = ulmo_io.load_main_table(tbl_file)

    # Find it
    i_large = np.argmin(np.abs((modis_tbl.US0-U0)**2 + (modis_tbl.US1-U1)**2))
    large_cutout = modis_tbl.iloc[i_large]
    large_img = image_utils.grab_image(large_cutout)

    # Plot it
    plotting.show_image(large_img, show=True)

    # Save it
    np.save(outfile, large_img)
    print(f'Wrote: {outfile}')

def gen_images_for_peter():
    # Large
    U0, U1 = 1, -1.8
    outfile = 'large_cutout.npy'
    gen_image(U0, U1, outfile)

    # Small
    U0, U1 = 3, 2.
    outfile = 'small_cutout.npy'
    gen_image(U0, U1, outfile)

if __name__ == '__main__':
    gen_images_for_peter()


import torch

class ComposedLoss:

    def __init__(self):
        self._composed_loss = 0
        self._bce_loss = 0
        self._mse_loss = 0
        self._kl_loss = 0
        self._batch_no = 1
        self._tag = ''

        self._get_bce_loss = torch.nn.BCELoss(reduction='sum')
        self._get_mse_loss = torch.nn.MSELoss(reduction='sum')

    def zero(self):
        self._composed_loss = 0
        self._bce_loss = 0
        self._mse_loss = 0
        self._kl_loss = 0
        self._batch_no = 1

    def add_batch(self, segms=None, uvae_recon=None, enc_evae=None, feats=None, evae_recon=None, get_batch_loss=False):
        self._batch_no += 1
        kl, mse, bce = self._get_losses(feats, segms, uvae_recon, evae_recon, enc_evae)

        self._bce_loss += bce
        self._kl_loss += kl
        self._mse_loss += mse
        self._composed_loss += (kl + mse + bce)
        if get_batch_loss:
            # for trainings backprop
            return kl + mse + bce

    def get_loss(self):
        loss = self._composed_loss / self._batch_no
        return loss

    def _get_losses(self, feats, segms, uvae_recon, evae_recon, enc_evae):
        bce = self._get_bce_loss(evae_recon, segms)  # reconstruction loss evae
        mse = self._get_mse_loss(uvae_recon[0], feats)  # reconstruction loss uvae
        kl = self._get_kl_loss(enc_evae[0], enc_evae[1], uvae_recon[1], uvae_recon[2])

        return kl, mse, bce

    @staticmethod
    def _get_kl_loss(mu_p, logvar_p, mu_q, logvar_q):
        # distribution q is matched to p

        q = torch.distributions.Normal(mu_q, torch.exp(logvar_q * 0.5))
        p = torch.distributions.Normal(mu_p, torch.exp(logvar_p * 0.5))

        loss = torch.distributions.kl.kl_divergence(p, q)
        loss = torch.sum(loss)

        return loss


import csv
import cv2
import numpy as np
import matplotlib.pyplot as plt

from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, Cropping2D, Dropout
from keras.layers.convolutional import Convolution2D
from keras.layers.pooling import MaxPooling2D

lines = []

with open('data/driving_log.csv') as csvfile:
    reader = csv.reader(csvfile)
    for line in reader:
        lines.append(line)

images = []
measurements = []
correction = 0.2

#Using all three Kameras

for line in lines:
    for i in range(3):
        source_path = line[i]
        filename = source_path.split('/')[-1]
        #print(filename)
        current_path = 'data/IMG/' + filename
        #print(current_path)
        #image = cv2.imread(current_path)
        # Read image and convert from BGR to RGB
        #The data has 3 dimensions: width, height, and color. ::-1 effectively reverses the order of the colors.
        #The width and height are not affected
        image = cv2.imread(current_path)[:,:,::-1]  # Read image and convert from BGR to RGB
        images.append(image)

    measurement = float(line[3])
    measurements.append(measurement)
    measurements.append(measurement + correction)
    measurements.append(measurement - correction)
    #print (measurements)

augumented_images, augumented_measurements = [], []

#FLipped Images

for image, measuerement in zip(images, measurements):
    augumented_images.append(image)
    augumented_measurements.append(measuerement)
    augumented_images.append(cv2.flip(image,1))
    augumented_measurements.append(measuerement*-1.0)

X_train = np.array(augumented_images)
y_train = np.array(augumented_measurements)

#Nvidia Network
model = Sequential()
model.add(Lambda(lambda x: (x / 255.0) - 0.5, input_shape=(160,320,3)))
model.add(Cropping2D(cropping=((70, 25), (1, 1))))

model.add(Convolution2D(24,5,5, subsample=(2,2), activation="relu"))
model.add(Convolution2D(36,5,5, subsample=(2,2), activation="relu"))
model.add(Convolution2D(48,5,5, subsample=(2,2), activation="relu"))
model.add(Convolution2D(64,3,3, activation="relu"))
model.add(Convolution2D(64,3,3, activation="relu"))
model.add(Flatten())
model.add(Dense(100))
model.add(Dense(50))
model.add(Dense(10))
model.add(Dense(1))

model.compile(loss='mse', optimizer='adam')
history = model.fit(X_train, y_train, validation_split=0.2, shuffle=True, nb_epoch=5, verbose=1)

model.save('model.h5')
'''
# Output visualization
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model MSE loss')
plt.ylabel('MSE loss')
plt.xlabel('Epoch')
plt.legend(['Training set', 'Validation set'], loc='upper right')
plt.show()
'''


#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf, Link
from subprocess import call

def myNetwork():

    net = Mininet(controller=RemoteController, link=TCLink, switch=OVSKernelSwitch)

    info( '*** Adding controller\n' )
    c0=net.addController(name='c0',
                      controller=RemoteController,
                      ip='127.0.0.1',
                      protocol='tcp',
                      port=6633)

    info( '*** Add switches\n')
    s1 = net.addSwitch('s1')
    s2 = net.addSwitch('s2')
    #s3 = net.addSwitch('s3')
    info( '*** Add hosts\n')
    client1 = net.addHost('client1', dpid="0000000000000002")
    router = net.addHost('router', dpid="0000000000000500")
    server = net.addHost('server', dpid="00000000000001000")
    #h3 = net.addHost('h3')

    info( '*** Add links\n')
    """net.addLink(h1, h2)
    net.addLink(h2, s1)
    net.addLink(h2, s2)
    net.addLink(s2, h3)
    net.addLink(h3, s1)"""
    Link(client1, s1, intfName1='c1-eth1', intfName2='s1-eth1')
    Link(client1, s2, intfName1='c1-eth2', intfName2='s2-eth1')
    Link(s1, router, intfName1='s1-eth0', intfName2='r-eth1')
    Link(s2, router, intfName1='s2-eth0', intfName2='r-eth2')
    Link(router, server, intfName1='r-eth0', intfName2='server-eth0')
    Link(router, server, intfName1='r-eth10', intfName2='server-eth1')
    #h1.cmd('ifconfig h1-eth1 10.0.1.1 netmask 255.255.255.0')
    #h1.cmd('ifconfig h1-eth2 10.0.2.1 netmask 255.255.255.0')
    #h2.cmd('ifconfig h2-eth0 10.0.100.1 netmask 255.255.255.0')
    #s1.cmd('ifconfig s1-eth0 10.0.1.2 netmask 255.255.255.0')
    #s1.cmd('ifconfig s1-eth1 10.0.1.3 netmask 255.255.255.0')
    #s2.cmd('ifconfig s2-eth0 10.0.2.2 netmask 255.255.255.0')
    #s2.cmd('ifconfig s2-eth1 10.0.2.3 netmask 255.255.255.0')

    info( '*** Starting network\n')
    net.build()
    info( '*** Starting controllers\n')
    for controller in net.controllers:
        controller.start()

    #info( '*** Starting switches\n')
    net.get('s1').start([c0])
    net.get('s2').start([c0])
    #net.get('s3').start([c0])

    #info( '*** Post configure switches and hosts\n')
    client1.cmd('ifconfig c1-eth1 10.0.1.1 netmask 255.255.255.0')
    client1.cmd('ifconfig c1-eth2 10.0.2.1 netmask 255.255.255.0')
    client1.cmd('ip route add 100.100.1.0/24 via 10.0.1.200 dev c1-eth1')
    client1.cmd('ip route add 100.100.2.0/24 via 10.0.2.200 dev c1-eth2')
    router.cmd('sysctl net.ipv4.ip_forward=1')
    router.cmd('ifconfig r-eth1 10.0.1.200 netmask 255.255.255.0')
    router.cmd('ifconfig r-eth2 10.0.2.200 netmask 255.255.255.0')
    router.cmd('ifconfig r-eth0 100.100.1.10 netmask 255.255.255.0')
    router.cmd('ifconfig r-eth10 100.100.2.10 netmask 255.255.255.0')
    router.cmd('ip route add 10.0.1.0/24 dev r-eth1')
    router.cmd('ip route add 10.0.2.0/24 dev r-eth2')
    router.cmd('ip route add 100.100.1.0/24 dev r-eth0')
    router.cmd('ip route add 100.100.2.0/24 dev r-eth10')
    server.cmd('ifconfig server-eth0 100.100.1.1 netmask 255.255.255.0')
    server.cmd('ifconfig server-eth1 100.100.2.1 netmask 255.255.255.0')
    server.cmd('ip route add 10.0.1.0/24 via 100.100.1.10 dev server-eth0')
    server.cmd('ip route add 10.0.2.0/24 via 100.100.2.10 dev server-eth1')
    #h1.cmd('ifconfig h1-eth2 10.0.2.1 netmask 255.255.255.0')
    #h2.cmd('ifconfig h2-eth0 10.0.100.1 netmask 255.255.255.0')
    #h1.cmd('ifconfig h1-eth1 10.0.1.1 netmask 255.255.255.0')
    #h1.cmd('ifconfig h1-eth2 10.0.2.1 netmask 255.255.255.0')
    #h2.cmd('ifconfig h2-eth0 10.0.100.1 netmask 255.255.255.0')
    #net.startTerms()
    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()


import time
import math

import matplotlib.pyplot as plt

from robot import Robot

start_scan =0
lower_bound = -90
upper_bound = 90
scan_step = 5

the_robot = Robot()
the_robot.set_tilt(0)

scan_data = {}
# Make the sensor scan
for facing in range(lower_bound, upper_bound, scan_step):
    the_robot.set_pan(-facing)
    time.sleep(0.1)
    scan_data[facing] = the_robot.left_distance_sensor.distance * 100
# make plot
axis = [math.radians(facing) for facing in scan_data.keys()]
print(axis)
print(scan_data.values())
plt.polar(axis, list(scan_data.values()), 'g-')
# dump to png
plt.savefig("scan.png")


#! /usr/bin/python3
# Message sender to be used in conjunction with the CorgiScraper.py

from twilio.rest import Client
from Config import Config

class Sender:

    def __init__(self):
        self.config = Config()
        self.settings = self.config.load_file("Twilio")

        self.accountSID = self.settings["accountsid"]  # twilio acct SID
        self.authToken = self.settings["authtoken"]     # authToken given by twilio service
        self.twilioNumber = self.settings["twilionumber"]      # the number assigned from the twilio service
        self.desiredCell = self.settings["recipientnumber"]       # desired cell phone number to send the message to.

        self.twilioClient = Client(self.accountSID, self.authToken)

    """
    Sends a message to the desired cell phone number

    :param title: Title of the post to send.
    :param link: The actual image to send.
    """
    def sendMessage(self, title, link):
        self.message = 'Corgs :) \n\n' + title + '\n\n' + link      # Creates a string safe for Twilio
        self.twilioClient.messages.create(body=self.message, from_=self.twilioNumber, to=self.desiredCell)

if __name__ == "__main__":

    sender = Sender()
    #sender.sendMessage('title', 'link')


'''
Author: Imad Dabbura

Algorithm description:
The main question that we are trying to answer: Is there a path between two
site? In other words, are the pair of sites connected?
We assume the connection has the following properties: Reflexive, Symmetric,
and Transitive.
We will use here the id array where each index is linked to
the other site the is connected to. Therefore, we have to follow the link
until we get to the root where the site will be pointing to itself. Contrary
to the QuickUnion version; instead of arbitrarly always connecting the left
tree when with the right tree, we will connect the smaller tree to the larger
tree based on the size of each one. Therefore, we have to introduce new array
that keeps the size of each tree.

Conclusion:
In the worst case, we would have linearithmic running time ~NlogN. The
improvement comes from find() where in worst cases it would take logN instead
of linear N.
'''

import time
import sys

class QuickUnionUnionFind:
    '''Implementation of QuickFind version of UnionFindß'''

    def __init__(self, n):
        self._n = n
        # number of connected components
        self._count = self._n
        # initialize the array so that each site is its own component at start
        self._id = list(range(self._n))
        # initialize the tree size array to zero
        self._size = [0] * self._n

    def count(self):
        '''Return number of connected components'''
        return self._count

    def _validate(self, p):
        '''Raise ValueError if p is invalid'''
        if p < 0 or p >= self._n:
            raise ValueError(f'Index {p} must be between 0 and '
                             f'{self._n - 1})')

    def _find(self, p):
        '''Return the root to p'''
        self._validate(p)
        # O(logN)
        while p != self._id[p]:
            p = self._id[p]
        return p

    def connected(self, p, q):
        '''Check whether the pair of sites is connected'''
        self._validate(p)
        self._validate(q)
        return self._find(p) == self._find(q)

    def union(self, p, q):
        '''Connect the pair(p, q) if they are not connected'''
        # Get the root of each site
        self._validate(p)
        self._validate(q)
        p_root = self._find(p)
        q_root = self._find(q)

        if self._size[p_root] < self._size[q_root]:
            self._id[p_root] = q_root
            self._size[q_root] += self._size[p_root]
        else:
            self._id[q_root] = p_root
            self._size[p_root] += self._size[q_root]
        self._count -= 1

def main():
    '''
    Reads the value N (number of sites) followed by a sequence of integers
    (range 0 to N-1), and for each pair check:
    - If they are connected --> do nothing.
    - If they are not connected --> connect them, increment number of connected
    sites and print the pair to stdin.
    '''
    start = time.time()
    with open(sys.argv[1], 'r') as f:
        n = int(f.readline())
        uf = QuickUnionUnionFind(n)
        for line in f:
            p, q = list(map(int, line.split()))
            if not uf.connected(p, q):
                uf.union(p, q)
                print(p, q)

    print(f'Number of components = {uf.count()}.')
    elapsed_time = time.time() - start
    print(f'Completed in : {elapsed_time // 60:.2} minutes and '
        f'{elapsed_time % 60:.2} seconds.')

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import discord
from discord.ext import commands
from discord.ext.commands import CommandNotFound
import config
import logging

cogs = [
    'cogs.system',
    'cogs.announcements',
    'cogs.mod',
    'cogs.fuckinghemlock',
    'cogs.maintenance',
    'cogs.userCommands',
    'cogs.inviteclean',
    'cogs.deletelog',
]

class Bot(commands.Bot):
  def __init__(self, **kwargs):
    super().__init__(
      command_prefix=config.prefix,
      case_insensitive=True,
      activity=discord.Game(name="at the Pokémon Center"),
      **kwargs)

    self.logger = logging.getLogger(__name__)
    logging.basicConfig(level=logging.INFO)
    self.disc_logger = logging.getLogger('discord')
    self.disc_logger.setLevel(logging.ERROR)

    self.twitterTokens = config.twitter_tokens

    for cog in cogs:
      try:
        self.load_extension(cog)
      except Exception as exc:
        self.logger.error(f"Cog {cog} due to {exc.__class__.__name__}: {exc}")

  async def on_ready(self):
    self.logger.info(f"Logged on as {self.user} (ID: {self.user.id})")
    self.logger.info(f"Using discord.py {discord.__version__}")

  async def on_command_error(self, ctx, error):
    if isinstance(error, CommandNotFound):
      pass

bot = Bot()
bot.run(config.token)


from PTnamespace import Namespace as op_nms

class FFT:
    def __init__(self, op_recon:str):
        self.op_recon:str=op_recon

class ART:
    def __init__(self,
                 op_recon: str,
                 niter: int=None,
                 lambda0: int=None,
                 delta: float=None,
                 ):
        self.op_recon:str=op_recon
        self.niter:int=niter
        self.lambda0:int=lambda0
        self.delta:float=delta

    @property
    def inputs(self) -> dict:

        return {
            op_nms.niter: [int(self.niter)],
            op_nms.lambda0: [int(self.lambda0)],
            op_nms.delta:[float(self.delta)],
            }

class ZeroPadding:
    def __init__(self,
                op_zeroPad: str,
                factor: float=None,
                 ):
        self.op_zeroPad:str=op_zeroPad
        self.factor:float=factor

    @property
    def inputs(self) -> dict:

        return {
            op_nms.factor: [int(self.factor)],
            }

class Nothing:
    def __init__(self, op_nothing: str):
        self.op_nothing:str=op_nothing

class Pchip:
    def __init__(self, op_pchip: str):
        self.op_pchip:str=op_pchip

class Spline:
    def __init__(self, op_spline: str):
        self.op_spline:str=op_spline

class GridData:
    def __init__(self, op_gridData: str):
        self.op_gridData:str=op_gridData

class BM4D_G:
    def __init__(self, op_BM4D_G: str):
        self.op_BM4D_G:str=op_BM4D_G

class BM4D_R:
    def __init__(self, op_BM4D_R: str):
        self.op_BM4D_R:str=op_BM4D_R

class PeronaMalik:
    def __init__(self, op_PeronaMalik: str):
        self.op_PeronaMalik:str=op_PeronaMalik

default_recons={
    'FFT':FFT('FFT'),
    'ART':ART('ART',1, 1, 0.0 ),
}

default_imregrid={
    'None':Nothing('None'),
    'Pchip':Pchip('Pchip'),
    'Spline':Spline('Spline'),
    'GridData':GridData('GridData')
}

default_ksregrid={
    'None':Nothing('None'),
    'ZeroPadding':ZeroPadding('ZeroPadding', 2),
}

default_filter={
    'None':Nothing('None'),
    'BM4D Gauss':BM4D_G('BM4D Gauss'),
    'BM4D Rice':BM4D_R('BM4D Rice'),
    'PeronaMalik':PeronaMalik('Perona Malik')
}


"""
791. Custom Sort String

S and T are strings composed of lowercase letters. In S, no letter occurs more than once.

S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically,
if x occurs before y in S, then x should occur before y in the returned string.

Return any permutation of T (as a string) that satisfies this property.

Example :
Input:
S = "cba"
T = "abcd"
Output: "cbad"
Explanation:
"a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", and "a".
Since "d" does not appear in S, it can be at any position in T. "dcba", "cdba", "cbda" are also valid outputs.

Note:

S has length at most 26, and no character is repeated in S.
T has length at most 200.
S and T consist of lowercase letters only.

"""

class CustomSortString:

    """
    Approach #1: Count and Write [Accepted]
    Intuition

    Let's first write to our answer the elements of T that occur in S, in order of S. After, we'll write any elements of T we didn't write. This obviously keeps all the ordering relationships we wanted.

    In the second write, the order doesn't matter because those elements aren't in S, so there are no ordering relationships these elements have to satisfy.

    Algorithm

    The trick is to count the elements of T. After we have some count[char] = (the number of occurrences of char in T), we can write these elements in the order we want. The order is S + (characters not in S in any order).

    For more details on the algorithm, please see the inlined comments in each implementation.

    Complexity Analysis

    Time Complexity: O(S.\text{length} + T.\text{length})O(S.length+T.length), the time it takes to iterate through S and T

    Space Complexity: O(T.\text{length})O(T.length). We count at most 26 different lowercase letters, but the final answer has the same length as T.
    """
    def doit_hashtable(self, S, T):
        # count[char] will be the number of occurrences of
        # 'char' in T.
        count = collections.Counter(T)
        ans = []

        # Write all characters that occur in S, in the order of S.
        for c in S:
            ans.append(c * count[c])
            # Set count[c] = 0 to denote that we do not need
            # to write 'c' to our answer anymore.
            count[c] = 0

        # Write all remaining characters that don't occur in S.
        # That information is specified by 'count'.
        for c in count:
            ans.append(c * count[c])

        return "".join(ans)

    def customSortString(self, S: str, T: str) -> str:
        from collections import Counter
        cnt = Counter(T)
        output = []

        for s in S:
            if s in T:
                output.append(cnt[s] * s)
                del cnt[s]

        for s, num in cnt.items():
            output.append(s * num)

        return ''.join(output)


from django.urls import path
from . import views

urlpatterns = [
  path('list', views.index, name='exam_list'),
  path('detail/<int:exam_id>', views.exam, name='exam_detail'),
  path('detail/event/<int:event_id>', views.exam, name='exam_event_detail'),
  path('score', views.predict_score, name='exam_score'),
  path('hours', views.predict_study_hours, name='exam_hours'),
  path('number', views.get_next_exam_number, name='exam_number'),
  path('detail', views.exam, name='exam_detail'),
  path('search', views.search, name='exam_search'),
  path('remove', views.remove, name='exam_remove'),
  path('id', views.get_exam_id, name='exam_id'),
]


#!/usr/bin/python3

import sys
import glob
import os
import natsort
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

sns.set(style="whitegrid", rc={'figure.figsize': (16, 9)}, font_scale=2)

names = ["1024x1024x1024", "2048x2048x2048", "4096x4096x4096", "8192x8192x8192", "16384x16384x16384"]
titles = ["M = 1024, N = 1024, K = 1024", "M = 2048, N = 2048, K = 2048",  "M = 4096, N = 4096, K = 4096", "M = 8192, N = 8192, K = 8192", "M = 16384, N = 16384, K = 16384"]
time_array = ["µs", "ms", "ms", "ms", "ms"]

j = 4

files = "/home/neville/git/cucosma/Benchmarks/SwizzleBenchmarks/swizzle.csv_ault05.cscs.ch_2020-09-13_16:57:14/nvprof_*.csv"
data = pd.DataFrame(dtype='float')

for i, filename in enumerate(natsort.natsorted(glob.glob(files), reverse=False)):
    # print(filename)
    df = pd.read_csv(filename, skiprows=3)
    # print(df)
    # print(df[df["Name"].str.contains("cosma", na=False)]['Duration'].to_string(index=False))
    col = df[df["Name"].str.contains("cosma", na=False)]['Duration']
    print(col)
    data[i + 1] = col
    # print(data)
    if i % 16 == 15:
        data.reset_index(drop=True, inplace=True)
        # data = data.astype(float) * 1000

        data.columns = ['1', "2", "3", "4", "5", "6", "7", "8", '9', '10', "11", "12", "13", "14", "15", "16"]
        print(data)
        ax = sns.violinplot(data=data)
        ax.set(xlabel='SWIZZLE factor', ylabel='Runtime [' + time_array[j] + ']', title=titles[j])
        # plt.show()
        plt.savefig("../thesis/images/swizzle_plot_V100_" + names[j] + ".pdf", format="pdf", bbox_inches="tight")
        #plt.savefig("swizzle_plot_V100_" + names[j] + ".pdf", format="pdf", bbox_inches="tight")
        plt.clf()
        data = pd.DataFrame(dtype='float')
        j -= 1


from socket import *
from time import ctime
import threading

HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)

tcpSerSock = socket(AF_INET,SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)

cnt_list = []
thread_list = []

class Serve_Thread(threading.Thread):
	def __init__(self,tcpCli,addr,cnt_list):
		threading.Thread.__init__(self)
		self.tcpCli = tcpCli
		self.addr = addr
		self.cnt_list = cnt_list
	def run(self):
		while True:
			data = ''
		#	try:
			data = self.tcpCli.recv(BUFSIZ)
		#	print(data.decode('utf-8'))
		#	except:
		#		pass
			if data:
				for (cli,Addr) in self.cnt_list:
					if Addr != self.addr:
						cli.send(bytes("<<<<Time[%s] From[%s]:\n%s" % (ctime(),self.addr,data),'utf-8'))

while True:
	print("waiting for connection...")
	tcpCli,addr = tcpSerSock.accept()
	print("connect from:",addr)
	if (tcpCli,addr) not in cnt_list:
		cnt_list.append((tcpCli,addr))
		thread_list.append(Serve_Thread(tcpCli,addr,cnt_list))
		thread_list[-1].start()


# coding: utf8
import codecs
import json
import logging
import os

import torch
from transformers import InputExample

logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s -   %(message)s',
                    datefmt='%m/%d/%Y %H:%M:%S',
                    level=logging.INFO)
logger = logging.getLogger(__name__)

def _truncate_seq_pair(tokens_a, tokens_b, max_length):
    """Truncates a sequence pair in place to the maximum length."""

    # This is a simple heuristic which will always truncate the longer sequence
    # one token at a time. This makes more sense than truncating an equal percent
    # of tokens from each, since if one sequence is very short then each token
    # that's truncated likely contains more information than a longer sequence.
    while True:
        total_length = len(tokens_a) + len(tokens_b)
        if total_length <= max_length:
            break
        if len(tokens_a) > len(tokens_b):
            tokens_a.pop()
        else:
            tokens_b.pop()

class InputFeatures(object):
    """A single set of features of data."""

    def __init__(self, input_ids, input_mask, segment_ids, label_id):
        self.input_ids = input_ids
        self.input_mask = input_mask
        self.segment_ids = segment_ids
        self.label_id = label_id

class DataProcessor(object):
    """Base class for data converters for sequence classification data sets."""

    def get_train_examples(self, data_dir):
        """Gets a collection of `InputExample`s for the train set."""
        raise NotImplementedError()

    def get_dev_examples(self, data_dir):
        """Gets a collection of `InputExample`s for the dev set."""
        raise NotImplementedError()

    def get_test_examples(self, data_dir):
        """Gets a collection of `InputExample`s for the test set."""
        raise NotImplementedError()

    def get_labels(self):
        """Gets the list of labels for this data set."""
        raise NotImplementedError()

    @classmethod
    def _read_json(cls, input_file, quotechar=None):
        """Reads a tab separated value file."""
        dicts = []
        with codecs.open(input_file, 'r', 'utf-8') as infs:
            for inf in infs:
                inf = inf.strip()
                dicts.append(json.loads(inf))
        return dicts

class MyPro(DataProcessor):
    def get_train_examples(self, data_dir):
        """Gets a collection of `InputExample`s for the train set."""
        file_path = os.path.join(data_dir, 'data_train.txt')  # 读入数据
        f = open(file_path, 'r', encoding='utf-8')  # 指定读入数据格式及编码
        train_data = []
        index = 0
        for line in f.readlines():
            guid = "train-%d" % index
            line = line.replace('\n', '').split('\t')
            print(line)
            text_a = str(line[1])
            label = str(line[0])
            train_data.append(
                InputExample(guid=guid, text_a=text_a, text_b=None, label=label))
            index += 1
        return train_data

    def get_dev_examples(self, data_dir):
        """Gets a collection of `InputExample`s for the dev set."""
        file_path = os.path.join(data_dir, 'data_val.txt')  # 读入数据
        f = open(file_path, 'r', encoding='utf-8')  # 指定读入数据格式及编码
        val_data = []
        index = 0
        for line in f.readlines():
            guid = "val-%d" % index
            line = line.replace('\n', '').split('\t')
            text_a = str(line[1])
            label = str(line[0])
            val_data.append(
                InputExample(guid=guid, text_a=text_a, text_b=None, label=label))
            index += 1
        return val_data

    def get_test_examples(self, data_dir):
        """Gets a collection of `InputExample`s for prediction."""
        file_path = os.path.join(data_dir, 'data_test.txt')  # 读入数据
        f = open(file_path, 'r', encoding='utf-8')  # 指定读入数据格式及编码
        test_data = []
        index = 0
        for line in f.readlines():
            guid = "test-%d" % index
            line = line.replace('\n', '').split('\t')
            text_a = str(line[1])
            label = str(line[0])
            test_data.append(
                InputExample(guid=guid, text_a=text_a, text_b=None, label=label))
            index += 1
        return test_data

    def get_labels(self):
        """Gets the list of labels for this data set."""
        return ['0', '1']

def convert_examples_to_features(examples, label_list, max_seq_length, tokenizer, show_exp=True):
    """Loads a data file into a list of `InputBatch`s.

    Args:
        examples      : [List] 输入样本，包括question, label, index
        label_list    : [List] 所有可能的类别，可以是int、str等，如['book', 'city', ...]
        max_seq_length: [int] 文本最大长度
        tokenizer     : [Method] 分词方法

    Returns:
        features:
            input_ids  : [ListOf] token的id，在chinese模式中就是每个分词的id，对应一个word vector
            input_mask : [ListOfInt] 真实字符对应1，补全字符对应0
            segment_ids: [ListOfInt] 句子标识符，第一句全为0，第二句全为1
            label_id   : [ListOfInt] 将Label_list转化为相应的id表示
    """
    label_map = {}
    for (i, label) in enumerate(label_list):
        label_map[label] = i

    features = []
    for (ex_index, example) in enumerate(examples):
        tokens_a = tokenizer.tokenize(example.text_a)

        tokens_b = None
        if example.text_b:
            tokens_b = tokenizer.tokenize(example.text_b)

        if tokens_b:
            # Modifies `tokens_a` and `tokens_b` in place so that the total
            # length is less than the specified length.
            # Account for [CLS], [SEP], [SEP] with "- 3"
            _truncate_seq_pair(tokens_a, tokens_b, max_seq_length - 3)
        else:
            # Account for [CLS] and [SEP] with "- 2"
            if len(tokens_a) > max_seq_length - 2:
                tokens_a = tokens_a[0:(max_seq_length - 2)]

        # The convention in BERT is:
        # (a) For sequence pairs:
        #  tokens:   [CLS] is this jack ##son ##ville ? [SEP] no it is not . [SEP]
        #  type_ids: 0   0  0    0    0     0       0 0    1  1  1  1   1 1
        # (b) For single sequences:
        #  tokens:   [CLS] the dog is hairy . [SEP]
        #  type_ids: 0   0   0   0  0     0 0
        #
        # Where "type_ids" are used to indicate whether this is the first
        # sequence or the second sequence. The embedding vectors for `type=0` and
        # `type=1` were learned during pre-training and are added to the wordpiece
        # embedding vector (and position vector). This is not *strictly* necessary
        # since the [SEP] token unambigiously separates the sequences, but it makes
        # it easier for the model to learn the concept of sequences.
        #
        # For classification tasks, the first vector (corresponding to [CLS]) is
        # used as as the "sentence vector". Note that this only makes sense because
        # the entire model is fine-tuned.
        tokens = []
        segment_ids = []
        tokens.append("[CLS]")
        segment_ids.append(0)
        for token in tokens_a:
            tokens.append(token)
            segment_ids.append(0)
        tokens.append("[SEP]")
        segment_ids.append(0)

        if tokens_b:
            for token in tokens_b:
                tokens.append(token)
                segment_ids.append(1)
            tokens.append("[SEP]")
            segment_ids.append(1)

        input_ids = tokenizer.convert_tokens_to_ids(tokens)

        # The mask has 1 for real tokens and 0 for padding tokens. Only real
        # tokens are attended to.
        input_mask = [1] * len(input_ids)

        # Zero-pad up to the sequence length.
        while len(input_ids) < max_seq_length:
            input_ids.append(0)
            input_mask.append(0)
            segment_ids.append(0)

        assert len(input_ids) == max_seq_length
        assert len(input_mask) == max_seq_length
        assert len(segment_ids) == max_seq_length

        label_id = label_map[example.label]
        if ex_index < 5 and show_exp:
            logger.info("*** Example ***")
            logger.info("guid: %s" % example.guid)
            logger.info("tokens: %s" % " ".join(
                [str(x) for x in tokens]))
            logger.info("input_ids: %s" % " ".join([str(x) for x in input_ids]))
            logger.info("input_mask: %s" % " ".join([str(x) for x in input_mask]))
            logger.info(
                "segment_ids: %s" % " ".join([str(x) for x in segment_ids]))
            logger.info("label: %s (id = %d)" % (example.label, label_id))

        features.append(
            InputFeatures(input_ids=input_ids,
                          input_mask=input_mask,
                          segment_ids=segment_ids,
                          label_id=label_id))
    return features


import subprocess as sp
import os
import shutil
import sys
import string
from time import localtime, strftime
#
# avlpath = '/home/josh/Documents/avl/bin/avl'
#avlpath = r'D:\home\josh\Documents\avl\bin\avl.exe'

v_cruise = 13.00
Rho = 1.225

avlpath = '/home/josh/Documents/Research/MACHMDO/runwaysim/AVL/avl'

def avl_run(filename, alpha_start, alpha_end ):

    def Cmd(cmd):
        ps.stdin.write(cmd+'\n')

    try:
        os.remove(filename+'_data.dat')
    except :
        pass

    try:
        os.remove('stab.txt')
    except:
        pass

    #    print ("no such file")
    # run avl
    ps = sp.Popen(avlpath,stdin=sp.PIPE,stderr=sp.PIPE,stdout=sp.PIPE)
    ps.stderr.close()

    Cmd('PLOP')
    Cmd('G')
    Cmd(' ')

    Cmd('load '+filename+'.txt')

    # Apply mass file
    Cmd('Mass' + filename +'.mass')
    Cmd('MSET 0')

    Cmd('OPER')
    # Cmd('g')
    # Cmd(' ')

    # Cmd('M')
    # Cmd('V ' + str(v_cruise))
    # Cmd('d ' + str(Rho))

    # Cmd('d1 rm 0')
    # Cmd('d3 ym 0')

    Cmd('a a ' + str(alpha_start))
    Cmd('x')
    # Cmd('t')
    # Cmd(' ')
    Cmd('ST')
    Cmd('stab.txt')
    Cmd('w')
    Cmd(filename+'_data.dat')  # output file

    for i in range(alpha_start + 1, alpha_end + 1):
        Cmd('a a ' + str(i))
        Cmd('x')
        Cmd('w')
        Cmd(' ')

    Cmd(' ')
    Cmd(' ')

    Cmd('quit')  # exit

    ps.stdout.close()
    ps.stdin.close()
    ps.wait()

def getData_AVL(filename):

    f = open(filename, 'r')
    flines = f.readlines()

    alpha = []
    CL = []
    CD = []
    CM = []
    elev_def = []

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

        words = string.split(flines[i])
        if len(words) > 1:
            if 'Alpha' in words:
                alpha.append(float(words[2]))
            elif 'CLtot' in words:
                CL.append(float(words[2]))
            elif 'CDtot' in words:
                CD.append(float(words[2]))
            elif 'Cmtot' in words:
                CM.append(float(words[5]))
            elif 'Elevator' in words:
                elev_def.append(float(words[2]))

    LtoD = [a/b for a,b in zip(CL,CD)]

    return (alpha, CL, CD, CM, LtoD, elev_def)

def getNP_AVL(filename):

    f = open(filename, 'r')
    flines = f.readlines()

    # print(len(flines))

    words = string.split(flines[64])

    x_np = float(words[4])

    return x_np