from jinja2 import StrictUndefined
import requests

from flask import Flask, render_template, redirect, request, flash, session
from flask import jsonify
from flask_debugtoolbar import DebugToolbarExtension

# import pdb; pdb.set_trace()

app = Flask(__name__)

app.secret_key = ["SERVER_APP_SECRET_KEY"]
app.jinja_env.undefined = StrictUndefined

###############################################################################

@app.route('/job_list')
def job_list():
    """Job List Page"""

    job_1 = {
             "name": "Job 1",
             "car_address": "683 Sutter St, San Francisco, CA 94109",
             "dealership_address": "999 Van Ness Ave, San Francisco, CA 94109",
             "pick_up_time":"never"
            }
    job_2 = {
             "name": "Job 2",
             "car_address": "15215 N. Kierland Blvd, Scottsdale, AZ 85254",
             "dealership_address": "7014 E. Camelback Road, Suite #1210, Scottsdale, AZ 85251",
             "pick_up_time":"never"
            }
    job_3 = {
             "name": "Job 3",
             "car_address": "45500 Fremont Blvd Fremont, CA 94538",
             "dealership_address": "6701 Amador Plaza Road Dublin, CA 94568",
             "pick_up_time": "never"
            }

    car_info = requests.get("https://192.168.2.45:8000/getinfo", verify=False)
    print(car_info)

    return render_template('job_list.html',
                           job_1=job_1,
                           job_2=job_2,
                           job_3=job_3)

@app.route('/job_details')
def job_details():
    """Job Details Page"""

    return render_template('job_details.html')

###############################################################################

if __name__ == "__main__":
    from server import app
    app.debug = True
    DebugToolbarExtension(app)

    app.run(host="0.0.0.0")


