## ---------------------------------
## wisdm - site data preparation
## ApexRMS, October 2023
## ---------------------------------

# built under Python version 3.11.0 & SyncroSim version 2.4.36
# Script pulls in covariate rasters and processes field data to ensure sites
# are in the template CRS and extent; aggregates or weights sites by spatial distribution;
# extracts site-specific covaraite data and generates datasheet of covariate site data.

#%% Source dependencies ----------------------------------------------------------

## Modify os path if multiple GDAL installations ----
import os
import glob
from win32api import GetFileVersionInfo, LOWORD, HIWORD

gdal_installations = []
if "PATH" in os.environ:
  for p in os.environ["PATH"].split(os.pathsep):
    if p and glob.glob(os.path.join(p, "gdal*.dll")):
      gdal_installations.append(os.path.abspath(p))

if len(gdal_installations) > 1:
    for folder in gdal_installations:
        filenames = [f for f in os.listdir(folder) if f.startswith("gdal") & f.endswith(".dll")]

        for filename in filenames:
            filename = os.path.join(folder, filename)

            if not os.path.exists(filename):
                print("no gdal dlls found in " + folder)
                os.environ['PATH'] = os.pathsep.join(
                        [p for p in os.environ['PATH'].split(os.pathsep) if folder not in p])
                continue
            try:
                info = GetFileVersionInfo (filename, "\\")
            except:
                continue

            major_version = HIWORD (info['FileVersionMS'])
            minor_version = LOWORD (info['FileVersionMS'])

            if (major_version < 3) | (minor_version < 6):
                os.environ['PATH'] = os.pathsep.join(
                    [p for p in os.environ['PATH'].split(os.pathsep) if folder not in p])

## dependencies
import pysyncrosim as ps
import numpy as np
import pandas as pd
import rioxarray
import xarray
import rasterio
import geopandas as gpd
import shapely
import dask
import pyproj
# import spatialUtils

from dask.distributed import Client, Lock
from shapely.geometry import Point #, shape
from rasterio.enums import Resampling #, MergeAlg

## Modify the os PROJ path (when running with Conda) ----
myLibrary = ps.Library()
mySession = ps.Session()

result = mySession._Session__call_console(["--conda", "--config"])
conda_fpath = result.stdout.decode('utf-8').strip().split(": ")[1]
if myLibrary.datasheets("core_Options").UseConda.item() == "Yes":
    os.environ["PROJ_DATA"] = os.path.join(conda_fpath , "envs\\wisdm\\wisdm-py-conda\\Library\\share\\proj")
    os.environ['PROJ_CURL_CA_BUNDLE'] = os.path.join(conda_fpath , "envs\\wisdm\\wisdm-py-conda\\Library\\ssl\\cacert.pem")

# if myLibrary.datasheets("core_Options").UseConda.item() == "Yes":
#    os.environ["PROJ_DATA"] = os.path.join(mySession.conda_filepath, "envs\\wisdm\\wisdm-py-conda\\Library\\share\\proj")
#    os.environ['PROJ_CURL_CA_BUNDLE'] = os.path.join(mySession.conda_filepath, "envs\\wisdm\\wisdm-py-conda\\Library\\ssl\\cacert.pem")
    # pyproj.datadir.set_data_dir(os.path.join(mySession.conda_filepath, "envs\\wisdm\\wisdm-py-conda\\Library\\share\\proj"))
    # pyproj.network.set_ca_bundle_path(os.path.join(mySession.conda_filepath, "envs\\wisdm\\wisdm-py-conda\\Library\\ssl\\cacert.pem"))

#%% Connect to SyncroSim library ------------------------------------------------

# Load current scenario
myScenario = ps.Scenario()

# Create a temporary folder for storing rasters
ssimTempDir = ps.runtime_temp_folder("Data")

# Get path to scnario inputs
ssimInputDir = myScenario.library.location + ".input\Scenario-" + str(myScenario.sid)

# Load datasheets
# inputs
networkSheet = myScenario.library.datasheets("Network")
# covariatesSheet = myScenario.project.datasheets("Covariates")
covariateDataSheet = myScenario.datasheets("CovariateData", show_full_paths=True)
fieldDataSheet = myScenario.datasheets("FieldData")
fieldDataOptions = myScenario.datasheets("FieldDataOptions")
templateRasterSheet = myScenario.datasheets("TemplateRaster", show_full_paths=False)
# restrictionRasterSheet = myScenario.datasheets("RestrictionRaster", show_full_paths=True)
multiprocessingSheet = myScenario.datasheets("core_Multiprocessing")

# outputs
# outputCovariateSheet = myScenario.datasheets("CovariateData", empty = True)
#%% Set progress bar ---------------------------------------------------------

steps = 5 + len(covariateDataSheet.CovariatesID)
ps.environment.progress_bar(report_type = "begin", total_steps = steps)

#%% Set up dask client -------------------------------------------------------

if multiprocessingSheet.EnableMultiprocessing.item() == "Yes":
    num_threads = multiprocessingSheet.MaximumJobs.item()
else:
    num_threads = 1

# Note: Follow link in output to view progress
dask.config.set(**{'temporary-directory': os.path.join(ssimTempDir, 'dask-worker-space')})
client = Client(threads_per_worker = num_threads, n_workers = 1, processes=False)
# client

#%% Check inputs and set defaults ---------------------------------------------

# Set PROJ network connection
if networkSheet.NetworkEnabled.item() == "No":
    pyproj.network.set_network_enabled(active=False)
    # os.environ["PROJ_NETWORK"] = "OFF"
    # pyproj.network.is_network_enabled()

# Check that a template raster was provided
if pd.isnull(templateRasterSheet.RasterFilePath.item()):
    raise ValueError("Template raster is missing.")

# check if field data was provided
if len(fieldDataSheet) == 0:
    # raise warning if field data was not provided
    raise ValueError("Field data was not provided. Please provide field data before continuing.")

#%% Load template raster ----------------------------------------------------------------

# set defualt chunk dimensions
chunkDims = 4096 #1024

templatePath = ssimInputDir + "\\wisdm_TemplateRaster\\" + templateRasterSheet.RasterFilePath.item()
templateRaster = rioxarray.open_rasterio(templatePath, chunks={'x': chunkDims, 'y': chunkDims}, masked=True)
templateMask = templateRaster.to_masked_array().mask[0]

# Get information about template
templateCRS = templateRaster.rio.crs
if templateCRS.is_valid == False:
    raise ValueError("Template has an invalid CRS (authority code). See {documention} for a list of accepted authority codes.")
# templateResolution = templateRaster.rio.resolution()
templateExtent = list(templateRaster.rio.bounds())
templateTransform = templateRaster.rio.transform()
# templatePixelSize = templateResolution[0]*-templateResolution[1]

if rasterio.dtypes.is_ndarray(templateMask):
    # Create template to polygon
    templatePolygons = []
    for shape, value in rasterio.features.shapes(templateMask.astype(np.int16), mask=np.invert(templateMask), transform=templateTransform):
        # shapely.geometry.shape(shape)
        templatePolygons.append(shapely.geometry.shape(shape))

    templatePolygons = shapely.geometry.MultiPolygon(templatePolygons)
    if not templatePolygons.is_valid:
        templatePolygons = templatePolygons.buffer(0)
        # Sometimes buffer() converts a simple Multipolygon to just a Polygon,
        # need to keep it a Multi throughout
        if templatePolygons.geom_type == 'Polygon':
            templatePolygons = shapely.geometry.MultiPolygon([templatePolygons])

    templatePolygons = gpd.GeoDataFrame(geometry=[templatePolygons], crs=templateCRS)
else:
    ps.environment.update_run_log('The template raster does not include a "No Data" mask. ',
    'All output covariate and site data will be clipped',
    ' to the full extent of the template raster.')
    templateMask = np.ones(templateRaster.shape, dtype=bool)
    templatePolygons = []

# update progress bar
ps.environment.progress_bar()

#%% Prepare site data -------------------------------------------------------

nInitial = len(fieldDataSheet.SiteID)

# Create shapely points from the coordinate-tuple list
siteCoords = [Point(x, y) for x, y in zip(fieldDataSheet.X, fieldDataSheet.Y)]
# gpd.GeoSeries(siteCoords).plot()

# Define field data crs
if pd.isnull(fieldDataOptions.EPSG[0]):
    fieldDataCRS = templateCRS
else:
    fieldDataCRS = fieldDataOptions.EPSG[0]

# Convert shapely object to a geodataframe with a crs
sites = gpd.GeoDataFrame(fieldDataSheet, geometry=siteCoords, crs=fieldDataCRS)

# Reproject points if site crs differs from template crs
if sites.crs != templateCRS:
    sites = sites.to_crs(templateCRS)

# Clip sites to template extent
if rasterio.dtypes.is_ndarray(templatePolygons):
    sites = gpd.clip(sites,templatePolygons)
else:
    sites = gpd.clip(sites, templateExtent)
nFinal = len(sites.SiteID)

if nFinal < nInitial:
    ps.environment.update_run_log(nInitial-nFinal, " sites out of ", nInitial,
            " total sites in the input field data were outside the template extent and were removed from the output. ",
            nFinal, " sites were retained.")

# Update xy to match geometry
sites.X = sites.geometry.apply(lambda p: p.x)
sites.Y = sites.geometry.apply(lambda p: p.y)

# Extract raster ids for each point
rasterCellIDs = []
rasterRows = []
rasterCols = []
with rasterio.open(templatePath) as src:
    for point in sites.geometry:
        x = point.xy[0][0]
        y = point.xy[1][0]
        row, col = src.index(x,y)
        rasterCellIDs.append((row,col))
        rasterRows.append(row)
        rasterCols.append(col)

sites["RasterRow"] = rasterRows
sites["RasterCol"] = rasterCols
sites["RasterCellID"] = rasterCellIDs

# update progress bar
ps.environment.progress_bar()

# If there are multiple points per cell - Aggregate or Weight sites
if pd.notnull(fieldDataOptions.AggregateAndWeight[0]):
    if len(np.unique(sites.RasterCellID)) != len(sites.RasterCellID):
        # find duplicates
        seen = set()
        dupes = []
        for x in sites.RasterCellID.tolist():
            if x in seen:
                dupes.append(x)
            else:
                seen.add(x)
        dupes = list(set(dupes)) # get unsorted unique list of tuples

        # if Aggregate sites is selected
        if fieldDataOptions.AggregateAndWeight[0] == "Aggregate":
            # if presence absence data
            if all(sites.Response.isin([0,1])):
                for d in dupes:
                    sitesInd = sites.index[sites.RasterCellID == d].to_list()
                    resp_d = sites.Response[sitesInd].to_list()
                    if sum(resp_d) == 0 or np.mean(resp_d) == 1: # if all absence or all presence
                        sites.Response[sitesInd[1:]] = -9999
                    else: # if response is mix of presence/absence
                        keep_d = (sites.Response[sitesInd] == 1).index[0] # keep a presence and convert rest of repeat sites to background points
                        sitesInd.remove(keep_d)
                        sites.Response[sitesInd] = -9999
            else: # if count data
                for d in dupes:
                    sitesInd = sites.index[sites.RasterCellID == d].to_list()
                    resp_d = sites.Response[sitesInd].to_list()
                    if sum(resp_d) == 0: # if all counts are zero
                        sites.Response[sitesInd[1:]] = -9999
                    else: # if any counts are greater then zero
                        sites.Response[sitesInd[0]] = sum(resp_d)
                        sites.Response[sitesInd[1:]] = -9999
        else: # if weight sites is selected
            if all(sites.Weight.isna()): # check for user defined weights;
                sites.Weight = 1
                for d in dupes:
                    sitesInd = sites.index[sites.RasterCellID == d].to_list()
                    weight_d = 1/len(sitesInd)
                    sites.Weight[sitesInd] = weight_d
            else:
                ps.environment.update_run_log("Weights were already present in the field data, new weights were not assigned.")

    else:
        ps.environment.update_run_log("Only one field data observation present per pixel; no aggregation or weighting required.")

