#Check-if-Palindrome
print("### Check if Palindrome ###")
print("type a word or sentence to see if its a palidrome")

def isPalindrome(arg):
    if arg == arg[::-1]:
        return True
    else:
        return False

while True:
    user = input("submit a palindrome: ")
    if isPalindrome(user):
        print("this is a palindrome")
        break
    else:
        print("this isnt a palindrome, keep trying!")
input("type anything to exit")


# -*- coding: utf-8 -*-
# pylint: disable=invalid-name
# pylint: disable=missing-docstring
# pylint: disable=too-many-public-methods

from __future__ import unicode_literals

import os.path
import sys

from weechat_otr_test.weechat_otr_test_case import WeechatOtrTestCase

import weechat_otr

class FingerprintTestCase(WeechatOtrTestCase):

    def test_fingerprint(self):
        account1 = weechat_otr.ACCOUNTS['nick@server']
        account1.getPrivkey()

        account2 = weechat_otr.ACCOUNTS['nick2@server2']
        account2.getPrivkey()

        weechat_otr.command_cb(None, None, 'fingerprint')

        self.assertPrinted('', (
            'eval(${{color:default}}:! ${{color:brown}}otr${{color:default}}'
            ' !:)\t'
            '(color default)nick2@server2 |{fp2}\r\n'
            '(color default)nick@server   |{fp1}').format(
                fp1=account1.getPrivkey(),
                fp2=account2.getPrivkey()))

    def test_fingerprint_pattern(self):
        fpr_path1 = os.path.join(
            sys.modules['weechat'].weechat_dir,
            'otr',
            'nick@server.fpr')
        with open(fpr_path1, 'w') as f:
            for fields in [
                    ['notamachxxx@server', 'nick@server', 'irc', 'fp123', ''],
                    ['matchxxxxxx@server', 'nick@server', 'irc', 'fp123', ''],
                    ['beforematch@server', 'nick@server', 'irc', 'fp123', ''],
                    ['before@servermatch', 'nick@server', 'irc', 'fp123', ''],
                ]:
                f.write("\t".join(fields))
                f.write("\n")

        account1 = weechat_otr.ACCOUNTS['nick@server']
        account1.getPrivkey()

        weechat_otr.command_cb(None, None, 'fingerprint match')

        self.assertNoPrintedContains('', 'notamachxxx@server')

        self.assertPrinted('', (
            'eval(${color:default}:! ${color:brown}otr${color:default}'
            ' !:)\t'
            '(color default)before@servermatch |nick@server |F P 1 2 3 |'
            'unverified\r\n'
            '(color default)beforematch@server |nick@server |F P 1 2 3 |'
            'unverified\r\n'
            '(color default)matchxxxxxx@server |nick@server |F P 1 2 3 |'
            'unverified'))

    def test_fingerprint_all(self):
        fpr_path1 = os.path.join(
            sys.modules['weechat'].weechat_dir,
            'otr',
            'nick@server.fpr')
        with open(fpr_path1, 'w') as f:
            for fields in [
                    ['peer1@server', 'nick@server', 'irc', 'fp111', ''],
                    ['peer2@server', 'nick@server', 'irc', 'fp222', 'smp'],
                    ['peer3@server', 'nick@server', 'irc', 'fp333', 'verified'],
                ]:
                f.write("\t".join(fields))
                f.write("\n")

        account1 = weechat_otr.ACCOUNTS['nick@server']
        account1.getPrivkey()

        fpr_path2 = os.path.join(
            sys.modules['weechat'].weechat_dir,
            'otr',
            'nick2@server2.fpr')
        with open(fpr_path2, 'w') as f:
            for fields in [
                    ['peer4@server2', 'nick2@server2', 'irc', 'fp444',
                     'verified'],
                ]:
                f.write("\t".join(fields))
                f.write("\n")

        account2 = weechat_otr.ACCOUNTS['nick2@server2']
        account2.getPrivkey()

        weechat_otr.command_cb(None, None, 'fingerprint all')

        self.assertPrinted('', (
            'eval(${color:default}:! ${color:brown}otr${color:default}'
            ' !:)\t'
            '(color default)peer4@server2 |nick2@server2 |F P 4 4 4 |'
            'verified    \r\n'
            '(color default)peer1@server  |nick@server   |F P 1 1 1 |'
            'unverified  \r\n'
            '(color default)peer2@server  |nick@server   |F P 2 2 2 |'
            'SMP verified\r\n'
            '(color default)peer3@server  |nick@server   |F P 3 3 3 |'
            'verified    '))


#!/usr/bin/env python
"""

"""

from elem import Elem

__author__ = "Bryan Alexis Freire Viteri"
__version__ = "3.0"
__email__ = "bryanfv95@gmail.com"

class Edge(Elem):
    def __init__(self,_id,frm,to):
        # Set the basic attributes of an edge
        attr         = dict()
        attr['from'] = frm.id
        attr['to']   = to.id
#        attr['sidewalkWidth'] = 6
        super(Edge,self).__init__('edge',_id,attr)
        # Adds 'to' as neighbor from 'frm'
        frm.connections[to.id] = frm.get_weight(to)


class Account():
    def __init__(self,owner,balance):
        self.owner=owner
        self.balance=balance
    def deposit(self,amount):

        self.balance +=amount
        return "{} Amount Credited Successfully".format(self.balance)
    def withdraw(self,amount):
        if(self.balance>amount):
            self.balance=self.balance-amount
            return "{} Amount Debited Successfully, Available Balance {}:".format(amount,self.balance)
        else:
            return "Insufficient Balance:"
i_account=Account("Soumya",0)
i=1
while i==1:
    t=input("d-Deposit w-Withdraw")
    if t=='d':
        amount=int(input("Deposit:"))
        print (i_account.deposit(amount))
    elif t=='w':
        amount=int(input("Withdraw:"))
        print (i_account.withdraw(amount))
    i=int(input("continue-1 exit-0"))


import scrapy

class QuotesSpider(scrapy.Spider):
    name = "0.scrapy_at_a_glance"
    start_urls = [
        "https://quotes.toscrape.com/tag/humor/",
    ]

    # custom_settings = {
    #     'ITEM_PIPELINES': {
    #         'my_project.pipelines.QuotesMongoPipeline': 300,
    #     }
    # }

    def parse(self, response):
        for quote in response.css("div.quote"):
            yield {
                "author": quote.xpath("span/small/text()").get(),
                "text": quote.css("span.text::text").get(),
            }

        next_page = response.css('li.next a::attr("href")').get()
        if next_page is not None:
            next_page = response.urljoin(next_page)
            yield scrapy.Request(next_page, callback=self.parse)

            # yield response.follow(next_page, self.parse)


from unittest import TestCase
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import json
from positionSynthesis import positionJsonDataParser, positionDataToPandasDf, positionDataPreproc, augFeatVecToPos, rollingWinSize, augmentationRatio

'''
將position資料以3D的型態繪製, 方便debug
'''

DBFullJointfilePath = './positionData/fromDB/leftFrontKickPositionFullJointsWithHead.json'
AfterSynthesisFullJointfilePath = './positionData/afterSynthesis/leftFrontKick_EWMA.json'
DBFullJointCount = 17
afterSynthesisFullJointCount = 16

def readInSynthesis3DPos(filePath, jointCount: int, fromUnity: bool=False):
    '''
    讀取存儲在json格式檔案當中的3d position資訊,
    並且轉換成numpy array格式
    '''
    joints3dPosArr = [None for i in range(jointCount)]
    with open(filePath, 'r') as fileIn:
        jsonData=json.load(fileIn)
        if fromUnity:
            jsonData = jsonData['results']
        for i in range(jointCount):
            tmpArr = np.zeros((len(jsonData), 3))
            for t in range(len(jsonData)):
                tmpArr[t, 0] = jsonData[t]['data'][i]['x']
                tmpArr[t, 1] = jsonData[t]['data'][i]['y']
                tmpArr[t, 2] = jsonData[t]['data'][i]['z']
            joints3dPosArr[i] = tmpArr
    return joints3dPosArr

def readInDBPosByOldWay(DBFilePath, jointCount: int):
    '''
    使用positionSynthesis.py當中的方法讀取DB的positions
    '''
    posDBFullJointsDf = None
    with open(DBFilePath, 'r') as fileIn:
        jsonStr=json.load(fileIn)
        positionsDB = positionJsonDataParser(jsonStr, jointCount)
        posDBFullJointsDf = positionDataToPandasDf(positionsDB, jointCount)
    print(posDBFullJointsDf.shape)
    DBFullJointsPreproc = positionDataPreproc(posDBFullJointsDf, jointCount, rollingWinSize, True, augmentationRatio)
    DBFullJointsPosNoAug = [augFeatVecToPos(i.values, rollingWinSize) for i in DBFullJointsPreproc]
    return DBFullJointsPosNoAug

def DBPosSetHipOrigin(DBPosArrs):
    '''
    設定DB(from)Unity的position以Hip的座標為中心,
    假設Hip的index是6

    Input:
    :DBPosArrs: 多個joints的position array構成的list
    '''
    for i in range(len(DBPosArrs)):
        if i != 6:
            DBPosArrs[i] = DBPosArrs[i] - DBPosArrs[6]
    DBPosArrs[6] = DBPosArrs[6] - DBPosArrs[6]
    return DBPosArrs

def findJointsAxesMinMax(PosArrs):
    '''
    尋找每個joint's axes的position最大最小值

    Input:
    :PosArrs: 多個joints的position array構成的list
    '''
    minmaxInJoints = [[None, None] for i in range(len(PosArrs))]
    for i in range(len(PosArrs)):
        minmaxInJoints[i][0] = PosArrs[i].max(axis=0)
        minmaxInJoints[i][1] = PosArrs[i].min(axis=0)
    return minmaxInJoints

def plot3D(positions3d, label: str='', isNewFigure:bool=False):
    '''
    繪製3D positions的trajectory

    Input:
    :positions3d: np.array, 維度為(time point, 3)
    '''
    fig=None
    ax=None
    if isNewFigure:
        fig= plt.figure()
        ax = fig.add_subplot(projection='3d')
    plt.plot(positions3d[:, 0], positions3d[:, 1], positions3d[:, 2], '.-', label=label)

# 使用測資測試原本的preprocessing正不正常
if __name__=='__main01__':
    fullJointsCount = 7
    timePointCount = 50
    axesCategory = ['x', 'y', 'z']
    DataInColumns={}
    for i in range(fullJointsCount):
        for k_i, k in enumerate(axesCategory):
            if i == 6:
                DataInColumns['{0}_{1}'.format(i, k)] = [0 for a in range(timePointCount)]
                continue
            DataInColumns['{0}_{1}'.format(i, k)] = [a+i+k_i for a in range(timePointCount)]
    testDataDf = pd.DataFrame(DataInColumns)
    preprocTestData = positionDataPreproc(testDataDf, fullJointsCount, rollingWinSize, True, augmentationRatio)
    preprocTestDataNoAug = positionDataPreproc(testDataDf, fullJointsCount, rollingWinSize, False, augmentationRatio)
    # print(preprocTestData[0].shape)
    # print(testDataDf.iloc[:50, :3])
    # print(preprocTestData[0].iloc[:10, :10])
    # print(preprocTestDataNoAug[0].iloc[:10, :10])

