"""Tricks

Reference:
https://github.com/soumith/ganhacks

without label flipping for G and D
other tricks are the same with train_with_flip_D.py and train_with_flip_G_D.py

"""

import itertools
import numpy as np
from torch.autograd import Variable

import torch
import torchvision.utils as vutils
from conf import Args
import os
import matplotlib.pyplot as plt

if not os.path.isdir('MNIST_DCGAN_results'):
    os.mkdir('MNIST_DCGAN_results')
if not os.path.isdir('MNIST_DCGAN_results/Random_results'):
    os.mkdir('MNIST_DCGAN_results/Random_results')
if not os.path.isdir('MNIST_DCGAN_results/Fixed_results'):
    os.mkdir('MNIST_DCGAN_results/Fixed_results')

def train(netG, netD, optimizerD, optimizerG, dataloader, criterion, num_epochs, device):
    img_list = []
    G_losses = []
    D_losses = []
    train_hist = {}
    num_iter = 0
    iters = 0
    # Establish convention for real and fake labels during training
    real_label = 1
    fake_label = 0
    fixed_noise = torch.randn(64, Args.dim_z, 1, 1, device=device)
    def show_result(num_epoch, show=False, save=False, path='result.png', isFix=False):
        z_ = torch.randn((5 * 5, 100), device=device).view(-1, 100, 1, 1)
        # with torch.no_grad():
        #     z_ = Variable(z_)

        netG.eval()
        if isFix:
            test_images = netG(fixed_noise)
        else:
            test_images = netG(z_)
        netG.train()

        size_figure_grid = 5
        fig, ax = plt.subplots(size_figure_grid, size_figure_grid, figsize=(5, 5))
        for i, j in itertools.product(range(size_figure_grid), range(size_figure_grid)):
            ax[i, j].get_xaxis().set_visible(False)
            ax[i, j].get_yaxis().set_visible(False)

        for k in range(5 * 5):
            i = k // 5
            j = k % 5
            ax[i, j].cla()
            ax[i, j].imshow(test_images[k, 0].cpu().data.numpy(), cmap='gray')

        label = 'Epoch {0}'.format(num_epoch)
        fig.text(0.5, 0.04, label, ha='center')
        plt.savefig(path)

    print("Starting Training Loop...")
    # For each epoch
    for epoch in range(num_epochs):
        # For each batch in the dataloader
        for i, data in enumerate(dataloader, 0):

            ############################
            # (1) Update D network: maximize log(D(x)) + log(1 - D(G(z)))
            ###########################
            ## Train with all-real batch
            netD.zero_grad()
            # Format batch
            real_cpu = data[0].to(device)
            b_size = real_cpu.size(0)
            label = torch.full((b_size,), real_label, device=device)
            # use soft labels
            # real:0.9-1
            label = label * (0.9 + 0.1 * torch.rand(label.shape, device=device))

            #randomly flip the labels
            # label_indx = np.random.choice(len(label), int(0.05*len(label)),replace=False)
            # label[label_indx] = 1- label[label_indx]

            # print(label.shape)
            # Forward pass real batch through D
            output = netD(real_cpu).view(-1)
            # print(output.shape)
            # Calculate loss on all-real batch
            errD_real = criterion(output, label)
            # Calculate gradients for D in backward pass
            errD_real.backward()
            D_x = output.mean().item()

            ## Train with all-fake batch
            # Generate batch of latent vectors
            noise = torch.randn(b_size, Args.dim_z, 1, 1, device=device)
            # Generate fake image batch with G
            # print("noise",noise.shape)
            fake = netG(noise)
            # print("fake",fake.shape)
            label.fill_(fake_label)
            # use soft labels
            # fake 0-0.1
            label = 0.1 * torch.rand(label.shape,device=device)
            # randomly flip the labels

            # label_indx = np.random.choice(len(label), int(0.05 * len(label)), replace=False)
            # label[label_indx] = 1 - label[label_indx]

            # print("label",label.shape)
            # Classify all fake batch with D
            output = netD(fake.detach()).view(-1)
            # Calculate D's loss on the all-fake batch
            errD_fake = criterion(output, label)
            # Calculate the gradients for this batch
            errD_fake.backward()
            D_G_z1 = output.mean().item()
            # Add the gradients from the all-real and all-fake batches
            errD = errD_real + errD_fake
            # Update D
            optimizerD.step()

            ############################
            # (2) Update G network: maximize log(D(G(z)))
            ###########################
            netG.zero_grad()
            label.fill_(real_label)  # fake labels are real for generator cost
            # Since we just updated D, perform another forward pass of all-fake batch through D
            output = netD(fake).view(-1)
            # Calculate G's loss based on this output
            errG = criterion(output, label)
            # Calculate gradients for G
            errG.backward()
            D_G_z2 = output.mean().item()
            # Update G
            optimizerG.step()

            # Output training stats
            if i % 50 == 0:
                print('[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tD(x): %.4f\tD(G(z)): %.4f / %.4f'
                      % (epoch, num_epochs, i, len(dataloader),
                         errD.item(), errG.item(), D_x, D_G_z1, D_G_z2))

            # Save Losses for plotting later
            G_losses.append(errG.item())
            D_losses.append(errD.item())

            # Check how the generator is doing by saving G's output on fixed_noise
            if (iters % 500 == 0) or ((epoch == num_epochs-1) and (i == len(dataloader)-1)):
                with torch.no_grad():
                    fake = netG(fixed_noise).detach().cpu()
                img_list.append(vutils.make_grid(fake, padding=2, normalize=True))

            iters += 1

        p = 'MNIST_DCGAN_results/Random_results/MNIST_DCGAN_without_flip_' + str(epoch + 1) + '.png'
        fixed_p = 'MNIST_DCGAN_results/Fixed_results/MNIST_DCGAN_without_flip_' + str(epoch + 1) + '.png'
        show_result((epoch + 1), save=True, path=p, isFix=False)
        show_result((epoch + 1), save=True, path=fixed_p, isFix=True)

    train_hist["D_losses"]=D_losses
    train_hist["G_losses"]=G_losses
    return train_hist


from cricket_center.models import PlayerDetail, JoiningDetail, IndiaWestIndiesTeam, MatchDetail, ContestDetail
from django.db.models import Q
from django.shortcuts import render, get_object_or_404

def update_total_team_points():
        player_points_dict = {}
        for player in PlayerDetail.objects.filter(Q(player_current_team__exact='WI') | Q(player_current_team__exact='IND')):
            player_points_dict[player.player_name] = player.current_points
        for detail in JoiningDetail.objects.all():
            total_points = 0
            team = IndiaWestIndiesTeam.objects.filter(full_team_name__exact=detail.joined_user + "[" + str(detail.joined_user_team) + "]")
            total_points = total_points + player_points_dict[team.first().Keeper]
            total_points = total_points + player_points_dict[team.first().Player2]
            total_points = total_points + player_points_dict[team.first().Player3]
            total_points = total_points + player_points_dict[team.first().Player4]
            total_points = total_points + player_points_dict[team.first().Player5]
            total_points = total_points + player_points_dict[team.first().Player6]
            total_points = total_points + player_points_dict[team.first().Player7]
            total_points = total_points + player_points_dict[team.first().Player8]
            total_points = total_points + player_points_dict[team.first().Player9]
            total_points = total_points + player_points_dict[team.first().Player10]
            total_points = total_points + (player_points_dict[team.first().Player11])
            total_points = total_points + player_points_dict[team.first().Captain]
            total_points = total_points + player_points_dict[team.first().Vice_Captain] / 2
            detail.total_team_points = total_points
            detail.save()

def CallThisForDestribution(match_slug):
    contest_obj = ContestDetail.objects.filter(contest_of_match__exact=match_slug)
    for contest in contest_obj:
        if contest.confirmed or contest.filled_status:
            DistributeWinning(contest.contest_slug, contest.prize_dist_type, contest.contest_winners)
        else:
            RefundContestAmount(match_slug, contest.contest_slug)

contest_prize_dict = {"hund_players_65_winners": {1: 60, 2: 45, 3: 35, 4: 25, 5: 25, 6: 20, 7: 20, 8: 20, 9: 20, 10: 20, 11: 20, 12: 20, 13: 20, 14: 20, 15: 20, 16: 20, 17: 20, 18: 20, 19: 20,
                           20: 20, 21: 20, 22: 20, 23: 20, 24: 20, 25: 20, 26: 14, 27: 14, 28: 14, 29: 14, 30: 14, 31: 14, 32: 14, 33: 14, 34: 14, 35: 14, 36: 14,
                           37: 14, 38: 14, 39: 14, 40: 14, 41: 8, 42: 8, 43: 8, 44: 8, 45: 8, 46: 8, 47: 8, 48: 8, 49: 8, 50: 8, 51: 8, 52: 8, 53: 8, 54: 8, 55: 8,
                           56: 8, 57: 8, 58: 8, 59: 8, 60: 8, 61: 8, 62: 8, 63: 8, 64: 8, 65: 8,
    }
}

def DistributeWinning(contest_slug, prize_dist_type, winners):
    all_joined = JoiningDetail.objects.filter(joined_contest_slug__exact=contest_slug).order_by('-total_team_points')
    tie_amount = 0
    tie_count = 1
    tie_start = None
    temp_var = None
    for i, joined in enumerate(all_joined):
        print("H")
        try:
            print("T")
            if joined.total_team_points == all_joined[i + 1].total_team_points:
                temp_var = True
            else:
                temp_var = False
        except IndexError:
            temp_var = False
        if temp_var:
            tie_amount = tie_amount + contest_prize_dict[prize_dist_type][i + 1]
            tie_count = tie_count + 1
            if tie_start is None:
                tie_start = i + 1
            print("Tie Start :" + str(tie_start))
            print("Me")
        else:
            print("IE")
            if (tie_amount != 0) and (tie_start is not None):
                tie_amount = tie_amount + contest_prize_dict[prize_dist_type][joined.rank]
                print("tie" + str(tie_amount))
                print("tiecount" + str(tie_count))
                winning_amount = tie_amount / (tie_count)
                for j in all_joined[tie_start - 1: i + 1]:
                    print("Executed")
                    print(winning_amount)
                    print(j.rank, j.joined_user)
                    j.winnings = winning_amount
                    print(j.winnings, winning_amount)
                    j.save()
                tie_amount = 0
                tie_start = 0
            if (tie_amount is 0) and (tie_start is None):
                print("No Teu")
                joined.winnings = contest_prize_dict[prize_dist_type][joined.rank]
                print(contest_prize_dict[prize_dist_type][joined.rank])
                joined.save()

def RefundContestAmount():
    pass
# # # from django.http import HttpResponseRedirect
# # # from django.urls import reverse
# # # from django.conf import settings