# Save updated field data to scenario
outputFieldDataSheet = sites.iloc[:,0:7]
myScenario.save_datasheet(name="FieldData", data=outputFieldDataSheet)

# update progress bar
ps.environment.progress_bar()

# Drop sites with repeat cell repeats
dropInd = sites.index[sites.Response == -9999].tolist()
sites = sites.drop(dropInd)

# Write sites to file (for testing)
# tempOutputPath = os.path.join(ssimTempDir, "sites.shp")
# sites.to_file(tempOutputPath)

# Create index arrays (note in xarray x=col and y=row from geodataframe)
yLoc = xarray.DataArray(sites.RasterRow, dims =["loc"])
xLoc = xarray.DataArray(sites.RasterCol, dims =["loc"])
sitesOut = sites[["SiteID"]] #, "RasterCellID"

# Extract covariate values for each site
for i in range(len(covariateDataSheet.CovariatesID)):
    # Load processed covariate rasters and extract site values
    outputCovariatePath = covariateDataSheet.RasterFilePath[i]
    covariateRaster = rioxarray.open_rasterio(outputCovariatePath, chunks=True)
    sitesOut[covariateDataSheet.CovariatesID[i]] = covariateRaster[0].isel(x=xLoc,y=yLoc).values.tolist()
    # update progress bar
    ps.environment.progress_bar()

# Convert site data to long format
siteData = pd.melt(sitesOut, id_vars= "SiteID", value_vars=sitesOut.columns[1:], var_name="CovariatesID", value_name="Value")

# Save site data to scenario
myScenario.save_datasheet(name="SiteData", data=siteData)

# update progress bar
ps.environment.progress_bar(report_type = "end")


"""
A星算法
核心在于维护两个数据结构：open集与close集。open集包含所有已搜索到的待检测节点，初始时只包含起始节点；close集包含所有已检测的节点，初始状态为空。
每个节点包含一个指向父节点的指针，以便确定追踪关系。
每个节点有G值与H值。G值代表从起始节点到该节点的移动量；H值代表该节点到目标节点移动量的估计值。
每个节点还有F值，F = G + H

算法主循环：
1. 从open集中取一个F值最小的节点（称为最优节点）；
2. 将该节点从open集中移除，加入到close集中；
3. 判断：
    3.1. 若该节点即终点，则完成并结束；
    3.2. 否则，检查该节点的所有相邻节点：
        3.2.1. 相邻节点在close集中，代表已经被检测过，无需考虑；
        3.2.2. 相邻节点在open集中，若从当前节点走到这个相邻节点所计算而得的G值更小，则更新此相邻节点的G值与F值，并重定向其父节点为当前节点
        3.2.3. 否则，将该相邻节点加入open集，设置其G值与F值，并设置其父节点为当前节点。
* 若迷宫实际不通，则在第一步时判断获取到的节点n为空，就会退出
"""
from config import MapGridType
from random import randint
from heapq import *

class TestMap(object):
    """ 简单的测试迷宫类。0代表可通行的路径单元，1代表不可通行的墙单元，2代表走过的路径 """
    def __init__(self, length, width):
        self.length = length
        self.width = width
        self.map = [[0] * self.length for _ in range(self.width)]
        self.origin_coor = 0, 0
        self.destination_coor = length - 1, width - 1

    # 随机生成若干个墙单元
    def create_block(self, block_num):
        for _ in range(block_num):
            x, y = (randint(0, self.length - 1), randint(0, self.width - 1))
            while (x, y) == self.origin_coor or (x, y) == self.destination_coor:
                x, y = (randint(0, self.length - 1), randint(0, self.width - 1))
            self.map[y][x] = MapGridType.MAP_BLOCK

    def show_map(self):
        print("+" * (3 * self.length + 2))
        for row in self.map:
            s = '+'
            for entry in row:
                s += ' ' + str(entry) + ' '
            s += '+'
            print(s)
        print("+" * (3 * self.length + 2))

    # 随机获取可移动的单元
    def generate_pos(self, range_x, range_y):
        x, y = (randint(range_x[0], range_x[1]), randint(range_y[0], range_y[1]))
        while self.map[y][x] == 1:
            x, y = (randint(range_x[0], range_x[1]), randint(range_y[0], range_y[1]))
        return x, y

class Node(object):
    """ 节点类。每个节点具有如下特征：横纵坐标、父节点、g值、f值 """
    def __init__(self, coor, g_val, h_val=0, father=None):
        self.coor = coor

        self.father = father
        self.g_val = g_val
        self.h_val = h_val
        self.f_val = 0

    def get_pos(self):
        return self.coor[0], self.coor[1]

    def get_h_val(self, dest_coor):
        return abs(dest_coor[0] - self.coor[0]) + abs(dest_coor[1] - self.coor[1])

    def reset_g_val(self, new_g_val):
        self.g_val = new_g_val
        self.reset_f_val()

    def reset_f_val(self):
        self.f_val = self.g_val + self.h_val

    def reset_father(self, father_node):
        self.father = father_node

class OpenList(object):
    """ OpenList类，基于堆，实现快速定位openList中最小F值的点. """
    def __init__(self):
        self.heap = list()
        self.open_list = dict()

    def push(self, node):
        heappush(self.heap, (node.f_val, node.coor))
        self.open_list[node.coor] = node

    def pop(self):
        node_coor = heappop(self.heap)[1]
        node = self.open_list.pop(node_coor)            # dict 移除键值对时间复杂度也是O(1)
        return node

    # 根据坐标，从open集中返回Node。若不存在则返回None
    def locate_node(self, coor):
        return self.open_list.get(coor, None)

class Astar(object):
    def __init__(self, maze):
        self.maze = maze
        self.open_list = OpenList()
        self.close_list = set()                             # close集存储的是坐标

    def search(self):
        # 建立起点终点对象，将起点加入open集
        cur_node = Node(self.maze.origin_coor, 0)                # 初始为起点
        destination = Node(self.maze.destination_coor, 0)
        self.open_list.push(cur_node)
        while True:
            # 寻找open集中f值最小的单元，并从open集中弹出。
            try:
                cur_node = self.open_list.pop()
            except IndexError:
                print(" 没有可行的路径 ")
                break
            # 若选择的单元就是终点，则结束
            if cur_node.coor == destination.coor:
                break
            # 将该单元的坐标加入到close集中
            self.close_list.add(cur_node.coor)
            # 检查该单元的相邻单元，调整open集
            self.add_adjacent_positions(cur_node)

        # 在结束后，若已经走到了终点，则依次寻找父单元，在地图上标记已经探寻过的路径单元
        while cur_node is not None:
            coor_x, coor_y = cur_node.get_pos()
            if (coor_x, coor_y) != self.maze.origin_coor and (coor_x, coor_y) != self.maze.destination_coor:
                self.maze.map[coor_y][coor_x] = MapGridType.MAP_PATH
            cur_node = cur_node.father

    def add_adjacent_positions(self, cur_node):
        # 找到当前单元的所有合法相邻单元（的坐标）
        neighbors = self.get_neighbors(cur_node.coor)
        for neighbor in neighbors:
            # 若相邻单元已经在close集中，则略过。
            if neighbor not in self.close_list:
                # 否则，检查相邻单元是否在open集中
                neighbor_node = self.open_list.locate_node(neighbor)
                # 计算这个相邻单元基于cur_node的g值
                g_val = cur_node.g_val + self.get_move_cost(cur_node, neighbor)
                # 若相邻单元不在open集中，则将其加入open集
                if neighbor_node is None:
                    node = Node(neighbor, g_val, father=cur_node)
                    node.h_val = node.get_h_val(self.maze.destination_coor)
                    node.reset_f_val()
                    self.open_list.push(node)
                # 若在open集中，且该相邻单元的原本的g值大于基于cur_node而来的g值，则更新该单元的g值为当前的g，并更新该单元的父单元
                elif neighbor_node.g_val > g_val:
                    neighbor_node.reset_g_val(g_val)
                    neighbor_node.reset_father(cur_node)

    # 判断相邻单元的坐标是否可移动。即：位于地图边界内，且不是墙单元
    def get_legal_neighbor(self, cur_coor, offset):
        new_x, new_y = (cur_coor[0] + offset[0], cur_coor[1] + offset[1])
        if new_x < 0 or new_x >= self.maze.length or \
                new_y < 0 or new_y >= self.maze.width or \
                self.maze.map[new_y][new_x] == MapGridType.MAP_BLOCK:
            return None
        return new_x, new_y

    # 定位当前单元的合法相邻单元的坐标
    def get_neighbors(self, cur_coor):
        # 定义“相邻”为周围的四个方向或八个方向
        offsets = [(-1, 0), (0, -1), (1, 0), (0, 1)]
        # offsets = [(-1,0), (0, -1), (1, 0), (0, 1), (-1,-1), (1, -1), (-1, 1), (1, 1)]
        neighbors = []
        for offset in offsets:
            neighbor = self.get_legal_neighbor(cur_coor, offset)
            if neighbor is not None:
                neighbors.append(neighbor)
        return neighbors

    # 计算移动cost。目的是区分平移还是对角移动
    @staticmethod
    def get_move_cost(cur_node, target_coor):
        if cur_node.coor[0] != target_coor[0] and cur_node.coor[1] != target_coor[1]:
            return 1.4
        return 1

if __name__ == '__main__':
    test_maze = TestMap(15, 10)
    test_maze.create_block(32)

    astar = Astar(test_maze)
    astar.maze.show_map()
    astar.search()
    astar.maze.show_map()


import socket
import sys
import os
import functools

result = None

import functools
@functools.lru_cache()
def get_storage():
    from epinephrine.__main__ import Storage
    def _(x):
        global result
        result = x
    return Storage(sendall=_)

def send(data):
    storage = get_storage().handle(data)
    return result.decode()

def test_send():
    assert "1" == send("#CLEAR")
    dummy = "#INSERT:data"
    assert "1" == send(dummy)
    dummy = "#INSERTdata"
    assert "0" == send(dummy)

def test_clear():
    assert "1" == send("#CLEAR")
    assert "0" == send("#LENGTH")

def test_pagination():
    assert "1" == send("#CLEAR")
    dummy = "#INSERT:data"
    assert "1" == send(dummy)
    assert "1" == send(dummy)
    assert "1" == send(dummy)
    assert "data\ndata\ndata" == send("#RETRIVE:3:0")
    assert "" == send("#RETRIVE:3:1")

def test_dump():
    assert "1" == send("#DUMP")
    dummy = "#INSERT:data"


from django.shortcuts import render
from django.http import JsonResponse
import openai, os

openai.api_key = os.environ.get('GPT_KEY')

def chat_view(request):
    if request.method == 'POST':
        user_input = request.POST['user_input']

        response = openai.ChatCompletion.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content": "Car Teller"},
                {"role": "system", "content": "Only answer question about cars"},
                {"role": "system", "content": "If is something non car related, to give answer that I'm only give answers about cars"},
                {"role": "user", "content": user_input},
            ],
            temperature=0.5,
            max_tokens=3035,
            top_p=1,
            frequency_penalty=0,
            presence_penalty=0,
            n=1,
            stop=["\nUser:"],
        )

        bot_response = response["choices"][0]["message"]["content"]
        return JsonResponse({'response': bot_response})

    return render(request, 'chatgpt_app/chat.html')


import shutil

from flask import Flask, render_template, request, send_file
from werkzeug import secure_filename
import os, csv, random, zipfile
import pandas as pd

app = Flask(__name__)

UPLOAD_FOLDER = './uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

ALLOWED_EXTENSIONS = {'xlsx', 'xls'}

