import sqlite3

#function to create a new table in database
def crt_table():
    connect = sqlite3.connect("students.db")
    cursor = connect.cursor()
    c = ""

    while(c != 'n'):
        n = input("Name for new table: ")
        num = int(input("How many columns do you want in your table: "))
        l1 = []

        for i in range(num):
            l1.append( input(f"Enter column {i+1} name:"))

        s1 = f'CREATE TABLE {n} (\n'

        s2 = ""

        for item in range(len(l1)):
            if item < (len(l1) - 1):
                s2 = s2 +  "\t" + f"{l1[item]}" + ",\n"
            else:
                s2 = s2 + "\t" + f"{l1[item]}" + "\n"

        s3 = ');'

        query = s1 + s2 + s3

        cursor.execute(query)

        connect.commit()
        c = input("Do you want to add another table: ")

    connect.close()

#function to add new records to table in database
def add_data(tb_nm):
    l = get_nm(tb_nm)

    connect = sqlite3.connect("students.db")
    cursor = connect.cursor()

    c = ""

    while(c != 'n'):
        inp = []
        inp2 = []
        for i in range(len(l)):
            inp2.append(input(f"Enter the value for {l[i][0]} column:"))

        tu = tuple(inp2)
        inp.append(tu)

        #creating a query string
        s1 = f"INSERT INTO {tb_nm} VALUES ("
        s2 = ""

        for i in range(len(l)):
            if i < (len(l) - 1):
                s2 = s2 + "?" + ", "
            else:
                s2 = s2 + "?);"

        query = s1 + s2

        #executing query
        cursor.executemany(query,inp)
        connect.commit()
        print("\n")
        c = input("Do you want to add another record? : ")

    connect.close()

#function to get column names of specified table
def get_nm(tb_nm):
    connect = sqlite3.connect("students.db")
    cursor = connect.cursor()

    query = f"SELECT name FROM pragma_table_info('{tb_nm}')"

    cursor.execute(query)

    l = cursor.fetchall()

    connect.commit()
    connect.close()
    return l

#this function displays the whole table i.e. specified
def show_tb():
    connect = sqlite3.connect("students.db")
    cursor = connect.cursor()
    c = ""

    while(c != "n"):
        print("\n\n")
        tb_nm = input("Enter the name of the table you want to see: ")
        l = get_nm(tb_nm)

        cursor.execute(f"SELECT * FROM {tb_nm}")
        l1 = cursor.fetchall()

        print(f"\nHere is your {tb_nm} table:")

        s1 = ""

        for i in range(len(l)):
            s1 = s1 + l[i][0] + "\t\t"

        s2 = ""

        for i in l1:
            for j in range(len(i)):
                s2 = s2 + i[j] + "\t\t"
            s2 = s2 + "\n"

        print(s1)
        print(s2)
        connect.commit()

        c = input("Do you want to see another table: ")

    connect.close()

#function to query a database
def query_tb():
    connect = sqlite3.connect("students.db")
    cursor = connect.cursor()
    c = ""

    while(c != "n"):
        q = input("Enter the query you want to execute: ")

        cursor.execute(q)

        items = cursor.fetchall()

        if items:
            print(items)

        connect.commit()
        c = input("Do you want to execute another query: ")

    connect.close()

#function to delete a whole table
def del_tb():
    connect = sqlite3.connect("students.db")
    cursor = connect.cursor()
    c = ""

    while(c !='n'):
        tb_nm = input("Name of the table you want to delete: ")

        cursor.execute(f"DROP TABLE {tb_nm}")

        connect.commit()
        c = input("Do you want to delete another table?")

    connect.close()

if __name__ == "__main__":
    crt_table("nothing", [1,2,3,4,5])


import torch

def iou(pred, target, num_classes):
    ious = []
    for cls in range(num_classes):
        pred_inds = pred == cls
        target_inds = target == cls
        intersection = (pred_inds[target_inds]).long().sum().data.cpu()  # Cast to long to prevent overflows
        union = pred_inds.long().sum().data.cpu() + target_inds.long().sum().data.cpu() - intersection
        if union == 0:
            ious.append(float('nan'))  # If there is no ground truth, do not include in evaluation
        else:
            ious.append(float(intersection) / max(union, 1))
    return ious

def dice(pred, target, num_classes):
    dice = []
    for cls in range(num_classes):
        pred_inds = pred == cls
        target_inds = target == cls
        intersection = (pred_inds[target_inds]).long().sum().data.cpu()  # Cast to long to prevent overflows
        dice.append(float(2 * intersection) / (pred_inds.long().sum().data.cpu() + target_inds.long().sum().data.cpu()))
    return dice

class MetricMaker:

    def __init__(self, classes) -> None:
        self.dices = {cls: [] for cls in classes}
        self.ious = {cls: [] for cls in classes}
        self.classes = classes

    def update(self, pred, target):
        dices = dice(pred, target, len(self.classes))
        ious = iou(pred, target, len(self.classes))
        for idx, cls in enumerate(self.classes):
            self.dices[cls].append(dices[idx])
            self.ious[cls].append(ious[idx])

    @property
    def last_dice(self):
        return {cls: self.dices[cls][-1] for cls in self.classes}

    @property
    def last_iou(self):
        return {cls: self.ious[cls][-1] for cls in self.classes}

    @property
    def mean_dice(self):
        return {cls: torch.tensor(self.dices[cls]).mean().item() for cls in self.classes}

    @property
    def mean_iou(self):
        return {cls: torch.tensor(self.ious[cls]).mean().item() for cls in self.classes}

    def reset(self):
        self.dices = {cls: [] for cls in self.classes}
        self.ious = {cls: [] for cls in self.classes}


# from django.http import JsonResponse #to return python data as JSON data # removed after adding api decarotor
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated, IsAdminUser
from rest_framework.response import Response
from .serializers import ProjectSerializer
from projects.models import Project, Review

# list specifies what methods function can take (only GET in this case)
@api_view(['GET'])
# gives all url paths or routes inside of api
def getRoutes(request):
    # list dictionaries that will be turned into javascript objects
    # specifying method that a specific route takes
    routes = [
        # returns a list of project objects
        {'GET': 'api/projects'},
        # returns a single project object
        {'GET': 'api/projects/id'},
        # passes in vote
        {'POST': 'api/projects/id/vote'},

        # built-in class to generate a token for user login
        {'POST': 'api/users/token'},
        # built-in class to generate a new json web token after it expires
        {'POST': 'api/users/token/refresh'},
    ]
    # return python data as json data
    # safe parameter allows us to return back something other than
    # a python dictionary, since we're sending a list, set safe to False
    # this is saying it's ok to turn any data into JSON data
    # return JsonResponse(routes, safe=False)
    # can use this instead of above b/c function is now wrapped in an api decarator
    return Response(routes)

# list specifies what methods function can take (only GET in this case)
@api_view(['GET'])
# requires authentication for user to get projects
# @permission_classes([IsAuthenticated])
def getProjects(request):
    projects = Project.objects.all()
    # takes queryset and turns it into json data
    # many = True b/c serializing multiple objects
    serializer = ProjectSerializer(projects, many=True)
    # .data used to get data out of serializer which is a class
    return Response(serializer.data)

# list specifies what methods function can take (only GET in this case)
@api_view(['GET'])
# requires authentication for user to get projects
# @permission_classes([IsAuthenticated])
def getProject(request, pk):
    project = Project.objects.get(id=pk)
    # takes queryset and turns it into json data
    # many = False b/c not serializing multiple objects
    serializer = ProjectSerializer(project, many=False)
    # .data used to get data out of serializer which is a class
    return Response(serializer.data)

# list specifies what methods function can take (only POST in this case)
@api_view(['POST'])
# requires authentication for user to get projects
@permission_classes([IsAuthenticated])
# takes in post request and modifies a project vote
def projectVote(request, pk):
    project = Project.objects.get(id=pk)
    user = request.user.profile
    # property available b/c of api decorator
    data = request.data
    # checks if object exists already and gets that instance
    # if it does and creates one if not
    # created will be true or false
    review, created = Review.objects.get_or_create(
        owner=user,
        project=project,
        )
    # getting vote value and setting it
    review.value = data['value']
    # update project vote count
    review.save()
    # because used @property decorator in models do not have to call as
    # traditional function
    project.getVoteCount

    # takes queryset and turns it into json data
    # many = False b/c not serializing multiple objects
    serializer = ProjectSerializer(project, many=False)
    # .data used to get data out of serializer which is a class
    return Response(serializer.data)


import typing
from .character import Character
from ...definition import AbstractGameLogic, AbstractGameState, CharacterStatus, InternalQuestion
from ...server import CharacterType

class CharacterGrey(Character):

    def __init__(self, state: CharacterType) -> None:
        super().__init__(state)
        self._post_movement_power = CharacterStatus.MANDATORY

    def get_power_possibilities(self, game_logic: AbstractGameLogic, game_state: AbstractGameState) -> typing.Tuple[InternalQuestion, tuple]:
        available_rooms = [room for room in range(10) if room is not game_state.room_shadow]
        game_state._end_of_turn = True
        return (InternalQuestion.ROOM_SHADOW, available_rooms)

    def reset(self) -> None:
        self._movement = CharacterStatus.AVAILABLE
        self._post_movement_power = CharacterStatus.MANDATORY


from Service.StudentService import StudentService

class Consola:
    def __init__(self, studentService: StudentService):
        self.__studentService = studentService

    def adaugaStudent(self):
        try:
            studentID = input("Dati ID-ul studentului: ")
            nume = input("Dati numele studentului: ")
            grup = input("Dati grupul studentului: ")
            self.__studentService.adauga(studentID, nume, grup)
        except KeyError as e:
            print(e)

    def modificaStudent(self):
        try:
            studentID = input("Dati ID-ul studentului care sa fie modificat: ")
            numeNou = input("Dati noul nume al studentului: ")
            grupNou = input("Dati noul grup al studentului: ")
            self.__studentService.modifica(studentID, numeNou, grupNou)
        except KeyError as e:
            print(e)

    def stergeStudent(self):
        try:
            studentID = input("Dati ID-ul studentului care sa fie sters: ")
            self.__studentService.sterge(studentID)
        except KeyError as e:
            print(e)

    def afiseaza(self,entitati):
        for entitate in entitati:
            print(entitate)
    def printMeniu(self):
        print("1. Adauga student")
        print("2. Modifica student")
        print("3. Sterge student")
        print("a. Afiseaza toti studentii")
        print("x. Iesire")

    def meniu(self):
        while True:
            self.printMeniu()
            tasta = input("Dati optiunea: ")
            if tasta == "1":
                self.adaugaStudent()
            elif tasta == "2":
                self.modificaStudent()
            elif tasta == "3":
                self.stergeStudent()
            elif tasta == "a":
                self.afiseaza(self.__studentService.getALLStudenti())
            elif tasta == "x":
                break
            else:
                print("Optiune grestia")


# -*- coding: utf-8 -*
# 载入模块
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np

# 创建 1 张画布
fig = plt.figure()

#===============

# 向画布添加子图 1
ax1 = fig.add_subplot(1, 2, 1, projection='3d')

# 生成子图 1 数据
x = np.linspace(-6 * np.pi, 6 * np.pi, 1000)
y = np.sin(x)
z = np.cos(x)

