import abc
import collections
import sys
import textwrap
if sys.version_info[:2] < (3, 2):
    from xml.sax.saxutils import escape
else:
    from html import escape

# Thanks to Nick Coghlan for these!
if sys.version_info[:2] >= (3, 3):
    class Renderer(metaclass=abc.ABCMeta):

        @classmethod
        def __subclasshook__(Class, Subclass):
            """可以在无须继承特定基类的前提下，创建出某套符合接口的对象"""
            if Class is Renderer:
                # 调用 Subclass.__mro__() 方法
                # 遍历 Subclass 及其超类的 Superclass.__dict__
                attributes = collections.ChainMap(
                    *(Superclass.__dict__ for Superclass in Subclass.__mro__))
                # 将待检测的方法放在一个元组中
                methods = ("header", "paragraph", "footer")
                # 遍历元组中的方法，判断这些方法是不是都在 attributes 映射表中
                if all(method in attributes for method in methods):
                    # 如果 methods 中每个方法都在 attributes 映射表里，那就返回 True
                    return True
            # 通过 Class 参数判断自己是不是在 Renderer 类上调用
            # 不是则返回 NotImplemented
            return NotImplemented
else:
    class Renderer(metaclass=abc.ABCMeta):

        @classmethod
        def __subclasshook__(Class, Subclass):
            if Class is Renderer:
                needed = {"header", "paragraph", "footer"}
                for Superclass in Subclass.__mro__:
                    for meth in needed.copy():
                        if meth in Superclass.__dict__:
                            needed.discard(meth)
                    if not needed:
                        return True
            return NotImplemented

MESSAGE = """This is a very short {} paragraph that demonstrates
the simple {} class."""

class Page:

    def __init__(self, title, renderer):
        if not isinstance(renderer, Renderer):
            raise TypeError("Expected object of type Renderer, got {}".
                            format(type(renderer).__name__))
        self.title = title
        self.renderer = renderer
        self.paragraphs = []

    def add_paragraph(self, paragraph):
        self.paragraphs.append(paragraph)

    def render(self):
        self.renderer.header(self.title)
        for paragraph in self.paragraphs:
            self.renderer.paragraph(paragraph)
        self.renderer.footer()

class TextRenderer:

    def __init__(self, width=80, file=sys.stdout):
        self.width = width
        self.file = file
        self.previous = False

    def header(self, title):
        """根据指定的宽度把标题输出到正中位置，然后换一行，在标题的每个字母下输出“=”字符"""
        self.file.write("{0:^{2}}\n{1:^{2}}\n".format(title,
                                                      "=" * len(title), self.width))

    def paragraph(self, text):
        """使用Python标准库的textwrap模块把文本段按照指定的宽度换行，并打印出来。"""
        if self.previous:
            self.file.write("\n")
        self.file.write(textwrap.fill(text, self.width))
        self.file.write("\n")
        self.previous = True

    def footer(self):
        pass

