import tensorflow as tf
import numpy as np
import os

from drl_atari import utils

class A2C:

    def __init__(self, cfg, input_shape, output_shape):
        self.cfg = cfg
        self.input_shape = input_shape
        self.output_shape = output_shape

        self.sess = None
        self.total_updates = 0

        dir = os.path.join(os.path.join(utils.output_dir(self.cfg), self.cfg['models_dir']))
        if not os.path.exists(dir):
            os.makedirs(dir)

        self._bias_initializer = tf.constant_initializer(value=0.)

    def _kernel_initializer(self, scale=np.sqrt(2)):
        return tf.orthogonal_initializer(gain=scale)

    def conv2d(self, x, filters, kernel_size, strides, name):
        return tf.layers.conv2d(x, filters, kernel_size, strides, padding='same',
                                activation=tf.nn.relu, name=name,
                                kernel_initializer=self._kernel_initializer(np.sqrt(2)),
                                bias_initializer=self._bias_initializer)

    def build(self, initial_learning_rate):
        num_actions = self.output_shape[0]
        batch = None
        height, width, channels = self.input_shape

        self.input = tf.placeholder(tf.float32, shape=(batch, height, width, channels), name='input')
        with tf.variable_scope("a2c_model"):

            x = (self.input / 255. - 0.5) * 2.
            if self.cfg['architecture'] == 'a3c_like':
                x = self.conv2d(x, filters=16, kernel_size=[8, 8], strides=[4, 4], name='conv1')
                x = self.conv2d(x, filters=32, kernel_size=[4, 4], strides=[2, 2], name='conv2')
                x_flat = tf.reshape(x, [-1, x.shape[1] * x.shape[2] * x.shape[3]])
                dense = tf.layers.dense(x_flat, units=256, activation=tf.nn.relu, name='dense')
            elif self.cfg['architecture'] == 'dqn_like':
                x = self.conv2d(x, filters=32, kernel_size=(8, 8), strides=[4, 4], name='conv1')
                x = self.conv2d(x, filters=64, kernel_size=(4, 4), strides=[2, 2], name='conv2')
                x = self.conv2d(x, filters=64, kernel_size=(3, 3), strides=[1, 1], name='conv3')
                x_flat = tf.reshape(x, [-1, x.shape[1] * x.shape[2] * x.shape[3]])
                dense = tf.layers.dense(x_flat, units=512, activation=tf.nn.relu, name='dense',
                                        kernel_initializer=self._kernel_initializer(np.sqrt(2)),
                                        bias_initializer=self._bias_initializer)

            self.value = tf.layers.dense(dense, units=1, name='value_func',
                                         kernel_initializer=self._kernel_initializer(scale=1.),
                                         bias_initializer=self._bias_initializer)
            self.value = tf.squeeze(self.value, axis=-1)
            self.action = tf.layers.dense(dense, units=num_actions, name='action',
                                          kernel_initializer=self._kernel_initializer(scale=1.),
                                          bias_initializer=self._bias_initializer)
            self.predict_action = tf.nn.softmax(self.action, name='action_softmax')

        self.cum_reward = tf.placeholder(tf.float32, shape=(batch, ), name='cum_reward')
        self.advantage = tf.placeholder(tf.float32, shape=(batch, ), name='advantage')
        self.action_labels = tf.placeholder(tf.int32, shape=(batch, ), name='action_label')
        self.masks_valid = tf.placeholder(tf.float32, shape=(batch, ), name='masks_valid')

        policy_loss = tf.reduce_mean(self.masks_valid * self.advantage *
                                     tf.nn.sparse_softmax_cross_entropy_with_logits(
                                         logits=self.action, labels=self.action_labels),
                                     name='policy_loss')

        entropy_reg = tf.reduce_mean(self.masks_valid * utils.cat_entropy(self.action),
                                     name='entropy_regularization')

        value_loss = tf.reduce_mean(self.masks_valid * tf.square(self.cum_reward - self.value),
                                    name='value_loss')

        total_loss = self.cfg['policy_loss_weight'] * policy_loss\
            - self.cfg['entropy_reg_value'] * entropy_reg\
            + self.cfg['value_loss_weight'] * value_loss

        self.global_step = tf.Variable(0, name='global_step', trainable=False)
        learning_rate = tf.train.exponential_decay(initial_learning_rate, self.global_step,
                                                   self.cfg['decay_steps'], self.cfg['decay_rate'],
                                                   staircase=True)
        if self.cfg['optimizer'] == "RMSProp":
            optimizer = tf.train.RMSPropOptimizer(learning_rate, decay=self.cfg['rmsprop_decay'],
                                                  momentum=self.cfg['momentum'], epsilon=1e-05)
        elif self.cfg['optimizer'] == "Adam":
            optimizer = tf.train.AdamOptimizer(learning_rate, epsilon=1e-05)
        else:
            raise ValueError("Optimizer %s not supported!" % self.cfg['optimizer'])

        trainable_vars = tf.trainable_variables()

        grads, vars = zip(*optimizer.compute_gradients(total_loss, trainable_vars))
        grads_clipped, _ = tf.clip_by_global_norm(grads, self.cfg['max_grad_norm'])

        self.update_op = optimizer.apply_gradients(zip(grads_clipped, vars),
                                                   global_step=self.global_step)

        self.summaries_scalar = tf.summary.merge([
            tf.summary.scalar("learning_rate", learning_rate),
            tf.summary.scalar("mean_policy_loss", tf.reduce_mean(policy_loss)),
            tf.summary.scalar("mean_value_loss", tf.reduce_mean(value_loss)),
            tf.summary.scalar("entropy_reg_loss", tf.reduce_mean(entropy_reg)),
            tf.summary.scalar("total_loss", tf.reduce_mean(total_loss)),
            tf.summary.scalar("global_step", self.global_step)
        ])

        self.summaries_histogram = tf.summary.merge([
            tf.summary.histogram("predicted_actions", tf.argmax(self.predict_action, axis=1)),
            tf.summary.histogram("predict_actions_probs", self.predict_action),
            tf.summary.histogram("value", self.value),
            tf.summary.histogram("cum_rewards", self.cum_reward),
            tf.summary.histogram("action_labels", self.action_labels),
            tf.summary.histogram("advantage", self.advantage)
        ])

        self.summaries_grad = tf.summary.merge([
            [tf.summary.histogram("%s-grad" % g[1].name, g[0]) for g in zip(grads, vars)],
            [tf.summary.histogram("%s-grad-clip" % g[1].name, g[0]) for g in zip(grads_clipped, vars)]
        ])

        tf_log_dir = os.path.join(utils.output_dir(self.cfg), self.cfg['logs_dir'])
        self.summary_writer = tf.summary.FileWriter(tf_log_dir, graph=tf.get_default_graph())

        self.saver = tf.train.Saver(
            max_to_keep=self.cfg['max_checkpoints_to_keep'],
            keep_checkpoint_every_n_hours=self.cfg['keep_checkpoint_every_n_hours']
        )

    def set_session(self, sess):
        self.sess = sess

    def update_params(self, states, actions, disc_cum_rewards, values, masks_valid):
        if self.total_updates % 4000 == 0:
            summaries_ops = [self.summaries_scalar, self.summaries_histogram, self.summaries_grad]
        else:
            summaries_ops = [self.summaries_scalar]

        advantages = disc_cum_rewards - values
        _, summary = self.sess.run([self.update_op, summaries_ops], feed_dict={
            self.input: states,
            self.cum_reward: disc_cum_rewards,
            self.action_labels: actions,
            self.advantage: advantages,
            self.masks_valid: masks_valid
        })
        [self.summary_writer.add_summary(x, self.total_updates) for x in summary]

        self.total_updates += 1
        if self.total_updates % self.cfg['save_model_frequency'] == 0 and self.total_updates > 1:
            dir = os.path.join(utils.output_dir(self.cfg), self.cfg['models_dir'])
            path = os.path.join(dir, 'a2c')
            self.saver.save(self.sess, path, global_step=self.global_step)

    def predict_action_probs(self, inputs):
        action_probs = self.sess.run(self.predict_action, feed_dict={self.input: inputs})
        return action_probs

    def predict_act(self, inputs):
        action_probs = self.sess.run(self.predict_action, feed_dict={self.input: inputs})
        action = np.argmax(action_probs, axis=-1)
        return action

    def predict_values(self, inputs):
        values = self.sess.run(self.value, feed_dict={
            self.input: inputs
        })

        return values

    def predict_action_probs_and_values(self, inputs):
        action_probs, values = self.sess.run(
            [self.predict_action, self.value],
            feed_dict={
                self.input: inputs
            }
        )
        return action_probs, values

    def load_weights(self, weights_path):
        self.saver.restore(self.sess, weights_path)

    def report_score(self, score):
        self.summary_writer.add_summary(tf.Summary(
            value=[tf.Summary.Value(tag="score", simple_value=score)]), self.total_updates)


##open pickle and map to images in dataset

import pickle
import os
import pandas as pd
import matplotlib.pyplot as plt
import PIL

data_set = '~/Imagery/WV2/multi/AMA/ReProj/Clipped_AMA_box/PCA/128/*/'
#data_set = '/home/bmetzler/Documents/Imagery/Accra/preprocessed/deepcluster/rgb_byte_scaled/'
#data_set = '/home/bmetzler/Documents/Imagery/Accra/preprocessed/kmeans/kmeans_4_tiles/'

def getListOfFiles(dirName):
    # create a list of file and sub directories
    # names in the given directory
    listOfFile = os.listdir(dirName)
    allFiles = list()
    # Iterate over all the entries
    for entry in listOfFile:
        # Create full path
        fullPath = os.path.join(dirName, entry)
        # If entry is a directory then get the list of files in this directory
        if os.path.isdir(fullPath):
            allFiles = allFiles + getListOfFiles(fullPath)
        else:
            allFiles.append(fullPath)

    return allFiles

def all_tif_files(dirName):
    file_list = []
    # name of folder with imagery data
    #dirName = '/path/to/data_set';

    # Get the list of all files in directory tree at given path
    listOfFiles = getListOfFiles(dirName)

    # Print the files
    #for elem in listOfFiles:
    #    print(elem)

    #print ("****************")

    # Get the list of all files in directory tree at given path
    listOfFiles = list()
    for (dirpath, dirnames, filenames) in os.walk(dirName):
        listOfFiles += [os.path.join(dirpath, file) for file in filenames]

    # Print the files
    for elem in listOfFiles:
        if elem.endswith(".tif"): #or .tif/png
            file_list.append(elem)
    return(file_list)

with open("/home/bmetzler/Documents/GitHub/deepcluster-master/exp/rgb_2000_vgg/clusters", "rb") as f:
    b = pickle.load(f)
    print (b[-1], max(b[-1]))

df = pd.DataFrame(all_tif_files(data_set), columns=['files'])
#print (df.head())

cluster_dict = dict(enumerate(b[-1]))
print (cluster_dict)
## plot 10 images of a certain cluster

###chose cluster to visualise:
cluster_number = 50
rows = 8
limit = 40
f, axarr = plt.subplots(3,3, figsize=(11,20))
f.tight_layout()

picture_list = cluster_dict[cluster_number]
print (len(picture_list))

for num, x in enumerate(picture_list):
    #print (str(df.loc[x][0]))
    if num>= limit: break
    img = PIL.Image.open(str(df.loc[x][0]))
    plt.subplot(rows,5,num+1)
    #plt.title(x.split('.')[0])
    plt.axis('off')
    plt.imshow(img)

plt.show()


# stdlib imports
import os.path
import zipfile

# third party imports
from esi_utils_io.smcontainers import ShakeMapOutputContainer
from mapio.gdal import GDALGrid
from mapio.geodict import GeoDict
from mapio.grid2d import Grid2D

# local imports
from shakelib.utils.imt_string import oq_to_file
from shakemap.coremods.base import Contents, CoreModule
from shakemap.utils.config import get_config_paths

FORMATS = {"shapefile": ("ESRI Shapefile", "shp"), "geojson": ("GeoJSON", "json")}

