# coding:utf-8
from LineApi import *
from LineApi.hooks import HooksTracer
from collections    import OrderedDict
import random, requests, json, random, sqlite3, sys

with open("Tokens.json","r",encoding="utf8") as f:
    tokens = json.loads(f.read(),object_pairs_hook=OrderedDict)

with open("Help.txt","r",encoding="utf8") as f:
    helpText = f.read()

yukino_1 = LINE(tokens["Main"],type="IOSIPAD")
yukino_2 = LINE(tokens["Sub1"],type="IOSIPAD")
yukino_3 = LINE(tokens["Sub2"],type="IOSIPAD")
yukino_4 = LINE(tokens["Sub3"],type="IOSIPAD")
yukino_5 = LINE(tokens["Sub4"],type="IOSIPAD")

db                 = sqlite3.connect("protect.db", check_same_thread=False)
tracer             = HooksTracer(yukino_1,prefix=["／","/","!","?",".","#"],db=db)
tracer.bots        = [yukino_1, yukino_2, yukino_3, yukino_4, yukino_5]
tracer.kickers     = [yukino_2, yukino_3, yukino_4, yukino_5]
tracer.botMids     = [b.getProfile().mid for b in tracer.bots]
tracer.kickerMids  = [k.getProfile().mid for k in tracer.kickers]
tracer.helptext    = helpText

class OpPs(object):
    @tracer.Operation(26)
    def OPERATION_MESSAGE(self,cl,op):
        msg = op.message
        self.trace(msg,"Content")
    @tracer.Content(0)
    def CONTENT_MESSAGE(self,cl,msg):
        self.log("[MESSAGE] "+msg.text)
        self.trace(msg,"Command")

    @tracer.Operation(19)
    def NOTIFIED_KICKOUT_FROM_GROUP(self,cl,op):
        self.log("NOTIFIED_KICKOUT_FROM_GROUP")
        gid    = op.param1
        kicker = op.param2
        gotban = op.param3
        try:
            if self.getGroup(gid,"normalProtect"):
                if  kicker not in self.kickerMids\
                and kicker not in self.getPermissionByName("White")\
                and kicker not in self.getPermissionByName("Admin"):
                    self.kk_kick(gid,gotban,kicker)
                    self.kk_invite(gid,gotban,kicker)
                elif gotban in self.botMids:
                    self.kk_invite(gid,gotban,kicker)
        except Exception as error:
            print(error)

    @tracer.Operation(13)
    def NOTIFIED_INVITE_INTO_GROUP(self,cl,op):
        self.log("NOTIFIED_INVITE_INTO_GROUP")
        gid        = op.param1
        inviter    = op.param2
        got_inv    = op.param3
        try:
            if cl.mid in got_inv:
                if inviter in self.getPermissionByName("White")\
                or inviter in self.getPermissionByName("Admin"):
                    cl.acceptGroupInvitation(gid)
                    for k in self.kickers:
                        try:
                            cl.inviteIntoGroup(gid, k.mid)
                            k.acceptGroupInvitation(gid)
                        except:
                            pass
                    cl.sendMessage(op.param1,"こんにちは! 私はグループ保護Botです。\nコマンド一覧は '/ヘルプ' で確認できます。")
            elif self.getGroup(gid,"normalProtect")\
            and (inviter not in self.getPermissionByName("White")\
                 or inviter in self.getPermissionByName("Admin")):
                gotinvs = got_inv.split("")
                for b in gotinvs:
                    cl.cancelGroupInvitation(gid, b)
        except Exception as error:
            print(error)

    @tracer.Operation(11)
    def NOTIFIED_UPDATE_GROUP(self,cl,op):
        self.log("NOTIFIED_UPDATE_GROUP")
        gid = op.param1
        usr = op.param2
        ctp = op.param3
        try:
            #グループ名変更
            if ctp == "1":
                gr = cl.getGroup(gid)
                if self.getGroup(gid,"protectGroupName"):
                    if  usr not in self.botMids\
                    and usr not in self.getPermissionByName("White")\
                    and usr not in self.getPermissionByName("Admin"):
                        gr.name = self.getGroup(gid,"groupName")
                        cl.updateGroup(gr)
                self.postGroup(gid,"groupName",gr.name)
            #グループ画変更
            elif ctp == "2":
                if self.getGroup(gid,"protectGroupImage"):
                    if  usr not in self.botMids\
                    and usr not in self.getPermissionByName("White")\
                    and usr not in self.getPermissionByName("Admin"):
                        cl.updateGroupPicture(gid,"./%s.jpg"%(gid))
            #グループURL変更
            elif ctp == "4":
                if self.getGroup(gid,"protectGroupUrl"):
                    if  usr not in self.botMids\
                    and usr not in self.getPermissionByName("White")\
                    and usr not in self.getPermissionByName("Admin"):
                        gr = cl.getGroup(gid)
                        if gr.preventedJoinByTicket:
                            gr.preventedJoinByTicket = False
                        else:
                            gr.preventedJoinByTicket = True
                        cl.updateGroup(gr)
                        self.kk_kick(gid,None,usr)
            else:
                print(ctp)
        except Exception as error:
            print(error)

class CmdPs(object):
    @tracer.Command()
    def mid(self,cl,msg):
        cl.replyMessage(msg,msg._from)

    @tracer.Command(sources=["Group"])
    def gid(self,cl,msg):
        cl.replyMessage(msg,msg.to)

    @tracer.Command(sources=["Group"],permissions=["Admin","White"])
    def さよなら(self,cl,msg):
        cl.replyMessage(msg,"さよなら :/")
        for b in self.bots:
            b.leaveGroup(msg.to)

    @tracer.Command(ignoreCase=True,alt=["help","?","へるぷ"])
    def ヘルプ(self,cl,msg):
        cl.replyMessage(msg, self.helptext)

    @tracer.Command()
    def 権限(self,cl,msg):
        cl.replyMessage(msg, "あなたは以下の権限を持っています\n"+("\n".join(self.getPermissionById(msg._from))))

    @tracer.Command(sources=["Group"])
    def グル情報(self,cl,msg):
        group = cl.getGroup(msg.to)
        md =  "[グループ名]:\n"+group.name+"\n\n"
        md += "[gid]:\n"+group.id+"\n\n"
        md += "[アイコン画像]:\n"+"http://dl.profile.line-cdn.net/"
        md += group.pictureStatus+"\n"
        if group.preventedJoinByTicket:
            md += "招待URLから参加: 拒否\n"
        else:
            md += "招待URLから参加: 許可\n"
        md += "メンバー数: " + str(len(group.members)) + "人\n"
        if group.invitee is None:
            md += "招待中: 0人\n"
        else:
            md += "招待中: " + str(len(group.invitee)) + "人\n"
        if self.getGroup(msg.to,"normalProtect"):
            md += "保護: オン\n"
        else:
            md += "保護: オフ\n"
        if self.getGroup(msg.to,"protectGroupUrl"):
            md += "保護URL: オン\n"
        else:
            md += "保護URL: オフ\n"
        if self.getGroup(msg.to,"protectGroupName"):
            md += "保護グル名: オン\n"
        else:
            md += "保護グル名: オフ\n"
        if self.getGroup(msg.to,"protectGroupImage"):
            md += "保護 グループ画像: オン\n"
        else:
            md += "保護 グループ画像: オフ\n"
        cl.replyMessage(msg, md)

    @tracer.Command(sources=["Group"],inpart=True,permissions=["Admin","White"])
    def ホワリス追加(self,cl,msg):
        mts = self.getMention(msg.contentMetadata)
        added  = ""
        for m in mts:
            if "White" not in cl.getPermissionById(m):
                self.addPermission(m,"White")
                added  += cl.getContact(m).displayName + "\n"
        if added != "":
            cl.replyMessage(msg, "%s\nこれらのユーザーをホワイトリストに追加しました"%(added))
        else:
            cl.replyMessage(msg, "メンションされたユーザーは既にホワイトリストに入っています")

    @tracer.Command(sources=["Group"],inpart=True,permissions=["Admin"])
    def ホワリス削除(self,cl,msg):
        mts = self.getMention(msg.contentMetadata)
        removed  = ""
        for m in mts:
            if "White" in cl.getPermissionById(m):
                self.removePermission(m,"White")
                removed  += cl.getContact(m).displayName + "\n"
        if removed != "":
            cl.replyMessage(msg, "%s\nこれらのユーザーをホワイトリストから削除しました"%(removed))
        else:
            cl.replyMessage(msg, "メンションされたユーザーは既にホワイトリストに入っていません")

    @tracer.Command(sources=["Group"],permissions=["Admin","White"])
    def 保護オン(self,cl,msg):
        if not self.getGroup(msg.to,"normalProtect"):
            self.postGroup(msg.to,'normalProtect',True)
            cl.replyMessage(msg, "保護機能を有効にしました。")
        else:
            cl.replyMessage(msg, "保護機能はすでに有効になっています。")

    @tracer.Command(sources=["Group"],permissions=["Admin","White"])
    def 保護オフ(self,cl,msg):
        if self.getGroup(msg.to,"normalProtect"):
            self.postGroup(msg.to,'normalProtect',False)
            cl.replyMessage(msg, "保護機能を解除しました。")
        else:
            cl.replyMessage(msg, "保護機能はすでに無効になっています。")

    @tracer.Command(alt=["保護グル名オン"],sources=["Group"],permissions=["Admin","White"])
    def 保護名前オン(self,cl,msg):
        if not self.getGroup(msg.to,"protectGroupName"):
            self.postGroup(msg.to,'protectGroupName',True)
            self.postGroup(msg.to,'groupName',cl.getGroup(msg.to).name)
            cl.replyMessage(msg, "グループ名の保護機能を有効にしました。")
        else:
            cl.replyMessage(msg, "グループ名の保護機能はすでに有効になっています。")

    @tracer.Command(alt=["保護グル名オフ"],sources=["Group"],permissions=["Admin","White"])
    def 保護名前オフ(self,cl,msg):
        if self.getGroup(msg.to,"protectGroupName"):
            self.postGroup(msg.to,'protectGroupName',False)
            cl.replyMessage(msg, "グループ名の保護機能を解除しました。")
        else:
            cl.replyMessage(msg, "グループ名の保護機能はすでに無効になっています。")

    @tracer.Command(alt=["保護グル画オン"],sources=["Group"],permissions=["Admin","White"])
    def 保護画像オン(self,cl,msg):
        if not self.getGroup(msg.to,"protectGroupImage"):
            gp = cl.getGroup(msg.to).pictureStatus
            resp = requests.get("http://dl.profile.line-cdn.net/"+gp)
            with open(msg.to+".jpg","wb") as f:
                f.write(resp.content)
            self.postGroup(msg.to,"protectGroupImage",True)
            cl.replyMessage(msg, "グループ画像の保護機能を有効にしました。")
        else:
            cl.replyMessage(msg, "グループ画像の保護機能はすでに有効になっています。")

    @tracer.Command(alt=["保護グル画オフ"],sources=["Group"],permissions=["Admin","White"])
    def 保護画像オフ(self,cl,msg):
        if self.getGroup(msg.to,"protectGroupImage"):
            self.postGroup(msg.to,"protectGroupImage",False)
            cl.replyMessage(msg, "グループ画像の保護機能を解除しました。")
        else:
            cl.replyMessage(msg, "グループ画像の保護機能はすでに無効になっています。")

    @tracer.Command(alt=["保護うあるオン"],ignoreCase=True,sources=["Group"],permissions=["Admin","White"])
    def 保護URLオン(self,cl,msg):
        if not self.getGroup(msg.to,"protectGroupUrl"):
            self.postGroup(msg.to,'protectGroupUrl',True)
            cl.replyMessage(msg, "保護機能を有効にしました。")
        else:
            cl.replyMessage(msg, "保護機能はすでに有効になっています。")

    @tracer.Command(alt=["保護うあるオフ"],ignoreCase=True,sources=["Group"],permissions=["Admin","White"])
    def 保護URLオフ(self,cl,msg):
        if self.getGroup(msg.to,"protectGroupUrl"):
            self.postGroup(msg.to,'protectGroupUrl',False)
            cl.replyMessage(msg, "グループurlの保護機能を無効にしました。")
        else:
            cl.replyMessage(msg, "グループurlの保護機能はすでに無効になっています。")

    @tracer.Command(alt=["招待うある許可"],ignoreCase=True,sources=["Group"],permissions=["Admin","White"])
    def 招待URL許可(self,cl,msg):
        gr = cl.getGroup(msg.to)
        if gr.preventedJoinByTicket:
            gr.preventedJoinByTicket = False
            cl.updateGroup(gr)
            cl.replyMessage(msg, "招待URLからの参加を許可にしました。")
        else:
            cl.replyMessage(msg, "既に許可されています。")

    @tracer.Command(alt=["招待うある拒否"],ignoreCase=True,sources=["Group"],permissions=["Admin","White"])
    def 招待URL拒否(self,cl,msg):
        gr = cl.getGroup(msg.to)
        if not gr.preventedJoinByTicket:
            gr.preventedJoinByTicket = True
            cl.updateGroup(gr)
            cl.replyMessage(msg, "招待URLからの参加を拒否にしました。")
        else:
            cl.replyMessage(msg, "既に拒否されています。")