class HtmlWriter:

    def __init__(self, file=sys.stdout):
        self.file = file

    def header(self):
        self.file.write("<!doctype html>\n\n")

    def title(self, title):
        self.file.write("<title>{}</title>\n".format(
            escape(title)))

    def start_body(self):
        self.file.write("\n")

    def body(self, text):
        self.file.write("
{}
\n".format(escape(text)))

    def end_body(self):
        self.file.write("\n")

    def footer(self):
        self.file.write("\n")

class HtmlRenderer:

    def __init__(self, htmlWriter):
        self.htmlWriter = htmlWriter

    def header(self, title):
        self.htmlWriter.header()
        self.htmlWriter.title(title)
        self.htmlWriter.start_body()

    def paragraph(self, text):
        self.htmlWriter.body(text)

    def footer(self):
        self.htmlWriter.end_body()
        self.htmlWriter.footer()

def main():
    paragraph1 = MESSAGE.format("plain-text", "TextRenderer")
    paragraph2 = """This is another short paragraph just so that we can
see two paragraphs in action."""
    title = "Plain Text"
    textPage = Page(title, TextRenderer(22))
    textPage.add_paragraph(paragraph1)
    textPage.add_paragraph(paragraph2)
    textPage.render()

    print()

    paragraph1 = MESSAGE.format("HTML", "HtmlRenderer")
    title = "HTML"
    file = sys.stdout
    htmlPage = Page(title, HtmlRenderer(HtmlWriter(file)))
    htmlPage.add_paragraph(paragraph1)
    htmlPage.add_paragraph(paragraph2)
    htmlPage.render()

    try:
        page = Page(title, HtmlWriter())
        page.render()
        print("ERROR! rendering with an invalid renderer")
    except TypeError as err:
        print(err)

if __name__ == "__main__":
    main()


"""
Count the number of alternative alleles in
each individual of the passed vcf

usage:
    python count_derived_per_sample.py <VCF> <outfile.csv>
"""
import sys

import allel
import pandas as pd

def count_alternative(allele_counts):
    """
    Counts the number of alternative alleles
    Args:
        allele_counts: allel.AlleleCountsArray
    Returns: counts (int)
    """
    # 2nd column has the count for the alternative
    return allele_counts[:, 1].sum()

def main(callset, outfile):
    """
    Count the number of alternative alleles in
        each individual of the passed vcf
    Args:
        callset: VCF file
        outfile: output file with counts
    Returns:
        None, saves output file
    """

    vcf = allel.read_vcf(callset)
    samples_to_index = {s: [i] for i, s in enumerate(vcf['samples'])}

    g_arr = allel.GenotypeArray(vcf['calldata/GT'])
    counts_per_sample = g_arr.count_alleles_subpops(samples_to_index,
                                                    max_allele=1)

    counts_per_sample = [(x, count_alternative(counts_per_sample[x]))
                         for x in counts_per_sample]

    counts_per_sample = pd.DataFrame(counts_per_sample,
                                     columns=['sample', 'alt_counts'])

    counts_per_sample.to_csv(outfile, index=False)

if __name__ == '__main__':
    par1, par2 = sys.argv[1:]
    main(par1, par2)


from tkinter import *
Root=Tk()
Nombre=StringVar()
Frame=Frame(Root,width=500,height=500)
Frame.pack()
Frame.config(bg="grey")
LabelN=Label(Frame,text="Nombre")
LabelN.grid(row=0,column=0,sticky="w",padx=20,pady=20)
LabelA=Label(Frame,text="Apellido")
LabelA.grid(row=1,column=0,sticky="w",padx=20,pady=20)
LabelD=Label(Frame,text="Dirrecion")
LabelD.grid(row=2,column=0,sticky="w",padx=20,pady=20)
LabelP=Label(Frame,text="Contraseña")
LabelP.grid(row=3,column=0,sticky="w",padx=20,pady=20)
LabelC=Label(Frame,text="Comentarios")
LabelC.grid(row=4,column=0,sticky="w",padx=20,pady=20)

TextoC=Text(Frame,width=20, height=10)
TextoC.grid(row=4, column=1,padx=20,pady=20)

BarraD=Scrollbar(Frame,command=TextoC.yview)#Barra vertical
#BarraD=Scrollbar(Frame,command=TextoC.xview)#Barra horizontal
BarraD.grid(row=4,column=2,sticky="nswe")
TextoC.config(bg="green",bd=12,fg="white",yscrollcommand=BarraD.set)

CuadroN=Entry(Frame, textvariable=Nombre)
CuadroN.grid(row=0,column=1)
CuadroA=Entry(Frame)
CuadroA.grid(row=1,column=1)
CuadroD=Entry(Frame)
CuadroD.grid(row=2,column=1)
CuadroP=Entry(Frame)
CuadroP.config(show="*")
CuadroP.grid(row=3,column=1)
def PonerNombre():
    Nombre.set("Jaider Ivan Vera")

Boton=Button(Root,text="Enviar",command=PonerNombre)
Boton.pack()

Root.mainloop()


class Solution(object):
    def countingSort(self, nums, exp):
        counts = [0] * 10

        for num in nums:
            index = (num // exp) % 10
            counts[index] += 1

        for i in range(1, 10):
            counts[i] += counts[i-1]

        output = [0] * len(nums)
        for i in range(len(nums)-1, -1, -1):
            index = (nums[i] // exp) % 10
            output[counts[index]-1] = nums[i]
            counts[index] -= 1

        for i in range(len(nums)):
            nums[i] = output[i]

    def radix_sort(self, nums):
        if not nums:
            return nums

        largest = max(nums)

        exp = 1
        while largest / exp >= 1:
            self.countingSort(nums, exp)
            exp *= 10

        return nums

    def maximumGap(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums or len(nums) < 2:
            return 0

        nums = self.radix_sort(nums)

        gap = 0
        for i in range(1, len(nums)):
            gap = max(gap, nums[i]-nums[i-1])

        return gap

solution = Solution()
print(solution.maximumGap([3,6,9,1]))


import math
import torch
import torch.nn as nn
from costnet import CostNet
from stackedhourglass import *

class PSMnet(nn.Module):

    def __init__(self, max_disp):
        super().__init__()

        self.cost_net = CostNet()
        self.stackedhourglass = StackedHourglass(max_disp)
        self.D = max_disp

    def forward(self, left_img, right_img):
        original_size = [self.D, left_img.size(2), left_img.size(3)]

        left_cost = self.cost_net(left_img)  # [B, 32, 1/4H, 1/4W]
        right_cost = self.cost_net(right_img)  # [B, 32, 1/4H, 1/4W]

        B, C, H, W = left_cost.size()

        cost_volume = torch.zeros(B, C * 2, self.D // 4, H, W).type_as(left_cost)  # [B, 64, D, 1/4H, 1/4W]

        for i in range(self.D // 4):
            if i > 0:
                cost_volume[:, :C, i, :, i:] = left_cost[:, :, :, i:]
                cost_volume[:, C:, i, :, i:] = right_cost[:, :, :, :-i]
            else:
                cost_volume[:, :C, i, :, :] = left_cost
                cost_volume[:, C:, i, :, :] = right_cost

        disp1, disp2, disp3 = self.stackedhourglass(cost_volume, out_size=original_size)

        return disp1, disp2, disp3


import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision.models import resnet50

def D(p, z, version='simplified'): # negative cosine similarity
    if version == 'original':
        z = z.detach() # stop gradient
        p = F.normalize(p, dim=1) # l2-normalize
        z = F.normalize(z, dim=1) # l2-normalize
        return -(p*z).sum(dim=1).mean()

    elif version == 'simplified':# same thing, much faster. Scroll down, speed test in __main__
        return - F.cosine_similarity(p, z.detach(), dim=-1).mean()
    else:
        raise Exception

class projection_MLP(nn.Module):
    def __init__(self, in_dim, hidden_dim=2048, out_dim=2048):
        super().__init__()
        ''' page 3 baseline setting
        Projection MLP. The projection MLP (in f) has BN ap-
        plied to each fully-connected (fc) layer, including its out-
        put fc. Its output fc has no ReLU. The hidden fc is 2048-d.
        This MLP has 3 layers.
        '''
        self.layer1 = nn.Sequential(
            nn.Linear(in_dim, hidden_dim),
            nn.BatchNorm1d(hidden_dim),
            nn.ReLU(inplace=True)
        )
        self.layer2 = nn.Sequential(
            nn.Linear(hidden_dim, hidden_dim),
            nn.BatchNorm1d(hidden_dim),
            nn.ReLU(inplace=True)
        )
        self.layer3 = nn.Sequential(
            nn.Linear(hidden_dim, out_dim),
            nn.BatchNorm1d(hidden_dim)
        )
        self.num_layers = 3
    def set_layers(self, num_layers):
        self.num_layers = num_layers

    def forward(self, x):
        if self.num_layers == 3:
            x = self.layer1(x)
            x = self.layer2(x)
            x = self.layer3(x)
        elif self.num_layers == 2:
            x = self.layer1(x)
            x = self.layer3(x)
        else:
            raise Exception
        return x

class prediction_MLP(nn.Module):
    def __init__(self, in_dim=2048, hidden_dim=512, out_dim=2048): # bottleneck structure
        super().__init__()
        ''' page 3 baseline setting
        Prediction MLP. The prediction MLP (h) has BN applied
        to its hidden fc layers. Its output fc does not have BN
        (ablation in Sec. 4.4) or ReLU. This MLP has 2 layers.
        The dimension of h’s input and output (z and p) is d = 2048,
        and h’s hidden layer’s dimension is 512, making h a
        bottleneck structure (ablation in supplement).
        '''
        self.layer1 = nn.Sequential(
            nn.Linear(in_dim, hidden_dim),
            nn.BatchNorm1d(hidden_dim),
            nn.ReLU(inplace=True)
        )
        self.layer2 = nn.Linear(hidden_dim, out_dim)
        """
        Adding BN to the output of the prediction MLP h does not work
        well (Table 3d). We find that this is not about collapsing.
        The training is unstable and the loss oscillates.
        """

    def forward(self, x):
        x = self.layer1(x)
        x = self.layer2(x)
        return x

class SimSiam(nn.Module):
    def __init__(self, backbone=resnet50()):
        super().__init__()

        self.backbone = backbone
        self.projector = projection_MLP(backbone.output_dim)

        self.encoder = nn.Sequential( # f encoder
            self.backbone,
            self.projector
        )
        self.predictor = prediction_MLP()

    def forward(self, x1, x2):

        f, h = self.encoder, self.predictor
        z1, z2 = f(x1), f(x2)
        p1, p2 = h(z1), h(z2)
        L = D(p1, z2) / 2 + D(p2, z1) / 2
        return {'loss': L}

if __name__ == "__main__":
    model = SimSiam()
    x1 = torch.randn((2, 3, 224, 224))
    x2 = torch.randn_like(x1)

    model.forward(x1, x2).backward()
    print("forward backwork check")

    z1 = torch.randn((200, 2560))
    z2 = torch.randn_like(z1)
    import time
    tic = time.time()
    print(D(z1, z2, version='original'))
    toc = time.time()
    print(toc - tic)
    tic = time.time()
    print(D(z1, z2, version='simplified'))
    toc = time.time()
    print(toc - tic)

# Output:
# tensor(-0.0010)
# 0.005159854888916016
# tensor(-0.0010)
# 0.0014872550964355469


from os import environ
from flask import Flask, request, render_template

app = Flask(__name__, static_folder='static')

@app.route('/')
def index_page():
    return render_template('index.html')

@app.route('/choose', methods=['GET', 'POST'])
def choose_page():
    if request.method == 'GET':
        return render_template('choose.html')

    if request.form.get('type') == 'powers':
        range = int(request.form.get('range', 0))

        if 10 <= range <= 20:
            return render_template('powers.html', max_power=range, use_input=request.form.get('use-input'))

        return render_template('choose.html', error=True)

    system_from = int(request.form.get('system-from') or 0)
    system_to = int(request.form.get('system-to') or 0)

    if 2 <= system_from <= 36 and 2 <= system_to <= 36 and system_from != system_to:
        return render_template('systems.html', system_from=system_from, system_to=system_to)

    return render_template('choose.html', error=True)

if __name__ == '__main__':
    if 'ANDROID_STORAGE' not in environ:
        import webview
        webview.create_window('Informatics Training', app, width=780, height=480)
        webview.start()
    else:
        app.run()


import requests

class Food:

    def __init__(self):
        """
        Initializes the drink object with the url for a random drink
        args: None
        return: None
        """
        self.url = "https://www.themealdb.com/api/json/v1/1/random.php"

    def get(self):
        """
        Gets data from the url and returns it
        args: None
        return: (list) list with keys and values from the url including the name, recipe, and ingredients
        """
        response = requests.get(self.url)
        self.data = response.json()
        if self.data.get('meals'):
            self.var = self.data['meals']
            return self.data['meals']
        else:
            return None

    def ingredients(self, data1):
        """
        Prints ingredients in the meal
        args: (dictionary) dictionary containing the keys and values for the different ingredients
        return: None
        """
        data = data1
        print("")
        print("Meal Ingredients:")
        for _ in range(20):
         key = "strIngredient" + str(_ + 1)
         ingredients = data[key]
         if ingredients == None or ingredients == "":
             break
         print(ingredients)

    def recipe(self, data1):
        """
        Prints the recipe for the meal
        args: (dictionary) dictionary containing the string for the meal recipe
        return: None
        """
        data = data1
        print("")
        print("Meal Recipe:")
        instructions1 = data['strInstructions']
        print(instructions1)


#-*- coding: utf-8 -*-
#引用函數庫

import tensorflow as tf
import numpy as np
import pandas as pd
import pdb
import matplotlib.pyplot as plt

#定義layer
def add_layer(inputs, in_size, out_size, activation_function=None):
    Weights = tf.Variable(tf.random_normal([in_size, out_size]))
    biases = tf.Variable(tf.zeros([1, out_size]) + 0.1)
    Wx_plus_b = tf.matmul(inputs, Weights) + biases

    #自由選擇激活函數
    if activation_function is None:
        outputs = Wx_plus_b
    else:
        outputs = activation_function(Wx_plus_b)
    return outputs

# 給tensorflow 一個placeholder 隨時置換數據 None 表示會自己計算出放了多少組數據
# 像這裡 None 就會自動放入300組 因為我們等等會放入300組數據訓練
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])

#組裝神經網路
# add hidden layer
l1 = add_layer(xs, 1,30, activation_function=tf.nn.relu)
# add output layer
prediction = add_layer(l1, 30, 1, activation_function=None)

#接下來製造一些數據和雜訊吧
#製造出範圍為-1~1之間的 row:300 col:1 矩陣
#x_data = np.random.rand(300)
#x_data = x_data.reshape(len(x_data), 1)
f=open('201707_F3_1_8_2454.csv')
df=pd.read_csv(f)
listData=[]
for ele in df['Price']:
    listData.append(ele)
#listData=[2102.38,2117.91,2127.94,2117.28,2128.58,2141.6,2166.17,2158.15,2160.58,2157.31,2155.51,2161.94,2163.66,2140.96,2170.61,2183.41,2226.85,2227.34,2230.29,2203.29,2211.03,2228.11,2240.25,2242.98,2228.87,2214.59,2212.35,2200.02,2176.3,2165.78]
#x_data = np.asarray( [3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167, 7.042, 10.791, 5.313, 7.997, 5.654, 9.27, 3.1])[:, np.newaxis]
listDataX=[]
for i in range(len(listData)):
    listDataX.append(i)
x_data = np.asarray( listDataX)[:, np.newaxis]

noise = np.random.normal(0, 1, x_data.shape)

#製造出要讓網路學習的Y 並加上雜訊
y_data= np.asarray( listData)[:, np.newaxis]- 1 + noise

#x_data = np.linspace(-1,1,300)[:, np.newaxis]

#製造出要讓網路學習的Y 並加上雜訊
#y_data = np.square(x_data) - 0.5 + noise
#y_data = np.asarray( [1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221, 2.827, 3.465, 1.65, 2.904, 2.42, 2.94, 1.3])[:, np.newaxis]

# 定義loss function 並且選擇減低loss 的函數 這裡選擇GradientDescentOptimizer
# 其他方法再這裡可以找到 https://www.tensorflow.org/versions/r0.10/api_docs/python/train.html
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))
#train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

#loss = tf.reduce_mean(tf.square(ys))
optimizer =tf.train.AdamOptimizer()
train_step=optimizer.minimize(loss)

#全部設定好了之後 記得初始化喔
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)

# 為了可以可視化我們訓練的結果
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data, y_data)
plt.ion()
plt.show()

#宣告儲存模型
saver=tf.train.Saver(tf.global_variables())

#載入模型
#saver.restore(sess, 'modle.ckpt')

# 之後就可以用for迴圈訓練了
for i in range(50000):

     # 整個訓練最核心的code , feed_dict 表示餵入 輸入與輸出
     # x_data:[300,1]   y_data:[300,1]
    _,loss_value=sess.run([train_step,loss], feed_dict={xs: x_data, ys: y_data})
    if i % 50 == 0:
        # 畫出下一條線之前 必須把前一條線刪除掉 不然會看不出學習成果
        try:
            ax.lines.remove(lines[0])
        except Exception:
            pass
        plt.ylim(200,400)
        # 要取出預測的數值 必須再run 一次才能取出
        prediction_value = sess.run(prediction, feed_dict={xs: x_data})
        # 每隔0.1 秒畫出來
        lines = ax.plot(x_data, prediction_value, 'r-', lw=5,label='data')
        #ax.plot(x_data, prediction_value, 'r-', lw=5,label='data')
        plt.title('use '+str(i)+' Epoch '+"Loss:"+str(loss_value))
        plt.pause(0.01)

    if i % 500== 0:
        print("model_save",saver.save(sess,'modle'))

#saver.restore(sess, 'modle.ckpt')

plt.pause(999)


#Mandy is a working artist and is commissioned to create some art pieces for 2018.
#The client wants Mandy to create art pieces out of cardboard boxes to display at the local art show.
#Occasionally, Mandy plans to drive by the local store on her way home to grab some boxes for her art pieces.
#
#Mandy's cat, Willow, likes to play with every box that Mandy brings home. Willow plays with a box for T days before getting bored of it. Once Willow is bored with a box, she never returns to it again, meaning that Mandy can finally use the empty box in her art projects.
#
#If Mandy brings home another box before Willow finishes playing with the previous one,
#Willow will wait until she is bored with the previous box before moving onto the new one.
#
#Given Mandy's box-shopping habits over the next N days, can you determine by how many days the project will be delayed due to Willow?
#Input Specification
#
#The standard input will contain 10 datasets.
#
#Each dataset begins with two integers T ( 2 ≤ T ≤ 7 ) and N ( 1 ≤ N ≤ 365 ) .
#The next N lines each contain either the letter E or B which represent whether Mandy came home empty-handed or with a box that day.
#Output Specification
#
#For each dataset, output the number of days that Willow will be playing with the boxes after the N days given in the dataset.

# Get the input
for i in range(10):
    first_line = input()
    t, n = map(int, first_line.split(' '))
    mandy_actions = []
    for i in range(n):
        action = input()
        mandy_actions.append(action)