# # # # class AuthRequiredMiddleware(object):
# # # #     def process_request(self, request):
# # # #         if not request.user.is_authenticated():
# # # #             return HttpResponseRedirect(reverse('home'))
# # # #         return None

# # # class StackOverflowMiddleware(object):
# # #     def __init__(self, get_response):
# # #         self.get_response = get_response

# # #     def __call__(self, request):
# # #         response = self.get_response(request)
# # #         if not request.user.is_authenticated():
# # #             return HttpResponseRedirect(reverse('IndexView'))
# # #         return response

# import re
# from django.conf import settings
# from django.shortcuts import redirect

# EXEMPT_URLS = ['IndexView']#[re.compile(settings.LOGIN_URL.lstrip('/'))]

# # # if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
# # #     EXEMPT_URL = []

# class LogInRequiredMiddleware:

#     def __init__(self, get_response):
#         self.get_response = get_response

#     def __call__(self, request):
#         response = self.get_response(request)
#         return response

#     def process_view(self, request, view_func, view_args, view_kwargs):
#         assert hasattr(request, 'user')
#         path = request.path_info
#         print(path)
#         if not request.user.is_authenticated():
#             if path not in EXEMPT_URLS:
#                 return redirect(settings.LOGIN_URL)

# # from django.shortcuts import HttpResponseRedirect

# # class LogInRequiredMiddleware(object):
# #     def __init__(self, get_response):
# #         self.get_response = get_response

# #     def __call__(self, request):
# #         # Code to be executed for each request before
# #         # the view (and later middleware) are called.

# #         response = self.get_response(request)
# #         if not request.user.is_authenticated():
# #             return HttpResponseRedirect('IndexView')

# #         # Code to be executed for each request/response after
# #         # the view is called.

# #         return response


from django.urls import path
from django.conf.urls import url
from . import views
app_name='myApp'
urlpatterns = [
    path('',views.index,name='index'),
    # path('Students/',views.student),
    # path('Greades/',views.greade),
    # #url可以用正则表达式
    # url(r'Greades/(\d+)\/$',views.gradestudent),
    # #在模型中添加数据
    # path('addstudent/',views.creadstudent),
    # #在控制器中添加数据
    # path('addstudent2/',views.creadstudent2),
    # #查询数据分页
    # url(r'stu/(\d+)\/$',views.stu),
    path('main/',views.main,name='shou'),
    path('login/',views.login,name='xiang'),
    path('show/',views.show,name='xianshi'),
    path('quit/',views.quit,name='tui'),
    path('varimage/',views.varcod,name='image'),
    path('fileimage/',views.file,name='files'),
    path('ajaxstudent/',views.ajaxstudents),
    path('ajaxstudent/studentsinfo/',views.studentsinfo,name='ajax'),
    path('fufu/',views.fufu,name='fuwenben'),
    path('savefufu/',views.savefufu,name='savefu'),

]


import eel, platform, sys, datetime, urllib.request, urllib.parse, chess, chess.pgn, chess.engine
from tkinter import Tk
from tkinter.filedialog import askopenfilename, asksaveasfile

eel.init("web")

whiteType = None
blackType = None
whiteID = None
blackID = None
curMove = None
curMoveNum = None
board = None
pgn = None
node = None
engine = chess.engine.SimpleEngine.popen_uci("stockfish_10_x64.exe")
whiteEngSpeed = 1.0
blackEngSpeed = 1.0
whiteEngine = None
blackEngine = None
hint = None

@eel.expose
def startNewGame(wType, bType, wID, bID):
    global whiteType
    whiteType = wType
    global blackType
    blackType = bType
    global whiteID
    whiteID = wID
    global blackID
    blackID = bID
    global curMove
    curMove = "white"
    global curMoveNum
    curMoveNum = 1
    global board
    board = chess.Board()
    global pgn
    pgn = chess.pgn.Game()
    pgn.headers["Event"] = "Desktop Chess"
    pgn.headers["Site"] = platform.node()
    pgn.headers["Date"] = datetime.datetime.now()
    pgn.headers["Round"] = "1"
    pgn.headers["White"] = whiteID
    pgn.headers["Black"] = blackID
    eel.updatePgnDisplay(str(pgn))
    updateBoardDisplay()
    if whiteType == "engine":
        global whiteEngine
        whiteEngine = chess.engine.SimpleEngine.popen_uci(whiteID)
    if blackType == "engine":
        global blackEngine
        blackEngine = chess.engine.SimpleEngine.popen_uci(blackID)
    if whiteType == "human":
        genHint()
        eel.openMoveInput(curMove)
    else:
        makeEngineMove(whiteEngine)

@eel.expose
def selectFile():
    Tk().withdraw()
    return askopenfilename()

@eel.expose
def saveFile(contents):
    Tk().withdraw()
    path = asksaveasfile()
    f = open(path.name, "w")
    f.write(contents)
    f.close()

@eel.expose
def testMove(move):
    global board
    return chess.Move.from_uci(move) in board.legal_moves

@eel.expose
def submitMove(move):
    global board
    global curMove
    global pgn
    global node
    global engine
    board.push(chess.Move.from_uci(move))
    updateBoardDisplay()
    # Update PGN and curMove
    if curMove == "white":
        global curMoveNum
        if curMoveNum == 1:
            node = pgn.add_variation(chess.Move.from_uci(move))
        else:
            node = node.add_variation(chess.Move.from_uci(move))
        curMove = "black"
    else:
        node = node.add_variation(chess.Move.from_uci(move))
        info = engine.analyse(board, chess.engine.Limit(depth=20))
        node.comment = "Score: " + str(info["score"].relative.score())
        curMove = "white"
    curMoveNum += 1
    # Check for continuation of game
    if board.is_game_over():
        updatePgnResult()
        eel.updatePgnDisplay(str(pgn))
        eel.endGame()
        return
    eel.updatePgnDisplay(str(pgn))
    # Handle next move
    if curMove == "white" and whiteType == "human":
        genHint()
        eel.openMoveInput(curMove)
    elif curMove == "black" and blackType == "human":
        genHint()
        eel.openMoveInput(curMove)
    elif curMove == "white" and whiteType == "engine":
        global whiteEngine
        makeEngineMove(whiteEngine)
    else:
        global blackEngine
        makeEngineMove(blackEngine)

def makeEngineMove(enginePlayer):
    global board
    global curMove
    global pgn
    global node
    global engine
    global whiteEngSpeed
    global blackEngSpeed
    eel.closeMoveInput(curMove)
    if curMove == "white":
        result = enginePlayer.play(board, chess.engine.Limit(time=whiteEngSpeed))
    else:
        result = enginePlayer.play(board, chess.engine.Limit(time=blackEngSpeed))
    board.push(result.move)
    updateBoardDisplay()
    # Update PGN and curMove
    if curMove == "white":
        global curMoveNum
        if curMoveNum == 1:
            node = pgn.add_variation(result.move)
        else:
            node = node.add_variation(result.move)
        curMove = "black"
    else:
        node = node.add_variation(result.move)
        info = engine.analyse(board, chess.engine.Limit(depth=20))
        node.comment = "Score: " + str(info["score"].relative.score())
        curMove = "white"
    curMoveNum += 1
    # Check for continuation of game
    if board.is_game_over():
        updatePgnResult()
        eel.updatePgnDisplay(str(pgn))
        eel.endGame()
        return
    eel.updatePgnDisplay(str(pgn))(continueFromEngineMove)

def continueFromEngineMove(x):
    global board
    global curMove
    global pgn
    global node
    global engine
    global engineTime
    # Handle next move
    if curMove == "white" and whiteType == "human":
        genHint()
        eel.openMoveInput(curMove)
    elif curMove == "black" and blackType == "human":
        genHint()
        eel.openMoveInput(curMove)
    elif curMove == "white" and whiteType == "engine":
        global whiteEngine
        makeEngineMove(whiteEngine)
    else:
        global blackEngine
        makeEngineMove(blackEngine)

def updatePgnResult():
    global board
    global pgn
    global node
    if board.is_game_over():
        result = board.result()
        pgn.headers["Result"] = result
        node.comment += "Result: " + result
        eel.updatePgnDisplay(str(pgn))

def genHint():
    global board
    global engine
    global hint
    result = engine.play(board, chess.engine.Limit(time=1))
    hint = result.move.uci()

@eel.expose
def getHint():
    global hint
    return hint

@eel.expose
def setWhiteEngSpeed(speed):
    global whiteEngSpeed
    whiteEngSpeed = float(speed)

@eel.expose
def setBlackEngSpeed(speed):
    global blackEngSpeed
    blackEngSpeed = float(speed)

def updateBoardDisplay():
    global board
    eel.updateBoardDisplay("https://chessboardimage.com/" + urllib.parse.quote(board.fen()) + ".png")

try:
    eel.start("main.html", mode="chrome", size=(1280, 720))
except EnvironmentError:
    if sys.platform in ["win32", "win64"] and int(platform.release()) >= 10:
        eel.start("main.html", mode="edge", size=(1280, 720))
    else:
        raise


# COPYRIGHT: see https://github.com/x19290/py.lisp4u/blob/master/0rights.md

from write_to import WriteTo

class Lisp(WriteTo):
    null = None  # to silent PyDev.

    def cons(self, car):
        return self.cons_ctor()(car, self)

    @classmethod
    def cons_ctor(cls):
        from .pair import Pair
        return Pair

    @staticmethod
    def pay_attention_to_the_ending_quote(params):
        iparams = iter(params)
        from ..globals.known import quote
        from .support.null_gen import NotNull
        from .pair import Pair
        from itertools import islice
        for y in iparams:
            if y is quote:
                ahead = tuple(islice(iparams, 2))
                if ahead.__len__() == 1:
                    raise NotNull(Pair.list(quote, ahead[0]))
                yield y
                for yy in ahead:
                    yield yy
                break
            else:
                yield y

del WriteTo


import json
import time
from operator import itemgetter

import openpyxl
import requests

start_time = time.time()

wb = openpyxl.load_workbook('report.xlsx')

YDAuth = {'za4aroff.ol': 'AgAAAAAzN9ktAAXAInwzJfwlY0E4taHqCQUC33I',
          'macsim.reshetar': 'AgAAAAArGY4IAAXAIq_OsOIJXEgIvBjrf0dpP0s'}

direct_client_logins = ["za4aroff.ol", "za4aroff.ol", "macsim.reshetar", "za4aroff.ol"]  # НП СПБ, НП МСК, 1001, Brand
clientTokens = {"za4aroff.ol": "AgAAAAAzN9ktAAXAInwzJfwlY0E4taHqCQUC33I",
                "macsim.reshetar": "AgAAAAArGY4IAAXAIq_OsOIJXEgIvBjrf0dpP0s"}