# 绘制第 1 张图
ax1.plot(x, y, z)

#===============

# 向画布添加子图 2
ax2 = fig.add_subplot(1, 2, 2, projection='3d')

# 生成子图 2 数据
X = np.arange(-2, 2, 0.1)
Y = np.arange(-2, 2, 0.1)
X, Y = np.meshgrid(X, Y)
Z = np.sqrt(X ** 2 + Y ** 2)

# 绘制第 2 张图
ax2.plot_surface(X, Y, Z, cmap=plt.cm.winter)

# 显示图
plt.show()


import aiohttp
import crescent
import hikari
from pydub import AudioSegment
import speech_recognition

plugin = crescent.Plugin()
recognizer = speech_recognition.Recognizer()

@plugin.include
@crescent.message_command(name="Process voice")
async def voice(ctx: crescent.Context, message: hikari.Message):
	if not hikari.MessageFlag.IS_VOICE_MESSAGE in message.flags:
		return await ctx.respond(content="not voice", ephemeral=True)
	await ctx.defer(ephemeral=True)
	with open("f.mp3", mode="wb") as f:
		async with aiohttp.ClientSession() as session:
			async with session.get(message.attachments[0].url) as response:
				thing = await response.read()
				f.write(thing)

	sound = AudioSegment.from_mp3("f.mp3")
	sound.export("f.wav", format="wav")

	with speech_recognition.AudioFile("f.wav") as source:
		audio_data = recognizer.record(source)
		text = recognizer.recognize_google(audio_data)

	await ctx.respond(text, ephemeral=True)


# import libraries
import sys

import pandas as pd
import pickle
from sqlalchemy import create_engine

from sklearn.pipeline import Pipeline, FeatureUnion
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.multioutput import MultiOutputClassifier
from sklearn.metrics import classification_report, f1_score
from sklearn.model_selection import GridSearchCV

from sklearn.base import BaseEstimator, TransformerMixin

from scipy.stats import hmean

import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.stem.porter import PorterStemmer
from nltk.corpus import stopwords
from nltk import pos_tag

nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
nltk.download('averaged_perceptron_tagger')
nltk.download('maxent_ne_chunker')

import re
import numpy as np

def load_data(database_filepath):
    """
	load data from database

	Input:
	database_filepath : path to database

	Output:
	X, Y, category_names : inputs to ML model
	"""

    database_filepath = "sqlite:///" + database_filepath
    engine = create_engine(database_filepath)
    df = pd.read_sql_table('MessagesTable', engine)
    X = df['message'].values
    Y = df.drop(['id', 'message', 'original', 'genre'], axis=1).values
    category_names = df.columns[3:]

    print('Loaded {} samples'.format(len(df)))

    return X, Y, category_names

def tokenize(text):
    """
	Tokenization function.

	Input:
	text : string to tokenize

	Output:
	tokens : list of tokens
	"""

	# URL processing
    url_regex = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
    detected_urls = re.findall(url_regex, text)
    for url in detected_urls:
        text = text.replace(url, "urlplaceholder")

    # Case Normalization and punctuation removal
    text = re.sub(r"[^a-zA-Z0-9]", " ", text.lower())

    # tokenize text
    tokens = word_tokenize(text)

	# Remove stopwords
    stop_words = stopwords.words("english")
    lemmatizer = WordNetLemmatizer()
    stop_words = list(set(stop_words) - {'over', 'not'})

    # lemmatize and stem
    tokens = [lemmatizer.lemmatize(word) for word in tokens if word not in stop_words]
    tokens = [lemmatizer.lemmatize(word, pos='n') for word in tokens]
    tokens = [lemmatizer.lemmatize(word, pos='v') for word in tokens]
    tokens = [PorterStemmer().stem(w) for w in tokens]

    return tokens

def generalized_fscore(y_true, y_pred):
    """
    Scoring function used for gridsearch.
    We use a generalization of the F score for precision recall for the '1' class of each category.

    Input :
    y_true, y_pred : arrays of true values and model predictions

    Output:
    f1score : model score
    """
    score_list = []
    for column in range(y_true.shape[1]):
		# First compute each F score for the '1' class of each category
        score = f1_score(y_true[:,column], y_pred[:,column], average='binary', labels=[1])
		# Add 0.01 to avoid zero scores
        score = min(1, score+0.01)
        score_list.append(score)
    f1_score_vec = np.asarray(score_list)
	# Compute harmonic mean
    f1score = hmean(f1_score_vec)
    return  f1score

class QuestionExtractor(BaseEstimator, TransformerMixin):
    """
	Transformer that returns 'True' if there is a question in the text.
	This feature was proven useful through grid-search.
	"""
    def __init__(self, used=True):
		# 'used' is boolean to decide if the feature is used or not.
        self.used = used

    def question_matcher(self, text):
        if text.find('?') != -1 and self.used:
            return True
        else:
            return False

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        X_tagged = pd.Series(X).apply(self.question_matcher)
        return pd.DataFrame(X_tagged)

class DollarsExtractor(BaseEstimator, TransformerMixin):
    """
	Transformer that returns 'True' if there is an amount in dollars in the text, like $25,000.
	This feature was proven useful through grid-search.
	"""

    def __init__(self, used=True):
		# 'used' is boolean to decide if the feature is used or not.
        self.used = used

    def dollars_matcher(self, text):
        money_regex = r"\$\ ?[+-]?[0-9]{1,3}(?:,?[0-9])*(?:\.[0-9]{1,2})?"
        z = re.findall(money_regex, text)
        if len(z)>0 and self.used:
            return True
        else:
            return False

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        X_tagged = pd.Series(X).apply(self.dollars_matcher)
        return pd.DataFrame(X_tagged)

class NumSentExtractor(BaseEstimator, TransformerMixin):
    """
	Transformer that returns the number of sentences in the text.
	This feature was proven useful through grid-search.
	"""
    def __init__(self, used=True):
		# 'used' is boolean to decide if the feature is used or not.
        self.used = used

    def numsent_extractor(self, text):
        try:
            sentences = sent_tokenize(text)
            if self.used:
                return len(sentences)
            else:
                return 0
        except:
            return 0

    def fit(self, X, y=None):
        return self

    def transform(self, X):
        X_tagged = pd.Series(X).apply(self.numsent_extractor)
        return pd.DataFrame(X_tagged)

def build_model(grid_search=1):
    """
    Model construction.

    Input :
    grid_search : if 1, the gridsearch is ran to find the best hyper-parameters. Warning : this is a long process.
    If 0, we take the model that was tuned.

    Output:
    cv : optimal model with fined-tuned hyper-parameters
    """
    if grid_search:

        scorer = make_scorer(generalized_fscore, greater_is_better = True)
        clf = MultiOutputClassifier(RandomForestClassifier())

        pipeline = 	Pipeline([

	        ('features', FeatureUnion([

	            ('nlp_pipeline', Pipeline([
	                ('vect', CountVectorizer(tokenizer=tokenize, max_df=0.75, max_features=1000, min_df=3, ngram_range=(1, 3))),
	                ('tfidf', TfidfTransformer())
	            ])),

	            ('dollars_extractor', DollarsExtractor()),
	            ('question_extractor', QuestionExtractor()),
	            ('numsent_extractor', NumSentExtractor())

	    	]))

	    ])

        parameters_loc = {
			'vect__ngram_range': [(1, 3)],
			'vect__max_df': (0.75, 1.0),
			'vect__max_features': (5000, 10000),
			'vect__min_df': (1,5),
			'clf__estimator__n_estimators': [50, 100, 200],
			'clf__estimator__min_samples_split': [2, 5]
		}

        cv = GridSearchCV(pipeline, param_grid=parameters_loc, scoring=scorer, n_jobs=-1, verbose=10)

    else:

        cv = Pipeline([

	        ('features', FeatureUnion([

	            ('nlp_pipeline', Pipeline([
	                ('vect', CountVectorizer(tokenizer=tokenize, max_df=0.75, max_features=1000, min_df=3, ngram_range=(1, 3))),
	                ('tfidf', TfidfTransformer())
	            ])),

	            ('dollars_extractor', DollarsExtractor()),
	            ('question_extractor', QuestionExtractor()),
	            ('numsent_extractor', NumSentExtractor())

	        ])),

	    	('clf', MultiOutputClassifier(RandomForestClassifier(min_samples_split=2, n_estimators=10)))

	    ])

    return cv

def evaluate_model(model, X_test, y_test, category_names):
    """
	Model evaluation.

	Input :
	model : model fit on data
	X_test : features of the test set.
	y_test : ground-truth on test set.
	category_names : names of categories

	Output:
	No output. The function computes and shows the model performance.
	"""

    y_pred = model.predict(X_test)

    acc = (y_pred == y_test).mean(axis=0)
    for ii, (name, acc_loc) in enumerate(zip(category_names, acc)):
        print('='*50)
        print('- '+name.upper()+':' )
        print('-'*50)
        print('Accuracy : {:.2f}'.format(acc_loc))
        print('-'*50)
        print(classification_report(y_test[:, ii], y_pred[:, ii]))

    pass

def save_model(model, model_filepath):
    """
    Model saving.
    """
    pickle.dump(model, open(model_filepath, 'wb'))

    pass

def main():
    """
	Main function.
	"""
    if len(sys.argv) == 3:

        database_filepath, model_filepath = sys.argv[1:]
        print('Loading data...\n    DATABASE: {}'.format(database_filepath))
        X, Y, category_names = load_data(database_filepath)
        X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)

        print('Building model...')
        model = build_model(grid_search=0)

        print('Training model...')
        model.fit(X_train, Y_train)

        print('Evaluating model...')
        evaluate_model(model, X_test, Y_test, category_names)

        print('Saving model...\n    MODEL: {}'.format(model_filepath))
        save_model(model, model_filepath)

        print('Trained model saved!')

    else:
        print('Please provide the filepath of the disaster messages database '\
              'as the first argument and the filepath of the pickle file to '\
              'save the model to as the second argument. \n\nExample: python '\
              'train_classifier.py ../data/DisasterResponse.db classifier.pkl')

if __name__ == '__main__':
    main()


import subprocess

import ajenti
from ajenti.api import *
from ajenti.plugins.main.api import SectionPlugin
from ajenti.ui import on
from ajenti.ui.binder import Binder

from reconfigure.configs import FSTabConfig
from reconfigure.items.fstab import FilesystemData

import disks

class Mount (object):
    def __init__(self):
        self.device = ''
        self.mountpoint = ''
        self.type = ''
        self.option = ''

@plugin
class MountsBackend (BasePlugin):
    def __init__(self):
        self.reload()

    def reload(self):
        self.filesystems = []
        try:
            df_data = subprocess.check_output(['df', '-P'])
        except:
            return

        for l in df_data.splitlines()[1:]:
            f = Mount()
            l = l.split()
            f.device = l[0]
            if f.device == 'map' and ajenti.platform == 'osx':
                continue
            f.mountpoint = l[5]
            f.used = int(l[2]) * 1024
            f.size = int(l[1]) * 1024
            f.usage = 1.0 * f.used / f.size
            self.filesystems.append(f)