def allowed_file(filename):
    print(filename.rsplit('.', 1)[1])
    allowed = '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
    return allowed

def write_to_csv(rounds, round_num, group_name):
    file_path = "./downloads/" + group_name + "_" + round_num + ".csv"
    with open(file_path, 'w') as csv_file:
        fieldnames = ["Room Number", "Team 1", "vs", "Team 2"]
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
        writer.writeheader()
        for i in range(len(rounds)):
            writer.writerow({"Room Number": rounds[i][0], "Team 1": " ".join([rounds[i][1]]), "vs": "vs",
                             "Team 2": " ".join([rounds[i][2]])})

def make_round(teams_a, teams_b, already_matched):
    random.shuffle(teams_a)
    random.shuffle(teams_b)
    teams_b_used = list()
    rounds = list()
    room_number = 1
    for i in teams_a:
        for j in teams_b:
            if j not in teams_b_used and j not in already_matched[i]:
                if j != "No Team, Bye":
                    rounds.append([room_number, i, j])
                    room_number += 1
                else:
                    rounds.append(["No room", i, j])
                teams_b_used.append(j)
                break
    if len(rounds) == len(teams_a):
        for round in rounds:
            already_matched[round[1]].append(round[2])
        return rounds
    else:
        return make_round(teams_a, teams_b, already_matched)    #if it's stupid but it works, it ain't stupid

def do_stuff(df, num_rounds, group_name):
    df.drop([df.columns[0]], axis='columns', inplace=True)
    df.drop([0, 1, 2, 3], inplace=True)
    df = df[df[3] == df[3]]
    team_codes = df[2].tolist()
    team_names = df[3].tolist()
    teams = list()
    for i in range(len(team_names)):
        try:
            if team_names[i].strip() != "":
                teams.append(team_codes[i].strip() + " " + team_names[i].strip())
        except:
            error_msg = "Exception has occured trying to strip " + str(team_names[i]) + " or " + str(team_codes[i])
            print(error_msg)
    teams = list(set(teams))
    random.shuffle(teams)
    if len(teams) % 2 != 0:
        teams.append("No Team, Bye")
    half_teams = int(len(teams) / 2)
    teams_a = teams[:half_teams]
    teams_b = teams[half_teams:]
    already_matched = dict()
    for i in teams_a:
        already_matched[i] = list()
    for i in range(num_rounds):
        rounds = make_round(teams_a, teams_b, already_matched)
        write_to_csv(rounds, "round_" + str(i + 1), group_name)

def generate_rounds(filename, num_rounds):
    if os.path.exists("./downloads"):
        shutil.rmtree("./downloads")
        os.mkdir("./downloads")
        filepath = './uploads/' + str(filename)
        xls = pd.ExcelFile(filepath)
        df1 = pd.read_excel(xls, 'Lions Registration', header=None)
        df2 = pd.read_excel(xls, 'Cubs Registration', header=None)
        do_stuff(df1, num_rounds, "lions")
        do_stuff(df2, num_rounds, "cubs")

@app.route('/')
def login():
    print("hello world")
    return render_template('index.html')