class AdminCmdPs(object):
    @tracer.Command(permissions=["Admin"],alt=["exec"],inpart=True)
    def execute_message(self,cl,msg):
        '''Execute Message as Python Script'''
        with open("temp.txt","w") as t:
            sys.stdout = t
            try:
                exec(msg.text.replace(self.getPrefix(msg.text)+"exec",""))
            except:
                print(traceback.format_exc())
        sys.stdout = sys.__stdout__
        with open("temp.txt","r") as r:
            cl.replyMessage(msg,r.read())

tracer.addClass(FuncPs())
tracer.addClass(OpPs())
tracer.addClass(CmdPs())
tracer.addClass(AdminCmdPs())
tracer.run()

販売保護キッカー５.py

販売保護キッカー５.py


import os
import pytest
from moto import mock_s3

import boto3
from graphql_relay import to_global_id
from django.conf import settings
from django.contrib.auth import get_user_model
from django.http.response import HttpResponse
from django.core.files import File
from django_s3_storage.storage import S3Storage

from creator.studies.models import Membership
from creator.data_reviews.factories import DataReviewFactory
from creator.ingest_runs.factories import ValidationResultsetFactory

User = get_user_model()

@pytest.mark.parametrize(
    "user_group,allowed",
    [
        ("Administrators", True),
        ("Services", False),
        ("Developers", True),
        ("Investigators", True),
        ("Bioinformatics", True),
        (None, False),
    ],
)
def test_local_download_and_delete(
    clients,
    db,
    mocker,
    data_review,
    user_group,
    allowed,
):
    """
    Test download of validation report and results files
    Test download after local file is deleted
    """
    mock_resp = mocker.patch("creator.ingest_runs.views.HttpResponse")
    mock_resp.return_value = HttpResponse(open("tests/data/data.csv"))

    if user_group:
        user = User.objects.filter(groups__name=user_group).first()
        Membership(collaborator=user, study=data_review.study).save()

    # Upload file and save validation resultset
    file_field = data_review.validation_resultset.report_file
    file_field.save("data.csv", File(open("tests/data/data.csv")))

    # Download file
    client = clients.get(user_group)
    download_url = data_review.validation_resultset.report_path
    resp = client.get(download_url)

    if allowed:
        assert resp.status_code == 200
        assert resp.get("Content-Disposition") == (
            f"attachment; filename*=UTF-8''"
            f"{file_field.name.split('/')[-1]}"
        )
        assert resp.content == b"aaa,bbb,ccc\nddd,eee,fff\n"
        # Delete file from storage system
        file_field.storage.delete(file_field.name)

        # Try to download
        resp = client.get(download_url)
        assert resp.status_code == 404
        assert b"file does not exist" in resp.content
    else:
        resp.status_code == 401

@mock_s3
def test_s3_download_and_delete(
    clients, db, mocker, tmp_uploads_s3, settings, data_review
):
    """
    Test s3 download of validation report and results files
    Test download after s3 file is deleted
    """
    mock_resp = mocker.patch("creator.ingest_runs.views.HttpResponse")
    mock_resp.return_value = HttpResponse(open("tests/data/data.csv"))

    # Upload file
    settings.DEFAULT_FILE_STORAGE = "django_s3_storage.storage.S3Storage"
    file_field = data_review.validation_resultset.report_file
    tmp_uploads_s3(bucket_name=data_review.study.bucket)
    file_field.storage = S3Storage(aws_s3_bucket_name=data_review.study.bucket)
    file_field.save("data.csv", File(open("tests/data/data.csv")))
    assert file_field.storage.exists(file_field.name)

    # Download file
    client = clients.get("Administrators")
    download_url = data_review.validation_resultset.report_path
    resp = client.get(download_url)

    resp.status_code == 200
    assert resp.get("Content-Disposition") == (
        f"attachment; filename*=UTF-8''" f"{file_field.name.split('/')[-1]}"
    )
    assert resp.content == b"aaa,bbb,ccc\nddd,eee,fff\n"
    # Delete file from storage system
    file_field.storage.delete(file_field.name)

    # Try to download
    resp = client.get(download_url)
    assert resp.status_code == 404
    assert b"file does not exist" in resp.content

def test_no_validation_result(clients, db, data_review):
    """
    Test download failures

    Data review not found
    Data review validation results don't exist
    """
    client = clients.get("Administrators")

    # Data review not found
    resp = client.get("/download/data_review/foo/validation/report")
    assert resp.status_code == 404
    assert b"No data review exists" in resp.content

    # Data review validation result set doesn't exist yet
    dr = DataReviewFactory()
    download_urls = [
        f"/download/data_review/{dr.kf_id}/validation/{ft}"
        for ft in ["report", "results"]
    ]
    for url in download_urls:
        resp = client.get(url)
        assert resp.status_code == 404
        assert b"Validation results not yet available" in resp.content

def test_file_not_found(clients, db, mocker, tmpdir, data_review):
    """
    Test validation report/results files have not been uploaded
    """
    client = clients.get("Administrators")

    for ft in ["report", "results"]:
        # Validation result set has no file uploaded yet
        download_url = (
            f"/download/data_review/{data_review.kf_id}/validation/{ft}"
        )
        resp = client.get(download_url)
        assert resp.status_code == 404
        assert b"file does not exist" in resp.content


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import codecs
from collections import OrderedDict
import fnmatch
import json
import os
import sys

# error counter
counterr = 0

# Need the json path
if len(sys.argv) < 2:
    dir = "json"
else:
    dir = sys.argv[1]

# collect all the JSON files
json_files = [
    os.path.join(dirpath, f)
    for dirpath, dirnames, files in os.walk(dir)
    for f in fnmatch.filter(files, '*.txt')
]

for files in json_files:
    update = False
    f = os.path.splitext(os.path.basename(files))[0]
    with codecs.open(files, mode='r', encoding='utf-8') as json_file:
        print("Opening {}".format(files))
        djson = json.load(json_file, object_pairs_hook=OrderedDict)
        for entry in djson:
            for data in entry:
                if data.startswith('tr_'):
                    continue
                elif data.startswith('jp_'):
                    continue
                elif data == 'title_id':
                    continue
                elif data.endswith('_id'):
                    entry[data] = 0
                    update = True
                elif entry[data] != "":
                    entry[data] = None
                    update = True

    if (update):
        print("Updating {}".format(files))
        with codecs.open(files, mode='w+', encoding='utf-8') as json_file:
            json.dump(
                djson, json_file, ensure_ascii=False,
                indent="\t", sort_keys=False)
            json_file.write("\n")

if counterr > 0:
    sys.exit("Issues found")


T = int(input())
temp = []

for i in range(0, T):
    S = input()
    temp.append(S)

for i in range(len(temp)):
    even = []
    odd = []

    for j in range(len(temp[i])):
        if j % 2 == 0:
            even.append(temp[i][j])
        else:
            odd.append(temp[i][j])

    print("".join(even) + " " + "".join(odd))


import csv
from pprint import pprint

def costo_camion(nombre_archivo):
    total = 0.0
    camion = []
    with open(nombre_archivo, 'rt') as f:
        rows = csv.reader(f)
        headers = next(rows)
        for row in rows:
            cajon = dict(zip(headers,row))
            camion.append(cajon)
        for x in camion:
            total += int(x['cajones'])*float(x['precio'])
    return camion,total

def leer_precios():
    camion2={}
    archivo=open('Data/precios.csv','rt')
    rows = csv.reader(archivo)
    for row in rows:
        try:
            camion2[row[0]]=float(row[1])
        except IndexError:
            print('')
    archivo.close()
    return camion2

x='Data/fecha_camion.csv'
var,ptotal=costo_camion(x)
frut=leer_precios()
ganancia=0.0
costototalcamion = 0.0
venta = 0.0

for x in var:
    precio=0.0
    cajones=int(x['cajones'])
    nomb=x['nombre']
    precio=float(x['precio'])
    costototalcamion += cajones*precio
    precio = frut[nomb]
    venta += cajones*precio

ganancia = venta-costototalcamion

print('La ganancia es de:', round(ganancia,2))


from models.DataLoader import DataLoader
from models.DataSet import DataSet
from models.models import DeepFMatNet, DeepFMatAlex, DeepFMatVGG16, DeepFMatResNet18
from models.Regularizer import L2Regularizer, L1Regularizer
import torch.optim as optim
import torch.nn as nn
from torch.optim.lr_scheduler import StepLR
import numpy as np
import torch
import os
import cv2
import pickle
import time
from torch.utils.tensorboard import SummaryWriter
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
import argparse
import datetime

# torch.autograd.set_detect_anomaly(True)

def epipoline(x, formula):
    '''

    :param x:
    :param formula:
    :return:
    '''
    array = formula.flatten()
    a = array[0]
    b = array[1]
    c = array[2]
    return int((-c - a * x) / b)

def verify_xfx(line, point):
    '''

    :param line:
    :param point:
    :return:
    '''
    l = np.array(line).flatten()
    a = l[0]
    b = l[1]
    return abs(line.dot(point))/np.sqrt(a*a+b*b)

def visualize2Images(args, left_paths, right_paths, f_mats, loss, epoch, img_idx, visualizeDir = "visualization"):
    '''

    :param args:
    :param left_paths:
    :param right_paths:
    :param f_mats:
    :param loss:
    :param epoch:
    :param img_idx:
    :param visualizeDir:
    :return:
    '''
    colors = [
        (255, 102, 102),
        (102, 255, 255),
        (125, 125, 125),
        (204, 229, 255),
        (0, 0, 204)
    ]
    f_mats = f_mats.cpu().numpy()
    THRESHOLD = 0.12
    sift = cv2.xfeatures2d.SIFT_create()
    bf = cv2.BFMatcher()
    images = []
    errors = {}
    for idx, (left_path, right_path, f_mat) in enumerate(zip(left_paths, right_paths, f_mats)):
        f_mat = np.array(f_mat.reshape((3,3)))

        left_img = cv2.imread(left_path)
        #-------
        hl, wl = left_img.shape[0], left_img.shape[1]
        left_img = left_img[int(hl / 2) - 128: int(hl / 2) + 128, int(wl / 2) - 128: int(wl / 2) + 128]