@plugin
class Filesystems (SectionPlugin):
    def init(self):
        self.title = _('Filesystems')
        self.icon = 'hdd'
        self.category = _('System')
        self.append(self.ui.inflate('fstab:main'))

        self.find('type').labels = ['Auto', 'EXT2', 'EXT3', 'EXT4', 'NTFS', 'FAT', 'ZFS', 'ReiserFS', 'Samba', 'None', 'Loop']
        self.find('type').values = ['auto', 'ext2', 'ext3', 'ext4', 'ntfs', 'vfat', 'zfs', 'reiser',  'smb',   'none', 'loop']

        self.fstab_config = FSTabConfig(path='/etc/fstab')
        self.mounts = MountsBackend.get()

        self.binder = Binder(None, self)
        self.find('fstab').find('filesystems').new_item = lambda c: FilesystemData()

        def post_mount_bind(object, collection, item, ui):
            ui.find('umount').on('click', self.on_umount, item)

        self.find('mounts').find('filesystems').post_item_bind = post_mount_bind

    def on_page_load(self):
        self.refresh()

    def on_umount(self, mount):
        subprocess.call(['umount', mount.mountpoint])
        self.context.notify('info', _('Unmounted'))
        self.refresh()

    @on('mount-all', 'click')
    def on_mount_all(self):
        self.save()
        if subprocess.call(['mount', '-a']):
            self.context.notify('error', _('mount -a failed'))
        self.refresh()

    @on('refresh', 'click')
    def refresh(self):
        self.binder.unpopulate()
        self.reload_disks()
        self.fstab_config.load()
        self.fstab = self.fstab_config.tree
        self.mounts.reload()
        self.binder.setup(self).populate()

    def reload_disks(self):
        lst = disks.list_devices(by_uuid=True, by_id=True, by_label=True)
        self.find('device').labels = [x[0] for x in lst]
        self.find('device').values = [x[1] for x in lst]

    @on('save', 'click')
    def save(self):
        self.binder.update()
        self.fstab_config.save()
        self.context.notify('info', _('Saved'))


#! /usr/bin/env python3

import matplotlib.pyplot as plt
import pandas as pd
pd.set_option('display.max_columns', 6)

mismatch = 2
df = pd.read_table(f'Result-m{mismatch}.tsv')

df.columns = ["target", "query", "start", "end", "orientation", "edits"]

df = df.astype({'edits': 'int'})
df = df.astype({'start': 'int'})
df = df.astype({'end': 'int'})

df['queryName'] = df['query'].str.split('_').str[1]
df['querySeq'] = df['query'].str.split('_').str[0]
df['targetName'] = df['target'].str.split('_').str[1]
df['targetSeq'] = df['target'].str.split('_').str[0]
df['family'] = ""

def add_name_to_query(dataf, query_sequence, counter):
    query_slice = dataf.loc[dataf["querySeq"] == query_sequence]
    for query_row in query_slice.itertuples():
        if query_row.family == "":
            dataf.loc[query_row.Index, "family"] = counter
            target_sequence = query_row.targetSeq
            dataf = add_name_to_query(dataf, target_sequence, counter)
    return dataf

novel = len(df[df["queryName"] == "novel"]["querySeq"].unique())

family = 0

# Get all sequences of novel miRNAs
for querySequence in df[df["queryName"] == "novel"]["querySeq"].unique():
    for mapping in df[df["querySeq"] == querySequence].itertuples():
        targetSequence = mapping.targetSeq
        if mapping.family == "":
            df.loc[df["querySeq"] == querySequence, "family"] = family
            df = add_name_to_query(df, targetSequence, family)
            print(family)
    ## Null + current count(0 indexed) + 1 -->family=0; unique={0,1}; len=2 means that a family was added in run
    if len(df["family"].unique()) > family+1:
        family += 1
print(df['accession'])

### Possible new stuff

piv = pd.pivot_table(df, "query", "accession", "queryName", aggfunc="count")['novel']
piv = pd.pivot_table(df, "queryName", "family", aggfunc="count")

## To make fig
dfNoDup = df[["query", "queryName", "querySeq", "family"]].drop_duplicates()
piv = pd.pivot_table(dfNoDup, "queryName", "family", aggfunc="count")

y = list(piv.values)
y.pop()
x = list(piv.index)
x.pop()
plt.title(f'Novel miRNAs that have been grouped\n into the same accession with mismatch=2')
plt.xlabel("accessions")
plt.ylabel("# mature miRNA")
plt.plot(x, y)
plt.savefig("Novel count same accession.png")

# Multiple conditionals
# df.loc[(df["accession"] != "") & (df["queryName"]=="novel"),:]

df2 = pd.read_table("~/Downloads/TEST_miRNAPlantPortal/all_seq-sRNA-SuLop.tsv")
df2.index = df2['sequence']

# 31372
dfNovelAll = df2[df2['name'] == "novel"]

# Import Excel
# pip install openpyxl
xlsx = pd.read_excel("~/Documents/Lab-People/Susana Lopes/Artigo sRNA/Supplementary Tables.xlsx",sheet_name=2,header=3)

# Exclusion attempt
expCols = [lib for lib in df2.columns if lib.startswith("Lib")]

labels = [["meta"] * 2 + ["abs"] * len(expCols) + ["bool"] * len(expCols), ["sequence", "name"] + expCols * 2]
tuples = list(zip(*labels))
cols = pd.MultiIndex.from_tuples(tuples, names=["Type", "Name"])

for lib in expCols:
    dfNovelAll.loc[lib+"_bool"] = 0
    dfNovelAll.loc[dfNovelAll[lib] > 0, lib+"_bool"] = 1

dfNovelAll.columns = cols

dfNovelAll.loc[:, 'mapped'] = False
dfNovelAll.loc[dfNovelAll['sequence'].isin(df.querySeq.unique()), 'mapped'] = True
dfNovelAll.loc[:, 'susana'] = False
susanaSeq = xlsx[xlsx.names == "novel"].Sequence.unique()
dfNovelAll.loc[dfNovelAll.sequence.isin(susanaSeq), "susana"] = True

# pip install matplotlib-venn
from matplotlib_venn import venn2

venn2([set(dfNovelAll[dfNovelAll.mapped].index), set(dfNovelAll[dfNovelAll.susana].index)], ("Mapped", "Susana"))
plt.show()

from matplotlib_venn import venn3

venn3([set(dfNovelAll.index), set(dfNovelAll[dfNovelAll.mapped].index), set(dfNovelAll[dfNovelAll.susana].index)], ("All", "Mapped", "Susana"))
plt.title("Novel miRNAs Qsuber dataset")
plt.savefig("Venn diagram all novel Susana.png")

dfNovelAll.loc[:, "family"] = None

mapped = df.loc[df.queryName == "novel", ['querySeq', "family"]].drop_duplicates()

for row in mapped.itertuples():
    dfNovelAll.loc[row.querySeq, 'family'] = row.family

novelCounter = df[df.family != ""].family.astype("int").max()
for row in dfNovelAll.itertuples():
    if row.family is None:
        novelCounter += 1
        dfNovelAll.loc[row.sequence, 'family'] = novelCounter

#From 31372 to 25077
dfNovelAll.to_excel("Novel_with_family.xlsx")


import copy

from heronpy.api.tuple import TupleHelper

from heronpy.api.component.component_spec import HeronComponentSpec
from heronpy.api.component.base_component import BaseComponent
from heronpy.api.stream import Stream

class BaseBolt(BaseComponent):
  """BaseBolt class

  This is the base for heron bolt, which wraps the implementation of publicly available methods.
  This includes:
    - <classmethod> spec()
    - emit()
    - <staticmethod> is_tick()
    - ack()
    - fail()

  They are compatible with StreamParse API.
  """
  # pylint: disable=no-member
  @classmethod
  def spec(cls, name=None, inputs=None, par=1, config=None, optional_outputs=None):
    """Register this bolt to the topology and create ``HeronComponentSpec``

    This method takes an optional ``outputs`` argument for supporting dynamic output fields
    declaration. However, it is recommended that ``outputs`` should be declared as
    an attribute of your ``Bolt`` subclass. Also, some ways of declaring inputs is not supported
    in this implementation; please read the documentation below.

    :type name: str
    :param name: Name of this bolt.
    :type inputs: dict or list
    :param inputs: Streams that feed into this Bolt.

                   Two forms of this are acceptable:

                   1. A `dict` mapping from ``HeronComponentSpec`` to ``Grouping``.
                      In this case, default stream is used.
                   2. A `dict` mapping from ``GlobalStreamId`` to ``Grouping``.
                      This ``GlobalStreamId`` object itself is different from StreamParse, because
                      Heron does not use thrift, although its constructor method is compatible.
                   3. A `list` of ``HeronComponentSpec``. In this case, default stream with
                      SHUFFLE grouping is used.
                   4. A `list` of ``GlobalStreamId``. In this case, SHUFFLE grouping is used.
    :type par: int
    :param par: Parallelism hint for this spout.
    :type config: dict
    :param config: Component-specific config settings.
    :type optional_outputs: list of (str or Stream) or tuple of (str or Stream)
    :param optional_outputs: Additional output fields for this bolt. These fields are added to
                             existing ``outputs`` class attributes of your bolt. This is an optional
                             argument, and exists only for supporting dynamic output field
                             declaration.
    """
    python_class_path = f"{cls.__module__}.{cls.__name__}"

    if hasattr(cls, 'outputs'):
      # avoid modification to cls.outputs
      _outputs = copy.copy(cls.outputs)
    else:
      _outputs = []

    if optional_outputs is not None:
      assert isinstance(optional_outputs, (list, tuple))
      for out in optional_outputs:
        assert isinstance(out, (str, Stream))
        _outputs.append(out)

    return HeronComponentSpec(name, python_class_path, is_spout=False, par=par,
                              inputs=inputs, outputs=_outputs, config=config)

  def emit(self, tup, stream=Stream.DEFAULT_STREAM_ID,
           anchors=None, direct_task=None, need_task_ids=False):
    """Emits a new tuple from this Bolt

    It is compatible with StreamParse API.

    :type tup: list or tuple
    :param tup: the new output Tuple to send from this bolt,
                which should contain only serializable data.
    :type stream: str
    :param stream: the ID of the stream to emit this Tuple to.
                   Leave empty to emit to the default stream.
    :type anchors: list
    :param anchors: a list of HeronTuples to which the emitted Tuples should be anchored.
    :type direct_task: int
    :param direct_task: the task to send the Tuple to if performing a direct emit.
    :type need_task_ids: bool
    :param need_task_ids: indicate whether or not you would like the task IDs the Tuple was emitted.
    """
    self.delegate.emit(tup, stream, anchors, direct_task, need_task_ids)

  @staticmethod
  def is_tick(tup):
    """Returns whether or not a given HeronTuple is a tick Tuple

    It is compatible with StreamParse API.
    """
    return tup.stream == TupleHelper.TICK_TUPLE_ID

  def ack(self, tup):
    """Indicate that processing of a Tuple has succeeded

    It is compatible with StreamParse API.
    """
    self.delegate.ack(tup)

  def fail(self, tup):
    """Indicate that processing of a Tuple has failed

    It is compatible with StreamParse API.
    """
    self.delegate.fail(tup)


import sys

k = sys.argv

if len(k) >=3:
	with open(k[1],'r') as fl:
		data1 = fl.read()
	with open(k[2],'r') as fl:
		data2 = fl.read()
	if data1 == data2:
		print(True)
	else:
		print(False)