@app.route('/uploader', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        num_rounds = int(request.form["num_rounds"])
        if num_rounds < 1:
            raise Exception("invalid number of rounds entered")
        print(num_rounds)
        if f and allowed_file(f.filename):
            curpath = os.path.abspath(os.curdir)
            print("Current path is: %s", (curpath))
            filename = secure_filename(f.filename)
            f.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            generate_rounds(filename, num_rounds)
            zipf = zipfile.ZipFile('Rounds.zip', 'w', zipfile.ZIP_DEFLATED)
            for root, dirs, files in os.walk('./downloads/'):
                for file in files:
                    zipf.write('./downloads/' + file)
            zipf.close()
            return send_file('Rounds.zip', mimetype='zip', attachment_filename='Rounds.zip', as_attachment=True)
        else:
            raise Exception("Something went wrong. please contact a dev")

if __name__ == '__main__':
    app.run(debug=True)


from sys import argv, path
import networkx as nx
from pipeline.NetworkVisCreator import NetworkVisCreator
from tqdm import tqdm
from prettytable import PrettyTable

path.append("..")

from pipeline.picklereader import PickleReader
from scripts.helpers import all_graphs, fetch_repo, num_graphs

output_path = None
for arg in argv:
    if "output" in arg:
        output_path = arg.split("=")[-1]

if not output_path:
    print("Must provide output path.")
    exit(1)

graph = nx.Graph()
gspan_format = []

pr = PickleReader([])
nwvc = NetworkVisCreator(None, [])
table = PrettyTable()
table.field_names = ["Graph #", "Repository"]

for i, path in tqdm(enumerate(all_graphs()), total=num_graphs()):
    target_repo = fetch_repo(str(path), from_graph=True)
    table.add_row([i, target_repo])
    gspan_format += [f"t # {i}"]
    nodes, _, comment_list, timeline_list, _ = pr.read_repo_local_file(None, target_repo)

    for index, node in enumerate(nodes):
        graph.add_node(node.number, type=0 if node.pull_request is not None else 1)
        #  graph.add_node(node.number, type="pull_request" if node.pull_request is not None else "issue")
        node_timeline = timeline_list[-index - 1]
        node_timeline = list(
            filter(
                lambda x: x.event == "cross-referenced"
                and x.source.issue.repository.full_name.replace("/", "-") == target_repo,
                node_timeline,
            )
        )
        for mention in node_timeline:
            mentioning_issue_comments = nwvc.find_comment(mention.source.issue.url, comment_list)
            graph.add_edge(
                mention.source.issue.number,
                node.number,
                link_type=nwvc.find_automatic_links(node.number, mention.source.issue.body, mentioning_issue_comments),
            )

    for node in graph.nodes(data=True):
        gspan_format += [f"v {node[0]} {node[1].get('type', None)}"]

    link_type_map = {"other": 0, "fixes": 1, "duplicate": 2}
    for edge in graph.edges(data=True):
        gspan_format += [f"e {edge[0]} {edge[1]} {link_type_map[edge[2].get('link_type', None)]}"]

gspan_format += ["t # -1"]

with open(output_path, "w") as x:
    x.write("\n".join(gspan_format))

print(table)


## Category variable and dummy variable and Undo ##

import numpy as np
import pandas as pd
from collections import defaultdict
from sklearn.preprocessing import LabelEncoder

class CategoricalPreprocessing():

    def formatting(self, data, dummies=[], categories=[], category_order=[]):
        """
        data (numpy or DataFrame): Input data.
        categories (list or None): Enter the column name or column number for categorical variable of nominal scale. Example : categories = ['carrier','substrate']
        category_order (dict or None): Enter the column name or column number for the categorical variable of the order scale.
        When specifying, {"column name": {"label": order}}。Example : category_order={'Sex':{'Male':0,'Female':1}}
        Make sure categories and category_order are not on the same column.
        dummies (list or None): Specify the column name or column number to be a dummy variable. Example : dummies=['material','carrier','substrate']
        The value converted by dummies comes to the front of the line.

        Return value
        data : Data after transformation.
        self.dummies_list : A list of the column numbers that get_dummies and what changed. [[Original column number, column name, [what changed]], ...]
        Example [[9, 'Race', ['White', 'Black']], [10, 'Sex', ['Male', 'Female']]]
        self.categories_reborn : Information necessary to restore the categorized variable.
        [[[Column name changed by category or column number], LabelEncorder defaultdict used for conversion], {category_order dictionary}]        例 [[[1, 2],defaultdict(sklearn.preprocessing.label.LabelEncoder,
              {'Education': LabelEncoder(), 'Martial_Status': LabelEncoder()})],
              {'Sex': {'Male': 0, 'Female': 1}}]
        self.categories : Column names of all columns that are categorized.

        dummies_list or categories_reborn is using in inverse_formatting
        dummies_list or category_colnames is using in make domain in GeneticAlgorithm
        """

        data = pd.DataFrame(data).copy()  # Convert to DataFrame for when input data is numpy

        # Convert according to categories
        le = LabelEncoder()
        if not(categories==None or categories==[] or categories==False):  # Whether to convert the nominal scale into categorical variables
            if all([type(x) == str for x in categories]):  # When category is column name
                le = defaultdict(LabelEncoder)
                data.loc[:,categories] = data.loc[:,categories].apply(lambda x: le[x.name].fit_transform(x))
                # Inverse the encoded
                #data.loc[:,categories].apply(lambda x: le[x.name].inverse_transform(x))
            elif all([type(x) == int for x in categories]):  # Whei category is column number
                le = defaultdict(LabelEncoder)
                data.iloc[:,categories] = data.iloc[:,categories].apply(lambda x: le[x.name].fit_transform(x))
            categories_reborn1 = [categories, le]  # List to undo
        else:
            categories = []
            categories_reborn1 = None

        category_column_list = []  # Save column name to categorize order scale
        if not (category_order==None or category_order==[] or category_order==False):  # Whether to categorize ordinal scale
            for column_name, value in category_order.items():  # Expand the input value dictionary into column names and order
                size_mapping = value
                if type(column_name)==str:
                    data.loc[:, column_name] = data.loc[:, column_name].map(size_mapping)
                elif type(column_name)==int:
                    data.iloc[:, column_name] = data.iloc[:, column_name].map(size_mapping)
                elif type(column_name)==float:
                    column_name = int(column_name)
                    data.iloc[:, column_name] = data.iloc[:, column_name].map(size_mapping)
                category_column_list.append(column_name)

        categories_ = categories +  category_column_list  # Save all converted column names in categories
        self.categories_reborn = [categories_reborn1, category_order]  # Save all information to return to categories_reborn

        if not(dummies==None or dummies==[] or dummies==False):  # When creating a dummy variable
            if all([type(x) == float for x in dummies]):
                dummies = [int(x) for x in dummies]

            dummies_column_list = []  # Save column names after dummy variable
            dummies_data_list = []  # Save DataFrame as dummy variable

            data_copy = data.copy()
            if all([type(x) == str for x in dummies]):
                for dummy in dummies:
                    colnum = data_copy.columns.get_loc(dummy)   # Column number of the place to be a dummy variable
                    colname = dummy   # Column name of the place to be a dummy variable
                    data_dummied = pd.get_dummies(data_copy.loc[:, dummy])  # to dummy variable
                    dummy_columns = list(data_dummied.columns.values)  # Get column name after dummy variable and store it in list
                    dummies_column_list.append([colnum, colname, dummy_columns])
                    dummies_data_list.append(data_dummied)
                    data.drop([dummy], axis = 1, inplace = True)  # Delete the specified column in the original data

            elif all([type(x) == int for x in dummies]):
                for dummy in dummies:
                    colnum = dummy  # Column number of the place to be a dummy variable
                    colname =  list(data_copy.iloc[:,[dummy]].columns)[0]  # Column name of the place to be a dummy variable
                    data_dummied = pd.get_dummies(data_copy.iloc[:, dummy])  # to dummy variable
                    dummy_columns = list(data_dummied.columns.values)  # Get column name after dummy variable and store it in list
                    dummies_column_list.append([colnum, colname, dummy_columns])
                    dummies_data_list.append(data_dummied)
                    data.drop([colname], axis = 1, inplace = True)  # Delete the specified column in the original data

            for dummies_data in dummies_data_list[::-1]:  # Iteratively process the dummy variable DataFrame to correspond to the column names
                data = pd.concat([dummies_data, data], axis = 1)  # Join so that dummy variable column comes first

        else:
            dummies_column_list = []  # If you do not want to make a dummy variable, output with [].

        self.dummies_list = dummies_column_list

        # It is difficult to use if the column name and column number are mixed, so convert it to the column name.
        category_colnames = []
        for category in categories_:
            if isinstance(category, int):
                column = list(data_copy.iloc[:,[category]].columns)[0]
                category_colnames.append(column)
            elif isinstance(category, str):
                category_colnames.append(category)
        self.category_colnames = category_colnames

        # Preformatted data, dummy columns and restoration information, categorization restoration information, categorized column names

        return data, self.dummies_list, self.categories_reborn, self.category_colnames

    def _reborn_dummies(self, dummies_data):
        """A function that returns the column name of the dummy variable df that is set to 1.
         That is, restoration of dummies. For apply."""
        dummies_data_index = list(dummies_data[dummies_data==1].index)
        if dummies_data_index==[]:
            return np.nan
        else:
            return dummies_data_index[0]

    def _pd_insert(self, original_df, insert_df, insert_index, axis):
        """Insert rows with pandas"""
        if axis==0:
            previous_df = original_df.iloc[:insert_index, :]
            behind_df = original_df.iloc[insert_index:,:]
            new_df = pd.concat([previous_df, insert_df, behind_df], axis=0)
        elif axis==1:
            previous_df = original_df.iloc[:, :insert_index]
            behind_df = original_df.iloc[:,insert_index:]
            new_df = pd.concat([previous_df, insert_df, behind_df], axis=1)
        return new_df

    def inverse_formatting(self, data, dummies_list='self', categories_reborn='self'):
        """
        Restore the processing performed in the preprocessing.
        data : (pandas) Data to be restored, processed with formatting.
        dummies_list : dummies_list generated by formatting
        categories_reborn : categories_reborn generated by formatting
        category_colnames has no turn. This is used when creating a domain with genetic_algorithm.
        """

        if dummies_list=='self' and hasattr(self, 'dummies_list'):
            # If it is defined in self, it is assigned as it is
            dummies_list = self.dummies_list
        elif dummies_list=='self' and not hasattr(self, 'dummies_list'):
            # If it is not defined, enter the default value for now.
            dummies_list = []
        if categories_reborn=='self' and hasattr(self, 'categories_reborn'):
            categories_reborn = self.categories_reborn
        elif categories_reborn=='self' and not hasattr(self, 'categories_reborn'):
            categories_reborn = [None, None]

        reborn_data = data.copy()

        # Restoration when dummy variables are used
        if not(dummies_list==[] or dummies_list==None or dummies_list==False):
            for i in range(len(dummies_list)):
                dummies_columns = dummies_list[i][2]  # Column name after conversion
                original_colname =  dummies_list[i][1]
                insert_colnum = dummies_list[i][0]
                dummies_data = data.loc[:,dummies_columns]

                reborn = pd.DataFrame(dummies_data.apply(self._reborn_dummies, axis=1), columns=[original_colname])

                reborn_data.drop(dummies_columns, axis=1, inplace=True)
                reborn_data = self._pd_insert(reborn_data, reborn, insert_colnum, axis=1)

        # Restoration of categorical variables converted with categories' LabelEncorder
        categories_reborn_le = categories_reborn[0]
        if not (categories_reborn_le==None or categories_reborn_le==[] \
                or categories_reborn_le==False):
            columns = categories_reborn_le[0]
            le = categories_reborn_le[1]

            if all([type(x) == int for x in columns]):
                # Inverse the encoded
                reborn_data.iloc[:, columns] = reborn_data.iloc[:, columns].astype(int)
                reborn_data.iloc[:, columns] = reborn_data.iloc[:, columns].apply(lambda x: le[x.name].inverse_transform(x))
            elif all([type(x) == str for x in columns]):
                # Inverse the encoded
                reborn_data.loc[:, columns] = reborn_data.loc[:, columns].astype(int)
                reborn_data.loc[:, columns] = reborn_data.loc[:, columns].apply(lambda x: le[x.name].inverse_transform(x))
            else:
                raise ValueError("Use same type or correct type")

        # Restoration of categorical variables converted by category_order dictionary
        categories_reborn_dict = categories_reborn[1]
        if not(categories_reborn_dict==None or categories_reborn_dict==[] \
                or categories_reborn_dict==False):
            # Originally, create a dictionary that reverses the conversion part of the conversion dictionary
            reborn_dict_swap = {}
            for key, items in categories_reborn_dict.items():
                d_swap = {v: k for k, v in items.items()}
                d_swap_ = {key: d_swap}
                reborn_dict_swap.update(d_swap_)

            # Convert as you would when converting
            for column_name, value in reborn_dict_swap.items():  # Expand the input value dictionary into column names and order
                size_mapping = value
                if type(column_name) == str:
                    reborn_data.loc[:, column_name] = reborn_data.loc[:, column_name].astype(int)
                    reborn_data.loc[:, column_name] = reborn_data.loc[:, column_name].map(size_mapping)
                elif type(column_name) == int:
                    reborn_data.iloc[:, column_name] = reborn_data.iloc[:, column_name].astype(int)
                    reborn_data.iloc[:, column_name] = reborn_data.iloc[:, column_name].map(size_mapping)
                elif type(column_name) == float:
                    column_name = int(column_name)
                    reborn_data.iloc[:, column_name] = reborn_data.iloc[:, column_name].astype(int)
                    reborn_data.iloc[:, column_name] = reborn_data.iloc[:, column_name].map(size_mapping)

        return reborn_data

    def change_columns_after_preprocess(self, exp, obj, mid=None, dummies_list='self'):
        """
        A function that converts the original exp, obj, and mid column numbers into column numbers after making them dummy variables, etc.
        exp : (list of int) original explanatory variable column number
        obj : (list of int) original objective variable column number
        mid : (list of int) original Intermediate variable column number
        It is not assumed that there will be dummy variables other than exp.
        Note the order of exp, obj, mid
        """
        if dummies_list=='self' and hasattr(self, 'dummies_list'):
            # If it is defined in self, it is assigned as it is
            dummies_list = self.dummies_list
        elif dummies_list=='self' and not hasattr(self, 'dummies_list'):
            # If it is not defined, enter the default value for now.
            dummies_list = []

        # If don't define
        if dummies_list==[] or dummies_list==None or dummies_list==False:
            return exp, obj, mid

        n_dummies = len(self.dummies_list)
        # Estimate extra columns with dummy variables
        x = 0
        for i in range(n_dummies):
            x += len(self.dummies_list[i][2])
        incre_num = x - n_dummies

        # shift exp and then others
        exp_shift = list(map(lambda x:x+incre_num, exp))
        exp_plus = [x for x in range(0, incre_num)]
        exp_after = exp_plus + exp_shift
        if mid==None or mid==[] or mid==False:
            mid_after = None
        else:
            mid_after = list(map(lambda x:x+incre_num, mid))
        obj_after = list(map(lambda x:x+incre_num, obj))

        return exp_after, obj_after, mid_after

    def str_for_int(self, original_df, exp, obj, mid=None):
        """
        Function to convert to int when the original exp, obj, and mid are column names.
        original_df : (pandas) original df
        exp : (list of str) Column name explanatory variable
        obj : (list of str) Column name objective variable
        mid : (list of str) Column name Intermediate variable
        """
        exp_num = [original_df.columns.get_loc(x) if isinstance(x,str) else x for x in exp]
        obj_num = [original_df.columns.get_loc(x) if isinstance(x,str) else x for x in obj]
        if not(mid==None or mid==[] or mid==False):
            mid_num = [original_df.columns.get_loc(x) if isinstance(x,str) else x for x in mid]
        else:
            mid_num = None
        return exp_num, obj_num, mid_num


import math

import pyglet
from pyglet.gl import *

class SmoothLineGroup(pyglet.graphics.Group):
    def set_state(self):
        glPushAttrib(GL_ENABLE_BIT)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
        glEnable(GL_LINE_SMOOTH)
        glLineWidth(2)

    def unset_state(self):
        glPopAttrib()

    def __hash__(self):
        return hash(self.__class__.__name__)

    def __eq__(self, other):
        return self.__class__ is other.__class__

def add_circle(batch, x, y, radius, color, num_points=30, antialised=True):
    l = []
    angle1=0
    for n in range(num_points):
        angle2 = (math.pi * 2 * n+1) / num_points
        l.append(x + radius * math.cos(angle1))
        l.append(y + radius * math.sin(angle1))
        l.append(x + radius * math.cos(angle2))
        l.append(y + radius * math.sin(angle2))
        angle1 = angle2

    l.append(x + radius * math.cos(angle1))
    l.append(y + radius * math.sin(angle1))
    l.append(x + radius * math.cos(0))
    l.append(y + radius * math.sin(0))
    vertices_count = int(len(l)/2 )
    if antialised:
        group = SmoothLineGroup()
    else:
        group = None
    return batch.add(vertices_count, GL_LINES, group, ('v2f', l),
        ('c4B', color*vertices_count))

def add_line(batch, x1, y1, x2, y2, color, group=None):
  l = (x1, y1, x2, y2)
  return batch.add(2, GL_LINES, group, ('v2f', l),
        ('c4B', color*2))

def add_arrow(batch, width, height, color, group=None):
  half_width = int(width / 2)
  half_height = int(height / 2)
  arrow_verts = (0, 0,
               -half_width,+half_height,
               width, 0,
               -half_width, -half_height)
  arrow_indices = [0, 1, 2, 0, 2, 3]
  batch.add_indexed(4, GL_TRIANGLES, group,
      arrow_indices,
      ('v2i', arrow_verts),
      ('c4B', color * 4))


# -*- coding: utf-8 -*-
import collections

(
    CODE_UNKNOWN,
    CODE_CLOUDY,
    CODE_FOG,
    CODE_HEAVY_RAIN,
    CODE_HEAVY_SHOWERS,
    CODE_HEAVY_SNOW,
    CODE_HEAVY_SNOW_SHOWERS,
    CODE_LIGHT_RAIN,
    CODE_LIGHT_SHOWERS,
    CODE_LIGHT_SLEET,
    CODE_LIGHT_SLEET_SHOWERS,
    CODE_LIGHT_SNOW,
    CODE_LIGHT_SNOW_SHOWERS,
    CODE_PARTLY_CLOUDY,
    CODE_SUNNY,
    CODE_THUNDERY_HEAVY_RAIN,
    CODE_THUNDERY_SHOWERS,
    CODE_THUNDERY_SNOW_SHOWERS,
    CODE_VERY_CLOUDY
 ) = range(19)

(
    UNITS_METRIC,
    UNITS_IMPERIAL,
    UNITS_SI
) = ('metric', 'imperial', 'si')

ThresholdColor = collections.namedtuple('ThresholdColor', 'threshold color')

class DataObject(object):
    __slots__ = ()
    default_value = None

    def __init__(self, *args, **kwargs):
        for att in self.__slots__:
            setattr(self, att, self.default_value)
        for k, v in zip(self.__slots__, args):
            setattr(self, k, v)
        for k, v in kwargs.items():
            setattr(self, k, v)

    def to_json(self):
        return {attr: getattr(self, attr) for attr in self.__slots__}

class Cond(DataObject):
    __slots__ = (
                  'time',
                  'code',
                  'desc',
                  'temp_c',
                  'feels_like_c',
                  'chance_of_rain_percent',
                  'precip_m',
                  'visible_dist_m',
                  'windspeed_kmph',
                  'wind_gust_kmph',
                  'winddir_degree',
                  'humidity'
                )

class Astro(DataObject):
    __slots__ = ('moonrise', 'moonset', 'sunrise', 'sunset')

class Day(DataObject):
    __slots__ = ("date", "slots")

class LatLon(DataObject):
    __slots__ = ("latitude", "longitude")

class Data(DataObject):
    __slots__ = ("current", "forecast", "location", "geoloc")

class UnitSystem(object):
    def __init__(self, unit):
        if unit not in (UNITS_METRIC, UNITS_IMPERIAL, UNITS_SI):
            raise Exception('unsupport unit system %s' % unit)
        self.unit = unit

    def temp(self, temp_c):
        if self.unit == UNITS_METRIC:
            return temp_c, "°C"
        elif self.unit == UNITS_IMPERIAL:
            return temp_c*1.8 + 32, "°F"
        elif self.unit == UNITS_SI:
            return temp_c + 273.16, "°K"

    def speed(self, spd_kmph):
        if self.unit == UNITS_METRIC:
            return spd_kmph, "km/h"
        elif self.unit == UNITS_IMPERIAL:
            return spd_kmph / 1.609, "mph"
        elif self.unit == UNITS_SI:
            return spd_kmph / 3.6, "m/s"

    def distance(self, dist_m):
        if self.unit == UNITS_METRIC or self.unit == UNITS_SI:
            if dist_m < 1:
                return dist_m * 1000, "mm"
            if dist_m < 1000:
                return dist_m, "m"
            return dist_m/1000, "km"
        elif self.unit == UNITS_IMPERIAL:
            res, unit = dist_m/0.0254, "in"
            if res < 3*12:
                return res, unit
            elif res < 8*10*22*36:
                return res / 36, "yd"
            return res / 8 / 10 / 22 / 36, "mi"

class Backend(object):
    def fetch(self, arg_ns):
        raise NotImplementedError

class Frontend(object):
    def render(self, data, unit):
        raise NotImplementedError


import argparse
def main():
    parser = argparse.ArgumentParser(description="""""")
    parser.add_argument('--preds')
    parser.add_argument('--feats')
    args = parser.parse_args()

    s_preds = []
    s_gold = []
    for line in open(args.preds).readlines():
        line = line.strip().split("  ")[0].replace(" ","")
        s_preds.append(line)
    acc = ""
    for line in open(args.feats).readlines():
        line = line.strip()
        if line:
            acc+=(line.split(" ")[0])
        else:
            s_gold.append(acc)
            acc = ""
    if acc:
        s_gold.append(acc)

    totalsymbols = 0
    totalmatches = 0
    for p,g in zip(s_preds,s_gold):
        if len(p) != len(g):
            print("error")
            print(p)
            print(g)
        else:
            totalsymbols+=len(p)
            totalmatches+= sum([p_i == g_i for p_i, g_i in zip(p,g)])

    print("acuracy=",totalmatches/totalsymbols)

if __name__ == "__main__":
    main()


import base64
import binascii
# The string:
# 49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d
# Should produce:
# SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t
# So go ahead and make that happen. You'll need to use this code for the rest of the exercises.
#
# Cryptopals Rule
# Always operate on raw bytes, never on encoded strings. Only use hex and base64 for pretty-printing.

def hex_to_base64(input):
    # Do Calculations and return answer
    # Solution Here:

    #Convert Hex to bytes as it says in directions
    hex_string_in_bytes = bytes.fromhex(input)

    print("Easter Egg: Result of converting hex to bytes: ", hex_string_in_bytes)

    #Convert the Hex Byte String to a base64 byte string
    base64_byte_string = base64.b64encode(hex_string_in_bytes)

    #Convert the base64 byte string to base64 binary  ascii characters
    base64_ascii_characters = binascii.b2a_base64(base64_byte_string)

    # Decode the base64 byte string
    decoded_base64_characters = base64.b64decode(base64_ascii_characters)

    #Convert decoded_base64_characters to utf-8 string and return
    return decoded_base64_characters.decode('utf-8')


from __future__ import annotations

import datetime
import typing

from pydantic import BaseModel, validator

from domain.user.user_schema import User

class AnswerCreate(BaseModel):
    content: str

    @validator('content')
    def not_empty(cls, v):
        if not v or not v.strip():
            raise ValueError('값이 없습니다')
        return v

class Answer(BaseModel):
    id: int
    content: str
    create_date: datetime.datetime
    user: typing.Union[User, None]
    question_id: int
    modify_date: typing.Union[datetime.datetime, None] = None
    voter: list[User] = []

    class Config:
        orm_mode = True

class AnswerUpdate(AnswerCreate):
    answer_id: int

class AnswerDelete(BaseModel):
    answer_id: int

class AnswerVote(BaseModel):
    answer_id: int


import cv2
import numpy as np
from pycocotools.coco import COCO
from pycocotools.cocoeval import COCOeval
from detection.utils import xyxy2xywh

def coco_decetion_eval(pred_file: str, ann_file: str):
    cocoGt = COCO(ann_file)
    cocoDt = cocoGt.loadRes(pred_file)
    cocoEval = COCOeval(cocoGt, cocoDt, "bbox")
    cocoEval.evaluate()
    cocoEval.accumulate()
    cocoEval.summarize()

def coco80_to_coco91_class():
    # converts 80-index (val2014) to 91-index
    # https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/
    return [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 28, 31, 32, 33, 34,
        35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
        64, 65, 67, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 86, 87, 88, 89, 90]

CLASSES = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
           'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
           'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
           'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
           'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
           'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch',
           'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone',
           'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear',
           'hair drier', 'toothbrush']  # coco80类别