if __name__=='__main__':
    DBPos3dArrs = readInSynthesis3DPos(DBFullJointfilePath, DBFullJointCount, True)
    DBPos3dArrs = DBPosSetHipOrigin(DBPos3dArrs)
    AugDBPos = readInDBPosByOldWay(DBFullJointfilePath, DBFullJointCount)
    AfterSynthesisPos3dArrs = readInSynthesis3DPos(AfterSynthesisFullJointfilePath, afterSynthesisFullJointCount)

    # print(AugDBPos[0].shape)
    # print(DBPos3dArrs[0].shape)
    # print(AfterSynthesisPos3dArrs[0].shape)
    plot3D(AfterSynthesisPos3dArrs[2], 'afterSyn', True)
    plot3D(DBPos3dArrs[2], 'DB', False)
    plot3D(AugDBPos[2], 'DBAfterAug', False)

    # Find min and max in each joint/axes
    DBPosMinMax = findJointsAxesMinMax(DBPos3dArrs)
    AfterSynthesisMinMax = findJointsAxesMinMax(AfterSynthesisPos3dArrs)
    for i in range(len(AfterSynthesisPos3dArrs)):
        if i >= 6:
            # print('Max: ', DBPosMinMax[i+1][0], ', ', AfterSynthesisMinMax[i][0])
            # print('Min: ', DBPosMinMax[i+1][1], ', ', AfterSynthesisMinMax[i][1])
            continue
        # print('Max: ', DBPosMinMax[i][0], ', ', AfterSynthesisMinMax[i][0])
        # print('Min: ', DBPosMinMax[i][1], ', ', AfterSynthesisMinMax[i][1])

    plt.legend()
    plt.show()


from django import forms,admin
from .models import Product

class ProductAdminForm(forms.ModelForm):
    category = forms.ChoiceField(choices=Product.CATEGORY_CHOICES)

    class Meta:
        model = Product
        fields = '__all__'

class ProductAdmin(admin.ModelAdmin):
    form = ProductAdminForm
    list_display = ('name', 'category', 'price', 'is_available')


from collections.abc import Iterable

from django import template
from django.core.paginator import Paginator

register = template.Library()

@register.simple_tag
def get_updated_elided_page_range(
    paginator: Paginator, number: int, on_each_side: int = 3, on_ends: int = 2
) -> Iterable:
    """Get an updated list of elided pages based on the passed in page number

    Gets a new list of pages, including the ellipsis, in the pagination navbar whenever
    the user goes to a new page.

    Args:
        paginator: The Paginator for the paginated view
        number: An integer of the current page number
        on_each_side: An integer for how many pages should be displayed
            on both sides of the current page
        on_ends: An integer for how many pages should be displayed
            on the end of the page list, past the ellipsis

    Returns:
        new_paginator: An iterable of the new list of elided pages
    """
    new_paginator = Paginator(paginator.object_list, paginator.per_page)
    return new_paginator.get_elided_page_range(
        number=number, on_each_side=on_each_side, on_ends=on_ends
    )


import numpy as np
import pandas as pd
import tensorflow as tf
import random
import math

from tensorflow_privacy.privacy.analysis.rdp_accountant import compute_rdp
from tensorflow_privacy.privacy.analysis.rdp_accountant import get_privacy_spent
from tensorflow_privacy.privacy.optimizers.dp_optimizer_keras import DPKerasSGDOptimizer

def train(
    noise_multiplier,
    l2_norm_clip,
    batch_size,
    microbatches,
    x_train,
    y_train,
    x_valid,
    y_valid,
    x_test,
    y_test,
    dpsgd=True,
    learning_rate=0.1,
    epochs=120,
    verbose=1,
    seed=0,
):

    random.seed(seed)
    tf.random.set_seed(seed)

    if dpsgd and batch_size % microbatches != 0:
        raise ValueError("Number of microbatches should divide evenly batch_size")

    # Define a sequential Keras model
    model = tf.keras.Sequential(
        [
            tf.keras.layers.Dense(40, input_dim=21, activation="relu"),
            tf.keras.layers.Dense(60, activation="relu"),
            tf.keras.layers.Dense(20, activation="relu"),
            tf.keras.layers.Dense(1, activation="sigmoid"),
        ]
    )

    if dpsgd:
        optimizer = DPKerasSGDOptimizer(
            l2_norm_clip=l2_norm_clip,
            noise_multiplier=noise_multiplier,
            num_microbatches=microbatches,
            learning_rate=learning_rate,
        )
        # Compute vector of per-example loss rather than its mean over a minibatch.
        loss = tf.keras.losses.BinaryCrossentropy(reduction=tf.losses.Reduction.NONE)
    else:
        optimizer = tf.keras.optimizers.SGD(learning_rate=learning_rate)
        loss = tf.keras.losses.BinaryCrossentropy()

    # Compile model with Keras
    model.compile(optimizer=optimizer, loss=loss, metrics=["accuracy"])

    # Train model with Keras
    model.fit(
        x_train,
        y_train,
        epochs=epochs,
        validation_data=(x_valid, y_valid),
        batch_size=batch_size,
        verbose=verbose,
    )

    score_train = model.evaluate(x_train, y_train, verbose=verbose)
    score_valid = model.evaluate(x_valid, y_valid, verbose=verbose)
    score_test = model.evaluate(x_test, y_test, verbose=verbose)

    weights = model.get_weights()

    # Compute the privacy budget expended.
    # // is integer division
    if dpsgd:
        training_size = x_train.shape[0]
        eps = compute_epsilon(
            epochs * training_size // batch_size,
            training_size=training_size,
            noise_multiplier=noise_multiplier,
            batch_size=batch_size,
        )
        print("For delta=0.00413223, the current epsilon is: %.2f" % eps)
    else:
        eps = "non-private SGD"
        print("Trained with vanilla non-private SGD optimizer")

    return score_train, score_valid, score_test, eps, weights

def compute_epsilon(steps, training_size, noise_multiplier, batch_size):
    # Computes epsilon value for given hyperparameters.
    #
    # Parameters required:
    #   steps: Number of steps the optimizer takes over the training data
    #          steps = FLAGS.epochs * training_size// FLAGS.batch_size
    #
    #   Noise multiplier:
    #       the amount of noise sampled and added to gradients during training
    if noise_multiplier == 0.0:
        return float("inf")

    # Delta bounds the probability of our privacy guarantee not holding.
    # rule of thumb is to set delta to less than the inverse of the training data size
    # so here, I opted for it to equal to 1.1*training size
    training_delta = 1 / (training_size * 1.1)

    # We need to define a list of orders, at which the Rényi divergence will be computed
    orders = np.linspace(
        1 + math.log(1.0 / training_delta) / 10,
        1 + math.log(1.0 / training_delta) / 1,
        num=100,
    )

    # Sampling ratio q is the probability of an individual training point being included in a minibatch
    sampling_probability = batch_size / training_size

    # compute Renyi Differential Privacy, a generalization of pure differential privacy
    # RDP is well suited to analyze DP guarantees provided by sampling followed by Gaussian noise addition,
    # which is how gradients are randomized in the TFP implementation of the DP-SGD optimizer.
    rdp = compute_rdp(
        q=sampling_probability,
        noise_multiplier=noise_multiplier,
        steps=steps,
        orders=orders,
    )

    return get_privacy_spent(orders, rdp, target_delta=training_delta)[0]


import unittest

class TestCase(unittest.TestCase):

    def setUp(self):

        import warnings # Must turn off warnings in the test function
        warnings.simplefilter("ignore")

        super().setUp()

    def test_basic_fetch(self):
        import rowgenerators as rg

        year = 2018
        release = 5
        state = 'CA'
        sl = 'tract'
        b11016 = rg.dataframe(f'census://{year}/{release}/{state}/{sl}/B11016')

        print(b11016.head())

    def test_2020_blocks(self):

        from publicdata.census.files.url_templates import tiger_url

        self.assertEqual('shape+ftp://ftp2.census.gov/geo/tiger/TIGER2019/TABBLOCK/tl_2019_44_tabblock10.zip',
                         tiger_url(2019, 'block', 'RI'))
        self.assertEqual('shape+ftp://ftp2.census.gov/geo/tiger/TIGER2020/TABBLOCK20/tl_2020_44_tabblock20.zip',
                         tiger_url(2020, 'block', 'RI'))

    def test_shell_not_linear_ring(self):

        import rowgenerators as rg

        t = rg.geoframe('censusgeo://2019/5/CA/tract')

        print(len(t))


import discord
from discord.ext import commands

class Time(commands.Cog):

    def __intit__(self, bot):

        self.bot = bot

    @discord.slash_command(description="Day/Night cycle.")
    async def time(self, ctx):
        embed = discord.Embed(
            title = "Day/Night Cycle for pokemongods",
            description= "03:00 - 09:00 Night \n 09:00 - 15:00 Day \n 15:00 - 21:00 Night \n 21:00 - 03:00 Day",
            color=discord.Colour.random(),
        )
        embed.set_footer(text="In game's English chat, type /time to know the time.")
        await ctx.respond(embed=embed)

def setup(bot):
    bot.add_cog(Time(bot))


#!python3
from stockfish import Stockfish
from termcolor import colored
import regex
# https://pypi.org/project/stockfish/

class ColorConst:
    # red, green, yellow, blue, magenta, cyan, white.
    # on_red, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white
    # bold, dark, underline, blink, reverse, concealed.
    WHITE_PIECE = "red"
    BLACK_PIECE = "blue"
    BOARD_WHITE = "on_white"
    BOARD_BLACK = "on_yellow"
    FEN_BG = None

class Player:
    BLACK = "Black"
    WHITE = "White"

def main():
    params = {
        "Debug Log File": "",
        "Contempt": 0,
        "Min Split Depth": 0,
        "Threads": 4,                   # More threads will make the engine stronger, but should be kept at less than the number of logical processors on your computer.
        "Ponder": "true",               # Let stockfish ponder the next move while the opponent is thinking
        "Hash": 2048,                   # 1024 MB for the hash table - you may want to increase/decrease this, depending on how much RAM you want to use. Should also be kept as some power of 2.
        "MultiPV": 4,                   # Output the N best lines (principal variations, PVs) when searching. Leave at 1 for best performance.
        "Skill Level": 100,             # Lower the Skill Level in order to make Stockfish play weaker (see also UCI_LimitStrength). Internally, MultiPV is enabled, and with a certain probability depending on the Skill Level a weaker move will be played.
        "Move Overhead": 0,             # Assume a time delay of x ms due to network and GUI overheads. This is useful to avoid losses on time in those cases.
        "Minimum Thinking Time": 60,
        "Slow Mover": 200,              # Lower values will make Stockfish take less time in games, higher values will make it think longer.
        "UCI_Chess960": "false",
        "UCI_LimitStrength": "false",
        "UCI_Elo": 1350
    }
    stockfish = Stockfish(
        path="/home/paul/repositories/chess/stockfish/src/stockfish",
        depth=20,
        parameters=params
    )

    finished = False        # whether to exit the program or not
    history = []            # history of board positions
    cache = {}              # cache of values for board positions
    cache["pov"] = False
    cache["eval_moves"] = False
    while (not finished):
        print("="*60)
        fen = stockfish.get_fen_position()
        if (len(history) == 0 or history[-1] != fen):
            # new board position
            history.append(fen)
        print("FEN: " + getFen(stockfish))
        finished = menu(stockfish, history, cache)

