import requests
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options

def get_CNN(name, day=7, out_put=False):

    url = f'https://www.cnn.com/search/?size=10&q={name}&category=business'

    #set chrome driver to headerless
    option_ = Options()
    option_.add_argument('--headless')

    #create driver to scrape
    driver = webdriver.Chrome(chrome_options=option_)
    driver.implicitly_wait(0.01)
    driver.get(url)

    # Wait maximum 120 seconds for page to load the need elements
    timeout = 120
    try:
        WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.XPATH, "//h3[@class='cnn-search__result-headline']")))
    except TimeoutException:
        print('Timed out waiting for page to load')
        return ''

    # find_elements_by_xpath/or _by_tag_name returns an array of selenium objects.
    h3_elements = driver.find_elements_by_tag_name("h3")

    #find a tag with h3 tag
    #get links from a_tag uneder h3 tags
    list_links = []
    for h3_element in h3_elements:
        try:
            #print(h3_element.find_element_by_tag_name('span').text)
            list_links.append(h3_element.find_element_by_tag_name("a").get_attribute('href'))
        except:
            continue

    #close web_driver
    driver.quit()

    #use request here for performence ot practice selenium
    #pratice selenium + multiprocess

    ####get article from list_links####

    #failure list
    list_fail_link = []
    #article text
    text = ''

    #get articles from the list_links
    for i_link in list_links:
        #set chrome driver to headerless
        option_ = Options()
        option_.add_argument('--headless')

        #create driver to scrape
        driver = webdriver.Chrome(chrome_options=option_)
        driver.get(i_link)

        # Wait x seconds for page to load the need elements
        timeout = 8
        try:
            WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='zn-body__paragraph']")))
        except TimeoutException:
            print('Timed out waiting for page to load')
            list_fail_link.append(i_link)
            continue

        p_elements = driver.find_elements_by_class_name("zn-body__paragraph")
        #get article and coleect all into var:text
        for p_element in p_elements:
            try:
                text = text + ' ' +  str(p_element.text)
            except:
                continue

        #clease driver
        driver.quit()

    if out_put:
        with open(f'{name}_CNN_result.txt', 'w') as fp:
            fp.write(text)

    return text

#print(get_CNN('apple'))


from django.contrib.auth.models import AbstractUser
from django.db import transaction
from django.dispatch import receiver

from baserow.contrib.database.api.tables.serializers import TableSerializer
from baserow.contrib.database.table import signals as table_signals
from baserow.contrib.database.table.models import Table
from baserow.contrib.database.table.object_scopes import DatabaseTableObjectScopeType
from baserow.contrib.database.table.operations import ReadDatabaseTableOperationType
from baserow.contrib.database.table.tasks import (
    unsubscribe_user_from_table_currently_subscribed_to,
)
from baserow.core import signals as core_signals
from baserow.core.utils import generate_hash
from baserow.ws.tasks import broadcast_to_group, broadcast_to_permitted_users

@receiver(table_signals.table_created)
def table_created(sender, table, user, **kwargs):
    transaction.on_commit(
        lambda: broadcast_to_permitted_users.delay(
            table.database.workspace_id,
            ReadDatabaseTableOperationType.type,
            DatabaseTableObjectScopeType.type,
            table.id,
            {"type": "table_created", "table": TableSerializer(table).data},
            getattr(user, "web_socket_id", None),
        )
    )

@receiver(table_signals.table_updated)
def table_updated(
    sender,
    table: Table,
    user: AbstractUser,
    force_table_refresh: bool = False,
    **kwargs
):
    transaction.on_commit(
        lambda: broadcast_to_permitted_users.delay(
            table.database.workspace_id,
            ReadDatabaseTableOperationType.type,
            DatabaseTableObjectScopeType.type,
            table.id,
            {
                "type": "table_updated",
                "table_id": table.id,
                "table": TableSerializer(table).data,
                "force_table_refresh": force_table_refresh,
            },
            getattr(user, "web_socket_id", None),
        )
    )

@receiver(table_signals.table_deleted)
def table_deleted(sender, table_id, table, user, **kwargs):
    transaction.on_commit(
        lambda: broadcast_to_permitted_users.delay(
            table.database.workspace_id,
            ReadDatabaseTableOperationType.type,
            DatabaseTableObjectScopeType.type,
            table.id,
            {
                "type": "table_deleted",
                "database_id": table.database_id,
                "table_id": table_id,
            },
            getattr(user, "web_socket_id", None),
        )
    )

@receiver(table_signals.tables_reordered)
def tables_reordered(sender, database, order, user, **kwargs):
    # Hashing all values here to not expose real ids of tables a user might not have
    # access to
    order = [generate_hash(o) for o in order]
    transaction.on_commit(
        lambda: broadcast_to_group.delay(
            database.workspace_id,
            {
                "type": "tables_reordered",
                # A user might also not have access to the database itself
                "database_id": generate_hash(database.id),
                "order": order,
            },
            getattr(user, "web_socket_id", None),
        )
    )

@receiver(core_signals.workspace_user_deleted)
def user_deleted_unsubscribe_from_page(
    sender, workspace_user_id, workspace_user, user, **kwargs
):
    transaction.on_commit(
        lambda: unsubscribe_user_from_table_currently_subscribed_to.delay(
            workspace_user.user_id, workspace_user.workspace_id
        )
    )


from funasr.models.e2e_ss import MossFormer

def build_ss_model(args):
    model = MossFormer(
        in_channels=args.encoder_embedding_dim,
        out_channels=args.mossformer_sequence_dim,
        num_blocks=args.num_mossformer_layer,
        kernel_size=args.encoder_kernel_size,
        norm=args.norm,
        num_spks=args.num_spks,
        skip_around_intra=args.skip_around_intra,
        use_global_pos_enc=args.use_global_pos_enc,
        max_length=args.max_length)

    return model


'''
Argument Parser for VicosinTool
'''

import argparse
import sys  # Maybe it's kinda lame

def parse_args():
    parser = argparse.ArgumentParser(
            description='VicosinTool - OSINT tool for Vk')

    user_id_required = not ('test' in sys.argv or
                            'help' in sys.argv or
                            'version' in sys.argv or
                            'chatlist' in sys.argv)
    access_token_required = not ('help' in sys.argv or
                                 'version' in sys.argv or
                                 '-tf' in sys.argv or
                                 '--token-file' in sys.argv)

    # Postional arguments: command, token, user_id
    parser.add_argument(
            "--command", "-c",
            type=str,
            required=True,
            help='''you know what does it mean'''
            )

    parser.add_argument(
            "--access_token", "-t",
            type=str,
            required=access_token_required,
            help='''access_token itself'''
            )
    parser.add_argument(
            "--user_id", "-T",
            type=int,
            required=user_id_required,
            help='''user_id of a target'''
            )

    # Optional positional arguments: --token_file
    parser.add_argument(
            "--token-file", "-tf",
            type=str,
            help='''path to file which contains token''',
            )

    parser.add_argument(
            "--column-separator", "-cs",
            type=str,
            help='''specify separator for the columns (default: |)'''
            )

    parser.add_argument(
            "--fields", "-f",
            type=str,
            help="specify fields for the command (default for 'friends'" +
            'command: "first_name,last_name,id")')

    # Optional arguments: --verbose --alive
    parser.add_argument(
            "--verbose", "-v",
            action="store_true",
            help="add more text to output"
            )

    parser.add_argument(
            "--alive", '-A',
            action="store_true",
            help="remove DELETED people from output"
            )

    return parser.parse_args()


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import statsmodels.graphics.tsaplots as sgt
from scipy.stats.distributions import chi2
import statsmodels.tsa.stattools as sts
from statsmodels.tsa.statespace.sarimax import SARIMAX

#Load Data
df_raw = pd.read_csv("./Data/index2018.csv")

#Data Sanitization--------------
df_copy = df_raw.copy()
df_copy.date = pd.to_datetime(df_copy.date, dayfirst = True)
df_copy.set_index("date", inplace=True)
df_copy=df_copy.asfreq('b')
df_copy=df_copy.fillna(method='ffill')
df_copy['market_value']=df_copy.spx

#del df_copy['spx'] Done delete this as its used for exogenous variable
del df_copy['dax']
del df_copy['ftse']
del df_copy['nikkei']
size = int(len(df_copy)*0.8)
df, df_test = df_copy.iloc[:size], df_copy.iloc[size:]
#---------------------------------------

# Use ACF plot to analyze coefficients for MA

#------------------- Find MA 1
# The (p,d,q) order of the model for the autoregressive, differences, and moving
model_sarimax_1_1 = SARIMAX(df.market_value, exog=df.spx, order=(1,1,1), ) #ARIMA 1,1,1, exog = MAX
results_sarimax_1_1 = model_sarimax_1_1.fit()
print("Summary:",results_sarimax_1_1.summary())

#------------ Print resisulta
print("Residuals:",results_sarimax_1_1.resid)

#---------------- AIC values
#-- For best model AIC should be least and LLR should be max
print("AIC model_arma_1_1:",results_sarimax_1_1.aic)


#! python3
#BulletPointAdder - adds bullet points to the start

import pyperclip

test = pyperclip.paste()
lines = test.split('\n')
for i in range(len(lines)):
    lines[i] = '*' + lines[i]
    print(lines[i])


# -- coding: utf-8 --
"""

Created on: 22/10/22
@author: Montse Navarro <montserrat.nvro.lpz@gmail.com>
Licence,
"""
if 2 > 1:
    print("This is a true Statement")

value = raw_imput("how much is that doggy in the window?")
value = int(value)

if value < 10:
    print("Taht's a great deal!")
elif 10 <= value <= 20:
    print("I'd still pay that...")
else:
    print("Wow! That's too much!")


if __name__ == "__main__":

    # # Input from file
    # with open('CSES/input.txt') as f:
    #     lines = f.readlines()
    # print(lines)
    # n,x = list(map(int,lines[0].rstrip('\n').split(" ")))
    # arrP = list(map(int,lines[1].rstrip('\n').split(" ")))

    # Input from terminal
    n,x = list(map(int,input().split(" ")))
    arrP = list(map(int,input().split(" ")))

    arrP = sorted(arrP)
    # print(arrP)
    i=0
    j=n-1
    count = 0
    weight = 0
    while i<=j:
        # print(i,j)
        if arrP[j]+arrP[i]<=x:
            j-=1
            i+=1
            count+=1
        else:
            j-=1
            count+=1

    print(count)


def nextGreaterElement(nums1, nums2):
    a = []
    pos = dict(map(reversed, enumerate(nums2)))
    for i in range(len(nums1)):
        index = pos[nums1[i]]
        for j in range(index, len(nums2)):
            if nums1[i] < nums2[j]:
                flag = nums2[j]
                break
            else:
                flag = -1
        a.append(flag)
    return a


import os.path

_base_ = [
    # '../_base_/models/faster_rcnn_r50_fpn.py',
    # '../_base_/datasets/coco_detection.py',
    '../_base_/default_runtime.py']

# 因为我是按照lwir图片的文件名索引的rgb的图片文件名。所以：
# 当读取一对rgbt图像时，modality应该是lwir。
# 当读取一张图像是，modality可以是lwir或者visible，但是要注意，将pipeline修改成单图的pipeline

modality = 'lwir'
load_RGBT = True

# server configuration
server = '3090'
# check server
assert server in ['2070', '3060', '3090']
if server == '3060':
    data_root = '/UsrFile/Usr3/zx/KAIST-Sanitized/coco_format'
elif server == '2070':
    data_root = '/home/ivlab/new_home/zx/cross-modality-det/datasets/KAIST-Sanitized/coco_format'