def draw_box(image_path, box_data):
    img = cv2.imread(image_path)
    boxes = box_data[..., :4].astype(np.int32)
    scores = box_data[..., 4]
    classes = box_data[..., 5].astype(np.int32)

    for box, score, cl in zip(boxes, scores, classes):
        top, left, right, bottom = box
        print('class: {}, score: {}'.format(CLASSES[cl], score))
        print('box coordinate left,top,right,down: [{}, {}, {}, {}]'.format(
            top, left, right, bottom))
        cv2.rectangle(img, (top, left), (right, bottom), (255, 0, 0), 2)
        cv2.putText(img, '{0} {1:.2f}'.format(CLASSES[cl], score),
                    (top, left),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    0.6, (0, 0, 255), 2)
    cv2.imshow('res', img)
    cv2.waitKey(20000)

def encode_results(boxes_list: list[np.ndarray], img_ids: list, class_map):
    res = []
    for i in range(len(img_ids)):
        img_id = img_ids[i]
        boxes = boxes_list[i]
        box_poses = xyxy2xywh(boxes[:, :4])
        # xy center to top-left corner
        box_poses[:, :2] -= box_poses[:, 2:] / 2
        for j in range(box_poses.shape[0]):
            box_pos = box_poses[j]
            box = boxes[j]
            res.append({'image_id': img_id,
                        'category_id': class_map[int(box[5])],
                        'bbox': [float(round(pos, 3)) for pos in box_pos],
                        'score': float(round(box[4], 5))})
    return res


import subprocess
import sys

from pathlib import Path

mpy_cross = '/home/pi/circuitpython/workdirs/test/circuitpython/mpy-cross/mpy-cross'

src = Path('lib')

for py in src.glob('**/*.py'):
    mpy = py.with_suffix('.mpy')
    if mpy.exists() and mpy.stat().st_mtime > py.stat().st_mtime:
        #print('skip:', mpy)
        continue

    print('compile:', mpy)
    subprocess.run([mpy_cross, '-o', str(mpy), str(py)])


#!/usr/bin/python3

import os
import sys
import mmap
import resource

from typing import Tuple

class COLORS:
    BLACK = "\033[30m"
    RED = "\033[31m"
    GREEN = "\033[32m"
    YELLOW = "\033[33m"
    BLUE = "\033[34m"
    MAGENTA = "\033[35m"
    CYAN = "\033[36m"
    WHITE = "\033[37m"

    RESET = "\033[0m"

# NOTE: Modify this table based on your hardware
# below example is based on UART0 of this: https://www.xilinx.com/html_docs/registers/ug1087/ug1087-zynq-ultrascale-registers.html
registerSpecification = {
    # NOTE: For all numeric values, they can be either dec, bin or hex
    # bin and hex need to be prefixed with 0b and 0x respectively
    #
    # base address all registers offset are based on
    "BaseAddress": "0x00FF000000",
    # How many bits to access when reading a register
    "AccessWidth": "32",
    # Endianess of the register, can be "little" or "big"
    "Endian": "little",
    #
    # example object that describes all the relevant field of the register
    # it will only print the fields you specified so you can skip over unimportant fields like
    # reserved

    # the key is the name of register and its offset from base
    # the value is an object describing the different fields in a register
    "mode_reg0:0x04": {
        "5:3": {
            "Name": "Parity type select",
            # optional, by default, use hex, can be bin, dec or hex
            "DisplayNumberBase": "bin",

            # there are two ways to describe a field, with a list of enum
            # or with a string
            # the below demonstrates the enum case
            "ValueDescription": {
                "0b000": "even parity",
                "0b001": "odd parity",
                "0b010": "forced to 0 parity (space)",
                "0b011": "forced to 1 parity (mark)",
                "*": "no parity",
            }
        },
        "2:1": {
            "Name": "Character length select",
            "DisplayNumberBase": "bin",
            "ValueDescription": {
                "0b11": "6 bits",
                "0b10": "7 bits",
                "*": "8 bits",
            }
        },
        "0": {
            "Name": "Clock source select",
            "DisplayNumberBase": "bin",
            "ValueDescription": {
                "0b000": "UART_REF_CLK",
                "0b001": "UART_REF_CLK/8",
            }
        }
    },
    "Rcvr_timeout_reg0:0x1C": {
        "7:0": {
            "Name": "Receiver timeout value",
            "DisplayNumberBase": "hex",

            # this shows the non enum case where this string is always displayed
            # regardless of the register's value, useful for open ended parameter
            "ValueDescription": "Specify Value of Receiver Timeout"
        }
    }
}

resultRowFormat = "{:<4}{:<50}{:<10}{:}"

def stringWithBaseToNum(str: str) -> int:
    """Convert a string that has base prefix like 0x to its int value"""
    numBase = str[0:2].lower()
    if numBase == "0x":
        return int(str, 16)
    if numBase == "0b":
        return int(str, 2)

    if not numBase.isdecimal():
        raise ValueError(f"Error: Can't convert number with base: {numBase}")

    return int(str, 10)

def printResultTableEntry(entry: Tuple) -> None:
    print(resultRowFormat.format("", *entry))

def parseRegId(regId: str) -> Tuple[str, int, str]:
    tmpArray = regId.split(":")
    if len(tmpArray) != 2:
        raise ValueError(f"Error: Invalid register ID: {regId}")

    return tmpArray[0], stringWithBaseToNum(tmpArray[1]), tmpArray[1]

def getRegValue(regAbsAddr: int, accessWidthInBit: int, endian: str) -> int:
    devmemFd = os.open("/dev/mem", os.O_RDONLY | os.O_SYNC)
    pageSize = resource.getpagesize()
    mappedOffset = regAbsAddr & (~(pageSize - 1))
    inPageOffset = regAbsAddr - mappedOffset

    with mmap.mmap(
            fileno=devmemFd,
            length=pageSize * 2,
            flags=mmap.MAP_SHARED,
            prot=mmap.PROT_READ,
            offset=mappedOffset) as mm:
        mm.seek(inPageOffset)
        bytesArray = mm.read(int(accessWidthInBit / 8))
        ret = int.from_bytes(bytesArray, byteorder=endian, signed=False)

    os.close(devmemFd)

    return ret

def getBitfieldShiftedVal(regVal: int, bitfieldRange: str) -> int:
    tmpArray = bitfieldRange.split(":")

    firstNum = stringWithBaseToNum(tmpArray[0])
    if len(tmpArray) == 1:
        secondNum = firstNum
    else:
        secondNum = stringWithBaseToNum(tmpArray[1])

    startBit = min(firstNum, secondNum)
    endBit = max(firstNum, secondNum)

    bitMask = 0
    for bit in range(startBit, endBit + 1):
        bitMask = bitMask | (1 << bit)

    return (regVal & bitMask) >> startBit