#--------------------------

        left_imgG = cv2.cvtColor(left_img.copy(), cv2.COLOR_BGR2GRAY)
        left_img_line = left_img.copy()

        right_img = cv2.imread(right_path)
#-------------------------------
        hr, wr = right_img.shape[0], right_img.shape[1]
        right_img = right_img[int(hr / 2) - 128: int(hr / 2) + 128, int(wr / 2) - 128: int(wr / 2) + 128]

        right_imgG = cv2.cvtColor(right_img.copy(), cv2.COLOR_BGR2GRAY)
        right_img_line = right_img.copy()

        (kps_left, descs_left) = sift.detectAndCompute(left_imgG, None)
        (kps_right, descs_right) = sift.detectAndCompute(right_imgG, None)

        matches = bf.knnMatch(descs_left, descs_right, k=2)
        good = []
        for m, n in matches:
            if m.distance < THRESHOLD * n.distance:
                good.append([m])

        err_l = []
        err_r = []
        img_W = left_img.shape[1] - 1
#---------------------------------------------------------------------
        for color_idx, g in enumerate(good):
            # get the ids of matching feature points
            id_l, id_r = g[0].queryIdx, g[0].trainIdx
            # x: column
            # y: row
            # get the feature points in both left and right images
            x_l, y_l = kps_left[id_l].pt
            x_r, y_r = kps_right[id_r].pt

            '''Color for line'''
            color = colors[color_idx % len(colors)]

            '''Epi line on the left image'''
            # epi line of right points on the left image
            point_r = np.array([x_r, y_r, 1])
            line_l = np.dot(f_mat.T, point_r)

            # calculating 2 points on the line
            y_0 = epipoline(0, line_l)
            y_1 = epipoline(img_W, line_l)
            # drawing the line and feature points on the left image
            left_img_line = cv2.circle(left_img_line, (int(x_l), int(y_l)), radius=4, color=color)
            left_img_line = cv2.line(left_img_line, (0, y_0), (img_W, y_1), color=color, lineType=cv2.LINE_AA)
            # displaying just feature points
            left_img = cv2.circle(left_img, (int(x_l), int(y_l)), radius=4, color=color)

            '''Epi line on the right image'''
            # epi line of left points on the right image
            point_l = np.array([x_l, y_l, 1])
            line_r = np.dot(f_mat, point_l)

            # verifying points
            err_R = verify_xfx(line_r, point_r)
            err_r.append(err_R)
            # verifying points
            err_L = verify_xfx(line_l, point_l)
            err_l.append(err_L)
            # calculating 2 points on the line
            y_0 = epipoline(0, line_r)
            y_1 = epipoline(img_W, line_r)

            # drawing the line on the right image
            right_img_line = cv2.circle(right_img_line, (int(x_r), int(y_r)), radius=4, color=color)
            right_img_line = cv2.line(right_img_line, (0, y_0), (img_W, y_1), color=color, lineType=cv2.LINE_AA)
            # displaying just feature points
            right_img = cv2.circle(right_img, (int(x_r), int(y_r)), radius=4, color=color)

        l_avgErr = np.average(err_l) if err_l else 0
        r_avgErr = np.average(err_r) if err_r else 0

        vis = np.concatenate((left_img_line, right_img_line), axis=0)
        font = cv2.FONT_HERSHEY_SIMPLEX

        img_H = vis.shape[0]
        cv2.putText(vis, '{:.4f}'.format(float(l_avgErr)), (10, 20), font, 0.3, color=(0, 255, 0), lineType=cv2.LINE_AA)
        cv2.putText(vis, '{:.4f}'.format(float(r_avgErr)), (10, img_H - 10), font, 0.3, color=(0, 255, 0), lineType=cv2.LINE_AA)
        cv2.putText(vis, '{:.4f}'.format(float(loss.data.cpu())), (int(img_W-img_W/2), img_H - 10), font, 0.3, color=(0, 255, 0), lineType=cv2.LINE_AA)

        sqResultDir = os.path.join(ROOT_DIR, visualizeDir, '{}'.format(epoch))
        if not os.path.exists(sqResultDir):
            os.makedirs(sqResultDir)

        cv2.imwrite(os.path.join(sqResultDir, 'epipoLine_sift_batch{}_img{}.png'.format(img_idx, idx)), vis)
        print("Writing image ... " + 'epipoLine_sift_batch{}_img{}.png'.format(img_idx, idx))
        images.append(vis)
        errors['batch{}_img{}_left'.format(img_idx, idx)] = l_avgErr
        errors['batch{}_img{}_right'.format(img_idx, idx)] = r_avgErr
    return np.array(images), errors

def training(args, model, device, trainLoader, optimizer, criterion, epoch, writer, allParamsRegularized= False):
    '''
    Training the model.
    :param args: input arguments
    :param model: training model
    :param device: device
    :param trainLoader: training loader
    :param optimizer: optimizer
    :param criterion: criterion
    :param epoch: current epoch
    :param writer: tensorboard writer
    :param allParamsRegularized: regularize all params?
    :return:
    '''
    # enter train mode
    model.train()
    # saving losses
    totalLoss = []
    print(50 * "*")
    print("Training Epoch ... ", epoch)
    print(50 * "*")

    # number of batches in the training dataset
    l = len(trainLoader)
    # length of the whole training dataset.
    L = len(trainLoader.dataset)
    # Regularizer
    reg_loss = L2Regularizer(model=model, lambda_reg=0.01)
    for batch_idx, (data, target, (_,_)) in enumerate(trainLoader):
        data, target = data.to(device, dtype=torch.float), target.to(device, dtype=torch.float)
        optimizer.zero_grad()
        output = model(data)
        loss = criterion(output, target)
        if allParamsRegularized:
            loss = reg_loss.regularized_all_param(reg_loss_function=loss)
        totalLoss.append(loss.item())

        loss.backward()

        optimizer.step()
        # writing the loss of the current batch to tensorboard
        writer.add_scalar('Batch Loss',
                          loss.data.cpu(),
                          epoch * l + batch_idx)
        if batch_idx % args.log_interval == args.log_interval-1:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, (batch_idx+1) * len(data), L ,
                       100. * (batch_idx+1) / l, loss.data.cpu()))

    # writing the loss of the current epoch to tensorboard
    writer.add_scalars('Train Epoch Loss', {'Training': np.mean(np.array(totalLoss))}, epoch)
    writer.flush()

def validating(args, model, device, valLoader, epoch, writer, log, trainloader=None):
    '''

    :param args:
    :param model:
    :param device:
    :param valLoader:
    :param epoch:
    :param writer:
    :param log:
    :param trainloader:
    :return:
    '''
    visualDir = "visualization/model_{}".format(args.exp)
    model.eval()
    totalLoss = []
    trainTotalLoss = []
    criterion = nn.MSELoss()

    print(50 * "*")
    print("Validating Epoch ... ", epoch)
    print(50 * "*")
    L = len(valLoader.dataset)
    with torch.no_grad():
        for id, (data, target, (left_img, right_img)) in enumerate(valLoader):
            data, target = data.to(device, dtype=torch.float), target.to(device, dtype=torch.float)
            output = model(data)
            loss = criterion(output, target)
            totalLoss.append(loss.item())

            if id == 0:
                print("Visualize ... Batch {}".format(id))
                imageBatch, errors = visualize2Images(args, left_img, right_img, output, loss, epoch, id, visualizeDir=visualDir)
                log["epoch_{}_batch_{}".format(epoch, id)] = errors

                nameErr = 'batch{}_img{}_right'.format(id, 0)
                print(nameErr)
                writer.add_scalars("Testing Image Errors", {"exp_{}_{}".format(args.exp, nameErr): errors[nameErr]}, epoch)
                writer.add_images("VisualResult_Exp_{}".format(args.exp),  imageBatch, global_step=epoch, dataformats='NHWC')

        if trainloader:
            for id, (data, target, (_, _)) in enumerate(trainloader):
                data, target = data.to(device, dtype=torch.float), target.to(device, dtype=torch.float)
                output = model(data)
                loss = criterion(output, target)
                trainTotalLoss.append(loss.item())

    valMean = np.mean(np.array(totalLoss))
    writer.add_scalars('Epoch Loss',{'Validate': valMean} ,epoch)
    if trainloader:
        trainMean = np.mean(np.array(trainTotalLoss))
        writer.add_scalars('Epoch Loss', {'Train': trainMean}, epoch)
        print('Trainning Set: Average Error: {:.6f}'.format(trainMean))
    writer.flush()
    print('Validation set: Average Error: {:.6f}. Length of set : {}.'.format(valMean, L))