else:
	print('Enter 2 files to compare')


import concurrent.futures
import time

def do_something(seconds):
	print("sleeping for %f seconds..." %seconds)
	time.sleep(seconds)
	return 'Done Sleeping ... %f seconds' %seconds

if __name__ == "__main__":

	start = time.perf_counter()

	with concurrent.futures.ProcessPoolExecutor() as executor:

		#Method 1
		# f1 = executor.submit(do_something, 1)
		# print(f1.result())

		#Method 2 - returns results ASAP and as completed
		# secs = [5, 4, 3, 2, 1]
		# results = [executor.submit(do_something, sec) for sec in secs] #list comprehension
		# for f in concurrent.futures.as_completed(results):
		# 	print(f.result())

		#Method 3 - returns results in order they were started
		secs = [5, 4, 3, 2, 1]
		results = executor.map(do_something, secs)

		for result in results:
			#exceptions are handled here
			print(result)

	finish = time.perf_counter()

	print("finished in %f seconds" %(finish - start))


from typing import List

class Solution:
    def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
        n = len(rooms)

        # Guarda quais salas já visitei
        visited = [False] * n
        visited[0] = True  # Começa pela primeira sala (0)

        queue = [0]  # Fila pra guardar quais salas ainda precisa visitar

        while queue:
            # Remove a sala da fila
            room = queue.pop(0)

            for key in rooms[room]:
                # Se ainda não tiver visitado a sala, atribui true para o valor de visited
                if not visited[key]:
                    visited[key] = True
                    queue.append(key)

        return all(visited)


import re, json
from flask import Flask, request

app = Flask(__name__)

@app.route("/getPersonCardInfo")
def getPersonCardInfo():
    cardno = request.args.get("cardno", "")
    card_info = get_cardno_info(cardno)
    return json.dumps(card_info, ensure_ascii=False)

def get_cardno_info(cardno):
    ret = {"err": 1, "area": "", "birthday": "", "sex": ""}

    cardno = cardno.upper()
    if re.fullmatch("[1-6]\\d{16}[\\d|X]", cardno) == None:
        return ret

    code = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
    i, s = 0, 0
    while i < 17:
        s += int(cardno[i]) * code[i]
        i += 1

    s = (12 - (s % 11)) % 11

    if s == 10:
        s = "X"
    else:
        s = str(s)

    if s != cardno[17]:
        return ret

    # 提取身份证号中的信息
    area_code = cardno[:6]
    with open("pid_data.txt", "r", encoding="utf-8") as f:
        while True:
            line = f.readline()
            if line == "":
                break

            if line[:6] == area_code:
                ret["area"] = line.split()[1]
                break

    birthday = cardno[6:14]
    birthday = "%s年%d月%d日" % (birthday[0:4], int(birthday[4:6]), int(birthday[6:8]))
    ret["birthday"] = birthday
    sex = "男"
    if int(cardno[-2]) % 2 == 0:
        sex = "女"
    ret["sex"] = sex
    if ret["area"] != "" and ret["birthday"] != "" and ret["sex"] != "":
        ret["err"] = 0
    return ret

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

获取身份证信息接口.py

获取身份证信息接口.py


import json
from pymongo import MongoClient

filename = "mongo-seed/employees.json"
# filename = "mongo-seed/employees_short.json"

with open(filename, "r") as f:
    data = json.load(f)

client = MongoClient("localhost", 27017)
db = client.employees
employees_collection = db.employees_collection
employees_id = employees_collection.insert_many(data).inserted_ids
print("Вставлено {} записей в коллекцию".format(len(employees_id)))


from app.common.util.json_util import to_json
from flask import Response
import app.auth.constant as constant

MIME_TYPE = 'application/json;charset=utf-8'
DEFAULT_MESSAGE = ''

class Resp:

    def __init__(self):
        pass

    @classmethod
    def success(cls, message=constant.SUCCESS_MESSAGE, data=None):
        """请求成功结果

        :param message: 消息
        :param data: 数据
        """
        resp = cls()
        resp.message = message
        if data:
            resp.data = data
        return resp

    @classmethod
    def error(cls, status=constant.INTERNAL_ERROR, description=None, fields=None):
        """请求失败结果

        :param status: 错误状态码
        :param description: 用户可读的错误消息
        :param fields: 字段描述，通常用于描述请求参数的错误
        """
        resp = cls()
        _error = cls()
        _error.status = status
        if description:
            _error.description = description
        if fields:
            _error.fields = fields
        resp.error = _error
        return resp

    def to_json(self):
        return to_json(self)

def success(message=constant.SUCCESS_MESSAGE, data=None):
    """请求成功结果

    :param message: 消息
    :param data: 数据
    """
    return Response(Resp.success(message=message, data=data).to_json(), mimetype=MIME_TYPE)

def error(status=None, description=None, fields=None, http_status=None):
    """请求失败结果

    :param status: 错误状态码
    :param description: 用户可读的错误消息
    :param fields: 字段描述，通常用于描述请求参数的错误
    :param http_status: http状态码
    """
    resp = Response(Resp.error(status=status, description=description, fields=fields).to_json(), mimetype=MIME_TYPE)
    if http_status:
        resp.status_code = http_status
    return resp


import json
import math
from pathlib import Path
from typing import Optional, Dict, List, Any

from flask import Blueprint, Flask, send_from_directory, url_for, request, render_template, flash, send_file
from werkzeug.security import safe_join
from werkzeug.utils import secure_filename, redirect

# to consider later: https://github.com/un33k/python-slugify

bp = Blueprint('manga', __name__, url_prefix='/manga')

instance_path: Optional[Path] = None
media_dir_path: Optional[Path] = None

def init(app: Flask):
    global instance_path, media_dir_path
    instance_path = Path(app.instance_path)
    media_dir_path = Path(app.instance_path, 'media')

# Root
@bp.get('/')
def root():
    return "This is not the URL you're looking for", 404

# Favicon
@bp.get('/favicon.png')
def favicon():
    favicon_path = Path(instance_path, 'favicon.png')
    if favicon_path.is_file():
        return send_file(path_or_file=favicon_path, mimetype='image/png')
    return '', 404

# CSS
@bp.get('/style.css')
def main_css():
    return send_file(path_or_file=Path(instance_path, 'style.css'), mimetype='text/css')

# Media endpoint for serving static files
@bp.get('/media/<path:filename>', endpoint='media')
def manga_media(filename: str):
    return send_from_directory(directory=media_dir_path, path=filename)

# Rules just for building URLs to media files, the actual routing goes to the media endpoint
bp.add_url_rule('/media/<string:series>/<path:filename>', endpoint='media_series', build_only=True)
bp.add_url_rule('/media/<string:series><string:chapter>/<path:filename>', endpoint='media_chapter', build_only=True)

def load_manga_json(series_slug: str) -> Optional[Dict[str, Any]]:
    json_path = Path(media_dir_path, f'{series_slug}.json')
    if not json_path.is_file():
        return None
    with json_path.open(mode='rt', encoding='utf-8') as f:
        return json.load(f)

# Series page
@bp.get('/<string:series_slug>')
def view_series(series_slug: str):
    series_slug = secure_filename(series_slug)
    page = request.args.get('page', default=1, type=int)

    manga_json = load_manga_json(series_slug=series_slug)
    if manga_json is None:
        return redirect(url_for('.root'))

    title = manga_json['title']
    chapters: List[Dict[str, Any]] = manga_json['chapters']

    # Slice chapters for pagination
    page_size = 20      # constant for now
    page_count = max(1, math.ceil(len(chapters) / page_size))
    page = max(1, min(page_count, page))
    chapters = chapters[page_size * (page - 1):page_size * page]

    return render_template('series_view.html', series_slug=series_slug, series_title=title, page=page,
                           last_page=page_count, chapters=chapters)

# Chapter page
@bp.get('/<string:series_slug>/<string:chapter_slug>')
def view_chapter(series_slug: str, chapter_slug: str):
    series_slug = secure_filename(series_slug)
    chapter_slug = secure_filename(chapter_slug)

    manga_json = load_manga_json(series_slug=series_slug)
    if manga_json is None:
        return redirect(url_for('.root'))

    chapters = manga_json['chapters']
    chapter, ci = next(((c, i) for i, c in enumerate(chapters) if c['slug'] == chapter_slug), (None, None))
    if chapter is None:
        flash('chapter not found', category='error')
        return redirect(url_for('.view_series', series_slug=series_slug))

    prev_chapter_slug = chapters[ci - 1]['slug'] if ci > 0 else None
    next_chapter_slug = chapters[ci + 1]['slug'] if ci < len(chapters) - 1 else None

    return render_template('chapter_view.html', series=manga_json, chapter=chapter, contents=chapter['contents'],
                           prev_slug=prev_chapter_slug, next_slug=next_chapter_slug)


"""
This module will set basic configuration for loggers.
"""

import logging
from typing import Any

formatter = logging.Formatter(
    "%(asctime)s %(levelname)s - LogGenerationFile: %(module)s - Line Number: %(lineno)d - Log:%(message)s"
)

def setup_logger(name: str, log_file: str, level: Any = logging.INFO) -> Any:
    """To setup as many loggers as you want"""

    handler = logging.FileHandler(log_file, mode="a")
    handler.setFormatter(formatter)

    logger = logging.getLogger(name)
    logger.setLevel(level)
    logger.addHandler(handler)

    return logger


from wave_train.io.load import Load
from wave_train.graphics.factory import VisualTDSE, VisualTISE, VisualQCMD, VisualCEoM

def load_file():

    # Pic[kle] file from previous simulation
    data_file = 'Exciton/tdse_1.pic'
    my_file = data_file.split('.')[0]
    file_type = data_file.split('.')[-1]

    # Load data from pickle file
    dynamics = Load(my_file, file_type)

    if dynamics.name == "TISE":
        factory = VisualTISE

    # Check conservation of energy, norm, and RMSD (if available)
    elif dynamics.name in ['TDSE', 'QCMD']:
        dynamics.check_ENR()

        if dynamics.name == "TDSE":
            factory = VisualTDSE
        else:
            factory = VisualQCMD

    # Redistribution of energy between kinetic and potential
    elif dynamics.name == 'CEoM':
        dynamics.check_EKP()
        factory = VisualCEoM

    # Redistribution of energy between quantum and classical subsystem
    elif dynamics.name == 'QCMD':
        dynamics.check_EQC()

    # Setup animated visualization
    graphics = factory(
        dynamics=dynamics,               # choice of "dynamics" (Load), see above
        plot_type='Populations',         # select your favorite plot style
        plot_expect=True,                # toggle plotting of expectation values
        figure_pos=(100, 50),            # specifying position (x,y) of upper left of figure [in pixels]
        figure_size=(1050, 450),         # specifying size (w,h) of figure [in pixels]
        image_file=my_file + '.png',     # if not None, image (last frame) will be written this file
        movie_file=my_file + '.mp4',     # if not None, animation will be written to this file
        frame_rate=1,                    # frames per second in mp4 animation file
        # pos_limits = (-25.0, 25.0)       # range of position axis (left, right)
        # mom_limits = (-0.05, 0.05)       # range of momentum axis (bottom, top)
        plot_style={}                    # additional plot style information
    ).create()
    graphics.solve()                     # Visualization only

if __name__ == '__main__':
    load_file()