def printBitfield(regVal: int, bitfieldRange: str, bitfieldDescription: object) -> None:
    maskedVal = getBitfieldShiftedVal(regVal, bitfieldRange)

    if "DisplayNumberBase" not in bitfieldDescription:
        displayVal = f"0x{format(maskedVal, 'x')}"
    else:
        if bitfieldDescription["DisplayNumberBase"] == "hex":
            displayVal = f"0x{format(maskedVal, 'x')}"
        elif bitfieldDescription["DisplayNumberBase"] == "dec":
            displayVal = f"{format(maskedVal, 'd')}"
        elif bitfieldDescription["DisplayNumberBase"] == "bin":
            displayVal = f"0b{format(maskedVal, 'b')}"
        else:
            raise ValueError(
                f"Error: Invalid display base: {bitfieldDescription['DisplayNumberBase']}")

    valueDescription = bitfieldDescription['ValueDescription']
    if isinstance(valueDescription, str):
        description = valueDescription
    else:
        wildCardDescription = valueDescription.get('*')
        description = "Unknown" if wildCardDescription == None else wildCardDescription
        valueDescription.pop('*', None)

        for val, valDescription in valueDescription.items():
            if stringWithBaseToNum(val) == maskedVal:
                description = valDescription
                break

    printResultTableEntry(
        (f"{COLORS.CYAN}{bitfieldDescription['Name'] + ' Field'}{COLORS.RESET}", displayVal, f"{COLORS.BLUE}{description}{COLORS.RESET}"))

def getRequiredIntValue(regSetDescription: dict, key: str) -> int:
    if key not in regSetDescription:
        raise ValueError(
            f"Error: Missing {key} property")

    ret = stringWithBaseToNum(regSetDescription[key])
    del regSetDescription[key]

    return ret

def main() -> int:
    baseAddr = getRequiredIntValue(registerSpecification, "BaseAddress")
    accessWidthInBit = getRequiredIntValue(registerSpecification, "AccessWidth")

    if "Endian" not in registerSpecification:
        raise ValueError(
            "Error: Missing Endian property")

    endian = registerSpecification["Endian"]
    del registerSpecification["Endian"]

    for regId, bitfieldDescriptionList in registerSpecification.items():
        regName, regOffset, regOffsetStr = parseRegId(regId)
        print(
            f"{COLORS.YELLOW}{regName} Register(offset {regOffsetStr}){COLORS.RESET}")

        regVal = getRegValue(baseAddr + regOffset, accessWidthInBit, endian)

        for bitfieldRange, bitfieldDescription in bitfieldDescriptionList.items():
            printBitfield(regVal, bitfieldRange, bitfieldDescription)

        print("\n")

    return 0

if __name__ == '__main__':
    sys.exit(main())  # next section explains the use of sys.exit


from os import listdir
from skimage.morphology import disk, square
from utils import populateFiles, readImages, saveImageFile, applyCustomThreshold, applyOtsuThreshold, applyAdaptiveThreshold

# Let's apply median, gaussian and mean
# all of the morphed images

sourceDir = 'step2'
destDir = 'step3'

# get morphed images
files = populateFiles(sourceDir)

# read each file and populate a dictionary
images = readImages(sourceDir, files)

# iterate over dictionary and apply thresholds
for name, image in images.items():
    # let's derive a custom threshold from the histogram, say 75
    customImage = applyCustomThreshold(image, 75)
    saveImageFile(customImage, destDir + '/' + name + '_custom75.bmp')
    otsuImage, thr = applyOtsuThreshold(image)
    saveImageFile(otsuImage, destDir + '/' + name + '_otsu' + str(thr) + '.bmp')
    # blocksize 35 and offset 5 seems OK
    adaptiveImage = applyAdaptiveThreshold(image, 35, 3)
    saveImageFile(adaptiveImage, destDir + '/' + name + '_adaptive.bmp')


from typing import TYPE_CHECKING, Optional
from ortools.linear_solver import pywraplp
if TYPE_CHECKING:
    from Entity import Team
    from Enum import Position
    from Optimizer.MathModel import MathModel

class Player:
    def __init__(self, name: str, team: "Team", position: "Position", price: float, pick: float = 0.0, minutes: int = 0,
                 points: int = 0, goals: int = 0, assists: int = 0, bonus: int = 0, bps: float = 0.0):
        self.name: str = name
        self.team: "Team" = team
        self.position: "Position" = position
        self.price: float = float(str(price).replace('Â£', '').replace('M', ''))
        self.pick: float = float(str(pick).replace('%', ''))
        self.minutes: int = int(minutes)
        self.points: int = int(points)
        self.goals: int = int(goals)
        self.assists: int = int(assists)
        self.bonus: int = int(bonus)
        self.bps: float = float(bps)
        self.selectionVariable: Optional[pywraplp.Variable] = None
        self.benchVariable: Optional[pywraplp.Variable] = None

    def initializeVariable(self, model: "MathModel") -> None:
        self.selectionVariable = model.solver.IntVar(0, 1, f'main_select_{self.key()}')
        self.benchVariable = model.solver.IntVar(0, 1, f'bench_select_{self.key()}')

    def key(self, price:bool = True) -> str:
        firstP = f"{self.name}({self.team.name})({self.position.name})"
        secondP = f"({self.price})" if price else ''
        return firstP + secondP

    def secondaryValue(self) -> float:
        return 0.01 * (self.points + self.goals + self.assists + self.pick + self.minutes)


# -*- coding: utf-8 -*-
"""
Created on Fri Jul 29 06:59:01 2016

@author: mike
"""

from utils import cutoff_funcs
from numpy import arange, array
reload(cutoff_funcs)

#### Plot of scaling factor
t = logspace(0.0, log10(10000), num=250).astype(int)
x = array([cutoff_funcs.fdr_func(arange(1.0, 1.0 +n) / float(n)) for n in t])

####

alpha_fdr = .1
beta_fdr = .05
N = 10

alpha_vec = cutoff_funcs.create_fdr_controlled_alpha(alpha_fdr,
                                                     arange(1.0, 1.0 + N))
beta_vec = cutoff_funcs.create_fdr_controlled_alpha(beta_fdr,
                                                    arange(1.0, 1.0 + N))


from abc import abstractmethod
from functools import reduce
from typing import Callable, Any, List
import random
from .benchmark import *
from copy import copy

class PopulationBasedHeuristics(object):
    """
    Population Based Heuristic Algorithm Template Class.
    """

    def __init__(self, objective_function: Callable[..., Any], soft_constrains: List[Callable[..., Any]] = None,
                 hard_constrains: List[Callable[..., Any]] = None, ranges: list = None, population_size: int = 32,
                 smooth: bool = False, epsilon: float = 10**-4, **params):
        r"""
        Initialize Population Based Heuristic algorithm.

        e.g.,

        objective_function:

        .. code-block:: python
            def objective_function(*x):
                return x[0]**2 + x_[1]**2

        soft_constrains $g_{i} (x) <= 0$:

        .. code-block:: python
            def g1(*x) -> float or int:
                return x[0]

            def g2(*x) -> float or int:
                return x[1]

            soft_constrains = [g1, g2]

        hard_constrains $h_{i} (x) == 0$:

        .. code-block:: python
            def h1(*x) -> float or int:
                return x[0]

            def h2(*x) -> float or int:
                return x[1]

            hard_constrains = [h1, h2]

        ranges:

            variables ranges:

        .. code-block:: python
            ranges = [[0, 100], [0, 90]]

        :param FunctionType objective_function: Method to evaluate objective function
        :param list[FunctionType] soft_constrains: List of Soft constrains methods
        :param list[FunctionType] hard_constrains: List of Hard constrains methods
        :param list[list[float, float]] ranges: List of variables ranges
        :return None:
        """
        self.objective_function = objective_function
        self.soft_constrains = soft_constrains
        self.hard_constrains = hard_constrains
        self.ranges = ranges
        self.dimension = len(ranges)
        self.population_size = population_size
        self.population: List[dict] = []
        self.history: List[dict] = []
        self.smooth: bool = smooth
        self.epsilon: float = epsilon
        self.custom_population: list = []
        self.function_evaluations: int = 0
        self.max_function_evaluations: int = params.get("max_fes", 10**10)

        # Check if population has enough size
        if self.population_size <= 0:
            raise ValueError("""
            Population size is not valid. Population size
            must be greater than 0
            """)

    def set_custom_population(self, custom_population: list) -> None:
        """
        Sets custom initial population
        :custom_population list: Custom initial population
        :return None:
        """
        self.custom_population = custom_population

    def create_individual(self) -> dict:
        """
        Creates individual randomly
        :return dict: Random individual
        """
        return {
            "x": [random.uniform(min(ri), max(ri))
                  for ri in self.ranges],
            "fx": None,
            "gx": None,
            "hx": None,
        }

    def fix_ranges(self, individual: dict) -> dict:
        """
        Fix ranges of an individual

        Checks if and individual is in the search space, if not replace it with a new random one
        :param dict individual: Individual
        :return dict: Fixed Individual
        """
        for j in range(self.dimension):
            sup = max(self.ranges[j])
            inf = min(self.ranges[j])
            x_j = individual["x"][j]
            if x_j < inf or x_j > sup:
                individual["x"][j] = random.uniform(inf, sup)
        return individual

    def create_population(self) -> List[dict]:
        r"""
        Creates population randomly

        e.g.,

        [
            { "x": [0.10, 0.20], fx: None, gx: None, hx: None },
            { "x": [0.05, 0.02], fx: None, gx: None, hx: None },
            { "x": [0.85, 0.72], fx: None, gx: None, hx: None }
        ]
        :custom_population list: Custom population
        :return list[dict]: Random population
        """
        if self.custom_population:
            return self.custom_population
        return [
            self.create_individual()
            for _ in range(self.population_size)
        ]

    def evaluate_population(self, population: List[dict]) -> List[dict]:
        r"""
        Evaluates objective function and constrains for a given population

        e.g.,

        Population:
        .. code-block:: json
        [
            { "x": [0.10, 0.20], fx: None, gx: None, hx: None },
            { "x": [0.05, 0.02], fx: None, gx: None, hx: None },
            { "x": [0.85, 0.72], fx: None, gx: None, hx: None }
        ]

        Evaluated population:

        .. code-block:: json
        [
            { "x": [0.10, 0.20], fx: 10.340, gx: 0, hx: 3.282 },
            { "x": [0.05, 0.02], fx: 1.0010, gx: 0, hx: -0.01 },
            { "x": [0.85, 0.72], fx: -35.02, gx: 0, hx: 5.439 }
        ]
        :return list[dict]: Return evaluated population
        """
        return list(map(self.evaluate_individual, population))

    def update_fes(self):
        self.function_evaluations += 1

    def evaluate_individual(self, individual: dict) -> dict:
        """
        Evaluates objective function and constrains for a single individual
        :param dict individual: Individual
        :return dict: Evaluated Individual
        """
        self.update_fes()
        individual["fx"] = self.objective_function(*individual["x"])
        individual["gx"] = sum([gi(*individual["x"]) * (gi(*individual["x"]) > 0)
                               for gi in self.soft_constrains])
        if self.smooth:
            individual["hx"] = sum([(abs(hi(*individual["x"])) - self.epsilon) *
                                    (abs(hi(*individual["x"])) > self.epsilon)
                                    for hi in self.hard_constrains])
        else:
            individual["hx"] = sum([abs(hi(*individual["x"])) * (hi(*individual["x"]) != 0)
                                    for hi in self.hard_constrains])
        individual["fes"] = copy(self.function_evaluations)

        return individual

    def get_best(self, population: List[dict], exclude_list: list = []) -> dict:
        """
        Gets the best individual for a given population using DEB conditions
        :population List[dict]: Population list
        :exclude_list list: List of excluded indexes
        :return dict: Best individual
        """
        pop = [{**population[j], "index": j} for j in range(len(population))
               if j not in exclude_list]
        return reduce(self.comparison, pop)

    @staticmethod
    def comparison(xi: dict, xj: dict) -> dict:
        """
        Compares to vectors evaluating DEB conditions
        :param dict xi: Vector i
        :param dict xj: Vector j
        :return dict: Return best vector based on DEB conditions
        """
        svr_i = xi["gx"] + xi["hx"]
        svr_j = xj["gx"] + xj["hx"]
        if svr_i <= 0 and svr_j <= 0:
            if xi["fx"] < xj["fx"]:
                return xi
            else:
                return xj
        elif svr_i > 0 and svr_j > 0:
            if svr_i < svr_j:
                return xi
            else:
                return xj
        else:
            if svr_i <= 0:
                return xi
            else:
                return xj

    @abstractmethod
    def population_enhancement(self) -> None:
        pass

    @abstractmethod
    def stop_condition(self) -> None:
        pass

    def save_history(self, iteration: int) -> None:
        """
        Save search data
        :param int iteration: Current iteration
        :return None:
        """
        best_individual = self.get_best(self.population)
        self.history.append({**best_individual, "iteration": iteration + 1,
                             "fes": self.function_evaluations})

    def search(self, iterations: int = 100, save_history: bool = False):
        """
        Search method
        :param int iterations: Number of iterations
        :param bool save_history: If True algorthm will store each iteration data
        :return None:
        """
        self.history = []
        initial_population = self.create_population()
        self.population = copy(self.evaluate_population(initial_population))
        for iteration in range(iterations):
            if self.stop_condition():
                break
            self.population_enhancement()
            if save_history:
                self.save_history(iteration)
            if self.function_evaluations > self.max_function_evaluations:
                break