elif server == '3090':
    data_root = '/home/zx/cross-modality-det/datasets/coco_format'
assert os.path.isdir(data_root)

# load_from = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'

base_lr = 1e-2
# base_lr = 1e-2
# base_lr = 3e-3
# base_lr = 5e-3 # UniDif_FasterRCNN_RGBT lr=1e-2时，loss=nan

# `img_scale` can either be a tuple (single-scale) or a list of tuple (multi-scale), There are 3 multiscale modes.
# Please see my_load_rgbt_pipeline.ResizeRGBT for more details.

bs = 3    # bs=3x4与bs=10差不多,但是bs=3x4速度是bs=10的3倍左右.
# bs = 1

img_scale = (640, 512)

lwir_mean = [44.712, 42.793, 44.712]
lwir_std = [28.165, 28.041, 28.165]     # [28.165, 28.041, 28.165]

rgb_mean = [91.392, 84.984, 75.076]
rgb_std = [67.841, 64.853, 65.535]  # [67.841, 64.853, 65.535]
classes = ['person', 'people', 'person?', 'cyclist', 'person?a']  # mmdet/datasets/coco.py 171行显示，需要将所有类别传入，否则将忽略其它类别的标签

norm_cfg = dict(type='SyncBN', requires_grad=True)
# norm_cfg = dict(type='BN', requires_grad=True)

model = dict(
    type='YOLOX_RGBT',
    share_weights=dict(backbone=True),
    input_size=img_scale,
    random_size_range=(15, 25),
    random_size_interval=10,
    backbone=dict(
        type='SSDVGG',
        depth=16,
        with_last_pool=False,
        ceil_mode=True,
        out_indices=(3, 4),
        out_feature_indices=(15, 22, 34),
        init_cfg=dict(
            type='Pretrained', checkpoint='open-mmlab://vgg16_caffe')),

    dice_weight=2.0,

    neck=dict(
        type='YOLOXPAFPN',
        in_channels=[256, 512, 1024],
        out_channels=256,
        num_csp_blocks=3),

    bbox_head=dict(
        type='YOLOXHead', num_classes=len(classes), in_channels=256, feat_channels=256),
    train_cfg=dict(assigner=dict(type='SimOTAAssigner', center_radius=2.5)),
    test_cfg=dict(score_thr=0.01, nms=dict(type='nms', iou_threshold=0.45)))

dataset_type = 'CocoDataset'

train_pipeline = [
        dict(type='LoadRGBTFromFile'),
        dict(type='LoadRGBTAnnotations', with_bbox=True, with_mask=True),
        dict(type='FillRGBTIgnoredBoxRegionsByMeanValues', rgb_mean=rgb_mean, lwir_mean=lwir_mean),
        dict(type='ResizeRGBT', img_scale=img_scale, keep_ratio=True),
        dict(type='RandomFlipRGBT', flip_ratio=0.5),
        dict(
            type='NormalizeRGBT',
            rgb_mean=rgb_mean,
            rgb_std=rgb_std,
            lwir_mean=lwir_mean,
            lwir_std=lwir_std,
            to_rgb=True),   # to_rgb：先从bgr转换成rgb，再归一化
        dict(type='PadRGBT', size_divisor=32),
        dict(type='DefaultFormatBundleRGBT'),
        dict(type='CollectRGBT', keys=['rgb_img', 'lwir_img', 'gt_bboxes', 'gt_labels', 'gt_masks'])
        # dict(type='CollectRGBT', keys=['rgb_img', 'lwir_img', 'gt_bboxes', 'gt_labels', 'gt_bboxes_ignore', 'gt_masks'])
    ]
test_pipeline = [
    dict(type='LoadRGBTFromFile'),
    dict(
        type='MultiScaleFlipAugRGBT',
        img_scale=img_scale,
        flip=False,
        transforms=[
            dict(type='ResizeRGBT', keep_ratio=True),
            dict(type='RandomFlipRGBT'),
            dict(
                type='NormalizeRGBT',
                rgb_mean=rgb_mean,
                rgb_std=rgb_std,
                lwir_mean=lwir_mean,
                lwir_std=lwir_std,
                to_rgb=True),
            dict(type='PadRGBT', size_divisor=32),
            dict(type='RGBTImageToTensor', keys=['lwir_img', 'rgb_img']),
            dict(type='CollectRGBT', keys=['lwir_img', 'rgb_img'])
        ])
]

data = dict(
    samples_per_gpu=bs,
    workers_per_gpu=8,
    train=dict(
        type=dataset_type,
        ann_file=f'{data_root}/{modality}_train.json',
        classes=classes,
        img_prefix='',
        pipeline=train_pipeline,
        filter_empty_gt=False),
    val=dict(
        type=dataset_type,
        ann_file=f'{data_root}/{modality}_test.json',
        classes=classes,
        img_prefix='',
        pipeline=test_pipeline,
        filter_empty_gt=False),
    test=dict(
        type=dataset_type,
        ann_file=f'{data_root}/{modality}_test.json',
        classes=classes,
        img_prefix='',
        pipeline=test_pipeline,
        filter_empty_gt=False))

max_epochs = 25
num_last_epochs = 2
interval = 1
evaluation = dict(interval=1, metric='bbox', classwise=True)

optimizer = dict(
    type='SGD',
    lr=base_lr,
    momentum=0.9,
    weight_decay=0.0005,
    nesterov=True,
    paramwise_cfg=dict(norm_decay_mult=0.0, bias_decay_mult=0.0))
optimizer_config = dict(grad_clip=None)

lr_config = dict(
    policy='YOLOX',
    warmup='exp',
    by_epoch=False,
    warmup_by_epoch=True,
    warmup_ratio=1,
    warmup_iters=1,
    num_last_epochs=num_last_epochs,
    min_lr_ratio=0.05)
runner = dict(type='EpochBasedRunner', max_epochs=max_epochs)

custom_hooks = [
    dict(type='YOLOXModeSwitchHook', num_last_epochs=num_last_epochs, priority=48),
    dict(type='SyncNormHook', num_last_epochs=num_last_epochs, interval=interval, priority=48),
    dict(
        type='ExpMomentumEMAHook',
        resume_from=None,
        momentum=0.0001,
        priority=49)
]

auto_scale_lr = dict(enable=False, base_batch_size=64)


from collections import OrderedDict
# class SingletonMeta(type):
#     _instances = {}
#
#     def __call__(cls, *args, **kwargs):
#         if cls not in cls._instances:
#             cls._instances[cls] = super().__call__(*args, **kwargs)
#         return cls._instances[cls]
#
# class SingletonClass(metaclass=SingletonMeta):
#     pass
#
# obj1 = SingletonClass()
# obj2 = SingletonClass()
# print(id(obj1) == id(obj2))
def singleton(clas):
    incopies={}
    def get_copy(*args,**kwargs):
        if clas not in incopies:
            incopies[clas]=clas(*args,*kwargs)
        return incopies[clas]
    return get_copy
@singleton
class LRUCache:
    def __init__(self, capacity=16):
        self.__capacity = capacity
        self.cache = OrderedDict()

    def put(self, key, value):
        if key in self.cache:
            del self.cache[key]
        elif len(self.cache) >= self.__capacity:
            self.cache.popitem(last=False)
        self.cache[key] = value

    def get(self, key):
        if key not in self.cache:
            return None
        self.cache.move_to_end(key)
        return self.cache[key]

    @property
    def capacity(self):
        return self.__capacity
c1=LRUCache()
c2=LRUCache(20)
print(id(c1)==id(c2))