import pyautogui as pag
import time
import os
import tkinter
import loadsettings
import move
ws = loadsettings.load()["walkspeed"]

def apd(k):
    cmd = """
        osascript -e  'tell application "System Events" to key down "{}"'
    """.format(k)
    os.system(cmd)
def apu(k):
    cmd = """
        osascript -e  'tell application "System Events" to key up "{}"'
    """.format(k)
    os.system(cmd)

def sleep(duration, get_now=time.perf_counter):
    now = get_now()
    end = now + duration
    while now < end:
        now = get_now()

move.hold("w",1)
pag.keyDown("s")
sleep(0.2)
pag.keyUp("s")
move.hold("a",1.5)
move.hold("d",3)


from django.urls import path
from .views import current_user, UserList, StorePracticeData, StoreTestData, RetrieveStatistics

urlpatterns = [
    path('current_user/', current_user),
    path('users/', UserList.as_view()),
    path('store_practice_data/', StorePracticeData.as_view()),
    path('store_test_data/', StoreTestData.as_view()),
    path('fetch_stats/', RetrieveStatistics.as_view()),
]


import os

import cv2.cv2 as cv2
import numpy as np
from torch.utils.data import Dataset as BaseDataset

class Dataset(BaseDataset):

    CLASSES = ['phyto']

    def __init__(
            self,
            images_dir,
            masks_dir,
            classes=None,
            augmentation=None,
            preprocessing=None,
    ):
        self.ids = os.listdir(images_dir)
        self.ids[0] = self.ids[-1]
        self.images_fps = [os.path.join(images_dir, image_id) for image_id
                           in
                           self.ids]
        self.masks_fps = [os.path.join(masks_dir, image_id) for image_id in
                          self.ids]

        # convert str names to class values on masks
        self.class_values = [self.CLASSES.index(cls.lower()) for cls in
                             classes]

        self.augmentation = augmentation
        self.preprocessing = preprocessing

    def __getitem__(self, i):

        # read data
        image = cv2.imread(self.images_fps[i])
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        mask = cv2.imread(self.masks_fps[i], 0)

        # extract certain classes from mask (e.g. cars)
        masks = [(mask == v) for v in self.class_values]
        mask = np.stack(masks, axis=-1).astype('float')

        if self.augmentation:
            sample = self.augmentation(image=image, mask=mask)
            image, mask = sample['image'], sample['mask']

        # apply preprocessing
        if self.preprocessing:
            sample = self.preprocessing(image=image, mask=mask)
            image, mask = sample['image'], sample['mask']

        return image, mask

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


#!/usr/bin/env python
# encoding: utf-8
"""
@author: HuRuiFeng
@file: 035_remove-minimum-characters.py
@time: 2023/8/8 10:18
@project: huawei-od-python
@desc: 035 删除最少的字符
"""
from collections import Counter

def solve_method(chars):
    # 统计字符频率
    freq = Counter(chars)
    # 求最小值
    min_freq = min(freq.values())
    result = ""
    for c in chars:
        if freq[c] != min_freq:
            result += c

    if len(result) == 0:
        return 'empty'
    return result

if __name__ == '__main__':
    assert solve_method("abcdd") == "dd"
    assert solve_method("aabbccdd") == "empty"


from abc import ABC, abstractclassmethod
from app.models.model import DownloadModel
from concurrent.futures import ThreadPoolExecutor
import requests
import logging
import os
import tqdm
import sys

class ModelDownloadService(ABC):
    def __init__(
        self,
        download_models: list[DownloadModel],
        destination: str,
        chunk: int = 1024
    ) -> None:
        self.download_models = download_models
        self.destination = destination
        self.chunk = chunk

        if not os.path.isdir(self.destination):
            self._create_dir()

    def _get_model_dest_path(self, model: DownloadModel) -> str:
        if model.is_url_in_model_name:
            file_name = model.get_model_download_url().split('/')[-1]
            combined_path = os.path.join(self.destination, file_name)

        else:
            combined_path = os.path.join(self.destination, model.model_name)

        return combined_path

    def _save_model(self, model: DownloadModel) -> None:
        dest_path = self._get_model_dest_path(model)
        res = requests.get(model.download_model_url, stream=True)
        total_size_in_bytes = int(res.headers.get('content-length', 0))

        with tqdm.tqdm(total=int(total_size_in_bytes / self.chunk), unit='KB', unit_scale=True, file=sys.stdout, desc=model.model_name) as pbar:
            with open(dest_path, 'wb') as f:
                for data in res.iter_content(chunk_size=self.chunk):
                    f.write(data)
                    pbar.update(1)
                    f.flush()

    def _create_dir(self):
        try:
            os.mkdir(self.destination)

        except Exception as e:
            raise e

    @abstractclassmethod
    def run(self) -> None:
        pass

class ModelMultiThreadDownloadService(ModelDownloadService):
    def run(self) -> None:
        logging.basicConfig(level=logging.DEBUG, format='%(threadName)s: %(message)s')

        with ThreadPoolExecutor() as executor:
            for download_model in self.download_models:
                executor.submit(self._download_thread, download_model)

            executor.shutdown(wait=True)
        logging.debug('All tasks have been completed.')

    def _download_thread(self, model: DownloadModel):

        logging.debug(f'started {model.model_name} download.')
        self._save_model(model)
        logging.debug(f'Completed {model.model_name} download.')


from django.core.validators import MinValueValidator
from django.db import models
from django.db.models import UniqueConstraint

from users.models import User

class Ingredient(models.Model):
    """Модель для ингредиентов."""
    name = models.CharField(
        max_length=100,
        verbose_name='Название ингредиента',
        help_text='Укажите название ингредиента',
        null=False,
    )
    measurement_unit = models.CharField(
        max_length=100,
        verbose_name='Единица измерения',
        help_text='Укажите единицу измерения ингредиента',
        null=False,
    )

    class Meta:
        verbose_name = 'Ингредиент'
        verbose_name_plural = 'Ингредиенты'

    def __str__(self):
        return f'{self.name}, {self.measurement_unit}'

class Tag(models.Model):
    """Модель для тэгов блюд."""
    name = models.CharField(
        max_length=200,
        verbose_name='Название тега',
        null=False,
        unique=True
    )
    slug = models.SlugField(
        verbose_name='Ссылка',
        unique=True,
        help_text='Ссылка тега'
    )
    color = models.CharField(
        max_length=7,
        default='#ffffff',
        unique=True,
        verbose_name='Цвет тэга'
    )

    class Meta:
        verbose_name = 'Тэг'
        verbose_name_plural = 'Тэги'

    def __str__(self):
        return self.name

class Recipe(models.Model):
    """Модель для рецептов блюд."""
    author = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='recipes',
        verbose_name='Автор'
    )
    name = models.CharField(
        max_length=100,
        verbose_name='Название рецепта',
        help_text='Укажите название рецепта'
    )
    image = models.ImageField(
        upload_to='media/',
        verbose_name='Изображение блюда'
    )
    ingredients = models.ManyToManyField(
        Ingredient,
        related_name='ingredients',
        verbose_name='Ингредиенты'
    )
    text = models.TextField(
        verbose_name='Описание рецепта',
        help_text='Введите описание рецепта'
    )
    tags = models.ManyToManyField(
        Tag, related_name='tags',
        verbose_name='Тэг'
    )
    cooking_time = models.PositiveSmallIntegerField(
        default=1,
        validators=[MinValueValidator(1, 'Значение не может быть меньше 1')],
        verbose_name='Время готовки блюда в минутах',
    )

    class Meta:
        verbose_name = 'Рецепт'
        verbose_name_plural = 'Рецепты'

    def __str__(self):
        return self.name

class Favorite(models.Model):
    """Модель для избранного."""
    user = models.ForeignKey(
        User,
        related_name='favorites',
        on_delete=models.CASCADE
    )
    recipe = models.ForeignKey(
        Recipe,
        related_name='favorites',
        on_delete=models.CASCADE
    )
    added = models.DateTimeField(
        auto_now_add=True,
        verbose_name='Дата добавления'
    )

    class Meta:
        verbose_name = 'Избранное'
        verbose_name_plural = 'Избранное'
        UniqueConstraint(
            fields=['recipe', 'user'],
            name='favorite_unique'
        )

    def __str__(self):
        return f'У {self.user} в избранном: {self.recipe.name}'

class ShoppingList(models.Model):
    """Модель для списка покупок."""
    user = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        related_name='shopping_list',
        verbose_name='Пользователь'
    )
    recipe = models.ForeignKey(
        Recipe,
        on_delete=models.CASCADE,
        related_name='purchases',
        verbose_name='Покупка'
    )
    added = models.DateTimeField(
        auto_now_add=True,
        verbose_name='Дата добавления'
    )

    class Meta:
        verbose_name = 'Список покупок'
        verbose_name_plural = 'Список покупок'

    def __str__(self):
        return f'У {self.user} список покупок: {self.recipe}'

class AmountOfIngredient(models.Model):
    """Модель для количества ингредиентов."""
    ingredient = models.ForeignKey(
        Ingredient,
        on_delete=models.CASCADE,
        related_name='ingredients_in_recipe',
        verbose_name='Ингредиент'
    )
    recipe = models.ForeignKey(
        Recipe,
        on_delete=models.CASCADE,
        related_name='recipes_ingredients_list',
        verbose_name='Рецепт'
    )
    amount = models.PositiveSmallIntegerField(
        default=1,
        validators=[MinValueValidator(1)],
        verbose_name='Количество ингредиентов'
    )

    class Meta:
        verbose_name = 'Количество ингредиентов'
        verbose_name_plural = 'Количество ингредиентов'

    def __str__(self):
        return f'{self.ingredient} в {self.recipe}'


INPUT_EXAMPLE = "FBFBBFFRLR"

def day5a(filename: str = "a.input.txt"):
    maximum = 0
    with open(filename, "r") as f:
        lines = f.read().splitlines()
        for line in lines:
            row = int(
                line[:7].replace("F", "0").replace("B", "1"), 2
            )  # parse binary number
            col = int(line[-3:].replace("L", "0").replace("R", "1"), 2)
            maximum = max(maximum, row * 8 + col)

    print(f"maximum seat ID is: {maximum}")

def day5b(filename: str = "b.input.txt"):
    listIDs: list[int] = []
    with open(filename, "r") as f:
        lines = f.read().splitlines()
        for line in lines:
            row = int(
                line[:7].replace("F", "0").replace("B", "1"), 2
            )  # parse binary number
            col = int(line[-3:].replace("L", "0").replace("R", "1"), 2)
            id = row * 8 + col
            listIDs.append(id)
    expected_total = sum(range(min(listIDs), max(listIDs) + 1))
    total = sum([s for s in listIDs])
    print("solution for part 2:", expected_total - total)

def main():
    day5a()
    day5b()

if __name__ == "__main__":
    main()


import streamlit as st
import numpy as np

def create_dataset(data, n_steps):
    X, y = [], []
    if len(data.shape) == 1:
        # Handle 1D input array
        for i in range(len(data) - n_steps + 1):
            X.append(data[i:i + n_steps])
            y.append(data[i + n_steps - 1])
    else:
        # Handle 2D input array
        for i in range(len(data) - n_steps + 1):
            X.append(data[i:i + n_steps, :-1])
            y.append(data[i + n_steps - 1, -1])
    return np.array(X), np.array(y)