if __name__ == "__main__":
    pass


import json
from dbnew import DB

db = DB("GNCorp.db")

create_employees = """
INSERT INTO employees (
    first_name,
    last_name,
    team_id,
    leader_id,
    title,
    description
    )
VALUES (?, ?, ? ,? ,? ,?)
"""
create_team ="""
INSERT INTO teams (
    team_name,
    leader_id,
    description
)
VALUES (?, ?, ?)
"""

with open("seed_all.json", "r") as seed:
    data = json.load(seed)

    for emp in data["employees"]:
        db.call_db(create_employees,
                   emp["first_name"],
                   emp["last_name"],
                   emp["team_id"],
                   emp["leader_id"],
                   emp["title"],
                   emp["description"])

    for team in data["teams"]:
        db.call_db(create_team,
                   team["team_name"],
                   team["leader_id"],
                   team["description"])

print(emp)
print(team)


from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.sgettext
import logging
log = logging.getLogger(".")
import re

#------------------------------------------------------------------------
#
# GNOME/GTK modules
#
#------------------------------------------------------------------------
from gi.repository import Gdk

#------------------------------------------------------------------------
#
# GRAMPS modules
#
#------------------------------------------------------------------------
from gramps.gen.plug.docgen import (StyleSheet, FONT_SERIF, FONT_SANS_SERIF,
            PARA_ALIGN_RIGHT, PARA_ALIGN_CENTER, PARA_ALIGN_LEFT,
            PARA_ALIGN_JUSTIFY)
from gramps.gen.constfunc import cuni
from ...listmodel import ListModel
from ...managedwindow import set_titles
from ...glade import Glade

#------------------------------------------------------------------------
#
# StyleListDisplay class
#
#------------------------------------------------------------------------
class StyleListDisplay(object):
    """
    Shows the available paragraph/font styles. Allows the user to select,
    add, edit, and delete styles from a StyleSheet.
    """

    def __init__(self, stylesheetlist, callback, parent_window):
        """
        Create a StyleListDisplay object that displays the styles in the
        StyleSheet.

        stylesheetlist - styles that can be editied
        callback - task called with an object has been added.
        """
        self.callback = callback

        self.sheetlist = stylesheetlist

        self.top = Glade(toplevel='styles')
        self.window = self.top.toplevel

        set_titles(self.window, self.top.get_object('title'),
                   _('Document Styles'))

        self.top.connect_signals({
            "destroy_passed_object" : self.__close,
            "on_ok_clicked" : self.on_ok_clicked,
            "on_add_clicked" : self.on_add_clicked,
            "on_delete_clicked" : self.on_delete_clicked,
            "on_button_press" : self.on_button_press,
            "on_edit_clicked" : self.on_edit_clicked,
            "on_save_style_clicked" : dummy_callback,
            })

        self.list = ListModel(self.top.get_object("list"),
                                        [(_('Style'), -1, 10)], )
        self.redraw()
        if parent_window:
            self.window.set_transient_for(parent_window)
        self.window.run()
        self.window.destroy()

    def __close(self, obj):
        self.top.destroy()

    def redraw(self):
        """Redraws the list of styles that are current available"""

        self.list.model.clear()
        self.list.add([_("default")])

        index = 1
        for style in sorted(self.sheetlist.get_style_names()):
            if style == "default":
                continue
            self.list.add([style])
            index += 1

    def on_add_clicked(self, obj):
        """Called with the ADD button is clicked. Invokes the StyleEditor to
        create a new style"""
        style = self.sheetlist.get_style_sheet("default")
        StyleEditor(_("New Style"), style, self)

    def on_ok_clicked(self, obj):
        """Called with the OK button is clicked; Calls the callback task,
        then saves the stylesheet."""
        if self.callback is not None:
            self.callback()
        try:
            self.sheetlist.save()
        except IOError as msg:
            from ...dialog import ErrorDialog
            ErrorDialog(_("Error saving stylesheet"), str(msg))
        except:
            log.error("Failed to save stylesheet", exc_info=True)

    def on_button_press(self, obj, event):
        if event.type == Gdk.EventType._2BUTTON_PRESS and event.button == 1:
            self.on_edit_clicked(obj)

    def on_edit_clicked(self, obj):
        """
        Called with the EDIT button is clicked.
        Calls the StyleEditor to edit the selected style.
        """
        store, node = self.list.selection.get_selected()
        if not node:
            return

        name = cuni(self.list.model.get_value(node, 0))
        if name == _('default'): # the default style cannot be edited
            return
        style = self.sheetlist.get_style_sheet(name)
        StyleEditor(name, style, self)

    def on_delete_clicked(self, obj):
        """Deletes the selected style."""
        store, node = self.list.selection.get_selected()
        if not node:
            return
        name = cuni(self.list.model.get_value(node, 0))
        if name == _('default'): # the default style cannot be removed
            return
        self.sheetlist.delete_style_sheet(name)
        self.redraw()

#------------------------------------------------------------------------
#
# StyleEditor class
#
#------------------------------------------------------------------------
class StyleEditor(object):
    """
    Edits the current style definition. Presents a dialog allowing the values
    of the paragraphs in the style to be altered.
    """

    def __init__(self, name, style, parent):
        """
        Create the StyleEditor.

        name - name of the style that is to be edited
        style - style object that is to be edited
        parent - StyleListDisplay object that called the editor
        """
        self.current_p = None
        self.current_name = None

        self.style = StyleSheet(style)
        self.parent = parent
        self.top = Glade(toplevel='editor')
        self.window = self.top.toplevel

        self.top.connect_signals({
            "on_save_style_clicked" : self.on_save_style_clicked,
            "destroy_passed_object" : self.__close,
            "on_ok_clicked" : dummy_callback,
            "on_add_clicked" : dummy_callback,
            "on_delete_clicked" : dummy_callback,
            "on_button_press" : dummy_callback,
            "on_edit_clicked" : dummy_callback,
            })

        self.pname = self.top.get_object('pname')
        self.pdescription = self.top.get_object('pdescription')

        set_titles(self.window, self.top.get_object('title'),
                   _('Style editor'))
        self.top.get_object("label6").set_text(_("point size|pt"))

        titles = [(_('Paragraph'), 0, 130)]
        self.plist = ListModel(self.top.get_object("ptree"), titles,
                                         self.change_display)

        self.top.get_object('color').connect('color-set', self.fg_color_set)
        self.top.get_object('bgcolor').connect('color-set', self.bg_color_set)
        self.top.get_object("style_name").set_text(name)

        def _alphanumeric_sort(iterable):
            """ sort the given iterable in the way that humans expect """
            convert = lambda text: int(text) if text.isdigit() else text
            sort_key = lambda k: [convert(c) for c in re.split('([0-9]+)', k)]
            return sorted(iterable, key=sort_key)

        names = _alphanumeric_sort(self.style.get_paragraph_style_names())
        for p_name in names:
            self.plist.add([p_name], self.style.get_paragraph_style(p_name))
        self.plist.select_row(0)

        if self.parent:
            self.window.set_transient_for(parent.window)
        self.window.run()
        self.window.destroy()

    def __close(self, obj):
        self.window.destroy()

    def draw(self):
        """Updates the display with the selected paragraph."""

        p = self.current_p
        self.pname.set_text( '%s' %
                             self.current_name)
        self.pname.set_use_markup(True)

        descr = p.get_description()
        self.pdescription.set_text(descr or _("No description available") )

        font = p.get_font()
        self.top.get_object("size").set_value(font.get_size())
        if font.get_type_face() == FONT_SERIF:
            self.top.get_object("roman").set_active(1)
        else:
            self.top.get_object("swiss").set_active(1)
        self.top.get_object("bold").set_active(font.get_bold())
        self.top.get_object("italic").set_active(font.get_italic())
        self.top.get_object("underline").set_active(font.get_underline())
        if p.get_alignment() == PARA_ALIGN_LEFT:
            self.top.get_object("lalign").set_active(1)
        elif p.get_alignment() == PARA_ALIGN_RIGHT:
            self.top.get_object("ralign").set_active(1)
        elif p.get_alignment() == PARA_ALIGN_CENTER:
            self.top.get_object("calign").set_active(1)
        else:
            self.top.get_object("jalign").set_active(1)
        self.top.get_object("rmargin").set_value(p.get_right_margin())
        self.top.get_object("lmargin").set_value(p.get_left_margin())
        self.top.get_object("pad").set_value(p.get_padding())
        self.top.get_object("tmargin").set_value(p.get_top_margin())
        self.top.get_object("bmargin").set_value(p.get_bottom_margin())
        self.top.get_object("indent").set_value(p.get_first_indent())
        self.top.get_object("tborder").set_active(p.get_top_border())
        self.top.get_object("lborder").set_active(p.get_left_border())
        self.top.get_object("rborder").set_active(p.get_right_border())
        self.top.get_object("bborder").set_active(p.get_bottom_border())

        self.fg_color = font.get_color()
        c = Gdk.Color(self.fg_color[0] << 8,
                  self.fg_color[1] << 8,
                  self.fg_color[2] << 8)
        self.top.get_object("color").set_color(c)
        self.top.get_object('color_code').set_text(
                                               "#%02X%02X%02X" % self.fg_color)

        self.bg_color = p.get_background_color()
        c = Gdk.Color(self.bg_color[0] << 8,
                  self.bg_color[1] << 8,
                  self.bg_color[2] << 8)
        self.top.get_object("bgcolor").set_color(c)
        self.top.get_object('bgcolor_code').set_text(
                                                "#%02X%02X%02X" % self.bg_color)

    def bg_color_set(self, x):
        c = x.get_color()
        self.bg_color = (c.red >> 8, c.green >> 8, c.blue >> 8)
        self.top.get_object('bgcolor_code').set_text(
                                                "#%02X%02X%02X" % self.bg_color)

    def fg_color_set(self, x):
        c = x.get_color()
        self.fg_color = (c.red >> 8, c.green >> 8, c.blue >> 8)
        self.top.get_object('color_code').set_text(
                                                "#%02X%02X%02X" % self.fg_color)

    def save_paragraph(self):
        """Saves the current paragraph displayed on the dialog"""
        p = self.current_p
        font = p.get_font()
        font.set_size(self.top.get_object("size").get_value_as_int())

        if self.top.get_object("roman").get_active():
            font.set_type_face(FONT_SERIF)
        else:
            font.set_type_face(FONT_SANS_SERIF)

        font.set_bold(self.top.get_object("bold").get_active())
        font.set_italic(self.top.get_object("italic").get_active())
        font.set_underline(self.top.get_object("underline").get_active())
        if self.top.get_object("lalign").get_active():
            p.set_alignment(PARA_ALIGN_LEFT)
        elif self.top.get_object("ralign").get_active():
            p.set_alignment(PARA_ALIGN_RIGHT)
        elif self.top.get_object("calign").get_active():
            p.set_alignment(PARA_ALIGN_CENTER)
        else:
            p.set_alignment(PARA_ALIGN_JUSTIFY)

        p.set_right_margin(self.top.get_object("rmargin").get_value())
        p.set_left_margin(self.top.get_object("lmargin").get_value())
        p.set_top_margin(self.top.get_object("tmargin").get_value())
        p.set_bottom_margin(self.top.get_object("bmargin").get_value())
        p.set_padding(self.top.get_object("pad").get_value())
        p.set_first_indent(self.top.get_object("indent").get_value())
        p.set_top_border(self.top.get_object("tborder").get_active())
        p.set_left_border(self.top.get_object("lborder").get_active())
        p.set_right_border(self.top.get_object("rborder").get_active())
        p.set_bottom_border(self.top.get_object("bborder").get_active())

        font.set_color(self.fg_color)
        p.set_background_color(self.bg_color)

        self.style.add_paragraph_style(self.current_name, self.current_p)

    def on_save_style_clicked(self, obj):
        """
        Saves the current style sheet and causes the parent to be updated with
        the changes.
        """
        name = cuni(self.top.get_object("style_name").get_text())

        self.save_paragraph()
        self.style.set_name(name)
        self.parent.sheetlist.set_style_sheet(name, self.style)
        self.parent.redraw()
        self.window.destroy()

    def change_display(self, obj):
        """Called when the paragraph selection has been changed. Saves the
        old paragraph, then draws the newly selected paragraph"""
        # Don't save until current_name is defined
        # If it's defined, save under the current paragraph name
        if self.current_name:
            self.save_paragraph()
        # Then change to new paragraph
        objs = self.plist.get_selected_objects()
        store, node = self.plist.get_selected()
        self.current_name =  store.get_value(node, 0)
        self.current_p = objs[0]
        self.draw()