# Get the day when willow finishes playing with a box
    last_play_day = 0
    # Go through Mandy's list and find the last day Willow plays with each box in the list
    for i in range(len(mandy_actions)):
        if mandy_actions[i] == 'B':
            if i == 0:
                last_play_day = t - 1
            else:
                if i <= last_play_day:
                    last_play_day += t
                elif i > last_play_day:
                    last_play_day = i + (t - 1)
    if last_play_day <= len(mandy_actions) - 1:
        print(0)
    else:
        print(last_play_day - len(mandy_actions) + 1)


# standard DS imports
import pandas as pd
import numpy as np

# for feature selection verification and evaluation
from sklearn.metrics import r2_score, mean_squared_error, explained_variance_score
from sklearn.feature_selection import SelectKBest, RFE, f_regression, SequentialFeatureSelector
from sklearn.linear_model import LinearRegression, LassoLars, TweedieRegressor
from sklearn.preprocessing import PolynomialFeatures
from sklearn.cluster import KMeans
from sklearn.preprocessing import MinMaxScaler

def get_models(train, val, test):
    '''
    This function takes in train, validate, and test dataframes, scales them,
    and returns the results for the below models as well as the X and y values.

    Models ran:
    - Baseline (mean)
    - OLS/Linear Regression
    - LassoLars (alpha=1)
    - Quadratic Polynomial Regression
    - Cubic Polynomial Regression
    '''
    # identify which features to scale
    cols_to_scale = ['baths', 'beds', 'living_space', 'county', 'lat', 'long', 'lotsize', 'pool', 'city_id','year_built', 'tax_value', 'price_sqft',
                    'mvp_0', 'mvp_1', 'mvp_2', 'mvp_3',
                    'value_0', 'value_1', 'value_2',
                    'size_0', 'size_1', 'size_2']
    # create copies to not mess with the original datasets
    train_scaled = train.copy()
    val_scaled = val.copy()
    test_scaled = test.copy()

    # create the scaler and fit it
    scaler = MinMaxScaler()
    scaler.fit(train[cols_to_scale])

    # use the scaler to scale the data and resave
    train_scaled[cols_to_scale] = pd.DataFrame(scaler.transform(train[cols_to_scale]),
                                               columns = train[cols_to_scale].columns.values).set_index([train.index.values])
    val_scaled[cols_to_scale] = pd.DataFrame(scaler.transform(val[cols_to_scale]),
                                               columns = val[cols_to_scale].columns.values).set_index([val.index.values])
    test_scaled[cols_to_scale] = pd.DataFrame(scaler.transform(test[cols_to_scale]),
                                               columns = test[cols_to_scale].columns.values).set_index([test.index.values])

    # create X and y from the scaled data, select features to be used in modeling
    X_train, y_train = train_scaled[['baths', 'beds', 'living_space', 'lat', 'long', 'county', 'lotsize', 'year_built', 'tax_value', 'price_sqft',
                    'mvp_0', 'mvp_2', 'value_2',
                                    ]], train.logerror
    X_val, y_val = val_scaled[['baths', 'beds', 'living_space', 'lat', 'long', 'county', 'lotsize', 'year_built', 'tax_value', 'price_sqft',
                    'mvp_0', 'mvp_2', 'value_2',
                              ]], val.logerror
    X_test, y_test = test_scaled[['baths', 'beds', 'living_space', 'lat', 'long', 'county', 'lotsize', 'year_built', 'tax_value', 'price_sqft',
                    'mvp_0', 'mvp_2', 'value_2',
                                 ]], test.logerror

    # make the y's into DataFrames
    y_train = pd.DataFrame(y_train)
    y_val = pd.DataFrame(y_val)
    y_test = pd.DataFrame(y_test)

    # Baseline Model
    # run the model
    pred_mean = y_train.logerror.mean()
    y_train['pred_mean'] = pred_mean
    y_val['pred_mean'] = pred_mean
    y_test['pred_mean'] = pred_mean
    rmse_train = mean_squared_error(y_train.logerror, y_train.pred_mean, squared=False)
    rmse_val = mean_squared_error(y_val.logerror, y_val.pred_mean, squared=False)

    # save the results
    metrics = pd.DataFrame(data=[{
        'model': 'baseline_mean',
        'rmse_train': rmse_train,
        'r2_train': explained_variance_score(y_train.logerror, y_train.pred_mean),
        'rmse_val': rmse_val,
        'r2_val': explained_variance_score(y_val.logerror, y_val.pred_mean)
    }])

    # Linear Regression - OLS model
    # run the model
    lm = LinearRegression()
    lm.fit(X_train, y_train.logerror)
    y_train['pred_lm'] = lm.predict(X_train)
    rmse_train = mean_squared_error(y_train.logerror, y_train.pred_lm, squared=False)
    y_val['pred_lm'] = lm.predict(X_val)
    rmse_val = mean_squared_error(y_val.logerror, y_val.pred_lm, squared=False)
    # save the results
    metrics = metrics.append({
        'model': 'OLS',
        'rmse_train': rmse_train,
        'r2_train': explained_variance_score(y_train.logerror, y_train.pred_lm),
        'rmse_val': rmse_val,
        'r2_val': explained_variance_score(y_val.logerror, y_val.pred_lm)
        }, ignore_index=True)

    # LassoLars Model
    # run the model
    lars = LassoLars(alpha=1)
    lars.fit(X_train, y_train.logerror)
    y_train['pred_lars'] = lars.predict(X_train)
    rmse_train = mean_squared_error(y_train.logerror, y_train.pred_lars, squared=False)
    y_val['pred_lars'] = lars.predict(X_val)
    rmse_val = mean_squared_error(y_val.logerror, y_val.pred_lars, squared=False)
    # save the results
    metrics = metrics.append({
        'model': 'LarsLasso, alpha 1',
        'rmse_train': rmse_train,
        'r2_train': explained_variance_score(y_train.logerror, y_train.pred_lars),
        'rmse_val': rmse_val,
        'r2_val': explained_variance_score(y_val.logerror, y_val.pred_lars
        )}, ignore_index=True)

    # Polynomial Models
    # set up the model
    pf = PolynomialFeatures(degree=2)
    X_train_d2 = pf.fit_transform(X_train)
    X_val_d2 = pf.transform(X_val)

    # run the model
    lm2 = LinearRegression()
    lm2.fit(X_train_d2, y_train.logerror)
    y_train['pred_lm2'] = lm2.predict(X_train_d2)
    rmse_train = mean_squared_error(y_train.logerror, y_train.pred_lm2, squared=False)
    y_val['pred_lm2'] = lm2.predict(X_val_d2)
    rmse_val = mean_squared_error(y_val.logerror, y_val.pred_lm2, squared=False)
    # save the results
    metrics = metrics.append({
        'model': 'Quadratic Linear Regression',
        'rmse_train': rmse_train,
        'r2_train': explained_variance_score(y_train.logerror, y_train.pred_lm2),
        'rmse_val': rmse_val,
        'r2_val': explained_variance_score(y_val.logerror, y_val.pred_lm2)
    }, ignore_index=True)

    # set up the model
    pf = PolynomialFeatures(degree=3)
    X_train_d2 = pf.fit_transform(X_train)
    X_val_d2 = pf.transform(X_val)
    # run the model
    lm2 = LinearRegression()
    lm2.fit(X_train_d2, y_train.logerror)
    y_train['pred_lm2'] = lm2.predict(X_train_d2)
    rmse_train = mean_squared_error(y_train.logerror, y_train.pred_lm2, squared=False)
    y_val['pred_lm2'] = lm2.predict(X_val_d2)
    rmse_val = mean_squared_error(y_val.logerror, y_val.pred_lm2, squared=False)
    # save the results
    metrics = metrics.append({
        'model': 'Cubic Linear Regression',
        'rmse_train': rmse_train,
        'r2_train': explained_variance_score(y_train.logerror, y_train.pred_lm2),
        'rmse_val': rmse_val,
        'r2_val': explained_variance_score(y_val.logerror, y_val.pred_lm2)
    }, ignore_index=True)

    return metrics, X_train, y_train, X_val, y_val, X_test, y_test

def run_test(X_train, y_train, X_val, y_val, X_test, y_test):
    '''
    This function takes in the X and y values for train, validate, and test
    and returns the results in a DataFrame for the zillow clustering project's
    best model: Linear Regression/OLS
    '''
    # run the model
    lm = LinearRegression()
    lm.fit(X_train, y_train.logerror)
    y_train['pred_lm'] = lm.predict(X_train)
    rmse_train = mean_squared_error(y_train.logerror, y_train.pred_lm, squared=False)
    y_val['pred_lm'] = lm.predict(X_val)
    rmse_val = mean_squared_error(y_val.logerror, y_val.pred_lm, squared=False)
    y_test['pred_lm'] = lm.predict(X_test)
    rmse_test = mean_squared_error(y_test.logerror, y_test.pred_lm, squared=False)

    # save the results
    results = pd.DataFrame({'train':
                               {
                                'rmse': rmse_train,
                                'r2': explained_variance_score(y_train.logerror, y_train.pred_lm)},
                           'validate':
                               {
                                'rmse': rmse_val,
                                'r2': explained_variance_score(y_val.logerror, y_val.pred_lm)},
                           'test':
                               {
                                'rmse': rmse_test,
                                'r2': explained_variance_score(y_test.logerror, y_test.pred_lm)}
                          })
    return results.T


import argparse
import asyncio
import os
import sys
import time
from datetime import datetime

import aioredis
import aiosqlite
import ujson as json

def redis_connection(host, port, db_id):
    """
    Creates a connection to a Redis instance
    """
    return aioredis.Redis(host=host, port=port, db=db_id)

class SQLite3Ops(object):
    def __init__(self, db_path):
        self._db = aiosqlite.connect(db_path)
        self._db.row_factory = aiosqlite.Row

    async def execute(self, sql, args):
        cursor = await self._db.execute(sql, args)
        return cursor

    async def executemany(self, sql, args_rows):
        cursor = await self._db.executemany(sql, args_rows)
        return cursor

    async def commit(self):
        await self._db.commit()

class Archiver(object):
    """
    A class which actually performs the process of archiving records
    from fast database (Redis) to a slow one (SQLite3)
    """

    def __init__(self, from_db: aioredis.Redis, to_db: SQLite3Ops, archive_queue_key: str):
        """
        arguments:
        from_db -- a Redis connection
        to_db -- a SQLite3 connection
        archive_queue_key -- a Redis key used to access archive queue
        """
        self._from_db = from_db
        self._to_db = to_db
        self._archive_queue_key = archive_queue_key

    async def _get_queue_size(self):
        return await self._from_db.llen(self._archive_queue_key)

    async def run(self, num_proc, dry_run):
        """
        Performs actual archiving process according to the parameters passed
        in constructor.

        Please note that dry-run is not 100% error-prone as it also pops the items
        from the queue and then inserts them again.

        arguments:
        num_proc -- how many items per run should be processed
        dry_run -- if True then no writing operations are performed

        returns:
        a dict containing some information about processed data (num_processed,
        error, dry_run, queue_size)
        """
        curr_time = time.time()
        conc_prefix = 'concordance:'
        inserts = []
        i = 0
        try:
            while i < num_proc:
                qitem = await self._from_db.lpop(self._archive_queue_key)
                if qitem is None:
                    break
                qitem = json.loads(qitem)
                data = await self._from_db.get(qitem['key'])
                inserts.append((qitem['key'][len(conc_prefix):], data, curr_time, 0))
                i += 1

            if not dry_run:
                await self._to_db.executemany(
                    'INSERT OR IGNORE INTO archive (id, data, created, num_access) VALUES (?, ?, ?, ?)', inserts)
                await self._to_db.commit()
            else:
                for ins in reversed(inserts):
                    await self._from_db.lpush(self._archive_queue_key,
                                              json.dumps(dict(key=conc_prefix + ins[0])))
        except Exception as ex:
            for item in inserts:
                await self._from_db.rpush(self._archive_queue_key, json.dumps(
                    dict(key=conc_prefix + item[0])))
            return dict(
                num_processed=i,
                error=str(ex),
                dry_run=dry_run,
                queue_size=await self._get_queue_size())
        return dict(
            num_processed=i,
            error=None,
            dry_run=dry_run,
            queue_size=await self._get_queue_size())