def menu(stockfish: Stockfish, history: list, cache: dict) -> bool:
    menu = ["1. Get best Moves",
            "2. Move help",
            "3. Set board (FEN)",
            "4. Flip sides",
            "5. Revert",
            "6. Evaluate Board",
            f"7. Switch POV (Current: {'b' if cache['pov'] else 'w'})",
            f"8. Evaluate Moves (Current: {'Y' if cache['eval_moves'] else 'N'})",
            "9. Exit"
            ]
    board = getBoardStr(stockfish=stockfish, flip=cache["pov"]).split("\n")
    # find the longest line in the menu
    menuMaxLineLen = 0
    for menuLine in menu:
        menuMaxLineLen = menuMaxLineLen if menuMaxLineLen >= len(menuLine) else len(menuLine)
    # adjust menu lines for neatly printing the board
    for i in range (0, len(menu)):
        menu[i] = menu[i].ljust(menuMaxLineLen)
    # matchup menu with board string and print them side by side.
    sep = (" " * 4) + "|" + (" " * 4)
    for i in range (0, len(menu) if len(menu) > len(board) else len(board)):
        # loop over all the lines. Need to check every time if the index exists
        # print menu part
        if (i < len(menu)):
            print(menu[i], end="")
        else:
            print(" " * menuMaxLineLen, end="")
        # print board part
        if (i < len(board)):
            print(sep, end="")
            print(board[i])
        else:
            print() # print new line

    # user input handling
    selection = input("Selection or Move ?> ")
    if (selection == "1"):
        # bestMove = stockfish.get_best_move_time(60000)
        # print(f"Best: {bestMove} - {describeMove(stockfish, bestMove)}")
        moves = getBestMoves(stockfish, cache)
        for move in moves:
            score = move["Centipawn"]
            mate = move["Mate"]
            move = move["Move"]
            moveDesc = describeMove(stockfish, move, color=False).ljust(50)
            if mate != None:
                print(f"{move}: {moveDesc} - Mate in {mate}")
            else:
                print(f"{move}: {moveDesc} - White advantage {score}")
    elif (selection == "2"):
        print("Move either A to B (a1b3) or shorthand")
        print("Shorthand: [Piece][file][rank]['x']target | 'oo' | 'ooo'\n"
              "If piece is ommited, pawn is assumed.\n"
              "First file and/or rank indicate source field and may be ommited\n"
              "if only one piece of the indicated or inferred type can move to target.\n"
              "x indicates capture.\n"
              "Target in the form of file and rank.\n"
              "oo and ooo for king and queen side castling.\n"
              "For convenience pieces may be identified with lower case letters except for 'B'.\n"
              "Or use the full length moves src dst"
              )
    elif (selection == "3"):
        setPosition(stockfish, cache=cache)
    elif (selection == "4"):
        fen = stockfish.get_fen_position()
        white_turn = (fen.find(" w ") > 0)
        if (white_turn):
            fen = fen.replace(" w ", " b ")
        else:
            fen = fen.replace(" b ", " w ")
        stockfish.set_fen_position(fen, False)
    elif (selection == "5"):
        for i in range(0, len(history)):
            ni = i - len(history)
            print(f"{i} | {ni}: " + colorFen(history[i]))
        try:
            idx = int(input("ID ?> "))
            if (idx >= (-1 * len(history)) and idx < len(history)):
                stockfish.set_fen_position(history[idx], False)
            else:
                print("Invalid index.")
        except ValueError:
            print("Not a number.")
    elif (selection == "6"):
        print(getBoardStr(stockfish=stockfish, flip=cache["pov"], cache=cache))
    elif (selection == "7"):
        flipPov(cache)
    elif (selection == "8"):
        flipEvalMoves(cache)
    elif (selection == "9"):
        return True
    else:
        move = selection
        evaluateMove(stockfish, move, cache)
    return False

def flipPov(cache: dict):
    cache["pov"] = not cache["pov"]

def flipEvalMoves(cache: dict):
    cache["eval_moves"] = not cache["eval_moves"]

# Shows move preview if valid, otherwise reports error
def evaluateMove(stockfish: Stockfish, move: str, cache: dict):
    move = resolveMove(stockfish, move)
    if (stockfish.is_move_correct(move)):
        desc = describeMove(stockfish, move) # describe move for later (because we update the board)
        highlight = [move[0:2], move[2:4]]
        fen = stockfish.get_fen_position() # backup board state
        if cache["eval_moves"]:
            ntm = getNTM(stockfish)
            # calculate the difference between the advantages before and after the move
            eval_before = getEval(stockfish, cache)
            stockfish.make_moves_from_current_position([move])
            eval_after = getEval(stockfish, cache)
            # generate a rating for the move
            rating = eval_after["value"] - eval_before["value"]
            if (ntm == Player.BLACK):
                # ntm is from before the move so we get the rating
                # for the player who makes the move
                rating = rating * -1
            print(getBoardStr(stockfish, flip=cache["pov"], highlight=highlight, cache=cache))
            print(f"Before: {eval_before}\n"
                  f" After: {eval_after}\n"
                  f"Move Rating for {ntm}: {rating}")
        else:
            stockfish.make_moves_from_current_position([move])
            print(getBoardStr(stockfish, flip=cache["pov"], highlight=highlight))
        print(desc)
        print("Play move?")
        choice = ask()
        if not choice:
            # reset the board to the previous state but don't clear cache
            stockfish.set_fen_position(fen, False)
    else:
        print(f"Error: {move}")

def resolveMove(stockfish: Stockfish, move: str, color: bool = True) -> str:
    # turn shorthand notation into valid moves for stockfish
    # "e4" = pawn to e4
    # "xe4" = pawn captures e4
    # "bxc5" = pawn in b file captures c5
    # "Bxc5" = bishop captures c5
    # "Bbxc5" = bishop in b file captures c5 (because another bishop could also capture c5)
    # "B3xd4" = bishop from rank 3 captures d4 (because another bishop from the same file could also capture)
    # "ra6" = rook to a6
    # "rfa6" = rook in f file (f6) to a6
    # "qxf4" = queen captures f4
    # "d2d1q" = pawn from d2 to d1 turns into a queen
    # "d2e1q" = pawn from d2 captures e1 and turns into a queen
    move = move.replace(" ", "").replace("-", "")
    ntm = getNTM(stockfish)
    if len(move) == 0:
        return "Empty move"
    if regex.match("^(?:[a-h][1-8]){2}[qrnb]$", move.lower()):
        move = move.lower()
        if stockfish.is_move_correct(move):
            return move
        else:
            return "Invalid move."
    else:
        # castling
        if (move.lower() == 'oo'):
            # castle king side
            # need to check if it's actually a king there as another piece could also have a valid move.
            if ntm == Player.WHITE and stockfish.get_what_is_on_square("e1") == Stockfish.Piece.WHITE_KING:
                move = "e1g1"
            elif ntm == Player.BLACK and stockfish.get_what_is_on_square("e8") == Stockfish.Piece.BLACK_KING:
                move = "e8g8"
            else:
                move = "Invalid"
            if stockfish.is_move_correct(move):
                return move
            else:
                return "Can not castle king side."
        elif (move.lower() == 'ooo'):
            # castle queen side
            if ntm == Player.WHITE and stockfish.get_what_is_on_square("e1") == Stockfish.Piece.WHITE_KING:
                move = "e1c1"
            elif ntm == Player.BLACK and stockfish.get_what_is_on_square("e8") == Stockfish.Piece.BLACK_KING:
                move = "e8c8"
            else:
                move = "Invalid"
            if stockfish.is_move_correct(move):
                return move
            else:
                return "Can not castle queen side."

        # resolve the rest with regex
        # do not allow lower case 'b' in first group because it conflicts with second group
        # allow other lower case letters for convenience
        match = regex.match("^([RNBKQrnkq]?)([a-h]?)([1-8]?)(x?)([a-h][1-8])(=?[RNBKQrnbkq]?)$", move)
        if match == None:
            return "Not a valid move string."
        groups = match.groups()
        piece = None

        # resolve piece class
        if len(groups[0]) == 0:
            piece = Stockfish.Piece.WHITE_PAWN if ntm == Player.WHITE else Stockfish.Piece.BLACK_PAWN
        else:
            if groups[0].lower() == 'r':
                piece = Stockfish.Piece.WHITE_ROOK if ntm == Player.WHITE else Stockfish.Piece.BLACK_ROOK
            elif groups[0] == 'B': # bxc6 is a pawn from b, not a bishop.
                piece = Stockfish.Piece.WHITE_BISHOP if ntm == Player.WHITE else Stockfish.Piece.BLACK_BISHOP
            elif groups[0].lower() == 'n':
                piece = Stockfish.Piece.WHITE_KNIGHT if ntm == Player.WHITE else Stockfish.Piece.BLACK_KNIGHT
            elif groups[0].lower() == 'k':
                piece = Stockfish.Piece.WHITE_KING if ntm == Player.WHITE else Stockfish.Piece.BLACK_KING
            elif groups[0].lower() == 'q':
                piece = Stockfish.Piece.WHITE_QUEEN if ntm == Player.WHITE else Stockfish.Piece.BLACK_QUEEN
            else:
                return f"Can not determine piece to move ('{groups[0]}')."

        # resolve source file
        src_file = None
        if len(groups[1]) == 1:
            src_file = groups[1]

        # resolve source rank
        src_rank = None
        if len(groups[2]) == 1:
            src_rank = groups[2]

        # resolve capture
        isCapture = groups[3] == 'x'

        # pawn conversion
        turnsInto = groups[5].lstrip('=')

        # resolve dst
        dst = groups[4]

        # resolve src
        src = None
        # find src
        if src_file != None and src_rank != None:
            src = f"{src_file}{src_rank}"
        else:
            possibleSrc = []
            # run through all the squares and check all the pieces if they can move to the square
            for file in range(ord('a'), ord('h') + 1):
                file = chr(file)
                if src_file != None and src_file != file:
                    continue
                for rank in range(1,8+1):
                    rank = str(rank)
                    if src_rank != None and src_rank != rank:
                        continue
                    src = f"{file}{rank}"
                    if piece == stockfish.get_what_is_on_square(src) and stockfish.is_move_correct(f"{src}{dst}{turnsInto}"):
                        possibleSrc.append(src)
            if len(possibleSrc) == 1:
                src = possibleSrc[0]
            elif len(possibleSrc) == 0:
                pieceDesc = str(piece).replace("Piece.", "")
                if color:
                    pieceDesc = colored(pieceDesc, color=ColorConst.WHITE_PIECE if ntm == Player.WHITE else ColorConst.BLACK_PIECE, on_color=ColorConst.FEN_BG, attrs=['bold'])
                if src_rank != None and src_file == None:
                    pieceDesc = pieceDesc + f" from rank {src_rank}"
                elif src_rank == None and src_file != None:
                    pieceDesc = pieceDesc + f" from file {src_file}"
                # no need to check for both since that is already covered above
                # no need to check for neither since no additional description is needed
                return f"No {pieceDesc} can go to {dst}"
            else:
                pieceDesc = str(piece).replace("Piece.", "")
                if color:
                    pieceDesc = colored(pieceDesc, color=ColorConst.WHITE_PIECE if ntm == Player.WHITE else ColorConst.BLACK_PIECE, on_color=ColorConst.FEN_BG, attrs=['bold'])
                return f"Could not determine which {pieceDesc} you want to move to {dst}"
        # build stockfish move
        move = f"{src}{dst}{turnsInto}"
        # check if resolved move is indeed a capture
        if stockfish.is_move_correct(move):
            if ((isCapture and turnsInto != '' and stockfish.get_what_is_on_square(dst) == None) or (isCapture and turnsInto == '' and stockfish.will_move_be_a_capture(move) == Stockfish.Capture.NO_CAPTURE)):
                return "Move is no Capture"
            elif (not isCapture and turnsInto != '' and stockfish.get_what_is_on_square(dst) != None) or (not isCapture and turnsInto == '' and stockfish.will_move_be_a_capture(move) != Stockfish.Capture.NO_CAPTURE):
                print("Warning: Move results in a capture, but capture was not indicated by the move string.")
            return move
    return "Invalid Move"