def main():
    '''
    Running the models here
    :return:
    '''
    parser = argparse.ArgumentParser(description='DeepF_noCorrs')

    parser.add_argument('--deviceID', type=int, default=0, metavar='N',
                        help='The GPU ID (default: 0)')
    parser.add_argument('--batch-size', type=int, default=8, metavar='N',
                        help='batch size for training set (default: 8)')
    parser.add_argument('--test-batch-size', type=int, default=8, metavar='N',
                        help='batch size for testing set (default: 8)')
    parser.add_argument('--epochs', type=int, default=200, metavar='N',
                        help='number of epochs (default: 200)')
    parser.add_argument('--lr', type=float, default=0.0000001, metavar='LR',
                        help='learning rate (default: 0.0000001)')
    parser.add_argument('--exp', type=int, default=0, metavar='experiment ID',
                        help='naming the experiment ID (default: 0)')

    parser.add_argument('--log-interval', type=int, default=100, metavar='N',
                        help='how many iterations to wait before printing the loss status')

    parser.add_argument('--k-fold', type=int, default=3, metavar='1-9',
                        help='how many folds in the test set (default: 3)')

    parser.add_argument('--kth', type=int, default=0, metavar='0-10',
                        help='The kth fold (default: 0)')

    parser.add_argument("--model", type=str, default='deepfmat', metavar="deepfmat, resnet, vgg16, alex",
                        help='Selecting the training models (default: deepfmat)')

    parser.add_argument("--norm", type=str, default='ETR', metavar='ETR, ABS, FBN',
                        help="Selecting the normalization method (default: ETR)")

    args = parser.parse_args()

    # -------------Dataset Path-----------------------------------
    POSES_PATH = "/media/slark/Data/Projects/dataset/data_kitti/dataset/poses"
    SEQUENCE_PATH = "/media/slark/Data/Projects/dataset/data_kitti/dataset/sequences"
    log_dir = os.path.join("logs/batchLosses/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
    resultDir = os.path.join(ROOT_DIR, "analysis/experiment_{}_lr{}_batch{}".format(args.exp, args.lr, args.batch_size))
    # ------------------------------------------------------------

    # -------------Loading dataset--------------------------------
    db = DataSet(SEQUENCE_PATH, POSES_PATH)
    train, val, test = db.dataSets(k_fold=args.k_fold, kth=args.kth)
    poses = db.poses
    train_loader = DataLoader(dataSet=train
                              , poses=poses
                              , dType="train"
                              , camera=0)
    val_loader = DataLoader(dataSet=val
                            , poses=poses
                            , dType="validate"
                            , camera=0)
    # ------------------------------------------------------------
    use_cuda = torch.cuda.is_available()
    kwargs = {'num_workers': 4, 'pin_memory': True} if use_cuda else {}
    device = torch.device(args.deviceID if use_cuda else "cpu")
    trainLoader = torch.utils.data.DataLoader(train_loader
                                              , batch_size=args.batch_size
                                              , shuffle=True
                                              , **kwargs)
    valLoader = torch.utils.data.DataLoader(val_loader
                                            , batch_size=args.test_batch_size
                                            , shuffle=False
                                            , **kwargs)

    resultModelFile = os.path.join(resultDir, log_dir, "result_{}".format(args.exp))

    print(50*"*")
    print("Running experiment {}".format(args.exp))
    print(50 * "*")

    outputSize = 9
    writer = SummaryWriter(os.path.join(resultDir, log_dir))
    if args.model == "deepfmat":
        model = DeepFMatNet(outputSize=outputSize, norm=args.norm).to(device)
    elif args.model == "alex":
        model = DeepFMatAlex(outputSize=outputSize, norm=args.norm).to(device)
    elif args.model == "vgg16":
        model = DeepFMatVGG16(outputSize=outputSize, norm=args.norm).to(device)
    elif args.model == "resnet":
        model = DeepFMatResNet18(outputSize=outputSize, norm=args.norm).to(device)

    if os.path.isfile(resultModelFile):
        try:
            model.load_state_dict(torch.load(resultModelFile))
        except:
            print("Cannot load the saved model")

    optimizer = optim.Adam(model.parameters(), lr=args.lr)
    criterion = nn.MSELoss()
    scheduler = StepLR(optimizer, step_size=5, gamma=0.1)
    testErrorLog = os.path.join(resultDir, "log.txt")
    log = {}
    if os.path.isfile(testErrorLog):
        with open(testErrorLog, "rb") as f:
            log = pickle.load(f)
    for epoch in range(1, args.epochs+1):
        startTime = time.time()
        training(args, model, device, trainLoader, optimizer, criterion, epoch, writer)

        validating(args, model, device, valLoader, epoch, writer, log, trainloader=trainLoader)
        scheduler.step()
        torch.save(model.state_dict(), resultModelFile)
        torch.save(model.state_dict(), resultModelFile + "_epoch_{}".format(epoch))
        with open(testErrorLog, 'wb') as f:
            pickle.dump(log, f)
        endTime = time.time()
        writer.add_scalar('Time Epoch', endTime, epoch)
        print('--------{}--------\n'.format(endTime - startTime))
    writer.close()

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import sys
import xml.etree.ElementTree as ElementTree
from pathlib import Path

FREE_MIND_NODE_TAG = 'node'
FREE_MIND_NODE_TEXT_ATTRIBUTE_TAG = 'TEXT'

time_re = re.compile(r'.+(\s+\(.*\))')
time_number_re = re.compile(r'.*\((\d+[.,]?\d?).*\)')

class NodeInfo:
    def __init__(self, text=""):
        self.text = text
        self.childs: [NodeInfo] = []
        self.time = None
        self.child_time = None

    def append_child(self, child):
        self.childs.append(child)

    def has_childs(self):
        return len(self.childs) > 0

    def update_time(self):

        if not self.has_childs():
            self.child_time = self.time
            return

        for child in self.childs:
            child.update_time()

            child_time = child.time if child.child_time is None else child.child_time

            if child_time is None:
                continue

            self.child_time = child_time if self.child_time is None else self.child_time + child_time

    def get_display_time(self):
        if self.time is None:
            return "" if self.child_time is None else str(self.child_time)

        if self.child_time is None:
            return "" if self.time is None else str(self.time)

        if self.time == self.child_time:
            return str(self.time)

        return f"{self.time} ?? {self.child_time}"

    def get_time(self):
        if self.time is None:
            return 0 if self.child_time is None else self.child_time

        if self.child_time is None:
            return 0 if self.time is None else self.time

        if self.time == self.child_time:
            return self.time

        return max(self.time, self.child_time)

def append_nodes(xml_parent_node, parent_node_info: NodeInfo):
    for child in xml_parent_node:
        if child.tag != FREE_MIND_NODE_TAG:
            continue

        if FREE_MIND_NODE_TEXT_ATTRIBUTE_TAG in child.attrib:
            node = NodeInfo(child.attrib[FREE_MIND_NODE_TEXT_ATTRIBUTE_TAG])

            time_match = time_re.match(node.text)

            if time_match is not None:
                time = time_match[1]
                node.text = node.text.replace(time, "")
                time_number_match = time_number_re.match(time)

                if time_number_match is not None:
                    node.time = float(time_number_match[1].replace(",", "."))

            parent_node_info.append_child(node)
            append_nodes(child, node)

def get_nodes_information(path: Path):
    tree = ElementTree.parse(path)
    root = tree.getroot()
    root_node_info = NodeInfo()

    for child in root:
        if child.tag != FREE_MIND_NODE_TAG:
            continue

        append_nodes(child, root_node_info)

    return root_node_info

def write_to_file_csv(outfile, node_info: NodeInfo, intend: str or None, number: str or None):
    if intend is None:
        node_intend = ""
        intend = ""
    else:
        node_intend = intend
        intend += "  "

    node_time = node_info.get_display_time().replace('.', ',')
    node_number = "" if not number else f"{number}."
    node_text = "Итого;" if not node_info.text else f"{node_info.text};"
    line = f"{node_intend}{node_number};{node_text}{node_time}"

    if line:
        outfile.write(line)
        outfile.write("\n")

    child_number = 1
    for child in node_info.childs:
        write_to_file_csv(outfile, child, intend, f"{node_number}{child_number}")
        child_number += 1

def write_to_file_skype_format(outfile, node_info: NodeInfo, intend: str or None, number: str or None, write_start: bool):
    if write_start:
        outfile.write("```\n")

    if intend is None:
        node_intend = ""
        intend = ""
    else:
        node_intend = intend
        intend += "  "

    node_time = node_info.get_display_time().replace(',', '.')
    node_time = node_time if not node_time else f" ({node_time} ч)"
    node_number = "" if not number else f"{number}."
    node_text = "Итого:" if not node_info.text else f" {node_info.text}"
    line = f"{node_intend}{node_number}{node_text}{node_time}"

    if line:
        outfile.write(line)
        outfile.write("\n")

    child_number = 1
    for child in node_info.childs:
        write_to_file_skype_format(outfile, child, intend, f"{node_number}{child_number}", False)
        child_number += 1

    if write_start:
        outfile.write("```\n")

def write_to_file_in_redmine_format_table(outfile, node_info: NodeInfo, number: str or None, write_start: bool):
    if write_start:
        outfile.write("|_. № п/п |_. Задача |_. Оценка, в часах) |\n")

    node_number = "" if not number else f"{number}."
    node_time = node_info.get_display_time().replace(',', '.')
    node_time = node_time if not node_time else f"{node_time}"

    if node_info.text:
        line = f"|{node_number}|{node_info.text}|{node_time}|"
    else:
        line = f"|*Итого:*| |_ *{node_time}*|"

    if line:
        outfile.write(line)
        outfile.write("\n")

    child_number = 1
    for child in node_info.childs:
        write_to_file_in_redmine_format_table(outfile, child, f"{node_number}{child_number}", False)
        child_number += 1

def write_to_file_in_redmine_format(outfile, node_info: NodeInfo, intend: str or None):
    if intend is None:
        node_intend = ""
        intend = "#"
    else:
        node_intend = intend
        intend += "#"

    node_time = node_info.get_display_time().replace(',', '.')
    node_time = node_time if not node_time else f"{node_time}"

    if node_info.text:
        line = f"{node_intend} {node_info.text}"

        if node_time:
            line += f" ({node_time})"
    else:
        line = f"*Итого: {node_time}*"

    if line:
        outfile.write(line)
        outfile.write("\n")

    child_number = 1
    for child in node_info.childs:
        write_to_file_in_redmine_format(outfile, child, intend)
        child_number += 1

def write_delimiter(outfile):
    outfile.write("\n-----\n")

def process_free_mind_document(path):
    in_file_path = Path(path)
    root_node_info = get_nodes_information(in_file_path)
    root_node_info.update_time()

    out_file_path = Path(in_file_path.parent).joinpath(in_file_path.name).with_suffix('.txt')

    if out_file_path.exists():
        out_file_path.unlink()

    with out_file_path.open('wt', encoding="utf-8") as outfile:
        outfile.write(f"{out_file_path.with_suffix('').name}\n")
        write_delimiter(outfile)
        write_to_file_csv(outfile, root_node_info, None, None)
        write_delimiter(outfile)
        write_to_file_in_redmine_format(outfile, root_node_info, None)
        write_delimiter(outfile)
        write_to_file_in_redmine_format_table(outfile, root_node_info, None, True)
        write_delimiter(outfile)
        write_to_file_skype_format(outfile, root_node_info, None, None, True)

if __name__ == "__main__":
    if len(sys.argv) < 2 or (sys.argv[1] is None):
        print('Usage: python3 convert-mm-to-excel.py path')
        sys.exit(-1)

    argv_iterator = iter(sys.argv)
    next(argv_iterator)

    for arg in argv_iterator:
        process_free_mind_document(arg)


"""Serializers for ecommerce"""
from rest_framework import serializers

from applications.models import BootcampApplication
from ecommerce.models import Order, Line, Receipt
from klasses.serializers import BootcampRunSerializer, InstallmentSerializer

class PaymentSerializer(serializers.Serializer):
    """
    Serializer for payment API, used to do basic validation.
    """

    payment_amount = serializers.DecimalField(
        max_digits=20, decimal_places=2, min_value=0.01
    )
    application_id = serializers.IntegerField()

class OrderPartialSerializer(serializers.ModelSerializer):
    """
    Serializer for Order
    """

    class Meta:
        model = Order
        fields = ("id", "status", "created_on", "updated_on")

class ReceiptSerializer(serializers.ModelSerializer):
    """Serializer for receipts for a user"""

    class Meta:
        model = Receipt
        fields = ["id", "payment_method"]

class ApplicationOrderSerializer(serializers.ModelSerializer):
    """Serializer for orders that are part of a bootcamp application"""

    total_price_paid = serializers.DecimalField(decimal_places=2, max_digits=20)
    payment_method = serializers.SerializerMethodField()

    def get_payment_method(self, order):
        """Get the payment method used in the last receipt for the order"""
        if order.payment_type == Order.CYBERSOURCE_TYPE:
            # There should only be one receipt for an order most of the time, but it's possible
            # there is a duplicate or a Cybersource error in one of the receipts.
            receipt = order.receipt_set.order_by("id").last()
            return receipt.payment_method if receipt is not None else None
        elif order.payment_type == Order.WIRE_TRANSFER_TYPE:
            return "Wire Transfer"

    class Meta:
        model = Order
        fields = [
            "id",
            "status",
            "total_price_paid",
            "created_on",
            "updated_on",
            "payment_method",
        ]

class OrderSerializer(serializers.ModelSerializer):
    """Serializer for orders"""

    class Meta:
        model = Order
        fields = ["id", "status", "application_id", "created_on", "updated_on"]

class LineSerializer(serializers.ModelSerializer):
    """
    Serializer for Line
    """

    order = OrderPartialSerializer(read_only=True)
    price = serializers.DecimalField(decimal_places=2, max_digits=20)
    run_key = serializers.SerializerMethodField()

    def get_run_key(self, line):
        """get run_key from bootcamp_run"""
        return line.bootcamp_run.run_key

    class Meta:
        model = Line
        fields = ("order", "price", "description", "run_key")

class CheckoutDataSerializer(serializers.ModelSerializer):
    """Serializer for ecommerce information for a BootcampApplication"""

    bootcamp_run = BootcampRunSerializer()
    total_price = serializers.DecimalField(
        max_digits=20, decimal_places=2, read_only=True, source="price"
    )
    total_paid = serializers.DecimalField(
        max_digits=20, decimal_places=2, read_only=True
    )
    payments = serializers.SerializerMethodField()
    installments = serializers.SerializerMethodField()

    def get_payments(self, application):
        """Serialized payments made by the user"""
        return LineSerializer(
            (
                order.line_set.first()
                for order in application.orders.all()
                if order.status == Order.FULFILLED
            ),
            many=True,
        ).data

    def get_installments(self, application):
        """Installments with prices and due dates"""
        return InstallmentSerializer(
            application.bootcamp_run.installment_set.order_by("deadline"), many=True
        ).data

    class Meta:
        model = BootcampApplication
        fields = [
            "id",
            "bootcamp_run",
            "total_price",
            "total_paid",
            "payments",
            "installments",
        ]


import experiments

import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import os

# lookups
default_split = {
    'cub200': 2,
    'cars196': 0,
    'sop': 0,
}

dataset_label_lookup = {
    'cub200': 'CUB200-2011',
    'cars196': 'CARS196',
    'sop': 'Online Products (SOP)'
}

# get wandb run data
name_list, group_list, summary_list, config_list, data_list = experiments.get_wandb_data()

project_group = 'ooDML_msloss'
labelspace = 'arch'
dataset = 'sop' # 'cub200'
filter_substrings = ['100epoch', 'vitS8Dino_bs30_c'] # 'vitS8Dino_bs30_c', 'gpu2_sop'

# filter runs data - project group
ids_valid = [i for i, group in enumerate(group_list) if group == project_group]
name_list = [name_list[i] for i in ids_valid]
data_list = [data_list[i] for i in ids_valid]

# filter runs data - dataset
ids_valid = [i for i, d in enumerate(data_list) if d['dataset'] == dataset]
name_list = [name_list[i] for i in ids_valid]
data_list = [data_list[i] for i in ids_valid]

# filter runs data - substrings in name
ids_valid = [i for i, n in enumerate(name_list) if not any(map(n.__contains__, filter_substrings))]
name_list = [name_list[i] for i in ids_valid]
data_list = [data_list[i] for i in ids_valid]

# get labels
labels_list = [d[labelspace] for d in data_list]
labels_unique = sorted(list(set(labels_list)))

# gather ooDML progressions
recall_seqs = {label: [] for label in labels_unique}
split_seqs = {label: [] for label in labels_unique}

for data in data_list:
    label_tmp = data[labelspace]
    recall_seqs[label_tmp].append(data['r@1_max'])
    split_seqs[label_tmp].append(data['ooDML_split_id'])

# sort progressions by split id
n_x_ticks = []
for label in labels_unique:
    recs = recall_seqs[label]
    split_ids = split_seqs[label]

    # sort recall values
    recs = [x for _, x in sorted(zip(split_ids, recs))]

    # reassign
    recall_seqs[label] = recs
    split_seqs[label] = sorted(split_ids)

    n_x_ticks.append(len(recs))
n_x_ticks = min(n_x_ticks)

# cut data to n_x_ticks - to still be able to visualize experiments in progress
for label in labels_unique:
    recall_seqs[label] = recall_seqs[label][:n_x_ticks]
    split_seqs[label] = split_seqs[label][:n_x_ticks]

# plotting
x_label = 'split_id'
x_ticks = list(range(1, n_x_ticks + 1)) # e.g. split ids
colors = ['green', 'red', 'blue', 'magenta', 'yellow', 'cyan', 'navy', 'orange', 'deeppink', 'springgreen', 'pink', 'darkkhaki']
alphas = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

# fig, ax = plt.subplots(figsize=(5, 4))
# for i, label in enumerate(labels_unique):
#     linestyle = 'dashed' if any(map(label.__contains__, ['resnet', 'bninception'])) else 'solid'
#     ax.plot(x_ticks, recall_seqs[label], label=f'{label}', marker='x', color=colors[i], linestyle=linestyle, alpha=alphas[i])
#
# leg = ax.legend(loc="best", shadow=True, fancybox=True, fontsize='xx-small')
# ax.set(xlabel=f'{x_label}', ylabel=f'{dataset} recall@1',
#        title=f'Eval ooDML [{labelspace}] [384]')
# ax.set_xticks(x_ticks)
# plt.axvline(x=x_ticks[default_split[dataset]]) # add default split indicator (vertical line)
# ax.yaxis.set_major_locator(ticker.MultipleLocator(0.02))
# ax.grid()

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 4))
# absolute performance plot
for i, label in enumerate(labels_unique):
    linestyle = 'dashed' if any(map(label.__contains__, ['resnet', 'bninception'])) else 'solid'
    ax1.plot(x_ticks, recall_seqs[label], label=f'{label}', marker='x', color=colors[i], linestyle=linestyle, alpha=alphas[i])