# https://leetcode.com/problems/is-subsequence/
class Solution(object):
    def isSubsequence(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        for c in s:
            pos = t.find(c)
            if pos==-1:
                return False
            t=t[pos+1:]
        return True


#Water Billing Program
from datetime import date, datetime
now  = datetime.now()
dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
print("Excuted time: ", dt_string)
name = "Kasidit_Boonchai"
std_id = "64100738"
lab = "Quiz-1"
print(lab +" Name : "+name+" Student ID: "+std_id)
print("-------------------------------------------")

print("Water billing program\n-------------------------------------------")
unit = int(input("Enter water unit(s): "))      #รับค่า unit จากคีย์บอร์ด
cost = 0        # Set ค่าตัวแปร Cost เป็น 0
if unit <= 15:      # ส่วนของเงื่อนไขในการคำนวณค่าน้ำ
    cost = 10       # Set ค่าตัวแปร Cost ตามเงื่อนไขการคำนวณราคาของแต่ละเงื่อนไข
elif unit <= 30:
    cost = 15
elif unit > 30:
    cost = 20
print("Total cost = {}".format(unit*cost+30))   # คำนวณและแสดงผลลัพธ์


#PyTorch lib
import paddle
import paddle.nn as nn
import paddle.nn.functional as F
#Tools lib

def print_network(net):
	num_params = 0
	for param in net.parameters():
		num_params += param.numel()
	print(net)
	print('Total number of parameters: %d' % num_params)

#Set iteration time
ITERATION = 4

#Model

ITERATION = 4

#Model
class Generator_lstm(nn.Layer):
    def __init__(self, recurrent_iter=4):
        super(Generator_lstm, self).__init__()
        self.iteration = recurrent_iter

        self.conv0 = nn.Sequential(
            nn.Conv2D(6, 32, 3, 1, 1),
            nn.ReLU()
            )
        self.res_conv1 = nn.Sequential(
            nn.Conv2D(32, 32, 3, 1, 1),
            nn.ReLU(),
            nn.Conv2D(32, 32, 3, 1, 1),
            nn.ReLU()
            )
        self.res_conv2 = nn.Sequential(
            nn.Conv2D(32, 32, 3, 1, 1),
            nn.ReLU(),
            nn.Conv2D(32, 32, 3, 1, 1),
            nn.ReLU()
            )
        self.res_conv3 = nn.Sequential(
            nn.Conv2D(32, 32, 3, 1, 1),
            nn.ReLU(),
            nn.Conv2D(32, 32, 3, 1, 1),
            nn.ReLU()
            )
        self.res_conv4 = nn.Sequential(
            nn.Conv2D(32, 32, 3, 1, 1),
            nn.ReLU(),
            nn.Conv2D(32, 32, 3, 1, 1),
            nn.ReLU()
            )
        self.res_conv5 = nn.Sequential(
            nn.Conv2D(32, 32, 3, 1, 1),
            nn.ReLU(),
            nn.Conv2D(32, 32, 3, 1, 1),
            nn.ReLU()
            )
        self.conv_i = nn.Sequential(
            nn.Conv2D(32 + 32, 32, 3, 1, 1),
            nn.Sigmoid()
            )
        self.conv_f = nn.Sequential(
            nn.Conv2D(32 + 32, 32, 3, 1, 1),
            nn.Sigmoid()
            )
        self.conv_g = nn.Sequential(
            nn.Conv2D(32 + 32, 32, 3, 1, 1),
            nn.Tanh()
            )
        self.conv_o = nn.Sequential(
            nn.Conv2D(32 + 32, 32, 3, 1, 1),
            nn.Sigmoid()
            )
        self.conv = nn.Sequential(
            nn.Conv2D(32, 3, 3, 1, 1),
            )

    def forward(self, input):
        batch_size, row, col = input.size(0), input.size(2), input.size(3)
        x = input
        h = paddle.zeros([batch_size, 32, row, col])
        c = paddle.zeros([batch_size, 32, row, col])

        x_list = []
        for i in range(self.iteration):
            x = paddle.concat((input, x), 1)
            x = self.conv0(x)
            resx = x
            x = F.relu(self.res_conv1(x) + resx)
            resx = x
            x = F.relu(self.res_conv2(x) + resx)
            resx = x
            x = F.relu(self.res_conv3(x) + resx)
            resx = x
            x = F.relu(self.res_conv4(x) + resx)
            resx = x
            x = F.relu(self.res_conv5(x) + resx)
            x = paddle.concat((x, h), 1)
            i = self.conv_i(x)
            f = self.conv_f(x)
            g = self.conv_g(x)
            o = self.conv_o(x)
            c = f * c + i * g
            h = o * F.tanh(c)
            x = self.conv(h)
            x_list.append(x)

        return x, x_list


import sklearn.metrics as sm
from sklearn import datasets
from sklearn.utils import shuffle
from sklearn.svm import SVR

data = datasets.load_boston()
X, y = shuffle(data.data, data.target, random_state = 7)

num_training = int(0.8 * len(X))
X_train, y_train = X[:num_training], y[:num_training]
X_test, y_test = X[num_training:], y[num_training:]

#creating linear model based on SVM
sv_regressor = SVR(kernel = 'linear', C = 1.0, epsilon = 0.1)

sv_regressor.fit(X_train, y_train)

y_test_pred = sv_regressor.predict(X_test)
print ("Mean absolute error = ", round(sm.mean_absolute_error(y_test, y_test_pred), 2))
print ("Median absolute error = ", round(sm.median_absolute_error(y_test, y_test_pred), 2))
print ("Mean squared error = ", round(sm.mean_squared_error(y_test, y_test_pred), 2))
print ("Explained variance score = ", round(sm. explained_variance_score(y_test, y_test_pred), 2))
print ("R2 score = ", round(sm.r2_score(y_test, y_test_pred), 2))

#test datapoint
test_data = [3.7, 0, 18.4, 1, 0.87, 5.95, 91, 2.5052, 26, 666, 20.2, 351.34, 15.27]
print ("Predicted price = ", sv_regressor.predict([test_data])[0])


import pytest

import numpy as np
from skimage.filters._gaussian import (gaussian, _guess_spatial_dimensions,
                                       difference_of_gaussians)
from skimage._shared import testing
from skimage._shared._warnings import expected_warnings

def test_negative_sigma():
    a = np.zeros((3, 3))
    a[1, 1] = 1.
    with testing.raises(ValueError):
        gaussian(a, sigma=-1.0)
    with testing.raises(ValueError):
        gaussian(a, sigma=[-1.0, 1.0])
    with testing.raises(ValueError):
        gaussian(a,
                 sigma=np.asarray([-1.0, 1.0]))

def test_null_sigma():
    a = np.zeros((3, 3))
    a[1, 1] = 1.
    assert np.all(gaussian(a, 0) == a)

def test_default_sigma():
    a = np.zeros((3, 3))
    a[1, 1] = 1.
    assert np.all(gaussian(a) == gaussian(a, sigma=1))

def test_energy_decrease():
    a = np.zeros((3, 3))
    a[1, 1] = 1.
    gaussian_a = gaussian(a, sigma=1, mode='reflect')
    assert gaussian_a.std() < a.std()

def test_multichannel():
    a = np.zeros((5, 5, 3))
    a[1, 1] = np.arange(1, 4)
    gaussian_rgb_a = gaussian(a, sigma=1, mode='reflect',
                              multichannel=True)
    # Check that the mean value is conserved in each channel
    # (color channels are not mixed together)
    assert np.allclose([a[..., i].mean() for i in range(3)],
                       [gaussian_rgb_a[..., i].mean() for i in range(3)])
    # Test multichannel = None
    with expected_warnings(['multichannel']):
        gaussian_rgb_a = gaussian(a, sigma=1, mode='reflect')
    # Check that the mean value is conserved in each channel
    # (color channels are not mixed together)
    assert np.allclose([a[..., i].mean() for i in range(3)],
                       [gaussian_rgb_a[..., i].mean() for i in range(3)])
    # Iterable sigma
    gaussian_rgb_a = gaussian(a, sigma=[1, 2], mode='reflect',
                              multichannel=True)
    assert np.allclose([a[..., i].mean() for i in range(3)],
                       [gaussian_rgb_a[..., i].mean() for i in range(3)])

def test_preserve_range():
    img = np.array([[10.0, -10.0], [-4, 3]], dtype=np.float32)
    gaussian(img, 1, preserve_range=True)

def test_4d_ok():
    img = np.zeros((5,) * 4)
    img[2, 2, 2, 2] = 1
    res = gaussian(img, 1, mode='reflect')
    assert np.allclose(res.sum(), 1)

def test_guess_spatial_dimensions():
    im1 = np.zeros((5, 5))
    im2 = np.zeros((5, 5, 5))
    im3 = np.zeros((5, 5, 3))
    im4 = np.zeros((5, 5, 5, 3))
    im5 = np.zeros((5,))
    testing.assert_equal(_guess_spatial_dimensions(im1), 2)
    testing.assert_equal(_guess_spatial_dimensions(im2), 3)
    testing.assert_equal(_guess_spatial_dimensions(im3), None)
    testing.assert_equal(_guess_spatial_dimensions(im4), 3)
    with testing.raises(ValueError):
        _guess_spatial_dimensions(im5)

@pytest.mark.parametrize(
    "dtype", [np.float32, np.float64]
)
def test_preserve_output(dtype):
    image = np.arange(9, dtype=dtype).reshape((3, 3))
    output = np.zeros_like(image, dtype=dtype)
    gaussian_image = gaussian(image, sigma=1, output=output,
                              preserve_range=True)
    assert gaussian_image is output

def test_output_error():
    image = np.arange(9, dtype=np.float32).reshape((3, 3))
    output = np.zeros_like(image, dtype=np.uint8)
    with pytest.raises(ValueError):
        gaussian(image, sigma=1, output=output,
                 preserve_range=True)

@testing.parametrize("s", [1, (2, 3)])
@testing.parametrize("s2", [4, (5, 6)])
def test_difference_of_gaussians(s, s2):
    image = np.random.rand(10, 10)
    im1 = gaussian(image, s)
    im2 = gaussian(image, s2)
    dog = im1 - im2
    dog2 = difference_of_gaussians(image, s, s2)
    assert np.allclose(dog, dog2)

@testing.parametrize("s", [1, (1, 2)])
def test_auto_sigma2(s):
    image = np.random.rand(10, 10)
    im1 = gaussian(image, s)
    s2 = 1.6 * np.array(s)
    im2 = gaussian(image, s2)
    dog = im1 - im2
    dog2 = difference_of_gaussians(image, s, s2)
    assert np.allclose(dog, dog2)

def test_dog_invalid_sigma_dims():
    image = np.ones((5, 5, 3))
    with testing.raises(ValueError):
        difference_of_gaussians(image, (1, 2))
    with testing.raises(ValueError):
        difference_of_gaussians(image, 1, (3, 4))
    with testing.raises(ValueError):
        difference_of_gaussians(image, (1, 2, 3), multichannel=True)

def test_dog_invalid_sigma2():
    image = np.ones((3, 3))
    with testing.raises(ValueError):
        difference_of_gaussians(image, 3, 2)
    with testing.raises(ValueError):
        difference_of_gaussians(image, (1, 5), (2, 4))


from fastapi import APIRouter
# from typing import Dict, List, Optional, Tuple
# from enum import Enum
import os
from ....rpc import Dero
from ....config.config import get_settings

router = APIRouter()
settings = get_settings()

DERO_DAEMON_URI = settings.DERO_DAEMON_URI

deroAPI = Dero(rpc_url=DERO_DAEMON_URI)

@router.get("/get_info")
def dero_get_info():
    try:
        resp = deroAPI.get_info()
        print('get_info()  ', resp)
        return resp
    except Exception as e:
        return e

@router.get("/get_height")
def get_dero_height():
    try:
        resp = deroAPI.get_height()
        print('get_height()  ', resp)
        return resp
    except Exception as e:
        return e

@router.get("/get_block_count")
def get_dero_block_count():
    try:
        resp = deroAPI.get_block_count()
        print('get_block_count()  ', resp)
        return resp
    except Exception as e:
        return e

# @router.get("/getinfo")
# async def z_getinfo():
#     try:
#         # p = os.popen(f"{settings.KOMODO_CLI} -ac_name=PIRATE getinfo")
#         # print(p)
#         # p = os.popen('/home/hauwei/komodo/src/komodo-cli -ac_name=PIRATE getinfo')
#         # p = p.read()
#         p = get_all_data()
#         print(p)
#         return {'command': p}

#     except Exception as e:
#         print('Exception: ', e)
#         return {'error': 'z_getinfo failed'}

# @router.get("/read/product/{id}", response_model=ReadProduct)
# async def get_product(product: ReadProduct = Depends(get_product_or_404)
#                       ) -> ReadProduct:
#     return product

# @router.get("/read/single_product/{id}", response_description="Get a single product",
#             response_model=ReadProduct)
# async def single_product(id: str,
#                          current_user: User = Depends(fastapi_users.current_user())):
#     '''Query by sku '''
#     if (p := await db["Products"].find_one({"sku": id})) is not None:
#         return p

#     return exception_400(
#         settings.READ_SINGLE_PROD_ERR, 'red')

# @router.get("/read/single_shipto/{id}", response_description="Read a single shipping address",
#             response_model=ReadShippingAddress)
# async def single_shipto(id: str,
#                         current_user: User = Depends(fastapi_users.current_user())):

#     '''Query by _id aka id '''
#     if not ObjectId.is_valid(id):
#         return exception_400(settings.OID_ERROR, 'red')

#     if (p := await db["ShipTo"].find_one({"_id": ObjectId(id)})) is not None:
#         return p

#     return exception_400(
#         settings.READ_SINGLE_SHIPTO_ERR, 'red')

# @router.get("/read/all_shipto", response_description="Read all shipping addresses",
#             response_model=List[ReadShippingAddress])
# async def all_shipto(
#     current_user: User = Depends(fastapi_users.current_user())):

#     p = await db["ShipTo"].find().to_list(100000)
#     if len(p) > 0:
#         return p

#     return exception_400(
#         settings.READ_ALL_SHIPTO_ERR, 'red')

# @router.get("/read/all_orders/{id}", response_model=List[ReadOrder])
# async def all_orders(id: str,
#                     current_user: User = Depends(fastapi_users.current_user())
#                     ):
#     lst = []
#     for d in await db['Order'].find(
#         {
#             'product_id': ObjectId(id),
#             'status': {'$nin': ['Cancelled', 'Complete', 'Reconcile', 'Shipped']}
#             # 'status': {'$in': ['Pending', 'In Production', 'Acknowledged', 'Back-Ordered']}
#         }).to_list(1000):
#         # print('d: ', d)
#         lst.append(d)
#     return lst

# # @app.post("/posts", response_model=PostDB, status_code=status.HTTP_201_CREATED)
# # async def create_post(
# #     post: PostCreate, database: AsyncIOMotorDatabase = Depends(get_database)
# # ) -> PostDB:
# #     post_db = PostDB(**post.dict())
# #     await database["posts"].insert_one(post_db.dict(by_alias=True))

# #     post_db = await get_post_or_404(post_db.id, database)

# #     return post_db

# # @app.patch("/posts/{id}", response_model=PostDB)
# # async def update_post(
# #     post_update: PostPartialUpdate,
# #     post: PostDB = Depends(get_post_or_404),
# #     database: AsyncIOMotorDatabase = Depends(get_database),
# # ) -> PostDB:
# #     await database["posts"].update_one(
# #         {"_id": post.id}, {"$set": post_update.dict(exclude_unset=True)}
# #     )

# #     post_db = await get_post_or_404(post.id, database)

# #     return post_db

# # @app.delete("/posts/{id}", status_code=status.HTTP_204_NO_CONTENT)
# # async def delete_post(
# #     post: PostDB = Depends(get_post_or_404),
# #     database: AsyncIOMotorDatabase = Depends(get_database),
# # ):
# #     await database["posts"].delete_one({"_id": post.id})


import random
import time

from practices.desing_patterns.state_pattern.colore import Color
from practices.desing_patterns.state_pattern.traffic_light import TrafficLight
from practices.desing_patterns.state_pattern.green_state import GreenState
from practices.desing_patterns.state_pattern.yellow_state import YellowState
from practices.desing_patterns.state_pattern.red_state import RedState

import pygame

class TrafficLightSimulation:

    def __init__(self):
        self.light = TrafficLight()
        self.cycle = 0
        self.screen = None
        self.width = 400
        self.height = 800

    def start(self) -> None:
        pygame.init()
        self.screen = pygame.display.set_mode((self.width, self.height))
        pygame.display.set_caption("Traffic Light Simulation")

        traffic_single = [GreenState(), RedState(), YellowState()]

        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    return
            self.light.traffic_single(traffic_single[random.randint(0, 2)])
            self.cycle += 1
            self.light.next()

            self.draw()
            pygame.display.update()
            time.sleep(2)

    def stop(self) -> None:
        pygame.quit()

    def draw(self) -> None:
        self.screen.fill((211, 211, 211))

        red_color = (255, 0, 0)
        yellow_color = (255, 255, 0)
        green_color = (0, 255, 0)

        outline_color = (70, 70, 70)

        rect_x = self.width // 4
        rect_y = self.height // 8
        rect_width = self.width // 2
        rect_height = self.height // 1.5

        pygame.draw.rect(self.screen, outline_color, (rect_x, rect_y, rect_width, rect_height), 10)

        radius = 50
        center_x = self.width // 2
        center_y = self.height // 2

        pygame.draw.circle(self.screen, outline_color, (center_x, center_y - (rect_height // 4)), radius + 5, 5)
        if self.light.get_color() == Color.RED:
            pygame.draw.circle(self.screen, red_color, (center_x, center_y - (rect_height // 4)), radius)
        else:
            pygame.draw.circle(self.screen, (211, 211, 211), (center_x, center_y - (rect_height // 4)), radius)

        pygame.draw.circle(self.screen, outline_color, (center_x, center_y), radius + 5, 5)
        if self.light.get_color() == Color.YELLOW:
            pygame.draw.circle(self.screen, yellow_color, (center_x, center_y), radius)
        else:
            pygame.draw.circle(self.screen, (211, 211, 211), (center_x, center_y), radius)

        pygame.draw.circle(self.screen, outline_color, (center_x, center_y + (rect_height // 4)), radius + 5, 5)
        if self.light.get_color() == Color.GREEN:
            pygame.draw.circle(self.screen, green_color, (center_x, center_y + (rect_height // 4)), radius)
        else:
            pygame.draw.circle(self.screen, (211, 211, 211), (center_x, center_y + (rect_height // 4)), radius)

if __name__ == "__main__":
    simulation = TrafficLightSimulation()

    try:
        #simulation.start()
      print(__name__)
    except KeyboardInterrupt:
        simulation.stop()


import sys
sys.stdin = open('input (43).txt', 'r')
from collections import deque
input = sys.stdin.readline

dr = [-1, 0, 1, 0]
dc = [0, 1, 0, -1]

# 불 있는 곳은 지훈이가 가지 못하기 때문에
# 지훈이 이동 후 불이 퍼지는 순서대로 해야한다.
def bfs():

    cnt = 0
    # 지훈이가 이동할 수 있을 때까지 while
    while jihoon:
        cnt += 1

        jihoon_length = len(jihoon) # 담겨진 위치들 이동하기
        fire_length = len(fire) # 담겨진 위치들 퍼지기

        # 지훈이
        for _ in range(jihoon_length):

            r, c = jihoon.popleft()

            if visited[r][c] == 2: # 만약에 2라면 불이 퍼진 지점이기 때문에 패스
                continue

            # 4방향 탐색
            for i in range(4):
                x = dr[i] + r
                y = dc[i] + c
                if -1 < x < R and -1 < y < C:
                    if visited[x][y] == 0 and arr[x][y] == '.':
                        jihoon.append((x, y))
                        visited[x][y] = 1
                else:
                    # 범위 벗어나면 탈출이라고 문제에 나옴
                    return cnt

        # 불 퍼지기
        for _ in range(fire_length):

            r, c = fire.popleft()

            for i in range(4):
                x = dr[i] + r
                y = dc[i] + c
                if -1 < x < R and -1 < y < C:
                    if visited[x][y] != 2 and arr[x][y] == '.': # 불 퍼진곳 아니면 다 체크
                        fire.append((x, y))
                        visited[x][y] = 2

    # 지훈이 밖으로 피신 못했네
    return -1

R, C = map(int, input().split())

arr = [list(input().rstrip()) for _ in range(R)]

visited = [[0] * C for _ in range(R)]

jihoon = deque() # 지훈이가 갈 수 있는 곳
fire = deque() # 불이 갈 수 있는 곳

for i in range(R):
    for j in range(C):
        if arr[i][j] == 'J': # 지훈이 위치 담기
            jihoon.append((i, j))
            visited[i][j] = 1

        if arr[i][j] == 'F': # 불 위치 담기
            fire.append((i, j))
            visited[i][j] = 2

result = bfs()

if result == -1:
    print('IMPOSSIBLE')
else:
    print(result)


import KMean
from numpy import *

class KMeanCluster:
    def __init__(self):
        self.centers = []
        self.dataMat = []

    def loadDataSet(self, filename):
        dataMat = []
        fr = open(filename)
        for line in fr.readlines():
            curLine = line.strip().split()
            fltLine = map(float, curLine)
            dataMat.append(fltLine)
        self.dataMat = mat(dataMat)
        # print(self.dataMat)
        return self.dataMat

    def train(self, k=4):
        centers, assess = KMean.binKMeans(self.dataMat, k)
        self.centers = centers

    def predict(self, sample):
        return KMean.getBestClass(self.centers, sample)

    def test(self):
        dataMat = self.loadDataSet('testSet.txt')
        self.train()
        print("centers:")
        print(self.centers)
        bestCenter, center = KMean.getBestClass(self.centers, [1, 1])
        print("best class:")
        print(bestCenter)
        print(center)

if __name__ == '__main__':
    kmean = KMeanCluster()
    kmean.test()


from random import randrange
from flask import Flask,request,render_template
import json
app=Flask(__name__)

datapencard = {}
panmat = []

class BackendError(Exception):
    def __init__(self):
        self.data=Exception
    def action(self):
        if self.data:
            return "there is error"
        else:
            return True

def get_pan_data(pan_number):
    num = randrange(10)
    if num in (8, 9):
        raise BackendError

    else:
        pandata = json.loads(pan_number)
        pan_number = pandata['pan_number']
        if pan_number in panmat:
            return datapencard[pan_number]
        else:
            return "data is not available for this pen card"

@app.route('/')
def student():
   return render_template('show.html')

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

@app.route('/result',methods = ['POST', 'GET'])
def result():
   if request.method == 'POST':
      result = request.form
      panno=None
      for key, value in result.items():
          panno=value
      data=get_pan_data(panno)
      return data

@app.route('/formfill')
def students():
   return render_template('pandata.html')

@app.route('/form',methods=['POST','GET'])
def put_pan_data():
    pan_number=None
    name=None
    dob=None
    father_name=None
    if request.method=='POST':
        datapan=request.form
        for key,value in datapan.items():
            if key=="pan_no":
                pan_number=value
            elif key=="name":
                name=value
            elif key=="dob":
                dob=value
            elif key=="fathername":
                father_name=value

        panmat.append(pan_number)
        datapencard[pan_number]={'pan': pan_number,
            'name':name,
            'dob': dob,
            'father_name': father_name}

    return "thankyou your response is recorded"

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


'''
No pseudocode here because the code template literally says what to do; that is my pseudocode
'''
#========The beginning of the class==========
class Shoe:

    def __init__(self, country, code, product, cost, quantity):
        '''
        In this function, you must initialise the following attributes:
            ● country,
            ● code,
            ● product,
            ● cost, and
            ● quantity.
        '''
        self.country = country
        self.code = code
        self.product = product
        self.cost = int(cost)
        self.quantity = int(quantity)

    def get_cost(self):
        '''
        Add the code to return the cost of the shoe in this method.
        '''
        return self.cost

    def get_quantity(self):
        '''
        Add the code to return the quantity of the shoes.
        '''
        return self.quantity

    def __str__(self):
        '''
        Add a code to returns a string representation of a class.
        '''
        return f"Identifier {self.code}, bound for {self.country}. Model {self.product}, costing {self.cost}. Quantity of stock: {self.quantity}"

#=============Shoe list===========
'''
The list will be used to store a list of objects of shoes.
'''
shoe_list = []
#==========Functions outside the class==============
def read_shoes_data():
    pass
    '''
    This function will open the file inventory.txt
    and read the data from this file, then create a shoes object with this data
    and append this object into the shoes list. One line in this file represents
    data to create one object of shoes. You must use the try-except in this function
    for error handling. Remember to skip the first line using your code.
    '''
    try:
        f = open("inventory.txt",'r')
        for line in f:
            #Skip the first line as it shouldn't be considered a data point
            if "Country,Code,Product,Cost,Quantity" in line:
                continue
            current_line = line.split(",")
            shoe_list.append(Shoe(current_line[0],current_line[1],current_line[2],current_line[3],current_line[4]))
        print("Database populated\n")
    except FileNotFoundError:
        print("File not found\n")
    except PermissionError:
        print("The inventory.txt file does exist but this program does not have the permissions to open it\n")
    except EOFError:
        print("The file is empty\n")
    except OSError:
        print("An error occurred while reading the file.\n")
    except IndexError:
        print("One or more of the lines in inventory.txt did not have enough data points to make a shoe object.\n")
    finally:
        f.close()

def capture_shoes():
    '''
    This function will allow a user to capture data
    about a shoe and use this data to create a shoe object
    and append this object inside the shoe list.
    '''
    try:
        country = input("What country is the set of shoes bound for?\n")
        code = input("What is the order's unique identifier?\n")
        product = input("What is the name of the product?\n")
        cost = int(input("What is the cost of the product? Enter a number\n"))
        quantity = int(input("How many pairs of shoes are in the order? Enter an integer\n"))
        shoe_list.append(Shoe(country,code,product,cost,quantity))
    except ValueError:
        print("A validation error occurred; a field which was supposed to have only a number did not have a number. Try again\n")
        capture_shoes()

def view_all():
    '''
    This function will iterate over the shoes list and
    print the details of the shoes returned from the __str__
    function. Optional: you can organise your data in a table format
    by using Python’s tabulate module.
    '''
    for count, order in enumerate(shoe_list):
        print(str(count) + " " +str(order)+"\n")

def re_stock():
    pass
    '''
    This function will find the shoe object with the lowest quantity,
    which is the shoes that need to be re-stocked. Ask the user if they
    want to add this quantity of shoes and then update it.
    This quantity should be updated on the file for this shoe.
    '''
    try:
        f=open("inventory.txt","r+")
        lowest_qty = shoe_list[0]
        for shoe in shoe_list:
            if shoe.get_quantity()<lowest_qty.get_quantity():
                lowest_qty = shoe
        user_add = int(input("How many pairs of shoes would you like to restock? enter a whole number\n"))
        initial_qty = lowest_qty.get_quantity()
        lowest_qty.quantity += user_add
        identifier = lowest_qty.code

        lines = []
        for line in f:
            lines.append(line.split(","))

        for line in lines:
            if identifier in line[1]:
                line[4]=str(lowest_qty.get_quantity())+"\n"

        for i in range(0,len(lines)):
            lines[i] = ",".join(lines[i])

        f.seek(0)
        f.truncate(0)
        f.writelines(lines)

    except ValueError:
        print("The number entered was not a valid integer. Try again\n")
        re_stock()
    except FileNotFoundError:
        print("The inventory.txt file does not exist\n")
    except PermissionError:
        print("The inventory.txt file does exist but this program does not have the permissions to open it\n")
    except EOFError:
        print("The file is empty\n")
    except OSError:
        print("An error occurred while reading the file.\n")
    except IndexError:
        print("There aren't any shoes in the shoe database yet\n")
    finally:
        f.close()

def seach_shoe():
    '''
    This function will search for a shoe from the list
    using the shoe code and return this object so that it will be printed.
    '''
    search_term = input("What is the shoe code?\n")
    for shoe in shoe_list:
        if search_term in shoe.code:
            print(shoe)
            return
    print("Shoe not found\n")

def value_per_item():
    '''
    This function will calculate the total value for each item.
    Please keep the formula for value in mind: value = cost * quantity.
    Print this information on the console for all the shoes.
    '''
    if len(shoe_list) == 0:
        print("There are no shoes in the database yet\n")
    for shoe in shoe_list:
        print(f"{shoe.code}: value = {(shoe.cost*shoe.quantity):.2f}")

def highest_qty():
    '''
    Write code to determine the product with the highest quantity and
    print this shoe as being for sale.
    '''
    try:
        highest_shoe = shoe_list[0]
        for shoe in shoe_list:
            if shoe.get_quantity()>highest_shoe.get_quantity():
                highest_shoe = shoe
        print(f"{highest_shoe.product} is for sale!")
    except IndexError:
        print("The list of shoes has not been populated yet\n")

#==========Main Menu=============
'''
Create a menu that executes each function above.
This menu should be inside the while loop. Be creative!
'''
while True:
    user_choice = input('''Welcome to the Nike stock viewer.\nMake one of the following choices:
    r - read shoe data in from inventory.txt and populate the shoe database
    c - capture shoe data from user input
    v - view all shoes in the database
    s - search the shoe database by code
    h - display the product with the highest quantity
    z - restock shoes of the lowest quantity
    m - calculate the monetary value per item
    q - exit the application
    ''').lower()
    if user_choice == "r":
        read_shoes_data()
    elif user_choice == "c":
        capture_shoes()
    elif user_choice == "v":
        view_all()
    elif user_choice == "s":
        seach_shoe()
    elif user_choice == "h":
        highest_qty()
    elif user_choice == "z":
        re_stock()
    elif user_choice == "m":
        value_per_item()
    elif user_choice == "q":
        print("Goodbye, enjoy your day")
        exit()
    else:
        print("That wasn't a valid option. Try again.\n")


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

# In[1]:

import numpy as np
import matplotlib.pyplot as plt
import os
from pathlib import Path
from keras.preprocessing import image

# # Load Images

# In[74]:

path = Path("./images/")
dirs = path.glob('*')

# In[136]:

labels_dict = {
    "cat":0,
    "dog":1,
    "horse":2,
    "human": 3
}

labels = []
img_data = []

for folder in dirs:
    label  = str(folder).split("\\")[-1][:-1]

    for image_path in folder.glob("*.jpg"):
        img = image.load_img(image_path,target_size=(64,64))
        img_array = image.img_to_array(img)
        img_data.append(img_array)
        labels.append(labels_dict[label])

# In[76]:

print(len(img_data))
print (labels)

# In[77]:

img_data = np.array(img_data)
labels = np.array(labels)

# In[78]:

(img_data).shape

# # Shuffle The Data

# In[79]:

combined = list(zip(img_data,labels))
import random
random.shuffle(combined)
img_data[:], labels[:] = zip(*combined)

# In[80]:

# def drawimg(img):
#     from matplotlib import pyplot as plt
#     plt.axis('off')
#     plt.imshow(img)
#     plt.show()

# In[81]:

# for i in range(5):
#     print(labels[i])
#     drawimg(img_data[i]/255.0)

# # Convert Data for one-for-one Classification

# In[82]:

M = img_data.shape[0]
img_data = img_data.reshape(M,-1)
print(img_data.shape)
print(labels.shape)

# In[83]:

CLASSES = len(np.unique(labels))
print(CLASSES)

# In[84]:

def classWiseData(X, y):
    data = {}
    for i in range(CLASSES):
        data[i] = []

    for i in range(X.shape[0]):
        data[y[i]].append(X[i])

    for k in data.keys():
        data[k] = np.array(data[k])

    return data

# In[85]:

data = classWiseData(img_data,labels)

# In[86]:

print(data[0].shape)
print(data[1].shape)
print(data[2].shape)
print(data[3].shape)

# In[87]:

def getDataPairForSVM(d1, d2):

    l1, l2 = d1.shape[0], d2.shape[0]

    samples = l1+l2
    features = d1.shape[1]

    X = np.zeros((samples, features))
    Y = np.zeros((features,))

    X[:l1,:] = d1
    X[l1:,:] = d2

    Y[:l1] = -1
    Y[l1:] = +1

    return X, Y

# # SVM Implementation

# In[88]:

class SVM:
    """Author: Manan Mongia"""
    def __init__(self,C = 1.0):
        self.C = C
        self.W = 0
        self.b = 0

    def hingeloss(self,C,W,b,X,Y):
        loss = 0.0

        loss = 0.5 * np.dot(W,W.T)

        m = X.shape[0]

        for i in range(m):
            ti = Y[i] * (np.dot(W.T,X[i]) + b)
            loss += self.C * max(0,1-ti)

        return loss

    def fit(self, X, Y,batch_size = 100, lr = 0.0001,maxItr=300):
        n = X.shape[1]
        m = X.shape[0]

        C = self.C
        W = np.zeros(n)
        bias = 0

        print("Intial Loss: ", str(self.hingeloss(C,W,bias,X,Y)))

        losses = []

        for i in range(maxItr):
            l = self.hingeloss(C,W,bias,X,Y)
            losses.append(l)
            #Batch Gradient Descent (Random Shuffling)
            ids = np.arange(m)
            np.random.shuffle(ids)

            for batch_start in range(0,m,batch_size):
                #Assume 0 gardient for the batch
                gradw = 0
                gradb = 0

                for j in range(batch_start,batch_start+batch_size):
                    if j<m :
                        i = ids[j]
                        ti =  Y[i]*(np.dot(W,X[i].T)+bias)

                        if ti > 1 :
                            gradw += 0
                            gradb += 0
                        else:
                            gradw += C * Y[i] * X[i]
                            gradb += C * Y[i]

                W = W - lr*W + lr*gradw
                bias = bias + lr*gradb

        self.W = W
        self.b = bias

        print ("Final loss: "+ str(losses[-1]))
        print()
        print("W: " + str(self.W))
        print("Bias: " + str(self.b))

        return W,bias,losses

# # Training

# In[89]:

mysvm = SVM(C = 1)

# In[118]:

def trainSVMs(x,y, lr = 0.0001,maxItr=300):
    svm_classifiers = {}
    for i in range(CLASSES):
        svm_classifiers[i] = {}
        for j in range(i+1,CLASSES):
            if i != j :
                print ("i: ",i,"j: ",j)
                xpair, ypair = getDataPairForSVM(data[i], data[j])
                print("lr: ",str(lr), "maxItr: ",str(maxItr))
                wts, bias, losses = mysvm.fit(xpair, ypair, lr = lr ,maxItr = maxItr)
#                 plt.plot(losses)
#                 plt.show()
                svm_classifiers[i][j] = (wts, bias)

    return svm_classifiers

# In[126]:

svm_classifiers = trainSVMs(img_data, labels, lr = 0.00000000001,maxItr=2000)

# In[129]:

import pprint as pp

# In[130]:

pp.pprint(svm_classifiers)

# # Prediction

# In[131]:

def binaryPredict(x,wts,bias):
    z = np.dot(x,wts.T) + bias
    if z >= 0:
        return +1
    else :
        return -1

# In[132]:

def predict(x):
    count = np.zeros(CLASSES)
    for i in range(CLASSES):
        for j in range(i+1, CLASSES):
            wts, bias = svm_classifiers[i][j]
            z = binaryPredict(x, wts, bias)
            if z == 1:
                count[j] += 1
            else:
                count[i] += 1

#     print(count)
    final_prediction = np.argmax(count)
    return final_prediction

# In[133]:

print(predict(img_data[0]))
print (label[0])

# In[134]:

def accuracy():
    count = 0
    for i in range(img_data.shape[0]):
        pred = predict(img_data[i])
        if pred == labels[i]:
            count += 1

    return count/img_data.shape[0]

# In[135]:

print(accuracy())


import re
import urllib2
from views import LoginView, ScoreView, HighestScoreView

class Urls(object):
    """
    Match url patterns and get the right view handler
    """
    def __init__(self):
        urls = [
            ('^/(?P<user_id>\d+)/login/?$', LoginView),
            ('^/(?P<level_id>\d+)/score/?$', ScoreView),
            ('^/(?P<level_id>\d+)/highscorelist/?$', HighestScoreView)
        ]

        self.urls = [(re.compile(r), c) for r, c in urls]

    def match(self, url):
        """
        If a valid url is found, it returns an initialized view, else None
        """
        parsed = urllib2.urlparse.urlparse(url)

        for regex, klass in self.urls:
            res = regex.match(parsed.path)
            if res:
                kwargs = res.groupdict()
                kwargs['query_params'] = urllib2.urlparse.parse_qs(parsed.query)
                return klass(**kwargs)

        return None


from typing import List

'''
39. Combination Sum

Given a set of candidate numbers (candidates) (without duplicates) and a target
number (target), find all unique combinations in candidates where the candidate
numbers sums to target.

The same repeated number may be choosen from canidates limited number of times.

Note:

    * All numbers (including target) will be positive integers.
    * The solution set must not contain duplicate combinations.

Example 1:

    Input: candidates = [2, 3, 6, 7], target = 7,
    A solution set is:
    [
        [7],
        [2, 2, 3],
    ]

Example 2:

    Input: canidates = [2, 3, 5], target = 8,
    A Solution set is:
    [
        [2, 2, 2, 2],
        [2, 3, 3],
        [3, 5],
    ]
'''

EXAMPLES = [
    (
        ([2, 3, 6, 7], 7),
        [
            [7],
            [2, 2, 3],
        ],
    ),
    (
        ([2, 3, 5], 8),
        [
            [2, 2, 2, 2],
            [2, 3, 3],
            [3, 5],
        ],
    ),
]

class Solution:
    def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
        result = []

        for i, v in enumerate(candidates):
            count = int(target / v)
            for c in range(count, 0, -1):
                new_target = target - c * v
                if new_target == 0:
                    result.append(
                        count * [v]
                    )
                else:
                    sub_results = self.combinationSum(candidates[i+1:], new_target)
                    if len(sub_results):
                        for sub_result in sub_results:
                            result.append(
                                c * [v] + sub_result
                            )

        return result


'''
A very basic NN to detect a linear function
'''

import numpy as np
from tensorflow import keras

model = keras.Sequential(layers=keras.layers.Dense(units=1, input_shape=[1]))
model.compile(optimizer='sgd', loss='mean_squared_error')

X = np.array([-2.0, 0.0, 1.0, 3.0 , 4.0], dtype='float32')
Y = np.array([-5.0, -1.0, 1.0, 5.0, 7.0], dtype='float32')

model.fit(X, Y, epochs=500)

model.predict([10.0])


#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# __author__ : YiXuan
# __date__ : 1/17/2021 12:47 PM
# __software__ : PyCharm

'''
简单计算器
'''

class Calculator:
    def __init__(self, num1, num2):
        self.num1 = num1
        self.num2 = num2

    # 相加
    def add(self):
        return self.num1 + self.num2

    # 相减
    def subtract(self):
        return self.num1 - self.num2

    # 相乘
    def multiply(self):
        return self.num1 * self.num2

    # 相除
    def divide(self):
        return self.num1 / self.num2

if __name__ == '__main__':
    while True:
        input_txt = input('***简单计算器（输入q退出）***\n:')
        if input_txt in ['q', 'Q']:
            break

        try:
            num1 = int(input('请输入第一个数字: '))
            num2 = int(input('请输入第二个数字: '))
            choice = int(input(' 1)相加\n 2)相减\n 3)相乘\n 4)相除\n请输入你的选择: '))

            calculator = Calculator(num1, num2)
            if choice == 1:
                result = calculator.add()
                print(f'{num1} + {num2} = {result}')
            elif choice == 2:
                result = calculator.subtract()
                print(f'{num1} - {num2} = {result}')
            elif choice == 3:
                result = calculator.multiply()
                print(f'{num1} * {num2} = {result}')
            elif choice == 4:
                if num2 == 0:
                    print('分母不能为0！')
                    continue
                result = calculator.divide()
                print(f'{num1} / {num2} = {result}')
            else:
                print('请选择正确的序号')

        except ValueError:
            print('请输入正确的数字！')


from django.shortcuts import render
from django.http import HttpResponse
from konlpy.tag import Okt
from sklearn.feature_extraction.text import TfidfVectorizer
import scipy
import re
import numpy as np
from tensorflow.keras.models import load_model
# Create your views here.

word3=''
# 첫 화면 연결 부분
def index(request):
    return render(request,'input.html')
# 각 분류를 연결해주는 부분
def go_game(request):
    return render(request,'game.html')

def go_web(request):
    return render(request,'web.html')

def go_mobile(request):
    return render(request,'mobile.html')

def word_exe(request):
    global word3
    word = request.GET['a1']

    import pickle
    import mini_web.views
    with open('../tf_main.pickle', 'rb') as f:
        tf_main_v = pickle.load(f)
        tf_main = pickle.load(f)
    word2=mini_web.views.word_predict2(word,tf_main_v,tf_main)

    data_dict={
        'result_list' : word3
    }

    if word2=='game':
        return render(request,'game.html',data_dict)
    elif word2=='web':
        return render(request,'web.html',data_dict)
    elif word2=='mobile':
        return render(request,'mobile.html',data_dict)
    else:
        return render(request,'input.html')

def word_predict2(word,tf2,to_tf):
    global word3
    ac_label=['game','mobile','web']
    okt = Okt()
    loaded_model = load_model('../model_tf-idf_end/tf-200-0.2624.hdf5')
    word_input = [okt.morphs(word.replace(' ', ''))]
    stopwords = ['을', '를', '이', '가', '은', '는','이','그','있','n','nn','t','의','되','라','한','다','하','더','에','서','나',
            '과','고','며','와','지','해','도','지','못','안','않','로','들','된','것','에는','안녕',
            '반갑','내','등','할','자','있다','','직','군','위','식','각','출','창','시','수','및','신','영','화','살','여','양','송','대','데',
            '연','구','살','장','또','적','D','력','인','향','년','후','생','기','봐','문','니','사','본','제','작','관','일','분','야',
            '중','거','명','어','개','타','제','또는','요','전','몇','중','에서','있는데요','된다','또한','되','돼','않','으로','만','있습니다','하는','하는것이',
            '만','푹','지금','하는','곳','로는','한다','이다','에서는','이라','고하','에서보다','이라고도','경','제등','이서','이라는',
            '가는','되는','마다','야하며','야한다','필요하다','되었을','필요한다','게','하고','좋아하고','원하는','통해','하여이에','고도',
            '세','드리지','않습니다','잉','님','합니다']
    f = open('../kor_stopword.txt',encoding='UTF8')
    lines = f.readlines()
    for line in lines:
        line = line.strip()
        stopwords.append(line)
    f.close()

    test_tt=[]
    for i, document in enumerate(word_input):
        temp_str = ''
        for doc in document:
            doc = re.sub(r'[^ ㄱ-ㅣ가-힣A-Za-z]', '', str(doc))
            if doc not in stopwords:
                temp_str = temp_str + ''.join(list(doc)) + ' '
        test_tt.append(temp_str)

    print(test_tt)

    vect = TfidfVectorizer(min_df=1,decode_error='ignore')
    tf2_input = vect.fit_transform(to_tf)
    new_vect = vect.transform(test_tt)
    scipy.sparse.csr_matrix.sort_indices(tf2_input)
    scipy.sparse.csr_matrix.sort_indices(new_vect)

    webtest=ac_label[np.argmax(loaded_model.predict(new_vect), axis=1)[0]]
    word3=test_tt
    return webtest


from random import randint as r
e = 'easy'
m = 'medium'
h = 'hard'

difficulty = input('I want to play a game. Choose difficulty now. (easy/medium/hard:  ').lower()
if difficulty[0] == 'e':
    rand = r(1,10)
    difficulty ='Easy'
    lives = 5
    n = 10
elif difficulty[0] == 'm':
    rand = r(1,100)
    difficulty = 'Medium'
    lives = 10
    n = 100
elif difficulty[0] == 'h':
    rand = r(1,1000)
    difficulty = 'Hard'
    lives = 10
    n = 1000
else:
    difficulty = input('Thats..not one of the three choices. Alright lets try this one more time! Choose difficulty now. (easy/medium/hard):  ').lower()
    if difficulty[0] == 'e':
        rand = r(1,10)
        difficulty ='Easy'
        lives = 5
        n = 10
    elif difficulty[0] == 'm':
        rand = r(1,100)
        difficulty = 'Medium'
        lives = 10
        n = 100
    elif difficulty[0] == 'h':
        rand = r(1,1000)
        difficulty = 'Hard'
        lives = 10
        n = 1000
    else:
        print("Bless your heart, maybe this game isn't for you...")
        quit('BYEEEEEE')
print(f"Okay! {difficulty} it is!! Guess a number between 1 and {n}. You'll start with {lives} lives, dont waste 'em!")
user_guess = int(input(f"I'm thinking of a number, can you guess what it is ({lives} lives)?  "))

if user_guess == rand:
    print(f'Wow.. good job, it was {rand}! First try, too!')

while rand != user_guess:
    if lives >= 0:
        if rand > user_guess:
            lives = lives - 1
            user_guess = int(input(f'Try higher ({lives} lives):  '))

        elif rand < user_guess:
            lives = lives - 1
            user_guess = int(input(f'Try lower ({lives} lives):  '))

if user_guess == rand:
    print(f'Wow.. good job, it was {rand}! And you still have {lives} lives!')
if lives < 0:
    print('Oh no, you ran out of lives! Better luck next time!')


#!/usr/bin/env python3
"""
12. Type Checking
"""

from typing import Tuple, List, Any

def zoom_array(lst: Tuple, factor: int = 2) -> List:
    """
    Returns a new list with each element multiplied
    by factor.
    """
    zoomed_in: List = [
        item for item in lst
        for i in range(factor)
    ]
    return zoomed_in

array: Tuple = (12, 72, 91)

zoom_2x = zoom_array(array)

zoom_3x = zoom_array(array, 3)


import os
import json
import base64
import win32crypt
from Crypto.Cipher import AES
import sqlite3

'''
[(0, 'creation_utc', 'INTEGER', 1, None, 0), (1, 'host_key', 'TEXT', 1, None, 0), (2, 'name', 'TEXT', 1, None, 0), (3, 'value', '
TEXT', 1, None, 0), (4, 'path', 'TEXT', 1, None, 0), (5, 'expires_utc', 'INTEGER', 1, None, 0), (6, 'is_secure', 'INTEGER', 1, No
ne, 0), (7, 'is_httponly', 'INTEGER', 1, None, 0), (8, 'last_access_utc', 'INTEGER', 1, None, 0), (9, 'has_expires', 'INTEGER', 1
, '1', 0), (10, 'is_persistent', 'INTEGER', 1, '1', 0), (11, 'priority', 'INTEGER', 1, '1', 0), (12, 'encrypted_value', 'BLOB', 0
, "''", 0), (13, 'samesite', 'INTEGER', 1, '-1', 0), (14, 'source_scheme', 'INTEGER', 1, '0', 0)]
'''
sql = """
SELECT
    host_key, name, path,encrypted_value as value
FROM
    cookies
"""

def get_decrypted_key():
    path = r'%LocalAppData%\Google\Chrome\User Data\Local State'
    path = os.path.expandvars(path)
    with open(path, 'r', encoding='utf8') as file:
        encrypted_key = json.loads(file.read())['os_crypt']['encrypted_key']
    encrypted_key = base64.b64decode(encrypted_key)  # Base64 decoding
    encrypted_key = encrypted_key[5:]  # Remove DPAPI
    decrypted_key = win32crypt.CryptUnprotectData(encrypted_key, None, None, None, 0)[1]  # Decrypt key
    # print("decrypt",decrypted_key)
    return decrypted_key

# get cookie
def get_chrome_cookie():
    cookies_path = os.environ['HOMEPATH'] + r'\AppData\Local\Google\Chrome\User Data\Default\Cookies'
    cookies_path = os.path.join(os.environ['LOCALAPPDATA'], os.environ['HOMEPATH'], cookies_path)
    con = sqlite3.connect(cookies_path)
    res = con.execute(sql).fetchall()
    # names = con.execute('PRAGMA  table_info([cookies])').fetchall()
    # print(names)
    con.close()
    # print(res)
    return res

def decrypt_chrome_cookie(decrypted_key, data):
    # data = bytes.fromhex('763130...') # the encrypted cookie
    if data[:3] == b'v10':
        nonce = data[3:3 + 12]
        ciphertext = data[3 + 12:-16]
        tag = data[-16:]
        cipher = AES.new(decrypted_key, AES.MODE_GCM, nonce=nonce)
        # plaintext = cipher.decrypt_and_verify(ciphertext, tag) # the decrypted cookie
        plaintext = cipher.decrypt(ciphertext)
        # print(plaintext)
        return plaintext
    else:
        # print('old cookie none decrypt')
        return ""

def fetch_chrome_cookies(domain=''):
    res = get_chrome_cookie()
    list = []
    for i in res:
        if domain in i[0]:
            item = {}
            # print(type(i[3]),i[3])
            data = i[3]  # the encrypted cookie
            key = get_decrypted_key()
            plaintext = decrypt_chrome_cookie(key, data)
            plaintext = str(plaintext, encoding="utf-8")
            # print("host:", i[0], "name:", i[1], "path:", i[2], "value:", plaintext)
            item["host"] = i[0]
            item["name"] = i[1]
            item["path"] = i[2]
            item["value"] = plaintext
            list.append(item)
    return list

def fetch_chrome_cookie(domain=''):
    cookie_list = fetch_chrome_cookies(domain)
    cookieValue = ''
    for item in cookie_list:
        cookieValue += item['name'] + '=' + item['value'] + '; '
    # print("fetch_chrome_cookie:" + cookieValue)
    return cookieValue[:-1]

if __name__ == '__main__':
    print(fetch_chrome_cookie('xiaomi.com'))


import os
import torch
from PIL import Image
from torchvision.transforms import ToTensor

from utils.misc import infer_deepfill
from model import load_model

import yaml
try:
    from yaml import CLoader as Loader
except ImportError:
    from yaml import Loader

def _load_models(config_path, device='cuda'):
    with open(config_path, 'r') as stream:
        config = yaml.load(stream, Loader)

    config = {name:cfg for name, cfg in config.items()
                       if os.path.exists(cfg['path'])}

    loaded_models = {}

    for name, cfg in config.items():
        is_loaded = False
        if cfg['load_at_startup']:
            model = load_model(cfg['path'], device)
            loaded_models[name] = model
            is_loaded = True
        config[name]['is_loaded'] = is_loaded

    return config, loaded_models

class Inpainter:
    def __init__(self, device=None):
        self.available_models = None
        self.loaded_models = None
        self.host = ""
        self.device = torch.device('cuda'
                      if torch.cuda.is_available() else 'cpu') \
                      if device is None else device

    def load_models(self, config_path):
        available_models, loaded_models = _load_models(config_path, self.device)
        self.available_models = available_models
        self.loaded_models = loaded_models

    def get_model_info(self):
        model_data = []
        for name, cfg in self.available_models.items():
            model_dict = cfg.copy()
            model_dict['name'] = name
            model_dict['type'] = 'df'
            model_data.append(model_dict)

        return model_data

    def check_requested_models(self, models):
        for name in models:
            if not name in self.loaded_models:
                path = self.available_models[name]['path']
                model = load_model(path, self.device)
                if model is None:
                    print(f"model @ {path} not found!")
                    continue
                self.loaded_models[name] = model
                self.available_models['is_loaded'] = True
                print(f'Loaded model: {name}')

    def inpaint(self, image, mask, models, max_size=512):

        req_models = models.split(',')
        self.check_requested_models(req_models)

        image_pil = Image.open(image.file).convert('RGB')
        mask_pil = Image.open(mask.file)

        mw, mh = mask_pil.size
        scale = max_size / max(mw, mh)

        mask_pil = mask_pil.resize(
            (max_size, int(scale*mh)) if mw > mh else (int(scale*mw), max_size))
        image_pil = image_pil.resize(mask_pil.size)

        image, mask = ToTensor()(image_pil), ToTensor()(mask_pil)

        response_data = []
        for idx_model, model_name in enumerate(req_models):
            return_vals = self.available_models[model_name]['return_vals']
            model_output_list = []
            outputs = infer_deepfill(
                self.loaded_models[model_name],
                image.to(self.device),
                mask.to(self.device),
                return_vals=return_vals
            )
            for idx_out, output in enumerate(outputs):
                Image.fromarray(output) \
                    .save(f'app/files/out_{idx_model}_{idx_out}.png')

                model_output_list.append({
                    'name': return_vals[idx_out],
                    'file': f'{self.host}/files/out_{idx_model}_{idx_out}.png'
                })

            model_output_dict = {
                'name': model_name,
                'output': model_output_list
            }
            response_data.append(model_output_dict)

        return response_data


"""Mimic doctest in Sphinx.

The extension automatically execute code snippets and checks their results.
"""

from __future__ import annotations

import doctest
import re
import sys
import time
from io import StringIO
from os import path
from typing import TYPE_CHECKING, Any, Callable

from docutils import nodes
from docutils.parsers.rst import directives
from packaging.specifiers import InvalidSpecifier, SpecifierSet
from packaging.version import Version

import sphinx
from sphinx.builders import Builder
from sphinx.locale import __
from sphinx.util import logging
from sphinx.util.console import bold  # type: ignore[attr-defined]
from sphinx.util.docutils import SphinxDirective
from sphinx.util.osutil import relpath

if TYPE_CHECKING:
    from collections.abc import Iterable, Sequence

    from docutils.nodes import Element, Node, TextElement

    from sphinx.application import Sphinx
    from sphinx.util.typing import OptionSpec

logger = logging.getLogger(__name__)

blankline_re = re.compile(r'^\s*<BLANKLINE>', re.MULTILINE)
doctestopt_re = re.compile(r'#\s*doctest:.+$', re.MULTILINE)

def is_allowed_version(spec: str, version: str) -> bool:
    """Check `spec` satisfies `version` or not.

    This obeys PEP-440 specifiers:
    https://peps.python.org/pep-0440/#version-specifiers

    Some examples:

        >>> is_allowed_version('<=3.5', '3.3')
        True
        >>> is_allowed_version('<=3.2', '3.3')
        False
        >>> is_allowed_version('>3.2, <4.0', '3.3')
        True
    """
    return Version(version) in SpecifierSet(spec)

# set up the necessary directives

class TestDirective(SphinxDirective):
    """
    Base class for doctest-related directives.
    """

    has_content = True
    required_arguments = 0
    optional_arguments = 1
    final_argument_whitespace = True

    def run(self) -> list[Node]:
        # use ordinary docutils nodes for test code: they get special attributes
        # so that our builder recognizes them, and the other builders are happy.
        code = '\n'.join(self.content)
        test = None
        if self.name == 'doctest':
            if '<BLANKLINE>' in code:
                # convert <BLANKLINE>s to ordinary blank lines for presentation
                test = code
                code = blankline_re.sub('', code)
            if doctestopt_re.search(code) and 'no-trim-doctest-flags' not in self.options:
                if not test:
                    test = code
                code = doctestopt_re.sub('', code)
        nodetype: type[TextElement] = nodes.literal_block
        if self.name in ('testsetup', 'testcleanup') or 'hide' in self.options:
            nodetype = nodes.comment
        if self.arguments:
            groups = [x.strip() for x in self.arguments[0].split(',')]
        else:
            groups = ['default']
        node = nodetype(code, code, testnodetype=self.name, groups=groups)
        self.set_source_info(node)
        if test is not None:
            # only save if it differs from code
            node['test'] = test
        if self.name == 'doctest':
            node['language'] = 'pycon'
        elif self.name == 'testcode':
            node['language'] = 'python'
        elif self.name == 'testoutput':
            # don't try to highlight output
            node['language'] = 'none'
        node['options'] = {}
        if self.name in ('doctest', 'testoutput') and 'options' in self.options:
            # parse doctest-like output comparison flags
            option_strings = self.options['options'].replace(',', ' ').split()
            for option in option_strings:
                prefix, option_name = option[0], option[1:]
                if prefix not in '+-':
                    self.state.document.reporter.warning(
                        __("missing '+' or '-' in '%s' option.") % option,
                        line=self.lineno)
                    continue
                if option_name not in doctest.OPTIONFLAGS_BY_NAME:
                    self.state.document.reporter.warning(
                        __("'%s' is not a valid option.") % option_name,
                        line=self.lineno)
                    continue
                flag = doctest.OPTIONFLAGS_BY_NAME[option[1:]]
                node['options'][flag] = (option[0] == '+')
        if self.name == 'doctest' and 'pyversion' in self.options:
            try:
                spec = self.options['pyversion']
                python_version = '.'.join([str(v) for v in sys.version_info[:3]])
                if not is_allowed_version(spec, python_version):
                    flag = doctest.OPTIONFLAGS_BY_NAME['SKIP']
                    node['options'][flag] = True  # Skip the test
            except InvalidSpecifier:
                self.state.document.reporter.warning(
                    __("'%s' is not a valid pyversion option") % spec,
                    line=self.lineno)
        if 'skipif' in self.options:
            node['skipif'] = self.options['skipif']
        if 'trim-doctest-flags' in self.options:
            node['trim_flags'] = True
        elif 'no-trim-doctest-flags' in self.options:
            node['trim_flags'] = False
        return [node]

class TestsetupDirective(TestDirective):
    option_spec: OptionSpec = {
        'skipif': directives.unchanged_required,
    }

class TestcleanupDirective(TestDirective):
    option_spec: OptionSpec = {
        'skipif': directives.unchanged_required,
    }

class DoctestDirective(TestDirective):
    option_spec: OptionSpec = {
        'hide': directives.flag,
        'no-trim-doctest-flags': directives.flag,
        'options': directives.unchanged,
        'pyversion': directives.unchanged_required,
        'skipif': directives.unchanged_required,
        'trim-doctest-flags': directives.flag,
    }

class TestcodeDirective(TestDirective):
    option_spec: OptionSpec = {
        'hide': directives.flag,
        'no-trim-doctest-flags': directives.flag,
        'pyversion': directives.unchanged_required,
        'skipif': directives.unchanged_required,
        'trim-doctest-flags': directives.flag,
    }

class TestoutputDirective(TestDirective):
    option_spec: OptionSpec = {
        'hide': directives.flag,
        'no-trim-doctest-flags': directives.flag,
        'options': directives.unchanged,
        'pyversion': directives.unchanged_required,
        'skipif': directives.unchanged_required,
        'trim-doctest-flags': directives.flag,
    }

parser = doctest.DocTestParser()

# helper classes

class TestGroup:
    def __init__(self, name: str) -> None:
        self.name = name
        self.setup: list[TestCode] = []
        self.tests: list[list[TestCode] | tuple[TestCode, None]] = []
        self.cleanup: list[TestCode] = []

    def add_code(self, code: TestCode, prepend: bool = False) -> None:
        if code.type == 'testsetup':
            if prepend:
                self.setup.insert(0, code)
            else:
                self.setup.append(code)
        elif code.type == 'testcleanup':
            self.cleanup.append(code)
        elif code.type == 'doctest':
            self.tests.append([code])
        elif code.type == 'testcode':
            # "testoutput" may replace the second element
            self.tests.append((code, None))
        elif code.type == 'testoutput':
            if self.tests:
                latest_test = self.tests[-1]
                if len(latest_test) == 2:
                    self.tests[-1] = [latest_test[0], code]
        else:
            raise RuntimeError(__('invalid TestCode type'))

    def __repr__(self) -> str:
        return (f'TestGroup(name={self.name!r}, setup={self.setup!r}, '
                f'cleanup={self.cleanup!r}, tests={self.tests!r})')

class TestCode:
    def __init__(self, code: str, type: str, filename: str,
                 lineno: int, options: dict | None = None) -> None:
        self.code = code
        self.type = type
        self.filename = filename
        self.lineno = lineno
        self.options = options or {}

    def __repr__(self) -> str:
        return (f'TestCode({self.code!r}, {self.type!r}, filename={self.filename!r}, '
                f'lineno={self.lineno!r}, options={self.options!r})')

class SphinxDocTestRunner(doctest.DocTestRunner):
    def summarize(self, out: Callable, verbose: bool | None = None,  # type: ignore[override]
                  ) -> tuple[int, int]:
        string_io = StringIO()
        old_stdout = sys.stdout
        sys.stdout = string_io
        try:
            res = super().summarize(verbose)
        finally:
            sys.stdout = old_stdout
        out(string_io.getvalue())
        return res

    def _DocTestRunner__patched_linecache_getlines(self, filename: str,
                                                   module_globals: Any = None) -> Any:
        # this is overridden from DocTestRunner adding the try-except below
        m = self._DocTestRunner__LINECACHE_FILENAME_RE.match(  # type: ignore[attr-defined]
            filename)
        if m and m.group('name') == self.test.name:
            try:
                example = self.test.examples[int(m.group('examplenum'))]
            # because we compile multiple doctest blocks with the same name
            # (viz. the group name) this might, for outer stack frames in a
            # traceback, get the wrong test which might not have enough examples
            except IndexError:
                pass
            else:
                return example.source.splitlines(True)
        return self.save_linecache_getlines(  # type: ignore[attr-defined]
            filename, module_globals)

# the new builder -- use sphinx-build.py -b doctest to run

class DocTestBuilder(Builder):
    """
    Runs test snippets in the documentation.
    """
    name = 'doctest'
    epilog = __('Testing of doctests in the sources finished, look at the '
                'results in %(outdir)s/output.txt.')

    def init(self) -> None:
        # default options
        self.opt = self.config.doctest_default_flags

        # HACK HACK HACK
        # doctest compiles its snippets with type 'single'. That is nice
        # for doctest examples but unusable for multi-statement code such
        # as setup code -- to be able to use doctest error reporting with
        # that code nevertheless, we monkey-patch the "compile" it uses.
        doctest.compile = self.compile  # type: ignore[attr-defined]

        sys.path[0:0] = self.config.doctest_path

        self.type = 'single'

        self.total_failures = 0
        self.total_tries = 0
        self.setup_failures = 0
        self.setup_tries = 0
        self.cleanup_failures = 0
        self.cleanup_tries = 0

        date = time.strftime('%Y-%m-%d %H:%M:%S')

        outpath = self.outdir.joinpath('output.txt')
        self.outfile = outpath.open('w', encoding='utf-8')  # NoQA: SIM115
        self.outfile.write(('Results of doctest builder run on %s\n'
                            '==================================%s\n') %
                           (date, '=' * len(date)))

    def _out(self, text: str) -> None:
        logger.info(text, nonl=True)
        self.outfile.write(text)

    def _warn_out(self, text: str) -> None:
        if self.app.quiet or self.app.warningiserror:
            logger.warning(text)
        else:
            logger.info(text, nonl=True)
        self.outfile.write(text)

    def get_target_uri(self, docname: str, typ: str | None = None) -> str:
        return ''

    def get_outdated_docs(self) -> set[str]:
        return self.env.found_docs

    def finish(self) -> None:
        # write executive summary
        def s(v: int) -> str:
            return 's' if v != 1 else ''
        repl = (self.total_tries, s(self.total_tries),
                self.total_failures, s(self.total_failures),
                self.setup_failures, s(self.setup_failures),
                self.cleanup_failures, s(self.cleanup_failures))
        self._out('''
Doctest summary
===============
%5d test%s
%5d failure%s in tests
%5d failure%s in setup code
%5d failure%s in cleanup code
''' % repl)
        self.outfile.close()

        if self.total_failures or self.setup_failures or self.cleanup_failures:
            self.app.statuscode = 1

    def write(self, build_docnames: Iterable[str] | None, updated_docnames: Sequence[str],
              method: str = 'update') -> None:
        if build_docnames is None:
            build_docnames = sorted(self.env.all_docs)

        logger.info(bold('running tests...'))
        for docname in build_docnames:
            # no need to resolve the doctree
            doctree = self.env.get_doctree(docname)
            self.test_doc(docname, doctree)

    def get_filename_for_node(self, node: Node, docname: str) -> str:
        """Try to get the file which actually contains the doctest, not the
        filename of the document it's included in."""
        try:
            filename = relpath(node.source, self.env.srcdir)\
                .rsplit(':docstring of ', maxsplit=1)[0]
        except Exception:
            filename = self.env.doc2path(docname, False)
        return filename

    @staticmethod
    def get_line_number(node: Node) -> int:
        """Get the real line number or admit we don't know."""
        # TODO:  Work out how to store or calculate real (file-relative)
        #       line numbers for doctest blocks in docstrings.
        if ':docstring of ' in path.basename(node.source or ''):
            # The line number is given relative to the stripped docstring,
            # not the file.  This is correct where it is set, in
            # `docutils.nodes.Node.setup_child`, but Sphinx should report
            # relative to the file, not the docstring.
            return None  # type: ignore[return-value]
        if node.line is not None:
            # TODO: find the root cause of this off by one error.
            return node.line - 1
        return None

    def skipped(self, node: Element) -> bool:
        if 'skipif' not in node:
            return False
        else:
            condition = node['skipif']
            context: dict[str, Any] = {}
            if self.config.doctest_global_setup:
                exec(self.config.doctest_global_setup, context)  # NoQA: S102
            should_skip = eval(condition, context)  # NoQA: PGH001
            if self.config.doctest_global_cleanup:
                exec(self.config.doctest_global_cleanup, context)  # NoQA: S102
            return should_skip

    def test_doc(self, docname: str, doctree: Node) -> None:
        groups: dict[str, TestGroup] = {}
        add_to_all_groups = []
        self.setup_runner = SphinxDocTestRunner(verbose=False,
                                                optionflags=self.opt)
        self.test_runner = SphinxDocTestRunner(verbose=False,
                                               optionflags=self.opt)
        self.cleanup_runner = SphinxDocTestRunner(verbose=False,
                                                  optionflags=self.opt)

        self.test_runner._fakeout = self.setup_runner._fakeout  # type: ignore[attr-defined]
        self.cleanup_runner._fakeout = self.setup_runner._fakeout  # type: ignore[attr-defined]

        if self.config.doctest_test_doctest_blocks:
            def condition(node: Node) -> bool:
                return (isinstance(node, (nodes.literal_block, nodes.comment)) and
                        'testnodetype' in node) or \
                    isinstance(node, nodes.doctest_block)
        else:
            def condition(node: Node) -> bool:
                return isinstance(node, (nodes.literal_block, nodes.comment)) \
                    and 'testnodetype' in node
        for node in doctree.findall(condition):  # type: Element
            if self.skipped(node):
                continue

            source = node['test'] if 'test' in node else node.astext()
            filename = self.get_filename_for_node(node, docname)
            line_number = self.get_line_number(node)
            if not source:
                logger.warning(__('no code/output in %s block at %s:%s'),
                               node.get('testnodetype', 'doctest'),
                               filename, line_number)
            code = TestCode(source, type=node.get('testnodetype', 'doctest'),
                            filename=filename, lineno=line_number,
                            options=node.get('options'))
            node_groups = node.get('groups', ['default'])
            if '*' in node_groups:
                add_to_all_groups.append(code)
                continue
            for groupname in node_groups:
                if groupname not in groups:
                    groups[groupname] = TestGroup(groupname)
                groups[groupname].add_code(code)
        for code in add_to_all_groups:
            for group in groups.values():
                group.add_code(code)
        if self.config.doctest_global_setup:
            code = TestCode(self.config.doctest_global_setup,
                            'testsetup', filename='<global_setup>', lineno=0)
            for group in groups.values():
                group.add_code(code, prepend=True)
        if self.config.doctest_global_cleanup:
            code = TestCode(self.config.doctest_global_cleanup,
                            'testcleanup', filename='<global_cleanup>', lineno=0)
            for group in groups.values():
                group.add_code(code)
        if not groups:
            return

        show_successes = self.config.doctest_show_successes
        if show_successes:
            self._out('\n'
                      f'Document: {docname}\n'
                      f'----------{"-" * len(docname)}\n')
        for group in groups.values():
            self.test_group(group)
        # Separately count results from setup code
        res_f, res_t = self.setup_runner.summarize(self._out, verbose=False)
        self.setup_failures += res_f
        self.setup_tries += res_t
        if self.test_runner.tries:
            res_f, res_t = self.test_runner.summarize(
                self._out, verbose=show_successes)
            self.total_failures += res_f
            self.total_tries += res_t
        if self.cleanup_runner.tries:
            res_f, res_t = self.cleanup_runner.summarize(
                self._out, verbose=show_successes)
            self.cleanup_failures += res_f
            self.cleanup_tries += res_t

    def compile(self, code: str, name: str, type: str, flags: Any, dont_inherit: bool) -> Any:
        return compile(code, name, self.type, flags, dont_inherit)

    def test_group(self, group: TestGroup) -> None:
        ns: dict = {}

        def run_setup_cleanup(runner: Any, testcodes: list[TestCode], what: Any) -> bool:
            examples = []
            for testcode in testcodes:
                example = doctest.Example(testcode.code, '', lineno=testcode.lineno)
                examples.append(example)
            if not examples:
                return True
            # simulate a doctest with the code
            sim_doctest = doctest.DocTest(examples, {},
                                          f'{group.name} ({what} code)',
                                          testcodes[0].filename, 0, None)
            sim_doctest.globs = ns
            old_f = runner.failures
            self.type = 'exec'  # the snippet may contain multiple statements
            runner.run(sim_doctest, out=self._warn_out, clear_globs=False)
            if runner.failures > old_f:
                return False
            return True

        # run the setup code
        if not run_setup_cleanup(self.setup_runner, group.setup, 'setup'):
            # if setup failed, don't run the group
            return

        # run the tests
        for code in group.tests:
            if len(code) == 1:
                # ordinary doctests (code/output interleaved)
                try:
                    test = parser.get_doctest(code[0].code, {}, group.name,
                                              code[0].filename, code[0].lineno)
                except Exception:
                    logger.warning(__('ignoring invalid doctest code: %r'), code[0].code,
                                   location=(code[0].filename, code[0].lineno))
                    continue
                if not test.examples:
                    continue
                for example in test.examples:
                    # apply directive's comparison options
                    new_opt = code[0].options.copy()
                    new_opt.update(example.options)
                    example.options = new_opt
                self.type = 'single'  # as for ordinary doctests
            else:
                # testcode and output separate
                output = code[1].code if code[1] else ''
                options = code[1].options if code[1] else {}
                # disable <BLANKLINE> processing as it is not needed
                options[doctest.DONT_ACCEPT_BLANKLINE] = True
                # find out if we're testing an exception
                m = parser._EXCEPTION_RE.match(output)  # type: ignore[attr-defined]
                if m:
                    exc_msg = m.group('msg')
                else:
                    exc_msg = None
                example = doctest.Example(code[0].code, output, exc_msg=exc_msg,
                                          lineno=code[0].lineno, options=options)
                test = doctest.DocTest([example], {}, group.name,
                                       code[0].filename, code[0].lineno, None)
                self.type = 'exec'  # multiple statements again
            # DocTest.__init__ copies the globs namespace, which we don't want
            test.globs = ns
            # also don't clear the globs namespace after running the doctest
            self.test_runner.run(test, out=self._warn_out, clear_globs=False)

        # run the cleanup
        run_setup_cleanup(self.cleanup_runner, group.cleanup, 'cleanup')

def setup(app: Sphinx) -> dict[str, Any]:
    app.add_directive('testsetup', TestsetupDirective)
    app.add_directive('testcleanup', TestcleanupDirective)
    app.add_directive('doctest', DoctestDirective)
    app.add_directive('testcode', TestcodeDirective)
    app.add_directive('testoutput', TestoutputDirective)
    app.add_builder(DocTestBuilder)
    # this config value adds to sys.path
    app.add_config_value('doctest_show_successes', True, False, (bool,))
    app.add_config_value('doctest_path', [], False)
    app.add_config_value('doctest_test_doctest_blocks', 'default', False)
    app.add_config_value('doctest_global_setup', '', False)
    app.add_config_value('doctest_global_cleanup', '', False)
    app.add_config_value(
        'doctest_default_flags',
        doctest.DONT_ACCEPT_TRUE_FOR_1 | doctest.ELLIPSIS | doctest.IGNORE_EXCEPTION_DETAIL,
        False)
    return {'version': sphinx.__display_version__, 'parallel_read_safe': True}