date1 = "2019-10-08"
date2 = "2019-10-09"
ids = ["35656455", "37340325", "42890799", "54227233"]
metrics = ["ym:s:goal39547444visits", "ym:s:goal39545641visits", "ym:s:goal44259842visits", "ym:s:goal52718371visits"]
dimensions = "ym:s:goal,ym:s:lastsignDirectClickOrder,ym:s:lastsignDirectBannerGroup"
filters = ["ym:s:goal==39547444", "ym:s:goal==39545641", "ym:s:goal==44259842", "ym:s:goal==52718371"]
url = "https://api-metrika.yandex.net/stat/v1/data"

def saveConversions(login_list, clientTokens, directIDs, metrics, startDate, endDate, filters):
    if 'Конверсии' in wb.sheetnames:
        report = wb['Конверсии']
    else:
        report = wb.create_sheet('Конверсии')

    for n, field in enumerate(['Кампания', 'Группа', 'ID Группы', 'Конверсии']):  # Вводим поля таблицы
        _ = report.cell(column=1 + n, row=1, value=field)

    for n, acc in enumerate(login_list):
        requestURL = url + "?direct_client_logins=%s&ids=%s&metrics=%s&date1=%s&date2=%s&dimensions=%s&filters=%s" \
                           "&pretty=true" % (acc, directIDs[n], metrics[n], startDate, endDate, dimensions, filters[n])

        headers = {"Authorization": "OAuth " + clientTokens.get(acc), "Client-Login": acc, }
        req = requests.get(requestURL, headers=headers)
        # print(req.text)
        jsonReport = json.loads(req.text)

        maxRow = report.max_row
        print(jsonReport["data"])
        for index, item in enumerate(jsonReport["data"]):
            if item['dimensions'][1]['name'] != "Other campaigns":
                _ = report.cell(row=index + maxRow, column=1, value=item['dimensions'][1]['name'])
                _ = report.cell(row=index + maxRow, column=2, value=item['dimensions'][2]['name'])
                _ = report.cell(row=index + maxRow, column=3, value=item['dimensions'][2]['id'])
                _ = report.cell(row=index + maxRow, column=4, value=int(item['metrics'][0]))
                print("Найдены конверсии в кампании %s, в группе %s в количестве: %s." % \
                      (item['dimensions'][1]['name'], item['dimensions'][2]['name'], int(item['metrics'][0])))
    wb.save(filename="report.xlsx")

def connectConversionsWithCampaigns():  # Добавляем конверсии в таблицу с данными
    data = wb["Данные"]
    conversions = wb["Конверсии"]
    for n, field in enumerate(["Конверсии", "% конверсии", "CPA"]):  # Вводим поля таблицы
        _ = data.cell(column=8 + n, row=1, value=field)

    dataList = []  # Сортируем "Данные" по группе объявлений
    for n, row in enumerate(data):
        dataList.append([])
        for col in row:
            dataList[n].append(col.value)
    sortedCosts = sorted(dataList[1:], key=itemgetter(3))
    maxCol = data.max_column
    for n, row in enumerate(sortedCosts):
        for col in range(maxCol):
            _ = data.cell(row=n + 2, column=col + 1, value=row[col])

    dataList = []  # Сортируем "Конверсии" по группе объявлений
    dataRow = 0
    for n, row in enumerate(conversions):
        if row[2].value != None:
            dataList.append([])
            for m, col in enumerate(row):
                # print(col.value)
                if col.value == None:
                    dataList[dataRow].append("")
                elif str(col.value).isdigit():
                    dataList[dataRow].append(int(col.value))
                else:
                    dataList[dataRow].append(str(col.value))
            dataRow += 1

    # print(dataList)
    sortedConversions = sorted(dataList[1:], key=itemgetter(2))
    maxCol = conversions.max_column
    for n, row in enumerate(sortedConversions):
        for col in range(maxCol):
            _ = conversions.cell(row=n + 2, column=col + 1, value=row[col])

    convCounter = 1
    for camp in range(1, data.max_row):  # Соотносим конверсии с группами объявлений
        if convCounter < conversions.max_row:
            if data["D"][camp].value == int(conversions["C"][convCounter].value):
                _ = data.cell(row=camp + 1, column=8, value=int(conversions["D"][convCounter].value))
                _ = data.cell(row=camp + 1, column=10, value=float(data["G"][camp].value)
                                                             / int(conversions["D"][convCounter].value))
                if data["F"][camp].value > 0:
                    _ = data.cell(row=camp + 1, column=9,
                                  value=int(conversions["D"][convCounter].value) / data["F"][camp].value)
                else:
                    _ = data.cell(row=camp + 1, column=9, value=0)
                print("Выгружаю конверсии для %s из кампании %s." %
                      (conversions["B"][convCounter].value, conversions["A"][convCounter].value))
                convCounter += 1
        else:
            break

    wb.save(filename="report.xlsx")

def mainFunc(login_list, clientTokens, directIDs, metrics, startDate, endDate, filters):
    saveConversions(login_list, clientTokens, directIDs, metrics, startDate, endDate, filters)
    connectConversionsWithCampaigns()

mainFunc(direct_client_logins, clientTokens, ids, metrics, date1, date2, filters)
print("\n--- %s seconds ---" % (time.time() - start_time))


import pygame

def load_image(name):
    fullname = '/'.join(['./data', name])
    image = pygame.image.load(fullname)
    image = image.convert_alpha()
    return image

def create_img(name, r):
    if name == 'Earth':
        clr = 'green'
    elif name == 'Solar':
        clr = 'yellow'
    elif name == 'Moon':
        clr = 'white'
    else:
        clr = 'cyan'
    image = pygame.Surface((2 * r, 2 * r))
    pygame.draw.circle(image, pygame.Color(clr), (r, r), r)
    return image

def create_screen(text):
    w = 1200
    h = 800
    image = pygame.Surface((w, h))
    image.fill((0, 0, 128))
    font = pygame.font.Font(None, 60)
    text_render = font.render(text, 1, pygame.Color('white'))
    text_rect = text_render.get_rect()
    text_rect.x = w//2 - text_rect.width // 2
    text_rect.y = h//2 - text_rect.height // 2
    image.blit(text_render, text_rect)
    return image


from kivy.app import App
from kivy.config import Config
Config.set('graphics', 'fullscreen', '0')
Config.set('graphics', 'width', '648')
Config.set('graphics', 'height', '1152')
Config.write()
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.core.text import FontContextManager as FCM
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button  # przyciski
from kivy.uix.image import Image
from kivy.graphics import *
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
import gmaps
import googlemaps
import sys
import datetime
from datetime import timedelta
import requests

gmaps.configure(api_key="KLUCZAPI")
GoogleMaps=googlemaps.Client(key="KLUCZAPI") #dla skrócenia komend