from django.conf.urls import url,patterns
from . import views
# urlpatterns = patterns('booktest.views',(r'^$','index'),)
urlpatterns = [
    url(r'^$',views.index),
    url(r'^get1/$', views.get1),
    url(r'^get2/$', views.get2),
    url(r'^get3/$', views.get3),
    url(r'^post1/$', views.post1),
    url(r'^post2/$', views.post2),
    url(r'^red1/$', views.red1),
    url(r'^cookie_set/$',views.cookie_set),
    url(r'^cookie_get/$',views.cookie_get),
    url(r'^session_test/$',views.session_test),
    url(r'^session_get/$',views.session_get),
    url(r'^session_delete/$',views.session_delete),
    url(r'^json1/$', views.json1),
    url(r'^json2/$', views.json2),
    url(r'^delete(\d+)/', views.delete),
    url(r'^create/$', views.create),
    url(r'^(?P<id1>\d+)/(?P<id2>\d+)/(?P<id3>\d+)/$', views.show1),
]


# Tarea 1.28 Crear una lista con los días hábiles de la semana
#  y otra lista con el sábado y el domingo
#  Extender la primera lista con la segunda
#  Mostrar el contenido de la lista con un único print

lista = ["Lunes","Martes","Miercoles","Jueves","Viernes"]
lista2= ["Sabado","Domingo"]

sabado = lista2.pop(0)
domingo = lista2.pop(0)

lista.append(sabado)
lista.append(domingo)

print("\nLista")
print(lista)


class Solution:
    #given a list of integers
    #return an integer
    def removeDuplicates(self, A):
        num = 0
        for i in range(1, len(A)):
            if A[i] == A[i-1-num]:
                num += 1
            A[i-num] = A[i]
        return len(A) - num


# coding: utf-8

# In[17]:

try:
    import unicodecsv
except:
    get_ipython().system(u'pip install unicodecsv')
    import unicodecsv

# In[18]:

try:
    import numpy
except:
    get_ipython().system(u'pip install numpy')
    import numpy

# In[19]:

try:
    import nltk
except:
    get_ipython().system(u'pip install nltk')
    import nltk

# In[20]:

import pandas as pd
import numpy as np
import time
from sklearn.model_selection import cross_val_predict
from sklearn import linear_model
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.feature_extraction.text import TfidfVectorizer

# In[21]:

print("We here")
time1 = time.clock()
x_dataset = pd.read_csv("bitcoin_dataset.csv", delimiter=",", dtype=str, header=0, usecols=["Date"]).values
y_dataset = pd.read_csv("bitcoin_dataset.csv", delimiter=",", dtype=str, header=0, usecols=["dervative"]).values
date_headlines = pd.read_csv("abcnews-date-text.csv", delimiter=",", dtype=str, header=0, usecols=["publish_date"]).values
text_headlines = pd.read_csv("abcnews-date-text.csv", delimiter=",", dtype=str, header=0, usecols=["headline_text"]).values
time2 = time.clock()
print("Loading training set inputs took " + str(time2-time1) + " seconds")

x_dataset = x_dataset.tolist()
y_dataset = y_dataset.tolist()
date_headlines = date_headlines.tolist()
text_headlines = text_headlines.tolist()

# In[ ]:

for i in range(len(x_dataset)):
    string = str(x_dataset[i])
    string = string.replace("-","")
    x_dataset[i] = string[2:10]

# In[ ]:

for j in range(len(date_headlines)):
    string = str(date_headlines[j])
    string = string.replace("'","")
    string = string.replace("[","")
    string = string.replace("]","")
    date_headlines[j] = string
print(date_headlines[0])

# In[6]:

for k in range(len(text_headlines)):
    string = str(text_headlines[k])
    string = string.replace("'","")
    string = string.replace("[","")
    string = string.replace("]","")
    text_headlines[k] = string
print(text_headlines[0])

# In[7]:

j = 0
time1 = time.clock()
for f in x_dataset:
    stuff = ' '
    i = 0
    for m in date_headlines:
        if (m == f):
            stuff += text_headlines[i]
            stuff += ' '
        i = i + 1
    x_dataset[j] = stuff
    j = j + 1
time2 = time.clock()
print("Loading this took " + str(time2-time1) + " seconds")

# In[8]:

y_dataset[0] = 0
y_dataset[2605] = 0.5
for r in range(len(y_dataset)):
    string = str(y_dataset[r])
    string = string.replace("'","")
    string = string.replace("[","")
    string = string.replace("]","")
    y_dataset[r] = float(string)

# In[9]:

for r in range(len(y_dataset)):
    #Stability
    if(y_dataset[r] > -0.33 and y_dataset[r] < 0.53):
        y_dataset[r] = 2
        print(2)
    #Low
    if(y_dataset[r] < 2.50 and y_dataset[r] >= 0.53):
        y_dataset[r] = 3
        print(3)
    if(y_dataset[r] >= 2.50):
        y_dataset[r] = 4
        print(4)
    if(y_dataset[r] <= -1.91):
        y_dataset[r] = 0
        print(0)
    if(y_dataset[r] <= -0.33 and y_dataset[r] > -1.91):
        y_dataset[r] = 1
        print(1)

# In[10]:

def create_vectorizer():
    # Arguments here are tweaked for working with a particular data set.
    # All that's really needed is the input argument.
    return TfidfVectorizer(input='content', max_features=200,
                           max_df=0.05,
                           stop_words='english')

# In[11]:

x_dataset_try = x_dataset
vectorizer = create_vectorizer()
tfidf_result = vectorizer.fit_transform(x_dataset_try)

# In[12]:

print(y_dataset[7] == 4)
print(y_dataset[45])
print(len(y_dataset))

# In[13]:

n = 0
j = 0
for i in range(len(y_dataset)):
    if(y_dataset[j] == 3):
        n = n + 1
    j = j + 1
print(n)

# In[14]:

from sklearn.neural_network import MLPClassifier
MLP = MLPClassifier(random_state=1, max_iter=1000, learning_rate='adaptive')
X_train, X_test, y_train, y_test = train_test_split(tfidf_result, y_dataset, train_size=0.7, test_size=0.3)

# In[15]:

MLP.fit(X_train,y_train)
score = MLP.score(X_test,y_test) * 100
print("Test score: %.4f" % score)

# In[16]:

xtfidf2 = vectorizer.transform(np.array(['australia crisis senate concern problem high rates']))
MLP.predict(xtfidf2)


from django.db import models
from django.db.models import Avg, Count
from django.urls import reverse
from django.contrib.auth.models import User

class Recipe(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    name = models.CharField(max_length=200, null=True)
    CATEGORY_CHOICES = [
        ('Breakfast', 'Breakfast'),
        ('Lunch', 'Lunch'),
        ('Dinner', 'Dinner'),
        ('Snack', 'Snack'),
        ('Sweets', 'Sweets'),
    ]
    category = models.CharField(max_length=20, choices=CATEGORY_CHOICES, null=True)
    APPLIANCE_CHOICES = [
       ('Air Fryer', 'Air Fryer'),
       ('Microwave', 'Microwave'),
       ('Oven', 'Oven'),
       ('Pressure Cooker', 'Pressure Cooker'),
       ('Slow Cooker', 'Slow Cooker'),
       ('Stove', 'Stove'),
    ]
    appliance = models.CharField(max_length=20, choices=APPLIANCE_CHOICES, null=True)
    description = models.TextField(max_length=2000, null=True)
    time = models.IntegerField(null=True)
    servings = models.IntegerField(null=True)
    ingredients = models.TextField(max_length=3000, null=True)
    directions = models.TextField(max_length=3000, null=True)
    bookmarks = models.ManyToManyField(User, related_name='bookmarked_recipes', blank=True)

    def average_rating(self):
       return self.review_set.aggregate(Avg('rating'))['rating__avg']

    def total_reviews(self):
       return self.review_set.count()

    def __str__(self):
      return f'{self.name} ({self.id})'

    def get_absolute_url(self):
      return reverse('recipe_detail', kwargs={'pk': self.id})

class Review(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    content = models.TextField(max_length=2000)
    RATING_CHOICES = [
      (1, '1'),
      (2, '2'),
      (3, '3'),
      (4, '4'),
      (5, '5'),
    ]
    rating = models.IntegerField(choices=RATING_CHOICES, default=5)
    recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)

    def __str__(self):
        return f'Review by {self.user.username}'

class Photo(models.Model):
    url = models.CharField(max_length=500)
    recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)


# import sys
# input = sys.stdin.readline
#
# G = int(input())
# P = int(input())
#
# gates = [False for _ in range(G+1)]
# for i in range(P):
#     t = int(input())
#     for j in range(t, 0, -1):
#         if not gates[j]:
#             gates[j] = True
#             break
#         elif j == 1:
#             print(i)
#             exit()
# print(P)

import sys
input = sys.stdin.readline

def find(node):
    if node == parent[node]:
        return node
    p = find(parent[node])
    parent[node] = p
    return p

def union(a, b):
    pa = find(a)
    pb = find(b)

    if pa != pb:
        parent[pa] = pb

G = int(input())
P = int(input())

parent = [i for i in range(G+1)]
for i in range(P):
    t = int(input())
    pt = find(t)
    if pt == 0:
        print(i)
        exit()

    union(pt, pt-1)
print(P)


"""

Created by Ali Hajian

"""

import segyio
from time import time
import numpy as np