leg = ax1.legend(loc="best", shadow=True, fancybox=True, fontsize='xx-small')
ax1.set(xlabel=f'{x_label}', ylabel=f'{dataset_label_lookup[dataset]} recall@1',
       title=f'Absolute recall performance [{labelspace}] [384]')
ax1.set_xticks(x_ticks)
plt.axvline(x=x_ticks[default_split[dataset]]) # add default split indicator (vertical line)
ax1.yaxis.set_major_locator(ticker.MultipleLocator(0.02))
ax1.grid()

# relative performance plot (reference value: performance for split 1)
for i, label in enumerate(labels_unique):
    linestyle = 'dashed' if any(map(label.__contains__, ['resnet', 'bninception'])) else 'solid'
    ax2.plot(x_ticks, [recall_seqs[label][0] - v for v in recall_seqs[label]], label=f'{label}', marker='x', color=colors[i], linestyle=linestyle, alpha=alphas[i])

leg = ax2.legend(loc="best", shadow=True, fancybox=True, fontsize='xx-small')
ax2.set(xlabel=f'{x_label}', ylabel=f'{dataset_label_lookup[dataset]} recall@1 difference',
       title=f'Relative recall performance w.r.t. split 1 [{labelspace}] [384]')
ax2.set_xticks(x_ticks)
plt.axvline(x=x_ticks[default_split[dataset]]) # add default split indicator (vertical line)
ax2.yaxis.set_major_locator(ticker.MultipleLocator(0.02))
ax2.grid()

# save plot
customstring = f'msloss_dino60_abs_rel'
path_save = f'/export/home/tmilbich/PycharmProjects/dml_pl/experiments/plots/ooDML/eval_{labelspace}_{dataset}'
path_save = "_".join([path_save, customstring])
fig.savefig(path_save + '.png', dpi=600) # .svg'

plt.show()


# https://www.acmicpc.net/problem/1157
a = input().upper()
abc = list(range(65, 91))
cnt = [0] * 26
mx = []

for i in range(len(a)):
    cnt[abc.index(ord(a[i]))] += 1

mxidx = max(cnt)
for i in range(26):
    if(mxidx <= cnt[i]):
        mx.append(cnt[i])
        mxidx = cnt[i]

if(len(mx) != 1):
    print("?")
else:
    print(chr(abc[cnt.index(mx[0])]))

단계별로 풀어보기/07. 문자열/단어 공부.py


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

from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('fieldsight', '0002_auto_20161108_0034'),
        ('logger', '0005_remove_xform_site'),
    ]

    operations = [
        migrations.CreateModel(
            name='FieldSightXF',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('is_staged', models.BooleanField(default=False)),
                ('is_scheduled', models.BooleanField(default=False)),
                ('date_created', models.DateTimeField(auto_now=True)),
                ('date_modified', models.DateTimeField(auto_now=True)),
            ],
            options={
                'ordering': ('date_modified',),
                'db_table': 'fieldsight_forms_data',
                'verbose_name': 'XForm',
                'verbose_name_plural': 'XForms',
            },
        ),
        migrations.CreateModel(
            name='Schedule',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=256, verbose_name=b'Schedule Name')),
                ('date_range_start', models.DateField(auto_now=True)),
                ('date_range_end', models.DateField(auto_now=True)),
            ],
            options={
                'ordering': ('date_range_start',),
                'db_table': 'fieldsight_forms_schedule',
                'verbose_name': 'Form Schedule',
                'verbose_name_plural': 'Form Schedules',
            },
        ),
        migrations.CreateModel(
            name='Stage',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=256)),
                ('order', models.IntegerField(default=0)),
                ('stage', models.ForeignKey(related_name='parent', blank=True, to='fsforms.Stage', null=True)),
            ],
            options={
                'ordering': ('order',),
                'db_table': 'fieldsight_forms_stage',
                'verbose_name': 'FieldSight Form Stage',
                'verbose_name_plural': 'FieldSight Form Stages',
            },
        ),
        migrations.AddField(
            model_name='fieldsightxf',
            name='schedule',
            field=models.ForeignKey(blank=True, to='fsforms.Schedule', null=True),
        ),
        migrations.AddField(
            model_name='fieldsightxf',
            name='site',
            field=models.ManyToManyField(related_name='site_forms', to='fieldsight.Site'),
        ),
        migrations.AddField(
            model_name='fieldsightxf',
            name='stage',
            field=models.ForeignKey(blank=True, to='fsforms.Stage', null=True),
        ),
        migrations.AddField(
            model_name='fieldsightxf',
            name='xf',
            field=models.ForeignKey(to='logger.XForm'),
        ),
    ]


import torch
from torch import nn
from models.sh import eval_sh

class Embedding(nn.Module):
    def __init__(self, in_channels, N_freqs, logscale=True):
        """
        Defines a function that embeds x to (x, sin(2^k x), cos(2^k x), ...)
        in_channels: number of input channels (3 for both xyz and direction)
        """
        super(Embedding, self).__init__()
        self.N_freqs = N_freqs
        self.in_channels = in_channels
        self.funcs = [torch.sin, torch.cos]
        self.out_channels = in_channels*(len(self.funcs)*N_freqs+1)

        if logscale:
            self.freq_bands = 2**torch.linspace(0, N_freqs-1, N_freqs)
        else:
            self.freq_bands = torch.linspace(1, 2**(N_freqs-1), N_freqs)

    def forward(self, x):
        """
        Embeds x to (x, sin(2^k x), cos(2^k x), ...)
        Different from the paper, "x" is also in the output
        See https://github.com/bmild/nerf/issues/12

        Inputs:
            x: (B, self.in_channels)

        Outputs:
            out: (B, self.out_channels)
        """
        out = [x]
        for freq in self.freq_bands:
            for func in self.funcs:
                out += [func(freq*x)]

        return torch.cat(out, -1)