class LoginScreen(FloatLayout):  # ekran nr 1

    def __init__(self, **var_args):

        super(LoginScreen, self).__init__(**var_args)
        now = datetime.datetime.now()
        hour = str(now.hour)
        if hour == "0":
            hour = "00"
        minuta = str(now.minute)

        #tlo
        self.tlo = Image(source='Tloo1.png')
        self.tlo.opacity = 0.75
        self.tlo.size_hint_x = 1
        self.tlo.size_hint_y = 1
        self.tlo.pos_hint = {"x": 0, "top": 1}
        self.add_widget(self.tlo)

        self.skad = TextInput(multiline=False, size_hint=(0.45, 0.1), font_size=20, pos_hint={"x": 0.28, "top": 0.7},
                              text='            Skąd wychodzisz?')
        self.add_widget(self.skad)

        self.dokad = TextInput(multiline=False, size_hint=(0.45, 0.1), font_size=20, pos_hint={"x": 0.28, "top": 0.5},
                               text='       Dokąd chcesz dojechać?')
        self.add_widget(self.dokad)

        self.godzina = TextInput(multiline=False, size_hint=(0.05, 0.04), font_size=15, pos_hint={"x": 0.4, "top": 0.3},
                              text=hour)

        self.minuta = TextInput(multiline=False, size_hint=(0.05, 0.04), font_size=15, pos_hint={"x": 0.5, "top": 0.3},
                                 text=minuta)
        self.add_widget(self.godzina)
        self.add_widget(self.minuta)

        przycisk = Button(text='Wyszukaj!')
        przycisk.bind(on_press=self.clkfunc)
        przycisk.opacity = 0.9
        self.add_widget(przycisk)

        # logo na gorze
        self.img = Image(source='2019.png')
        self.img.opacity = 0.9
        self.img.size_hint_x = 1
        self.img.size_hint_y = 0.26
        self.img.pos_hint = {"x": 0, "top": 0.98}
        self.add_widget(self.img)

        # obrazek skad
        self.img1 = Image(source='skad2.png')
        self.img1.opacity = 0.9
        self.img1.size_hint_x = 0.1
        self.img1.size_hint_y = 0.05
        self.img1.pos_hint = {"x": 0.18, "top": 0.7}
        self.add_widget(self.img1)

        # obrazek dokad
        self.img2 = Image(source='skad3.png')
        self.img2.opacity = 0.9
        self.img2.size_hint_x = 0.1
        self.img2.size_hint_y = 0.05
        self.img2.pos_hint = {"x": 0.18, "top": 0.5}
        self.add_widget(self.img2)

        # obrazek zegara
        self.img3 = Image(source='clocku.png')
        self.img3.opacity = 0.9
        self.img3.size_hint_x = 0.1
        self.img3.size_hint_y = 0.05
        self.img3.pos_hint = {"x": 0.18, "top": 0.31}
        self.add_widget(self.img3)

    def clkfunc(self, event): # przycisk ktory odpowiada za zebranie danych + nowy ekran

        adres1 = self.skad.text + "Łódź"
        adres2 = self.dokad.text+ "Łódź"

        now=datetime.datetime.now()
        now-= timedelta(hours=(now.hour),minutes=now.minute)
        now+= timedelta(hours=int(self.godzina.text),minutes=int(self.minuta.text))

        #adres1 = "Piotrkowska cetnurm łódź"
        #adres2 = "Mikołaja Kopernika 76/78"

        self.clear_widgets()

        while True:
            try:
                geocode1 = GoogleMaps.geocode(adres1)
                geocode2 = GoogleMaps.geocode(adres2)
                for x, y in geocode1[0].items():  # wyciganiecie szerokosci geograf z pierwszej lokalizacji
                    if (x == "geometry"):
                        for i, j in y.items():
                            if (i == "location"):
                                lat1 = list(j.values())[0]
                                lng1 = list(j.values())[1]
                                lokacja1 = []
                                lokacja1.append(lat1)
                                lokacja1.append(lng1)

                for xx, yy in geocode2[0].items():  # wyciganiecie szerokosci geograf z 2 lokalizacji
                    if (xx == "geometry"):
                        for ii, jj in yy.items():
                            if (ii == "location"):
                                lat2 = list(jj.values())[0]
                                lng2 = list(jj.values())[1]
                                lokacja2 = []
                                lokacja2.append(lat2)
                                lokacja2.append(lng2)
                break
            except:
                print("Błedne lokacje lub brak przejazdów")
                sys.exit()

        POMIN = False
        przesiadka = False
        wskazowki = (GoogleMaps.directions(lokacja1, lokacja2, mode="transit", departure_time=now))
        direkcje = (GoogleMaps.distance_matrix(adres1, adres2, mode='transit'))

        odlegloscD = (direkcje['rows'][0]['elements'][0]['distance']['text'])  # odleglosc całej drogi od A do B
        czasD = (direkcje['rows'][0]['elements'][0]['duration']['text'])

        if 'Walk' in (wskazowki[0]['legs'][0]['steps'][0]['html_instructions']) and len(wskazowki[0]['legs'][0]['steps'])<2:
            POMIN = True
        try:
            nazwatramwaju = (wskazowki[0]['legs'][0]['steps'][0]['transit_details']['headsign'])
            nrtramwaju = (wskazowki[0]['legs'][0]['steps'][0]['transit_details']['line']['short_name'])
            godzinawyjazdutram = (wskazowki[0]['legs'][0]['steps'][0]['transit_details']['departure_time']['text'])
            godzinadojazdu = (wskazowki[0]['legs'][0]['steps'][0]['transit_details']['arrival_time']['text'])
            gdziewsiadasz = (wskazowki[0]['legs'][0]['steps'][0]['transit_details']['departure_stop']['name'])
            lprzystankow = (wskazowki[0]['legs'][0]['steps'][0]['transit_details']['num_stops'])
        except:
            try:
                nazwatramwaju = (wskazowki[0]['legs'][0]['steps'][1]['transit_details']['headsign'])
                lprzystankow = (wskazowki[0]['legs'][0]['steps'][1]['transit_details']['num_stops'])
                nrtramwaju = (wskazowki[0]['legs'][0]['steps'][1]['transit_details']['line']['short_name'])
                godzinawyjazdutram = (wskazowki[0]['legs'][0]['steps'][1]['transit_details']['departure_time']['text'])
                godzinadojazdu = (wskazowki[0]['legs'][0]['steps'][1]['transit_details']['arrival_time']['text'])
                gdziewsiadasz = (wskazowki[0]['legs'][0]['steps'][1]['transit_details']['departure_stop']['name'])
            except:
                lprzystankow = ""
                nrtramwaju = ""
                godzinawyjazdutram = ""
                godzinadojazdu = ""
                gdziewsiadasz = ""
                nazwatramwaju = ""

        try:
            nazwatramwaju2 = (wskazowki[0]['legs'][0]['steps'][2]['transit_details']['headsign'])
            nrtramwaju2 = (wskazowki[0]['legs'][0]['steps'][2]['transit_details']['line']['short_name'])
            lprzystankow2 = (wskazowki[0]['legs'][0]['steps'][2]['transit_details']['num_stops'])
            godzinawyjazdutram2 = (wskazowki[0]['legs'][0]['steps'][2]['transit_details']['departure_time']['text'])
            godzinadojazdu2 = (wskazowki[0]['legs'][0]['steps'][2]['transit_details']['arrival_time']['text'])
            gdziewsiadasz2 = (wskazowki[0]['legs'][0]['steps'][2]['transit_details']['departure_stop']['name'])
        except:
            try:
                gdziewsiadasz2 = (wskazowki[0]['legs'][0]['steps'][3]['transit_details']['departure_stop']['name'])
                godzinadojazdu2 = (wskazowki[0]['legs'][0]['steps'][3]['transit_details']['arrival_time']['text'])
                godzinawyjazdutram2 = (wskazowki[0]['legs'][0]['steps'][3]['transit_details']['departure_time']['text'])
                lprzystankow2 = (wskazowki[0]['legs'][0]['steps'][3]['transit_details']['num_stops'])
                nrtramwaju2 = (wskazowki[0]['legs'][0]['steps'][3]['transit_details']['line']['short_name'])
                nazwatramwaju2 = (wskazowki[0]['legs'][0]['steps'][3]['transit_details']['headsign'])
            except:
                nazwatramwaju2 = ""
                nrtramwaju2 = ""
                lprzystankow2 = ""
                godzinawyjazdutram2 = ""
                godzinadojazdu2 = ""
                gdziewsiadasz2 = ""

        if not POMIN:

            if nazwatramwaju2!="":
                droga=(
                    "\nWyjazd: " + godzinawyjazdutram + "\nze stacji: \n"+gdziewsiadasz2+ "\n\nWsiądź do: \n     [" + str(
                        nrtramwaju) + "] " + str(nazwatramwaju) + "\nPrzejedź " + str(
                        lprzystankow)+ " przystanków " + "\n\nWsiądź do: \n     [" + str(
                        nrtramwaju2) + "] " + str(nazwatramwaju2) + "\nPrzejedź " + str(
                        lprzystankow2)+  "przystanków\n\n\nOdległość: " + odlegloscD)
            else:
                droga=("\nWyjazd: " + godzinawyjazdutram + "\nze stacji: \n"+gdziewsiadasz+ "\nWsiądź do: [" +str(nrtramwaju) +"] "+ str(
                    nazwatramwaju) + "\nLiczba przystanków: " + str(lprzystankow)+"\nOdległość: " + odlegloscD  +"\nDojedziesz o: " +godzinadojazdu)

        else:

            if (len(wskazowki[0]['legs'][0]['steps'])) == 1 and godzinawyjazdutram!="" :
                droga=("\nWyjazd o:\n" + godzinawyjazdutram + "\n\n" + wskazowki[0]['legs'][0]['steps'][0][
                'html_instructions'] + "\nNazwa tramwaju:\n" + str(nazwatramwaju) + "\nNr: " + str(nrtramwaju))

            elif (len(wskazowki[0]['legs'][0]['steps'])) == 2:
                droga=(wskazowki[0]['legs'][0]['steps'][0][
                'html_instructions'] + "\nWyjedź o: " + godzinawyjazdutram + "\nOdległoość: " + odlegloscD + "\nCzas: " + czasD + "\nNazwa pierwszego tramwaju: " + str(
                nazwatramwaju) + "\nNr: " + str(nrtramwaju) + "\nLiczba przystanków: " + str(
                lprzystankow) + "\nNazwa drugiego tramwaju: " + str(nazwatramwaju2) + " Nr: " + str(
                nrtramwaju2) + "\nLiczba przystanków: " + str(lprzystankow2))
            else:
                droga=("Przejdź się!\n"+wskazowki[0]['legs'][0]['steps'][0]['html_instructions'])

        self.add_widget(Button(text=droga,pos_hint = {"x": 0, "top": 1},size_hint=(1.1, 1)))
        self.tlo = Image(source='Tloo1.png')
        self.tlo.opacity = 0.2
        self.tlo.size_hint_x = 1
        self.tlo.size_hint_y = 1
        self.tlo.pos_hint = {"x": 0, "top": 1}
        self.add_widget(self.tlo)

        self.canvas.add(Color(72/255, 19/255, 91/255))
        self.canvas.add(Rectangle(pos=(0, 730), size=(1000, 130)))

        print(len(wskazowki[0]['legs'][0]['steps']))

        self.two = Image(source='twopts.png')
        self.two.opacity = 0.9
        self.two.size_hint_x = 0.2
        self.two.size_hint_y = 0.1
        self.two.pos_hint = {"x": 0.4, "top": 0.98}
        self.add_widget(self.two)
        #user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
        #map.map_source = source
        #MapView().map_source = "osm"
        #mapview = MapView(zoom=11, lat=50.6394, lon=3.057)
        #self.add_widget(mapview)

class MyApi(App):
    def build(self):
        self.title = 'Trip Planner- Mikolaj Bozecki'
        return LoginScreen()

if __name__ == '__main__':
    MyApi().run()


"""Preference management for camera component."""
from __future__ import annotations

from typing import Final

from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import UNDEFINED, UndefinedType

from .const import DOMAIN, PREF_PRELOAD_STREAM

STORAGE_KEY: Final = DOMAIN
STORAGE_VERSION: Final = 1

class CameraEntityPreferences:
    """Handle preferences for camera entity."""

    def __init__(self, prefs: dict[str, bool]) -> None:
        """Initialize prefs."""
        self._prefs = prefs

    def as_dict(self) -> dict[str, bool]:
        """Return dictionary version."""
        return self._prefs

    @property
    def preload_stream(self) -> bool:
        """Return if stream is loaded on hass start."""
        return self._prefs.get(PREF_PRELOAD_STREAM, False)

class CameraPreferences:
    """Handle camera preferences."""

    def __init__(self, hass: HomeAssistant) -> None:
        """Initialize camera prefs."""
        self._hass = hass
        self._store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY)
        self._prefs: dict[str, dict[str, bool]] | None = None

    async def async_initialize(self) -> None:
        """Finish initializing the preferences."""
        prefs = await self._store.async_load()

        if prefs is None:
            prefs = {}

        self._prefs = prefs

    async def async_update(
        self,
        entity_id: str,
        *,
        preload_stream: bool | UndefinedType = UNDEFINED,
        stream_options: dict[str, str] | UndefinedType = UNDEFINED,
    ) -> None:
        """Update camera preferences."""
        # Prefs already initialized.
        assert self._prefs is not None
        if not self._prefs.get(entity_id):
            self._prefs[entity_id] = {}

        for key, value in ((PREF_PRELOAD_STREAM, preload_stream),):
            if value is not UNDEFINED:
                self._prefs[entity_id][key] = value

        await self._store.async_save(self._prefs)

    def get(self, entity_id: str) -> CameraEntityPreferences:
        """Get preferences for an entity."""
        # Prefs are already initialized.
        assert self._prefs is not None
        return CameraEntityPreferences(self._prefs.get(entity_id, {}))


from flask import Blueprint, redirect, render_template, request, url_for, flash
from Blog import db
from flask_login import login_required
from Blog.post.forms import PostForm
from flask_login import current_user
from Blog.post.models import Posts

post = Blueprint('post', __name__)