# class to create SGY reader objects
class SGY_Reader():
    # need to pass the file name for initialization
    def __init__(self, file_name):
        self.file_name = file_name
        with segyio.open(self.file_name) as src:
            self.ilines = list(src.ilines)
            self.xlines = list(src.xlines)
            self.trace_len = src.trace[0].size
            self.depths = range(self.trace_len)

    # function to select iline slices and load them from the sgy file into a numpy array
    def get_iline_slices(self, ilines):
        t = time()
        # make sure ilines are iterables
        try:
            n_ilines = len(ilines)
        except:
            ilines = [ilines]
            n_ilines = len(ilines)

        # make sure all ilines are valid
        for i in range(n_ilines):
            if ilines[i] not in self.ilines:
                print("iline %i is not valid" %ilines[i])
                return None

        # open and read the iline slices from the SGY file
        with segyio.open(self.file_name) as src:
            if n_ilines > 1:
                slice_list = np.empty(
                    (n_ilines, len(self.xlines), self.trace_len),
                    dtype=np.float32
                )
            for i in range(n_ilines):
                if n_ilines > 1:
                    slice_list[i] = src.iline[ilines[i]]
                else:
                    slice_list = src.iline[ilines[i]]

        print('--> %i iline slices from %s loaded in %.2f sec' %(n_ilines,
                                                                 self.file_name,
                                                                 time()-t))

        return slice_list

    # function to select xline slices and load them from the sgy file into a numpy array
    def get_xline_slices(self, xlines):
        t = time()
        # make sure xlines are iterables
        try:
            n_xlines = len(xlines)
        except:
            xlines = [xlines]
            n_xlines = len(xlines)

        # make sure all xlines are valid
        for i in range(n_xlines):
            if xlines[i] not in self.xlines:
                print("xline %i is not valid" %xlines[i])
                return None

        # open and read the xline slices from the SGY file
        with segyio.open(self.file_name) as src:
            if n_xlines > 1:
                slice_list = np.empty(
                    (n_xlines, len(self.ilines), self.trace_len),
                    dtype=np.float32
                )
            for i in range(n_xlines):
                if n_xlines > 1:
                    slice_list[i] = src.xline[xlines[i]]
                else:
                    slice_list = src.xline[xlines[i]]

        print('--> %i xline slices from %s loaded in %.2f sec' %(n_xlines,
                                                                 self.file_name,
                                                                 time()-t))

        return slice_list

    # function to select depth slices and load them from the sgy file into a numpy array
    def get_depth_slices(self, depths):
        t = time()
        # make sure depths are iterables
        try:
            n_depths = len(depths)
        except:
            depths = [depths]
            n_depths = len(depths)

        # make sure all depths are valid
        for i in range(n_depths):
            if depths[i] not in self.depths:
                print("depth %i is not valid" %depths[i])
                return None

        # open and read the depth slices from the SGY file
        with segyio.open(self.file_name) as src:
            if n_depths > 1:
                slice_list = np.empty(
                    (n_depths, len(self.ilines), len(self.xlines)),
                    dtype=np.float32
                )
            for i in range(n_depths):
                if n_depths > 1:
                    slice_list[i] = src.depth_slice[depths[i]]
                else:
                    slice_list = src.depth_slice[depths[i]]

        print('--> %i depth slices from %s loaded in %.2f sec' %(n_depths,
                                                                 self.file_name,
                                                                 time()-t))

        return slice_list

    # function to select traces and load them from the sgy file into a numpy array
    def get_traces(self, ilines, xlines):
        t = time()
        # make sure ilines and xlines are iterables
        try:
            n_ilines = len(ilines)
        except:
            ilines = [ilines]
            n_ilines = len(ilines)
        try:
            n_xlines = len(xlines)
        except:
            xlines = [xlines]
            n_xlines = len(xlines)

        # make sure ilines and xlines have the same size
        if n_ilines == n_xlines:
            n_traces = n_ilines
        else:
            print("Number of ilines and xlines need to be the same.")
            return None

        # make sure all ilines and xlines are valid
        for i in range(n_traces):
            if ilines[i] not in self.ilines:
                print("iline %i is not valid" %ilines[i])
                return None
            if xlines[i] not in self.xlines:
                print("xline %i is not valid" %xlines[i])
                return None

        # open and read the traces from the SGY file
        with segyio.open(self.file_name) as src:
            if n_traces > 1:
                trace_list = np.empty((n_traces, self.trace_len), dtype=np.float32)
            for i in range(n_traces):
                if n_traces > 1:
                    trace_list[i] = src.gather[ilines[i], xlines[i]]
                else:
                    trace_list = src.gather[ilines[i], xlines[i]][0]

        print('--> %i traces from %s loaded in %.2f sec' %(n_traces, self.file_name, time()-t))

        return trace_list

    # function to select slices or traces and load them from the sgy file into a numpy array
    def get_data(self, ilines=None, xlines=None, depths=None):
        if depths is not None:
            if ilines or xlines:
                print("Too many arguments. Can not use depths " \
                       "and ilines xlines at the same time.")
                return None
            else:
                # get depth slices
                data = self.get_depth_slices(depths)
        elif ilines is not None:
            if xlines is not None:
                # get the traces at the intersection of inlines and xlines
                data = self.get_traces(ilines, xlines)
            else:
                # get iline slices
                data = self.get_iline_slices(ilines)
        elif xlines is not None:
            # get xline slices
            data = self.get_xline_slices(xlines)
        else:
            print("Need to pass at least one of ilines or xlines or depths.")
            return None

        return data


import npx
import numpy as np
from numpy.typing import ArrayLike

from ..illuminants import whitepoints_cie1931
from ._color_space import ColorSpace
from ._helpers import register

class RLAB(ColorSpace):
    """
    Mark D. Fairchild,
    Refinement of the RLAB Color Space,
    1996,
    <https://doi.org/10.1002/(SICI)1520-6378(199610)21:5<338::AID-COL3>3.0.CO;2-Z>.

    See also
    Mark D. Fairchild,
    Color Appearance Models, Second Edition,
    <https://last.hit.bme.hu/download/firtha/video/Colorimetry/Fairchild_M._Color_appearance_models__2005.pdf>.

    and

    Mark D. Fairchild,
    RLAB: A Color Appearance Space for Color Reproduction,
    Device-Independent Color Imaging and Imaging Systems Integration,
    1993, Proceedings Volume 1909,
    <https://doi.org/10.1117/12.149061>

    for the original RLAB. (This implementation uses the "refined RLAB" scheme.)
    """

    name = "RLAB"
    labels = ("LR", "aR", "bR")
    k0 = 0

    def __init__(
        self,
        Y_n: float = 318.0,
        D: float = 0.0,
        whitepoint: ArrayLike = whitepoints_cie1931["D65"],
        sigma: float = 1.0 / 2.3,
    ):
        # One purpose of RLAB is to account for the adaptation in the human visual
        # system. That is, the visual system sees a red apple no matter if it is looked
        # at in bright daylight, at dawn, or in the light of a fire.  To achieve this,
        # RLAB first converts a tristimulus value XYZ into a tristimulus value under the
        # reference illuminant D65.

        # <https://en.wikipedia.org/wiki/LMS_color_space#Hunt,_RLAB>.
        # M_HPE, normalized to equal-energy illuminant:
        M = np.array(
            [[0.3897, 0.6890, -0.0787], [-0.2298, 1.1834, 0.0464], [0.0, 0.0, 1.0]]
        )

        self.whitepoint = np.asarray(whitepoint)

        lms_n = M @ self.whitepoint
        lms_e = 3.0 * lms_n / np.sum(lms_n)
        Yn3 = Y_n ** (1 / 3)
        p_lms = (1.0 + Yn3 + lms_e) / (1.0 + Yn3 + 1.0 / lms_e)
        a_lms = (p_lms + D * (1 - p_lms)) / lms_n

        # reference stimulus
        # The matrix R below can be computed explicitly with
        #
        #   wp = whitepoints_cie1931["D65"]
        #   Y_n = 318.0
        #   D = 0.0
        #   #
        #   lms_n = M @ wp
        #   lms_e = 3 * lms_n / np.sum(lms_n)
        #   Yn3 = Y_n ** (1 / 3)
        #   p_lms = (1 + Yn3 + lms_e) / (1 + Yn3 + 1 / lms_e)
        #   a_lms = (p_lms + D * (1 - p_lms)) / lms_n
        #   # D65-normalized Hunt-Pointer-Estevez matrix:
        #   M = np.array([
        #       [0.4002, 0.7076, -0.0808],
        #       [-0.2263, 1.1653, 0.0457],
        #       [0.0, 0.0, 0.9182]
        #   ])
        #   R = np.diag(1 / wp) @ np.linalg.inv(M) @ np.diag(1 / a_lms)
        #
        # In the remainder of model, however, one uses the equal-energy normalized
        # matrix. This seems to be a mistake in the model.
        # TODO M looks like it already is the inverse of another matrix given to
        # some digits. Find out which.
        S = np.array(
            [
                [1.03565229, -0.05781178, 0.02215949],
                [-0.0060366, 1.00482272, 0.00121388],
                [0.0, 0.0, 1.0],
            ]
        )
        # R = np.array(
        #     [[1.9569, -1.1882, 0.2313], [0.3612, 0.6388, 0.0], [0.0, 0.0, 1.0]]
        # )
        # R = S @ np.linalg.inv(M)

        self.sigma = sigma

        self.A = S @ np.linalg.solve(M, (a_lms * M.T).T)
        self.Ainv = np.linalg.solve(M, ((M @ np.linalg.inv(S)).T / a_lms).T)

    # Y_n is the absolute luminance of the adapting stimulus (typically a stimulus that
    # appears white in the image) in cd/m2 (=nit). Most laptop displays have a
    # brightness of 300 to 400 nits (2019).
    #
    # The D factor allows various proportions of cognitive discounting-the-illuminant. D
    # should be set equal to 1.0 for hard-copy images, 0.0 for soft-copy displays, and
    # an intermediate value for situations such as projected transparencies in
    # completely darkened rooms.
    #
    # The exponents in Equations 13.15–13.17 vary, depending on the relative luminance
    # of the surround. For an average surround sigma = 1/2.3, for a dim surround sigma =
    # 1/2.9, and for a dark surround sigma = 1/3.5.
    def from_xyz100(self, xyz: ArrayLike) -> np.ndarray:
        # First, the stimuli xyz are translated into reference stimuli xyz_ref to
        # account for the environment adaptation of the human visual system.
        xyz_ref = npx.dot(self.A, xyz)

        x_ref_s, y_ref_s, z_ref_s = xyz_ref**self.sigma

        # LR represents an achromatic response analogous to CIELAB L*. The red-green
        # chromatic response is given by a_R (analogous to CIELAB a*) and the
        # yellow–blue chromatic response is given by b_R (analogous to CIELAB b*).
        L_R = 100 * y_ref_s
        a_R = 430 * (x_ref_s - y_ref_s)
        b_R = 170 * (y_ref_s - z_ref_s)

        return np.array([L_R, a_R, b_R])

    def to_xyz100(self, lab: ArrayLike) -> np.ndarray:
        L_R, a_R, b_R = np.asarray(lab)

        y_ref_s = L_R / 100
        x_ref_s = a_R / 430 + y_ref_s
        z_ref_s = y_ref_s - b_R / 170

        xyz_ref = np.array([x_ref_s, y_ref_s, z_ref_s]) ** (1.0 / self.sigma)
        return npx.dot(self.Ainv, xyz_ref)

register("rlab", RLAB())


import datetime
import heapq
import sys

import networkx as nx
import numpy as np
import pandas as pd
import pickle
import json
import joblib

def init_real_graph():
    data = pd.read_csv('./data.csv')
    data = data.iloc[:, 1:]
    data = data.to_numpy()
    G = nx.Graph()
    for weight in data:
        G.add_edge(int(weight[0]), int(weight[1]), weight=weight[2])
    G.add_edge(0, 7388, weight=1.4108)

    return G

class PriorityQueue:
    def __init__(self):
        self.queue = []
        self.index = 0

    def __iter__(self):
        self.a = 1
        return self

    def __next__(self):
        x = self.a
        self.a += 1
        return x

    def empty(self):
        return len(self.queue) == 0  # if Q is empty

    def put(self, item, priority):
        heapq.heappush(self.queue, (priority, self.index, item))  # reorder x using priority
        self.index += 1

    def get(self):
        return heapq.heappop(self.queue)[-1]  # pop out element with smallest priority

class Label:
    def __init__(self, start, end):
        self.start = start
        self.end = end
        # 暂定为0
        self.timeList = [0]
        self.routes = [start]
        self.time = 0
        self.car_time = 0

def init_labels(query_num, graph_size):
    labels = []
    for i in range(query_num):
        start = np.random.randint(0, graph_size)
        end = np.random.randint(0, graph_size)
        if start == end:
            continue
        label = Label(start, end)
        labels.append(label)
    return labels

def get_flow(init_time, init_node, edge_dict, node):
    car_num = 0
    if (init_node, node) in edge_dict:
        for i in edge_dict[(init_node, node)]:
            if i[0] < init_time < i[1]:
                car_num += 1
    return car_num