DEFAULT_FILTER_SIZE = 10

class RasterModule(CoreModule):
    """
    raster -- Generate GIS raster files of all IMT values from
                    shake_result.hdf.
    """

    command_name = "raster"
    targets = [r"products/raster\.zip"]
    dependencies = [("products/shake_result.hdf", True)]

    def __init__(self, eventid):
        super(RasterModule, self).__init__(eventid)
        self.contents = Contents(None, None, eventid)

    def execute(self):
        """
        Write raster.zip file containing ESRI Raster files of all the IMTs
        in shake_result.hdf.

        Raises:
            NotADirectoryError: When the event data directory does not exist.
            FileNotFoundError: When the the shake_result HDF file does not
                exist.
        """

        install_path, data_path = get_config_paths()
        datadir = os.path.join(data_path, self._eventid, "current", "products")
        if not os.path.isdir(datadir):
            raise NotADirectoryError(f"{datadir} is not a valid directory.")
        datafile = os.path.join(datadir, "shake_result.hdf")
        if not os.path.isfile(datafile):
            raise FileNotFoundError(f"{datafile} does not exist.")

        # Open the ShakeMapOutputContainer and extract the data
        container = ShakeMapOutputContainer.load(datafile)
        if container.getDataType() != "grid":
            raise NotImplementedError(
                "raster module can only operate on " "gridded data, not sets of points"
            )

        # create GIS-readable .flt files of imt and uncertainty
        self.logger.debug("Creating GIS grids...")
        layers = container.getIMTs()

        # Package up all of these files into one zip file.
        zfilename = os.path.join(datadir, "raster.zip")
        zfile = zipfile.ZipFile(zfilename, mode="w", compression=zipfile.ZIP_DEFLATED)

        files_written = []
        for layer in layers:
            _, layer = layer.split("/")
            fileimt = oq_to_file(layer)
            # This is a bit hacky -- we only produce the raster for the
            # first IMC returned. It should work as long as we only have
            # one IMC produced per ShakeMap run.
            imclist = container.getComponents(layer)
            imtdict = container.getIMTGrids(layer, imclist[0])
            mean_grid = Grid2D(imtdict["mean"], GeoDict(imtdict["mean_metadata"]))
            std_grid = Grid2D(imtdict["std"], GeoDict(imtdict["std_metadata"]))
            mean_gdal = GDALGrid.copyFromGrid(mean_grid)
            std_gdal = GDALGrid.copyFromGrid(std_grid)
            mean_fname = os.path.join(datadir, f"{fileimt}_mean.flt")
            mean_hdr = os.path.join(datadir, f"{fileimt}_mean.hdr")
            std_fname = os.path.join(datadir, f"{fileimt}_std.flt")
            std_hdr = os.path.join(datadir, f"{fileimt}_std.hdr")
            self.logger.debug(f"Saving {mean_fname}...")
            mean_gdal.save(mean_fname)
            files_written.append(mean_fname)
            files_written.append(mean_hdr)
            self.logger.debug(f"Saving {std_fname}...")
            std_gdal.save(std_fname)
            files_written.append(std_fname)
            files_written.append(std_hdr)
            zfile.write(mean_fname, f"{fileimt}_mean.flt")
            zfile.write(mean_hdr, f"{fileimt}_mean.hdr")
            zfile.write(std_fname, f"{fileimt}_std.flt")
            zfile.write(std_hdr, f"{fileimt}_std.hdr")

        zfile.close()

        # nuke all of the copies of the files we just put in the zipfile
        for file_written in files_written:
            os.remove(file_written)

        self.contents.addFile(
            "rasterData",
            "ESRI Raster Files",
            "Data and uncertainty grids in ESRI raster " "format",
            "raster.zip",
            "application/zip",
        )
        container.close()


# Макс d, при котором выведет 46

d = int(input())
n = 8
s = 78
while s <= 1200:
    s += d
    n += 2
print(n)  # 46

stdin = 1
answer_n = 46
answers = []
while stdin < 1000:
    d = stdin
    n = 8
    s = 78
    while s <= 1200:
        s += d
        n += 2
    if n == answer_n:
        answers.append(stdin)
    stdin += 1
    print(1)

print(answers)
print(max(answers))


#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
    File Name: data_set.py
    Description:

Created by YongBai on 2020/10/14 1:48 PM.
"""
import os
import numpy as np
import pandas as pd
import torch
from torch.utils.data import Dataset
import skimage
import logging

class PRSDataset(Dataset):
    def __init__(self, in_df, data_root_dir, embed_id=0):
        """

        Parameters
        ----------
        in_df
        data_root_dir
        embed_id: 0: one-hot, 1: 3-mer fixed sliding win length, 2: 4-mer fixed sliding win lenth.
        """
        self.in_df = in_df  # train_df
        self.data_root_dir = data_root_dir
        self.embed_id = embed_id

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

    def __getitem__(self, item: int):

        phage_id = self.in_df.iloc[item, 1]
        bact_id = self.in_df.iloc[item, 0]
        x_fname = self.in_df.iloc[item, 3]

        y = self.in_df.iloc[item, 6]  # label
        x = self.__get_data(x_fname, phage_id, bact_id)

        # normalization
        # https://nlp.stanford.edu/IR-book/html/htmledition/maximum-tf-normalization-1.html
        x_max = np.amax(x, axis=0)
        x_max[x_max == 0] = 0.00001
        # x_max = np.expand_dims(x_max, axis=0)
        # print(x_max)
        # print(x_max.shape)

        alpha = 0.4
        norm_x = alpha + (1.0 - alpha) * x / x_max

        return norm_x, y.astype(np.float32)

    def __get_data(self, fname, phage_id=None, bact_id=None):

        if self.embed_id == 0:
            # read one hot encoded data with original DNA length
            tmp = np.load(os.path.join(self.data_root_dir, '{}.npz'.format(fname)), allow_pickle=True)
            tmp_bact = np.transpose(tmp['bac_onehot'].tolist().toarray())  # tmp_bact.size = [5, n]
            tmp_phage = np.transpose(tmp['phage_onehot'].tolist().toarray())
            return tmp_phage.astype(np.float32), tmp_bact.astype(np.float32)

        else:
            # reading 3-mer/4-mer fixed sliding win length
            SEQ_LEN = 11000
            phage_fname = os.path.join(self.data_root_dir, 'phage_500/{}.countMatrix.csv'.format(phage_id))
            bact_fname = os.path.join(self.data_root_dir, 'bac_1000/{}.countMatrix.csv'.format(bact_id))

            phage_data = np.transpose(pd.read_csv(phage_fname, header=None).values.astype(np.float32))
            bact_data = np.transpose(pd.read_csv(bact_fname, header=None).values.astype(np.float32))
            x_phage_bact = np.concatenate((phage_data, bact_data), axis=-1)

            batch_x = np.zeros((x_phage_bact.shape[0], SEQ_LEN), dtype=np.float32)

            if x_phage_bact.shape[-1] > SEQ_LEN:
                batch_x = x_phage_bact[:, :SEQ_LEN]
            else:
                batch_x[:, :x_phage_bact.shape[-1]] = x_phage_bact

            return batch_x

def get_slide(seq_array, fix_len):

    nb_feats, total_len = seq_array.shape

    if total_len < fix_len:
        return seq_array

    # logging.info('sliding window...')
    # np.floor((L-win)/stride + 1) = phage_fix_len
    # first let win = 2 * stride
    strid = int(np.floor(total_len / (fix_len + 1)))
    win = total_len - (fix_len - 1) * strid

    slide_array = skimage.util.view_as_windows(seq_array,
                                               (nb_feats, win),
                                               step=strid).squeeze().sum(axis=-1)
    slide_array_max = np.expand_dims(slide_array.max(axis=-1), axis=1)

    # normalization
    # https://nlp.stanford.edu/IR-book/html/htmledition/maximum-tf-normalization-1.html
    alpha = 0.4
    re_slide = alpha + (1.0 - alpha) * slide_array / slide_array_max

    return np.transpose(re_slide)


from __future__ import absolute_import, division, print_function
import iotbx.pdb.xray_structure
from cctbx.development import random_structure
from cctbx import miller
from cctbx import crystal
from cctbx import sgtbx
from cctbx.array_family import flex
from libtbx.test_utils import show_diff
from six.moves import cStringIO as StringIO
import math
import sys

def exercise_basic():
  a = flex.double((10,20))
  p = flex.double((-25,355))
  c = flex.polar(a, p, True)
  f = flex.double((0.3,0.9))
  s = miller.set(
    crystal_symmetry=crystal.symmetry(
      unit_cell=(10,10,10,90,90,90),
      space_group_symbol="P1"),
    indices=flex.miller_index([(1,2,3),(-3,4,-6)]),
    anomalous_flag=False)
  out = StringIO()
  s.array(data=c).as_phases_phs(out=out)
  assert not show_diff(out.getvalue(), """\
   1   2   3 4999.99    1.00  -25.00
  -3   4  -6 9999.99    1.00   -5.00
""")
  out = StringIO()
  s.array(data=c).as_phases_phs(out=out, scale_amplitudes=False)
  assert not show_diff(out.getvalue(), """\
   1   2   3   10.00    1.00  -25.00
  -3   4  -6   20.00    1.00   -5.00
""")
  for phases in [s.array(data=p), p]:
    out = StringIO()
    s.array(data=c).amplitudes().as_phases_phs(
      out=out, phases=phases, phases_deg=True)
    assert not show_diff(out.getvalue(), """\
   1   2   3 4999.99    1.00  -25.00
  -3   4  -6 9999.99    1.00  355.00
""")
  for phases in [s.array(data=p*(math.pi/180)), p*(math.pi/180)]:
    out = StringIO()
    s.array(data=c).amplitudes().as_phases_phs(
      out=out, phases=phases, phases_deg=False)
    assert not show_diff(out.getvalue(), """\
   1   2   3 4999.99    1.00  -25.00
  -3   4  -6 9999.99    1.00  355.00
""")
  for figures_of_merit in [s.array(data=f), f]:
    out = StringIO()
    s.array(data=c).as_phases_phs(out=out, figures_of_merit=figures_of_merit)
    assert not show_diff(out.getvalue(), """\
   1   2   3 4999.99    0.30  -25.00
  -3   4  -6 9999.99    0.90   -5.00