async def run(conf, num_proc, dry_run):
    from_db = redis_connection(conf.get('plugins', 'db')['host'],
                               conf.get('plugins', 'db')['port'],
                               conf.get('plugins', 'db')['id'])
    to_db = SQLite3Ops(conf.get('plugins')['query_persistence']['archive_db_path'])

    archive_queue_key = conf.get('plugins')['query_persistence']['archive_queue_key']
    archiver = Archiver(from_db=from_db, to_db=to_db, archive_queue_key=archive_queue_key)
    return await archiver.run(num_proc, dry_run)

def _create_archive(conf, dry_run):
    arch_db_path = conf.get('plugins')['query_persistence']['archive_db_path']
    if os.path.exists(arch_db_path):
        arch_filename = os.path.splitext(os.path.basename(arch_db_path))[0]
        dt = time.strftime('%Y-%m-%d', datetime.now().timetuple())
        backed_up_arch_path = os.path.join(os.path.dirname(
            arch_db_path), arch_filename + '.' + dt + '.db')
        print(('Archive {0} already exists'.format(arch_db_path)))
        print(('Please rename the current file to {0} and the run the action again'.format(
            backed_up_arch_path)))
        sys.exit(1)
    sql = ('CREATE TABLE archive (id text, data text NOT NULL, created integer NOT NULL, num_access integer '
           'NOT NULL DEFAULT 0, last_access integer, PRIMARY KEY (id))')
    if dry_run:
        print(sql)
    else:
        to_db = SQLite3Ops(arch_db_path)
        to_db.execute(sql, ())
        to_db.commit()
        print(('Created a new concordance archive file {0}.'.format(arch_db_path)))
        print('Please do not forget to set proper ownership and read+write permissions')
        print('for web server user (www-data on Ubuntu)')
        print('When done, please restart KonText server and also worker server')
        print('')

if __name__ == '__main__':
    sys.path.insert(0, os.path.realpath('%s/../..' % os.path.dirname(os.path.realpath(__file__))))
    sys.path.insert(0, os.path.realpath('%s/../../../scripts/' %
                                        os.path.dirname(os.path.realpath(__file__))))
    import autoconf
    from action.plugin import initializer
    settings = autoconf.settings
    logger = autoconf.logger

    initializer.init_plugin('db')
    initializer.init_plugin('auth')

    parser = argparse.ArgumentParser(
        description='Archive old records from Redis to a SQLite3 database')
    parser.add_argument('action', metavar='ACTION', type=str, help='One of {new_archive, backup}')
    parser.add_argument('-n', '--num_proc', type=int, default=1000,
                        help='For backup, how many items to process. Default is 1000')
    parser.add_argument('-d', '--dry-run', action='store_true', default=False,
                        help=('allows running without affecting storage data '
                              '(not 100%% error prone as it reads/writes to Redis)'))
    args = parser.parse_args()
    if args.action == 'backup':
        ans = asyncio.run(run(conf=settings, num_proc=args.num_proc, dry_run=args.dry_run))
        print(ans)
    elif args.action == 'new_archive':
        try:
            _create_archive(conf=settings, dry_run=args.dry_run)
        except Exception as ex:
            print(('{0}: {1}'.format(ex.__class__.__name__, ex)))
            sys.exit(1)
    else:
        print(('Unknown action "{0}"'.format(args.action)))
        sys.exit(1)


"""Routines which are useful to create and plot graphs
"""
from typing import Set, Tuple, Optional, List, Any, Text

from numpy import abs, ndarray
from numpy import random as rand
from math import factorial
import matplotlib.pyplot as plt
from seaborn import heatmap

import networkx as nx
from networkx.algorithms import bipartite
from networkx.generators.random_graphs import (
    fast_gnp_random_graph,
    newman_watts_strogatz_graph,
)

def generate_graph(
    n_nodes: int, n_edges: int, n_edge_max: int = 5, seed: Optional[int] = None
) -> Set[Tuple[int]]:
    """Creates random edges for graph.

    Node labels are from 0 to n_nodes - 1

    Arguments:
        n_nodes: Number of nodes.
        n_edges: Number of edges.
        n_edge_max: Maximal number of edges for graph.
        seed: Random seed to set before graph is generated.

    Todo:
        What about connected / unconnected graphs?
        Am I creating the right graph?
        Think about ill input conditions.
    """
    assert n_edge_max > 0
    assert n_nodes * n_edge_max // 2 > n_edges

    if seed:
        rand.seed(42)

    nodes = {n: 0 for n in range(n_nodes)}
    edges = set()

    while len(edges) < n_edges:
        choices = [n for n, count in nodes.items() if count <= n_edge_max]
        e1, e2 = (int(e) for e in rand.choice(choices, size=2, replace=False))
        edge = (e2, e1) if e2 < e1 else (e1, e2)
        if edge not in edges:
            edges.add(edge)
            nodes[e1] += 1
            nodes[e2] += 2

    return edges

def get_plot(
    graph: Set[Tuple[int]],
    color_nodes: Optional[List[int]] = None,
    show_plot: bool = False,
    backend="mpl",
    seed: int = 42,
    directed: bool = False,
):
    """Creates graph plot from edge set.

    Arguments:
        graph: The graph to plot.
        color_nodes: Colors given nodes green, default is white.
        show_plot: Wether or not the plot should be rendered.
        backend: The plotting backend. Either "mpl" or "bokeh".
        directed: Plot graph as a directed graph
    """
    if backend == "mpl":
        fig = get_plot_mpl(
            graph,
            color_nodes=color_nodes,
            show_plot=show_plot,
            seed=seed,
            directed=directed,
        )
    elif backend == "bokeh":
        if directed:
            print("Directed graph not supported for bokeh")
        fig = get_plot_bokeh(graph, color_nodes=color_nodes, show_plot=show_plot)
    else:
        raise KeyError("Only implemented matplotlib ('mpl') or ")

    return fig

def _flatten(obj: Tuple[Any]) -> Tuple[Any]:
    """Flattens nested tuple of tuple to just a tuple.

    Returns iterator.

    Follows:
    https://symbiosisacademy.org/tutorial-index/python-flatten-nested-lists-tuples-sets/
    """
    for item in obj:
        if isinstance(item, tuple):
            yield from _flatten(item)
        else:
            yield item

def _v2str(v: Tuple[int]) -> str:
    """Maps nested tuple of ints to flat string.

    String is joined on '' if v_max < 9 else ','
    """
    if isinstance(v, tuple):
        vflat = list(_flatten(v))
        sep = "," if max(vflat) > 9 else ""
        out = sep.join(map(str, vflat))
    else:
        out = str(v)
    return out

def generate_hamming_graph(
    d: int, q: int, v_as_string: bool = False
) -> Tuple[Set[Tuple[str, str]], Text]:
    """Returns edges for hamming graph (caretesian product of complete graphs).

    See also http://mathworld.wolfram.com/HammingGraph.html

    Arguments:
        d: Dimension of graph (number of copies).
        q: Number of nodes for complete graph used in cartesian product.
        v_as_string: Convert vertices to strings of format "v10v11v12...".
    """
    kq = nx.complete_graph(q)
    graph = kq
    for _ in range(d - 1):
        graph = nx.cartesian_product(graph, kq)

    edges = set((_v2str(v1), _v2str(v2)) for v1, v2 in graph.edges)

    if not v_as_string:
        nodes = dict()
        n_nodes = 0
        int_edges = set()
        for v1, v2 in edges:
            if v1 not in nodes:
                nodes[v1] = n_nodes
                n_nodes += 1
            if v2 not in nodes:
                nodes[v2] = n_nodes
                n_nodes += 1

            int_edges.add((nodes[v1], nodes[v2]))
        edges = int_edges

    return edges, f"Hamming({d},{q})"

def generate_bipartite_graph(p: int, q: int) -> Tuple[Set[Tuple[int, int]], Text]:
    """Returns edges of a complete bipartite graph.

    K(4,4) is the fundamental unit of a Chimera graph.

    Arguments:
        p: Number of vertices in set 1
        q: Number of vertices in set 2
    """

    return set(bipartite.complete_bipartite_graph(p, q).edges), f"K({p},{q})"

def generate_erdos_renyi_graph(n: int, p: float) -> Tuple[Set[Tuple[int, int]], Text]:
    """Return edges of a Erdos Renyi graph.

    A generalized random graph.
    Can be disconnected

    The following statements are true on average

    ``
        np < 1 has O(log(n)) clusters
        np = 1 has O(n^2/3) clusters
        np -> c > 1 will cluster to one large component, sub clusters have O(log(n)) vertices

        p < ln(n)/n the graph will be disconnected
        p > ln(n)/n the graph will be connected
    ``

    G(n,p)

    Arguments:
        n: Number of vertices
        p: Probability of edge creation
    """
    G = fast_gnp_random_graph(n, p)
    return set(G.edges), f"G({n},{p})"

def generate_newman_watts_strogatz_graph(
    n: int, k: int, p: float
) -> Tuple[Set[Tuple[int, int]], Text]:
    """Returns edges of a NWS small-world graph.

    NWS graph is a knn ring graph + random connections.
    This is guaranteed to be connected since edges are not removed for shortcuts.
    The WS graph removes edges from the ring when spawning shortcuts.
    Has applications to modeling social networks.

    Arguments:
        n: Number of vertices
        k: Number of nearest neighbors
        p: Probability of spawning new edge on top of base ring

    """
    return set(newman_watts_strogatz_graph(n, k, p).edges), f"WS({n},{k},{p})"

def generate_nn_graph(v: int) -> Tuple[Set[Tuple[int, int]], Text]:
    """Returns edges of a 1 dimensional nearest neighbor graph.

    Arugments:
        v: Number of vertices
    """
    graph = {(i - 1, i) for i in range(1, v)}
    return graph, f"NN({v})"