"""Notas URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.contrib.auth import views as auth_views
from Usuarios import views as VistaUsuarios
from ManejoNotas import views as VistaManejoNotas

urlpatterns = [
    # Aquí se pueden ver las tablas de las bases de datos
    path('admin/', admin.site.urls),

    # Muestra el formulario para registrarse o si es post registra un usuario
    path('registrarse/', VistaUsuarios.registrarse, name='registrarse'),
    # Muestra el formulario para iniciar sesión o si es post inicia sesión
    path('iniciar-sesion/', auth_views.LoginView.as_view(
        template_name='usuarios/iniciar-sesion.html',
        next_page='/mis-notas/'), name='iniciar-sesion'),
    # Cierra sesión y lo devuelve a la ventana de iniciar sesión
    path('cerrar-sesion/', auth_views.LogoutView.as_view(next_page='/iniciar-sesion/'), name='cerrar-sesion'),
    # Si se está autenticado muestra la ventana de mis notas, sino lo manda
    # a iniciar sesión
    path('mis-notas/', VistaManejoNotas.mis_notas, name='mis-notas'),
    # Muestra una lista con las notas del usuario
    path('ver-mis-notas/', VistaManejoNotas.ver_mis_notas, name='ver-mis-notas'),
    # Guarda la nota ya sea que la esté editando o creando, si la está editando
    # se debe enviar el id de la nota
    path('guardar-nota/', VistaManejoNotas.guardar_nota, name='guardar-nota'),
    # Borra una nota según el id
    path('borrar-nota/', VistaManejoNotas.borrar_nota, name='borrar-nota'),
    # vista de editar nota
    path('editar-nota/', VistaManejoNotas.editar_nota, name='editar-nota'),
    # vista de crear nota
    path('crear-nota/', VistaManejoNotas.crear_nota, name='crear-nota'),
]


from query_machine.chatgpt import send_to_chatgpt
import query_machine.db as dbs
import json
import yaml
import openai

class QueryMachine:
    def __init__(self, config):
        self.name = config["name"]
        self.type = config["type"]
        self.connection = config["connection"]
        self.tables = config["tables"]
        # database
        db_class = getattr(dbs, self.type)
        self.db_instance = db_class(**self.connection)
        self.get_table_schema()

    def get_table_schema(self):
        if self.tables["table_schema_type"] == "yaml":
            with open(self.tables["table_schema_path"]) as f:
                self.table_schema = json.dumps(
                    yaml.load(f, Loader=yaml.FullLoader))
        elif self.tables["table_schema_type"] == "ddl":
            with open(self.tables["table_schema_path"]) as f:
                self.table_schema = f.read()
        else:
            raise Exception(
                f"no such schema type {self.tables['table_schema_type']}")

    def make_prompt(self, natural_language_query):
        return f"""We defined tables : {self.table_schema},
        make sql in {self.type} db language for next quote
        '{natural_language_query}'
        """

    # if you want to get sql only and no request to database required then use this
    def get_sql(self, natural_language_query):
        prompt = self.make_prompt(natural_language_query)
        try:
            response = send_to_chatgpt(prompt)
        except openai.error.APIConnectionError:
            response = send_to_chatgpt(prompt)

        try:
            ret = response.split("[")[1].split("]")[0]
        except:
            ret = response
        return ret

    def get_data(self, sql):
        return self.db_instance.send_query(sql)

    def ask(self, natural_language_query):
        sql_str = self.get_sql(natural_language_query)
        result = self.get_data(sql_str)
        return sql_str, result

    def info(self):
        return {
            "name": self.name,
            "type": self.type,
            "tables": self.table_schema,
        }


from utils.common import *
from scipy import ndimage
import numpy as np
from torchvision import transforms as T
import torch, os
from torch.utils.data import Dataset, DataLoader
import matplotlib.pyplot as plt

class Verse_DataSet(Dataset):
    def __init__(self, crop_size, resize_scale, dataset_path, mode=None):
        self.crop_size = crop_size
        self.resize_scale = resize_scale
        self.dataset_path = dataset_path
        self.n_labels = 25

        if mode == 'train':
            self.filename_list = load_file_name_list(os.path.join(dataset_path, 'train_name_list.txt'))
        elif mode == 'val':
            self.filename_list = load_file_name_list(os.path.join(dataset_path, 'val_name_list.txt'))
        else:
            raise TypeError('Dataset mode error!!! ')

    def __getitem__(self, index):
        data, target = self.get_train_batch_by_index(crop_size=self.crop_size, index=index,
                                                     resize_scale=self.resize_scale)
        return torch.from_numpy(data), torch.from_numpy(target)

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

    def get_train_batch_by_index(self, crop_size, index, resize_scale=1):
        img, label = self.get_np_data_3d(self.filename_list[index], resize_scale=resize_scale)
        img, label = random_crop_3d(img, label, crop_size)
        return np.expand_dims(img, axis=0), label

    def get_np_data_3d(self, filename, resize_scale=1):
        data_np = sitk_read_raw(self.dataset_path + '/data/' + filename,
                                resize_scale=resize_scale)
        data_np = norm_img(data_np)
        label_np = sitk_read_raw(self.dataset_path + '/label/' + filename.replace('image', 'mask'),
                                 resize_scale=resize_scale)
        return data_np, label_np

def main():
    fixd_path = r'F:\Verse_Data\fixed_path'
    dataset = Verse_DataSet([16, 128, 128], 0.5, fixd_path, mode='train')  # batch size
    data_loader = DataLoader(dataset=dataset, batch_size=2, num_workers=0, shuffle=True)
    for batch_idx, (data, target) in enumerate(data_loader):
        target = to_one_hot_3d(target.long())
        print(data.shape, target.shape)
        plt.subplot(121)
        plt.imshow(data[0, 0, 0])
        plt.subplot(122)
        plt.imshow(target[0, 1, 0])
        plt.show()

if __name__ == '__main__':
    main()


from __future__ import annotations

import copy
import logging
from typing import TYPE_CHECKING

from qcfractal.components.dataset_socket import BaseDatasetSocket
from qcfractal.components.neb.record_db_models import NEBRecordORM
from qcportal.molecules import Molecule
from qcportal.neb import NEBDatasetNewEntry, NEBSpecification
from qcportal.record_models import PriorityEnum
from .dataset_db_models import (
    NEBDatasetORM,
    NEBDatasetSpecificationORM,
    NEBDatasetEntryORM,
    NEBDatasetInitialMoleculeORM,
    NEBDatasetRecordItemORM,
)

if TYPE_CHECKING:
    from sqlalchemy.orm.session import Session
    from qcportal.metadata_models import InsertMetadata
    from qcfractal.db_socket.socket import SQLAlchemySocket
    from typing import Optional, Sequence, Iterable, Tuple

class NEBDatasetSocket(BaseDatasetSocket):
    # Used by the base class
    dataset_orm = NEBDatasetORM
    specification_orm = NEBDatasetSpecificationORM
    entry_orm = NEBDatasetEntryORM
    record_item_orm = NEBDatasetRecordItemORM
    record_orm = NEBRecordORM

    def __init__(self, root_socket: SQLAlchemySocket):
        BaseDatasetSocket.__init__(
            self,
            root_socket,
        )

        self._logger = logging.getLogger(__name__)

    def _add_specification(
        self, session: Session, specification: NEBSpecification
    ) -> Tuple[InsertMetadata, Optional[int]]:
        return self.root_socket.records.neb.add_specification(specification, session=session)

    def _create_entries(self, session: Session, dataset_id: int, new_entries: Sequence[NEBDatasetNewEntry]):
        all_entries = []
        for entry in new_entries:
            meta, mol_ids = self.root_socket.molecules.add_mixed(entry.initial_chain, session=session)

            new_ent_chain = [
                NEBDatasetInitialMoleculeORM(molecule_id=mid, position=pos) for pos, mid in enumerate(mol_ids)
            ]
            new_ent = NEBDatasetEntryORM(
                dataset_id=dataset_id,
                name=entry.name,
                comment=entry.comment,
                additional_keywords=entry.additional_keywords,
                additional_singlepoint_keywords=entry.additional_singlepoint_keywords,
                attributes=entry.attributes,
                initial_chain_assoc=new_ent_chain,
            )
            all_entries.append(new_ent)

        return all_entries

    def _submit(
        self,
        session: Session,
        dataset_id: int,
        entry_orm: Iterable[NEBDatasetEntryORM],
        spec_orm: Iterable[NEBDatasetSpecificationORM],
        existing_records: Iterable[Tuple[str, str]],
        tag: str,
        priority: PriorityEnum,
        owner_user_id: Optional[int],
        owner_group_id: Optional[int],
        find_existing: bool,
    ):
        for spec in spec_orm:
            neb_spec_obj = spec.specification.to_model(NEBSpecification)
            neb_spec_input_dict = neb_spec_obj.dict()

            for entry in entry_orm:
                if (entry.name, spec.name) in existing_records:
                    continue

                new_neb_spec = copy.deepcopy(neb_spec_input_dict)
                new_neb_spec["keywords"].update(entry.additional_keywords)
                new_neb_spec["singlepoint_specification"]["keywords"].update(entry.additional_singlepoint_keywords)
                initial_chain = [x.to_model(Molecule) for x in entry.initial_chain]
                meta, neb_ids = self.root_socket.records.neb.add(
                    initial_chains=[initial_chain],
                    neb_spec=NEBSpecification(**new_neb_spec),
                    tag=tag,
                    priority=priority,
                    owner_user=owner_user_id,
                    owner_group=owner_group_id,
                    find_existing=find_existing,
                    session=session,
                )

                if meta.success:
                    rec = NEBDatasetRecordItemORM(
                        dataset_id=dataset_id,
                        entry_name=entry.name,
                        specification_name=spec.name,
                        record_id=neb_ids[0],
                    )
                    session.add(rec)


def translate(text, conversion_dict, before=None):
    """
    Translate words from a text using a conversion dictionary

    Arguments:
        text: the text to be translated
        conversion_dict: the conversion dictionary
        before: a function to transform the input
        (by default it will to a lowercase)
    """
    # if empty:
    if not text: return text
    # preliminary transformation:
    before = before or str
    t = before(text)
    for key, value in conversion_dict.items():
        t = t.replace(key, value)
    return t


import torchvision.transforms as transforms
import torch

# Hyperparameters

IMG_SIZE = 224
BATCH_SIZE = 32
LR = 3e-04
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
NUM_EPOCHS = 50
NUM_CLASSES = 2
print(f"Selected Device: {DEVICE}")

# Dataset

AUGMENTATION = transforms.Compose(
    [
        transforms.RandomHorizontalFlip(),
        transforms.RandomHorizontalFlip(),
        transforms.RandomRotation(15),
        transforms.Resize(224),
        transforms.ToTensor(),
        transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
    ]
)
TRANSFORM = transforms.Compose(
    [
        transforms.Resize(224),
        transforms.ToTensor(),
        transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]),
    ]
)

ROOT_DATA_DIR = 'Datasets/FNAC_Total_Dataset_'

# Computation Related
PRECISION = '32-true'
ACCELERATOR = 'gpu'
DEVICES = [0]

# Log Related
MODEL_CKPT_DIR = 'ckpt/full/'
MODEL_CKPT_FILENAME = 'model-{epoch:02d}-{val_loss:.2f}'
LOG_DIR = 'log_dir/'
LOG_NAME = 'Resnet18_Pretrained_Full'
VERSION = '0_1(32-true)'


# Импорт библиотеки flask
from flask import Flask

# Импорт модуля sqlite3
import sqlite3

# Импорт json
import json

app = Flask(__name__)

def get_data_sql(sql):
    """Создание подключения к БД с помощью метода sqlite3.connect"""
    with sqlite3.connect("netflix.db") as connection:
        connection.row_factory = sqlite3.Row
        result = connection.execute(sql).fetchall()
    return result

@app.route('/movie/<title>', methods=['GET'])
def get_by_title(title):
    """ Представление страницы с выборкой фильма по названию в формате JSON"""
    result = {}
    for item in get_data_sql(sql=f"""
        SELECT title, country, release_year, listed_in as genre, description
        FROM netflix
        WHERE title LIKE '%{title.title()}%'
        ORDER BY release_year desc
        """):
        result = dict(item)

    return app.response_class(
        json.dumps(result, ensure_ascii=False, indent=4),
        mimetype="application/json",
        status=200
    )

@app.route('/movie/<int:year_one>/to/<int:year_two>', methods=['GET'])
def get_by_period(year_one, year_two):
    """ Представление страницы с выборкой фильма по диапазону лет выпуска в формате JSON"""
    sql = f"""
        select * from netflix
        WHERE release_year between '{year_one}' AND '{year_two}'
        LIMIT 100
        """

    result = []
    for item in get_data_sql(sql):
        result.append(dict(item))

    return app.response_class(
        json.dumps(result[:100], ensure_ascii=False, indent=4),
        mimetype="application/json",
        status=200
    )

@app.route('/rating/<rating>', methods=['GET'])
def get_rating(rating):
    """ Представление страницы поиска по рейтингу."""
    rating_dict = {
        "children": ("G", "G"),
        "family": ('G', 'PG', 'PG-13'),
        "adult": ('R', 'NC-17')
    }

    sql = f"""
        select * from netflix
        WHERE rating in {rating_dict.get(rating, ('PG', 'NC-17'))}
        """

    result = []
    for item in get_data_sql(sql):
        result.append(dict(item))

    return app.response_class(
        json.dumps(result, ensure_ascii=False, indent=4),
        mimetype="application/json",
        status=200
    )

@app.route('/genre/<genre>', methods=['GET'])
def get_genre(genre):
    """ Представление страницы с выборкой 10 самых свежих фильмов в формате json по жанру"""
    sql = f"""
            select title, description from netflix
            WHERE listed_in LIKE '%{str(genre).title()}%'
            ORDER by release_year DESC
            LIMIT 10
        """

    result = []
    for item in get_data_sql(sql):
        result.append(dict(item))

    return app.response_class(
        json.dumps(result, ensure_ascii=False, indent=4),
        mimetype="application/json",
        status=200
    )

def get_names(name1='Rose McIver', name2='Ben Lamb'):
    """ Функция получает в качестве аргумента имена двух актеров,
           и возвращает список тех, кто играет с ними в паре больше 2 раз"""
    global result
    sql = f"""
            select "cast" from netflix
            WHERE "cast" LIKE '%{name1}%'
            AND "cast" LIKE '%{name2}%'
        """
    names_dict = {}
    for item in get_data_sql(sql):
        result = dict(item)

        names = set(result.get('cast').split(", ")) - {name1, name2}

        for name in names:
            names_dict[name.strip()] = names_dict.get(name.strip(), 0) + 1

    print(names_dict)

    for key, value in names_dict.items():
        if value > 2:
            print(key)

    return app.response_class(
        json.dumps(result, ensure_ascii=False, indent=4),
        mimetype="application/json",
        status=200
    )
#print(get_names())

def get_film(types='Movie', year=2020, genre='Horror'):
    """Функция передает тип картины (фильм или сериал),
        год выпуска и ее жанр и получать на выходе список
        названий картин с их описаниями в JSON."""
    sql = f"""
            select * from netflix
            WHERE type = '{types.title()}'
            AND release_year = '{year}'
            AND listed_in LIKE  '%{genre.title()}%'
        """

    result = []
    for item in get_data_sql(sql):
        result.append(dict(item))

    return json.dumps(result, ensure_ascii=False, indent=4)

#print(get_film())

if __name__ == '__main__':
    app.run(port=5000, debug=True)


#!/usr/bin/python3

import requests
import time
from bs4 import BeautifulSoup as bs
from datetime import datetime
import json
import sqlite3
import os

# esta funcion es la encargada de crear la base de datos
def crear_db():
    conex = sqlite3.connect('crypto.db')
    c = conex.cursor()

    c.execute('''CREATE TABLE crypto
         (ID text,
          CRYPTO text,
          COMPRA float,
          VENTA float,
          MONTO float,
          BENEFICIO float,
          FECHA_COMPRA text,
          FECHA_VENTA text)''')
    conex.close()

# esta funcion inserta los datos dentro de la base de datos

def insertar_data(data):
    conex = sqlite3.connect('crypto.db')
    c = conex.cursor()
    # manipula la variable data para tener un formato que acepta
    # el comando execute de la base de datos
    string = 'INSERT INTO crypto VALUES('
    number = len(data) - 1
    j = 0
    for k in data:
        if j < number:
            string += k + ', '
        else:
            string += k
        j += 1
    string += ')'
    print(string)
    ### inserta valores en la base de datos
    c.execute(string)
    ### hace commit a la base de datos para registrar los valores.
    conex.commit()
    conex.close()

# obtiene la fecha actual de la computadora, es importante porque
# sera una de las entradas de la base de datos.
def obtener_fecha():
    # datetime object containing current date and time
    actual = datetime.now()

    # dd/mm/YY H:M:S
    fecha = actual.strftime("%Y/%m/%d %H:%M:%S")
    return fecha

# fabrica una id de localizacion, una especie de numero de factura
# para las transacciones, simplemente usa la fecha de venta.
def obtener_id(fecha_venta):
    id = 'RE'
    for k in fecha_venta:
        if k == '/' or k == ':':
            id += ''
        elif k == ' ':
            id += '_'
        else:
            id += k
    return id

# funcion que hace scraping, trae el valor en dolares de la cryptomoneda
# en el momento que es invocada, recibe el nombre de la cryptomoneda
# si no recibe nada usara bitcoin como moneda por defecto.
def scrape(crypto = 'bitcoin'):
    url = "https://coinmarketcap.com/currencies/" + crypto + '/'
    respuesta = requests.get(url)
    datos = bs(respuesta.content,'html.parser')
    # busca la clase priceValue en el codigo html de la pagina
    # para poder extraer el precio correcto.
    contenido = datos.find('div',{'class':'priceValue'})
    precio = contenido.select_one('span')
    precio = precio.text

    # quita los adornos de la moneda para poder convertirla en un
    # valor float para su uso posterior.
    value = ''
    for k in precio:
        cond = (k == "$" or k == ",")
        if not cond:
            value += k

    value = float(value)
    return value

# se encarga de borrar una entrada del json que ya este completa, en otras
# palabras que ya haya realizado su operacion financiera, es necesario
# borrar la entrada para que la base de datos no tome mas de una vez el
# valor de la transaccion.
def borrar_entrada_json():
    with open('file.json', 'r') as file:
        json_data = json.load(file)
        num = 0
        for k in json_data:
            if k["id"] != "0":
                num += 1

        for k in range(num):
            for i in range(len(json_data)):
                if json_data[i]["id"] != "0":
                    json_data.pop(i)
                    open("file.json", "w").write(json.dumps(json_data,
                                                            indent = 4))
                    break

# revisa el archivo json para poder hacer las operaciones que en el se indican
def json_update(archivo_json):
    with open(archivo_json, 'r') as file:
        json_data = json.load(file)
        size = len(json_data)
        num = 0
        for item in json_data:
            if item['id'] != '0':
                # crear data para la base de datos
                data = []
                for k in item:
                    if type(item[k]) == str:
                        val = "'" + item[k] + "'"
                    else:
                        val = str(item[k])
                    data.append(val)
                # enviar data a la base de datos
                insertar_data(data)
    #borrar data de json
    borrar_entrada_json()

    with open(archivo_json, 'r') as file:
        json_data = json.load(file)
        for item in json_data:
            if item['fecha_venta'] == '0':
                #obtener el precio
                precio = scrape(item['crypto'])
                print("precio crypto: ", precio)
                if item['fecha_compra'] == '0':
                    if precio <= float(item['compra']):
                        item['compra'] = precio
                        item['fecha_compra'] = obtener_fecha()
                else:
                    if precio >= float(item['venta']):
                        item['venta'] = precio
                        item['fecha_venta'] = obtener_fecha()
                        item['beneficio'] = item['monto'] * (item['venta'] / item['compra'] - 1)
                        item['id'] = obtener_id(item['fecha_venta'])
            else:
                num += 1

        if num == size:
            print("TODAS LAS OPERACIONES SE COMPLETARON")

    with open(archivo_json, 'w') as file:
        json.dump(json_data, file, indent=4)

# verifica si existe la base de datos
file_name = r"crypto.db"
bool = os.path.exists(file_name)

# si no existe la base de datos la crea
if not bool:
    crear_db()

# loop de peticiones de precios
while 1:
    json_update('file.json')
    time.sleep(60)


from torch.utils.data import Dataset
from PIL import Image
import os
from pathlib import Path
from utilities.utils import standardize
from torchvision import transforms
import os.path

class Isic:
    def __init__(self, dataroot, dim=64):
        transform = transforms.Compose([transforms.Resize([dim, dim]), transforms.ToTensor(), standardize])
        path = Path(dataroot) / "data" / "ISIC"
        self.train_dataset = ISICDataset(path, transform=transform, test=False, malignant=False)
        self.test_dataset = ISICDataset(path, transform=transform, test=True, malignant=False)
        self.out_dataset = ISICDataset(path, transform=transform, malignant=True)
        self.imDim = (3, dim, dim)
        self.num_classes = 2

class ISICDataset(Dataset):
    def __init__(self, img_path, transform, test=False, malignant=False):
        if malignant:
            self.img_path = os.path.join(img_path, "malignant")
            self.ims = os.listdir(self.img_path)
            self.target = 1
        else:
            if test:
                self.img_path = os.path.join(img_path, "benign_test")
            else:
                self.img_path = os.path.join(img_path, "benign")
            self.ims = os.listdir(self.img_path)
            self.target = 0
        self.transform = transform

    def __getitem__(self, index):
        img_name = os.path.join(self.img_path, self.ims[index])
        img = Image.open(img_name)
        img = self.transform(img)
        return img, self.target

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


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.metrics import r2_score
import pickle
warnings.filterwarnings("ignore")

dataframe = pd.read_csv("C:\\Users\\paulo\\PycharmProjects\\feature-selection\\hour.csv")

def showHist(dataframe):
    plt.rcParams["figure.figsize"] = 15, 18
    dataframe.hist()

showHist(dataframe)

def showPairPlot(dataframe):
    sns.pairplot(dataframe)

showPairPlot(dataframe)

arra = dataframe["dteday"].str.split("-")[0]

def extractColumnsYearMonthDateFromDteday(dataframe):
    dataframe['Year'] = dataframe['dteday'].str.split('-').str[0]
    dataframe['Year'] = dataframe['Year'].astype(int)
    dataframe['Month'] = dataframe['dteday'].str.split('-').str[1]
    dataframe['Month'] = dataframe['Month'].astype(int)
    dataframe['Date'] = dataframe['dteday'].str.split('-').str[2]
    dataframe['Date'] = dataframe['Date'].astype(int)

    dataframe = dataframe.drop(['dteday'], axis = 1)
    dataframe = dataframe.drop(['yr'], axis = 1)
    dataframe = dataframe.drop(['mnth'], axis = 1)
    return(dataframe)

print(dataframe.isna().sum())

dataframe = extractColumnsYearMonthDateFromDteday(dataframe)
DISTANCE_MID_TO_TAIL_IN_BOX_PLOT = 1.5

def outlier(x):
    high=0
    q1 = x.quantile(.25)
    q3 = x.quantile(.75)
    iqr = q3 - q1
    #***************************************************
    #          _________________
    #          |       |        | DISTANCE BETWEEN MID TO LOWER TAIL IS 1.5
    #----------|      mid       |------------
    #          |       |        |
    #          |________________|
    #
    #***************************************************
    low = q1 - DISTANCE_MID_TO_TAIL_IN_BOX_PLOT * iqr
    high += q3 + DISTANCE_MID_TO_TAIL_IN_BOX_PLOT * iqr
    outlier = (x.loc[(x < low) | (x > high)])
    return(outlier)

cont = outlier(dataframe['cnt']).count()

print(cont)

q1 = dataframe['cnt'].quantile(.25)
q3 = dataframe['cnt'].quantile(.75)
iqr = q3-q1
df_new = dataframe[~((dataframe['cnt'] < (q1 - 1.5 * iqr)) | (dataframe['cnt'] > (q3 + 1.5 * iqr)))]

x = df_new.drop(['cnt'], axis = 1)
y = df_new['cnt']
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.30, random_state=40)

lr = LinearRegression(copy_X = True, fit_intercept = True, n_jobs = 0, normalize = False)
LR = lr.fit(x_train, y_train)
LR_Pred = lr.predict(x_test)

print('ROOT EAN ERROR SQUARE:', np.sqrt(metrics.mean_squared_error(y_test, LR_Pred)))

print('R SQUARE:', r2_score(y_test, LR_Pred))

pickle.dump(lr, open('LinearRegression.pkl', 'wb'))


# Find the Maximum Divisibility Score
# You are given two 0-indexed integer arrays nums and divisors. The divisibility score of divisors[i] is the number
# of indices j such that nums[j] is divisible by divisors[i]. Return the integer divisors[i] with the maximum
# divisibility score. If there is more than one integer with the maximum score, return the minimum of them.
def maxDivScore(nums, divisors):
    maxScore, maxDivisor = -1, -1
    for divisor in divisors:
        score = 0
        for num in nums:
            if num % divisor == 0:
                score += 1
        if score > maxScore or (score == maxScore and divisor < maxDivisor):
            maxScore = score
            maxDivisor = divisor
    return maxDivisor

print(maxDivScore(nums=[20, 14, 21, 10], divisors=[5, 7, 5]))


import pygame
from math import *

class Planet:
    def __init__(self, x_expr, y_expr, angle_expr, radius, mass, filename):
        self.x_expr = x_expr
        self.y_expr = y_expr
        self.angle_expr = angle_expr
        self.radius = radius
        self.mass = mass
        self.sprite = pygame.image.load(filename).convert_alpha()

        self.update(0)

    def update(self, time):
        self.pos = (eval(self.x_expr.replace("t", str(time))),
                    eval(self.y_expr.replace("t", str(time))))
        self.angle = eval(self.angle_expr.replace("t", str(time)))

    def draw(self, canvas):
        rotated = pygame.transform.rotozoom(self.sprite, self.angle, 1)
        width, height = rotated.get_size()
        x = self.pos[0] - width // 2
        y = self.pos[1] - height // 2
        canvas.blit(rotated, [x, y])

        # pygame.draw.rect(canvas,[0,255,0],
        #                  [self.pos[0]-self.radius,self.pos[1]-self.radius,
        #                   2*self.radius, 2*self.radius],1)


# Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

# Note: You can only move either down or right at any point in time.

# Example 1:

# [[1,3,1],
#  [1,5,1],
#  [4,2,1]]

# Given the above grid map, return 7. Because the path 1→3→1→1→1 minimizes the sum.

class Solution:
    def minPathSum(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        for x in range(1, len(grid)):
            grid[x][0] += grid[x - 1][0]
        for y in range(1, len(grid[0])):
            grid[0][y] += grid[0][y - 1]
        for x in range(1, len(grid)):
            for y in range(1, len(grid[0])):
                grid[x][y] += min(grid[x - 1][y], grid[x][y -1])
        return grid[-1][-1]


#!/usr/bin/env python3
# encoding:utf-8
# 2020/01/25 aiden
import time
import numpy as np
from math import sqrt
import inverse_kinematics
import json

#机械臂根据逆运动学算出的角度进行移动
ik = inverse_kinematics.IK()

class ArmIK:
    servo3Range = (0, 1000, 0, 240.0) #脉宽， 角度
    servo4Range = (0, 1000, 0, 240.0)
    servo5Range = (0, 1000, 0, 240.0)
    servo6Range = (0, 1000, 0, 240.0)

    def __init__(self):
        self.setServoRange()

    def setServoRange(self, servo3_Range=servo3Range, servo4_Range=servo4Range, servo5_Range=servo5Range, servo6_Range=servo6Range):
        # 适配不同的舵机
        self.servo3Range = servo3_Range
        self.servo4Range = servo4_Range
        self.servo5Range = servo5_Range
        self.servo6Range = servo6_Range
        self.servo3Param = (self.servo3Range[1] - self.servo3Range[0]) / (self.servo3Range[3] - self.servo3Range[2])
        self.servo4Param = (self.servo4Range[1] - self.servo4Range[0]) / (self.servo4Range[3] - self.servo4Range[2])
        self.servo5Param = (self.servo5Range[1] - self.servo5Range[0]) / (self.servo5Range[3] - self.servo5Range[2])
        self.servo6Param = (self.servo6Range[1] - self.servo6Range[0]) / (self.servo6Range[3] - self.servo6Range[2])

    def transformAngelAdaptArm(self, theta3, theta4, theta5, theta6):
        #将逆运动学算出的角度转换为舵机对应的脉宽值
        servo3 = int(round(theta3 * self.servo3Param + (self.servo3Range[1] + self.servo3Range[0])/2))

        servo4 = int(round(-theta4 * self.servo4Param + (self.servo4Range[1] + self.servo4Range[0])/2))

        servo5 = int(round((self.servo5Range[1] + self.servo5Range[0])/2 + theta5 * self.servo5Param))

        servo6 = int(round(((self.servo6Range[3] - self.servo6Range[2])/2 + theta6)) * self.servo6Param)

        return {"servo3": servo3, "servo4": servo4, "servo5": servo5, "servo6": servo6}

    def setPitchRanges(self, coordinate_data, alpha, alpha1, alpha2, d = 0.01):
        #给定坐标coordinate_data和俯仰角alpha,以及俯仰角范围的范围alpha1, alpha2，自动寻找最接近给定俯仰角的解
        #如果无解返回False,否则返回舵机角度、俯仰角
        #坐标单位m， 以元组形式传入，例如(0, 0.5, 0.1)
        #alpha为给定俯仰角, 单位度
        #alpha1和alpha2为俯仰角的取值范围

        x, y, z = coordinate_data
        a_range = abs(int(abs(alpha1 - alpha2)/d)) + 1
        for i in range(a_range):
            if i % 2:
                alpha_ = alpha + (i + 1)/2*d
            else:
                alpha_ = alpha - i/2*d
                if alpha_ < alpha1:
                    alpha_ = alpha2 - i/2*d
##            print(alpha_)
            result = ik.getRotationAngle((x, y, z), alpha_)
            if result:
                theta3, theta4, theta5, theta6 = result['theta3'], result['theta4'], result['theta5'], result['theta6']
                servos = self.transformAngelAdaptArm(theta3, theta4, theta5, theta6)
                return result, servos, alpha_

        return False

import rospy
import armpi_fpv.bus_servo_control as bus_servo_control
from hiwonder_servo_msgs.msg import MultiRawIdPosDur

    # 初始化节点
rospy.init_node('ik_test', log_level=rospy.DEBUG)

    # 舵机发布
joints_pub = rospy.Publisher('/servo_controllers/port_id_1/multi_id_pos_dur', MultiRawIdPosDur, queue_size=1)
rospy.sleep(0.2)

def armlist_generate():
    numlist = ['a','b','c','d','e','f','g','h']
    armlist = []
    for i in numlist:
        for j in range(8):
            k = i+str(j+1)
            armlist.append(k)
    return armlist # a1--h8

close = 165
loose = 90

def common_draw(x,y,z,grab):
    AK = ArmIK()
    target = AK.setPitchRanges((x/1000,y/1000,z/1000), -180, -180, 0)
    if target:
        servo_data = target[1]
        bus_servo_control.set_servos(joints_pub, 500, ((1, grab), (2, 500), (3, servo_data['servo3']), (4, servo_data['servo4']), (5, servo_data['servo5']), (6, servo_data['servo6'])))
        rospy.sleep(1)
def test(x,y,z):
    common_draw(x,y,z,loose)

def move1(x, y, z):
    common_draw(x,y,z+30,loose)  # qi zi shang fang
    common_draw(x,y,z,loose)        # qi zi wei zhi
    common_draw(x,y,z,close)            # zhua zi
    common_draw(x,y,z+30,close)      # tai zi
def move2(x, y, z):
    common_draw(x,y,z+30,close)      # yi dong zi
    common_draw(x,y,z,close)            # fang xia zi
    common_draw(x,y,z,loose)        # song kai zhi
    common_draw(x,y,z+30,loose)  # shang yi

if __name__ == "__main__":
    armlist = armlist_generate()
    with open('armdict.txt','r') as f:
        armditext = f.readline()
        armdict = eval(armditext)
    while True:
        function_chosen = input('输入1测试all, input 2 test like a1a2, 输入5测试一列, 退出按q:')
        if function_chosen == '1':
            for i in armlist:
                v=armdict[i]
                x,y,z = v[0],v[1],v[2]
                print('{0:s}坐标：'.format(i),x,y,z)
                test(x,y,z)
                rospy.sleep(1)
        elif function_chosen == '5':
            col_test = input('请输入列，比如a 则自动测试a1-a8：')
            num = 0
            for num in range(8):
                i = col_test+str(num+1)
                v = armdict[i]
                x,y,z = v[0],v[1],v[2]
                print('{0:s}坐标：'.format(i),x,y,z)
                test(x,y,z)
                rospy.sleep(1)
        elif function_chosen == '2':
            while True:
                str = input('input like a1,a2   input q quit   input y change： ')
                if str == 'q':
                    break
                elif str == 'y':
                    cord_chosen = input('输入坐标代号，比如a1:')
                    u = armdict[cord_chosen]
                    x,y,z = u[0],u[1],u[2]
                    while True:
                        print('现在的坐标:',x,y,z)
                        try:
                            x,y,z = input('请输入x,y,z空格隔开:').split()
                            x = eval(x)
                            y = eval(y)
                            z = eval(z)
                            armdict[cord_chosen]=(x,y,z)
                            print('更新后的值：',armdict[cord_chosen])
                            test(x,y,z)
                            rospy.sleep(1)
                        except:
                            print('输错了')
                            break
                else:
                    # v1
                    str1 = str[0:2]
                    v1 = armdict[str1]
                    x1,y1,z1 = v1[0],v1[1],v1[2]
                    print('{0:s}坐标：'.format(str1),x1,y1,z1)
                    move1(x1,y1,z1)
                    rospy.sleep(1)
                    # V2
                    str2 = str[2:4]
                    v2= armdict[str2]
                    x2,y2,z2 = v2[0],v2[1],v2[2]
                    print('{0:s}坐标：'.format(str2),x2,y2,z2)
                    move2(x2,y2,z2)
                    rospy.sleep(1)
        elif function_chosen == 'q':
            break
        else:
            print('输入错误')


import requests

from bs4 import BeautifulSoup

import csv

url = 'https://www1.nseindia.com/live_market/dynaContent/live_watch/option_chain/optionKeys.jsp'

headers = {'User-Agent' : 'Mozilla-5.0'}

resp = requests.get(url,headers = headers)

# print (resp.status_code)

soup = BeautifulSoup(resp.text,'html.parser')

def nifty_value(soup):
	return (soup.find('table').span.text)

table_find = soup.find('table', id = 'octable')

table_body = table_find.find('tr')

# print (table_find.prettify())

# rws = table_body.find_all('th')
# print (rws[0].text)

# print (rws)

final_list = []

# print (table_body)

for rowes in table_find.find_all('tr'):
	inter_list = []

	# print (rowes)

	for colum in rowes.find_all('th'):
		# print (colum)
		inter_list.append(colum.text)

	if len(inter_list) != 0:

		inter_list = [i.upper() for i in inter_list]

		final_list.append(inter_list)

# print (final_list)

# def extract_data(ref_text_row,ref_text_col,list_name):
# 	table_body = table_find.find(ref_text_row)

# print (final_list)

final_list1 = []

for rowes in table_find.find_all('tr'):
	inter_list = []

	# print (rowes)

	for colum in rowes.find_all('td'):
		# print (colum)
		inter_list.append(colum.text)

	if len(inter_list) != 0:

		inter_list = [i.strip() for i in inter_list]

		final_list1.append(inter_list)

# print (final_list1)

input_csv_data = final_list[1:] + final_list1

# print (input_csv_data)

with open('option_data.csv','w') as file1:

	file1_writer = csv.writer(file1)

	for i in input_csv_data:
		# print (i)
		file1_writer.writerow(i)

print (nifty_value(soup))


"""empty message