""")

def generate_random_f_calc(
      space_group_info,
      n_elements=10,
      d_min=1.5,
      verbose=0):
  structure = random_structure.xray_structure(
    space_group_info,
    elements=["Si"]*n_elements,
    volume_per_atom=1000,
    min_distance=3.,
    general_positions_only=False)
  if (0 or verbose):
    structure.show_summary()
    structure.show_scatterers()
    print()
  print("Writing tmp.pdb")
  s = structure.as_pdb_file(
    remark="random structure",
    resname="RND")
  with open("tmp.pdb", "w") as f:
    f.write(s)
  if (0 or verbose):
    print()
  f_calc = structure.structure_factors(
    d_min=d_min, anomalous_flag=False).f_calc()
  if (0 or verbose):
    f_calc.show_summary()
    print()
  print("Writing: tmp.phs")
  with open("tmp.phs", "w") as f:
    f_calc.as_phases_phs(out=f)
  if (0 or verbose):
    print()

def run():
  exercise_basic()
  if (len(sys.argv) > 1):
    verbose = 0
    args = []
    for arg in sys.argv[1:]:
      if (arg.lower() == "--verbose"):
        verbose = 1
      else:
        args.append(arg)
    for arg in args:
      generate_random_f_calc(sgtbx.space_group_info(arg), verbose=verbose)
  print("OK")

if (__name__ == "__main__"):
  run()


# Import Dependencies
import numpy as np
import datetime as dt
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func
from flask import Flask, jsonify

#################################################
# Database Setup
#################################################
engine = create_engine("sqlite:///../Resources/hawaii.sqlite")

# reflect an existing database into a new model
Base = automap_base()

# reflect the tables
Base.prepare(autoload_with=engine)

# Save reference to the table
Measurement = Base.classes.measurement
Station = Base.classes.station

# Create session (link) from Python to the DB
session = Session(engine)

#################################################
# Flask Setup
#################################################
app = Flask(__name__)

# Create Global variables defining most recent date, date 12 months prior, and most active station
    # Determine most recent date in the data set
mosrec_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()

    # Calculate the date one year from the last date in data set.
prev_year = dt.datetime.strptime(mosrec_date[0], "%Y-%m-%d") - dt.timedelta(days=365)

    # Query to find the most active station
most_active = session.query(Measurement.station, func.count(Measurement.tobs)).\
    group_by(Measurement.station).\
    order_by(func.count(Measurement.tobs).desc()).first()

#################################################
# Flask Routes
#################################################
@app.route("/")
def welcome():
    """List all available api routes."""
    return (
        f"
Welcome to Surf's Up Weather Alchemy!
"
        f"Available Routes:
"
        f"/api/v1.0/precipitation
"
        f"(Date and precipitation amounts from the most recent 12 months of available data)

"

        f"/api/v1.0/stations
"
        f"(A list of our observation stations)

"

        f"/api/v1.0/tobs
"
        f"(Temperature observations from the most recent 12 months of available data at our most active station)

"

        f"/api/v1.0/<start>
"
        f"(Find the minimum, average and max temperature since a given start day)

"

        f"/api/v1.0/<start>/<end>
"
        f"(Find the minimum, average and max temperature for a given range of days - start to end)

"
    )

# Setup precipitation Route
@app.route("/api/v1.0/precipitation")
def precipitation():
    """Return a JSON representation of a dictionary for precipitation within the most recent 12 months"""
    # Query date and precipitation values for the most recent year of data only
    prevyear_prcp = session.query(Measurement.date, Measurement.prcp).\
    filter(Measurement.date >= prev_year).\
    order_by(Measurement.date).all()

    # Convert queried list of tuples into dictionary with date as key and prcp as value
    prevyear_precipitation = dict((x, y) for x, y in prevyear_prcp)

    return jsonify(prevyear_precipitation)

# Setup stations Route
@app.route("/api/v1.0/stations")
def stations():
    """Return a JSON list of all stations"""
    # Query all stations
    station_data = session.query(Station.name).all()

    # Convert queried list of tuples into normal list
    all_stations = list(np.ravel(station_data))

    return jsonify(all_stations)

# Setup tobs Route
@app.route("/api/v1.0/tobs")
def tobs():
    """Return tobs data for only the past year of the most active station"""
    # Query only the most active station's tobs for the most recent year of data
    tobs_data = session.query(Measurement.date, Measurement.tobs).\
    filter(Measurement.station == most_active[0]).\
    filter(Measurement.date >= prev_year).all()

    # Convert queried list of tuples into dictionary with date as key and tobs as value
    most_active_station = dict((x, y) for x, y in tobs_data)
    # most_active_station = list(np.ravel(results))

    return jsonify(most_active_station)

# Setup <start> Route
@app.route("/api/v1.0/<start>")
def start_temps(start):
    """Return a JSON list of the tmin, tavg, and tmax temperatures for the range
    beginning from the date variable supplied by the user."""
    # Query list for tmin, tavg & tmax temps
    defsel = [func.min(Measurement.tobs),
        func.avg(Measurement.tobs),
        func.max(Measurement.tobs)]

    # Query data by unpacking query list and using user provided start date
    start_range = session.query(*defsel).\
        filter(Measurement.date >= start).all()

    # Convert queried list of tuples into normal list
    start_results = list(np.ravel(start_range))

    return jsonify(start_results)

# Setup <start>/<end> Route
@app.route("/api/v1.0/<start>/<end>")
def start_end_temps(start, end):
    """Return a JSON list of the tmin, tavg, and tmax temperatures for the range
    beginning from the start date to the end date variables supplied by the user."""
    # Create Query list for tmin, tavg & tmax temps
    defsel = [func.min(Measurement.tobs),
        func.avg(Measurement.tobs),
        func.max(Measurement.tobs)]

    # Query data by unpacking query list and using user provided start and end dates
    start_end_range = session.query(*defsel).\
        filter(Measurement.date >= start).\
        filter(Measurement.date <= end).all()

    # Convert queried list of tuples into normal list
    start_end_results = list(np.ravel(start_end_range))

    return jsonify(start_end_results)

session.close()

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


import datetime
import re
import json
import os
import pathlib
from packaging.version import Version

import numpy as np
import torch
from transformers import CLIPTextModel
from diffusers import *
import safetensors.torch

import PIL

from .util import torch_device, is_torch_2_0
from .prompt import Prompt

def filter_image_size(len):
    if not isinstance(len, int) or len < 0:
        return 16
    if len % 8 != 0:
        return (len // 8 + 1) * 8
    return len

def load_safetensors_lora(pipeline, checkpoint_path, LORA_PREFIX_UNET="lora_unet", LORA_PREFIX_TEXT_ENCODER="lora_te", alpha=0.75):
    # load LoRA weight from .safetensors
    state_dict = safetensors.torch.load_file(checkpoint_path)

    visited = []

    # directly update weight in diffusers model
    for key in state_dict:
        # it is suggested to print out the key, it usually will be something like below
        # "lora_te_text_model_encoder_layers_0_self_attn_k_proj.lora_down.weight"

        # as we have set the alpha beforehand, so just skip
        if ".alpha" in key or key in visited:
            continue

        if "text" in key:
            layer_infos = key.split(".")[0].split(LORA_PREFIX_TEXT_ENCODER + "_")[-1].split("_")
            curr_layer = pipeline.text_encoder
        else:
            layer_infos = key.split(".")[0].split(LORA_PREFIX_UNET + "_")[-1].split("_")
            curr_layer = pipeline.unet

        # find the target layer
        temp_name = layer_infos.pop(0)
        while len(layer_infos) > -1:
            try:
                curr_layer = curr_layer.__getattr__(temp_name)
                if len(layer_infos) > 0:
                    temp_name = layer_infos.pop(0)
                elif len(layer_infos) == 0:
                    break
            except Exception:
                if len(temp_name) > 0:
                    temp_name += "_" + layer_infos.pop(0)
                else:
                    temp_name = layer_infos.pop(0)

        pair_keys = []
        if "lora_down" in key:
            pair_keys.append(key.replace("lora_down", "lora_up"))
            pair_keys.append(key)
        else:
            pair_keys.append(key)
            pair_keys.append(key.replace("lora_up", "lora_down"))

        # update weight
        if len(state_dict[pair_keys[0]].shape) == 4:
            weight_up = state_dict[pair_keys[0]].squeeze(3).squeeze(2).to(torch.float32)
            weight_down = state_dict[pair_keys[1]].squeeze(3).squeeze(2).to(torch.float32)
            curr_layer.weight.data += alpha * torch.mm(weight_up, weight_down).unsqueeze(2).unsqueeze(3)
        else:
            weight_up = state_dict[pair_keys[0]].to(torch.float32)
            weight_down = state_dict[pair_keys[1]].to(torch.float32)
            curr_layer.weight.data += alpha * torch.mm(weight_up, weight_down)

        # update visited list
        for item in pair_keys:
            visited.append(item)

    return pipeline

# Pipeline wrappers
class SDPipes:
    def __init__(self):
        self.model_path = None
        self.clip_skip = 0

        self.prompt = Prompt(".")
        self.negative_prompt = Prompt(".")

        # Real pipelines
        self.txt2img = None
        self.img2img = None

    def _load_model(
        self,
        state,
        path,
        dtype=torch.float16,
        clip_skip=0,
        lora_path="",
        lora_alpha=0.75,
    ):
        # Create kwargs
        kwargs = {}

        # Remove old model
        if self.txt2img is not None:
            del self.txt2img
            # Clean cuda cache
            if torch_device() == "cuda":
                torch.cuda.empty_cache()

        # Create txt2img pipeline
        print(f"[INFO] Loading pipeline from {path}")
        print(f"       kwargs = {kwargs}")
        try:
            txt2img = DiffusionPipeline.from_pretrained(
                path,
                **kwargs,
                torch_dtype=dtype,
                local_files_only=True,
            )
        except Exception as e:
            print(f"[ERROR] Cannot load model {path}: {e}")
            raise Exception(f"Cannot load model {path}, please check selected model")

        # Clip skip
        print(f"[INFO] Clip skip {clip_skip}")
        if clip_skip > 0:
            txt2img.text_encoder = CLIPTextModel.from_pretrained(
                path,
                torch_dtype=dtype,
                subfolder="text_encoder",
                num_hidden_layers=(
                    txt2img.text_encoder.config.num_hidden_layers - clip_skip
                ),
                local_files_only=True,
            )
        print(
            f"[INFO] CLIP num hidden layers = {txt2img.text_encoder.config.num_hidden_layers}"
        )

        # Disable safety checker for performance
        txt2img.safety_checker = None

        # Load Lora
        if lora_path != "":
            print(f"[INFO] Loading Lora from {lora_path}")
            load_safetensors_lora(txt2img, lora_path, alpha=lora_alpha)

        # Load Textual Inversion
        for root, dirs, files in os.walk(state.models_root):
            # Check if root is textual inversion root
            base = os.path.basename(root)
            base.lower()
            base = re.sub("[^a-z0-9]+", "", base)
            if base == "textualinversion" or base == "textualinversions":
                print(f"[INFO] Found textual inversion root {root}")
                for f in files:
                    ti_path = os.path.join(root, f)
                    if ti_path.endswith(".pt") or ti_path.endswith(".safetensors"):
                        token = pathlib.Path(ti_path).stem
                        print(
                            f"[INFO] Loading textual inversion from {path} as {token}"
                        )
                        try:
                            txt2img.load_textual_inversion(
                                ti_path,
                                token=token,
                            )
                        except Exception as e:
                            print(
                                f"[WARNING] Cannot load textual inversion {ti_path}: {e}"
                            )
                            print(f"[WARINIG] just ignore {token}")

        # Send to device
        txt2img = txt2img.to(torch_device())

        txt2img.scheduler = DPMSolverMultistepScheduler.from_config(
            txt2img.scheduler.config
        )
        txt2img.scheduler.prediction_type = "sample"
        txt2img.scheduler.use_karras_sigmas = True

        print(f"[INFO] Set-up pipeline")

        # Enable memory efficient options
        # txt2img.enable_attention_slicing(slice_size="auto")
        # txt2img.enable_vae_slicing()
        # txt2img.enable_vae_tiling()

        # Torch 2.0 Performance Tuning
        if is_torch_2_0():
            from diffusers.models.attention_processor import AttnProcessor2_0

            txt2img.unet.set_attn_processor(AttnProcessor2_0())

        if torch_device() == "cuda":
            # txt2img.unet = torch.compile(txt2img.unet)
            try:
                import xformers

                txt2img.enable_xformers_memory_efficient_attention()
            except ImportError:
                print(
                    f"[WARN] Cannot import xformers, cannot enable memory efficient attention"
                )
                pass

        img2img = StableDiffusionImg2ImgPipeline(
            vae=txt2img.vae,
            text_encoder=txt2img.text_encoder,
            tokenizer=txt2img.tokenizer,
            unet=txt2img.unet,
            scheduler=txt2img.scheduler,
            safety_checker=None,
            feature_extractor=txt2img.feature_extractor,
            requires_safety_checker=False,
        ).to(torch_device())

        # Done, update variables
        self.model_path = path
        self.clip_skip = clip_skip
        self.lora_path = lora_path
        self.lora_alpha = lora_alpha
        self.txt2img = txt2img
        self.img2img = img2img
        self.default_scheduler = txt2img.scheduler

    def _txt2img_load_model(self, state):
        print("[INFO] SDPipes: load_model")
        state.write_state("load_model", {})
        values = state.values
        lora_path = "" if len(values['model']['lora_path']) == 0 else f"{state.models_root}/{values['model']['lora_path']}"
        self._load_model(
            state,
            f"{state.models_root}/{values['model']['path']}",
            clip_skip=values["model"]["clip_skip"],
            lora_path=lora_path,
            lora_alpha=values["model"]["lora_alpha"],
        )
        return self._txt2img_update_prompt(state)

    def _txt2img_update_prompt(self, state):
        print("[INFO] SDPipes: update_prompt")
        state.write_state("update_prompt", {})
        values = state.values
        params = values["params"]
        self.prompt.update_embed(params["prompt"], self.txt2img)
        self.negative_prompt.update_embed(params["negative_prompt"], self.txt2img)
        # Prompt may changed because of random choose. put the values in state
        state.values["choosed_prompt"] = {
            "positive": self.prompt.pp_text,
            "negative": self.negative_prompt.pp_text,
        }
        return self._txt2img_generate(state)

    def _update_sampling_method(self, name):
        print(f"[INFO] SDPipes: update_sampling_method to {name}")
        new_scheduler = self.default_scheduler
        if name == "Euler":
            new_scheduler = EulerDiscreteScheduler.from_config(new_scheduler.config)
        elif name == "Euler A":
            new_scheduler = EulerAncestralDiscreteScheduler.from_config(
                new_scheduler.config
            )
        elif name == "LMS":
            new_scheduler = LMSDiscreteScheduler.from_config(new_scheduler.config)
        elif name == "Heun":
            new_scheduler = HeunDiscreteScheduler.from_config(new_scheduler.config)
        elif name == "DDIM":
            new_scheduler = DDIMScheduler.from_config(new_scheduler.config)
        elif name == "DDIM Inverse":
            new_scheduler = DDIMInverseScheduler.from_config(new_scheduler.config)
        elif name == "DDPM":
            new_scheduler = DDPMScheduler.from_config(new_scheduler.config)
        elif name == "DPM++ 2S":
            new_scheduler = DPMSolverSinglestepScheduler.from_config(
                new_scheduler.config
            )
            new_scheduler.solver_order = 2
            new_scheduler.algorithm_type = "dpmsolver++"
        elif name == "DPM++ 2M":
            new_scheduler = DPMSolverMultistepScheduler.from_config(
                new_scheduler.config
            )
            new_scheduler.solver_order = 2
            new_scheduler.algorithm_type = "dpmsolver++"
            new_scheduler.use_karras_sigmas = False
        elif name == "DPM++ 2M Karras":
            new_scheduler = DPMSolverMultistepScheduler.from_config(
                new_scheduler.config
            )
            new_scheduler.solver_order = 2
            new_scheduler.algorithm_type = "dpmsolver++"
            new_scheduler.use_karras_sigmas = True
        elif name == "DPM++ 3S":
            new_scheduler = DPMSolverSinglestepScheduler.from_config(
                new_scheduler.config
            )
            new_scheduler.solver_order = 3
            new_scheduler.algorithm_type = "dpmsolver++"
        elif name == "DPM++ 3M":
            new_scheduler = DPMSolverMultistepScheduler.from_config(
                new_scheduler.config
            )
            new_scheduler.solver_order = 3
            new_scheduler.algorithm_type = "dpmsolver++"
            new_scheduler.use_karras_sigmas = False
        elif name == "DPM++ 3M Karras":
            new_scheduler = DPMSolverMultistepScheduler.from_config(
                new_scheduler.config
            )
            new_scheduler.solver_order = 3
            new_scheduler.algorithm_type = "dpmsolver++"
            new_scheduler.use_karras_sigmas = True
        elif name == "PNDM":
            new_scheduler = PNDMScheduler.from_config(new_scheduler.config)
        elif name == "IPNDM":
            new_scheduler = IPNDMScheduler.from_config(new_scheduler.config)
        else:
            print(f"[ERROR] Unknown sampling method: {name}")
        # Change scheduler
        self.txt2img.scheduler = new_scheduler
        self.img2img.scheduler = new_scheduler

    def _txt2img_generate(self, state):
        print("[INFO] SDPipes: generate")
        values = state.values
        params = values["params"]

        state.write_state("setup_params", {})
        kwargs = {}

        # Check params and generate
        kwargs["num_inference_steps"] = params["sampling_steps"]
        kwargs["guidance_scale"] = params["cfg_scale"]

        # Put size
        w = int(params["width"])
        h = int(params["height"])
        try:
            rng = float(params["size_range"])
            if rng > 0:
                dr = float(np.random.uniform(low=-rng, high=rng))
                # Preserve area
                area = w * h
                w = int(w * (1 + dr))
                h = int(area / w)
        except:
            print("[WARNING] Invalid size range, ignore it.")
        w = filter_image_size(w)
        h = filter_image_size(h)

        kwargs["width"] = w
        kwargs["height"] = h

        state.job["values"]["fixed_size"] = {
            "w": w,
            "h": h,
        }

        # If seed is given, use it
        if params["seed"] != "":
            try:
                s = int(params["seed"])
                kwargs["generator"] = [
                    torch.Generator(device=torch_device()).manual_seed(s)
                ]
            except:
                print("[WARNING] Invalid seed ignore it.")

        # Change sampling method
        state.write_state("update_sampler", {})
        self._update_sampling_method(params["sampling_method"])

        # Generate
        state.write_state("start_generate", {})
        total_steps = int(params["sampling_steps"])

        def callback(step, timestep, latents):
            state.write_state(
                "txt2img",
                {
                    "step": int(step),
                    "total_steps": total_steps,
                },
            )

        result = self.txt2img(
            # prompt=self.prompt.text,
            # negative_prompt=self.negative_prompt.text,
            prompt_embeds=self.prompt.embeds,
            negative_prompt_embeds=self.negative_prompt.embeds,
            num_images_per_prompt=1,
            return_dict=True,
            callback=callback,
            **kwargs,
        )
        callback(total_steps, 0, None)

        # If highres,
        highres_fix = params["highres_fix"]
        if len(highres_fix) > 0:
            return self._txt2img_highres_fix(state, result.images[0])

        # Return image
        state.write_state("done", {})
        return result.images[0]

    def _txt2img_highres_fix(self, state, img):
        values = state.values
        params = values["params"]
        highres_fix = params["highres_fix"]

        for c, hf in enumerate(highres_fix):
            print(f"[INFO] SDPipes: highres_fix - {c}")
            state.write_state("setup_highres_params", {})
            # Get Size
            if "scale" in hf:
                hf_scale = float(hf["scale"])
                hf_scale = min(max(hf_scale, 0.1), 10.0)
                hf_width = int(img.width * hf_scale)
                hf_height = int(img.height * hf_scale)
            else:
                hf_width = filter_image_size(hf["width"])
                hf_height = filter_image_size(hf["height"])
            # Resize
            img = img.resize((hf_width, hf_height), PIL.Image.LANCZOS)

            kwargs = {}
            kwargs["num_inference_steps"] = params["sampling_steps"]
            kwargs["guidance_scale"] = params["cfg_scale"]
            kwargs["strength"] = hf["strength"]

            # Generation
            state.write_state("start_highres_fix", {})
            total_steps = int(params["sampling_steps"] * hf["strength"])

            def callback(step, timestep, latents):
                state.write_state(
                    "highres_fix",
                    {
                        "count": c,
                        "step": int(step),
                        "total_steps": total_steps,
                    },
                )

            result = self.img2img(
                image=img,
                # prompt=self.prompt.text,
                # negative_prompt=self.negative_prompt.text,
                prompt_embeds=self.prompt.embeds,
                negative_prompt_embeds=self.negative_prompt.embeds,
                num_images_per_prompt=1,
                callback=callback,
                **kwargs,
            )
            callback(total_steps, 0, None)
            img = result.images[0]
        state.write_state("done", {})
        return img

    def text_to_image(self, state):
        with torch.inference_mode():
            values = state.values
            params = values["params"]
            # If model changed, run from reload model
            print(f"A: {self.model_path}")
            print(f"B: {state.models_root}/{values['model']['path']}")
            lora_path = "" if len(values['model']['lora_path']) == 0 else f"{state.models_root}/{values['model']['lora_path']}"
            if (
                self.model_path != f"{state.models_root}/{values['model']['path']}"
                or self.clip_skip != values["model"]["clip_skip"]
                or self.lora_path != lora_path
                or self.lora_alpha != values['model']['lora_alpha']
            ):
                return self._txt2img_load_model(state)
            # Otherwise, run from update_prompt
            return self._txt2img_update_prompt(state)


import icalendar
import requests
from datetime import datetime
from bs4 import BeautifulSoup
import json
import re
import os
from dotenv import load_dotenv

load_dotenv()  # This reads the environment variables inside .env
AuthToken = os.getenv('AuthToken', "NOT FOUND")

# Check mandatory variable
needExit = False
if AuthToken == "NOT FOUND":
    print("[ERROR] AuthToken not found in .env, you must set it in .env file to run this programme")
    needExit = True

if needExit:
    print("Exiting ...")
    exit()

url = 'https://tc-net.insa-lyon.fr/aff/AffichageEdtPalmGroupe.jsp?promo=4&groupe=4&dateDeb=1696111200000'

allLocations = []
linkLocationMaps = {"1-Amphi huma ouest": "",
                    "1-Amphi Chappe": "",
                    "2-TD C": "",
                    "2-TD F(LS)": "",
                    "5-Projet TD B": "",
                    "3-TP Info C": "",
                    "3-TP Info B": "",
                    "1-Amphi mediatheque-Emilie du Chatelet": "",
                    "2-TD E": "",
                    "2-TD D": "",
                    "2-TD D": "",
                    "1-Amphi Chappe + Hall Chappe": "",
                    "3-TP Info E": "",
                    "3-TP Info D": "",
                    "2-TD (C-D-E)": "",
                    " NA": "",
                    "amphi": "",
                    "TP-Telecom": "",
                    "TD": ""}

matieres = ["3TC-Accueil-2023",
            "3TC-ALG-2023",
            "3TC-ANG1-2023",
            "3TC-ANG2-2023",
            "3TC-ARC-2023",
            "3TC-ASDS-2023",
            "3TC-ASTUS-2023",
            "3TC-Autonomie-2023",
            "3TC-Divers-2023",
            "3TC-DLG1-2023",
            "3TC-DLG2-2023",
            "3TC-DRE-2023",
            "3TC-ELP-2023",
            "3TC-ForumRA-2023",
            "3TC-GNS3-2023",
            "3TC-HOP-2023",
            "3TC-International-2023",
            "3TC-IP-2023",
            "3TC-MAC-2023",
            "3TC-MAS-2023",
            "3TC-MET-2023",
            "3TC-MUSIC-2023",
            "3TC-NAS-2023",
            "3TC-NRP-2023",
            "3TC-PALE-2023",
            "3TC-PARRAIN-2023",
            "3TC-PBS-2023",
            "3TC-PBS2-2023",
            "3TC-PBS3-2023",
            "3TC-PIT-2023",
            "3TC-PPC-2023",
            "3TC-PPP-2023",
            "3TC-Pres4TC-2023",
            "3TC-PRESMETIER-2023",
            "3TC-PTIR-2023",
            "3TC-Rab-2023",
            "3TC-RencontrePart-2023",
            "3TC-SDR-2023",
            "3TC-SIS-2023",
            "3TC-SNC2-2023",
            "3TC-SNC22-2023",
            "3TC-SON-2023",
            "3TC-SPO1-2023",
            "3TC-SPO2-2023",
            "3TC-Stage3TC-2023",
            "3TC-THEATRE-2023",
            "3TC-THEATRE1-2023",
            "3TC-TSA-2023",
            "3TC-WEB-2023",
            "3TC-WEI-2023",
            "3TCA-ANG2-2023",
            "3TCA-AppDebrief1-2023",
            "3TCA-AppDebrief2-2023",
            "3TCA-AppDebrief3-2023",
            "3TCA-AppDivers-2023",
            "3TCA-AppSKI-2023",
            "3TCA-AppSOU1-2023",
            "3TCA-AppSoutenance1-2023",
            "3TCA-AppTutorat1-2023",
            "3TCA-DEBRIEF2-2023",
            "3TCA-SPO-2023",
            "4TC-ACCROBRANCHE-2023",
            "4TC-Accueil-2023",
            "4TC-ANG1-2023",
            "4TC-ANG2-2023",
            "4TC-APPDEBRIEF5-2023",
            "4TC-APPDEBRIEF6-2023",
            "4TC-ARM-2023",
            "4TC-ASTUS-2023",
            "4TC-Autonomie-2023",
            "4TC-BLF-2023",
            "4TC-CME-2023",
            "4TC-CNA-2023",
            "4TC-CSC-2023",
            "4TC-Divers-2023",
            "4TC-DLG1-2023",
            "4TC-DLG2-2023",
            "4TC-ExamComp-2023",
            "4TC-ForumRA-2023",
            "4TC-IAT-2023",
            "4TC-INR-2023",
            "4TC-INS1-2023",
            "4TC-INS2-2023",
            "4TC-International-2023",
            "4TC-JEF-2023",
            "4TC-JOURNÉEANCIENS-2023",
            "4TC-PAO-2023",
            "4TC-PPP2-2023",
            "4TC-PRESENTATION5TC-2023",
            "4TC-PRESMETIER-2023",
            "4TC-PresPPH-2023",
            "4TC-PRF-2023",
            "4TC-PSC-2023",
            "4TC-Rab-2023",
            "4TC-RAN-2023",
            "4TC-RencontresPart-2023",
            "4TC-RetourSTA-2023",
            "4TC-RPE-2023",
            "4TC-SimuEnt-2023",
            "4TC-SIR-2023",
            "4TC-SPO1-2023",
            "4TC-SPO2-2023",
            "4TC-SPOC-2023",
            "4TC-SYD-2023",
            "4TC-TCP-2023",
            "4TC-TIP-2023",
            "4TC-TOS-2023",
            "4TC-VIR-2023",
            "4TC-WEI-2023",
            "4TCA-AppANG3-2023",
            "4TCA-AppANG4-2023",
            "4TCA-AppAutonomie-2023",
            "4TCA-AppDebrief4-2023",
            "4TCA-AppDebrief5-2023",
            "4TCA-AppDebrief6-2023",
            "5TC-10ELK-2023",
            "5TC-10PROJreb-2023",
            "5TC-11BTC-2023",
            "5TC-11PRJSAT-2023",
            "5TC-1ARC-2023",
            "5TC-2AWS-2023",
            "5TC-3SRS-2023",
            "5TC-4DMO-2023",
            "5TC-5CIT-2023",
            "5TC-5IAR-2023",
            "5TC-6SMR-2023",
            "5TC-6SPR-2023",
            "5TC-7SAT-2023",
            "5TC-7SVP-2023",
            "5TC-8AUD-2023",
            "5TC-8CDN-2023",
            "5TC-9CQN-2023",
            "5TC-9SAR-2023",
            "5TC-ACCUEIL-2023",
            "5TC-ASTUS-2023",
            "5TC-HUMA-2023",
            "5TC-HUMA2-2023",
            "5TC-PILS-2023",
            "5TC-PPH-2023",
            "5TC-PPP-2023",
            "5TC-Rab-2023",
            "5TC-RET-STA-2023",
            "5TC-SHE-2023",
            "5TC-SIMU-2023",
            "5TC-SOUT-STA-2023",
            "5TC-SPO-2023",
            "IST-ASM",
            "IST-DBM1",
            "IST-DBM2",
            "IST-JAV",
            "IST-MID",
            "IST-NET1",
            "IST-NET2",
            "IST-OPS",
            "IST-SIP1",
            "IST-SIP2",
            "IST-TLRS",
            "IST-WCB",
            "MSTC-ANPM",
            "MSTC-AOS",
            "MSTC-APKI",
            "MSTC-ART",
            "MSTC-ASRF",
            "MSTC-CISO",
            "MSTC-CLSE",
            "MSTC-CONF",
            "MSTC-CYSI",
            "MSTC-GOSE",
            "MSTC-IAM",
            "MSTC-ICR",
            "MSTC-INTR",
            "MSTC-MCYR",
            "MSTC-MNSR",
            "MSTC-PENT",
            "MSTC-PPC",
            "MSTC-RGPD",
            "MSTC-RISE",
            "MSTC-SAW",
            "MSTC-SDD",
            "MSTC-SECP",
            "MSTC-SINF",
            "MSTC-SOUTENANCE",
            "MSTC-SPT",
            "MSTC-VSRA",
            "RESASALLE"]

def getCourBymatiere(matiere):
    url = 'https://tc-net.insa-lyon.fr/edt/std/AffichageEdtTexteMatiere.jsp'
    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'Accept-Language': 'fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7',
        'Authorization': f"Basic {AuthToken}",
        'Cache-Control': 'max-age=0',
        'Connection': 'keep-alive',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Cookie': 'JSESSIONID=D49EC627435268DCA32F4A3DBD42EC31; AGIMUS=TRACE-878779-qgBamGsmb1RcveoJz3KUlfeTos59PfwYecn9zYKcHJXm3Vfujb-dsi69',
        'DNT': '1',
        'Origin': 'https://tc-net.insa-lyon.fr',
        'Referer': 'https://tc-net.insa-lyon.fr/edt/std/AffichageEdtTexteMatiere.jsp',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-User': '?1',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Mobile Safari/537.36',
        'sec-ch-ua': '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
        'sec-ch-ua-mobile': '?1',
        'sec-ch-ua-platform': '"Android"'
    }

    def data(matiere: str):
        return {
            'choixMatiere': f"{matiere}",
            'SelectionMatiere': 'ok'
        }

    try:
        response = requests.post(url, headers=headers, data=data(matiere=matiere))
    except:
        print(f"Error in requesting {url} for getting {matiere}")
        print(f"Exiting ...")
        exit()
    soup = BeautifulSoup(response.text, 'html.parser')

    print(response)

    # Trouvez la structure de la matière
    # structure_matiere = soup.find('b').text.strip()

    # Trouvez toutes les lignes de données dans la balise <pre>
    data_rows = soup.find_all('pre')

    if data_rows:
        # Enlever la première et la dernière ligne
        ligne = str(data_rows[0]).strip().split('\n')[1:-1]

        # Parser le texte
        cours = []
        for l in ligne:
            # print(l)
            elements = l.split()
            elements2 = l.split("  ")
            Enseignant = ""
            for e in elements2:
                if "[" in e and "]" in e:
                    Enseignant = e

            try:
                autre = re.findall(r'\{(.*?)\}', l)[0]
            except:
                autre = ""

            try:
                salle = str(re.findall(r'\]  (.*?)\ {', l)[0])
            except:
                salle = ""

            if salle == "000000000":
                salle = "Pas de salle (000000000)"
            cours.append({
                "Matiere": matiere,
                # Enlève la virgule à la fin de la semaine
                "Semaine": elements[1][:-1],
                "Date": elements[3],
                "Heure": elements[4],
                "Groupe": elements[5],
                "Type": elements[6],
                "Enseignant": Enseignant,
                "Salle": salle,
                "Autres": autre,
            })
        return cours

cours = []
cours_tries = []

# print(getCourBymatiere("4TC-INS1-2023"))
# exit()
for matiere in matieres:
    if "4TC" in matiere and matiere != "4TC-SIR-2023":
        print(f"Crawling : {matiere}")
        listCour = getCourBymatiere(matiere)
        for cour in listCour:
            cours.append(cour)

cours_tries = sorted(
    cours, key=lambda x: datetime.strptime(x['Date'], "%d/%m/%Y"))

# with open('data.json', 'r') as fp:
#     cours_tries = json.load(fp)

print(cours_tries)

enseignants = []
for cour in cours_tries:
    if cour['Enseignant'] not in enseignants:
        enseignants.append(cour['Enseignant'])

print(enseignants)

Salles = []
for cour in cours_tries:
    if cour['Salle'] not in Salles:
        Salles.append(cour['Salle'])

print(Salles)

with open('data.json', 'w') as fp:
    json.dump(cours_tries, fp)


# coding: utf-8
import asyncio
import logging

from wings_sanic.app import WingsSanic
from wings_sanic.blueprints import WingsBluePrint
from wings_sanic.serializers import *
from wings_sanic import settings

logger = logging.getLogger('wings_sanic')

class registry:
    def __init__(self):
        self._data = {}

    def set(self, k, v, group=None):
        if not group:
            group = '__default'
        if group not in self._data:
            self._data[group] = {}
        self._data[group][k] = v

    def get(self, k, group=None):
        if not group:
            group = '__default'
        val = self._data.get(group, {}).get(k, None)
        return val

registry = registry()

class inspector:
    """ 巡查员，定期巡查任务并执行
    """

    def __init__(self):
        self._count = 0  # 次数
        self._interval = 1  # 间隔(秒)
        self.tasks = []

    def start(self):
        """ 启动
        """
        loop = registry.get('event_loop') or asyncio.get_event_loop()
        report_interval = settings.get('INSPECTOR_REPORT_INTERVAL')
        if self._count % report_interval == 0:
            logger.info('inspector working, count: %s', self._count)
        loop.call_later(self._interval, self.start)

        self._count += 1
        if self._count > 9999999:
            self._count = 1

        for task in self.tasks:
            func = task['func']
            args = task['args']
            if self._count % task['interval'] != 0 or task['times'] == 0:
                continue
            if asyncio.iscoroutinefunction(func):
                loop.create_task(func(*args))
            else:
                loop.call_soon(func, *args)

            if task['times'] > 0:
                task['times'] -= 1

    def register(self, func, *args, interval: int = 3, times: int = -1):
        """ 注册一个任务, 并制定间隔时间执行
        :param func 执行的函数
        :param interval 间隔时间, 单位秒
        :param  times 重复执行次数, <0时一直重复, =0时不重复
        """
        assert isinstance(interval, int), 'interval should be integer'
        assert isinstance(times, int), 'times should be integer'

        t = {
            'func': func,
            'args': args,
            'interval': interval,
            'times': times
        }
        self.tasks.append(t)

inspector = inspector()


from typing import Dict, List, Tuple
from pytest_benchmark_example.model import (
    EmbeddingConfig,
    NamedTensor,
    SimpleRegressionModelData,
    SimpleRegressionModel,
)
import torch
import pytest

def generate_data(
    categorical_data_info: List[EmbeddingConfig],
    numerical_data_info: List[str],
    n_rows: int,
) -> SimpleRegressionModelData:

    _categorical_data = []
    _categorical_data_columns = []
    for embedding_cfg in categorical_data_info:
        _categorical_data.append(
            torch.randint(0, embedding_cfg.cardinality - 1, (n_rows, 1))
        )
        _categorical_data_columns.append(embedding_cfg.name)

    _numerical_data = torch.randn((n_rows, len(numerical_data_info)))

    return SimpleRegressionModelData(
        numericals=NamedTensor(tuple(numerical_data_info), _numerical_data),
        categoricals=NamedTensor(
            tuple(_categorical_data_columns), torch.cat(_categorical_data, dim=1)
        ),
    )

@pytest.fixture
def categorical_data_info() -> List[EmbeddingConfig]:
    return [
        EmbeddingConfig("feature_a", 10, 2),
        EmbeddingConfig("feature_b", 100, 10),
        EmbeddingConfig("feature_c", 123, 8),
        EmbeddingConfig("feature_d", 250, 12),
    ]

@pytest.fixture
def numerical_data_info() -> List[str]:
    return ["feature_e", "feature_f"]

def setup_model_and_data(
    categorical_data_info: List[EmbeddingConfig],
    numerical_data_info: List[str],
    n_hidden_layers: int,
    batch_size: int,
) -> Tuple[SimpleRegressionModel, SimpleRegressionModelData]:
    data = generate_data(categorical_data_info, numerical_data_info, batch_size)

    hidden_layers = [2**n for n in range(1, n_hidden_layers)]
    hidden_layers.reverse()
    dummy_model = SimpleRegressionModel(
        embeddings=categorical_data_info,
        hidden_layers=hidden_layers,
        numerical_cols=numerical_data_info,
    )

    return dummy_model, data

def test_benchmark_model_batch_1(
    benchmark,
    categorical_data_info: List[EmbeddingConfig],
    numerical_data_info: List[str],
) -> None:
    model, data = setup_model_and_data(categorical_data_info, numerical_data_info, 4, 1)

    model.eval()
    with torch.no_grad():
        benchmark(model.forward, data)

@pytest.mark.parametrize(
    "n_hidden_layers",
    [
        pytest.param(8, id="n_hidden_layers=8"),
        pytest.param(4, id="n_hidden_layers=4"),
    ],
)
@pytest.mark.parametrize("batch_size", [1, 8, 16, 32, 64, 128, 256, 512, 1024])
def test_benchmark_model_parametrized(
    benchmark,
    batch_size: int,
    n_hidden_layers: int,
    categorical_data_info: List[EmbeddingConfig],
    numerical_data_info: List[str],
) -> None:

    model, data = setup_model_and_data(
        categorical_data_info, numerical_data_info, n_hidden_layers, batch_size
    )

    model.eval()
    with torch.no_grad():
        benchmark(model.forward, data)


from collections import deque

def solution(queue1, queue2):
  answer = 0

  queue1 = deque(queue1)
  queue2 = deque(queue2)

  sum_q1 = sum(queue1)
  sum_q2 = sum(queue2)

  max_cnt = len(queue1) * 3

  while (queue1 and queue2) and max_cnt != answer:
    if sum_q1 == sum_q2:
      return answer
    elif sum_q1 > sum_q2:
      tmp = queue1.popleft()
      queue2.append(tmp)
      sum_q1 -= tmp
      sum_q2 += tmp
    elif sum_q1 < sum_q2:
      tmp = queue2.popleft()
      queue1.append(tmp)
      sum_q1 += tmp
      sum_q2 -= tmp
    answer += 1

  return -1

queue1 = [1, 2, 1, 2]
queue2 = [1, 10, 1, 2]
# expect : 7

print(solution(queue1, queue2))

Random/Programmers/Kakao_2022_tech/두 큐 합 같게 만들기/sungsik_queue.py


#一个循环队列而已

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

#很简单 nimabi

l=[i for i in range(1,n+1)]

cur=0
while len(l)!=1:
    cur+=k-1
    if cur>=len(l) and len(l)>=k:
        cur=cur%(len(l))
    elif  len(l)<k:
        cur=k%len(l)

    print(l,l[cur],cur)
    l.pop(cur)
print(l)

第九届蓝桥杯大赛决赛/约瑟夫.py


# -*- coding: utf-8 -*-
"""
Created on Thu Feb 17 15:13:04 2022