def generate_corona_graph(k: int, n: int) -> Tuple[Set[Tuple[int, int]], Text]:
    """Returns edges of a n-shortcut 3k-ring graph with broken rotational symmetry.

    Has unique ground state solution with increased connectivity

    Arguments:
        k: number of 3 vertex segments
        n: number of shortcuts. must be < 3*k - 2 for this definition
    """

    if n >= factorial(3 * k - 2):
        raise ValueError(
            "n >= (3k-2)!: More shortcuts than allowed for graph.\n Choose a smaller number."
        )

    # construct base ring
    ring, _ = generate_nn_graph(3 * k)
    ring.add((3 * k - 1, 0))

    # add antigen
    antigen = ((3 * i + 1, 3 * k + i) for i in range(k))
    ring.update(antigen)

    # add shortcuts
    count = 0
    for nidx in range(k):
        for kidx in range(3 * k):
            if count < n:
                start = kidx
                end = (kidx + nidx + 2) % (3 * k)
                ring.add((start, end))
                count += 1
            else:
                break

    return ring, f"C19({k},{n})"

def generate_limited_corona_graph(k: int, n: int):
    """Returns edges of a n-shortcut 3k-ring graph with broken rotational symmetry.

    Has unique ground state solution with increased connectivity
    For limited corona, the shortcuts are added to keep max connections / vertex the smallest

    Arguments:
        k: number of 3 vertex segments
        n: number of shortcuts.
    """

    if n >= factorial(3 * k - 2):
        raise ValueError(
            "n >= (3k-2)!: More shortcuts than allowed for graph.\n Choose a smaller number."
        )

    # construct base ring
    ring, _ = generate_nn_graph(3 * k)
    ring.add((3 * k - 1, 0))

    # add antigen
    antigen = ((3 * i + 1, 3 * k + i) for i in range(k))
    ring.update(antigen)

    # connectivity
    connections = {idx: 2 for idx in range(3 * k)}
    for i in range(k):
        connections[3 * i + 1] += 1

    # add shortcuts
    shortcuts = [
        (kidx, (kidx + nidx + 2) % (3 * k))
        for nidx in range(k)
        for kidx in range(3 * k)
    ]
    count = 0
    minconnections = min(connections.values())
    while count < n:
        for idx, shortcut in enumerate(shortcuts):
            start = shortcut[0]
            end = shortcut[1]
            if connections[start] > minconnections or connections[end] > minconnections:
                deadend = True
                pass
            else:
                ring.add(shortcut)
                shortcuts.pop(idx)
                connections[start] += 1
                connections[end] += 1
                minconnections = min(connections.values())
                count += 1
                deadend = False
                break
        if idx == len(shortcuts) - 1 and deadend:
            minconnections += 1
    return ring, f"lC19({k},{n})"

def get_plot_mpl(
    graph: Set[Tuple[int]],
    color_nodes: Optional[List[int]] = None,
    show_plot: bool = False,
    ax: Optional["Axes"] = None,
    seed: int = 42,
    directed: bool = False,
) -> Optional["Figure"]:
    """Creates matplotlib graph plot from edge set.

    Arguments:
        graph: The graph to plot.
        color_nodes: Colors given nodes green, default is white.
        show_plot: Wether or not the plot should be rendered.
        ax: The axes to plot in
        directed: Plot graph as a directed graph
    """
    if not ax:
        fig, ax = plt.subplots()
    else:
        fig = None

    G = nx.DiGraph(list(graph)) if directed else nx.Graph(list(graph))
    node_color = (
        ["lightgreen" if node in color_nodes else "white" for node in G.nodes]
        if color_nodes
        else "white"
    )

    if seed is not None:
        pos = nx.spring_layout(G, seed=seed)
        # pos = nx.spectral_layout(G)

    nx.draw(
        G,
        pos,
        with_labels=True,
        node_color=node_color,
        font_size=10,
        node_shape="o",
        linewidths=1.0,
        edgecolors="black",
        ax=ax,
    )

    if show_plot:
        plt.show()

    return fig

def get_plot_bokeh(  # pylint: disable=R0914
    graph: Set[Tuple[int]],
    color_nodes: Optional[List[int]] = None,
    show_plot: bool = False,
) -> "Plot":
    """Creates bokeh graph plot from edge set.

    Arguments:
        graph: The graph to plot.
        color_nodes: Colors given nodes green, default is white.
        show_plot: Wether or not the plot should be rendered.
    """
    from bokeh.io import show
    from bokeh.models import Plot, Range1d, Circle, HoverTool
    from bokeh.models.graphs import from_networkx

    color_nodes = color_nodes or []

    G = nx.Graph(list(graph))
    edge_attrs = {}
    node_attrs = {}

    for start_node, end_node, _ in G.edges(data=True):
        edge_attrs[(start_node, end_node)] = "black"

    for node in G.nodes:
        node_attrs[node] = "green" if node in color_nodes else "white"

    nx.set_edge_attributes(G, edge_attrs, "edge_color")
    nx.set_node_attributes(G, node_attrs, "node_color")

    # Show with Bokeh
    plot = Plot(
        plot_width=400,
        plot_height=400,
        x_range=Range1d(-1.1, 1.1),
        y_range=Range1d(-1.1, 1.1),
    )

    graph_renderer = from_networkx(G, nx.circular_layout, scale=1, center=(0, 0))
    graph_renderer.node_renderer.glyph = Circle(size=15, fill_color="node_color")
    plot.renderers.append(graph_renderer)  # pylint: disable=E1101

    node_hover_tool = HoverTool(tooltips=[("index", "@index")])
    plot.add_tools(node_hover_tool)

    if show_plot:
        show(plot)

    return plot

def plot_qubo(
    qubo, ax: Optional["Axes"] = None, axes: bool = False, fontsize: int = 4, **kwargs
) -> "Axes":
    """Plots qubo as heat map

    Kwargs are fed to heatmap.
    """
    if not ax:
        ax = plt.gca()

    m = qubo.todense() if not isinstance(qubo, ndarray) else qubo
    vmax = abs(m).max()

    data = dict(
        annot=True,
        annot_kws={"fontsize": fontsize},
        cmap="RdBu_r",
        vmin=-vmax,
        vmax=vmax,
        mask=m == 0,
        cbar=False,
    )
    data.update(kwargs)

    heatmap(m, ax=ax, **data)

    if not axes:
        ax.set_yticks([])
        ax.set_xticks([])

    return ax

def main():
    """Creates a random graph (fixed seed) and plots it.
    """

    n_nodes = 5
    n_edges = 5
    n_edge_max = 3

    test_graph = generate_graph(n_nodes, n_edges, n_edge_max=n_edge_max, seed=42)
    get_plot(test_graph, show_plot=True)

if __name__ == "__main__":
    main()


import pytest
from application import create_app
from flask import session

@pytest.fixture()
def app():
    app = create_app()
    app.config.from_pyfile('config.py')
    app.config.update({
        "TESTING": True,
    })

    # other setup can go here

    yield app

    # clean up / reset resources here

@pytest.fixture()
def client(app):
    with app.test_client() as testing_client:
        with app.app_context():

            yield testing_client


import sys

sys.stdin = open('boj_14722.txt', 'r')

import sys
readlines = sys.stdin.readline

N = int(readlines())

field = [list(map(int, readlines().split())) for _ in range(N)]
dp = [[[0, 0] for _ in range(N)] for _ in range(N)]

if field[0][0] == 0:
    dp[0][0][0], dp[0][0][1] = 1, 1
else:
    dp[0][0][0], dp[0][0][1] = 0, 0

for y in range(N):
    for x in range(N):
        up_val = dp[y - 1][x] if y > -1 else (-1, -1)
        left_val = dp[y][x - 1] if x > -1 else (-1, -1)
        up_new_val = up_val[:]
        left_new_val = left_val[:]
        if field[y][x] == up_val[1]:
            up_new_val = (up_val[0] + 1, (up_val[1] + 1) % 3)

        if field[y][x] == left_new_val[1]:
            left_new_val = (left_val[0] + 1, (left_val[1] + 1) % 3)

        dp[y][x] = max(up_new_val, left_new_val, key=lambda x: x[0])

print(dp[N - 1][N - 1][0])


# Importing the Libraries

import pandas
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np

# Loading the dataset

data = pandas.read_csv(r'C:/Users/Shashi/Documents/DataSets/animal_category.csv')

data.dtypes

data.info()

data.describe()

# 1st B.M

data.mean()

data.median()

data.mode()

# 2nd B.M

data.var()

data.std()

# 3rd B.M

data.skew()

# 4th B.M

data.kurt()

# Graphical Representation

plt.boxplot(data)

plt.hist(data)

data.dtypes

duplicates = data.duplicated()
duplicates.sum()

# Dummy variables

#data.drop(['Index'], axis=1, inplace=True)

data_new = pandas.get_dummies(data)

data_new1 = pandas.get_dummies(data, drop_first = True)

# data.columns()

data = data[['Animals', 'Gender', 'Homly', 'Types']]

# One Hot Encoding

from sklearn.preprocessing import OneHotEncoder

enc = OneHotEncoder()

enc_data = pandas.DataFrame(enc.fit_transform(data.iloc[:,2:]).toarray())

# Label Encoding

from sklearn.preprocessing import LabelEncoder

labelencoder = LabelEncoder()

# Data splite into input, output labels

X = data.iloc[:,:4]
Y = data.iloc[:,1]

X['Animals'] = labelencoder.fit_transform(X['Animals'])

X['Gender'] = labelencoder.fit_transform(X['Gender'])

X['Homly'] = labelencoder.fit_transform(X['Homly'])

X['Types'] = labelencoder.fit_transform(X['Types'])


import os
import shutil
import subprocess

import pytest
from sos import execute_workflow
from sos.parser import SoS_Script
from sos.targets import file_target
from sos.utils import env

def test_progress_bar():
    # progress bar with nested workflow
    execute_workflow(
        '''
import time
time.sleep(0)
[sub_1]
[sub_2]
[sub_3]
[sub_4]
[a_1]
[a_2]
[a_3]
sos_run('sub')
[a_4]
[a_5]
''',
        workflow='a')

def test_nested_workflow(temp_factory, clear_now_and_after):
    '''Test the creation and execution of combined workfow'''
    clear_now_and_after('a.done')
    temp_factory('a.txt', 'b.txt', 'b.begin')
    execute_workflow(
        '''
        if 'executed' not in locals():
            executed = []
        if 'inputs' not in locals():
            inputs = []

        [a_1: shared=['executed', 'inputs']]
        executed.append(step_name)
        inputs.append(_input)

        [a_2: shared=['executed', 'inputs']]
        executed.append(step_name)
        inputs.append(_input)

        [a_3: shared=['executed', 'inputs']]
        executed.append(step_name)
        inputs.append(_input)

        [a_4: shared=['executed', 'inputs']]
        executed.append(step_name)
        output: 'a.done'
        inputs.append(_input)
        run: expand=True
        touch {_output}

        [b_1: shared=['executed', 'inputs']]
        executed.append(step_name)
        input: 'b.begin'
        inputs.append(_input)

        [b_2: shared=['executed', 'inputs']]
        executed.append(step_name)
        inputs.append(_input)

        [b_3: shared=['executed', 'inputs']]
        executed.append(step_name)
        inputs.append(_input)

        [b_4: shared=['executed', 'inputs']]
        executed.append(step_name)
        output: 'b.txt'
        inputs.append(_input)

        [c: shared=['executed', 'inputs']]
        executed.append(step_name)
        input: 'a.txt'
        output: 'b.txt'
        inputs.append(_input)
        sos_run('a+b', shared=['executed', 'inputs'])
        ''',
        workflow='c',
        options={'sig_mode': 'ignore'})

    # order of execution is not guaranteed
    assert sorted(env.sos_dict['executed']) == sorted(
        ['c', 'a_1', 'a_2', 'a_3', 'a_4', 'b_1', 'b_2', 'b_3', 'b_4'])
    env.sos_dict.pop('executed', None)