class NeRF(nn.Module):
    def __init__(self,
                 D=8, W=256,
                 in_channels_xyz=63, in_channels_dir=27,
                 skips=[4], deg=2):
        """
        D: number of layers for density (sigma) encoder
        W: number of hidden units in each layer
        in_channels_xyz: number of input channels for xyz (3+3*10*2=63 by default)
        in_channels_dir: number of input channels for direction (3+3*4*2=27 by default)
        skips: add skip connection in the Dth layer
        """
        super(NeRF, self).__init__()
        self.D = D
        self.W = W
        self.in_channels_xyz = in_channels_xyz
        self.in_channels_dir = in_channels_dir
        self.skips = skips
        self.deg = deg

        # xyz encoding layers
        for i in range(D):
            if i == 0:
                layer = nn.Linear(in_channels_xyz, W)
            elif i in skips:
                layer = nn.Linear(W+in_channels_xyz, W)
            else:
                layer = nn.Linear(W, W)
            layer = nn.Sequential(layer, nn.ReLU(True))
            setattr(self, f"xyz_encoding_{i+1}", layer)
        # self.xyz_encoding_final = nn.Linear(W, W)

        # # direction encoding layers
        # self.dir_encoding = nn.Sequential(
        #                         nn.Linear(W+in_channels_dir, W),
        #                         nn.ReLU(True))

        # output layers
        self.sigma = nn.Sequential(nn.Linear(W, W),
                                   nn.ReLU(True),
                                   nn.Linear(W, 1))
        # self.sh = nn.Linear(W, 3 * (self.deg + 1)**2)
        self.sh = nn.Sequential(nn.Linear(W, W),
                                   nn.ReLU(True),
                                   nn.Linear(W, 3 * (self.deg + 1)**2))

    def forward(self, x, dirs=None, sigma_sh_only=False):
        """
        Encodes input (xyz+dir) to sh+sigma (not ready to render yet).
        For rendering this ray, please see rendering.py

        Inputs:
            x: (B, self.in_channels_xyz(+self.in_channels_dir))
               the embedded vector of position and direction
            sigma_only: whether to infer sigma only. If True,
                        x is of shape (B, self.in_channels_xyz)

        Outputs:
            if sigma_ony:
                sigma: (B, 1) sigma
            else:
                out: (B, 4), sh and sigma
        """
        input_xyz = x

        xyz_ = input_xyz
        for i in range(self.D):
            if i in self.skips:
                xyz_ = torch.cat([input_xyz, xyz_], -1)
            xyz_ = getattr(self, f"xyz_encoding_{i+1}")(xyz_)

        sigma = self.sigma(xyz_)
        sh = self.sh(xyz_)

        if sigma_sh_only:
            out = torch.cat([sigma, sh], -1)
            return out

        rgb = eval_sh(deg=self.deg, sh=sh.reshape(-1, 3, (self.deg + 1)**2), dirs=dirs) # sh: [..., C, (deg + 1) ** 2]
        rgb = torch.sigmoid(rgb)

        # if extract_time:
        out = torch.cat([sigma, rgb, sh], -1)
        return out


import os
import time
from subprocess import Popen

asic = os.environ.get('ASIC', 'capri')
HNTAP_CMD = '../nic/build/x86_64/iris/' + asic + '/bin/nic_infra_hntap'
hntap_log = os.environ['WS_TOP']  + "/nic/" + "/hntap.log"

class Hntap(object):

    def __init__(self, conf_file):
        self._conf_file = conf_file
        self._process = None

    def Run(self):
        log = open(hntap_log, "w")
        cmd = [HNTAP_CMD, '-f', self._conf_file, '-n', '2']
        p = Popen(cmd, stdout=log, stderr=log)
        self._process = p
        print (self._conf_file)
        print("* Starting Host-Network tapper, pid (%s)" % str(p.pid))
        print("    - Log file: " + hntap_log + "\n")

        log2 = open(hntap_log, "r")
        loop = 1
        time.sleep(2)

        # Wait until tap devices setup is complete
        while loop == 1:
            for line in log2.readlines():
                if "listening on" in line:
                    loop = 0
        log2.close()
        return

    def Stop(self):
        os.kill(int(self._process.pid), 9)


from urllib.request import urlopen
from bs4 import BeautifulSoup
import random

html = urlopen("https://dhlottery.co.kr/gameResult.do?method=statByNumber")
bsObject = BeautifulSoup(html, "html.parser")

theory_rate = 7/45 # 이론상 숫자가 나올 확률
current_round = int(bsObject.body.find("select",{"id":"edDrwNo"}).find("option",{"selected":""}).text) # 총 시행횟수 (최신 회차)
theory_pop_count = int(current_round * theory_rate) # 이론상 숫자가 나와야 하는 횟수

data = bsObject.body.find_all("tbody")[-1].text.split()
Pop_count = {} # 실제 숫자가 나온 횟수를 담을 딕셔너리
num = 1

for i in range(2,len(data),3):
    Pop_count[ num ] = int(data[i])
    num += 1

sorted_Pop = sorted(Pop_count.items(),key=(lambda x:x[1]), reverse=True)

most_high = sorted_Pop[:6]
most_low = sorted_Pop[-6:]
middle = sorted_Pop[ (int(len(sorted_Pop)/2)) - 3 : (int(len(sorted_Pop)/2)) + 3 ]

print("most high :", most_high)
print("most low :", most_low)

random_seq = most_high + most_low
random.shuffle(random_seq)

random_seq = random_seq[:6]
random_seq.sort()

print("shuffled :", random_seq)


import time

start_time = time.time()

# faster than normal mergesort

def merge_sort(arr):
    if len(arr) <= 1:
        return arr

    # Splitting the array into two halves
    mid = len(arr) // 2
    left_half = arr[:mid]
    right_half = arr[mid:]

    # Recursively sort each half
    left_half_sorted = merge_sort(left_half)
    right_half_sorted = merge_sort(right_half)

    # Merge the sorted halves
    sorted_arr = []
    i = j = 0
    while i < len(left_half_sorted) and j < len(right_half_sorted):
        if left_half_sorted[i] < right_half_sorted[j]:
            sorted_arr.append(left_half_sorted[i])
            i += 1
        else:
            sorted_arr.append(right_half_sorted[j])
            j += 1

    sorted_arr.extend(left_half_sorted[i:])
    sorted_arr.extend(right_half_sorted[j:])

    return sorted_arr

# arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]
arr = [9, 12, 11, 5, 14, 18, 6, 10, 4, 7, 2, 13, 20, 1, 8]
sorted_arr = merge_sort(arr)
print(sorted_arr)  # Output: [1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

end_time = time.time()
duration = end_time - start_time

print("Duration: ", duration)


import pika
import sys

connection = pika.BlockingConnection(
        pika.ConnectionParameters('localhost'))
channel = connection.channel()

# durable: 声明队列为持久化队列
channel.queue_declare(queue='task_queue', durable=True)

message = ' '.join(sys.argv[1:]) or "Hello World"
channel.basic_publish(
        exchange='',
        routing_key='task_queue',
        body=message,
        # delivery_mode=2: 将消息设置为持久化消息
        properties=pika.BasicProperties(
            delivery_mode=2,)  # make message persistent
        )
print("[x] Sent 'Hello World!'")
connection.close()


import os
import logging
import discord
from discord.ext import commands
from dotenv import load_dotenv
import utils

load_dotenv()

logger = logging.getLogger('discord')
logger.setLevel(logging.INFO)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger.addHandler(handler)

client = discord.Client()

@client.event
async def on_ready():
    logger.info(f"logged in as {client.user}")

@client.event
async def on_message(ctx):
    content = ctx.content
    if ctx.author == client.user:
        logger.info("message sent by bot itself, ignoring...")
        return

    if content.startswith('!o'):
        fields = content.split(' ')
        try:
            dice_pool = int(fields[1])
            modifier = int(fields[2])
            dt = int(fields[3])
        except IndexError as error:
            logger.warning(f"invalid fields for {ctx.author} message threw the exception bellow...\n {error}")
            await ctx.channel.send("Campos inválidos!")
            return

        dice_info = utils.dice_roll(dice_pool,modifier,dt)

        embed_image = discord.Embed(
            title = f'{ctx.author} rolou...',
            color=dice_info[3],
        )
        embed_image.add_field(name="**Dados**", value=dice_info[0], inline=True)
        embed_image.add_field(name="**Modificador**", value=modifier, inline=True)
        embed_image.add_field(name="**Dificuldade**", value=dt, inline=True)
        embed_image.add_field(name="**Rolagem final:**", value=dice_info[5]+modifier)
        embed_image.add_field(name="**Resultado:**", value=dice_info[4])
        embed_image.set_thumbnail(url=dice_info[2])

        logger.info(f"rolling dice for {ctx.author}...")
        await ctx.channel.send(embed=embed_image)
        return

    elif content.startswith('ordo!ping'):
        embed_image = discord.Embed(
            title = 'Ping...',
            color=0x492ea4,
        )
        logger.info(f'running ping command by {ctx.author}...')
        embed_image.add_field(name="**Pong!**", value=f'{round(client.latency * 1000)}ms')
        await ctx.channel.send(embed=embed_image)
        return

    elif content.startswith('!ro'):
        fields=content.split(' ')
        dice_info = utils.other_dice(fields[1])
        embed_image = discord.Embed(
            color=0x492ea4
        )
        embed_image.add_field(name="**Dados**", value=dice_info[0], inline=True)
        embed_image.add_field(name="**Total**", value=dice_info[3], inline=True)
        await ctx.channel.send(embed=embed_image)
        return

if __name__ == "__main__":
    client.run(os.environ["BOT_TOKEN"])


from django.db.models import fields
from rest_framework import serializers
from .models import Order, OrderRecipe, Recipe
from recipe.serializers import RecipeSerializer

class OrderRecipeSerializer(serializers.ModelSerializer):

    class Meta:
        model = OrderRecipe
        fields = (
            'id',
            'recipe_id',
            'order_id',
            'quantity',

        )

class OrderSerializer(serializers.ModelSerializer):
# recipe_list = RecipeSerializer(source='recipe', read_only=True, many=True)
    created = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')
    class Meta:
        model = Order
        fields = (
            'id',
            'name',
            'table',
            'price',
            'status',
            'slug',
            'deleted',
            'created',
        )


import sys
import time
import random

import os
import shutil

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

from_dir = "C:/Users/judit/Downloads"

class FileEventHandler(FileSystemEventHandler):

    def on_created(self, event):
        print(f"¡Oye, {event.src_path} ha sido creado!")

    def on_deleted(self, event):
        print(f"¡Lo siento! ¡Alguien borró {event.src_path}!")

    def on_modified(self, event):
        print(f"¡Ve!, {event.src_path} se modificó")

    def on_moved(self, event):
        print(f"Se movió {event.src_path} a {event.dest_path}")

event_handler = FileEventHandler()

observer = Observer()

observer.schedule(event_handler, from_dir, recursive=True)

observer.start()

try:
    while True:
        time.sleep(2)
        print("ejecutando...")
except KeyboardInterrupt:
    print("¡detenido!")
    observer.stop()


def isKingsTour(board):
  N = len(board)
  x = [x for x in board if 1 in x][0]
  rpos = board.index(x)
  cpos = x.index(1)

  for i in range(1, N**2):
    foundNext = False
    for r in range(rpos-1, rpos+2):
      for c in range(cpos-1, cpos+2):
        #print(r, c, board[r][c])
        if correctPosition(r, c, N) and not (r == rpos and c == cpos) and board[r][c] == i+1:
          rpos = r
          cpos = c
          foundNext = True
          break

      if foundNext:
        break
    if not foundNext:
      return False
  return True

"""
def findAdjacent(board, row, col, N):
  arr = []
  for r in range(row-1, row+2):
    for c in range(col-1, col+2):
      if correctPosition(r, c, N) and not r == row and not c == col:
        arr.append(board[r][c])
  return arr
"""
def correctPosition(r, c, N):
  if r< N and r >= 0 and c < N and c >= 0:
    return True
  return False

testKings = [ [3, 2, 1], [6, 4, 9], [5, 7, 8] ]
testKings2 = [ [1, 2, 3], [7, 4, 8], [6, 5, 9] ]
testKings3 = [ [3, 2, 1], [6, 4, 9], [5, 7, 8] ]
testKings4 = [ [1, 14, 15, 16], [13, 2, 7, 6], [12, 8, 3, 5], [11, 10, 9, 4] ]

print(isKingsTour(testKings))
print(isKingsTour(testKings2))
print(isKingsTour(testKings3))
print(isKingsTour(testKings4))


import re
from ipykernel.ipkernel import IPythonKernel
from .shell import OverrideShell