def dummy_callback(obj):
    """Dummy callback to satisfy gtkbuilder on connect of signals.
    There are two widgets in the glade file, although only one is needed,
    the signals of the other must be connected too
    """
    pass


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

#import and create spark session
from pyspark.sql import SparkSession
import pyspark.sql.functions as f

spark = SparkSession.builder.master("local[*]").appName("BaseisII").config("spark.jars", "spark-measure_2.12-0.17.jar").getOrCreate()

#import csv
movies = spark.read.csv("/home/administrator/Downloads/movielens/movie.csv",inferSchema =True, header=True)
ratings = spark.read.csv("/home/administrator/Downloads/movielens/rating.csv",inferSchema =True, header=True)
tags = spark.read.csv("/home/administrator/Downloads/movielens/tag.csv",inferSchema =True, header=True)

#erotima 9
ratings.alias("ra1").join(ratings.alias("ra2"), on='timestamp')\
                    .where("ra1.timestamp == ra2.timestamp") \
                    .where("ra1.movieId == ra2.movieId")\
                    .where("ra1.userId != ra2.userId")\
                    .select(f.col('ra1.userId').alias('id1')).groupBy().count().show()

spark.stop()


from django.conf.urls import url

from users.views import show, log_in, reg_in, findback, personpage, gladdress, address, short_msg, choice_address, \
    address_change, address_delet, set_default

urlpatterns = [
    url(r'^$',show,name='主页'),
    url(r'^reg_in/$',reg_in,name='注册'),
    url(r'^log_in/$',log_in,name='登录'),
    url(r'^findback/$',findback,name='找回密码'),
    url(r'^infor/$',personpage,name='个人首页'),
    url(r'^gladdress/$',gladdress,name='收货地址显示页'),
    url(r'^address/$',address,name='添加'),
    url(r'^short_msg/$',short_msg,name='短信验证'),
    url(r'^choice_address/$',choice_address,name='添加收货地址'),
    url(r'^address_change/',address_change,name='修改收货地址'),
    url(r'^address_delet/',address_delet,name='删除收货地址'),
    url(r'^set_default/',set_default,name='修改默认地址'),
]


""" Username to UUID
 Converts a Minecraft username to it's UUID equivalent.

Uses the official Mojang API to fetch player data.

credit: https://github.com/EthanJoyce/MinecraftUsernameToUUID tnx buddy <3
"""
import http.client
import json

class UsernameToUUID:
    def __init__(self, username):
        self.username = username

    def get_uuid(self, timestamp=None):
        """
          Get the UUID of the player.

          Parameters
          ----------
          timestamp : long integer
              The time at which the player used this name, expressed as a Unix timestamp.
        """
        get_args = "" if timestamp is None else "?at=" + str(timestamp)

        http_conn = http.client.HTTPSConnection("api.mojang.com")
        http_conn.request("GET", "/users/profiles/minecraft/" + self.username + get_args,
            headers={'User-Agent':'Minecraft Username -> UUID', 'Content-Type':'application/json'})
        response = http_conn.getresponse().read().decode("utf-8")

        if not response and timestamp is None: # No response & no timestamp
            return self.get_uuid(0) # Let's retry with the Unix timestamp 0.
        if not response: # No response (player probably doesn't exist)
            return ""

        json_data = json.loads(response)
        try:
            uuid = json_data['id']
        except KeyError as e:
            print("KeyError raised:", e)

        return uuid


#!/usr/bin/env python

import numpy as np
import pandas as pd
import click as ck
from sklearn.metrics import classification_report
from sklearn.metrics.pairwise import cosine_similarity
import sys
from collections import deque
import time
import logging
from sklearn.metrics import roc_curve, auc, matthews_corrcoef
from scipy.spatial import distance
from scipy import sparse
import math
from utils import FUNC_DICT, Ontology, NAMESPACES
from matplotlib import pyplot as plt
import os

logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)

@ck.command()
@ck.option(
    '--data-root', '-dr', default='data',
    help='Prediction model')
@ck.option(
    '--ont', '-ont', default='mf',
    help='Prediction model')
def main(data_root, ont):
    train_data_file = f'data/{ont}/train_data.pkl'
    valid_data_file = f'{data_root}/{ont}/valid_data.pkl'
    test_data_file = f'{data_root}/{ont}/test_data.pkl'
    terms_file = f'{data_root}/{ont}/terms.pkl'

    go_rels = Ontology(f'data/go-basic.obo', with_rels=True)
    terms_df = pd.read_pickle(terms_file)
    terms = terms_df['gos'].values.flatten()
    terms_dict = {v: i for i, v in enumerate(terms)}

    train_df = pd.read_pickle(train_data_file)
    valid_df = pd.read_pickle(valid_data_file)
    train_df = pd.concat([train_df, valid_df])

    test_df = pd.read_pickle(test_data_file)

    annotations = train_df['prop_annotations'].values
    annotations = list(map(lambda x: set(x), annotations))

    prot_index = {}
    for i, row in enumerate(train_df.itertuples()):
        prot_index[row.proteins] = i

    prot_ac_index = {}
    for i, row in enumerate(train_df.itertuples()):
        prot_ac_index[row.accessions[0]] = i

    dsets = {'train': train_df, 'valid': valid_df, 'test': test_df}

    # BLAST Similarity (Diamond)
    for dset in ('test',):
        diamond_scores_file = f'{data_root}/{ont}/{dset}_diamond.res'
        diamond_scores = {}
        with open(diamond_scores_file) as f:
            for line in f:
                it = line.strip().split()
                if it[0] == it[1]: # Ignore same proteins (for train)
                    continue
                if it[0] not in diamond_scores:
                    diamond_scores[it[0]] = {}
                diamond_scores[it[0]][it[1]] = float(it[2])

        # scores_file = f'{data_root}/{ont}/{dset}_foldseek.m8'
        # scores = {}
        # with open(scores_file) as f:
        #     for line in f:
        #         it = line.strip().split()
        #         if it[0] == it[1]: # Ignore same proteins (for train)
        #             continue
        #         p1, p2 = it[0].split('.')[0], it[1].split('.')[0]
        #         if p1 not in scores:
        #             scores[p1] = {}
        #         scores[p1][p2] = float(it[2])

        # fold_preds = []
        # print(f'Preds for {dset}')
        # df = dsets[dset]
        # for i, row in enumerate(df.itertuples()):
        #     annots = {}
        #     prop_annots = {}
        #     prot_id = row.accessions[0]
        #     if prot_id in scores:
        #         sim_prots = scores[prot_id]
        #         allgos = set()
        #         total_score = 0.0
        #         for p_id, score in sim_prots.items():
        #             if p_id in prot_ac_index:
        #                 allgos |= annotations[prot_ac_index[p_id]]
        #                 total_score += score
        #         allgos = list(sorted(allgos))
        #         sim = np.zeros(len(allgos), dtype=np.float32)
        #         for j, go_id in enumerate(allgos):
        #             s = 0.0
        #             for p_id, score in sim_prots.items():
        #                 if p_id in prot_ac_index and go_id in annotations[prot_ac_index[p_id]]:
        #                     s += score
        #             sim[j] = s / total_score
        #         for go_id, score in zip(allgos, sim):
        #             annots[go_id] = score
        #         prop_annots = annots.copy()

        #         for go_id, score in annots.items():
        #             for sup_go in go_rels.get_ancestors(go_id):
        #                 if sup_go in prop_annots:
        #                     prop_annots[sup_go] = max(prop_annots[sup_go], score)
        #                 else:
        #                     prop_annots[sup_go] = score
        #     fold_preds.append(prop_annots)

        diam_preds = []
        print(f'Diamond preds for {dset}')
        df = dsets[dset]
        for i, row in enumerate(df.itertuples()):
            annots = {}
            prop_annots = {}
            prot_id = row.proteins
            # DiamondScore
            if prot_id in diamond_scores:
                sim_prots = diamond_scores[prot_id]
                allgos = set()
                total_score = 0.0
                for p_id, score in sim_prots.items():
                    allgos |= annotations[prot_index[p_id]]
                    total_score += score
                allgos = list(sorted(allgos))
                sim = np.zeros(len(allgos), dtype=np.float32)
                for j, go_id in enumerate(allgos):
                    s = 0.0
                    for p_id, score in sim_prots.items():
                        if go_id in annotations[prot_index[p_id]]:
                            s += score
                    sim[j] = s / total_score
                for go_id, score in zip(allgos, sim):
                    annots[go_id] = score

                prop_annots = annots.copy()
                for go_id, score in annots.items():
                    for sup_go in go_rels.get_ancestors(go_id):
                        if sup_go in prop_annots:
                            prop_annots[sup_go] = max(prop_annots[sup_go], score)
                        else:
                            prop_annots[sup_go] = score
            diam_preds.append(prop_annots)

        # new_preds = []
        # for dp, fp in zip(diam_preds, fold_preds):
        #     for go_id, score in fp.items():
        #         if go_id in dp:
        #             dp[go_id] = max(dp[go_id], score)
        #         else:
        #             dp[go_id] = score
        #     new_preds.append(dp)

        df['diam_preds'] = diam_preds
        # df['fold_preds'] = fold_preds
        # df['diam_fold_preds'] = new_preds
        filename = f'{data_root}/{ont}/{dset}_data_diam.pkl'
        df.to_pickle(filename)

if __name__ == '__main__':
    main()