def test_looped_nested_workflow(temp_factory, clear_now_and_after):
    # step will be looped
    clear_now_and_after('a.txt.a1', 'a.txt.a1.a2', 'b.txt.a1', 'b.txt.a1.a2')
    temp_factory('a.txt', 'b.txt')

    execute_workflow(
        '''
        if 'executed' not in locals():
            executed = []
        if 'inputs' not in locals():
            inputs = []

        [a_1:shared=['executed', 'inputs']]
        executed.append(step_name)
        output: _input[0] + '.a1'
        inputs.append(_input)
        run: expand=True
        touch {_output}

        [a_2:shared=['executed', 'inputs']]
        executed.append(step_name)
        output: _input[0] + '.a2'
        inputs.append(_input)
        run: expand=True
        touch {_output}

        [c:shared=['executed', 'inputs']]
        executed.append(step_name)
        input: 'a.txt', 'b.txt', group_by='single'
        inputs.append(_input)
        sos_run('a', shared=['executed', 'inputs'])
        ''',
        workflow='c')
    assert env.sos_dict['executed'] == ['c', 'a_1', 'a_2', 'a_1', 'a_2']

def test_single_looped_nested_workflow(temp_factory, clear_now_and_after):
    temp_factory('a.txt', 'b.txt')
    env.sos_dict.pop('executed', None)

    execute_workflow(
        '''
        if 'executed' not in locals():
            executed = []
        [a_1:shared='executed']
        executed.append(step_name)
        [a_2:shared='executed']
        executed.append(step_name)
        [c_0:shared='executed']
        executed.append(step_name)
        [c_1:shared='executed']
        depends: sos_variable('executed')
        executed.append(step_name)
        input: 'a.txt', 'b.txt', group_by='single'
        sos_run('a:2', shared='executed')
        ''',
        workflow='c')
    assert env.sos_dict['executed'] == ['c_0', 'c_1', 'a_2', 'a_2']
    env.sos_dict.pop('executed', None)
    # allow specifying a single step
    # step will be looped
    execute_workflow(
        '''
        if 'executed' not in locals():
            executed = []

        [a_1:shared='executed']
        executed.append(step_name)

        [a_2:shared='executed']
        executed.append(step_name)

        [c_0:shared='executed']
        executed.append(step_name)

        [c_1:shared='executed']
        depends: sos_variable('executed')
        executed.append(step_name)
        input: 'a.txt', 'b.txt', group_by='single'
        sos_run('a:2', shared='executed')
        ''',
        workflow='c')

    assert env.sos_dict['executed'] == ['c_0', 'c_1', 'a_2', 'a_2']

def test_recursive_nested_workflow(temp_factory):
    # recursive subworkflow not allowed
    temp_factory('a.txt', 'b.txt')
    with pytest.raises(Exception):
        execute_workflow(
            '''
            if 'executed' not in locals():
                executed = []
            [a_1:shared='executed']
            executed.append(step_name)
            [a_2:shared='executed']
            executed.append(step_name)
            [c_0:shared='executed']
            executed.append(step_name)
            [c_1:shared='executed']
            executed.append(step_name)
            input: 'a.txt', 'b.txt', group_by='single'
            sos_run('a_2+c', shared='executed')
            ''',
            workflow='c')
    #
    env.sos_dict.pop('executed', None)
    # nested subworkflow is allowed
    execute_workflow(
        '''
        if 'executed' not in locals():
            executed = []
        [a_1:shared='executed']
        executed.append(step_name)
        [a_2:shared='executed']
        executed.append(step_name)
        [a_3:shared='executed']
        executed.append(step_name)
        [b_1:shared='executed']
        executed.append(step_name)
        [b_2:shared='executed']
        executed.append(step_name)
        sos_run('a:1-2', shared='executed')
        [c_0:shared='executed']
        executed.append(step_name)
        [c_1:shared='executed']
        depends: sos_variable('executed')
        executed.append(step_name)
        input: 'a.txt'
        sos_run('a+b', shared='executed')
        ''',
        workflow='c')

    assert env.sos_dict['executed'] == [
        'c_0', 'c_1', 'a_1', 'a_2', 'a_3', 'b_1', 'b_2', 'a_1', 'a_2'
    ]

    env.sos_dict.pop('executed', None)

def test_subworkflow_with_options(temp_factory, clear_now_and_after):
    # nested subworkflow with step option and others
    temp_factory('a.txt', 'b.txt')
    clear_now_and_after('a.done')
    script = '''
        if 'executed' not in locals():
            executed = []
        [a_1:shared='executed']
        executed.append(step_name)

        [a_2:shared='executed']
        executed.append(step_name)

        [a_3:shared='executed']
        executed.append(step_name)

        [b:shared='executed']
        executed.append(step_name)
        input: 'a.txt', 'b.txt', group_by='single'
        sos_run('a:3+a:1', shared='executed')

        [d:shared='executed']
        executed.append(step_name)
        input: 'a.txt', 'b.txt', group_by='single'
        sos_run('a:2', shared='executed')

        [e2_2:shared='executed']
        executed.append(step_name)
        input: 'a.txt', 'b.txt', group_by='single'
        '''
    execute_workflow(script, workflow='b')
    assert env.sos_dict['executed'] == ['b', 'a_3', 'a_1', 'a_3', 'a_1']

    env.sos_dict.pop('executed', None)

    execute_workflow(script, workflow='d')
    assert env.sos_dict['executed'] == ['d', 'a_2', 'a_2']
    env.sos_dict.pop('executed', None)

    execute_workflow(script, workflow='e2')
    assert env.sos_dict['executed'] == ['e2_2']

def test_dynamic_nested_workflow():
    '''Test nested workflow controlled by command line option'''
    script = '''
        if 'executed' not in locals():
            executed = []

        parameter: wf='a'

        [a_1:shared='executed']
        executed.append(step_name)
        [a_2:shared='executed']
        executed.append(step_name)
        [a_3:shared='executed']
        executed.append(step_name)
        [b_1:shared='executed']
        executed.append(step_name)
        [b_2:shared='executed']
        executed.append(step_name)
        [b_3:shared='executed']
        executed.append(step_name)

        [default:shared='executed']
        executed.append(step_name)
        sos_run(wf, shared='executed')
        '''
    execute_workflow(script, args=['--wf', 'b'])
    assert env.sos_dict['executed'] == ['default', 'b_1', 'b_2', 'b_3']
    #
    env.sos_dict.pop('executed', None)
    execute_workflow(script, args=['--wf', 'a'])
    assert env.sos_dict['executed'] == ['default', 'a_1', 'a_2', 'a_3']

def test_sos_run(clear_now_and_after):
    '''Test action sos_run with keyword parameters'''
    clear_now_and_after('0.txt', '1.txt')
    execute_workflow(
        r'''
        [A]
        parameter: num=5

        run: expand=True
            touch {num}.txt

        [batch]
        for k in range(2):
            sos_run('A', num=k)
        ''',
        workflow='batch')

    for f in ['0.txt', '1.txt']:
        assert file_target(f).target_exists()
    #
    clear_now_and_after('0.txt', '1.txt', '5.txt')

    execute_workflow(
        r'''
        [A]
        parameter: num=5
        run: expand=True
            touch {num}.txt

        [batch]
        for num in range(2):
            sos_run('A')
        ''',
        workflow='batch')
    for f in ['0.txt', '1.txt']:
        assert not file_target(f).target_exists()

    assert file_target('5.txt').target_exists()

    clear_now_and_after('10.txt', '11.txt')
    #
    # test parameter shared to send and return vars
    #
    execute_workflow(
        r'''
        [A: shared='k']
        k += 10

        [batch]
        for k in range(2):
            sos_run('A', shared='k')
            run(f"touch {k}.txt")
        ''',
        workflow='batch')
    for f in ['10.txt', '11.txt']:
        assert file_target(f).target_exists()

def test_da_gof_dynamic_nested_workflow(clear_now_and_after):
    #
    # Because we are not sure which workflows would be executed
    # until run time, the DAG should not contain nested workflow
    # until runtime.
    #
    clear_now_and_after('B0.txt', 'B0.txt.p', 'B1.txt', 'B1.txt.p', 'B2.txt',
                        'B2.txt.p')
    #
    #  A1 <- P <- B
    #  A1 <- P <- B
    #  A2
    #
    #  ALL calls A and B with parameter
    #
    execute_workflow(
        '''
        [A_1]
        parameter: num = 2
        input: f"B{num}.txt.p"

        [B: provides='B{num}.txt']
        run: expand=True
        touch 'B{num[0]}.txt'

        [P: provides='{filename}.p']
        input: filename
        run: expand=True
        touch {_output}

        [ALL]

        for i in range(3):
            sos_run('A', num=i)
    ''',
        workflow='ALL')
    for f in ['B0.txt', 'B0.txt.p', 'B1.txt', 'B1.txt.p', 'B2.txt', 'B2.txt.p']:
        assert file_target(f).target_exists()

def test_outcome_oriented_nested_workflow(clear_now_and_after):
    '''test nested workflow triggered by targets'''
    clear_now_and_after('test_15.txt')

    execute_workflow('''
[A: provides='test_{idx}.txt']
_output.touch()

[default]
sos_run(targets='test_15.txt')
    ''')
    assert os.path.isfile('test_15.txt')

def test_passing_vars_to_nested_workflow():
    '''Test if variables can be passed to nested workflows'''
    execute_workflow(r"""
        import time
        import random

        [nested]
        parameter: nested=True
        parameter: seed=1
        print(f'I am nested {nested} with seed {seed}')

        [0]
        reps = range(5)
        input: for_each='reps'
        import random
        nested = _reps
        seed = random.randint(1, 1000)
        print(f'Passing {seed} to {nested}')
        sos_run('nested', nested=nested, seed=seed)
    """)

def test_user_defined_func():
    '''Test the use of user-defined functions in SoS script'''
    execute_workflow(
        r"""
        def myfunc():
            return 'a'

        [1: shared={'test':'_output'}]
        output: myfunc()

        myfunc()

        """,
        options={'run_mode': 'dryrun'})
    assert env.sos_dict['test'] == ['a']
    # User defined function should also work under nested workflows
    # This is difficult because the 'local namespace' is usually
    # not seen inside function definition. The solution now is to
    # use a single workspace.
    execute_workflow(
        r"""
        def myfunc():
            # test if builtin functions (sum and range) can be used here.
            return 'a' + str(sum(range(10)))

        [1: shared={'test':'_output'}]
        output: [myfunc() for i in range(10)][0]

        myfunc()

        """,
        options={'run_mode': 'dryrun'})
    assert env.sos_dict['test'] == ['a45']

def test_config_file_of_nested_workflow(config_factory):
    '''Test passing of configurationg to nested workflow'''
    cfg = config_factory("""{'1':'hi'}""")

    execute_workflow(
        '''
[test_1]
parameter: key = None
print(CONFIG[key])

[default_1]
sos_run('test:1', key = '1')
''',
        options={'config_file': cfg})