class NowKernel(IPythonKernel):

    def __init__(self, **kwargs):
        super(NowKernel, self).__init__(**kwargs)
        OverrideShell(self.shell)

    def do_execute(self, code, silent, store_history=True,
                   user_expressions=None, allow_stdin=False):
        reply_content = super(NowKernel, self).do_execute(
            code, silent, store_history=store_history,
            user_expressions=user_expressions, allow_stdin=allow_stdin)
        return reply_content


import logging
import random

from chess.models.conf import DATA_TOURNAMENTS
from tinydb import Query

from chess.models.round import Round

class Tournament:
    """ """

    MAX_PLAYERS = 4
    table = DATA_TOURNAMENTS

    def __init__(
            self,

            name="",
            place="",
            start_date="",
            end_date="",
            rounds_number=4,
            current_round=-1,
            rounds_list=[],
            players_list=[],
            description="",
            status="created",  # créé, en cours, terminé,

    ) -> None:
        """ """
        self.name = name
        self.place = place
        self.start_date = start_date
        self.end_date = end_date
        self.rounds_number = rounds_number
        self.current_round = current_round
        self.rounds_list = rounds_list
        self.players_list = players_list
        self.description = description
        self.status = status

    def create(self):
        """ """
        self.table.insert(self.__dict__)

    def end_tournament(self):
        """ """
        Obj = Query()
        self.table.update({"status": "closed"}, Obj.name == self.name)

    def remove_one(self):
        """ """
        Obj = Query()
        self.table.remove(Obj.name == self.name)

    @classmethod
    def remove_all(cls):
        """ """
        cls.table.truncate()

    def update(self, tournament_value, value_to_change):
        """ """
        Obj = Query()
        self.table.update({tournament_value: value_to_change},
                          Obj.name == self.name)

    @classmethod
    def find_one(cls, data, value):
        """ Chercher un tournois """
        Obj = Query()
        t_list = cls.table.search(Obj[data] == value)

        if len(t_list) != 1:
            raise AttributeError("More than 1 tournament")
        return t_list[0]

    @classmethod
    def find_all(cls):
        """ """
        list_doc = cls.table.all()
        list_dict = [dict(doc) for doc in list_doc]

        return list_dict

    def add_player(self, ine_player: str):
        """ """
        if (self.status == "created") and (self.n_players < self.MAX_PLAYERS):

            if ine_player not in self.players_list:
                self.players_list.append(ine_player)
                self.table.update({'players_list': self.players_list},
                                  Query().name == self.name)
                logging.warning("Joueur inscrit")

            else:
                logging.warning(" Attention : joueur déjà inscrit")

        elif self.n_players >= self.MAX_PLAYERS:
            logging.warning("Maximum de joueurs atteint")

        else:
            logging.warning("Attention : tournois non créé")

    def add_players(self, players_list: list):
        """ """
        for player in players_list:
            self.add_player(player)

    def start_tournament(self):
        """ """
        if (self.status == "created") and (self.n_players == self.MAX_PLAYERS):
            self.status = "running"
            self.table.update({"status": self.status},
                              Query().name == self.name)

        elif self.status != "created":
            logging.error(f"Erreur de status {self.status}")
        else:
            logging.error("problème nombre de players")

    def create_first_round(self):
        """ """
        if self.current_round == -1 and self.status == "running":
            self.current_round += 1
            self.table.update({"current_round": self.current_round},
                              Query().name == self.name)

            shuffled_list = self.players_list
            random.shuffle(shuffled_list)
            match_1 = (f"{shuffled_list[0]}", 0), (f"{shuffled_list[1]}", 0)
            match_2 = (f"{shuffled_list[2]}", 0), (f"{shuffled_list[3]}", 0)
            matchs_list = [match_1, match_2]
            r = Round(matchs_list)
            r.create()
            self.rounds_list.append(r.round_id)
            self.table.update({"rounds_list": self.rounds_list},
                              Query().name == self.name)
            return r.round_id

    @classmethod
    def get_instance(cls, document):
        name = document["name"]
        place = document["place"]
        start_date = document["start_date"]
        end_date = document["end_date"]
        rounds_number = document["rounds_number"]
        current_round = document["current_round"]
        rounds_list = document["rounds_list"]
        players_list = document["players_list"]
        description = document["description"]
        status = document["status"]
        return Tournament(name, place, start_date, end_date, rounds_number,
                          current_round, rounds_list, players_list,
                          description, status)

    @property
    def scores(self):
        """ """
        if not self.n_players:
            return {}

            # no score before 1st round

        scores = {id_player: 0 for id_player in self.players_list}
        if self.current_round < 1:
            return scores

        # else
        for round_id in self.rounds_list:
            ronde = Round.find_one("round_id", round_id)
            matchs_list = ronde.matchs_list
            for match in matchs_list:
                scores[match[0][0]] += match[0][1]
                scores[match[1][0]] += match[1][1]

        return scores

    @property
    def classement(self):
        """ """
        classement = [(i, j) for i, j in self.scores.items()]

        classement = sorted(classement, key=lambda i: i[1], reverse=True)

        classement = [i[0] for i in classement]
        self.players_list = classement
        self.table.update({"players_list": self.players_list},
                          Query().name == self.name)

        return classement

    def have_already_played(self, player_x, player_0):
        """"""
        flatten_match_list = []
        for round_id in self.rounds_list:
            rounde = Round.find_one("round_id", round_id)

            matchs_list = rounde.matchs_list

            for match in matchs_list:
                m = (match[0][0], match[1][0])
                flatten_match_list.append(m)

        cand_match = (player_0, player_x)

        for match in flatten_match_list:

            if match == cand_match:
                return True

        cand_match = (player_x, player_0)
        for match in flatten_match_list:

            if match == cand_match:
                return True

        return False

    def compute_round(self):
        """define the roundes"""

        logging.warning("_compute_ronde  called")

        # seulement si c'est la 1er la current_round 0
        if self.current_round == 0:
            logging.warning("not self._current_round:")

            # we need 3 object storage
            match_list = []
            players_choisis = []
            players_non_choisis = [i for i in self.players_list]

            while len(players_non_choisis) != 0:
                # just to be more readable
                p1 = players_non_choisis[0]
                p2 = players_non_choisis[1]

                # match and match list
                match = [(p1, 0), (p2, 0)]
                match_list.append(match)

                # update players_choisis & non choisis
                players_choisis.extend([p1, p2])

                players_non_choisis.remove(p1)
                players_non_choisis.remove(p2)

            return match_list

        # else
        match_list = []
        players_choisis = []
        self.players_list = self.classement
        players_non_choisis = [i for i in self.players_list]

        i = 0
        while len(players_non_choisis) != 0:
            p1 = players_non_choisis[i]

            if p1 in players_choisis:
                continue

            for p_id in self.players_list:
                if p1 == p_id:
                    continue

                if p_id in players_choisis:
                    continue

                already_played = self.have_already_played(p1, p_id)
                if not already_played:
                    break

                # match and match list
            match = [(p1, 0), (p_id, 0)]
            match_list.append(match)

            # update players_choisis & non choisis
            players_choisis.extend([p1, p_id])

            if p_id in players_non_choisis:
                players_non_choisis.remove(p_id)
            players_non_choisis.remove(p1)

        return match_list

    def next_round(self):
        """ """
        i = self.rounds_list[self.current_round]
        previous_round = Round.find_one("round_id",
                                        f"{i}")
        previous_round.end()

        self.current_round += 1
        self.table.update({"current_round": self.current_round},
                          Query().name == self.name)

        if int(self.current_round) >= int(self.rounds_number):
            self.status = "closed"
            self.table.update({"status": self.status},
                              Query().name == self.name)

            return logging.warning("Tournois terminé !")
        new_match_list = self.compute_round()

        new_round = Round(new_match_list)

        self.rounds_list.append(new_round.round_id)
        self.table.update({"rounds_list": self.rounds_list},
                          Query().name == self.name)
        new_round.create()

    @property
    def n_players(self):
        """ """
        return len(self.players_list)

    def __repr__(self):
        """ """
        rep = 'Tournois(' + self.name + ',' + self.place + str(
            self.players_list) + ')'
        return rep


from datascientist.model.regression.skl.linear_model.lassolarsic import _lassolarsic

import numpy as np
from pytest import raises

def test_lassolarsic():
    x_train = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
    y_train = np.dot(x_train, np.array([1, 2])) + 3

    x_test = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
    y_test = np.dot(x_test, np.array([1, 2])) + 3

    metrics = 'mae'
    answer = _lassolarsic(train=(x_train, y_train), test=(x_test, y_test), metrics=metrics)
    assert answer[0] == 'LassoLarsIC'
    assert round(answer[1], 2) == 0.85
    assert answer[2] is None

    metrics = 'mse'
    answer = _lassolarsic(train=(x_train, y_train), test=(x_test, y_test), metrics=metrics)
    assert answer[0] == 'LassoLarsIC'
    assert round(answer[1], 2) == 0.85
    assert answer[2] is None

    metrics = 'rmse'
    answer = _lassolarsic(train=(x_train, y_train), test=(x_test, y_test), metrics=metrics)
    assert answer[0] == 'LassoLarsIC'
    assert round(answer[1], 2) == 0.92
    assert answer[2] is None

    answer = _lassolarsic(train=(x_train, y_train), test=(x_test, y_test), metrics=metrics, x_predict=x_test)
    arr = np.array([7.20710678, 8.5       , 8.5       , 9.79289322])
    for i in range(len(answer[2])):
        assert round(answer[2][i], 2) == round(arr[i], 2)


import numpy as np

from ..numerical_method import NumericalMethod
from .equation_systems_parent import concatenate, exchange_rows, regressive_sustitution, is_not_invertible, process_params

class GaussEliminationSimple(NumericalMethod):
    def evaluate(self, parameters):
        A = parameters["A"]
        b = parameters["b"]

        A, b = process_params(A, b)
        response = self.init_response()

        if is_not_invertible(A):
            response["error"] = "The matrix can not be inverted"
            return response

        augmented = elimination(A, b)

        A = np.delete(augmented.copy(), len(augmented), 1)
        b = augmented[:, len(augmented)]
        x = regressive_sustitution(A, b)

        response["augmented"] = np.round(augmented, 4).tolist()
        response["x"] = np.round(x, 4).tolist()

        return response

    def init_response(self):
        response = dict()

        return response

def elimination(A, b):
    augmented = concatenate(A, b)
    n = len(augmented)

    for k in range(0, n-1):
        if augmented[k, k] == 0:
            fila = k
            for i in range(k, n):
                if augmented[i, k] != 0:
                    fila = i
                    break
            augmented = exchange_rows(augmented, fila, k)

        for i in range(k+1, n):
            mult = augmented[i, k]/augmented[k, k]
            for j in range(k, n+1):
                augmented[i, j] = augmented[i, j] - mult * augmented[k, j]

    return augmented


from turtle import *
from random import randrange

graph = dict()

def draw(g):
	penup()
	visted = []
	position = {}

	#sets position for nodes and writes it
	for i in g.keys():
		a = True
		while a:
			a = False
			x = randrange(-400, 400)
			y = randrange(-200, 200)
			for j in position.keys():
				if 10000 > (position[j][0]-x)**2 + (position[j][1]-y)**2:
					a = True
					break
		setposition(x, y)
		position[i] = [x, y]
		write(i)

	#draw edges
	for i in g.keys():
		visted.append(i)
		for j in g[i]:
			if j not in visted:
				penup()
				setposition(position[i][0], position[i][1])
				pendown()
				setposition(position[j][0], position[j][1])
	mainloop()
	exit()