Revision ID: b0403976f34e
Revises: 90dfed859bc1
Create Date: 2018-04-10 06:19:07.312447

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'b0403976f34e'
down_revision = '90dfed859bc1'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('tokenBlacklists',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('user_identity', sa.String(length=50), nullable=False),
    sa.Column('token', sa.String(length=400), nullable=False),
    sa.Column('revoked', sa.Boolean(), nullable=False),
    sa.Column('blacklisted_date', sa.DateTime(), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('tokenBlacklists')
    # ### end Alembic commands ###


# """
# "r" - Open file for reading purpose - default
# "w" - Open file for Writing Purpose
# "x" - Create file if does not exist
# "a" - Append mode
# "t" - Text mode
# "b" - Binary Mode
# "+" - read and write
# """
# # Reading Function with file.
#
# f = open("Subhan.txt.txt")
# # content = f.read()
# # print(content)
# # print(f.readline())
# # print(f.readline())
# print(f.readlines())
# # for line in f:
# #     print(line)
#
# f.close()
#
# # Writing a file
# file = open("Subhan2.txt", "a")
# file.write("So it is Subhan 2 file \n")
#
# file2 = open("Subhan3.txt", "a")
# file2.write("So it is Subhan 3 file.\n")
#
# file3 = open("Subhan3.txt", "r+")
# print(file3.tell())
# file3.write("It is another line.")
# print(file3.read())
#
#
# """
# Seek function will reset the pointer to our given position.
# Tell function will tell us the recent position of our file pointer.
# """
#
# f = open("Subhan.txt.txt")
#
# print(f.seek(2))
# print(f.tell())
# print(f.read())
#
# """
# Opening file with "with" block
# """
with open("Subhan4.txt", "r+") as filein:
    filein.write("Hello! This file name is Subhan 4.")

with open("Subhan4.txt") as filein:
       contents =  filein.read()
       print(contents)

# file.close()
# file2.close()
# file3.close()
#
#
#
#


import sys
from lxml import etree

xmlfile = sys.argv[1]
master_tp_file = sys.argv[2]
take_tp_files = sys.argv[3:]

def main():
    with open(xmlfile) as fh:
        doc = etree.parse(fh)

    #clip, asset-clip, ref-clip are all ok
    clips = doc.xpath("library/event/project/sequence/spine/clip|library/event/project/sequence/spine/asset-clip|library/event/project/sequence/spine/ref-clip")

    log(f"Number of clips in xml : {len(clips)}")
    log(f"Number of Take sp files: {len(take_tp_files)}")
    assert len(clips) == len(take_tp_files)

    master_tp = readTimePointFile(master_tp_file)

    nr = 0
    while len(clips) > nr:
        take_tp = readTimePointFile(take_tp_files[nr])
        assert len(master_tp) == len(take_tp)
        addTimepointsToClip(clips[nr], master_tp, take_tp)
        nr += 1

    etree.indent(doc, "    ")
    print(str(etree.tostring(doc, xml_declaration=True), "utf-8"))

def log(msg):
    sys.stderr.write(f"{msg}\n")

def readTimePointFile(filename):
    with open(filename) as fh:
        lines = fh.readlines()
    tps = []
    for line in lines:
        tps.append(float(line.split("\t")[0]))
    return tps

def addTimepointsToClip(clip, master_tp, take_tp):
    log(clip)
    #log(take_tp)

    timeMaps = clip.xpath(".//timeMap")
    for timeMap in timeMaps:
        log(timeMap)
        addTimepointsToTimeMap(timeMap, master_tp, take_tp)

def addTimepointsToTimeMap(timeMap, master_tp, take_tp):
    #first_timept = timeMap[0]
    second_timept = timeMap[1]
    zeropoint_string = second_timept.get("time")

    #save last timepoint in timeMap, to append finally
    last_timept = timeMap[-1]
    timeMap.remove(last_timept)

    #remove second last timepoint in timeMap, it will be replaced by last in tp
    #print(dir(timeMap))
    second_last_timept = timeMap[-2]
    timeMap.remove(second_last_timept)

    (zeropoint, fps) = zeropoint_string[:-1].split("/")
    #log(fps)
    factor = 50/int(fps)
    #log(factor)
    zeropoint_50f = int(int(zeropoint)*factor)
    log(f"zeropoint_50f: {zeropoint_50f}")

    nr = 0
    while len(master_tp) > nr:
        master_timepoint = master_tp[nr]
        take_timepoint = take_tp[nr]

        master_timepoint_50f = f"{int(master_timepoint*50+zeropoint_50f)}/50s"
        take_timepoint_50f = f"{int(take_timepoint*50+zeropoint_50f)}/50s"

        new_timept = etree.Element("timept")
        new_timept.set("interp", "linear")
        #new_timept.set("value", master_timepoint_50f)
        #new_timept.set("time", take_timepoint_50f)
        new_timept.set("time", master_timepoint_50f)
        new_timept.set("value", take_timepoint_50f)

        #log(etree.tostring(new_timept))
        timeMap.append(new_timept)

        nr += 1

    timeMap.append(last_timept)

main()


import copy
import random
from typing import Tuple, Union

import numpy as np
import gym
from gym import spaces
from tetris_environment.tetris_engine import UnrenderedTetrisGame
from tetris_environment.rendering_tetris_engine import RenderingTetrisGame

SCREEN_WIDTH, SCREEN_HEIGHT = 200, 400

class TetrisEnv(gym.Env):
    metadata = {'render.modes': ['human', 'rgb_array']}

    def __init__(self, type: str, render: bool = False, low: int = -3, high: int = 3):

        if type not in ("fourer", 'regular', 'extended fourer'):
            raise RuntimeError("Invalid Tetris type")

        # open up a game state to communicate with emulator
        if not render:
            self.game_state = UnrenderedTetrisGame(type)
            self.game_type = UnrenderedTetrisGame
        else:
            self.game_state = RenderingTetrisGame(type)
            self.game_type = RenderingTetrisGame
            # raise RuntimeError()

        self._action_set = self.game_state.get_action_set()
        self.action_space = spaces.Discrete(len(self._action_set))
        self.observation_space = spaces.Box(low=low, high=high, shape=(9,), dtype=int)
        self.viewer = None
        self.type = type
        self.rendering = render

    def step(self, a: int) -> Tuple[tuple, float, bool, dict]:
        """
        Takes one step in the environment using the defined action a
        :param a: the action to take
        :return: a 4-tuple consisting of
                • the current encoded state (see get_encoded_state())
                • the reward received
                • whether or not the end of an epîsode has been reached
                • several observations with their own labels
                    - label "score" (int): the total score received during this step and this step alone
                    - label "new_piece (bool): is True when a new falling piece was added to the board
                    - label "lines_cleared" (int): the total amount of lines cleared during this step
        """
        self._action_set = np.zeros([len(self._action_set)])
        self._action_set[a] = 1
        _, reward, terminal, observations = self.game_state.frame_step(self._action_set)
        state = self.get_encoded_state()
        return state, reward, terminal, observations

    @property
    def n_actions(self):
        return len(self._action_set)

    # return: (states, observations)
    def reset(self):
        do_nothing = np.zeros(len(self._action_set))
        do_nothing[0] = 1
        # self.observation_space = spaces.Box(low=0, high=255, shape=(SCREEN_WIDTH, SCREEN_HEIGHT, 3))
        self.observation_space = spaces.Box(low=-3, high=3, shape=(9,), dtype=int)
        self.game_state.frame_step(do_nothing)
        state = self.get_encoded_state()
        return state

    def render(self, mode='human', close=False):

        if not self.rendering:
            return

        if close:
            if self.viewer is not None:
                self.viewer.close()
                self.viewer = None
            return
        img = self.game_state.get_image()
        if mode == 'rgb_array':
            return img
        elif mode == 'human':
            from gym.envs.classic_control import rendering
            if self.viewer is None:
                self.viewer = rendering.SimpleImageViewer()
            self.viewer.imshow(img)

    def get_encoded_state(self, board=None) -> tuple:
        """
        Encodes the state space from the 10-by-20 (for a normal Tetris game) board to an integer array of size 9 by
        only noting the height differences between adjacent columns. If this difference is greater than 3 or
        smaller than -3, the value is equal to 3 or -3 respectively.
        :param board: the Tetris board to be encoded. If none is provided, the board of the class is used
        :return: a tuple of size board_width - 1 containing h_(i+1)-h_i in the i'th spot.
        """
        if board is None:
            board = self.game_state

        board_width = board.board_width
        state = [0 for _ in range(board_width - 1)]  # list of zeroes

        low = -3
        high = 3

        for i in range(board_width - 1):
            height_diff = board.get_column_height(i + 1) - board.get_column_height(i)
            if height_diff < low:
                height_diff = low
            elif height_diff > high:
                height_diff = high
            state[i] = height_diff

        return tuple(state)  # finally, convert to hashable type, i.e. tuple

    def all_possible_placements(self):
        """
        Move the piece from all the way left to all the way right, and rotate it in all possible ways.
        Then return the state achieved by dropping the piece down and the first (of several) actions to
        take to achieve this state
        :return:
        """
        all_possible_positions = []

        piece = self.game_state.fallingPiece
        if piece is not None:
            for width in range(-4, self.game_state.board_width + 4):
                shape = piece['shape']
                for rotation in range(len(self.game_state.pieces.get(shape))):
                    new_piece = copy.deepcopy(piece)
                    new_piece['x'] = width
                    new_piece['rotation'] = rotation
                    if self.game_state.is_valid_position(piece=new_piece):
                        all_possible_positions.insert(random.randint(0, len(all_possible_positions)), new_piece)

            all_possible_placements = []
            for piece in all_possible_positions:
                board = copy.deepcopy(self.game_state.board)
                new_game = self.game_type(board=board, type=self.type)
                new_game.fallingPiece = piece
                actions = self._get_actions(old_board=self.game_state, new_board=new_game)
                move_down = np.zeros(6)
                move_down[4] = 1
                new_game.frame_step(move_down)
                state = self.get_encoded_state(new_game)
                all_possible_placements.append((state, actions))

            return all_possible_placements
        else:
            return []

    @staticmethod
    def _get_actions(old_board, new_board):
        """
        Finds the actions required to position the falling piece on :param old_board in the same way as
        the falling piece on :param new_board
        :return: a list of all actions required (first rotations, then lateral movement). The last action added
                is the move_down action
        :raise AssertionError if the shapes of the falling pieces are different
        """
        # Define actions
        move_left = 1
        move_right = 3
        move_down = 4
        rotate_clockwise = 5

        actions = []

        curr_piece = old_board.fallingPiece
        target_piece = new_board.fallingPiece

        assert curr_piece['shape'] == target_piece['shape']

        if curr_piece['rotation'] != target_piece['rotation']:
            for _ in range(abs(curr_piece["rotation"] - target_piece["rotation"])):
                actions.append(rotate_clockwise)
        if curr_piece['x'] < target_piece['x']:
            for _ in range(target_piece['x'] - curr_piece['x']):
                actions.append(move_right)
        if curr_piece['x'] > target_piece['x']:
            for _ in range(curr_piece['x'] - target_piece['x']):
                actions.append(move_left)
        if len(actions) == 0:
            # piece placements are identical, so no actions are added.
            pass

        actions.append(move_down)

        return actions

    def get_falling_piece(self) -> Union[tuple, None]:
        """
        :return: a tuple of the form (piece.shape, piece.x, piece.y, piece.rotation)
        """
        return self.game_state.get_falling_piece()

    @property
    def no_move(self):
        return 0


# content of test_class.py
class TestClass:
    def test_one(self):
        x = "this"
        assert 'h' in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

class MyClass1:
    variable = "blah"

    def function(self):
        print("This is a message inside the class.")

class MyClass:
    variable = "blah"

    def function(self):
        print("This is a message inside the class.")

myobjectx = MyClass()
myobjecty = MyClass()

myobjecty.variable = "yackity"

# Then pring out both values
print(myobjectx.variable)
print(myobjecty.variable)

# define the Vehicle class
class Vehicle:
    name = ""
    kind = "car"
    color = ""
    value = 100.00
    def description(self):
        desc_str = "%s is a %s %s worth $%.2f." % (self.name, self.color, self.kind, self.value)
        return desc_str
# your code goes here
car1 = Vehicle()
car2 = Vehicle()

car1.color = 'red'
car1.name = 'Fer'
car1.value = 60000.00
car1.kind = 'convertible'

car2.color = 'blue'
car2.name = 'Jump'
car2.value = 10000.00
car2.kind = 'van'

# test code
print(car1.description())
print(car2.description())


import requests
from bs4 import BeautifulSoup
import pandas as pd
import re

URL = "https://en.wikipedia.org/wiki/List_of_jet_airliners"

response = requests.get(URL)  # page

soup = BeautifulSoup(response.text, 'html.parser')  # scraped

tables = soup.find_all('table', {"class":"wikitable sortable"})  #all tables from page
total = [] # array to contain plane objects

for table in tables:

    rows = table.find_all('tr')  #each row in plane

    columns = [v.text.replace('\n', "") for v in rows[0].find_all('th')]  #finds headers, puts them into columns list
    df = pd.DataFrame(columns=columns)  #empty column data structure

    for i in range(1, len(rows)):
        tds = rows[i].find_all('td')  #each table cell

        values = [re.sub("[\(\[].*?[\)\]]", "", td.text.replace('\n', "").replace(',', "")) for td in tds]  #adds table cell to a values array

        df = df.append(pd.Series(values, index=columns),ignore_index=True)  #puts values array into df datastructure under respective column

    total.append(df)  #add df to total array
final = pd.concat(total, ignore_index=True)  #adds all of total to a final data structure

#column formating - combining columns with *slightly* different titles
final["Origin"] = final["Origin"].astype(str).replace('nan', "") + final["Country"].astype(str).replace('nan', "")
final["Engines"] = final["Engines[a]"].astype(str).replace('nan', "") + final["Engines[c]"].astype(str).replace('nan', "") + final["Engines[f]"].astype(str).replace('nan', "")
final["In Service"] = final["In Service [1][b]"].astype(str).replace('nan', "") + final["In Service (2020)[1][d]"].astype(str).replace('nan', "")
final = final.drop(["Origin", "Country", "Engines[a]", "Engines[c]", "Engines[f]","In Service [1][b]","In Service (2020)[1][d]"], axis=1)

#row formatting - deleting cancelled or only planned planes
final = final[~final["First Flight"].str.contains("planned")]
final = final[~final["First Flight"].str.contains("cancelled")]
final = final[~final["Airline service entry"].str.contains("planned")]
final = final[~final["Airline service entry"].str.contains("cancelled")]

#export to csv file
final.to_csv(r'planes.csv')


#!/usr/bin/env python3

import argparse
import pathlib

import pandas as pd

import gradescope_mean

parser = argparse.ArgumentParser(
    description='grade synthesis from gradescope (see doc at: https://github.com/matthigger/gradescope_mean/blob/main/readme.md)')
parser.add_argument('f_scope', type=str,
                    help='gradescope output csv (Assignments > Download Grades > CSV)')
parser.add_argument('--config', dest='f_config', action='store',
                    default=None,
                    help='yaml configuration (see doc at: https://github.com/matthigger/gradescope_mean/blob/main/doc/config.md)')
parser.add_argument('--plot', dest='plot_flag', action='store',
                    default=True,
                    help='html of histogram plot per assignment category ('
                         'none generated if no html output file specified)')
parser.add_argument('--late_csv', dest='f_late_csv', action='store',
                    default=None,
                    help='csv of late days applied per assignment')
parser.add_argument('--per_student', dest='per_stud', action='store_true',
                    help='outputs csv per student')

def main(args=None):
    if args is None:
        args = parser.parse_args()

    # load config
    folder = pathlib.Path(args.f_scope).resolve().parent
    if args.f_config is None:
        config = gradescope_mean.Config.cli_copy_config(folder)
    else:
        config = gradescope_mean.Config.from_file(args.f_config)

    # process
    gradebook, df_grade_full = config(f_scope=args.f_scope)

    # output
    df_grade_full.to_csv(str(folder / 'grade_full.csv'))

    # outputs csv per student
    if args.per_stud:
        _folder = folder / 'per_student'
        _folder.mkdir(exist_ok=True)
        for idx, row in df_grade_full.iterrows():
            _df = pd.DataFrame(row)
            last = row['last name']
            first = row['first name']
            _df.to_csv(_folder / f'{last}_{first}.csv')

    # print late days to csv
    if args.f_late_csv is not None:
        f_late = folder / args.f_late_csv
        gradebook.df_lateday.to_csv(f_late.with_suffix('.csv'))

    # plot
    if args.plot_flag:
        fig = gradescope_mean.plot_hist(df_grade_full=df_grade_full,
                                        cat_weight_dict=config.cat_weight_dict)
        f_html = folder / f'hist.html'
        fig.write_html(str(f_html), include_plotlyjs='cdn')

if __name__ == '__main__':
    main()


"""
Divisors of 42 are : 1, 2, 3, 6, 7, 14, 21, 42. These divisors squared are: 1, 4, 9, 36, 49, 196, 441, 1764. The sum of the squared divisors is 2500 which is 50 * 50, a square!

Given two integers m, n (1 <= m <= n) we want to find all integers between m and n whose sum of squared divisors is itself a square. 42 is such a number.

The result will be an array of arrays or of tuples (in C an array of Pair) or a string, each subarray having two elements, first the number whose squared divisors is a square and then the sum of the squared divisors.

#Examples:

list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
list_squared(42, 250) --> [[42, 2500], [246, 84100]]
The form of the examples may change according to the language, see Example Tests: for more details.

Note

In Fortran - as in any other language - the returned string is not permitted to contain any redundant trailing whitespace: you can use dynamically allocated character strings.
"""

from functools import reduce
from math import sqrt

def factors(n):
    return set(reduce(list.__add__,
                ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))

def list_squared(m, n):
    nums = []
    for i in range(m,n+1):
        fc = factors(i)
        sm = sum(map(lambda x:x**2, fc))
        if sqrt(sm).is_integer():
            nums.append([i, sm])
    return nums

Codewars/5 kyu/Integers Recreation One.py


import base64
import boto3
import gzip
import json
import logging
import os
import re
import time

from datetime import datetime, timezone

logging.basicConfig()
logging.getLogger().setLevel(level=logging.INFO)

## fail fast on bad configuration
kinesis_client = boto3.client('kinesis')
kinesis_stream = os.environ['DESTINATION_STREAM_NAME']

python_logging_regex    = re.compile(r'\[([A-Z]+)]\s+([0-9]{4}-.*Z)\s+([-0-9a-fA-F]{36})\s+(.*)')

lambda_start_regex      = re.compile(r'START RequestId:\s+([-0-9a-fA-F]{36})\s+Version:\s+(.+)')
lambda_finish_regex     = re.compile(r'END RequestId:\s+([-0-9a-fA-F]{36})')
lambda_report_regex     = re.compile(r'REPORT RequestId:\s+([-0-9a-fA-F]{36})\s+Duration:\s+([0-9.]+)\s+ms\s+Billed Duration:\s+([0-9]+)\s+ms\s+Memory Size:\s+([0-9]+)\s+MB\s+Max Memory Used:\s+([0-9]+)\s+MB')
lambda_extended_regex   = re.compile(r'.*Init Duration:\s+([0-9.]+)\s+ms')
lambda_xray_regex       = re.compile(r'.*XRAY TraceId:\s+([0-9a-fA-F-]+)\s+SegmentId:\s+([0-9a-fA-F]+)\s+Sampled:\s+(true|false)')

def lambda_handler(event, context):
    output_messages = extract_messages(event)
    logging.info(f'total number of messages to output: {len(output_messages)}')
    logging.debug(f'output messages: {json.dumps(output_messages)}')
    write_to_kinesis(output_messages)

def extract_messages(event):
    """ Source records may contain multiple log messages, so we can't just use a list
        comprehension to extract them. We also want to skip control records, and any
        records that we can't process for unforeseen reasons. Thus this function.
        """
    output_messages = []
    for record in event['Records']:
        output_messages += process_input_record(record)
    return output_messages

def process_input_record(record):
    try:
        payload = record['kinesis']['data']
        decoded = gzip.decompress(base64.b64decode(payload))
        data = json.loads(decoded)
        message_type = data.get('messageType')
        if message_type == 'DATA_MESSAGE':
            log_group = data['logGroup']
            log_stream = data['logStream']
            events = data.get('logEvents', [])
            logging.info(f'processing {len(events)} events from group "{log_group}" / stream "{log_stream}"')
            logging.debug(f'input events: {json.dumps(events)}')
            return [transform_log_event(log_group, log_stream, event) for event in events]
        elif message_type == 'CONTROL_MESSAGE':
            logging.debug('skipping control message')
        elif message_type:
            logging.warning(f'unexpected message type: {message_type}')
    except:
        logging.error(f'failed to process record; keys = {record.keys()}', exc_info=True)
    # fall-through for any unprocessed messages (exception or unhandled message type)
    return []

def transform_log_event(log_group, log_stream, event):
    """ Turns the message into JSON if it isn't already, recognizing standard logging
        formats; adds tracking fields.
        """
    message = event.get('message', '').strip()
    result = try_parse_json(message)
    if not result:
        result = try_parse_python_log(message)
    if not result:
        result = try_parse_lambda_status(message)
    if not result:
        result = {
            'level': 'INFO',
            'message': message
        }

    result['source'] = {
        'logGroup': log_group,
        'logStream': log_stream
    }
    opt_add_timestamp(result, event)
    return result

def try_parse_json(message):
    if message.startswith('{') and message.endswith('}'):
        try:
            return json.loads(message)
        except:
            pass

def try_parse_python_log(message):
    """ Attempts to parse the passed message as output from the Lambda Python logger, as
        documented here: https://docs.aws.amazon.com/lambda/latest/dg/python-logging.html
        """
    match = python_logging_regex.match(message)
    if match:
        return {
            'level':        match.group(1),
            'timestamp':    match.group(2),
            'lambda':       { 'requestId': match.group(3) },
            'message':      match.group(4)
        }

def try_parse_lambda_status(message):
    """ Attempts to parse the message as one of the standard Lambda status messages.
        """
    try:
        if message.startswith('START RequestId:'):
            match = lambda_start_regex.match(message)
            if match:
                data = {
                    'requestId':    match.group(1),
                    'version':      match.group(2)
                }
        elif message.startswith('END RequestId:'):
            match = lambda_finish_regex.match(message)
            if match:
                data = {
                    'requestId':    match.group(1)
                }
        elif message.startswith('REPORT RequestId:'):
            message = message.replace('\n', '\t')
            match = lambda_report_regex.match(message)
            if match:
                data = {
                    'requestId':    match.group(1),
                    'durationMs':   float(match.group(2)),
                    'billedMs':     float(match.group(3)),
                    'maxMemoryMb':  int(match.group(4)),
                    'usedMemoryMb': int(match.group(5))
                }
                # initialization stats are only reported for first invocation
                match = lambda_extended_regex.match(message)
                if match:
                    data['initializationMs'] = float(match.group(1))
                # x-ray stats are only reported if enabled
                match = lambda_xray_regex.match(message)
                if match:
                    data['xrayTraceId'] = match.group(1)
                    data['xraySegment'] = match.group(2)
                    data['xraySampled'] = match.group(3)
        if data:
            return {
                'level':   'INFO',
                'message': message,
                'lambda':  data
            }
    except:
        pass

def opt_add_timestamp(data, event):
    """ If the passed data field already has a "timestamp" element, it's returned
        unchanged. Otherwise the log event's timestamp is formatted and added to
        the message.
        """
    if data.get('timestamp'):
        return
    dt = datetime.fromtimestamp(event['timestamp'] / 1000.0, tz=timezone.utc)
    data['timestamp'] = dt.isoformat()

def write_to_kinesis(listOfEvents):
    """ Makes a best-effort attempt to write all messages to Kinesis, batching them
        as needed to meet the limits of PutRecords.
        """
    records = prepare_records(listOfEvents)
    while records:
        records = process_batch(records)
        if (records):
            time.sleep(2) # an arbitrary sleep; we should rarely hit this
    return

def prepare_records(listOfEvents):
    records = []
    for event in listOfEvents:
        partition_key = event.get('cloudwatch', {}).get('logStream', 'DEFAULT')
        records.append({
            'PartitionKey': partition_key,
            'Data': json.dumps(event)
        })
    return records

def process_batch(records):
    """ Forms a batch from the provided records and attempts to send it; any records
        that won't fit in the batch will be returned, as well as any that couldn't
        be sent (we return unattempted records first to give them the best chance of
        being sent if there are persistent errors).
        """
    to_be_sent, to_be_returned = build_batch(records)
    logging.info(f'sending batch of {len(to_be_sent)} records with {len(to_be_returned)} remaining')
    try:
        response = kinesis_client.put_records(
            StreamName=kinesis_stream,
            Records=to_be_sent
        )
        return process_response(response, to_be_sent) + to_be_returned
    except kinesis_client.exceptions.ProvisionedThroughputExceededException:
        logging.warn(f'received throughput-exceeded on stream {kinesis_stream}; retrying all messages')
        return to_be_sent + to_be_returned

def build_batch(records):
    """ Creates a batch of records based on Kinesis limits; returns both the batch
        and any remaining records that didn't fit in the batch.
        """
    rec_count = 0
    byte_count = 0

    while rec_count < min(len(records), 500) and byte_count < 1048576:
        record = records[rec_count]
        rec_count += 1
        byte_count += len(record['Data']) + len(record['PartitionKey'])

    # we already added the record before we knew the size, so we'll compensate
    if byte_count > 1048576:
        rec_count = rec_count - 1

    # this should never happen: it would require a max-size log event and a
    # long partition key
    if rec_count == 0:
        logging.warn('throwing out too-long message')
        return [], records[1:]

    return records[:rec_count], records[rec_count:]

def process_response(response, records):
    """ Examines the response from PutRecords, returning any records that couldn't be sent.
        """
    if response['FailedRecordCount'] == 0:
        return []

    result = []
    dropped_record_count = 0
    for ii in range(len(response['Records'])):
        entry = response['Records'][ii]
        errorCode = entry.get('ErrorCode')
        if errorCode == 'ProvisionedThroughputExceededException':
            result.append(records[ii])
        elif errorCode:
            dropped_record_count += 1

    if dropped_record_count > 0:
        logging.warn(f'dropped {dropped_record_count} records due to Kinesis internal errors')
    if len(result) > 0:
        logging.info(f'requeueing {len(result)} records due to throughput-exceeded')

    return result


from typing import Dict, Tuple

import numpy as np

import visualization
from map import SimulationMap

class AutoExpandingMap:
    def render_chunk(self, chunk_x, chunk_y):
        self.assert_exist_chunk((chunk_x, chunk_y))
        img = np.zeros((self.chunk_y * 16, self.chunk_x * 16, 3), dtype=np.uint8)
        for x in range(self.chunk_x):
            for y in range(self.chunk_y):
                img[y * 16:(y + 1) * 16, x * 16:(x + 1) * 16, :] = self.tilemap[
                    self.chunks[(chunk_x, chunk_y)].type_map[x, y]]
        return img

    def __init__(self, chunk_shape):
        self.chunks: Dict[Tuple[int, int], SimulationMap] = {}
        self.chunk_x, self.chunk_y = chunk_shape
        self.tilemap = visualization.TileSet(16)

    def __getitem__(self, args):
        x, y = args
        chunk_x, inchunk_x = divmod(x, self.chunk_x)
        chunk_y, inchunk_y = divmod(y, self.chunk_y)
        chunk_index = (chunk_x, chunk_y)
        self.assert_exist_chunk(chunk_index)
        return self.chunks[chunk_index].height_map[inchunk_x, inchunk_y], \
               self.chunks[chunk_index].type_map[inchunk_x, inchunk_y]

    def assert_exist_chunk(self, chunk_index):
        if chunk_index not in self.chunks:
            self.chunks[chunk_index] = SimulationMap((self.chunk_x, self.chunk_y), *chunk_index)


def get_words(language, max_words=None):
    words = []
    path = './bin/resources/%s/%s_words.txt' % (language, language)
    with open(path, 'r') as f:
        for idx, line in enumerate(f.readlines()):
            if max_words is not None and idx == max_words:
                break
            if line.count('    ') != 1 or line.count(': ') != 1:
                raise Exception(
                    'Invalid format on line %s in file %s' % ((idx + 1), path))
            line = line.strip()
            foreign, pron_eng = line.split('    ')
            pron, eng = pron_eng.split(': ')
            new_word = {
                'foreign': foreign,
                'pronunciation': pron,
                'english': eng
            }
            words.append(new_word)
    return words


#import numpy #删除列表重复的
import requests
from bs4 import BeautifulSoup
import re
import urllib3
urllib3.disable_warnings()  # 忽略https证书告警

def DNS_Climb_censys(DNS):
    # try:
    amount = 4  # 默认页数

    Dns_List = []
    headers={
        'Host': 'search.censys.io',
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Firefox/91.0',
        'Accept': '*/*',
        'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
        'Accept-Encoding': 'gzip, deflate',
        'X-Requested-With': 'XMLHttpRequest',
        'Connection': 'close',
        'Referer': 'https://search.censys.io',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
    }

    for i in range(int(amount)):
        url=f"https://search.censys.io/certificates/_search?q={DNS}&page={i}"
        html=requests.get(url=url,headers=headers,verify=False)
        soup = BeautifulSoup(html.text, 'lxml')
        dns_ = soup.find_all(text=re.compile(rf"{DNS}"))
        for i in dns_: # 临时发挥写的现在我也懵看不太懂
            a=re.sub('\*=', '', i)
            a=a.split(',')[0]
            a = a.split('=')
            if len(a)==2:
                if not (a in Dns_List):  # 取反如果Dns_List列表里面有就不添加
                    Dns_List.append(a[1])

    #Dns_List=numpy.unique(Dns_List)
    return Dns_List

    # except Exception as bc:
    #     print("有错误！错误提示" + str(bc))

if __name__=="__main__":
    print(DNS_Climb_censys('baidu.com'))


n = int(input('Введите количество элементов массива:'))
a = input('Введите элементы массива: ').split()
x = int(input('Введите количество элементов на которое надо сдвинуть: '))
for i in range(x):
    temp = a[i]
    a[i] = a[n - x + i]
    a[n - x + i] = temp
for i in range(x, n - 1):
    for j in range(x, n - i - 1):
        if a[j] > a[j + 1]:
            temp = a[j]
            a[j] = a[j + 1]
            a[j + 1] = temp
print(*a)


from pycaret.datasets import get_data
from pycaret.classification import *
from Species import Species
from Model import Model
from FileService import FileService
from Prints import Prints
from StoreService import StoreService
from StoreRepository import StoreRepository
from StoreDTO import StoreDTO
from Menu import Menu

class Prediction(StoreService, StoreRepository, Model, object):

    fileName = ''
    fileNameWithExt = ''

    PrintsService = None

    def __init__(self, *args):
        pass
        self.PrintsService = Prints()
        self.PrintsService.print_options()

    def input_features(self):
        data = input('ENTER FEATURES: ')
        self.features = data.split()
        if len(self.features) < 8:
            print("INVALID INPUT FEATURES")
            self.input_features()

    def print_features(self):
        self.PrintsService.print_features(self.features)

    def input_prediction(self):

        self.PrintsService.print_options()

        winning = input("ENTER WINNING RESULT: ")

        if winning == "": self.input_prediction()
        if winning in [member.value for member in Menu]:

            if winning == Menu("RESET").value:
                self.features = []
                print(self.features)
                self.input_prediction()

            elif winning == Menu("BACK").value:
                self.features = self.featuresHistory.copy()
                print(self.features)
                self.input_prediction()

            elif winning == Menu("QUIT").value:
                quit()

        self.featuresHistory = self.features.copy()
        self.features.append(winning)

        if len(self.features) > 24:
            self.features.pop(0)
        else:
            print(len(self.features))
            print(self.features)
            self.input_prediction()

        row = self.features.copy()

        prev_rediction = self.prediction_list.copy()

        self.saveToFile(row, self.fileNameWithExt)
        self.predictFeature() # starts predicting

        # update stats
        self.setStat(Species(int(winning)).name, 1)
        self.print_features()

        setVeggies = self.PrintsService.set_veggies(self.prediction_list)
        self.set_prediction_list(setVeggies)
        self.PrintsService.numeric_to_alpha_prediction(setVeggies)

        self.setWinLossStat(winning, prev_rediction)

        # get win and percentage
        print(f"\n")

        predict_logic = Prints()
        self.prediction_list =  predict_logic.prediction_logic(self.prediction)

        print(f"PREVIOUS WINNING RESULT: {Species(int(winning)).name}")

        self.setVeggieHitCount(winning, self.prediction_list)
        print(f"\n")

        self.resetStatValue(Species(int(winning)).name, 0)
        for idx in range(10): self.setStat(Species(int(idx)).name, 1)
        self.printStats()

        self.input_prediction()

    def predictFeature(self):
        data = self.predictNext(data=get_data(self.fileName))
        for item in data:
            self.prediction.append(item)

    def set_prediction_list(self, data):
        self.prediction_list = data.copy()

    def printStats(self):

        TOTAL_ROUNDS = self.getTotalRounds()
        WIN_PERCENT = self.getWinPercent()
        LOSS_PERCENT = self.getLossPercent()

        print(f"TOTAL ROUNDS: {TOTAL_ROUNDS}")
        print(f"TOTAL WIN: {self.TOTAL_WIN} {WIN_PERCENT}%, \t TOTAL LOSS: {self.TOTAL_LOSS} {LOSS_PERCENT}%")
        print(f"WINNING STREAK: {self.STREAK_WIN}, \t LOSING STREAK: {self.STREAK_LOSS}")
        print(f"VEGGIE STRIKE: {self.VEGE_STRIKE}, \t VEGGIE SPARE: {self.VEGE_SPARE}")
        print(f"\n")
        print(f"VEGGIE TOP: {self.VEGE_TOP}, \t VEGGIE MID: {self.VEGE_MID}, \t VEGGIE BOT: {self.VEGE_BOT}")
        print(f"\n")
        print(f"HOT_DOG: {self.HOT_DOG}, BBQ: {self.BBQ}, HAM: {self.HAM}, STEAK: {self.STEAK}")
        print(f"TOMATO: {self.TOMATO}, CABBAGE: {self.CABBAGE}, CORN: {self.CORN}, CARROT: {self.CARROT}")
        print(f"\n")


# -*- coding: utf-8 -*-

from eaglet.core import api_resource
from eaglet.decorator import param_required

from business.mall.corporation_factory import CorporationFactory
from business.common.page_info import PageInfo

class AProductCreationOption(api_resource.ApiResource):
	"""
	商品配置相关信息
	"""
	app = "product"
	resource = "creation_option"

	@staticmethod
	def __load_pay_interfaces(corp):
		"""
		获取支付接口配置
		"""
		pay_interfaces = corp.pay_interface_repository.get_active_pay_interfaces()
		datas = []
		for pay_interface in pay_interfaces:
			data = {
				"id": pay_interface.id,
				"type": pay_interface.type,
				"name": pay_interface.name
			}
			datas.append(data)

		return datas

	@staticmethod
	def __load_postag_config(corp, product_id=None):
		"""
		获取创建/查看商品时候,运费模板配置信息
		"""
		# 普通平台,商品使用平台设置的,自应平台应该显示商品的供货商的默认运费模板
		if not corp.is_self_run_platform():
			postage_config = corp.postage_config_repository.get_corp_used_postage_config()
			return {
				'id': postage_config.id,
				'name': postage_config.name
			}
		else:
			if not product_id:
				return {'id': '', 'name': ''}
			weizoom_corp = CorporationFactory.get_weizoom_corporation()
			CorporationFactory.set(weizoom_corp)
			product = corp.product_pool.get_product_by_id(product_id)
			postage_config = weizoom_corp.postage_config_repository.get_supplier_used_postage_config(product.supplier_id)
			CorporationFactory.set(corp)
			return {
				'id': postage_config.id,
				'name': postage_config.name
			}

	@staticmethod
	def __load_product_property_templates(corp):
		"""
		获取商品属性模板集合
		"""
		product_property_templates = corp.product_property_template_repository.get_templates()
		datas = []
		for property_template in product_property_templates:
			data = {
				"id": property_template.id,
				"name": property_template.name
			}
			datas.append(data)

		return datas

	@staticmethod
	def __load_categories(corp):
		"""
		获取分类集合
		"""
		target_page = PageInfo(1, 1000)
		categories, _ = corp.category_repository.get_all_categories(target_page)
		datas = []
		for category in categories:
			datas.append({
				"id": category.id,
				"name": category.name
			})

		return datas

	@staticmethod
	def __load_limit_zones(corp, product_id=None):
		"""
		获取限定区域信息
		"""
		if not corp.is_self_run_platform():
			limit_zones = corp.limit_zone_repository.get_limit_zones()
			datas = []
			for limit_zone in limit_zones:
				datas.append({
					"id": limit_zone.id,
					"name": limit_zone.name
				})
			return datas
		else:
			# 自营平台创建商品
			if not product_id or int(product_id) <= 0:
				return []
			# 自应平台查看商品
			product = corp.product_pool.get_product_by_id(product_id)
			if not product.limit_zone:
				return []
			weizoom_corp = CorporationFactory.get_weizoom_corporation()
			CorporationFactory.set(weizoom_corp)
			limit_zone = weizoom_corp.limit_zone_repository.get_limit_zone_by_id(product.limit_zone)
			CorporationFactory.set(corp)
			return [{"id": limit_zone.id, "name": limit_zone.name}]

	@param_required(['corp_id'])
	def get(args):
		corp = args['corp']
		# 查看商品
		product_id = args.get('product_id')
		config = {}

		#支付接口配置
		config['pay_interfaces'] = AProductCreationOption.__load_pay_interfaces(corp)

		#运费模板
		config['postage_config_info'] = AProductCreationOption.__load_postag_config(corp, product_id=product_id)

		#属性模板
		config['property_templates'] = AProductCreationOption.__load_product_property_templates(corp)

		#分类信息
		config['categories'] = AProductCreationOption.__load_categories(corp)

		#限定区域信息
		config['limit_zones'] = AProductCreationOption.__load_limit_zones(corp, product_id=product_id)

		return config


"""Model config in json format"""

CFG = {
    "data": {
        "image_path": "data/Images/",
        "captions_file": "data/captions.txt"
    },
    "tokenize": {
        "vocab_size": 5000
    },
    "train": {
        "batch_size": 64,
        "buffer_size": 1000,
        "epoches": 10
    },
    "model": {
        "embedding_dim":256,
        "units":512
    }
}


"""
1) Напишите программу, которая запрашивает ввод двух значений. Если хотя бы одно из них не является числом, то должна выполняться конкатенация, т. е. соединение, строк. В остальных случаях введенные числа суммируются.
"""

inp1 = input()
inp2 = input()
try:
    print(int(inp1) + int(inp2))
except:
    print(inp1 + inp2)

"""
2) Создайте словарь, в котором ключами будут ID, а значениями - пользовательские имена (username). Напишите программу, которая запрашивает у вас юзернейм и выдает ID этого юзера. Если же данного юзернейма нет, то программа выдает вам исключение, которое содержит следующее сообщение:”Введенного юзера нет в базе данных”. Если исключение не выявилось (т.е. Данный юзернейм существует) программа должна приветствовать юзера по юзернейму. Также в любом случае программа должна в конце выдавать сообщение: “Спасибо!”
"""
data = {
  1: 'oomat',
  2: 'beknaz',
  3: 'eljaz',
  4: 'bekzat',
  5: 'yrys',
  6: 'arafat'
}
try:
  inp = input().lower()
  for key, value in data.items():
    if str(inp) == value:
      print(key)
    elif str(inp) not in list(data.values()):
      raise Exception
except:
  print('There is no such a username')
else:
  print(f'Hello {inp.title()}')
finally:
  print('Thank you!')
"""
3) Напишите программу, которая будет запрашивать ваш возраст. Если пользователь вводит отрицательное число либо 0, программа должна выдать исключение: “Ваш возраст должен быть больше 0 ”.
"""
try:
  age = int(input())
  if age <= 0:
    raise ValueError
except:
  print('Ваш возраст должен быть больше 0')
else:
  print(f'Ваш возраст {age}')


# import the necessary packages
import numpy as np
import imutils
import cv2
from copy import*

#TO-DO: FIND THE REASON WHY IT DOESNT WORK FOR PERSPECTIVE TRANFORMATION (CHECK BOUNDING RECT)
#       FIX COMBINING ALGORITHM

class Stitcher:
	def __init__(self):
		# determine if we are using OpenCV v3.X
		self.isv3 = imutils.is_cv3()

	#DOESNT WORK WITH PERSPECTIVE
	def stitch(self, images, ratio=0.75, reprojThresh=4.0,
		showMatches=False,resize = 400):
		#copy of images
		imgs = []
		for image in images:
			img = cv2.imread(image)
			if resize is not None:
				img = imutils.resize(img, width=resize)
			imgs.append(img)
		globalXOffset = 0
		globalYOffset = 0
		#keypoints
		ks = []
		#features
		fs = []
		#homographies, in the form of i+1 respect to i
		hs = []
		#find keypoints and features
		for image in imgs:
			kp,ft = self.detectAndDescribe(image)
			ks.append(kp)
			fs.append(ft)

		#calculate homographies
		for i in range(len(imgs)-1):
			hs.append((self.matchKeypoints(ks[i+1], ks[i],
			fs[i+1], fs[i], ratio, reprojThresh))[1])

		lt,rb = self.calcBoundingRect(imgs,hs)

		xOffset, yOffset = int(lt[0]),int(lt[1])
		w,h = int(rb[0])-xOffset, int(rb[1])-yOffset

		#create empty image of appropriate size
		main = np.zeros((h,w,3), np.uint8)

		#add identity matrix as the first homography matrix
		hs.insert(0,np.identity(3))

		#instantiate base homography matrix
		homography = np.identity(3)

		for i in range(len(imgs)):
			#update base homography matrix
			homography = homography.dot(hs[i])

			#create a copy of the base homography matrix to
			#translate it without affecting the other homographies
			hTranslated = deepcopy(homography)
			hTranslated[0][2] += -xOffset
			hTranslated[1][2] += -yOffset

			#warpPerspective
			result = cv2.warpPerspective(imgs[i], hTranslated,
			(w, h))
			#cv2.imshow('result'+str(i),result)
			#cv2.imshow('im1',imgs[-1])
			#cv2.imshow('im2',imgs[-2])

			#stich them with masking
			anded = cv2.bitwise_and(result,main)
			gray = cv2.cvtColor(anded, cv2.COLOR_BGR2GRAY)
			ret,binary = cv2.threshold(gray,1,255,cv2.THRESH_BINARY_INV)
			binary = cv2.cvtColor(binary,cv2.COLOR_GRAY2RGB)
			maskedMain = cv2.bitwise_and(main,binary)
			main = cv2.bitwise_or(maskedMain,result)

		cv2.imshow('main',main)
		return result

	def detectAndDescribe(self, image):
		# convert the image to grayscale
		gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

		# check to see if we are using OpenCV 3.X
		if self.isv3:
			# detect and extract features from the image
			descriptor = cv2.xfeatures2d.SIFT_create()
			(kps, features) = descriptor.detectAndCompute(image, None)

		# otherwise, we are using OpenCV 2.4.X
		else:
			# detect keypoints in the image
			detector = cv2.FeatureDetector_create("SIFT")
			kps = detector.detect(gray)

			# extract features from the image
			extractor = cv2.DescriptorExtractor_create("SIFT")
			(kps, features) = extractor.compute(gray, kps)

		# convert the keypoints from KeyPoint objects to NumPy
		# arrays
		kps = np.float32([kp.pt for kp in kps])

		# return a tuple of keypoints and features
		return (kps, features)

	def matchKeypoints(self, kpsA, kpsB, featuresA, featuresB,
		ratio, reprojThresh):
		# compute the raw matches and initialize the list of actual
		# matches
		matcher = cv2.DescriptorMatcher_create("BruteForce")
		rawMatches = matcher.knnMatch(featuresA, featuresB, 2)
		matches = []

		# loop over the raw matches
		for m in rawMatches:
			# ensure the distance is within a certain ratio of each
			# other (i.e. Lowe's ratio test)
			if len(m) == 2 and m[0].distance < m[1].distance * ratio:
				matches.append((m[0].trainIdx, m[0].queryIdx))

		# computing a homography requires at least 4 matches
		if len(matches) > 4:
			# construct the two sets of points
			ptsA = np.float32([kpsA[i] for (_, i) in matches])
			ptsB = np.float32([kpsB[i] for (i, _) in matches])

			# compute the homography between the two sets of points
			(H, status) = cv2.findHomography(ptsA, ptsB, cv2.RANSAC,
				reprojThresh)

			# return the matches along with the homograpy matrix
			# and status of each matched point
			return (matches, H, status)

		# otherwise, no homograpy could be computed
		return None

	def drawMatches(self, imageA, imageB, kpsA, kpsB, matches, status):
		# initialize the output visualization image
		(hA, wA) = imageA.shape[:2]
		(hB, wB) = imageB.shape[:2]
		vis = np.zeros((max(hA, hB), wA + wB, 3), dtype="uint8")
		vis[0:hA, 0:wA] = imageA
		vis[0:hB, wA:] = imageB

		# loop over the matches
		for ((trainIdx, queryIdx), s) in zip(matches, status):
			# only process the match if the keypoint was successfully
			# matched
			if s == 1:
				# draw the match
				ptA = (int(kpsA[queryIdx][0]), int(kpsA[queryIdx][1]))
				ptB = (int(kpsB[trainIdx][0]) + wA, int(kpsB[trainIdx][1]))
				cv2.line(vis, ptA, ptB, (0, 255, 0), 1)

		# return the visualization
		return vis

	def getBoundingRect(self,x0,y0,w0,h0,x1,y1,w1,h1):
		return (min(x0,x1),min(y0,y1),max(x0+w0,x1+w1),max(y0+h0,y1+h1))

	def negToZero(self,number):
		if number < 0:
			return 0
		return number

	def calcBoundingRect(self,imgs,hs):
		points = []
		h = np.identity(3)
		for i in range(len(imgs)):
			width,height = (imgs[i].shape[1],imgs[i].shape[0])
			lt = h.dot(np.array([0, 0, 1]))
			lb = h.dot(np.array([0, height,1]))
			rt = h.dot(np.array([width, 0, 1]))
			rb = h.dot(np.array([width, height,1]))
			points.extend([lt,lb,rt,rb])
			if i < len(imgs)-1:
				h = h.dot(hs[i])
		lt,rb = ((min(points,key = lambda t: t[0])[0],min(points,key = lambda t: t[1])[1]),
				(max(points,key = lambda t: t[0])[0],max(points,key = lambda t: t[1])[1]))
		return (lt,rb)


ANIMAL_NAMES = {
    "אוגר"
    "אייל",
    "איילה",
    "איל",
    "אילה",
    "אנקה",
    "ארנב",
    "ארנבון",
    "ארנבת",
    "אריה",
    "ברדלס",
    "בנאי",
    "דב",
    "דבורה",
    "דביבון",
    "דג",
    "דוב",
    "דורבן",
    "דרבן",
    "זאב",
    "זבוב",
    "זחל",
    "חרק",
    "חתול",
    "חיפושית",
    "חפושית",
    "חרגול",
    "טיגריס",
    "יתוש",
    "כלב",
    "כפיר",
    "כריש",
    "לביא",
    "לטאה",
    "ליש",
    "מדוזה",
    "מרבה רגליים",
    "נחש",
    "נמר",
    "ס",
    "סוס",
    "סנאי",
    "סרטן",
    "עכביש",
    "עכבר",
    "עכברוש",
    "עלוקה",
    "עקרב",
    "פישפש",
    "פנתר",
    "פר",
    "פרה",
    "פרעוש",
    "פשפש",
    "צב",
    "צבוע",
    "צפרדע",
    "צרצר",
    "קרפד",
    "קרפדה",
    "קרציה",
    "שועל",
    "שחף",
    "שפירית",
    "תולעת",
    "תמנון",
    "תנין"
}
ARTISTS_BY_LETTER_SIZES = {
    "א": 52,
    "ב": 8,
    "ג": 13,
    "ד": 16,
    "ה": 8,
    "ו": 3,
    "ז": 3,
    "ח": 8,
    "ט": 5,
    "י": 25,
    "כ": 2,
    "ל": 11,
    "מ": 24,
    "נ": 15,
    "ס": 7,
    "ע": 16,
    "פ": 5,
    "צ": 4,
    "ק": 5,
    "ר": 17,
    "ש": 18,
    "ת": 5
}


import torch as tr
import torchvision as tv
import numpy as np
import random, os, math
from tqdm import trange
from pathlib import Path
from datetime import datetime
from tensorboardX import SummaryWriter
from config import get_config
from models import Generator, Discriminator
from vgg import FeatureExtractor

from processing import get_dataset, downsample, reverse_normalize, normalize, randomcrop, tt

device = tr.device('cuda' if tr.cuda.is_available() else 'cpu')
cfg = get_config()

class SRGAN(object):
    def __init__(self, cfg):
        super(SRGAN, self).__init__()

        # Networks
        self.generator = Generator(cfg)
        self.discriminator = Discriminator(cfg)

        # Optimizers
        self.optim_gen = tr.optim.Adam(self.generator.parameters(), lr=cfg.learning_rate, betas=(cfg.beta1, 0.999))
        self.optim_disc = tr.optim.Adam(self.discriminator.parameters(), lr=cfg.learning_rate, betas=(cfg.beta1, 0.999))

        # loss models
        self.mse_loss = tr.nn.MSELoss()
        self.bce_loss = tr.nn.BCELoss(reduction='sum')
        self.feature_extractor = FeatureExtractor()

        # Get image dataset (cropped)
        dataset = get_dataset(cfg.data_dir)
        self.dataloader = tr.utils.data.DataLoader(dataset, batch_size=cfg.batch_size, shuffle=True)

        # For logging results to tensorboard
        self.global_step = 0
        self.build_writers()

    def build_writers(self):
        if not Path(cfg.save_dir).is_dir():
            os.mkdir(cfg.save_dir)
        if cfg.extension is None:
            cfg.extension = datetime.now().strftime('%Y-%m-%d_%H:%M:%S')

        self.save_path = cfg.save_dir + cfg.extension

        log_path = cfg.log_dir + cfg.extension
        self.writer = SummaryWriter(log_path)

    def logger(self, name, loss):
        if self.global_step % cfg.log_freq == 0:
            # Log vars
            self.writer.add_scalar(name, loss, self.global_step)

    def log_state(self, name, image):
        if self.global_step % (cfg.log_freq * 5) == 0:
            image = reverse_normalize(image)
            self.writer.add_image(name, image, self.global_step)

    def pretrain(self):
        '''Pretrain the generator network using a MSE loss function. This is done so that SRGAN generator does not fall into a local optima'''

        if os.path.isfile('./pretrained_models/pretrained.pt'):
            print("loaded_checkpoint: ./pretrained_models/pretrained.pt")
            checkpoint = tr.load('./pretrained_models/pretrained.pt')
            self.generator.load_state_dict(checkpoint['generator_state'])
            self.optim_gen.load_state_dict(checkpoint['generator_optimizer'])
            self.generator.train()

        print('Pretraining Generator')

        ds = tr.FloatTensor(cfg.batch_size, cfg.num_channels, cfg.cropsize//cfg.factor, cfg.cropsize//cfg.factor)

        for epoch in trange(cfg.pretrain_epochs):

            # Batch
            for i, data in enumerate(self.dataloader):

                # mini-batch data
                hr, _ = data

                if hr.size(0) < cfg.batch_size:
                    break

                # Downsample images to low resolution
                # Removing NORMALIZE -> [0,1] instead of ~ N(0.485, 0.29)
                # remove in downscale if so
                for j in range(cfg.batch_size):
                    ds[j] = downsample(hr[j])
                    hr[j] = normalize(hr[j])

                # Generate the super resolution image
                sr = self.generator(ds)

                # https://stackoverflow.com/questions/48001598/why-do-we-need-to-call-zero-grad-in-pytorch
                self.generator.zero_grad()

                loss = self.mse_loss(sr, hr)
                loss.backward()
                self.optim_gen.step()

                self.logger('Pretrain/Generator loss', loss)

                self.log_state('Pretrain/Downsampled', ds[0])
                self.log_state('Pretrain/Generated', sr[0])
                self.log_state('Pretrain/Original', hr[0])
                self.global_step += 1

            if epoch % cfg.save_freq == 0:
                tr.save({
                    'generator_state': self.generator.state_dict(),
                    'generator_optimizer': self.optim_gen.state_dict()
                }, './pretrained_models/pretrained.pt')

    def train(self):
        '''Train the SRGAN using the just pretrained or past pretrained generator.'''

        if os.path.isfile('./pretrained_models/pretrained.pt'):
            print("loaded_checkpoint: ./pretrained_models/pretrained.pt")
            checkpoint = tr.load('./pretrained_models/pretrained.pt')
            self.generator.load_state_dict(checkpoint['generator_state'])
            self.optim_gen.load_state_dict(checkpoint['generator_optimizer'])
            self.generator.train()

        print('Training SRGAN')

        ds = tr.FloatTensor(cfg.batch_size, cfg.num_channels, cfg.cropsize//cfg.factor, cfg.cropsize//cfg.factor)

        # Restart global step for SRGAN tape
        self.global_step = 0

        real_label = tr.ones((cfg.batch_size,1))
        fake_label = tr.zeros((cfg.batch_size,1))

        for epoch in trange(cfg.epochs):

            # Mini-batchs
            for i, data in enumerate(self.dataloader):
                # Generate data
                hr, _ = data

                # Prevent error if the dataloader runs out of images
                if hr.size(0) < cfg.batch_size:
                    break

                # mini-batch data (ToTensor() normalizes between [0,1])
                # randn() instead of rand() for normalized input [-1,1]
                nz = tr.rand((hr.size(0),hr.size(1),hr.size(2)//cfg.factor, hr.size(3)//cfg.factor))
                for j in range(cfg.batch_size):
                    ds[j] = downsample(hr[j])
                    hr[j] = normalize(hr[j])
                    nz[j] = normalize(nz[j])

                ## Begin Training Discriminator
                # ----------------------------------------------------------------
                self.discriminator.zero_grad()
                # Why zero_grad
                # https://stackoverflow.com/questions/48001598/why-do-we-need-to-call-zero-grad-in-pytorch

                # BCELoss use explained here
                # https://pytorch.org/tutorials/beginner/dcgan_faces_tutorial.html

                # train real
                truth = self.discriminator(hr)
                loss_truth = self.bce_loss(truth, real_label)
                loss_truth.backward()

                # train fake
                noise = self.generator(nz)
                fake = self.discriminator(noise.detach())
                loss_fake = self.bce_loss(fake, fake_label)
                loss_fake.backward()

                loss_disc = loss_truth + loss_fake
                self.optim_disc.step()

                # Begin Training Generator
                # ----------------------------------------------------------------
                self.generator.zero_grad()

                # Generate the super resolution image and judge it
                sr = self.generator(ds)
                generated = self.discriminator(sr)

                # Calculate Perceptual Loss
                true_features = self.feature_extractor(hr)
                generated_features = self.feature_extractor(sr)
                content_loss = self.mse_loss(generated_features, true_features)
                adversarial_loss = self.bce_loss(generated, real_label)
                loss_gen = content_loss + (10e-3 * adversarial_loss)

                # Optimize Generator
                loss_gen.backward()
                self.optim_gen.step()

                # Log Losses
                self.logger('SRGAN/Content Loss', content_loss)
                self.logger('SRGAN/Adversarial Loss', adversarial_loss)
                self.logger('SRGAN/Discriminator Loss', loss_disc)
                self.logger('SRGAN/Generator Loss', loss_gen)

                # Log Images
                self.log_state('SRGAN/Original', hr[0])
                self.log_state('SRGAN/Downsampled', ds[0])
                self.log_state('SRGAN/Generated', sr[0])

                # Increment Tape
                self.global_step += 1

            if epoch % cfg.save_freq == 0:
                tr.save({
                    'generator_state': self.generator.state_dict(),
                    'generator_optimizer': self.optim_gen.state_dict()
                }, self.save_path+'.pt')

            # Get image dataset (cropped)
            dataset = get_dataset(cfg.data_dir)
            self.dataloader = tr.utils.data.DataLoader(dataset, batch_size=cfg.batch_size, shuffle=True)

def main():
    srgan = SRGAN(cfg)

    if cfg.pretrain is True:
        srgan.pretrain()

    srgan.train()

if __name__ == '__main__':
    main()