def test_error_from_subworkflow():
    '''Test if error from subworkflow is passed to master (#396)'''
    with pytest.raises(Exception):
        execute_workflow('''
            [test_1]
            R:
            set.seed(xxx)

            [default]
            sos_run('test')
            ''')

def test_fun_def(temp_factory):
    '''Test defintion of function that can be used by other steps'''
    temp_factory('aa.txt', 'ab.txt')
    # in nested workflow?
    execute_workflow(
        r"""
        def myfunc(a):
            return ['a' + x for x in a]

        [mse: shared={'test':'_output'}]
        input: myfunc(['a.txt', 'b.txt'])

        [1]
        sos_run('mse')
        """,
        options={'run)mode': 'dryrun'})
    #
    # Names defined in subworkflow is not returned to the master dict
    assert 'test' not in env.sos_dict

def test_search_path(clear_now_and_after):
    '''Test if any action should exit in five seconds in dryrun mode'''
    clear_now_and_after('crazy_path', 'test.yml')

    sos_config_file = os.path.join(
        os.path.expanduser('~'), '.sos', 'config.yml')
    shutil.copy(sos_config_file, 'test.yml')
    #
    subprocess.call(
        'sos config --set sos_path {0}/crazy_path {0}/crazy_path/more_crazy/'
        .format(os.getcwd()),
        shell=True)
    #
    if not os.path.isdir('crazy_path'):
        os.mkdir('crazy_path')
        os.mkdir(os.path.join('crazy_path', 'more_crazy'))
    with open(os.path.join('crazy_path', 'crazy_master.sos'), 'w') as crazy:
        crazy.write('''
[0]
sos_run('cc', source='crazy_slave.sos')

''')
    with open(os.path.join('crazy_path', 'more_crazy', 'crazy_slave.sos'),
              'w') as crazy:
        crazy.write('''
[cc_0]
print('hay, I am crazy')
''')

    script = SoS_Script(filename='crazy_master.sos')
    script.workflow()
    #
    os.remove(sos_config_file)
    shutil.copy('test.yml', sos_config_file)

def test_nested_workdir(clear_now_and_after, purge_tasks):
    '''Test nested runtime option for work directory'''
    clear_now_and_after('tmp', 'tmp1')
    execute_workflow(
        '''
        [step]
        task: workdir='tmp1'
        run:
            touch 'a.txt'

        [default]
        sos_run('step', workdir='tmp')
        ''',
        options={'default_queue': 'localhost'})

    assert os.path.isfile('tmp1/a.txt')

def test_failure_of_nested_workflow(clear_now_and_after):
    '''Test failure of nested workflow #838'''
    clear_now_and_after('a.txt')
    with pytest.raises(Exception):
        execute_workflow('''
            [something]
            input: 'a.txt'

            [default]
            sos_run('something')
            ''')

def test_nested_from_another_file(clear_now_and_after):
    '''Test nested runtime option for work directory'''
    clear_now_and_after('a.txt', 'another.sos')
    with open('another.sos', 'w') as another:
        another.write('''
[whatever]
run:
touch 'a.txt'

''')
    execute_workflow('''
        [default]
        sos_run('whatever', source='another.sos')
        ''')
    assert os.path.isfile(
        'a.txt'
    ), 'a.txt should have been created by nested workflow from another file'

def test_concurrent_sub_workflow():
    '''Test concurrent subworkflow sos_run '''
    import time
    st = time.time()

    execute_workflow(
        '''
        [A]
        parameter: idx=0
        import time
        time.sleep(5)

        [default]
        input: for_each=dict(i=range(6))
        sos_run('A', idx=i)
        ''',
        options={'worker_procs': ['8']})
    assert time.time() - st < 30

def test_sos_multi_workflow():
    '''Test multiple workflows in sos_run '''
    import time
    st = time.time()

    execute_workflow(
        '''
        [B]
        parameter: idx=2
        import time
        time.sleep(idx)

        [A]
        import time
        time.sleep(idx)

        [default]
        input: for_each=dict(i=range(4))
        sos_run(['A', 'B'], idx=i)
    ''',
        options={'worker_procs': ['8']})
    assert time.time() - st < 20

def test_pass_of_args():
    '''Test passing of arguments through sos_run #1164'''
    with pytest.raises(Exception):
        execute_workflow('''
            parameter: b=2
            [A]
            parameter: c=1
            print(c)

            [default]
            sos_run('A', a=2)
        ''')
    #
    execute_workflow('''
        parameter: b=2
        [A]
        parameter: c=1
        print(c)

        [default]
        sos_run('A', b=2)
        ''')
    #
    execute_workflow('''
        parameter: b=2
        [A]
        parameter: c=1
        print(c)

        [default]
        sos_run('A', c=2)
    ''')

def test_nested_dynamic_depends(clear_now_and_after):
    '''Test the execution of nested workflow with dynamic depends'''
    clear_now_and_after('B30.txt', 'B30.txt.p', 'B0.txt.p', 'B0.txt')
    execute_workflow('''
        [A_1]
        parameter: num = 20
        input: f"B{num}.txt"
        depends: _input + '.p'

        [B: provides='B{num}.txt']
        run: expand=True
            touch {_output}

        [P: provides='{filename}.p']
        input: filename
        run: expand=True
            touch {_output}

        [default]
        sos_run('A', num=0)
    ''')

def test_neste_with_both_workflow_and_targets(clear_now_and_after):
    '''Test nested workflow with both workflow and targets'''
    clear_now_and_after('A_out.txt', 'B_out.txt')
    execute_workflow(r'''
        [A_20]
        output: 'A_out.txt'
        _output.touch()

        [B]
        output: 'B_out.txt'
        _output.touch()

        [default]
        sos_run('A', targets='B_out.txt')
        ''')
    assert os.path.isfile('A_out.txt')
    assert os.path.isfile('B_out.txt')


import functools

from googlecloudsdk.calliope.concepts import concepts
from googlecloudsdk.calliope.concepts import deps as deps_lib
from googlecloudsdk.calliope.concepts import util
from googlecloudsdk.core import exceptions

class Error(exceptions.Error):
  """Base class for errors in this module."""

class ParseError(Error):
  """Raised if a concept fails to parse."""

  def __init__(self, presentation_name, message):
    msg = 'Error parsing [{}].\n{}'.format(presentation_name, message)
    super(ParseError, self).__init__(msg)

class RuntimeHandler(object):
  """A handler to hold information about all concept arguments in a command.

  The handler is assigned to 'CONCEPTS' in the argparse namespace and has an
  attribute to match the name of each concept argument in lower snake case.
  """

  def __init__(self):
    # This is set by the ArgumentInterceptor later.
    self.parsed_args = None

  def ParsedArgs(self):
    """Basically a lazy property to use during lazy concept parsing."""
    return self.parsed_args

  def AddConcept(self, name, concept_spec, concept_info):
    """Adds a concept handler for a given concept.

    Args:
      name: str, the name to be used for the presentation spec.
      concept_spec: googlecloudsdk.calliope.concepts.ConceptSpec, the spec for
        the underlying concept.
      concept_info: ConceptInfo, the object that holds dependencies of the
        concept.
    """

    class LazyParse(object):

      def __init__(self, arg_getter):
        self.parse = functools.partial(Parse, concept_spec, concept_info)
        self.arg_getter = arg_getter

      def Parse(self):
        try:
          return self.parse(self.arg_getter())
        except concepts.InitializeError as e:
          raise ParseError(name, e.message)

    setattr(self, name, LazyParse(self.ParsedArgs))

class ConceptInfo(object):
  """Holds information for a concept argument.

  The ConceptInfo object is responsible for holding information about the
  dependencies of a concept, and building a Deps object when it is time for
  lazy parsing of the concept.

  Attributes:
    concept_spec: The concept spec underlying the concept handler.
    attribute_to_args_map: A map of attributes to the names of their associated
      flags.
    fallthroughs_map: A map of attributes to non-argument fallthroughs.
    arg_info_map: A map of attribute names to values passed to the corresponding
      flag.
  """

  def __init__(self, concept_spec, attribute_to_args_map,
               fallthroughs_map):
    """Initializes the ConceptInfo.

    Args:
      concept_spec: googlecloudsdk.calliope.concepts.ConceptSpec, The underlying
        concept spec.
      attribute_to_args_map: {str: str}, A map of attribute names to the names
        of their associated flags.
      fallthroughs_map: {str: [deps_lib.Fallthrough]} A map of attribute names
        to non-argument fallthroughs.
    """
    self.concept_spec = concept_spec
    self.attribute_to_args_map = attribute_to_args_map
    self.fallthroughs_map = fallthroughs_map

  def _BuildFinalFallthroughsMap(self, parsed_args=None):
    """Helper method to build all fallthroughs including arg names."""
    final_fallthroughs_map = {}
    for attribute in self.concept_spec.attributes:
      attribute_name = attribute.name
      attribute_fallthroughs = []

      # Start the fallthroughs list with the primary associated arg for the
      # attribute.
      arg_name = self.attribute_to_args_map.get(attribute_name)
      if arg_name:
        arg_value = getattr(parsed_args,
                            util.NamespaceFormat(arg_name),
                            None)
        # Required positionals end up stored as lists of strings.
        if isinstance(arg_value, list) and attribute.value_type == str:
          arg_value = arg_value[0] if arg_value else None
        attribute_fallthroughs.append(
            deps_lib.ArgFallthrough(arg_name, arg_value))

      attribute_fallthroughs += self.fallthroughs_map.get(attribute_name, [])
      final_fallthroughs_map[attribute_name] = attribute_fallthroughs
    return final_fallthroughs_map

  def GetDeps(self, parsed_args=None):
    """Builds the deps.Deps object to get attribute values.

    Gets a set of fallthroughs for each attribute of the handler's concept spec,
    including any argument values that were registered through RegisterArg.
    Then initializes the deps object.

    Args:
      parsed_args: (calliope.parser_extensions.Namespace) the parsed arguments
        from command line.

    Returns:
      (deps_lib.Deps) the deps object representing all data dependencies.
    """
    final_fallthroughs_map = self._BuildFinalFallthroughsMap(
        parsed_args=parsed_args)
    return deps_lib.Deps(final_fallthroughs_map)

  def GetHints(self, attribute_name):
    """Gets a list of string hints for how to set an attribute.

    Given the attribute name, gets a list of hints corresponding to the
    attribute's fallthroughs.

    Args:
      attribute_name: str, the name of the attribute.

    Returns:
      A list of hints for its fallthroughs, including its primary arg if any.
    """
    fallthroughs = self._BuildFinalFallthroughsMap().get(attribute_name, [])
    return [f.hint for f in fallthroughs]

def Parse(concept_spec, concept_info, parsed_args=None):
  """Parses a concept at runtime.

  Args:
    concept_spec: googlecloudsdk.calliope.concepts.ConceptSpec, The underlying
      concept spec.
    concept_info: ConceptInfo, the object that holds dependencies of the
      concept.
    parsed_args: (calliope.parser_extensions.Namespace) the parsed arguments
      from command line.

  Returns:
    The fully initialized concept.

  Raises:
    googlecloudsdk.calliope.concepts.concepts.InitializeError, if the concept
      can't be initialized.
  """
  deps = concept_info.GetDeps(parsed_args=parsed_args)
  return concept_spec.Initialize(deps)