def add_node(node):
	if node not in graph.keys():
		graph[node] = []
	else:
		print("node lready exists")

def add_vertex(node1, node2):
	if node1 not in graph.keys() and node2 not in graph.keys():
		print("Node 1 and 2 not exists")
	elif node1 not in graph.keys():
		print("Node 1 not exists")
	elif node2 not in graph.keys():
		print("Node 2 not exists")
	elif node1 in graph[node2]:
		print("edge already exists")
	else:
		graph[node1].append(node2)
		graph[node2].append(node1)

def print_vertex():
	visited = []
	going_to_visit = graph.keys()
	for i in going_to_visit:
		visited.append(i)
		for j in graph[i]:
			#if j not in visited: #for not printing double time
			print(i,'-->',j)

def matrix_to_graph(list1):
	g = dict()
	for i in range(len(list1)):
		g[i] = []
	for index in range(len(list1)):
		for i in range(len(list1)):
			if list1[index][i] == 1 and index != i:
				g[index].append(i)
	return g

print("press 1 to enter nodes and edges")
print("Press 2 to enter adjacency matrix")
opt = int(input())

if opt == 2:
	number_of_nodes = int(input("Enter number of nodes"))
	g = []
	print("Enter the matrix seperated by spaces")
	for i in range(number_of_nodes):
		temp = [int(x) for x in input().split()]
		g.append(list(temp))
	graph = matrix_to_graph(g)
while True:
	print("\n\npress 1 to add node")
	print("press 2 to add edges")
	print("press 3 to print")
	print("Press 4 to draw and exit")
	print("press 5 to exit")
	a = int(input())
	if a == 1:
		node = input('Enter node:')
		add_node(node)
	elif a == 2:
		node1 = input("Enter node 1:")
		node2 = input("Enter node 2:")
		add_vertex(node1, node2)
	elif a == 3:
		print_vertex()
	elif a == 4:
		draw(graph)
	else:
		break


#  https://www.codewars.com/kata/5e1ab1b9fe268c0033680e5f/train/zzpython

from math import isqrt, floor

def triang(n):
    return n * (n + 1) // 2

def reverse_triang(n):
    return floor(0.5 * (isqrt(8 * n + 1) - 1))

def count_digits(n):
    if n < 10:
        return 1

    return 1 + count_digits(n // 10)

def go_back(i, n):
    pos = count_digits(i)

    while n > 1:

        while pos > 0:
            pos -= 1
            n -= 1

            if n == 1:
                break

        pos = count_digits(i) - 1
        i -= 1

    print(i, pos)
    return str(i)[pos]

def solve(n):
    if n == 1:
        return 1

    i = reverse_triang(n)
    k = 0
    while True:
        k = triang(i)

        if k >= n:
            break

        i += 1

    return go_back(i-1, k-n)

def main():
    print(solve(100), 1)
    # print(solve(2100), 2)
    # print(solve(31000), 2)

if __name__ == "__main__":
    main()


import rsa, hashlib

class Chain:
	def __init__(self):
		self.validatedBlocks = [Block(self, 'Genisis Block')]
		self.validatedBlocks[0].hash = hashlib.sha256('Genisis Block')
		self.pendingBlocks = []
		self.transactionBlock = Block(self, hashlib.sha256('Genisis Block'))
		self.accounts = {}
		self.validators = []

class Block:
	def __init__(self, chain, prevBlockHash):
		self.transactions = []
		self.chain = chain
		self.capacity = 3
		self.previousHash = prevBlockHash
		self.hash = None
		self.data = ''
		self.votes = []

	def addTransaction(self, transaction):
		self.transactions.append(transaction)
		if len(self.transactions) >= self.capacity:
			self.data = ' | '.join([' - '.join((transaction.sender, transaction.amount, transaction.reciver)) \
				for transaction in self.transactions] + ' || ' + self.previousHash).encode('ascii')
			self.hash = hashlib.sha256(self.data)
			self.chain.pendingBlocks.append(self)

	def vote(self, vote, blockSignature, publicKey):
		if publicKey in self.chain.validators and rsa.decrypt(blockSignature, publicKey) == self.data:
			self.votes.append((vote, blockSignature, publicKey))

class Transaction:
	def __init__(self, amount, senderPubKey, senderPrivKey, reciver):
		self.amount = amount
		self.sender = senderPubKey
		self.reciver = reciver
		self.signature = self.sign(senderPrivKey)

	def sign(self, key):
		data = f'{self.sender} sending {self.amount} to {self.reciver}'
		return rsa.encrypt(data.encode('ascii'), key)

class Wallet:
	def __init__(self, chain):
		self.balance = 0
		self.publicKey, self.privateKey = rsa.newkeys(4096)
		chain.accounts[self.publicKey] = 0

	def send(self, amount, reciver):
		transaction = Transaction(amount, self.publicKey, self.privateKey, reciver)

class Validator:
	def __init__(self, chain, wallet):
		self.chain = chain
		self.wallet = wallet
		self.chain.validators.append(self.wallet.publickKey)

	def validateBlock(self):
		block = self.chain.pendingBlocks[0]
		for transaction in block.transactions:
			if rsa.decrypt(transaction.signature, transaction.sender) == \
			f'{transaction.sender} sending {transaction.amount} to {transaction.reciver}' and \
			self.chain.accounts[transaction.sender] >= transaction.amount:
				block.vote(True, rsa.encrypt(block.data.encode('ascii'), self.wallet.privateKey), self.wallet.publicKey)


# Import Section
import re
from .validation import MathValidation
from calculators.calculator import Calculator

# Code Section
class FunctionValidation:
    @staticmethod
    def is_all_params_exist(params: list, expression: str, operation: list, open_parenthesis: str,
                            close_parenthesis: str) -> bool:
        """
        The function check if params in the expression exist in the params list
        :param operation: operation possible
        :param params: the params list
        :param expression: the expression to check
        :return: True if all the params in the expression in the the params list
        """
        # Put a number instant of all the params in the list
        number = 1
        for param in params:
            expression = re.sub(f'{param}', f'{number}', expression)
        # Check if there are params left in expression that are not in the params list
        return MathValidation.is_contains_only_numbers_and_operations(expression, operation, open_parenthesis,
                                                                      close_parenthesis)

    @staticmethod
    def is_valid_parameters(params: list, operations: list, open_parenthesis: str, close_parenthesis: str) -> bool:
        """
        Thr function check if the parameters are valid not an operation or a parenthesis
        :param params: the list of the parameters
        :param operations: the valid operation
        :return: True if the parameters are valid
        """
        for param in params:
            if not FunctionValidation.is_valid_use_of_naming(param, operations, open_parenthesis, close_parenthesis):
                return False
        return True

    @staticmethod
    def is_valid_use_of_naming(name: str, operations: list, open_parenthesis: str, close_parenthesis: str) -> bool:
        """
        Thr function check if the name used isn't used in the other operation of the calculator
        :param name: the name to check
        :param operations: the valid operation
        :param open_parenthesis: open parenthesis
        :param close_parenthesis: close parenthesis
        :return: True if the naming is valid
        """
        return name not in operations and name != open_parenthesis and name != close_parenthesis

    @staticmethod
    def is_valid_expression(expression: str, params: list, calculator_to_check: Calculator) -> bool:
        """
        The function check if the expression is valid
        :param expression: thr expression to check
        :param params: the params list
        :param calculator_to_check: calculator for checking the expression according to
        :return: True if valid otherwise False
        """
        # Put a number instant of all the params in the list
        number = 1
        for param in params:
            expression = re.sub(f'{param}', f'{number}', expression)
        try:
            calculator_to_check.evaluate(expression)
        except SyntaxError:
            return False
        except ZeroDivisionError:
            return True
        return True


class Vector2:
    def __init__(self, x, y):
        self.x = x
        self.y = y

class Collision:
    def __init__(self, left, right, top, bottom):
        self.top = top
        self.bottom = bottom
        self.left = left
        self.right = right

class Direction:
    def __init__(self, left, right, up, down):
        self.left = left
        self.right = right
        self.up = up
        self.down = down


# 문자열 재정렬

S = input().rstrip()
strs = []
nums = 0
for s in S:
    if s >= '0' and s <= '9':
        nums += int(s)
    else:
        strs.append(s)

strs.sort()
if nums > 0:
    strs.append(str(nums))
print(''.join(strs))


'''
Created on May 16, 2017

@author: jesper
'''

class AuditModule():
    @staticmethod
    def read(file):
        pass
    @staticmethod
    def evaluate(info_dict, yaml_path):
        pass

class lynis(AuditModule):

    @staticmethod
    def read(file):
        warning_dict = dict()
        suggestion_dict = dict()

        next_line = file.readline()

        while next_line:

            if "Warning:" in next_line:
                start_index = next_line.find("W")
                end_index = next_line.find("[")
                warning = next_line[start_index:end_index-1].replace("Warning: ", "")

                start_index = next_line.find("[test:")
                end_index = next_line.find("]")
                test = next_line[start_index + len("[test:"):end_index]

                next_line = next_line[end_index+2:]
                start_index = next_line.find("[details:")
                end_index = next_line.find("]")
                details = next_line[start_index + len("[details:"):end_index]

                next_line = next_line[end_index+2:]
                start_index = next_line.find("[solution:")
                end_index = next_line.find("]")
                solution = next_line[start_index + len("[solution:"):end_index]

                inner_dict = dict()
                inner_dict["warning"] = warning
                inner_dict["details"] = details
                inner_dict["solution"] = solution

                warning_dict[test] = inner_dict

            elif "Suggestion:" in next_line:
                start_index = next_line.find("S")
                end_index = next_line.find("[")
                suggestion = next_line[start_index:end_index-1].replace("Suggestion: ", "")

                start_index = next_line.find("[test:")
                end_index = next_line.find("]")
                test = next_line[start_index + len("[test:"):end_index]

                next_line = next_line[end_index+2:]
                start_index = next_line.find("[details:")
                end_index = next_line.find("]")
                details = next_line[start_index + len("[details:"):end_index]

                next_line = next_line[end_index+2:]
                start_index = next_line.find("[solution:")
                end_index = next_line.find("]")
                solution = next_line[start_index + len("[solution:"):end_index]

                inner_dict = dict()
                inner_dict["suggestion"] = suggestion
                inner_dict["details"] = details
                inner_dict["solution"] = solution

                suggestion_dict[test] = inner_dict

            next_line = file.readline()

        info_dict = dict()
        info_dict["warnings"] = warning_dict
        info_dict["suggestions"] = suggestion_dict
        return info_dict

    @staticmethod
    def evaluate(info_dict, yaml_path):

        return_string = ""

        if info_dict.has_key("warnings"):
            return_string += "The Lynis audit has found the following warnings: \n\n"
            for test in info_dict["warnings"]:
                inner_dict = info_dict["warnings"][test]
                warning = inner_dict["warning"]
                details = inner_dict["details"]
                solution = inner_dict["solution"]

                return_string +=  "The test " + test + " has found the following warning:\n" + warning + "\n" + "details: " + details + "\n" + "solution: " + solution + "\n\n"

        return_string += "\n##########################\n\n\n"

        if info_dict.has_key("suggestions"):
            return_string += "The Lynis audit has found the following suggestions: \n\n"
            for test in info_dict["suggestions"]:
                inner_dict = info_dict["suggestions"][test]
                suggestion = inner_dict["suggestion"]
                details = inner_dict["details"]
                solution = inner_dict["solution"]

                return_string +=  "The test " + test + " has found the following suggestion:\n" + suggestion + "\n" + "details: " + details + "\n" + "solution: " + solution + "\n\n"

        return return_string