@post.route('/post/new', methods = ['GET', 'POST'])
@login_required
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        post = Posts(title = form.title.data, content = form.content.data, author = current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post has been created!', 'success')
        return redirect(url_for('main.home'))
    return render_template('create_post.html', title="New Post",  form=form, legend = 'New Post')

@post.route('/post/<int:post_id>')
def post_detail(post_id):
    post = Posts.query.get_or_404(post_id)
    return render_template('post.html', titile = post.title, post = post)

@post.route('/post/<int:post_id>/delete', methods = ['POST'])
def delete_post(post_id):
    post = Posts.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    db.session.delete(post)
    db.session.commit()
    flash('Your post has been deleted!', 'success')
    return redirect(url_for('main.home'))

@post.route('/post/<int:post_id>/update', methods = ['GET', 'POST'])
@login_required
def update_post(post_id):
    post = Posts.query.get_or_404(post_id)
    if post.author != current_user:
        abort(403)
    form = PostForm()
    if form.validate_on_submit():
        post.title = form.title.data
        post.content = form.content.data
        db.session.commit()
        flash('Your post has been updated!', 'success')
        return redirect(url_for('post.post_detail', post_id = post.id))
    elif request.method == 'GET':
        form.title.data = post.title
        form.content.data = post.content
    return render_template('create_post.html', title = 'Update Post', form = form, legend = 'Update Post')


from torch import tensor

from QNetwork import QNetwork
import random
import torch
import torch.nn.functional as F
import numpy as np
from ReplayBuffer import ReplayBuffer
import os

BATCH_SIZE = 64
GAMMA = 0.99  #
TAU = 1e-3

class Agent:
    def __init__(self, env, action_size, state_size, use_dueling=False,
                 use_double=False, network_file=None):
        self.device = torch.device('cpu')
        self.action_size = action_size
        self.env = env
        self.state_size = state_size
        self.seed = 1234
        self.target_network = QNetwork(state_size=state_size, action_size=action_size, seed=self.seed,
                                       use_dueling=use_dueling).to(self.device)
        self.local_network = QNetwork(state_size=state_size, action_size=action_size, seed=self.seed,
                                      use_dueling=use_dueling).to(self.device)
        self.optimizer = torch.optim.Adam(self.local_network.parameters(), lr=5e-4)

        if network_file is not None:
            if os.path.exists(network_file):
                checkpoints = torch.load(network_file)
                self.local_network.load_state_dict(checkpoints['local'])
                self.target_network.load_state_dict(checkpoints['target'])
                self.optimizer.load_state_dict(checkpoints['optimizer'])

        self.memory = ReplayBuffer(self.seed, batch_size=BATCH_SIZE, device=self.device)
        self.use_double = use_double

    def act(self, state, epsilon):
        if random.random() > epsilon:
            self.local_network.eval()
            state = torch.from_numpy(state).float().unsqueeze(0).to(self.device)
            ans = None
            with torch.no_grad():
                ans = self.local_network.forward(state).cpu().data.numpy()
                ans = np.argmax(ans).astype(int)
            self.local_network.train()
            return ans
        else:
            return random.choice(range(self.action_size))

    def soft_update(self, local_model, target_model):
        for target_param, local_param in zip(target_model.parameters(), local_model.parameters()):
            target_param.data.copy_(TAU * local_param.data + (1.0 - TAU) * target_param.data)

    def step(self, timestep, state, next_state, action, reward, done):
        self.memory.add(state, next_state, action, reward, done)

        if timestep % 4 == 0:
            if len(self.memory) >= BATCH_SIZE:
                states, next_states, actions, rewards, dones = self.memory.sample()
                q_target = None
                if not self.use_double:
                    q_target = self.target_network(next_states).detach().max(1)[0].unsqueeze(1)  # need to be fixed
                else:
                    indice = torch.argmax(self.local_network(next_states).detach(), 1)
                    q_target = self.target_network(next_states).detach().gather(1, indice.unsqueeze(1))

                target = rewards + (GAMMA * q_target * (1 - dones))
                q_expected = self.local_network(states).gather(1, actions)
                loss = F.mse_loss(q_expected, target)
                self.optimizer.zero_grad()
                loss.backward()
                self.optimizer.step()
                self.soft_update(self.local_network, self.target_network)


from sqlalchemy import (
    Column,
    String,
    Integer,
    ForeignKey,
    Boolean,
    Float,
    UniqueConstraint,
    ForeignKeyConstraint,
)
from sqlalchemy.orm import relationship

from gresq.database import Base

class SemAnalysis(Base):
    """[summary]

    Args:
        Base ([type]): [description]

    Returns:
        [type]: [description]
    """

    __tablename__ = "sem_analysis"
    id = Column(Integer, primary_key=True, info={"verbose_name": "ID"})
    sem_file_id = Column(
        Integer, ForeignKey("sem_file.id", ondelete="CASCADE"), index=True
    )
    software_name = Column(String(20), info={"verbose_name": "Analysis Software"})
    software_version = Column(String(20), info={"verbose_name": "Software Version"})
    __table_args__ = (
        ForeignKeyConstraint(
            [software_name, software_version],
            ["software.name", "software.version"],
            name="fk_sem_analysis_software",
        ),
    )

    __table_args__ = (UniqueConstraint("id", "sem_file_id"),)
    __mapper_args__ = {"confirm_deleted_rows": False}
    sem_file = relationship(
        "SemFile",
        back_populates="analyses",
        foreign_keys=[sem_file_id],
        lazy="subquery",
    )

    mask_url = Column(String(256))
    px_per_um = Column(Integer, info={"verbose_name": "Pixels/um"})
    growth_coverage = Column(
        Float,
        info={
            "verbose_name": "Growth Coverage",
            "std_unit": "%",
            "conversions": {"%": 1},
            "required": False,
        },
    )
    automated = Column(
        Boolean, info={"verbose_name": "Automated Detection"}, default=False
    )

    def json_encodable(self):
        return {
            "growth_coverage": {"value": self.growth_coverage, "unit": "%"},
            "px_per_um": {"value": self.px_per_um, "unit": "1/um"},
            "automated": {"value": self.automated, "unit": None},
        }


#
# plan class for Gig-o-Matic 2 - links members to gigs
#
# Aaron Oppenheimer
# 24 August 2013
#

import debug
from google.appengine.ext import ndb
from debug import debug_print
import webapp2

import gig
import band
import member
import assoc
import datetime

plan_text = ["No Plan", "Definitely", "Probably", "Don't Know", "Probably Not", "Can't Do It", "Not Interested"]

#
# class for plan
#
class Plan(ndb.Model):
    """ Models a gig-o-matic plan """
    member = ndb.KeyProperty()
    value = ndb.IntegerProperty()
    feedback_value = ndb.IntegerProperty()
    comment = ndb.StringProperty(indexed=False)
    section = ndb.KeyProperty()
    last_update = ndb.DateTimeProperty(auto_now=True)

    @classmethod
    def lquery(cls, *args, **kwargs):
        if debug.DEBUG:
            print('{0} query'.format(cls.__name__))
        return cls.query(*args, **kwargs)

    def set_comment(self, the_comment):
        self.comment=the_comment;
        self.put()

def new_plan(the_gig, the_member, value):
    """ associate a gig and a member """
    the_plan = Plan(parent=the_gig.key, member=the_member.key, value=value, comment="", section=None)
    the_plan.put()
    return the_plan

def get_plan_from_id(the_gig, id):
    """ Return plan object by id; needs the key for the parent, which is the band for this plan"""
    return Plan.get_by_id(int(id), parent=the_gig.key)

def get_plan_keys_for_gig_key(the_gig_key):
    """ Return plan objects by gig"""
    plan_query = Plan.lquery(ancestor=the_gig_key)
    plan_keys = plan_query.fetch(keys_only=True)
    return plan_keys

def get_plan_for_member_key_for_gig_key(the_member_key, the_gig_key):
    plan_query = Plan.lquery(Plan.member==the_member_key, ancestor=the_gig_key)
    plans = plan_query.fetch()
    if len(plans)>1:
        return None #todo what to do if there's more than one plan
    if len(plans)>0:
        return plans[0]
    else:
        # no plan? make a new one
        return new_plan(the_gig_key.get(), the_member_key.get(), 0)

def get_plan_for_member_for_gig(the_member, the_gig):
    return get_plan_for_member_key_for_gig_key(the_member_key=the_member.key, the_gig_key=the_gig.key)

def get_recent_changes_for_band_key(the_band_key, the_time_delta_days=1, keys_only=False):
    "find plans for gigs belonging to a band key that have changed lately"
    plan_query = Plan.lquery(Plan.last_update>(datetime.datetime.now() - datetime.timedelta(days=the_time_delta_days)), ancestor=the_band_key)
    plans = plan_query.fetch(keys_only=keys_only)
    return plans

def update_plan(the_plan, the_value):
    the_plan.value=the_value
    the_plan.put()

def update_plan_feedback(the_plan, the_value):
    the_plan.feedback_value=the_value
    the_plan.put()

def update_plan_comment(the_plan, the_value):
    the_plan.comment=the_value
    the_plan.put()

def update_plan_section_key(the_plan, the_section_key):
    the_plan.section=the_section_key
    the_plan.put()

def remove_section_from_plans(the_section_key):
    """ if any plans have this section key, set the section to None """
    plan_query = Plan.lquery(Plan.section==the_section_key)
    the_plans = plan_query.fetch()
    for the_plan in the_plans:
        the_plan.section=None
    ndb.put_multi(the_plans)

def delete_plans_for_gig_key(the_gig_key):
    """ A gig is being deleted, so forget everyone's plans about it """
    plan_query = Plan.lquery(ancestor=the_gig_key)
    the_plan_keys = plan_query.fetch(keys_only=True)
    ndb.delete_multi(the_plan_keys)

def delete_plans_for_member_key_for_band_key(the_member_key, the_band_key):
    """ A gig is being deleted, so forget everyone's plans about it """
    plan_query = Plan.lquery(Plan.member==the_member_key, ancestor=the_band_key)
    plans = plan_query.fetch(keys_only=True)
    ndb.delete_multi(plans)

class UpdatePlan(webapp2.RequestHandler):
    def post(self):
        """post handler - if we are edited by the template, handle it here and redirect back to info page"""
        the_value=int(self.request.get("val", 0))
        the_plan_key=self.request.get("pk",'0')

        if (the_plan_key=='0'):
            return #todo figure out what to do if no plan passed in

        the_plan=ndb.Key(urlsafe=the_plan_key).get()

        if (the_plan is not None):
            update_plan(the_plan, the_value)
        else:
            pass # todo figure out why there was no plan

class UpdatePlanFeedback(webapp2.RequestHandler):
    def post(self):
        """post handler - if we are edited by the template, handle it here and redirect back to info page"""
        the_value=int(self.request.get("val", 0))
        the_plan_key=self.request.get("pk",'0')

        if (the_plan_key=='0'):
            return #todo figure out what to do if no plan passed in

        the_plan=ndb.Key(urlsafe=the_plan_key).get()

        if (the_plan is not None):
            update_plan_feedback(the_plan, the_value)
        else:
            pass # todo figure out why there was no plan

        strings = band.get_feedback_strings(the_plan.key.parent().parent().get())

        if the_value == 0:
            resp = ''
        elif the_value <= len(strings):
            resp = strings[the_value-1]
        self.response.write(resp)

class UpdatePlanComment(webapp2.RequestHandler):
    def post(self):
        """post handler - if a comment is edited, update the database"""

        the_value=self.request.get("value", "")
        the_plan_key=self.request.get("pk",'0')

        if (the_plan_key=='0'):
            return #todo figure out what to do if no plan passed in

        the_plan=ndb.Key(urlsafe=the_plan_key).get()

        if (the_plan is not None):
            update_plan_comment(the_plan, the_value)
        else:
            pass # todo figure out why there was no plan

class UpdatePlanSection(webapp2.RequestHandler):
    def post(self):
        """post handler - if a section is edited, update the database"""

        the_section_keyurl=self.request.get("sk", '0')
        the_plan_keyurl=self.request.get("pk",'0')

        if (the_plan_keyurl=='0' or the_section_keyurl=='0'):
            return #todo figure out what to do if no plan passed in

        the_plan=ndb.Key(urlsafe=the_plan_keyurl).get()
        the_section_key=ndb.Key(urlsafe=the_section_keyurl)

        if (the_plan is not None):
            update_plan_section_key(the_plan, the_section_key)
        else:
            pass # todo figure out why there was no plan


"""sys import """
import sys
from PyQt5.QtWidgets import (
    QApplication,
    QLabel,
    QPushButton,
    QVBoxLayout,
    QMainWindow
)
from PyQt5.QtGui import(QPalette, QColor)

class Main(QMainWindow):
    """
    Main class
    """
    def __init__(self):
        super().__init__()
        self.setWindowTitle("My App")
        label = QLabel(self)
        label.setText("chck")
        layout = QVBoxLayout()
        layout.addWidget(label)
        self.setLayout(layout)
        # self.show()

palette = QPalette()
palette.setColor(QPalette.Window, QColor(53,78,98))
app = QApplication(sys.argv)
app.setPalette(palette)
w = Main()
w.show()

app.exec_()


import koconf.common as common
import pytest

from datetime import datetime, timedelta
from koconf.db import DataBaseManager
from tinydb import TinyDB, Query
from tinydb.storages import MemoryStorage

def days_from_now(days):
    return (datetime.now() + timedelta(days=days)).strftime(common.date_format)

@pytest.fixture(autouse=True)
def mock_init(mocker):
    def init(self):
        self._DataBaseManager__db = TinyDB(storage=MemoryStorage)
        self._DataBaseManager__query = Query()
        data = [
            {
                "id": "BAPPLY",
                "title": "BAPPLY, 서울",
                "city": "서울",
                "address": "강남구",
                "homepage": "https://test.data.complete",
                "tag": "BAPPLY, APPLY, SEOUL",
                "apply_start": days_from_now(1),
                "apply_end": days_from_now(2),
                "event_start": days_from_now(3),
                "event_end": days_from_now(4),
                "is_remind": True,
            },
            {
                "id": "OAPPLY",
                "title": "OAPPLY, 대전",
                "city": "대전",
                "address": "중구",
                "homepage": "https://test.data.complete",
                "tag": "OAPPLY, APPLY, DAEJEON",
                "apply_start": days_from_now(-1),
                "apply_end": days_from_now(2),
                "event_start": days_from_now(3),
                "event_end": days_from_now(4),
                "is_remind": False,
            },
            {
                "id": "BEVENT",
                "title": "BEVENT, 서울",
                "city": "서울",
                "address": "강남구",
                "homepage": "https://test.data.complete",
                "tag": "BEVENT, EVENT, SEOUL",
                "apply_start": days_from_now(-2),
                "apply_end": days_from_now(-1),
                "event_start": days_from_now(3),
                "event_end": days_from_now(4),
                "is_remind": False,
            },
            {
                "id": "OEVENT",
                "title": "BEVENT, 부산",
                "city": "부산",
                "address": "해운대구",
                "homepage": "https://test.data.complete",
                "tag": "OEVENT, EVENT, BUSAN",
                "apply_start": days_from_now(-4),
                "apply_end": days_from_now(-3),
                "event_start": days_from_now(-1),
                "event_end": days_from_now(4),
                "is_remind": False,
            },
            {
                "id": "EXPIRE",
                "title": "EXPIRE, 대전",
                "city": "대전",
                "address": "중구",
                "homepage": "https://test.data.complete",
                "tag": "EXPIRE, DAEJEON",
                "apply_start": days_from_now(-5),
                "apply_end": days_from_now(-4),
                "event_start": days_from_now(-3),
                "event_end": days_from_now(-2),
                "is_remind": True,
            },
        ]
        self._DataBaseManager__db.insert_multiple(data)

    mocker.patch.object(DataBaseManager, '__init__', init)

@pytest.fixture()
def db():
    with DataBaseManager() as db:
        yield db

@pytest.mark.parametrize(
    'method, expects',
    [
        (
            'show',
            ['BAPPLY', 'OAPPLY', 'BEVENT', 'OEVENT', 'EXPIRE'],
        ),
        (
            'show_by_applicable',
            ['BAPPLY', 'OAPPLY'],
        ),
    ],
)
def test_show_with_no_param(db, method, expects):
    result = getattr(db, method)().get_string()

    for expect in expects:
        assert expect in result

@pytest.mark.parametrize(
    'method, param, expects',
    [
        (
            'show_by_id',
            'BAPPLY',
            ['BAPPLY'],
        ),
        (
            'show_by_id',
            'OAPPLY',
            ['OAPPLY'],
        ),
        (
            'show_by_title',
            'BEVENT',
            ['BEVENT'],
        ),
        (
            'show_by_title',
            '서울',
            ['BAPPLY', 'BEVENT'],
        ),
        (
            'show_by_city',
            '대전',
            ['OAPPLY'],
        ),
        (
            'show_by_city',
            '서울',
            ['BAPPLY', 'BEVENT'],
        ),
        (
            'show_by_tag',
            'EVENT',
            ['BEVENT', 'OEVENT'],
        ),
        (
            'show_by_tag',
            'SEOUL',
            ['BAPPLY', 'BEVENT'],
        ),
    ],
)
def test_show_with_single_param(db, method, param, expects):
    result = getattr(db, method)(param).get_string()

    for expect in expects:
        assert expect in result


import wikipedia as wiki
import pandas as pd
import os

GENRE_TYPES = 'Talk-Show|News'
TITLE_TYPE = 'tvSeries'
PROFESSION_TYPES =  [' host',' presenter',' journalist',' news anchor','correspondent',' anchor']
SPLIT_TYPES = [' is a ',' is an ',' was a ',' was an ',' is the ']
NONE = 'Not Found'
PAGE_ERROR = 'does not match any pages'
DISAMBIGUATION_ERROR = 'disambiguation'
NETWORK_TYPES = ['FOX','ABC','CBS','NBC','CNN','United Paramount Net','Warner Bros.','Pure Independent','PBS','Pax TV','Telemundo']
REGION_TYPE = 'US'
SHOW_NOT_IN_IMDB = 'Show is not present in IMDb'
NAME_BASICS = "IMDB_Datasets/name.basics.tsv/data.tsv"
TITLE_BASICS = "IMDB_Datasets/title.basics.tsv/data.tsv"
TITLE_PRINCIPALS = "IMDB_Datasets/title.principals.tsv/data.tsv"
TITLE_AKAS = "IMDB_Datasets/title.akas.tsv/data.tsv"
TITLE_CREW = "IMDB_Datasets/title.crew.tsv/data.tsv "
Read_Namebasics = pd.read_table(NAME_BASICS, sep='\t')

""" After getting all professions of a person we can easily confirm that
    our previous top 5 prediction is right or not & by using this we find
    correct anchor.
"""

def get_wiki_professions(Anchor_Name):

  if len(wiki.search(Anchor_Name)) == 0:
    return NONE

  Store_name = Read_Namebasics.primaryName.tolist()

  try:
    if Anchor_Name in Store_name:
      anchor_content = (((wiki.page(Anchor_Name,auto_suggest=False)).content).replace(' and ',', ').replace('.','. '))
    else:
      return NONE

  except wiki.exceptions.PageError as p:
    if PAGE_ERROR in str(p):
      return NONE

  except wiki.exceptions.DisambiguationError as e:
    if Anchor_Name in Store_name:
      list_disambiguation_error = e.options

      for i in list_disambiguation_error:
        if i.find(DISAMBIGUATION_ERROR)!= -1:
          return NONE

        anchor_content = (((wiki.page(i,auto_suggest=False)).content).replace(' and ',', ').replace('.','. '))
        split_type_list = []
        prefix = None
        for x in SPLIT_TYPES:
          if x in anchor_content:
            prefix = x
            split_type_index = anchor_content.find(prefix)
            split_type_list.append(split_type_index)
          else:
            split_type_list.append(1000)

        if prefix is None:
          return NONE

        low_index = SPLIT_TYPES[split_type_list.index(min(split_type_list))]
        final_split = anchor_content.split(low_index)[1].split('. ')[0].split(', ')
        final_professions = [anchor_content.lower() for anchor_content in final_split]
        required_professions = PROFESSION_TYPES

        check_individual_prof = []
        for x in required_professions:
          final_check= [(x in y) for y in final_professions]
          check_individual_prof.append(any(final_check))

        if (any(check_individual_prof)==True):
          return final_professions
      else:
        return NONE

    else:
      return NONE

  split_type_list = []
  prefix = None
  for x in SPLIT_TYPES:
    if x in anchor_content:
      prefix = x
      split_type_index = anchor_content.find(prefix)
      split_type_list.append(split_type_index)
    else:
      split_type_list.append(1000)

  low_index = SPLIT_TYPES[split_type_list.index(min(split_type_list))]

  if prefix is None:
    return NONE

  final_split = anchor_content.split(low_index)[1].split('. ')[0].split(', ')
  final_professions = [anchor_content.lower() for anchor_content in final_split]
  return final_professions

def check_wiki_professions(list_profession):
  required_professions = PROFESSION_TYPES

  check_individual_prof = []
  for x in required_professions:
    final_check= [(x in y) for y in list_profession]
    check_individual_prof.append(any(final_check))

  if (any(check_individual_prof)==True):
    return True
  else:
    return False

"""  From top 5 anchor we get most probable anchor in that show
     Make sure that the input anchors list follows given format. Here's an example
     Anchor_list = "Colleen-Williams_Ashwini-Bhave_Elaine-Quijano_Becky-Hobbs_Heidi-Collins"
"""
def get_correct_anchor(top_5):
  top_5_list = (top_5.replace('-',' ').replace('_',',')).split(',')
  top_5_list = list(filter(None, top_5_list))

  for x in top_5_list:
    if check_wiki_professions(get_wiki_professions(x)) is True:
      Read_Namebasics = pd.read_table(NAME_BASICS, sep='\t')
      Store_name = Read_Namebasics.primaryName.tolist()
      if x in Store_name:
        return x

  return ''

## get finals anchors from the multiple probable hosts

def Final_Anchor(last_line):
  filter_probable_list = []

  ## Here all_lines[len(all_lines)-1] == put from last year code

  last_line_anchors = last_line.split('ANC|')[1].split('\n')[0].replace(' ','')

  # print(last_line)
  avoid_num = ''.join([i for i in last_line_anchors if not i.isdigit()])
  probable_host_list = avoid_num.split('probable_host:')
  probable_host_list = list(filter(None, probable_host_list))

  # print(probable_host_list)

  for i in probable_host_list:
    filter_probable_list.append(get_correct_anchor(i))
    filter_probable_list = list(filter(None, filter_probable_list))
  return filter_probable_list

#this will give us network name either from anchor or show

def get_channel(name):
  try:
    anchor_content = ((((wiki.page(name,auto_suggest=False)).content).replace(' and ',', ').replace('.','. ')))

  except wiki.exceptions.PageError as p:
    if PAGE_ERROR in str(p):
      return NONE

  except wiki.exceptions.DisambiguationError as e:
    list_disambiguation_error = e.options
    for anchor_name in list_disambiguation_error:
      if anchor_name.find(DISAMBIGUATION_ERROR)!= -1:
        return NONE

      else:
        anchor_content = ((((wiki.page(anchor_name,auto_suggest=False)).content).replace(' and ',', ').replace('.','. ')))

  Networks_list = NETWORK_TYPES
  Network_filter = []

  for network in Networks_list:
    if anchor_content.find(network)!=-1:
      Network_filter.append(network)
  return Network_filter

def get_majority_network(anchors_list):
  list_of_network = []
  for anchorlist in anchors_list:
    for anchor in anchorlist:
      list_of_network.append(get_channel(anchor))
  list_of_network = list(filter(None,list_of_network))
  # print(list_of_network)

  X ={} #dict contains networks and respected votes
  for network in list_of_network:
    for particular in network:
      if particular in X:
        X[particular] +=1
      else:
        X[particular] = 1
  Max_votes = max(X.values())
  Majority_Network = [particular for particular, votes in X.items() if votes == Max_votes]
  return Majority_Network


#!/usr/bin/env python3
import datetime

import argparse
from pathlib import Path
import json
import random
import string
import importlib

from botocore.exceptions import ClientError

target = ''

technique_info = {
    'blackbot_id': 'T1530',
    'external_id': '',
    'controller': 'rds_explore_snapshots_cleanup',
    'services': ['RDS'],
    'prerequisite_modules': [],
    'external_dependencies': [],
    'arguments_to_autocomplete': ['--regions'],
    'version': '1',
    'aws_namespaces': [],
    'last_updated_by': 'Blackbot, Inc. Sun Sep 20 04:13:33 UTC 2020' ,
    'ttp_exec': '',
    'ttp_mitigation': '',
    'ttp_detection': '',
    'intent': 'Creates copies of running RDS databases to access protected information',
    'name': 'ADD_NAME_HERE',
}

parser = argparse.ArgumentParser(add_help=False, description=technique_info['name'])
parser.add_argument('--regions', required=False, default=None, help='One or more (comma separated) AWS regions in the format us-east-1. Defaults to all session regions.')

def main(args, awsattack_main):
    args = parser.parse_args(args)

    import_path = 'ttp.src.rds_explore_snapshots_cleanup_src'
    src_code = __import__(import_path, globals(), locals(), ['technique_info'], 0)
    importlib.reload(src_code)

    awsattack_main.chain = True
    return src_code.main(args, awsattack_main)

def summary(data, awsattack_main):
    if 'cleanup' in data:
        out = data['cleanup'] + '\n'
    return out


import scipy.stats as stats
import matplotlib.pyplot as plt
import numpy as np
import random as rr
plt.style.use('seaborn-darkgrid')
#%config InlineBackend.figure_format = 'svg'

snr = 4

mu = 0
sigma = np.sqrt(9/10**(snr/10)/2)

height_distr = stats.norm(loc=mu, scale=sigma)
r = np.arange(-10, 10, 0.001)
s = np.array([rr.randint(0,1)*2-1 for i in range(r.shape[0])])*3

spread = np.random.standard_normal(r.shape)
print(height_distr.pdf(0))

gamma = 10**(snr/10) #SNR to linear scale
if s.ndim==1:# if s is single dimensional vector
    P=1*sum(abs(s)**2)/len(s) #Actual power in the vector
else: # multi-dimensional signals like MFSK
    P=1*sum(sum(abs(s)**2))/len(s) # if s is a matrix [MxN]
print(P)
N0=P/gamma # Find the noise spectral density
n = np.sqrt(N0/2)*np.random.standard_normal(s.shape) # computed noise
t = s + n # received signal
sigma= np.sqrt(N0/2)
height_distr2 = stats.norm(loc=mu, scale=sigma)

plt.plot(r, height_distr.pdf(r), color="r");
plt.plot(r, height_distr2.pdf(r), color="g");
plt.hist(n, bins=50, density=True, color="b");
plt.show()


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

from argparse import ArgumentParser
from os import path, remove
import gzip
import shutil

"""
Versionsnummer hochzählen für UPDATE
"""
__author__ = 'Dirk Marciniak'
__copyright__ = 'Copyright 2020'
__license__ = 'GPL'
__version__ = '0.2'

"""
mache config.html zu config.hex
"""

OUTPATH = "../include/"
LENSTR = "const uint32_t CONFIG_PAGE_SIZE = {};"
CONTENTSTR_START = "uint8_t CONFIG_PAGE_CONTENT[] PROGMEM = { "
CONTENTSTR_END = " };"

def encode_to_hexdump(file):
    infile = file + ".html"
    outfile = OUTPATH + file + ".hex"
    countfile = OUTPATH + file + ".inc"
    columnCounter = 0
    compressed_file = 'temp_' + file + '.gz'
    print("encode ascii source file <{0}> to hexdump file <{1}>".format(
        infile, outfile))
    #
    # erst komprimieren
    #
    try:
        with open(infile, 'rb') as f_in:
            with gzip.open(compressed_file, 'wb') as f_out:
                shutil.copyfileobj(f_in, f_out)
    except IOError as e:
        print("can't compress infile. abort : {}".format(e))
        return (-1)
    #
    # jetzt hexdump erzeugen
    #
    try:
        print("open compressed infile {}...".format(compressed_file))
        i_file = open(compressed_file, 'rb')
    except IOError as e:
        print("can't open infile. abort : {}".format(e))
        return (-1)
    try:
        print("open hexdump outfile {}...".format(outfile))
        o_file = open(outfile, mode='w', newline='\n')
        o_file.write(CONTENTSTR_START + "\n")
    except IOError as e:
        i_file.close()
        print("can't open hexdump outfile. abort : {}".format(e))
        remove(compressed_file)
        return (-1)
    rbyte = i_file.read(1)
    while rbyte:
        #
        # wandeln nach code
        #
        num = int.from_bytes(rbyte, 'little', signed=False)
        ascii_str = "{0:d}".format(num)
        # print("char: {0}".format(ascii_str))
        #
        # versuchen das nächste Byte zu holen
        #
        rbyte = i_file.read(1)
        if rbyte:
            ascii_str += ","
        o_file.write(ascii_str)
        # wie viel schreibe ich rein
        # spalten zählen
        columnCounter += 1
        if columnCounter >= 29:
            columnCounter = 0
            o_file.write("\n")
    o_file.write(CONTENTSTR_END)
    o_file.close()
    i_file.close()
    file_size = path.getsize(compressed_file)
    #
    # date mit der Länge schreiben
    #
    try:
        print("open length outfile {}...".format(countfile))
        o_file = open(countfile, mode='w', newline='\n')
        o_file.write(LENSTR.format(file_size))
    except IOError as e:
        i_file.close()
        print("can't open length outfile. abort : {}".format(e))
        remove(compressed_file)
        return (-1)
    remove(compressed_file)
    print("compressed file size: {} bytes".format(file_size))

def main():
    infile = None
    outfile = None
    #
    # parse argumente
    #
    parser = ArgumentParser(fromfile_prefix_chars='@',
                            description='encode html to hex for c++ program',
                            epilog="from Dirk Marciniak\n")
    parser.add_argument("--infile", type=str, help="input file without .html")
    #
    args = parser.parse_args()
    #
    # eingabedatei
    #
    if args.infile:
        infile = args.infile
        if path.isfile(infile + ".html"):
            print("infile is a file and his name is {}".format(infile + ".html"))
        else:
            print('infile {} is not exist. abort.'.format(infile + ".html"))
            exit(-1)
    else:
        print("there is no infile, abort!")
        exit(-1)
    encode_to_hexdump(infile)

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    main()


from itertools import combinations
N, K = map(int, input().split())
words = []
all = []
for _ in range(N):
    tmp = set(input()[4:-4])
    words.append(tmp)
    all += tmp
all = set(all)

if K < 5:
    print(0)
else:
    visited = 0
    for i in ['a', 'c', 'i', 't', 'n']:
        index = ord(i)-97
        visited = visited | (1 << index)  # 체크해줌
    K = K-5
    coms = list(combinations(all, K))
    get = 0
    for com in coms:
        tmpvisit = visited
        for ch in com:
            if (tmpvisit & (1 << ord(ch)-97)) == 0:  # 존재하지 않으면
                tmpvisit = tmpvisit | (1 << ord(ch)-97)
        # 이제 단어 비교
        total = 0
        for word in words:
            length = len(word)
            for ch in word:
                if tmpvisit & (1 << ord(ch)-97):  # 존재하면 길이 줄이기
                    length -= 1
            if length == 0:  # 전부 존재함
                total += 1
        get = max(get, total)
    print(get)

2021/13주차 부르트포스,비트마스킹,위클리/백준/가르침/김재환.py


import copy
import math
import pandas as pd
from framework.utilities.datadescriptor import DataDescriptorMetadata, DataDescriptorBase, DataDescriptorTimeSeries
from itertools import chain
from framework.similarity.similaritylist import SimilarityList
from framework.utilities.outputgroupper import DataDescriptorTerms
import itertools
import pdb
import numpy as np
import pickle

class Resampler:
    def __init__(self):
        self.meta_data = []
        self.row_meta_data = pd.DataFrame()

    def resample_data_into_blocks(self,data,data_descriptors,similarities_list, min_resample_factor):
        output_data = pd.DataFrame()

        index = 0
        for data_descriptor in data_descriptors:
            if isinstance(data_descriptor, DataDescriptorMetadata):
                self.meta_data.append(data_descriptor)
                meta_data_rows = data.iloc[:, data_descriptor.data_start_index:data_descriptor.data_end_index+1]
                temp_index = (data_descriptor.data_end_index-data_descriptor.data_start_index) + index
                data_descriptor.data_start_index = index
                data_descriptor.data_end_index = temp_index
                index += temp_index + 1
                meta_data_rows.columns = [range(data_descriptor.data_start_index,data_descriptor.data_end_index+1)]
                self.row_meta_data = pd.concat([self.row_meta_data,meta_data_rows], axis=1)
        #Clear framework settings
        _data_descriptors = []
        _similarities_list = SimilarityList()
        index = 0

        for similarity in similarities_list.similarities:
            data_descriptor = similarity.data_descriptor
            if isinstance(data_descriptor, DataDescriptorTimeSeries):
                data_length = (data_descriptor.data_end_index - data_descriptor.data_start_index)+1
                resample_factor = math.floor(similarity.data_descriptor.output_frequency.value / data_descriptor.sampling_frequency.value)
                if resample_factor < min_resample_factor:
                    resample_factor = min_resample_factor
                amount_of_resamples = math.floor(data_length / resample_factor)
                start_index = data_descriptor.data_start_index
                data_descriptor.data_start_index = index
                index = index + resample_factor-1
                data_descriptor.data_end_index = index
                _similarities_list.add_similarity(similarity)
                _data_descriptors.append(data_descriptor)
                for i in range(0,amount_of_resamples):
                    loop_start_index = start_index+(i*resample_factor)
                    loop_end_index = start_index+((i+1)*resample_factor)
                    new_data = data.iloc[:, loop_start_index:loop_end_index]
                    new_data.columns = list(range(data_descriptor.data_start_index,data_descriptor.data_end_index+1))
                    row_data = pd.concat([new_data], axis=1)
                    output_data = output_data.append(row_data, ignore_index=True)
        return output_data, _similarities_list, _data_descriptors, resample_factor

    def create_timeserices_from_slices_of_data(self, transformed_data,similarities_list, amount_of_sensors):
        output_data = pd.DataFrame()
        transformed_data = transformed_data.sort_index()
        _similarities_list = SimilarityList()
        _data_descriptors = []
        index = 0
        for meta_dd in self.meta_data:
            _data_descriptors.append(meta_dd)
            index = meta_dd.data_end_index + 1

        amount_of_inputs = len(transformed_data.index)

        time_series_lenged = math.floor(amount_of_inputs / amount_of_sensors)*len(transformed_data.columns)

        for similarity in similarities_list.similarities:
            similarity.data_descriptor.data_start_index = index
            similarity.data_descriptor.data_end_index = index + time_series_lenged -1
            _similarities_list.add_similarity(similarity)
            _data_descriptors.append(similarity.data_descriptor)

        for i in range(0,amount_of_sensors):
            # import pdb; pdb.set_trace()
            new_data = transformed_data.iloc[i::amount_of_sensors,:]._values
            new_data = list(chain.from_iterable(new_data))
            new_data = pd.DataFrame.from_items([(i, new_data)],orient='index', columns=list(range(index,index+len(new_data))))
            row_data = pd.concat([self.row_meta_data,new_data], axis=1)
            output_data = output_data.append(row_data.iloc[i])
        return output_data, _similarities_list, _data_descriptors

class Subsampling:
    def __init__(self,data):
        self.data = data # sanitized database to be sampled from
        self.pair_index_all = list(itertools.combinations(self.data.index,2))
        self.pairsize_total = len(self.pair_index_all)
        self.size_total = len(self.data)
        self.pairdata_labeled = []
        self.label = []
        self.k_already = 0
        self.subsample_status = np.zeros(self.pairsize_total)
        # self.dist_pairdata_us = []
        # self.dist_pairdata_s = []

    def reset(self):
        self.pairdata_labeled = []
        self.label = []
        self.k_already = 0
        self.subsample_status = np.zeros(self.pairsize_total)
        # self.dist_pairdata_us = []
        # self.dist_pairdata_s = []

    def get_pairdata(self,pair_subsample_index):
        pairdata = [(self.data.loc[ind1], self.data.loc[ind2]) for ind1, ind2 in pair_subsample_index]
        return pairdata

    def uniform_sampling(self,subsample_size,seed = None):
        if seed is not None:
            np.random.seed(seed)
        pair_subsample_i = np.random.choice(a=self.pairsize_total,size=subsample_size,replace=False)
        pair_subsample_index = [self.pair_index_all[i] for i in pair_subsample_i]
        pair_subsample_index.sort(key=lambda x: x[1])
        pair_subsample_index.sort(key=lambda x: x[0])
        pair_subsample = self.get_pairdata(pair_subsample_index=pair_subsample_index)
        return pair_subsample, pair_subsample_index

    def batch_uniform_sampled(self,batch_size):
        unsample_idx = [i for i in range(len(self.subsample_status)) if self.subsample_status[i] == 0]
        pair_subsample_i = np.random.choice(a=len(unsample_idx), size=batch_size, replace=False)
        next_sample_idx = [unsample_idx[i] for i in pair_subsample_i]
        pair_subsample_index = [self.pair_index_all[j] for j in next_sample_idx]
        pair_subsample = self.get_pairdata(pair_subsample_index=pair_subsample_index)
        for i in pair_subsample_i:
            self.subsample_status[unsample_idx[i]] = 1
        return pair_subsample,next_sample_idx

    def get_range_n_clusters(self,datasize,max_allowed):
        max_n_clusters = int(np.floor(datasize / 2))
        if max_n_clusters <= 2:
            range_n_clusters = [2]
        elif max_n_clusters >= max_allowed:
            range_n_clusters = range(2, max_allowed)
        else:
            range_n_clusters = range(2, max_n_clusters)
        return range_n_clusters

    def active_sampling(self,dist_metric,k_init,batch_size,seed=None):
        # at the beginning, uniformly randomly sample k_init samples for manual labeling
        if self.k_already == 0:
            np.random.seed(seed)
            next_sample_idx = np.random.choice(a=self.pairsize_total, size=k_init, replace=False).tolist()
            pairdata_idx = [self.pair_index_all[i] for i in next_sample_idx]
            pairdata_idx.sort(key=lambda x: x[1])
            pairdata_idx.sort(key=lambda x: x[0])
            self.k_already = k_init
        else:
            unsample_idx = [i for i in range(len(self.subsample_status)) if self.subsample_status[i] == 0]
            uncertainty_vec = []
            for i in unsample_idx:
                pair_index = self.pair_index_all[i]
                pair = self.get_pairdata(pair_subsample_index=[pair_index])
                uncertainty = self.get_uncertainty_entropy_deep(pair=pair[0], dist_metric=dist_metric, mu=0)
                # uncertainty = self.get_uncertainty_probability_difference(pair=pair[0], dist_metric=dist_metric, mu=0)
                uncertainty_vec.append(uncertainty)
            # pdb.set_trace()
            if len(unsample_idx) < batch_size:
                batch_size = len(unsample_idx)
            start = 0
            most_uncertain_idx = np.argsort(uncertainty_vec)[::-1][start:start + batch_size]
            next_sample_idx = [unsample_idx[i] for i in most_uncertain_idx]
            pairdata_idx = [self.pair_index_all[i] for i in next_sample_idx]
            self.k_already = self.k_already + batch_size

        for i in next_sample_idx:
            self.subsample_status[i] = 1
        pairdata = self.get_pairdata(pair_subsample_index=pairdata_idx)
        return pairdata, pairdata_idx

    def get_uncertainty_entropy_deep(self, pair, dist_metric, mu):
        dist = dist_metric.transform(pair)
        # pdb.set_trace()
        if not np.isscalar(dist):
            dist = dist[0, 0]
        prob_s = 1 / (1 + np.exp(dist - mu))
        prob_us = 1 / (1 + np.exp(-(dist - mu)))
        entropy = -prob_s * np.log(prob_s) - prob_us * np.log(prob_us)
        return entropy

    def get_uncertainty_entropy(self,pair,dist_metric,mu):
        x_diff = (pair[0] - pair[1]).values
        dist = x_diff.dot(dist_metric).dot(x_diff)
        # pdb.set_trace()]
        if not np.isscalar(dist):
            dist = dist[0,0]
        prob_s = 1/(1+np.exp(dist-mu))
        prob_us = 1/(1+np.exp(-(dist-mu)))
        entropy = -prob_s*np.log(prob_s) - prob_us*np.log(prob_us)
        return entropy

    def get_uncertainty_probability_difference(self,pair,dist_metric,mu):
        x_diff = (pair[0] - pair[1]).values
        dist = x_diff.dot(dist_metric).dot(x_diff)
        # pdb.set_trace()
        if not np.isscalar(dist):
            dist = dist[0,0]
        prob_s = 1/(1+np.exp(dist-mu))
        prob_us = 1/(1+np.exp(-(dist-mu)))
        uncertainty = np.abs(prob_s-prob_us)
        return uncertainty


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 11 23:02:57 2019

Electricity consumption for buildings

@author: alirezagoudarzi
"""
import datetime
import pandas as pd
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt
import numpy as np

data = pd.read_csv('../data/buildings-2018-3.csv')

def getTime(x):
    m=int(x.split(' ')[0].split('/')[0])

    h=int(x.split(' ')[2].split(':')[0])
    if x.split(' ')[3] == 'PM':
        h+=12
    return m,h

energy = data.iloc[:,1].tolist();
times = np.array(data.iloc[:,0].tolist());

days = [ datetime.datetime.strptime(d.split('T')[0],'%Y-%m-%d').day for d in times]
month = [ datetime.datetime.strptime(d.split('T')[0],'%Y-%m-%d').month for d in times]

#clean the NAN
for i in range(len(energy)):
    if np.isnan(energy[i]):
        energy[i] = (energy[i-1] + energy[i+1])/2

#mon_hour = [ [getTime(x)[0],getTime(x)[1]] for x in times ]
winhours=48
inthours=2
plt.plot(energy[0:winhours])
plt.xticks(range(0,len(times[0:winhours]),inthours),times[(range(0,len(times[0:winhours]),inthours))],rotation='vertical')
plt.grid('on')
plt.show()

winL = 4

movAvg = []
for i in range(0,len(energy),winL):
    movAvg.append(sum(energy[i:i+winL])/winL)

plt.plot(movAvg[0:100])


"""empty message

Revision ID: c47235fff6f4
Revises: 954967dc4cde
Create Date: 2019-09-17 18:54:20.131023

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'c47235fff6f4'
down_revision = '954967dc4cde'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('regtb', 'userid')
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('regtb', sa.Column('userid', sa.VARCHAR(length=200), nullable=True))
    # ### end Alembic commands ###