def describeMove(stockfish: Stockfish, move: str, color: bool = True) -> str:
    if (stockfish.is_move_correct(move)):
        # move stuff
        src = move[0:2]
        dst = move[2:4]
        turnsInto = move[4:5].lower()
        cap = None
        pieceMoving = str(stockfish.get_what_is_on_square(src)).replace("Piece.", "")
        attrs = ['bold']
        fg, bg = None, None
        tgt_fg, tgt_bg = ColorConst.FEN_BG, ColorConst.FEN_BG
        if color:
            if getNTM(stockfish) == Player.WHITE:
                fg = ColorConst.WHITE_PIECE
                tgt_fg = ColorConst.BLACK_PIECE
            else:
                fg = ColorConst.BLACK_PIECE
                tgt_fg = ColorConst.WHITE_PIECE
            pieceMoving = colored(pieceMoving, fg, bg, attrs)

        ret = f"{pieceMoving} from {src} to {dst}"

        # if capture
        cap = stockfish.will_move_be_a_capture(move)

        if cap == Stockfish.Capture.DIRECT_CAPTURE:
            tgt = stockfish.get_what_is_on_square(dst)
            tgt = str(tgt).replace("Piece.", "")
            if (color):
                tgt = colored(tgt, color=tgt_fg, on_color=tgt_bg, attrs=attrs)
            ret = ret + f" capturing {tgt}"
        elif cap == Stockfish.Capture.EN_PASSANT:
            tgt = Stockfish.Piece.WHITE_PAWN if getNTM(stockfish) == Player.BLACK else Stockfish.Piece.BLACK_PAWN
            tgt = str(tgt).replace("Piece.", "")
            if (color):
                tgt = colored(tgt, color=tgt_fg, on_color=tgt_bg, attrs=attrs)
            ret = ret + f" capturing {tgt} en passant"

        # turning pawn into something else.
        if turnsInto != '':
            if turnsInto == 'q':
                turnsInto = Stockfish.Piece.WHITE_QUEEN if getNTM(stockfish) == Player.WHITE else Stockfish.Piece.BLACK_QUEEN
            elif turnsInto == 'n':
                turnsInto = Stockfish.Piece.WHITE_KNIGHT if getNTM(stockfish)== Player.WHITE else Stockfish.Piece.BLACK_KNIGHT
            elif turnsInto == 'b':
                turnsInto = Stockfish.Piece.WHITE_BISHOP if getNTM(stockfish)== Player.WHITE else Stockfish.Piece.BLACK_BISHOP
            elif turnsInto == 'r':
                turnsInto = Stockfish.Piece.WHITE_ROOK if getNTM(stockfish)== Player.WHITE else Stockfish.Piece.BLACK_ROOK
            # remove 'Piece.'
            turnsInto = str(turnsInto).replace('Piece.', '')
            # color
            if color:
                turnsInto = colored(turnsInto, color=ColorConst.WHITE_PIECE if getNTM(stockfish) == Player.WHITE else ColorConst.BLACK_PIECE, on_color=ColorConst.FEN_BG, attrs=['bold'])
            if cap != None:
                ret = ret + " and"
            ret = ret + f" turning into {turnsInto}"
        return ret
    else:
        return "Invalid Move"

def setPosition(stockfish: Stockfish, cache: dict):
    print("https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation")
    print("Small letters are black. Possible pieces: R, N, B, Q, K, P")
    print("Numbers denote empty spaces. Rows are delimited by '/'")
    print("Next is whose turn it is. 'w' or 'b'")
    print("Then possible castlings. K or Q for king's or queen's side. '-' for none.")
    print("Then the en passant target square, even if such a move is not possible. '-' for none")
    print("Then the number of the current half turn since last capture. 0 if unsure.")
    print("Finally the number of the next turn. 1 if unsure.")
    print("Leave empty to not do anything to the current board.")
    correct = False
    while not correct:
        fen = input("FEN ?> ")
        if (len(fen) == 0):
            correct = True # abort without changing the board.
        else:
            old_fen = stockfish.get_fen_position()
            try:
                fenPass(fen)
                stockfish.set_fen_position(fen, False)
                print(getBoardStr(stockfish, flip=cache["pov"]))
                print("Correct?")
                choice = ask()
                if choice:
                    stockfish.set_fen_position(fen, True)
                    correct = True
                else:
                    stockfish.set_fen_position(old_fen, False)
                    correct = False
            except ValueError as ex:
                print("Invalid position.")
                print(ex)
                correct = False

def getBoardStr(stockfish: Stockfish, flip: bool = False, highlight: list[str] = [], cache = None, color = True) -> str:
    # unfliped A8 is top left
    range_ranks = range(1, 8 + 1) if flip else range(8, 1 - 1, -1)
    range_files = range(ord('a'), ord('h') + 1) if not flip else range(ord('h'), ord('a') - 1, -1)
    white_squre = True # first square is white regardless of orientation
    lineSep = "  " + (("+---" * 9)[:-3]) # the seperator between the lines on the board.

    result = " " # spacing for header to line up right
    # make files header
    for file in range_files:
        file = chr(file)
        result = result + f"   {file}"
    result = result.rstrip() + "\n"

    # print actual board
    for rank in range_ranks:
        result = result + lineSep + "\n"
        result = result + f"{rank} |"
        for file in range_files:
            file = chr(file)
            sqr = f"{file}{rank}"
            piece = stockfish.get_what_is_on_square(sqr)
            fieldStr = "   " if piece == None else f" {piece.value} "
            if color:
                fg = None
                bg = ColorConst.BOARD_WHITE if white_squre else ColorConst.BOARD_BLACK
                attrs = ['bold']
                if sqr in highlight:
                    attrs.append('reverse')
                if piece != None:
                    if piece.value in ['r', 'n', 'b', 'q', 'k', 'p']:
                        fg = ColorConst.BLACK_PIECE
                    else:
                        fg = ColorConst.WHITE_PIECE
                fieldStr = colored(fieldStr, color=fg, on_color=bg, attrs=attrs)
            result = result + f"{fieldStr}|"
            white_squre = not white_squre
        result = result + f" {rank}\n"
        white_squre = not white_squre
    result = result + lineSep + "\n"

    # make files footer
    result = result + " " # spacing for footer to line up right
    for file in range_files:
        file = chr(file)
        result = result + f"   {file}"
    result = result.rstrip() + "\n"

    # more description stuffs
    result = result + getFen(stockfish, color) + "\n"
    result = result + f"Next to move: {getNTM(stockfish)}\n"
    if (not cache is None):
        result = result + f"Evaluation (Positive is advantage for White):\n{str(getEval(stockfish, cache))}\n"
        result = result + f"Win/Draw/Loose stats for {getNTM(stockfish)}\n{str(getWDL(stockfish, cache))}\n"
    return result

def getWDL(stockfish: Stockfish, cache: dict) -> list:
    key = "wdl"
    fen = stockfish.get_fen_position()
    if not (fen in cache and key in cache[fen]):
        if not fen in cache:
            cache[fen] = {}
        cache[fen][key] = stockfish.get_wdl_stats() if stockfish.does_current_engine_version_have_wdl_option() else [0,0,0]
    return cache[fen][key]

def getBestMoves(stockfish: Stockfish, cache: dict, moves: int = 0) -> list[dict]:
    key = "best_moves"
    if moves <= 0:
        moves = stockfish.get_parameters()["MultiPV"]
    fen = stockfish.get_fen_position()
    if not (fen in cache and key in cache[fen]):
        if not fen in cache:
            cache[fen] = {}
        cache[fen][key] = stockfish.get_top_moves(moves)
    return cache[fen][key]

def getEval(stockfish: Stockfish, cache: dict) -> dict:
    key = "eval"
    fen = stockfish.get_fen_position()
    if not (fen in cache and key in cache[fen]):
        if not fen in cache:
            cache[fen] = {}
        cache[fen][key] = stockfish.get_evaluation()
    return cache[fen][key]

def getNTM(stockfish: Stockfish) -> str:
    return Player.WHITE if (stockfish.get_fen_position().find(" w ") > 0) else Player.BLACK

def getFen(stockfish: Stockfish, color: bool = True) -> str:
    fen = stockfish.get_fen_position()
    if color:
        return colorFen(fen)
    else:
        return fen

def colorFen(fen: str) -> str:
    coloredFen = ""
    attrs=['bold']
    for c in fen:
        cc = c
        if cc in ['R','N','B','Q','K','P', 'w']:
            cc = colored(c, color=ColorConst.WHITE_PIECE, on_color=ColorConst.FEN_BG, attrs=attrs)
        elif cc in ['r','n','b','q','k','p']:
            cc = colored(c, color=ColorConst.BLACK_PIECE, on_color=ColorConst.FEN_BG, attrs=attrs)
        else:
            cc = colored(c, color=None, on_color=ColorConst.FEN_BG, attrs=attrs)
        coloredFen = coloredFen + cc
    return coloredFen

def fenPass(fen):
    regexMatch=regex.match('\s*^(((?:[rnbqkpRNBQKP1-8]+\/){7})[rnbqkpRNBQKP1-8]+)\s([b|w])\s([K|Q|k|q]{1,4}|-)\s(-|[a-h][1-8])\s(\d+\s\d+)$', fen)
    if  regexMatch:
        regexList = regexMatch.groups()
        fen = regexList[0].split("/")
        if len(fen) != 8:
            raise ValueError("expected 8 rows in position part of fen: {0}".format(repr(fen)))

        white_king = False
        black_king = False
        for fenPart in fen:
            field_sum = 0
            previous_was_digit = False

            for c in fenPart:
                if c in ["1", "2", "3", "4", "5", "6", "7", "8"]:
                    if previous_was_digit:
                        raise ValueError("two subsequent digits in position part of fen: {0}".format(repr(fen)))
                    field_sum += int(c)
                    previous_was_digit = True
                elif c.lower() in ["p", "n", "b", "r", "q", "k"]:
                    field_sum += 1
                    previous_was_digit = False
                    if c == "K" and not white_king:
                        white_king = True
                    elif c == "k" and not black_king:
                        black_king = True
                    elif c == "K" and white_king:
                        raise ValueError("Multiple white kings.")
                    elif c == "k" and black_king:
                        raise ValueError("Multiple black kings.")
                else:
                    raise ValueError("invalid character in position part of fen: {0}".format(repr(fen)))
            # end for in fenPart
        # end for in fen
        if field_sum != 8:
            raise ValueError("expected 8 columns per row in position part of fen: {0}".format(repr(fen)))
        if not black_king:
            raise ValueError("Black king not present.")
        if not white_king:
            raise ValueError("White king not present.")
    else: # else regex doesn't match
        raise ValueError("fen doesn`t match follow this example: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 ")

def ask(defaultTrue: bool = True) -> bool:
    if defaultTrue:
        answer = input("Y/n ?> ")
        if answer == '' or answer[0].lower() != 'n':
            return True
        else:
            return False
    else:
        answer = input("y/N ?> ")
        if anwer == '' or answer[0].lower() != 'y':
            return False
        else:
            return True

if __name__ == "__main__":
    main()


from django.db.models import Count, Sum
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse_lazy
from django.views import View
from django.views.generic import ListView, TemplateView, DeleteView

from webapp.models import Article, Basket

from webapp.forms import OderForm

from webapp.models import Products

class BasketAddView(View):
    def post(self, request, **kwargs):
        product = get_object_or_404(Article, pk=kwargs.get('pk'))
        baskets = Basket.objects.filter(product=product.pk)
        if baskets:
            for basket in baskets:
                basket.plus()
            product.minus()
            return redirect('index_article')
        else:
            Basket.objects.create(
                product=product,
                quantity=1
            )
            product.minus()
        return redirect('index_article')

class BasketView(TemplateView):
    template_name = "basket.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        basket = Basket.objects.exclude(quantity=0)
        for i in basket:
            i.sum = i.quantity * i.product.price
        context['basket_sum'] = 0
        for i in basket:
            context['basket_sum'] += i.sum
        context['basket'] = basket
        context['form'] = OderForm()
        return context

    def post(self, request, *args, **kwargs):
        form = OderForm(data=request.POST)
        products = Basket.objects.all()
        if form.is_valid():
            order_pk = form.save()
            for po in products:
                Products.objects.create(product_id=po.product, order_id=order_pk, count=po.quantity)
                po.delete()
            return redirect('index_article')
        return render(request, 'basket.html', context={'form': form})

class BasketDeleteView(DeleteView):
    model = Basket
    success_url = reverse_lazy('baskets_view')


# coding:UTF-8
import argparse
import os
import glob
import cv2
import numpy as np
import tqdm

import torch
import torchvision
import torch.nn as nn
import torch.optim as optim
import torch.utils.data as data
from torch.utils.data import TensorDataset, DataLoader
from torchvision import models, transforms