def show_answer(res):
    total_cost = 0
    for label in res:
        total_cost += label.timeList[-1] - label.timeList[0]

def greedy_algorithms(labels, G, distances, alpha, edge_dict):
    global car_end, car_end
    pq = PriorityQueue()
    res = []
    for label in labels:
        pq.put(label, label.timeList[-1])

    while not pq.empty():
        label = pq.get()
        nodes = list(G.neighbors(label.routes[-1]))

        EGT_tmp = sys.maxsize
        init_time = label.timeList[-1]
        init_node = label.routes[-1]
        end_time = 0
        end_node = 0
        time_start = datetime.datetime.now()
        for node in nodes:
            # if distances.get(node) is not None and distances.get(init_node) is not None:
            if node not in label.routes and distances[node][label.end] < distances[init_node][label.end]:

                car_start_time = datetime.datetime.now()
                car_num = get_flow(init_time, init_node, edge_dict, node)
                car_end_time = datetime.datetime.now()

                label.car_time += (car_end_time - car_start_time).microseconds
                time_tmp = G[init_node][node]["weight"] * (1 + alpha * car_num)
                EGT = init_time - label.timeList[0] + time_tmp + distances[node][init_node]
                if EGT_tmp == sys.maxsize and EGT < EGT_tmp:
                    label.routes.append(node)
                    label.timeList.append(init_time + time_tmp)
                    end_node = node
                    end_time = init_time + time_tmp
                    EGT_tmp = EGT
                elif EGT_tmp != sys.maxsize and EGT < EGT_tmp:
                    label.routes[-1] = node
                    label.timeList[-1] = init_time + time_tmp
                    end_node = node
                    end_time = init_time + time_tmp
                    EGT_tmp = EGT
        time_end = datetime.datetime.now()

        # calculate time
        label.time += (time_end - time_start).microseconds
        if end_node != 0 and end_time != 0:
            if (init_node, end_node) in edge_dict:
                edge_dict[(init_node, end_node)].append([init_time, end_time])
            else:
                edge_dict[(init_node, end_node)] = [[init_time, end_time]]

        if label.routes[-1] == label.end:
            res.append(label)

        else:
            pq.put(label, label.timeList[-1])
    return res

def refining_algorithm(combination, G, distances, rate):
    pass

# def save_dict(filename, dic):
#     """save dict into json file"""
#     with open(filename, 'w') as json_file:
#         json.dump(dic, json_file, ensure_ascii=False)
#
#
# def load_dict(filename):
#     """load dict from json file"""
#     with open(filename, "r") as json_file:
#         dic = json.load(json_file)
#     return dic

def main():
    # 先设置场数，例如query_num等, graph_num=18262, 真实图中的点为18262
    query_num = 2000
    graph_size = 18262
    alpha = 0.02
    rate = 0.02

    G = init_real_graph()
    distances = dict(nx.all_pairs_dijkstra_path_length(G, weight="weight"))
    with open('distances.pkl', 'wb') as f:
        joblib.dump(distances, f, pickle.HIGHEST_PROTOCOL)

    start = datetime.datetime.now()
    with open('distances.pkl', 'rb') as f:
        distances = pickle.load(f)
        print(type(distances))

    end = datetime.datetime.now()
    print((end - start).seconds)
    # # save_dict('distances', distances)
    # distances = load_dict('distances')

    labels = init_labels(query_num=query_num, graph_size=graph_size)

    edge_dict = {}

    # 计算算法一时间和cost
    start_time = datetime.datetime.now()
    res = greedy_algorithms(labels, G, distances, alpha, edge_dict)
    end_time = datetime.datetime.now()
    print("time cost1" + str((end_time - start_time).seconds))
    print("cost1" + str(show_answer(res)))
    for i in range(len(res)):
        print(res[i].time)
        print("计算汽车时间" + str(res[i].car_time))

    # 计算算法二时间和cost
    # start_time = datetime.datetime.now()
    # combination2 = refining_algorithm(res, G, distances, rate)
    # end_time = datetime.datetime.now()
    # print("time cost2" + str((end_time - start_time).seconds))
    # print("cost2" + str(show_answer(res)))

if __name__ == '__main__':
    main()


from ball import *
import random as rd

class Simulation:
    def __init__(self, numBalls, screenSize, time, screen):
        self.time = time
        self.numBalls = numBalls
        self.screenSize = screenSize
        self.screen = screen
        self.balls = []
        self.colors = [(0,0,255), (255,255,0), (255,0,0), (0,128,0)]
        self.createBalls()

    def checkPosition(self, radius, x, y):
        for ball in self.balls:
            distance = math.sqrt((ball.x - x)**2 + (ball.y - y)**2)
            if distance < ball.radius + radius:
                return False
        return True

    def createBalls(self):
        for _ in range(self.numBalls):
            radius = rd.randint(10, 50)
            while(True):
                x = rd.randint(radius, self.screenSize - radius)
                y = rd.randint(radius, self.screenSize - radius)
                if(self.checkPosition(radius, x, y)):
                    break
            speed_x = speed_y = 0
            while speed_x == 0 and speed_y == 0:
                speed_x = rd.randint(0, 10)
                speed_y = rd.randint(0, 10)
            mass = rd.randint(1, 30)
            color = self.colors[rd.randint(0, 3)]
            self.balls.append(Ball(x, y, radius, speed_x, speed_y, mass, color))

        self.drawBalls()

    def moveBalls(self):
        for ball in self.balls:
            ball.move()

    def checkCollisions(self):
        for i in range(self.numBalls-1):
            for j in range(i+1, self.numBalls):
                self.balls[i].checkCollision(self.balls[j], self.screenSize)

    def checkBounds(self):
        for ball in self.balls:
            ball.checkBounds(self.screenSize)

    def drawBalls(self):
        for ball in self.balls:
            ball.draw(self.screen)

    def isRunning(self):
        return self.time > 0

    def run(self):
        self.checkCollisions()
        self.checkBounds()
        self.moveBalls()
        self.drawBalls()
        self.time -= 1


import os
from typing import Tuple

import pandas as pd
import pandera as pa
from loguru import logger
from pandera.typing import DataFrame

from src.dataset.shema import (
    MoviesBaseSchema,
    MoviesRatingSchema,
    MoviesSchema,
    RatingsBaseSchema,
    TagsBaseSchema,
)
from src.models.dataset import Dataset

class DataLoader:
    def __init__(
        self,
        num_users: int = 1000,
        num_test_items: int = 5,
        data_path: str = "../data/ml-10m/ml-10M100K/",
    ):
        self.num_users = num_users
        self.num_test_items = num_test_items
        self.data_path = data_path

    def load_data(self) -> Dataset:
        """データを読み込み、Datasetに変換する

        Returns:
            Dataset: データセット
        """
        logger.info("Start load data")
        ratings, movie_content = self._load()
        movie_train, movie_test = self._split_data(ratings)

        movie_test_user2items = (
            movie_test[movie_test.rating >= 4]
            .groupby("user_id")
            .agg({"movie_id": list})["movie_id"]
            .to_dict()
        )

        return Dataset(
            train=movie_train,
            test=movie_test,
            test_user2items=movie_test_user2items,
            item_content=movie_content,
        )

    @pa.check_types
    def _split_data(
        self, movies: DataFrame[MoviesSchema]
    ) -> Tuple[DataFrame[MoviesSchema], DataFrame[MoviesSchema]]:
        """データを学習用とテスト用に分割する

        Args:
            movies (DataFrame[MoviesSchema]): 映画データ

        Returns:
            Tuple[DataFrame[MoviesSchema], DataFrame[MoviesSchema]]: 学習用データ、テスト用データ
        """
        logger.info("Start split data")

        # 学習用とテスト用にデータを分割する
        # 各ユーザーの直近の映画5件を評価用に使い、それ以外を学習用とする
        # まずは、それぞれのユーザーが評した映画の順序を計算する
        # 直近付与した映画から順番を付与していく（0始まり）
        movies["rating_order"] = movies.groupby("user_id")["timestamp"].rank(
            method="first", ascending=False
        )

        movie_train = movies[movies.rating_order > self.num_test_items]
        movie_train = MoviesSchema(movie_train)
        movie_test = movies[movies.rating_order <= self.num_test_items]
        movie_test = MoviesSchema(movie_test)

        return movie_train, movie_test

    @pa.check_types
    def _load(self) -> Tuple[DataFrame[MoviesRatingSchema], DataFrame[MoviesSchema]]:
        """データを読み込む

        Returns:
            Tuple[DataFrame[MoviesRatingSchema], DataFrame[MoviesSchema]]: 映画評価データ、映画データ
        """
        movies = self._load_movies()
        ratings = self._load_ratings()

        # データを結合する
        logger.info("merge ratings data")
        movies_ratings = ratings.merge(movies, on="movie_id")
        movies_ratings = MoviesRatingSchema(movies_ratings)

        logger.info(
            f"unique_users={len(movies_ratings.user_id.unique())}, unique_movies={len(movies_ratings.movie_id.unique())}"
        )

        return movies_ratings, movies

    @pa.check_types
    def _load_movies(self) -> DataFrame[MoviesSchema]:
        """映画データを読み込む

        Returns:
            DataFrame[MoviesSchema]: 映画データ
        """
        # 映画の情報の読み込み
        logger.info("load movies data")
        m_cols = ["movie_id", "title", "genres"]
        movies = pd.read_csv(
            os.path.join(self.data_path, "movies.dat"),
            names=m_cols,
            sep="::",
            encoding="latin-1",
            engine="python",
        )

        # genreをlistを形式で保持する
        movies["genres"] = movies.genres.apply(lambda x: x.split("|"))
        movies = MoviesBaseSchema(movies)

        # ユーザーがタグ付けした映画の情報の読み込み
        logger.info("load tags data")
        t_cols = ["user_id", "movie_id", "tag", "timestamp"]
        user_tagged_movies = pd.read_csv(
            os.path.join(self.data_path, "tags.dat"),
            names=t_cols,
            sep="::",
            engine="python",
        )
        # tagを小文字にする
        user_tagged_movies["tag"] = user_tagged_movies["tag"].str.lower()
        user_tagged_movies = TagsBaseSchema(user_tagged_movies)
        movie_tags = user_tagged_movies.groupby("movie_id").agg({"tag": list})

        # タグ情報を映画情報に結合する
        movies = movies.merge(movie_tags, on="movie_id", how="left")
        movies = MoviesSchema(movies)

        return movies

    @pa.check_types
    def _load_ratings(self) -> DataFrame[RatingsBaseSchema]:
        """映画評価データを読み込む

        Returns:
            DataFrame[RatingsBaseSchema]: 映画評価データ
        """
        # ユーザーの評価情報の読み込み
        r_cols = ["user_id", "movie_id", "rating", "timestamp"]
        ratings = pd.read_csv(
            os.path.join(self.data_path, "ratings.dat"),
            names=r_cols,
            sep="::",
            encoding="latin-1",
            engine="python",
        )
        # ユーザー数をnum_usersに制限する
        valid_user_ids = sorted(ratings.user_id.unique())[: self.num_users]
        ratings = ratings[ratings.user_id <= max(valid_user_ids)]
        ratings = RatingsBaseSchema(ratings)

        return ratings