@author: KAIZEN
"""

import tkinter as tk
from tkinter import ttk

from models.database import db

class Filter(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.pack(side='left', fill='y')
        self.holder_frame = tk.Frame(self, bg='white') # wraps around session and subject frames to keep them aligned
        self.holder_frame.pack(side='left', fill='y', pady=26, padx=15)

        # Create class dropdown menu as attribute of holder frame.
        # Initiate class_list variable
        self.class_list = ["Select class"]
        # Get list of classes from database
        db_fetch_class_list = [class_pick[1] for class_pick in db.fetch_class()]
        # Concatenate both lists
        self.class_list += db_fetch_class_list
        # Create dropdown menu of classes
        self.class_dropdown_menu = ttk.Combobox(self.holder_frame, value=self.class_list, width=30, state='readonly')
        self.class_dropdown_menu.current(0)
        self.class_dropdown_menu.grid(row=0, column=0, pady=(5,20), padx=10)

        # Create subject dropdown menu as attribute of holder frame.
        self.subject_dropdown_list = ["Select subject"]
        # Get list of subjects from database
        db_fetch_subject_list = [subject[1] for subject in db.fetch_subject()]
        # Concatenate both lists
        self.subject_dropdown_list += db_fetch_subject_list
        # Create dropdown menu of subjects
        self.subject_dropdown_menu = ttk.Combobox(self.holder_frame, value=self.subject_dropdown_list, width=30, state='readonly')
        self.subject_dropdown_menu.grid(row=1, column=0, pady=(100,20), padx=10) # labeled as self so I can destroy in Chart class when filter is by class
        self.subject_dropdown_menu.current(0)

# Create a button which has hover effect and changes colour to indicate it has been clicked.
class CustomButton(tk.Button):
    def __init__(self, parent, text, width, height, fontsize, command):
        tk.Button.__init__(self, parent, width=width, height=height, text=text,
                                           font=('Calibri', fontsize, 'bold'),
                                           fg='#076cf7',
                                           bg='#f3f3f4',
                                           border=0,
                                           activeforeground='#f3f3f4',
                                           activebackground='#54aeff',
                                           command=command
                                           )
        #self.place(x=20, y=20)
        self.parent = parent
        # Bind button to enter event
        self.bind('<Enter>', lambda e: self.on_enter('#54aeff', '#f1f1f1'))
        # Bind button to leave event
        self.bind('<Leave>', lambda e: self.on_leave('#076cf7', '#f3f3f4'))
        # Bind button to click event
        #self.bind('<ButtonRelease-1>', lambda e: self.on_click('white', '#076cf7'))

    def on_enter(self, bcolor, fcolor):
        # Changes button's colour when mouse enters the button
        if self['background']=='#076cf7': # To check if button has been clicked. Here, a clicked button does not change
            return
        else:
            self['background']=bcolor
            self['foreground']=fcolor

    def on_leave(self, bcolor, fcolor):
        # Changes button's colour when mouse leaves the button
        if self['background']=='#076cf7': # To check if button has been clicked. Here, a clicked button does not change
            return
        else:
            self['background']=fcolor
            self['foreground']=bcolor

# Create a blue button.
class BlueButton(tk.Button):
    def __init__(self, parent, text, width, height, fontsize, command):
        tk.Button.__init__(self, parent, width=width, height=height, text=text,
                                           font=('Calibri', fontsize, 'bold'),
                                           fg='#f1f1f1',
                                           bg='#54aeff',
                                           border=0,
                                           activeforeground='#f3f3f4',
                                           activebackground='#54aeff',
                                           command=command)


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

# In[2]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')

# In[3]:

df=pd.read_csv('loan_data.csv')

# In[4]:

df.head()

# In[5]:

fig=sns.pairplot(df)

# In[9]:

fig.savefig('pairplots2.jpg')

# In[12]:

df['purpose'].unique()

# In[15]:

df

# In[21]:

fig=df[df['credit.policy']==1]['fico'].hist(label='Credit_policy=1',alpha=0.6)
fig=df[df['credit.policy']==0]['fico'].hist(label='Credit_policy=0',alpha=0.6)
plt.legend()

# In[22]:

fig=df[df['not.fully.paid']==1]['fico'].hist(label='Not Fully Paid=1',alpha=0.6)
fig=df[df['not.fully.paid']==0]['fico'].hist(label='Not Fully Paid=0',alpha=0.6)
plt.legend()

# In[28]:

plt.figure(figsize=(11,8))
fig=sns.countplot(x='purpose',data=df,hue='not.fully.paid')

# In[29]:

sns.jointplot(x='fico',y='int.rate',data=df)

# In[32]:

#creating dummies for catagorical data i.e purpose

# In[33]:

cf=['purpose']

# In[34]:

final_data=pd.get_dummies(df,columns=cf,drop_first=True)

# In[35]:

final_data.head()

# In[36]:

#train test split the data

# In[39]:

x=final_data.drop('not.fully.paid',axis=1)
y=final_data['not.fully.paid']

# In[40]:

from sklearn.model_selection import train_test_split

# In[41]:

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.5)

# In[42]:

#set the model using decision tree

# In[43]:

from sklearn.tree import DecisionTreeClassifier

# In[44]:

dtc=DecisionTreeClassifier()

# In[45]:

dtc.fit(x_train,y_train)

# In[46]:

pred1=dtc.predict(x_test)

# In[47]:

pred1

# In[48]:

#evaluate data

# In[49]:

from sklearn.metrics import confusion_matrix,classification_report

# In[53]:

print(confusion_matrix(y_test,pred1))
print()
print(classification_report(y_test,pred1))

# In[55]:

sns.lineplot(y_test,pred1)

# In[52]:

#now using random forest with n_estimator as 500

# In[56]:

from sklearn.ensemble import RandomForestClassifier

# In[63]:

rfc=RandomForestClassifier(n_estimators=500)

# In[64]:

rfc.fit(x_train,y_train)

# In[65]:

pred2=rfc.predict(x_test)

# In[66]:

pred2

# In[61]:

#evaluate data

# In[67]:

print(confusion_matrix(y_test,pred2))
print()
print(classification_report(y_test,pred2))

# In[68]:

sns.lineplot(y_test,pred2)

# In[69]:

#comparison

# In[74]:

sns.lineplot(pred1,pred2)

# In[ ]:

#the end


class FirstClass:

    def set_data(self, value):
        self.data = value

    def display(self):
        print(self.data)

if __name__ == '__main__':
    x = FirstClass()
    y = FirstClass()

    x.set_data(5000)
    x.display()
    x.data = 'Hi'
    x.display()
    x.another_name = 'something'


"""
Interaction diagram item.
"""

from gaphor import UML
from gaphor.diagram.presentation import ElementPresentation, Named
from gaphor.diagram.shapes import Box, EditableText, Text, draw_highlight
from gaphor.diagram.support import represents
from gaphor.diagram.text import TextAlign, VerticalAlign
from gaphor.UML.modelfactory import stereotypes_str

@represents(UML.Interaction)
class InteractionItem(ElementPresentation, Named):
    def __init__(self, id=None, model=None):
        super().__init__(id, model)

        self.shape = Box(
            Box(
                Text(
                    text=lambda: stereotypes_str(self.subject),
                    style={
                        "min-width": 0,
                        "min-height": 0,
                        "text-align": TextAlign.LEFT,
                    },
                ),
                EditableText(
                    text=lambda: self.subject.name or "",
                    style={"text-align": TextAlign.LEFT},
                ),
                style={"padding": (4, 4, 4, 4)},
            ),
            style={
                "min-width": 150,
                "min-height": 100,
                "vertical-align": VerticalAlign.TOP,
            },
            draw=draw_interaction,
        )

        self.watch("subject[NamedElement].name")
        self.watch("subject.appliedStereotype.classifier.name")

def draw_interaction(box, context, bounding_box):
    cr = context.cairo
    cr.rectangle(0, 0, bounding_box.width, bounding_box.height)
    draw_highlight(context)
    # draw pentagon
    w, h = box.sizes[0]
    h2 = h / 2.0
    cr.move_to(0, h)
    cr.line_to(w - 4, h)
    cr.line_to(w, h2)
    cr.line_to(w, 0)
    cr.stroke()


#importa modulos
from tkinter import *
import random
import playsound

def escolha_dificuldade():
    Label(interface_dificuldade,text='escolha a sua dificuldade abaixo',font=('Arial',12),fg='black').pack()
    #
    Button(interface_dificuldade,text='facil-10 errros',font=('Arial',12),fg='black',
           command=escolha_dificuldade_facil).pack()
    Button(interface_dificuldade,text='Meio-10 errros',font=('Arial',12),fg='black',
           command=escolha_dificuldade_medio).pack()
    Button(interface_dificuldade,text='Dificil-10 errros',font=('Arial',12),fg='black',
           command=escolha_dificuldade_dificil).pack()

    #
    def escolha_dificuldade_facil():
        dificuldade.append(10)
        interface_dificuldade.destroy()

    def escolha_dificuldade_medio():
        dificuldade.append(8)
        interface_dificuldade.destroy()

    def escolha_dificuldade_dificil():
        dificuldade.append(6)
        interface_dificuldade.destroy()

    def forca(event):
        cabeca_olhos_nariz = interface_forca.create_oval #cria uma forma oval
        corpo = interface_forca.create_line #cra linha
        boca = interface_forca.create_arc #arco
        try:
            char =crate.get().upper()[0]
        except IndexError:
            pass
        else:
            try:
                int(char)#o q e letra
            except ValueError:
                if char not in letras_escolhidas:
                    letras_escolhidas.append(char)
                    for indice in range(len(letras)):
                        if char == letras [indice]:
                            lista_traco[indice] = letras[indice]
                            caractere_vazio['text'] = lista_traco
                            lista.conferencia.appnd(char)
                    if char not in letras:
                        lista.erro.append(char)
                        caracteres_anteriores['text'] = lista_erro
    entrada_dados.set('')
    if len(lista_conferencial) == len(letras):
        mensagem_final['text'] = 'jogo ganho parabens'
        mensagem_final['fg'] = 'green'
        caracter.destroy()
        button(interface,text='finalizar',font=('Arial',12),fg='red',command=quit).pack()
    if len(lista_conferencial) == len(letras):
        mensagem_final['text'] = 'erros maximos'
        mensagem_final['fg'] = 'red'
        caracter.destroy()
        button(interface,text='finalizar',font=('Arial',12),fg='red',command=quit).pack()

     #bonequinho
    if len(lista_erro) == 1: #cabeçç
        cabeca_olhos_nariz(165,95,215,140,fill='gray',outline='black')
    if len(lista_erro) == 2:#corpo
        corpo(190,140,190,235)
    if len (lista_erro) == 3:#braco
        corpo(190,140,130,190)
    if len (lista_erro) == 4:#braco2
        corpo(190,140,250,190)
    if len (lista_erro) == 5:#perna
        corpo(190,235,125,300)
    if len (lista_erro) == 6:#perna2
        corpo(190,140,250,190)
    if len (lista_erro) == 7:#olho
        cabeca_olhos_nariz(175,105,185,115,fill='white',outline='black')
    if len (lista_erro) == 8:#olho2
        cabeca_olhos_nariz(195,105,205,115,fill='white',outline='black')
    if len (lista_erro) == 9:#nariz
        cabeca_olhos_nariz(187.5,117.5,192.5,122.5,fill='white',outline='black')
    if len (lista_erro) == 7:#olho
        bca(165,125,205,130,fill='white')

def quit(): #finaliza
    interface.destroy()

playsound.playsound('musica_fundo.mp3',block = False)

#leitura
with open('palavras.txt') as arq:
    leitura = arq.readlines()
    palavra = random.choice(leitura).split('\n')[0].upper()
    #dcdcdcddc
    #
   #

#
letras = []
lista_traco= []
lista_erro = []
ltras_escolhidas =[]
lista_conferencial = []
dificuldade =[]

#armz dados na lstas
for indice in range(len(palavra)):
    letras.append(palavra[indice])
    lista_traco.append(' ___ ')

interface_dificuldade = Tk()
escolha_dificuldade()
interface_dificuldade.mainloop()

if len(dificuldade) == 1:
    interface = Tk
    #canvas
    interface_titulo=Canvas(interface)
    interface_titulo.pack(side=TOP)
    interface_forca = Canvas(interface,width=400,height=400)
    interface_forca.pack(side=TOP)
    interface_texto = Canvas(interface,width=400,height=400)
    interface_forca.pack(side=TOP)

    #
    #

    interface_forca.create_rectangle(10,400,400,390,fill='yellow')
    #
    interface_forca.create_rectangle(10,400,20,30,fill='yellow')
    interface_forca.create_rectangle(10,400,400,40,fill='yellow')
    interface_forca.create_rectangle(180,40,200,50,fill='red')
    interface_forca.create_rectangle(187.5,50,192.5,90,fill='black')
    interface_forca.create_oval(160,90,220,145,fill='black')
    interface_forca.create_oval(165,95,215,140,fill='white')

    #
    Label(interface_titulo,text='bem vindo ao jogo da orca',font=('arial',12),fg='black').pack()
    Label(interface_titulo,text= 'digite a sua letra;\n',font=('arial',12),fg='black').pack()

    entrada_dados = StringVar()
    caracter = Entry(interface_texto,textvariable=entrada_dados)
    #
    #
    caracter.pack()
    caracter.bind('<return>',forca)

#
    caracter_vazio = Label(interface_texto,text=traco)
    caracter_vazio.pack()

    #
    Label(interface_texto,text='letras erradas:').pack()
    caracteres_anteriores = Label(interface_texto,text=lista_traco)
    caracteres_anteriores.pack()

    #
    mensagem_final = Label(interface_texto,text='')
    mensagem_final.pack()
    interface.mainloop()


from kubernetes import client

def template(context):
    name = context["name"]
    labels={"name": name}

    ports = context["ports"]
    for port in ports:
        port.update(name="tcp-{}-{}".format(port["port"],port["targetPort"]))

    type = "NodePort" if ports[0].get("nodePort") else "ClusterIP"

    return client.V1Service(
        api_version="v1",
        kind="Service",
        metadata=client.V1ObjectMeta(name = name,labels = labels),
        spec=client.V1ServiceSpec(
            type=type,
            ports=ports,
            selector={"name": name},
        ),
    )


import pickle
import pandas as pd
import sklearn

class LoadModel:
    #Loading the model
    def __init__(self, MODEL_PATH):
        self.loaded_model=pickle.load(open(MODEL_PATH, 'rb'))

    def predict_class(self,pregnant,insulin,bmi,age,glucose,bp,pedigree):
        #initiatlise list of list
        data = [[pregnant, insulin, bmi, age, glucose, bp, pedigree]]

        #create the pandas dataframe

        df = pd.DataFrame(data, columns= ['pregnant','insulin','bmi','age','glucose','bp','pedigree'])
        new_pred = self.loaded_model.predict(df)
        return new_pred

    # Test Model

if __name__ == '__main__':
    MODEL_PATH = "/Users/Naresh_Prajapati/Documents/2021/diabetesprediction/models/logistic_reg.sav"
    model = LoadModel(MODEL_PATH)
    predicted_class = model.predict_class(6,0,33.6,50,148,72,0.627)
    print(predicted_class)


import scrapy
import os
import hashlib
import pymongo
import mimetypes
import pandas as pd
from Imagecapture.items import ImageItem
from scrapy.utils.python import to_bytes

class ExampleSpider(scrapy.Spider):
    name = 'example'

    input_urls_directory = os.path.abspath("./input/urls") + '/'
    start_urls = []
    col_list = ["category", "layer", "indomain", "urls"]
    relative_output_images_path = ''
    mongodb_uri = ''
    mongodb_db = ''
    collection = 'images'

    def __init__(self, *args, **kwargs):
        super(ExampleSpider, self).__init__(*args, **kwargs)
        self.input_urls_directory += self.input
        self.relative_output_images_path = '/cat' + str(self.category) + '/' + self.parseLayer(self.layer) + '/'

        self.mongodb_uri = self.MONGODB_URI
        self.mongodb_db = self.MONGODB_DATABASE

        # Read a URL file
        df = pd.read_csv(self.input_urls_directory, usecols = self.col_list, encoding= 'unicode_escape')
        self.start_urls = df["urls"].tolist()

        # Connect and open MongoDB
        self.client = pymongo.MongoClient(self.mongodb_uri)
        self.db = self.client[self.mongodb_db]

    def start_request(self):
        for url in self.start_urls:
            yield Request(url = url, callback = self.parse)

    def parse(self, response):
        domain = self.getDomain(response.url)

        if response.status == 200:
            rel_img_urls = response.xpath("//img/@src").getall()

            for rel_img_url in rel_img_urls:
                complet_url = response.urljoin(rel_img_url)

                if rel_img_url.startswith('http') or rel_img_url.startswith('https'):
                    target_domain = self.getDomain(complet_url)
                # If the url is DATA URI, consider it is in domain
                else:
                    target_domain = domain

                # Get image hash name
                # Reference from source code: https://docs.scrapy.org/en/latest/_modules/scrapy/pipelines/files.html#FilesPipeline.file_path
                image_hash_name = hashlib.sha1(to_bytes(complet_url)).hexdigest()
                image_ext = os.path.splitext(complet_url)[1]

                if image_ext not in mimetypes.types_map:
                    image_ext = ''
                    media_type = mimetypes.guess_type(complet_url)[0]
                    if media_type:
                        image_ext = mimetypes.guess_extension(media_type)

                image_name = image_hash_name + image_ext

                item = ImageItem()
                item['image_urls'] = complet_url
                item['layer'] = self.layer
                item['category'] = self.category
                item['relative_path'] = self.relative_output_images_path

                # Compare the target url is in the same domain with the request url
                if domain == target_domain:
                    item['indomain'] = 1
                else:
                    item['indomain'] = 0

                item['image_name'] = image_name

                data = dict(item)

                try:
                    # insert into new collection
                    self.db[self.collection].insert_one(data)
                except pymongo.errors.DuplicateKeyError:
                    # skip document because it already exists in new collection
                    pass
                except:
                    raise
                else:
                    yield item

    def close_spider(self, spider):
        self.client.close()

    def getDomain(self, url):
        request_url = url.split('/')
        print(request_url)
        domain = request_url[2]
        return domain

    def parseLayer(self, pattern):
        if pattern == '0':
            return "root"
        elif pattern ==  '1':
            return "first"
        elif pattern ==  '2':
            return "second"
        else:
            return "third"


# 3009

x1, y1 = input().split(' ')
x1 = int(x1)
y1 = int(y1)
x2, y2 = input().split(' ')
x2 = int(x2)
y2 = int(y2)
x3, y3 = input().split(' ')
x3 = int(x3)
y3 = int(y3)

def checkP(x1, x2, x3):
    if x1 == x2:
        return x3
    elif x1 == x3:
        return x2
    elif x2 == x3:
        return x1

print(str(checkP(x1, x2, x3))+' '+str(checkP(y1, y2, y3)))


import torch.nn as nn
import torchvision.transforms as transforms
import torch
from dataset import CloudDataset
from model import UNet
from tqdm import tqdm
import torch.optim as optim
from torch.utils.data import DataLoader
from utils import (
    load_checkpoint,
    save_checkpoint,
    check_accuracy,
    save_predictions_as_imgs,
)
from DiceLoss import DiceLoss
import matplotlib.pyplot as plt
import os

# Hyperparameters
_LEARNING_RATE = 0.001
_DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
_LOSS = 'dice'
_BATCH_SIZE = 16
_NUM_EPOCHS = 30000
_NUM_WORKERS = 2
_IMAGE_HEIGHT = 224
_IMAGE_WIDTH = 224
_LOAD_MODEL = False
_TRAIN_IMG_DIR = 'data/subscenes/'
_TRAIN_MASK_DIR = 'data/cloud_masks/'

# Load Data
dataset = CloudDataset(
    _TRAIN_IMG_DIR,
    _TRAIN_MASK_DIR,
    _IMAGE_HEIGHT,
    _IMAGE_WIDTH,
    transform=transforms.ToTensor(),)

train_set, val_set = torch.utils.data.random_split(dataset, [0.9, 0.1])
train_loader = DataLoader(train_set, batch_size=_BATCH_SIZE, shuffle=True)
val_loader = DataLoader(val_set, batch_size=_BATCH_SIZE, shuffle=False)

# Model
model = UNet(in_channels=4, out_channels=1).to(_DEVICE)

#  Loss and optimizer
if _LOSS == 'dice':
    criterion = DiceLoss()
elif _LOSS == 'bce':
    criterion = nn.BCEWithLogitsLoss()
elif _LOSS == 'cross_entropy':
    criterion = nn.CrossEntropyLoss()
elif _LOSS == 'mse':
    criterion = nn.MSELoss()
else:
    raise Exception("Loss not defined")

def edit_optimizer(optimizer, lr):
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr

optimizer = optim.Adam(model.parameters(), lr=_LEARNING_RATE)

if _LOAD_MODEL and os.path.exists("my_checkpoint.pth_dice.tar"):
    load_checkpoint(torch.load("my_checkpoint.pth_dice.tar"), model)

check_accuracy(val_loader, model, device=_DEVICE, epoch=0)
scaler = torch.cuda.amp.GradScaler()
plot_loss = []

# Train Network
for epoch in range(_NUM_EPOCHS):
    loop = tqdm(train_loader)
    # for batch_idx, (input_data, targets) in enumerate(train_loader):
    # for batch_idx, (input_data, targets) in enumerate(loop):
    for index, batch in enumerate(loop):
        data, targets = batch

        # plt.imshow(input_data[0].permute(1,2,0))
        # plt.show()

        if index == 110:
            edit_optimizer(optimizer, lr=0.00001)

        # targets_show = torch.argmax(targets, dim=1).permute(1,2,0)
        # show_target = np.zeros((targets_show.shape[0], targets_show.shape[1], 3))
        # show_target[:, :, 0] = targets_show[:, :, 0]
        # show_target[:, :, 1] = targets_show[:, :, 1]
        # show_target[:, :, 2] = targets_show[:, :, 1] * 0

        # plt.imshow(show_target)
        # plt.show()

        data = data.to(device=_DEVICE)
        targets = targets.to(device=_DEVICE)
        # targets = targets.float().to(device=DEVICE)

        # # forward
        # output = model(input_data)
        # loss = criterion(output, targets)

        # forward
        with torch.cuda.amp.autocast():
            output = model(data)
            output = torch.softmax(output, dim=0)

            loss = criterion(output, targets)

            # for i in range(16):
            #
            #     fig, ax = plt.subplots(1, 3, figsize=(15, 8))
            #     output = torch.concat((output, output[:,0,:,:].unsqueeze(1)*0), dim=1)
            #     ax[0].imshow(output[i].cpu().permute(1, 2, 0).detach().numpy().astype(np.float32))
            #     targets = torch.concat((targets, targets[:,0,:,:].unsqueeze(1)*0), dim=1)
            #     ax[1].imshow(targets[i].cpu().permute(1, 2, 0).detach().numpy().astype(np.float32))
            #     ax[2].imshow(input_data[i][:3].cpu().permute(1, 2, 0).detach().numpy().astype(np.float32))
            #     plt.show()

            plot_loss.append(loss.item())

            # if epoch % 100 == 0:
            #     show_output_pytorch = output[0].cpu().permute(1, 2, 0).detach().numpy()
            #     show_output = np.zeros((show_output_pytorch.shape[0], show_output_pytorch.shape[1], 3))
            #     show_output[:, :, 0] = show_output_pytorch[:, :, 0]
            #     show_output[:, :, 1] = show_output_pytorch[:, :, 1]
            #     show_output[:, :, 2] = show_output_pytorch[:, :, 1] * 0
            #
            #     plt.imshow(show_output)
            #     # plt.show()

        optimizer.zero_grad()
        #  Scale Gradients
        scaler.scale(loss).backward()
        #  Update Optimizer
        scaler.step(optimizer)
        scaler.update()

        # update tqdm loop
        loop.set_postfix(loss=loss.item())

    # save model
    checkpoint = {
        "state_dict": model.state_dict(),
        "optimizer": optimizer.state_dict(),
    }
    if epoch % 1 == 0:
        save_checkpoint(checkpoint, filename="my_checkpoint.pth.tar")

        # check accuracy
        check_accuracy(val_loader, model, device=_DEVICE, epoch=epoch)

        # print some examples to a folder
        save_predictions_as_imgs(
            val_loader, model, folder="saved_images/", epoch=epoch, device=_DEVICE)

        # save figure of loss x = epoch, y = loss
        plt.plot(plot_loss, label='loss')
        plt.savefig(f'loss.png')
        plt.close()

    print(f"Epoch {epoch} Train loss: {loss.item()}")


from collections.abc import Generator
import json
from requests import HTTPError
import re

class AutoPaginateGraphQL(Generator):
    def __init__(self,
                 session,
                 url: str,
                 pagination_type: str,
                 data_path,
                 query,
                 paging_variable_name=None,
                 paging_param_path=None,
                 extra_params=None,
                 extra_headers=None,
                 query_variables=None,
                 step_size=None,
                 clean_query=True
                 ):
        self.url = url
        self.pagination_type = pagination_type
        self.session = session
        self.data_path = data_path
        self.query = query
        self.paging_param_path = paging_param_path
        self.paging_param = paging_variable_name
        self.raw_page = None
        self.is_last_page = False

        if query_variables is None:
            self.variables = {}
        else:
            self.variables = query_variables

        if extra_headers is None:
            self.extra_headers = {}
        else:
            self.extra_headers = extra_headers

        if extra_params is None:
            self.extra_params = {}
        else:
            self.extra_params = extra_params

        # offset pagination
        self.offset = 0
        self.step_size = step_size

        # both of the above
        self.num_returned = 9999

        # cursor pagination
        self.next_cursor = None

        # data to return
        self.page_data = []

        if type(self.data_path) == list and self.data_path[0] == "data":
            raise UserWarning("'data' in data_path")
        if clean_query:
            self.clean_gql_query()

    def send(self, ignore):
        # these conditionals look weird, but here's the reasoning:
        # for url pagination, you'll simply not get a next url when you hit the end often,
        # so trying to call the next page and stopping when you get nothing will error.
        # we also try and use it for page_number and limit/offset paging, but the 0 check
        # is needed for the case where it's only one page, since we set the expected page
        # size based on the first call in page_number paging
        if not self.page_data and self.is_last_page:
            raise StopIteration
        elif not self.page_data:
            self._next_page()
        if not self.page_data:
            raise StopIteration

        # remove the first entry in the list and return it
        return self.page_data.pop(0)

    def throw(self, typ, val=None, tb=None):
        super().throw(typ, val, tb)

    def _next_page(self):
        if self.pagination_type == "offset":
            self._get_offset_page()
        elif self.pagination_type == "cursor":
            self._get_cursor_page()
        else:
            raise ValueError('Unknown pagination type, try "offset" or "cursor"')

    def _get_offset_page(self):
        raise NotImplementedError

    def _get_cursor_page(self):
        headers = self.extra_headers
        params = self.extra_params
        if self.next_cursor:
            new_variables = {self.paging_param: self.next_cursor}
        else:
            new_variables = {}

        self.variables.update(new_variables)

        body = "{\"query\":\"" + self.query + "\", \"variables\":" + \
               str(self.variables).replace("'", "\"").replace("\"null\"", "null") + "}"

        raw_response = self.session.post(self.url, headers=headers, params=params, data=body)
        self.page_data = self.content_into_list(raw_response)

        parsed_response = json.loads(raw_response.content)["data"]

        try:
            self.next_cursor = self.get_value_from_path(parsed_response, self.paging_param_path)
        except KeyError:
            self.is_last_page = True

    def content_into_list(self, raw_page):
        # override this if you want something nuanced
        content = json.loads(raw_page.content)

        if "message" in content.keys():
            if content["message"] == "Problems parsing JSON":
                body = "{\"query\":\"" + self.query + "\", \"variables\":" + \
                       str(self.variables).replace("'", "\"") + "}"
                hint = "\nMake sure your query uses all double quotes, " \
                       "and that all double quotes are escaped with double backslash. " \
                       "(they should look like this below: `\\\\\"`)\n" + body
                raise HTTPError("Status {}. ".format(raw_page.status_code) + content["message"] + hint)
            else:
                raise HTTPError("Status {}. ".format(raw_page.status_code) + content["message"])

        if "errors" in content.keys():
            raise HTTPError("Status {}. ".format(raw_page.status_code) + str(content["errors"]))

        content = content["data"]

        output = self.get_value_from_path(content, self.data_path)
        return output

    def clean_gql_query(self):
        self.query = re.sub(r'(?<=\w\w\w)\s*\n\s*(?=\w+)', ",", self.query)
        self.query = self.query.replace("\n", "")
        self.query = self.query.replace(" ", "")
        self.query = re.sub(r'^query', "query ", self.query)
        self.query = re.sub(r'(?<!\\)"', "\\\"", self.query)

    @staticmethod
    def get_value_from_path(content, path):
        if type(path) == str:
            output = content[path]
        elif type(path) == list:
            output = content
            for item in path:
                output = output[item]
        else:
            raise ValueError("Unexpected data type for `path`: {}, use Str or List".format(type(path)))
        return output


#!/usr/bin/python3

from flask import Flask, request, send_file
from termcolor import colored
from uuid import uuid4
import json, time, fire

##### SETTINGS #####

debug_to_console = True # log debug messages to console
debug_to_file = True # log debug messages to a file called 'debug_log.txt'

##### SETTINGS #####

# debugging
def debug(*args):
	if debug_to_console:
		for i in args:
			print("[DEBUG] : "+i)
	if debug_to_file:
		try:
			with open("debug_log.txt","a") as f:
				for i in args:
					f.write(i)
		except Exception as e:
			with open("debug_log.txt","w") as f:
				f.write(alert("Error writing to debug file, creating..."))
				for i in args:
					f.write(i)

# coloured messages
def alert(msg): return colored("[!] ","red") + msg
def info(msg): return colored("[*] ","cyan") + msg
def plus(msg): return colored("[+] ","green") + msg
def minus(msg): return colored("[-] ","blue") + msg

# importing previous clients
def clients():
	try:
		return json.loads(open("clients.json","r").read())
	except Exception as e:
		debug(alert("Error importing clients from clients.json: "+e))
		try:
			a = open("clients.json","r").read()
			debug(info("File is readable, json must be corrupt"))
			debug(info("Wiping clients.json, old data can be found in old.clients.json"))
			with open("old.clients.json","w") as f:
				f.write(a)
			b = open("clients.json","w").close()
			return {}
		except:
			debug(alert("File appears to not exist, creating..."))
			a = open("clients.json","w").close()
			return {}
# adding new clients to the file
def add_client(uuid, nick=None):
	old = clients()
	old[uuid] = {'nick':nick,'timestamps':[],'hits':0,'hosts':{}}
	with open("clients.json","w") as f:
		f.write(json.dumps(old))

# flask server

app = Flask(__name__)

@app.route("/images/<uuid>/<image_name>")
def send_image(uuid, image_name):
	ts = time.time()
	cl = clients()
	if not uuid in cl:
		debug(alert("Unknown uuid [%s] attempted to access image [%s] at timestamp: %s" % (uuid,image_name,str(ts))))
		return "", 404
	else:
		cl[uuid]['timestamps'].append(ts)
		cl[uuid]['hits'] += 1
		cl[uuid]['hosts'][ts] = request.remote_addr
		debug(plus("We got a hit from client [%s] (ip: %s) at timestamp: %s" % (uuid,request.remote_addr,str(ts))))
		with open("clients.json","w") as f:
			f.write(json.dumps(cl))
		return send_file("blank_pixel.png", mimetype="image/gif")

class CLI(object):
	def run(self, ip, port):
		app.run(host=ip, port=int(port))
	def addClient(self, nickname):
		add_client(str(uuid4()),nickname)
if __name__ == "__main__":
	fire.Fire(CLI)


def PSRLQ(cpu, dest, src):
        """Shift Packed Data Right Logical

        Shifts the bits in the individual quadword in the destination operand to the right by
        the number of bits specified in the count operand . As the bits in the data elements
        are shifted right, the empty high-order bits are cleared (set to 0). If the value
        specified by the count operand is greater than  63, then the destination operand is set
        to all 0s.

        if(OperandSize == 64) {
                        //PSRLQ instruction with 64-bit operand:
                        if(Count > 63) Destination[64..0] = 0;
                        else Destination = ZeroExtend(Destination >> Count);
                }
                else {
                        //PSRLQ instruction with 128-bit operand:
                        if(Count > 15) Destination[128..0] = 0;
                        else {
                                Destination[0..63] = ZeroExtend(Destination[0..63] >> Count);
                                Destination[64..127] = ZeroExtend(Destination[64..127] >> Count);
                        }
                }
        """

        count = src.read()
        count = Operators.ITEBV(src.size, Operators.UGT(count, 63), 64, count)
        count = Operators.EXTRACT(count, 0, 64)
        if dest.size == 64:
            dest.write(dest.read() >> count)
        else:
            hi = Operators.EXTRACT(dest.read(), 64, 64) >> count
            low = Operators.EXTRACT(dest.read(), 0, 64) >> count
            dest.write(Operators.CONCAT(128, hi, low))