from srcnn_model import srcnn_net
"""
画像を拡大するネットワーク
"""

class SrcnnDataSet(data.Dataset):
    """
    ジェネレータークラス
    """
    def __init__(self, file_list, transforms=None):
        self.img_file_list = file_list
        self.transform = transforms

    def __len__(self):
        return len(self.img_file_list)

    def __getitem__(self, index):
        anno_img = cv2.imread(self.img_file_list[index])
        #img = cv2.resize(img, (960, 1280)) #あまりに大きい画像が来るとエラーになるため
        o_h, o_w = anno_img.shape[0:2]
        anno_img = anno_img.astype(np.float32)
        anno_img = anno_img.T/255.

        img = cv2.imread(self.img_file_list[index])
        img = cv2.resize(img, (int(o_w/4), int(o_h/4)))
        img = cv2.resize(img, (o_w, o_h))
        img = img.astype(np.float32)
        img = img.T/255.

        return img, anno_img

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="srcnn")
    parser.add_argument('--train_dir', help="学習データがあるディレクトリ", default='train')
    parser.add_argument('--val_dir', help="検証用データがあるディレクトリ", default='val')
    parser.add_argument('--weights_dir', help="pthファイルの出力先ディレクトリ", default='weights')
    parser.add_argument('--batch_size', help="ミニバッチのサイズ", default=4, type=int)
    parser.add_argument('--device', help="GPU", default="cuda:1")
    parser.add_argument('--epochs', help="GPU", default=100, type=int)

    args = parser.parse_args()

    train_path = args.train_dir
    val_path = args.val_dir
    weights_path = args.weights_dir
    batch_size = args.batch_size
    device = args.device
    epochs = args.epochs

    # 訓練用画像のファイルリスト取得
    train_img_list = []
    tmp_img_list = glob.glob(os.path.join(train_path, "*.jpg"))
    for filename in tmp_img_list:
        s = os.path.getsize(filename)
        if s > 30*1000:
            train_img_list.append(filename)

    # 検証用画像のファイルリスト取得
    val_img_list = glob.glob(os.path.join(val_path, "*.jpg"))

    # ジェネレーター作成
    train_dataset = SrcnnDataSet(train_img_list, transforms=transforms.ToTensor())
    train_loader = DataLoader(train_dataset, batch_size, shuffle=True)

    net = srcnn_net().to(device)
    net.train()

    # 損失関数は最小二乗法
    loss_fn = nn.MSELoss()

    optimizer = optim.Adam(net.parameters())

    for epoch in range(epochs):
        print("epoch:{}/{}".format(epoch, epochs))
        loss_val = 0.0
        for i, (img, anno_img) in tqdm.tqdm(enumerate(train_loader), total=len(train_loader)):
            x = img.to(device)
            y = anno_img.to(device)
            p = net(x)
            loss = loss_fn(x, p)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            loss_val += loss.item()

        print("loss:{:.05f}".format(loss_val))
        output_pth = os.path.join(weights_path, "srcnn-{}.pth".format(epoch))
        if not os.path.exists(weights_path):
            os.makedirs(weights_path)
        torch.save(net.state_dict(), output_pth)


import logging
logging.basicConfig(filename="SwapNumber.py", filemode="w")
def swap_numbers(num1, num2):
    """
    :param num1:
    :param num2:
    :return:
    """
    temp = num1
    num1 = num2
    num2 = temp
    print("After swapping, number1 is %d and number2 is %d"% (num1, num2))

if __name__ == '__main__':
    try:
        num1 = int(input("Enter number1: "))
        num2 = int(input("Enter number2: "))
        print("Before swapping, number1 is %d and number2 is %d"% (num1, num2))
        swap_numbers(num1, num2)
    except:
        print("Enter valid number")


n, k = map(int, input().split())

it = 0
while n != 1:
    div, mod = divmod(n, k)
    if mod == 0:
        n //= k
        it += 1
    else:
        n = div
        it += mod + 1
print(it)


#!/usr/bin/env python

import itertools

from collections import defaultdict, Counter
from typing import List, Set, Tuple

def gen_primes(n):
    primes = [2]
    for p in range(3, n+1, 2):
        if not any(p % q == 0 for q in primes):
            primes.append(p)
    return primes