import json
import re
import sqlite3
import string

def addWord(wordDf, rStr):
    print(f"input: '{rStr}'")
    wEnd = rStr.find(' (')
    wd = rStr[:wEnd]
    altern = []
    if wd.find('/') > 0:
        mwd = wd.split('/')
        wd = mwd[0].strip()
        altern = mwd[1:]
    elif wd.find('(') >= 0:
        altern = [re.sub(r'[\(\)]', '', wd)]
        wd = re.sub(r'\(.*\)', '', wd)
    wNotes = [wn.rstrip(' )') for wn in rStr[wEnd:].split('(')[1:]]
    wordDf[wd] = {
        'alternatives': altern,
        'notes':        wNotes[1:],
        'partOfSpeech': wNotes[0],
        'example':      [],
    }
    return wd

def addExample(wordDf, wd, example):
    wordDf[wd]['example'].append(example)
    # print(f"'{wd}' example: {wordDf[wd]['example']}")

def fileTowordDf(wordFile, wordDf):
    with open(wordFile) as f:
        curWd = None
        for line in f:
            if line.isspace():  # ignore empty line
                continue
            line = line.strip()
            if line.isupper() and len(line) == 1:
                print(f"ignore '{line}'\n")
                continue
            # new word or example
            if line.startswith('.'):  # example of current word
                if not curWd:
                    continue  # no current word
                addExample(wordDf, curWd, line.lstrip(" ."))
            else:
                curWd = addWord(wordDf, line)

def dbQueryWord(cur, wd):
    sql = f'select phonetic, definition, translation from ecdict where word = "{wd}"'
    cur.execute(sql)
    return cur.fetchone()

def lookupDict(wordDf, dbFile):
    maxlen = 640
    # ecdict imported from https://github.com/skywind3000/ECDICT
    conn = sqlite3.connect(dbFile)
    cur = conn.cursor()
    invalidWords = []
    # lookup the ecdict
    for wd in wordDf:
        lwd = wd.strip('.!?')
        row = dbQueryWord(cur, lwd)
        if row:
            wordDf[wd]['phonetic'], wordDf[wd]['definition'], wordDf[wd]['translation'] = row
        else:  # try lookup lower case word
            row = dbQueryWord(cur, lwd.lower())
            if row:
                wordDf[wd]['phonetic'], wordDf[wd]['definition'], wordDf[wd]['translation'] = row
            else:
                print(f"{wd} not found in ecdict, deleting")
                invalidWords.append(wd)
                continue
        deflen = len(wordDf[wd]['definition']) + len(wordDf[wd]['translation'])
        if deflen > maxlen:
            print(f'!!def of {wd} is {deflen}!!')
    conn.close()
    return invalidWords

def wordListToHtml(wordList, wordDf, ignoreWords, htmlFile, title):
    jscript = '''

    '''
    htmlHead = f'''
    <meta charset="UTF-8">
        <title>{title}</title>
        <link href="flashcard.css" rel="stylesheet">
        {jscript}

    '''
    htmlTail = '''

    '''
    outFile = open(htmlFile, 'w', encoding='utf-8')
    outFile.write(htmlHead)
    # format word list to html
    for wd in wordList:
        if wd not in wordDf:
            print(f"{wd} not found, ignore!")
            continue
        partOfSpeech = ''
        if wordDf[wd]['partOfSpeech']:
            partOfSpeech += f"({wordDf[wd]['partOfSpeech']})"
        alternatives = ''
        if wordDf[wd]['alternatives']:
            alternatives = '/' + '/'.join(wordDf[wd]['alternatives'])
        phonetic = ''
        if wordDf[wd]['phonetic']:
            phonetic = f"[{wordDf[wd]['phonetic']}]"
        notes = ''
        if wordDf[wd]['notes']:
            notes = '
' + '; '.join(wordDf[wd]['notes']) + '
'
        examples = ''
        if wordDf[wd]['example']:
            for item in wordDf[wd]['example']:
                examples += f"
{item}
\n"
        if examples:
            examples = '
' + examples + '
'
        translation = wordDf[wd]['translation'].replace('\\n', '
\n')
        if translation:
            translation = '
' + translation + '
'
        definition = ''
        if wd not in ignoreWords:
            definition = wordDf[wd]['definition'].replace('\\n', '
\n')
        if definition:
            definition = '
' + definition + '
'

        cardHtml = f'''

Charlie

                        {wd}{alternatives}
                         {partOfSpeech}

                        {phonetic}

                    {notes}
                    {examples}

                    {translation}
                    {definition}

        '''
        outFile.write(cardHtml)

    outFile.write(htmlTail)
    outFile.close()

def printWrodDfStats(wordDf):
    # general info of wordDf
    words = wordDf.keys()
    for c in string.ascii_lowercase:
        cnt = len([wd for wd in words if wd[0].lower() == c])
        print(f'{c}: {cnt}')
    print("total words: " + str(len(words)))

def woriteWordDfHtmlByGrp(wordDf, workDir, ignoreWords):
    htmlOutGrp = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'ijk',
                  'l', 'm', 'no', 'p', 'qr', 's', 't', 'uvwxyz']
    for i in htmlOutGrp:
        htmlFile = f'{workDir}words_{i}.html'
        wlist = [wd for wd in wordDf if (wd[0].lower() in i)]
        wordListToHtml(wlist, wordDf, ignoreWords, htmlFile, f'Cambridge English A2 vocabulary Flashcards {i}')

if __name__ == "__main__":
    workDir = 'd:\\data\\flashcards\\'
    wordDf = {}
    readWordsFromJson = True
    wdJsnFile = workDir + 'a2keyWL.json'
    if readWordsFromJson:
        wordDf = json.load(open(wdJsnFile, 'r', encoding='utf-8'))
    else:
        wordFile = workDir + 'a2vl.txt'
        fileTowordDf(wordFile, wordDf)
        dbFile = workDir + 'ecdict.db'
        invalidWords = lookupDict(wordDf, dbFile)
        for wd in invalidWords:
            del wordDf[wd]
        # write wordDf to json file
        with open(wdJsnFile, 'w', encoding='utf-8') as jf:
            json.dump(wordDf, jf, sort_keys=True)
    #printWrodDfStats(wordDf)
    # words with loong definitions
    # wlist = ['CD', 'against', 'answer', 'as', 'capital', 'chips', 'colour', 'directions', 'father', 'field',
    #          'for', 'from', 'gas', 'glasses', 'green', 'herself', 'himself', 'hot', 'into', 'kite', 'left',
    #          'long', 'lots', 'more', 'mouse', 'mushroom', 'of', 'post', 'programme', 'right', 'shall', 'shorts',
    #          'since', 'stripes', 'studies', 'subject', 'than', 'that', 'to', 'us', 'water', 'what', 'when', 'where',
    #          'which', 'with', 'without', 'work']
    # wordListToHtml(wlist, wordDf, [], f'{workDir}test.html', 'flashcard test')

    # write all words in one large html file
    wordListToHtml(wordDf.keys(), wordDf, [], f'{workDir}test.html', 'Cambridge English A2 vocabulary Flashcards')

    # grp = 'uvwxyz'
    # wlist = [wd for wd in wordDf if (wd[0].lower() in grp)]
    # wordListToHtml(wlist, wordDf, [], f'{workDir}words_{grp}.html', f'Cambridge English A2 vocabulary Flashcards {grp}')

    # write html group by leading letter
    # woriteWordDfHtmlByGrp(wordDf, workDir, [])


# Garret Lowe 2022

import json

DATA_DIR = '..\\data\\'

routes = {
	'road': {
	},
	'boat': {
	},
	'silt_strider': {
	},
	'mage_guild': {
	},
	'propylon': {
	},
	'special': {
	}
}

with open(DATA_DIR + 'routes.csv', 'r') as file:
	first = True
	for line in file:
		if first:
			first = False
			continue
		route = line.strip().split(',')
		if route[0] not in routes[route[2]]:
			routes[route[2]][route[0]] = dict()
		routes[route[2]][route[0]][route[1]] = {
			'travel_time': int(route[3]),
			'gold_cost': 0
		}

with open('out.json', 'w') as file:
	json.dump(routes, file, indent=2)


import adsense_util
import sys
import google.auth.exceptions
from googleapiclient import discovery

# The max page size when fetching data from the API.
MAX_PAGE_SIZE = 50

def main(argv):
  # Authenticate and construct service.
  credentials = adsense_util.get_adsense_credentials()
  with discovery.build('adsense', 'v2', credentials = credentials) as service:
    try:
      # Retrieve account list in pages and display data as we receive it.
      request = service.accounts().list(pageSize=MAX_PAGE_SIZE)

      while request is not None:
        result = request.execute()
        accounts = result['accounts']

        for account in accounts:
          print ('Account with ID "%s" and name "%s" was found. '
                 % (account['name'], account['displayName']))

        request = service.accounts().list_next(request, result)

    except google.auth.exceptions.RefreshError:
      print('The credentials have been revoked or expired, please delete the '
            '"%s" file and re-run the application to re-authorize.' %
            adsense_util.CREDENTIALS_FILE)

if __name__ == '__main__':
  main(sys.argv)


from django_filters.rest_framework import FilterSet

from .. import models

class AttendanceFilter(FilterSet):
    class Meta:
        model = models.Lesson
        fields = {
            'completion_timestamp': ['gte', 'lte', 'range'],
            'took_place': ['exact'],
            'studentlesson__student': ['exact'],
            'group': ['exact'],
        }


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right

# Solution 1
# class Solution:
#     def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
#         ans = []
#         queue = [root]
#         level_queue = []
#         level_ans = []

#         while root and queue:
#             while queue:
#                 node = queue.pop(0)
#                 level_ans.append(node.val)

#                 if node.left:
#                     level_queue.append(node.left)

#                 if node.right:
#                     level_queue.append(node.right)

#             ans.append(level_ans)
#             queue = level_queue
#             level_queue = []
#             level_ans = []

#         return ans

# Solution 2: BFS
class Solution:
    def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        levels = []
        if not root:
            return levels

        queue = [root]
        while queue:
            # start the current level
            level = []
            for i in range(len(queue)):
                node = queue.pop(0)
                level.append(node.val)

                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)

            levels.append(level)

        return levels


from posixpath import split

from itertools import combinations
def comb(inarr,innum):
    c=combinations(inarr,4)
    count=0
    for i in list(c):
        if sum(i)==innum:
            count+=1

inarr=map(int,input().split(''))
innum=int(input())


def verificaPrimo (numero):
    metade=numero//2
    if (numero<2):
        return False

    while (metade>1):
        if (numero%metade==0):
            return False
        metade-=1
    return True

def imprimeresultado (num,result):
        mensagem=f"O número {num} não é primo"
        if(result):
            mensagem=f"O número {num} é primo"
        return mensagem

eprimo = eval(input("Digite o número a ser verificado:"))
resultado = verificaPrimo(eprimo)
impressao=imprimeresultado(eprimo,resultado)

print(impressao)