def print_set_short_repr(l, count=10) -> str:
    if len(l) <= count:
        return str(l)
    return " ".join(map(str, l[:count//2])) + " ... " + " ".join(map(str, l[-count//2:]))

def enumerate_quadratic_form(a: int, b: int, c: int, n: int) -> Set[int]:
    """Enumerate all numbers a x^2 + b y^2 <= n."""
    population = set()
    stop = n if c >= 0 else abs(c) * n

    for x in range(n):
        temp_x = a * x*x
        if temp_x > n:
            break

        for y in range(n):
            temp = temp_x + c*x*y +  b * y*y
            if temp > n:
                break

            if temp > 0:
                population.add(temp)

    return population

def merge_primes(primes: List[int], n: int) -> Tuple[int, ...]:
    """
    Merge "primes" (some are prime^2)

    Similar to Smooth Numbers
    Returns all numbers ( <= n) composed only of primes from primes

    Can be speed up if so desired
    """
    gen = [1]
    for prime in primes:
        if prime == 1:
            continue
        nextGen = gen[:]
        primePower = prime
        while primePower <= n:
            for i in gen:
                number = i * primePower
                if number > n:
                    break
                nextGen.append(number)
            primePower *= prime
        gen = sorted(nextGen)
    #gen.remove(1)
    return tuple(gen)

def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(len(s)+1))

def raise_to_power(l: List[int], p: int) -> List[int]:
    """Raise each item in l to p"""
    return tuple([a ** p for a in l])

def enumerate_prime_splits(n: int):
    primes = gen_primes(n)
    print("primes:", print_set_short_repr(primes))

    prime_splits = set()

    # Exclude 2
    test_pop = merge_primes([p for p in primes if p > 2], n)
    yield test_pop, "p > 2"

    # Exclude 2/3
    test_pop = merge_primes([p for p in primes if p > 3], n)
    yield test_pop, "p > 3"

    # TODO how to enumerate more / better splits
    for modulo in range(3, 100+1):
        mods = tuple(sorted(set(p % modulo for p in primes)))
        # TODO maybe loosen this later
        if len(mods) > 13:
            continue

        common_mods = tuple(sorted(m for m, c in Counter(p % modulo for p in primes).items() if c > 3))
        extra_mods = tuple(sorted(set(mods) - set(common_mods)))
        print("\t", modulo, "\tmods", common_mods, " + ", extra_mods)

        # Exclude common mods
        test_pop = merge_primes([p for p in primes if p % modulo not in extra_mods], n)
        yield test_pop, f"exclude p % {modulo} not in {extra_mods}"

        # Exclude a single prime (modulo)
        if modulo in primes:
            test_pop = merge_primes([p for p in primes if p != modulo], n)
            yield test_pop, f"p != {modulo}"

        for m_split in powerset(mods):
            # Don't allow the full / empty set
            if not m_split or m_split == mods:
                continue

            # Consider itertool partition recipe
            a_primes = []
            b_primes = []
            for p in primes:
                if p % modulo in m_split:
                    a_primes.append(p)
                else:
                    b_primes.append(p)
            a_primes = tuple(a_primes)
            b_primes = tuple(b_primes)

            if a_primes in prime_splits:
                continue
            prime_splits.add(a_primes)

            """
            Try only a primes
            Try a^1, b^2
            Inverses are tested with the inverse m_split
            """

#            print()
#            print(f"\t{m_split!s:12}", print_set_short_repr(a_primes))
#            print(f"\t{' ':12}", print_set_short_repr(b_primes))

            test_pop = merge_primes(a_primes, n)
            yield test_pop, f"p % {modulo} in {m_split}"

            test_pop = merge_primes(raise_to_power(a_primes, 2) + b_primes, n)
            yield test_pop, f"p % {modulo} in {m_split} to an even power"

            if a_primes[0] ** 3 <= n and b_primes[0] ** 2 <= n:
                test_pop = merge_primes(raise_to_power(a_primes, 3) + b_primes, n)
                yield test_pop, f"p % {modulo} in {m_split} higher powers (3, 1)"

                test_pop = merge_primes(raise_to_power(a_primes, 3) + raise_to_power(b_primes, 2), n)
                yield test_pop, f"p % {modulo} in {m_split} higher powers (3, 2)"

            if a_primes[0] ** 4 <= n:
                test_pop = merge_primes(raise_to_power(a_primes, 4) + b_primes, n)
                yield test_pop, f"p % {modulo} in {m_split} higher powers (4, 1)"

                test_pop = merge_primes(raise_to_power(a_primes, 4) + raise_to_power(b_primes, 2), n)
                yield test_pop, f"p % {modulo} in {m_split} higher powers (4, 1)"

            #test_pop = merge_primes([a ** 2 for a in a_primes] + [b ** 2 for b in b_primes], n)
            #yield test_pop, f"p % {modulo} all even power"

def find_brute(n = 1000):

    """
    Can find some of these with searches like
        "even exponent" "prime" "quadratic form"
        "even power" "prime" "quadratic form"
        "quadratic form" "prime" "representation"
    """

    names = {
        (1, -2, 0): "A035251 / A000047",
        (1, 1, 0): "A001481 / A000050",
        (1, 2, 0): "A002479 / A000067",

        (1, 3, 0): "A003136 / A000205",
        (1, 1, -1): "A003136 / A000205",
        (1, 1, 1): "A003136 / A000205",

        (5, -1, 0): "A031363",
        (1, 1, 3): "A031363",
    }

    min_n = 20

    forms = defaultdict(list)

    # TODO: Negative sequences are hard to enumerate
    # Find some general trick?
    for a in range(1, 100+1):
        for b in range(a, 100+1):
            for cross in (-1, 0, 1, 2, 3):

                # Repeated forms not needed
                if (a, b, cross) in [(1, 1, 1), (1, 2, 2), (1, 1, -1), (1, 3, 3), (2, 2, -1)]:
                    continue

                population = enumerate_quadratic_form(a, b, cross, n)
                if not population:
                    continue

                population = tuple(sorted(p for p in population if p >= min_n))
                default_name = f"{a} x^2 {cross:+} xy + {b} y^2" if cross != 0 else f"{a} x^2 + {b} y^2"
                name = names.get((a, b, cross), default_name)

                other_name = forms.get(population)
                #assert other_name is None, f"{name} same as {other_name}"
                if other_name:
                    print(f"\t{name} same as {other_name}")

                forms[population].append(name)

    print(f"Enumerated {len(forms)} Quadratic Forms")
    print()
    for test, split_name in enumerate_prime_splits(n):
        test = tuple(sorted(p for p in test if p >= min_n))
        if test in forms:
            print()
            print("Match!", split_name, "  <==>  ", " | ".join(forms[test]))
            print("\tsequence:", print_set_short_repr(test, 24))
            if not any("A" in seq for seq in forms[test]):
                print("\t", test)
            print()

def subgroups():
    comb_with_repl = itertools.combinations_with_replacement

    primes = gen_primes(1000)
    Zp = 9

    def is_closed(subgroup):
        return all(a * b % Zp in subgroup for a, b in itertools.product(subgroup, repeat=2))

    all_mods = tuple(sorted(set(p % Zp for p in primes) - {0}))
    minus_two = tuple(m for m in all_mods if m != 2)
    minus_three = tuple(m for m in all_mods if m != 3)
    minus_two_three = tuple(m for m in all_mods if m > 3)

    for mods in [all_mods, minus_two, minus_three, minus_two_three]:
        missing = sorted(set(all_mods) - set(mods))

        sub_groups = [g for g in powerset(mods) if is_closed(g)]
        # empty sub_group is kinda boring
        sub_groups.remove(tuple())

        for g1, g2 in itertools.combinations(sub_groups, 2):
            if set(g1) | set(g2) == set(mods) and (not (set(g1) & set(g2))):
                print("\tA", g1, g2)

        for g in powerset(mods):
            if not g: continue

            inv = tuple(sorted(set(mods) - set(g)))
            # Check if g^2 -> !g
            if all(a * b % Zp in inv for a, b in comb_with_repl(g, 2)):
                if inv in sub_groups:
                    g_a = tuple([p for p in primes if p % Zp in g])
                    g_b = tuple([p for p in primes if p % Zp in inv])
                    m_p = tuple([p for p in primes if p % Zp in missing])
                    print("\tA^2 -> B, B^2 -> B:", g, inv, "\tmissing:", m_p)
                    print()

                    # Could try powerset(m_p) if it contains more than one
                    print("merged a^2          :", merge_primes(raise_to_power(g_a, 2) + g_b + m_p, 100))
                    print("merged b^2          :", merge_primes(g_a + m_p + raise_to_power(g_b, 2), 100))
                    if m_p:
                        print("merged (a+missing)^2:", merge_primes(raise_to_power(g_a + m_p, 2) + g_b, 100))
                        print("merged (b+missing)^2:", merge_primes(g_a + raise_to_power(g_b + m_p, 2), 100))
                    print()
                    print()

def find_quadratic_with_primes():
    n = 5
    present = [11, 19, 31, 59, 71, 79, 131, 139, 151, 179, 191, 199]
    not_present = [29, 41, 61, 89, 101, 109, 149, 181]

    present = set(present)
    not_present = set(not_present)
    assert not (present & not_present)

    # TODO: Negative sequences are hard to enumerate
    # Find some general trick?
    for a in range(-100, 100+1):
        for b in range(-100, 100+1):
            for cross in range(-100, 100+1):
                if cross*cross - 4*a*b != -4 * n:
                    continue
                print(a, cross, b)

                population = enumerate_quadratic_form(a, b, cross, max(max(present), max(not_present)))
                population = set(population)
                if (population & present) == set(present) and len(population | not_present) == 0:
                    print("Found!", (a,b,cross))

if __name__ == "__main__":
    #find_brute(8000)
    #subgroups()
    find_quadratic_with_primes()


import datetime
from unittest import TestCase
from unittest.mock import patch

from backend import slack_util, db, match_making, configs
from backend.commands import user_stats
from backend.commands.command_message import CommandMessage
from backend.league_context import LeagueContext
from tests import test_league_setup

lctx = None

class Test(TestCase):
    def setUp(self):
        test_league_setup.teardown_test_league()
        test_league_setup.create_test_league()

        league_name = 'unittest'
        global lctx
        lctx = LeagueContext.load_from_db(league_name)

        for g in ['A', 'B', 'C']:
            for i in range(1, 9):
                db.add_player(lctx.league_name, 'player{}{}'.format(g, i), 'Player {}{}'.format(g, i), g)

        match_making.create_matches_for_season(lctx.league_name, datetime.date(2022, 1, 3), 3, [])

        for i in range(2, 9):
            db.update_match_by_id(lctx.league_name, 'playerA1', 'playerA{}'.format(i), 3, 0, 0)
        for i in range(2, 9):
            db.update_match_by_id(lctx.league_name, 'playerB1', 'playerB{}'.format(i), 3, 1, 0)
        for i in range(2, 9):
            db.update_match_by_id(lctx.league_name, 'playerC1', 'playerC{}'.format(i), 3, 2, 0)

        # new season with more sets needed
        match_making.create_matches_for_season(lctx.league_name, datetime.date(2022, 1, 3), 4, [])
        for i in range(2, 9):
            db.update_match_by_id(lctx.league_name, 'playerA1', 'playerA{}'.format(i), 4, 1, 0)
        for i in range(2, 9):
            db.update_match_by_id(lctx.league_name, 'playerB1', 'playerB{}'.format(i), 4, 2, 0)
        for i in range(2, 9):
            db.update_match_by_id(lctx.league_name, 'playerC1', 'playerC{}'.format(i), 4, 3, 0)

    def test_handles_message(self):
        self.assertEqual(True, user_stats.handles_message(lctx, CommandMessage('My ToTaL StAtS', 'Dchannel', 'any_user', 'any_timestamp')))
        self.assertEqual(False, user_stats.handles_message(lctx, CommandMessage('My ToTaL StAtS', 'any_channel', 'any_user', 'any_timestamp')))
        self.assertEqual(False, user_stats.handles_message(lctx, CommandMessage('a my total stats', 'any_channel', 'any_user', 'any_timestamp')))
        self.assertEqual(False, user_stats.handles_message(lctx, CommandMessage('my total stats and more', 'any_channel', 'any_user', 'any_timestamp')))
        self.assertEqual(False, user_stats.handles_message(lctx, CommandMessage('just stats', 'any_channel', 'any_user', 'any_timestamp')))

        db.set_config(lctx.league_name, configs.ENABLE_COMMAND_USER_STATS, 'FALSE')
        fresh_lctx = LeagueContext.load_from_db(lctx.league_name)
        self.assertEqual(False, user_stats.handles_message(fresh_lctx, CommandMessage('My ToTaL StAtS', 'Dchannel', 'any_user', 'any_timestamp')))

    def test_build_stat_message(self):
        matches = db.get_matches(lctx.league_name)
        result = user_stats.build_stat_message(matches, 'playerA1')
        self.assertEqual('\n Matches Won: 14 | Matches Lost: 0 | Sets Won: 49 | Sets Lost: 7', result)

        result = user_stats.build_stat_message(matches, 'playerA2')
        self.assertEqual('\n Matches Won: 0 | Matches Lost: 2 | Sets Won: 1 | Sets Lost: 7', result)

    @patch.object(slack_util, 'post_message')
    def test_handle_message(self, mock_post_message):
        matches = db.get_matches(lctx.league_name)
        msg = CommandMessage('help', 'Dchannel', 'playerA1', 'any_timestamp')
        user_stats.handle_message(lctx, msg)
        mock_post_message.assert_called_once_with(lctx, user_stats.build_stat_message(matches, 'playerA1'), 'Dchannel')


import dropbox
import csv

client = dropbox.Dropbox('XXXXXXXXXXXXXXXXXXXXXXXXX')
# print(client.users_get_current_account())
metadata = client.files_list_folder('/All onboard images').entries
# metadata = metadata.json()
#print(metadata)

with open('dates.csv', mode='w') as metadata_file:
    writer = csv.writer(metadata_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    writer.writerow(['date','filename'])

    for i in range(0, 10):
        #print(metadata[i].client_modified)
        #print(metadata[i].name)

        dates = metadata[i].client_modified

        files = metadata[i].name

        writer.writerow([dates,files])


# -*- coding: utf-8 -*-
# File: predict_cam.py
# @Author: 黄文俊

# @Time: 2021/12/10 15:58
import joblib
import numpy as np
import cv2
from numpy import ndarray

from webcam_base import CameraThread, thread_lock

class CameraThreadPredict(CameraThread):
    """
    实时颜色识别类，继承CameraThread父类
    """

    def __init__(self, *args, **kwargs):
        super(CameraThreadPredict, self).__init__(*args, **kwargs)
        # 预加载模型
        self.clf = joblib.load("../imgs/svm.model")

    def pretreatment_frame(self, img: ndarray) -> ndarray:
        """
        对视频帧预处理
        :param img:视频帧
        :return 绘制完轮廓矩形框后的视频帧
        """
        contours = self.find_counters(img)
        img = img.copy()
        for cnt in contours:
            try:
                l_corner, r_corner, mid, rect_area = self._find_img_mid_point(cnt)
                # img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
                print(rect_area)
                if 8000  > rect_area > 5000:
                    res = self.predict_color(mid['x'], mid['y'], img)
                    # 圈出颜色块
                    img = cv2.rectangle(img, l_corner, r_corner, (0, 255, 0), 2)
                    # 在色块上打上识别结果
                    img = cv2.putText(img, res, (mid['x'], mid['y']), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (255, 255, 255), 5,
                                      cv2.LINE_AA, False)
                else:
                    continue
            except Exception as e:
                print(e)
                continue
        return img

    def predict_color(self, mid_x: int, mid_y: int, img: ndarray) -> str:
        """
        截取基于中点的 25*25*3的截图与模型匹配获取识别结果并在原图绘制轮廓矩形框以及识别结果
        :param mid_x:轮廓中点x轴坐标
        :param mid_y:轮廓中点y轴坐标
        :param img:视频帧
        :return 识别结果
        """
        # 基于中点要检测的位置
        check_img = img[mid_y - 12:mid_y + 13, mid_x - 12:mid_x + 13]
        # 图片归一化处理
        img_normalization = (check_img - np.min(check_img)) / (np.max(check_img) - np.min(check_img))
        # 二维向量转为一维向量
        one_vector = np.reshape(img_normalization, (1, -1))
        # 模型匹配结果
        res = self.clf.predict(one_vector)
        print(res)
        return res[0]

def main():
    camera_id = "rtsp://admin:admin@192.168.101.5:8554/live"

    thread = CameraThreadPredict(camera_id)
    thread.start()

    while True:
        thread_lock.acquire()
        frame = thread.get_frame()
        thread_lock.release()

        frame = thread.pretreatment_frame(frame)
        cv2.imshow('Video', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            thread_lock.acquire()
            thread.set_thread_exit(True)
            thread_lock.release()
            break

    thread.join()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()


from typing import List, Dict

import nltk
import scipy
from sentence_transformers import SentenceTransformer

from text_api.models import Text

class SentenceEmbedding:
    top_amount_results: int = 20

    def __init__(self, sentence: str, slug: str):
        """sentence to compare with"""
        self.sentence: str = sentence
        # slug of sentence text
        self.slug: str = slug
        self.texts: List[Text] = self._get_texts()
        # Sentence Embedding used with SentenceBERT model
        self.model: SentenceTransformer = SentenceTransformer('bert-base-nli-mean-tokens')
        self.results: List[Dict[str, str]] = []

    @classmethod
    def run(cls, sentence: str, slug: str) -> List[Dict[str, str]]:
        """starting point"""
        obj = cls(sentence, slug)
        obj.search()
        obj.sort_results()

        return obj.results

    def _get_texts(self) -> List[Text]:
        """find all texts except current"""
        return Text.query.filter(Text.slug != self.slug).all()

    @staticmethod
    def _convert_text_sentences(text: str) -> List[str]:
        """convert text to sentences"""
        return nltk.tokenize.sent_tokenize(text)

    def search(self) -> None:
        """define search query and embed into vector"""
        query = self.model.encode(self.sentence)
        # iterate through texts
        for text in self.texts:
            self._calculate_text_result(text, query)

    def _calculate_text_result(self, text: Text, query: List):
        """create vector from sentence we try to compare with needed"""
        corpus = self._convert_text_sentences(text.content)
        corpus_embeddings = self.model.encode(corpus)
        # compute the cosine similarity with scipy
        distances = scipy.spatial.distance.cdist([query], corpus_embeddings, 'cosine')[0]

        # merge distances with ids and sort
        results = zip(range(len(distances)), distances)
        results = sorted(results, key=lambda x: x[1])

        # create result's dictionary with top distance
        for idx, distance in results[:self.top_amount_results]:
            self.results.append({
                'sentence': corpus[idx].strip(),
                'distance': 1 - distance,
                'slug': text.slug,
            })

    def sort_results(self) -> None:
        """sort reverse results"""
        self.results = sorted(self.results, key=lambda r: r['distance'], reverse=True)


"""
Manages the list of addresses for the wallet, including labels and derivation paths
"""
import os
from ..persistence import write_csv, read_csv
import logging

logger = logging.getLogger(__name__)

def to_bool(v):
    if isinstance(v, bool):
        return v
    if isinstance(v, int):
        return bool(v)
    return v == "True"

class Address(dict):
    columns = [
        "address",  # str, address itself
        "index",  # int, derivation index
        "change",  # bool, change or receive
        "label",  # str, address label
        "used",  # bool, does this address have a transaction?
        "service_id",  # str, reserved for or used by Service.id
    ]
    type_converter = [
        str,
        int,
        to_bool,
        str,
        to_bool,
        str,
    ]

    def __init__(self, rpc, **kwargs):
        self.rpc = rpc
        # copy
        kwargs = dict(**kwargs)
        # replace with None or convert
        for i, k in enumerate(self.columns):
            v = kwargs.get(k, "")
            kwargs[k] = None if v in ["", None] else self.type_converter[i](v)

        super().__init__(**kwargs)

    def set_label(self, label):
        if self["label"] == label:
            return
        self["label"] = label
        self.rpc.setlabel(self.address, label)

    def set_service_id(self, service_id: str):
        if service_id:
            # Declares that this Address is associated with a Service
            self["service_id"] = service_id
        else:
            # Frees a reserved Address; reset its label
            self["service_id"] = None
            if not self.used and self["label"]:
                self.set_label("")

    @property
    def is_external(self):
        return self.index is None

    @property
    def is_mine(self):
        return not self.is_external

    @property
    def is_receiving(self):
        return self.is_mine and not self.change

    @property
    def is_change(self):
        return self.is_mine and self.change

    @property
    def index(self):
        return self["index"]

    @property
    def address(self):
        return self["address"]

    @property
    def change(self):
        return self["change"]

    @property
    def used(self):
        return self["used"]

    @property
    def label(self):
        if self["label"]:
            return self["label"]
        if self["change"] is not None and self["index"] is not None:
            prefix = "Change #" if self.change else "Address #"
            index = self["index"]
            return f"{prefix}{index}"
        return self.address

    @property
    def is_labeled(self):
        return bool(self["label"])

    @property
    def is_reserved(self):
        return bool(self["service_id"])

    @property
    def service_id(self):
        return self["service_id"]

    def __str__(self):
        return self.address

    def __repr__(self):
        if self.label != self.address:
            return f"Address({self.label}, {self.address})"
        else:
            return f"Address({self.address})"

class AddressList(dict):
    AddressCls = Address

    def __init__(self, path, rpc):
        super().__init__()
        self.path = path
        self.rpc = rpc
        file_exists = False
        if os.path.isfile(self.path):
            try:
                addresses = read_csv(self.path, self.AddressCls, self.rpc)
                # dict allows faster lookups
                for addr in addresses:
                    self[addr.address] = addr
                file_exists = True
            except Exception as e:
                logger.exception(e)
        self._file_exists = file_exists

    def save(self):
        if len(list(self.keys())) > 0:
            write_csv(self.path, list(self.values()), self.AddressCls)
        self._file_exists = True

    def set_labels(self, arr):
        """
        Sets the labels for existing addresses

        arr should be a list with dicts, example:
            [{'address':'bc1qabc...', 'label':'mylabel1'}, {'address':'bc1qabd...', 'label':'mylabel2'}, ...]
        """
        for addr in arr:
            if addr["address"] in self:
                self[addr["address"]].set_label(addr["label"])
        self.save()

    def add(self, arr, check_rpc=False):
        """
        arr should be a list with dicts, example:
            [{'address':'bc1qabc...', 'label':'mylabel1'}, {'address':'bc1qabd...', 'label':'mylabel2'}, ...]
        """
        labeled_addresses = {}
        if check_rpc:
            # get all available labels
            labels = self.rpc.listlabels()
            if "" in labels:
                labels.remove("")
            # get addresses for all labels
            res = self.rpc.multi([("getaddressesbylabel", label) for label in labels])
            for i, result in enumerate(res):
                label = labels[i]
                # go through all addresses and assign labels
                for k in result["result"].keys():
                    labeled_addresses[k] = label
        # go through all addresses and assign
        for addr in arr:
            if addr["address"] in self:
                continue
            # if we found a label for it - import
            if addr["address"] in labeled_addresses:
                addr["label"] = labeled_addresses[addr["address"]]
            self[addr["address"]] = self.AddressCls(self.rpc, **addr)
        # add all labeled addresses but not from the array (destination)
        for addr in labeled_addresses:
            if addr not in self:
                self[addr] = self.AddressCls(
                    self.rpc,
                    address=addr,
                    label=labeled_addresses[addr],
                    change=None,
                    index=None,
                )
        self.save()

    def get_address(self, address: str) -> Address:
        if address not in self:
            self[address] = self.AddressCls(self.rpc, address=address)
        return self[address]

    def set_label(self, address, label):
        addr_obj = self.get_address(address)
        addr_obj.set_label(label)
        self.save()

    def get_labels(self):
        labels = {}
        for addr in self.values():
            lbl = addr.get("label", "")
            if lbl:
                labels[lbl] = labels.get(lbl, []) + [addr.address]
        return labels

    def associate_with_service(
        self, address: str, service_id: str, label: str, autosave: bool = True
    ):
        """
        Associates the Address (i.e. sets Address.service_id) with the specified
        Service.id.
        """
        addr_obj = self.get_address(address)
        addr_obj.set_service_id(service_id)
        addr_obj.set_label(label)
        if autosave:
            self.save()

    def deassociate(self, address: str, autosave: bool = True):
        """
        Removes the Address's association with a Service (i.e. sets
        Address.service_id to None and resets Address.label).
        """
        addr_obj = self.get_address(address)
        addr_obj.set_service_id(None)
        if autosave:
            self.save()

    def set_used(self, addresses):
        need_save = False
        for address in addresses:
            if address not in self:
                # external maybe???
                continue
            addr = self[address]
            # doesn't make sense to set used for external addresses
            if addr.is_external or addr.used:
                continue
            addr["used"] = True
            need_save = True
        if need_save:
            self.save()

    def max_index(self, change=False):
        return max(
            0,
            0,
            *[
                addr.index or 0
                for addr in self.values()
                if addr.is_mine and addr.change == change
            ],
        )

    def max_used_index(self, change=False):
        return max(
            -1,
            -1,
            *[
                addr.index or -1
                for addr in self.values()
                if addr.is_mine and addr.used and addr.change == change
            ],
        )

    @property
    def file_exists(self):
        return self._file_exists and os.path.isfile(self.path)


# https://leetcode.com/problems/running-sum-of-1d-array/description/

class Solution(object):
    def runningSum(self, nums):
        result = []
        accum = 0
        for n in nums:
            accum += n
            result.append(accum)
        return result


from spreadAnalysis.reqs.req import Req
from spreadAnalysis.utils.link_utils import LinkCleaner
import spreadAnalysis.utils.helpers as hlp
from datetime import datetime
import random
import pandas as pd

class Reddit:

	def __init__(self):

		self.base_url = "https://api.pushshift.io/reddit"

	def _update_output(self,res,output_data):

		if res is not None and res.ok:
			for e in res.json()["data"]:
				output_data["output"].append(e)
		return output_data

	def _get_data(self,data,call_url,params,wait_time=0,add_param_to_paginate=None):

		res = Req.get_response(call_url,params=params,fail_wait_time=40,wait_time=wait_time)
		data = self._update_output(res,data)

		return data

	def url_referals(self,url,start_date=None,end_date=None):

		data = {"input":url,
				"input_type":"link",
				"output":[],
				"method":"reddit"}
		start_date, end_date = hlp.get_default_dates(start_date,end_date)
		search_urls = [url]
		if "youtube." in url and "watch?v=" in url:
			search_urls.append(url.split("watch?v=")[-1].split("&")[0])
		for search_url in search_urls:
			for call_url in [self.base_url+"/search/comment",self.base_url+"/search/submission"]:
				params = {"q":search_url,
							"before":int(hlp.to_default_date_format(end_date).replace().timestamp()),
							"after":int(hlp.to_default_date_format(start_date).replace().timestamp()),
							"size":500}
				data = self._get_data(data,call_url,params,wait_time=2.19)

		return data

	def domain_referals(self,domain,start_date=None,end_date=None,full=True,max_results=None,interval=400,only_in_domain_urls=True):

		new_data = {}
		start_date, end_date = hlp.get_default_dates(start_date,end_date)
		initial_results = 100
		while initial_results >= 100:
			if interval < 1: interval = 1
			for sd,ed in hlp.create_date_ranges(start_date,end_date,interval=interval)[:1]:
				dom_data = self.url_referals(domain,start_date=sd,end_date=ed)
				initial_results = len(dom_data["output"])
				if initial_results >= 100:
					interval = int(interval/2)
				else:
					break
				if interval < 4:
					break
		result_count = 0
		for start_date,end_date in hlp.create_date_ranges(start_date,end_date,interval=interval):
			dom_data = self.url_referals(domain,start_date=start_date,end_date=end_date)
			for doc in dom_data["output"]:
				new_url = None
				if "selftext" in doc:
					url_list = LinkCleaner().get_url_list_from_text(str(doc["selftext"]))
				elif "body" in doc:
					url_list = LinkCleaner().get_url_list_from_text(str(doc["body"]))
				if len(url_list) > 0:
					for turl in url_list:
						if turl is not None and only_in_domain_urls and LinkCleaner().remove_url_prefix(str(domain)) in str(turl):
							new_url = LinkCleaner().single_clean_url(turl)
							break
						elif only_in_domain_urls == False and turl is not None:
							new_url = LinkCleaner().single_clean_url(turl)
							break
				if new_url is not None:
					if new_url not in new_data:
						new_data.update({new_url:{"input":new_url,
									"input_type":"link",
									"output":[],
									"method":"reddit"}})
					new_data[new_url]["output"].append(doc)
			result_count+=len(dom_data["output"])
			if max_results is not None and result_count > max_results:
				break
		new_data = list(new_data.values())
		return new_data

	def query_content(self,query,start_date=None,end_date=None,max_results=None,interval=400,only_in_domain_urls=True):

		new_data = {"input":query,
					"input_type":"query",
					"output":[],
					"method":"reddit"}
		start_date, end_date = hlp.get_default_dates(start_date,end_date)
		initial_results = 100
		while initial_results >= 100:
			for sd,ed in hlp.create_date_ranges(start_date,end_date,interval=interval)[:1]:
				dom_data = self.url_referals(query,start_date=sd,end_date=ed)
				initial_results = len(dom_data["output"])
				if initial_results >= 100:
					interval = int(interval/2)
				else:
					break
				if interval < 4:
					break
		result_count = 0
		for start_date,end_date in hlp.create_date_ranges(start_date,end_date,interval=interval):
			dom_data = self.url_referals(query,start_date=start_date,end_date=end_date)
			for doc in dom_data["output"]:
				new_data["output"].append(doc)
			result_count+=len(dom_data["output"])
			if max_results is not None and result_count > max_results:
				break
		return new_data


import random

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ' ']

alphabetExt = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
               'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1',
               '2', '3', '4', '5', '6', '7', '8', '9', '0', ' ']

def generateZero(size, filePath):
    content = ''
    for i in range(size):
        content += (alphabet[random.randint(0, len(alphabet) - 1)])
    open(filePath, 'w+').write(content)

def computeSignsProbabilities(filePath):
    counterList = []
    for i in range(len(alphabetExt)):
        counterList.append(0)
    inFile = open(filePath, 'r')
    for line in inFile.readlines():
        for sign in line:
            counterList[alphabetExt.index(sign)] += 1
    sumOfSigns = sum(counterList)
    signsProbs = []
    for i in range(len(alphabetExt)):
        signsProbs.append([alphabetExt[i], counterList[i] / sumOfSigns])
    signsProbs.sort(key=lambda elem: elem[1], reverse=True)
    return signsProbs

def generateOne(size, signsProbs, filePath):
    rangesList = []
    for i in range(len(signsProbs) + 1):
        rangesList.append(0.0)
    for i in range(1, len(rangesList)):
        for j in range(i, len(rangesList)):
            rangesList[j] += signsProbs[i - 1][1]
    rangesList[-1] = 1
    content = ''
    for i in range(size):
        rand = random.random()
        randIndex = 0
        for j in range(len(rangesList) - 1):
            if rangesList[j] <= rand <= rangesList[j + 1]:
                randIndex = j
                break
        content += signsProbs[randIndex][0]
    open(filePath, 'w+').write(content)

# visualization of local 'markov' dict
# {
#     'a': {
#         'ab': 0.023526,
#         'ac': 0.015322,
#         'ad': 0.005354,
#         ...
#     }
#     ...
# }
def computeMarkovSource(inFilePath, markovLevel):
    markov = {}
    inFile = open(inFilePath, 'r')
    for line in inFile.readlines():
        for i in range(len(line) - markovLevel):
            signs = line[i:i + markovLevel]
            signsWithTail = line[i:i + markovLevel + 1]
            if signs not in markov:
                markov[signs] = {}
            if signsWithTail not in markov[signs]:
                markov[signs][signsWithTail] = 1
            else:
                markov[signs][signsWithTail] += 1
    for k, v in markov.items():
        sum = 0
        for k1, v1 in v.items():
            sum += v1
        markov[k] = {k1: v1 for k1, v1 in sorted({k2: v2 / sum for k2, v2 in v.items()}.items(), key=lambda item: item[1], reverse=True)}
    return markov

def generateUsingMarkov(size, markov, outFilePath):
    markovSize = len([k for k, v in markov.items()][0])
    rangeMarkov = {}
    for k, v in markov.items():
        rangeMarkov[k] = []
        probs = [v1 for k1, v1 in v.items()]
        for i in range(len(probs) + 1):
            rangeMarkov[k].append(0.0)
        for i in range(1, len(rangeMarkov[k])):
            for j in range(i, len(rangeMarkov[k])):
                rangeMarkov[k][j] += probs[i - 1]
    content = ''
    startString = 'probability'[-markovSize:]
    currentString = startString
    for i in range(size):
        rand = random.random()
        randIndex = 0
        for j in range(len(rangeMarkov[currentString]) - 1):
            if rangeMarkov[currentString][j] <= rand <= rangeMarkov[currentString][j + 1]:
                randIndex = j
                break
        content += [k for k, v in markov[currentString].items()][randIndex][-1]
        if len(content) < markovSize:
            currentString = startString[-(markovSize - len(content)):] + content
        else:
            currentString = content[-markovSize:]
    open(outFilePath, 'w+').write(content)

def countAvgWordLength(filePath):
    alphabetWithoutSpace = alphabetExt[0:-1]
    file = open(filePath, 'r')
    wordCount = 0
    wordsLengths = []
    wordStarted = False
    for line in file.readlines():
        for char in line:
            if char in alphabetWithoutSpace:
                if not wordStarted:
                    wordCount += 1
                    wordStarted = True
                    wordsLengths.append(1)
                else:
                    wordsLengths[wordCount - 1] += 1
            elif char == ' ':
                wordStarted = False
    return sum(wordsLengths)/wordCount

def printMarkovSource(markovSource):
    for k, v in markovSource.items():
        print('\'%s\': {' % k)
        for k1, v1 in v.items():
            print('\t\'%s\': %f' % (k1, v1))
        print('}')

if __name__ == '__main__':

    print('zad1')
    # generateZero(10000000, 'generatorZero.txt')
    print('Average word length for generatorZero: %.2f' % countAvgWordLength('generatorZero.txt'))
    print()

    print('zad2')
    signProbabilities = computeSignsProbabilities('../data/norm_wiki_sample.txt')
    print('Probabilities for each sign (based on norm_wiki_sample.txt)')
    for sign, probability in signProbabilities:
        print('\'%s\': %s' % (sign, probability))
    print()

    print('zad3')
    generateOne(10000000, signProbabilities, 'generatorOne.txt')
    print('Average word length for generatorOne: %.2f' % countAvgWordLength('generatorOne.txt'))
    print('Average word length for norm_wiki_sample.txt: %.2f' % countAvgWordLength('../data/norm_wiki_sample.txt'))
    print()

    print('zad4')
    markovSource1 = computeMarkovSource('norm_wiki_sample.txt', 1)
    markovSource1ForTwoMostPopularSigns = {' ': markovSource1[' '], 'e': markovSource1['e']}
    printMarkovSource(markovSource1ForTwoMostPopularSigns)
    print()

    print('zad5')
    # printMarkovSource(markovSource1)
    generateUsingMarkov(10000000, markovSource1, 'generatorMarkov1.txt')
    print('Average word length for generatorMarkov1: %.2f' % countAvgWordLength('generatorMarkov1.txt'))

    markovSource3 = computeMarkovSource('norm_wiki_sample.txt', 3)
    # printMarkovSource(markovSource3)
    generateUsingMarkov(10000000, markovSource3, 'generatorMarkov3.txt')
    print('Average word length for generatorMarkov3: %.2f' % countAvgWordLength('generatorMarkov3.txt'))

    markovSource5 = computeMarkovSource('norm_wiki_sample.txt', 5)
    # printMarkovSource(markovSource5)
    generateUsingMarkov(10000000, markovSource5, 'generatorMarkov5.txt')
    print('Average word length for generatorMarkov5: %.2f' % countAvgWordLength('generatorMarkov5.txt'))


"""
    PyProfiler/tests/test_utils.py

"""
# Python Dependencies
import pytest

from pstats import SortKey

from PyProfiler import get_default_args
from PyProfiler.utils import default_arg
from PyProfiler.utils import check_keyword
from PyProfiler.utils import is_valid_mode
from PyProfiler.utils import is_valid_sortkey
from PyProfiler.errors import InvalidMode
from PyProfiler.errors import InvalidSortingMethod

from .functions import example
from .functions import example_2
from .functions import Example

@pytest.mark.parametrize("value, expected", [
    (SortKey.CALLS, True), ("calls", True),
    (SortKey.PCALLS, True), ("pcalls", True),
    (SortKey.CUMULATIVE, True), ("cumulative", True), ("cumtime", True),
    (SortKey.NFL, True), ("nfl", True),
    (SortKey.NAME, True), ("name", True),
    (SortKey.STDNAME, True), ("stdname", True),
    (SortKey.FILENAME, True), ("filename", True), ("module", True),
    (SortKey.LINE, True), ("line", True),
    (SortKey.TIME, True), ("time", True), ("tottime", True),
    pytest.param("INVALID", False, marks=pytest.mark.xfail(raises=InvalidSortingMethod)),
    ("file", True),
])
def test_sortkey(value, expected):
    assert is_valid_sortkey(value) is expected

@pytest.mark.parametrize("function, keyword, args, kwargs, expected", [
    # Function without Default Value
    (example, "debug", (), {"debug": True}, True),  # Keyword Arg is True
    (example, "debug", (0, 0, True), {}, True),  # Positional Arg is True

    (example, "debug", (0, 0, False), {}, False),  # Positional Arg is False
    (example, "debug", (), {}, False),  # No Default Value Specified
    (example, "verbose", (), {"verbose": True}, False),  # Wrong Keyword (Keyword not present)
    (example, "debug", (), {"debug": False}, False),  # Keyword Arg is False

    # Function with Default Value (True)
    (example_2, "verbose", (), {}, True),  # Default Value is True
    (example_2, "verbose", (0, 0, True), {}, True),  # Positional Arg is True

    (example_2, "verbose", (), {"verbose": False}, False),  # Keyword Arg is False
    (example_2, "verbose", (0, 0, False), {}, False),  # Positional Arg is False
    (example_2, "debug", (0, 0, True), {}, False),  # Wrong Keyword (Keyword not present)
    (example_2, "verbose", (0, 0, 1), {}, False),  # Positional Arg is not bool
    (example_2, "verbose", (0, 0), {"verbose": False}, False),  # Keyword Arg is False
    (example_2, "verbose", (0, 0), {"verbose": 1}, False),  # Positional Arg is not bool

    # Method with Default (True)
    (Example().magic, "profile", ("self", 0, True), {}, True),  # Positional Arg is True
    (Example().magic, "profile", ("self", 0), {}, True),  # Default Value is True
    (Example().magic, "profile", ("self",), {}, True),  # Default Value is True
    (Example().magic, "profile", ("self",), {"profile": True}, True),  # Keyword Arg is True

    (Example().magic, "profile", ("self",), {"profile": False}, False),  # Keyword Arg is False
    (Example().magic, "profile", ("self", 0, False), {}, False),  # Positional Arg is False
    (Example().magic, "wrong", ("self", 0, True), {}, False),  # Wrong Keyword (Keyword not present)
    (Example().magic, "profile", ("self", 0, 1), {}, False),  # Positional Arg is not bool

    # Class Method (wrapper) with Default (False)
    (Example().black, "debug", ("cls", 0, True), {}, True),  # Positional Arg is True
    (Example().black, "debug", ("cls", 0), {"debug": True}, True),  # Keyword Arg is True
    (Example().black, "debug", ("cls", 0, False), {"debug": True}, True),  # Keyword Arg is True

    (Example().black, "debug", ("cls", 0, False), {}, False),  # Positional Arg is False
    (Example().black, "debug", ("cls", 0, 1), {}, False),  # Positional Arg is not bool
    (Example().black, "debug", ("cls", 0), {}, False),  # Default Value is False
    (Example().black, "debug", ("cls",), {"debug": False}, False),  # Keyword Arg is False

    # Static Method (wrapper) without Default Value
    (Example().lady, "profile", (0, True), {}, True),  # Positional Arg is True
    (Example().lady, "profile", (), {"profile": True}, True),  # Keyword Arg is True

    (Example().lady, "profile", (0, False), {}, False),  # Positional Arg is False
    (Example().lady, "profile", (0,), {}, False),  # No Default Value --> False
    (Example().lady, "profile", (0, 1), {}, False),  # Positional Arg is not bool
    (Example().lady, "profile", (), {}, False),  # No Default Value --> False
    (Example().lady, "profile", (), {"profile": False}, False),  # Keyword Arg is False
])
def test_keyword(function, keyword, args, kwargs, expected):
    assert check_keyword(function, keyword, *args, **kwargs) is expected

@pytest.mark.parametrize("function, expected", [
    (example, dict(zip(["a", "b", "debug"], [None] * 3))),
    (example_2, dict(zip(["a", "b", "verbose"], [1, 2, True]))),
    (Example.magic, dict(zip(["self", "a", "profile"], [None, None, True]))),
    (Example.black, dict(zip(["b", "debug"], [None, False]))),  # classmethod wrapper modifies inspection of method
    (Example.lady, dict(zip(["a", "profile"], [None] * 2))),
])
def test_default_values(function, expected):
    assert get_default_args(function, None) == expected

@pytest.mark.parametrize("function, keyword, default, expected", [
    (example, "debug", True, True),
    (example, "debug",  False, False),
    (example_2, "verbose", True, True),
    (example_2, "verbose", False, True),
    (example_2, "", True, True),
    (example_2, "", False, False),
    (Example.magic, "profile", True, True),
    (Example.magic, "profile", False, True),
    (Example.magic, "", True, True),
    (Example.magic, "", False, False),
    (Example.black, "debug", False, False),
    (Example.black, "debug", True, False),
    (Example.black, "", False, False),
    (Example.black, "", True, True),
    (Example.lady, "profile", True, True),
    (Example.lady, "profile", False, False),
    (Example.lady, "", True, True),
    (Example.lady, "", False, False),
])
def test_default_keyvalue(function, keyword, default, expected):
    assert default_arg(function, keyword, default) is expected

@pytest.mark.parametrize("value, expected", [
    ("a", True),
    ("w", True),
    ("ab", True),
    ("wb", True),
    ("at", True),
    ("wt", True),
    pytest.param("r", False, marks=pytest.mark.xfail(raises=InvalidMode)),
    pytest.param("rb", False, marks=pytest.mark.xfail(raises=InvalidMode))
])
def test_mode(value, expected):
    assert is_valid_mode(value) is expected