import unittest
from ud_to_amr import ud_to_amr
import grew
import pprint
import os

pp = pprint.PrettyPrinter(indent = 4)

amr_roles = [':ARG0', ':ARG1', ':ARG2', ':ARG3', ':ARG4', ':ARG5',
			':accompanier', ':age', ':beneficiary',
			':concession', ':condition', ':consist-of',
			':degree', ':destination', ':direction', ':domain', ':duration',
			':example', ':extent', ':frequency', ':instrument', ':li', ':location',
			':manner', ':medium', ':mod', ':mode', ':name', ':ord',
			':part', ':path', ':polarity', ':polite', ':poss', ':purpose',
			':quant', ':range', ':scale', ':source', ':subevent',
			':time', ':topic', ':unit', ':value', ':wiki',
			':calendar', ':century', ':day', ':dayperiod', ':decade',
			':era', ':month', ':quarter', ':season', ':timezone', ':weekday', ':year', ':year2']
#need regex-es for :op1, :op2, ... :opN,:prep-* and :conj-*

grew.init()
# g = grew.graph( '''graph {
# 	W1 [ phon ="the", cat =DET ];
# 	W2 [ phon ="child", cat =N];
# 	W3 [ phon ="plays", cat =V];
# 	W4 [ phon ="the", cat =DET ];
# 	W5 [ phon ="fool", cat =N];
# 	W2 -[det ]->W1;
# 	W3 -[suj ]->W2;
# 	W3 -[obj ]->W5;
# 	W5 -[det ]->W4;
# }''')

class ParserTest(unittest.TestCase):

	def has_pos(self):
		'''every word should have a POS tag'''
		pass

	def has_lemma(self):
		'''every word should have a lemma'''
		pass

	def has_incoming_deprel(self):
		'''every word should have an incoming dependency relation'''
		pass

	def has_features(self):
		'''every word should have features'''
		pass

class UDtoAMRtest(unittest.TestCase):

	test_graphs = []
	grs_filename = './grs/grs_amr_main.grs'

	for filename in os.listdir("./data/dev_data/ud_graphs/"):
		if filename.endswith('.conll'):
			ud_graph = load_data("./data/dev_data/ud_graphs/"+filename)
			test_graphs.extend(ud_to_amr(grs_filename, ud_graph, strat="simple"))
	print(test_graphs)

	def test_has_no_iso(self):
		'''UD_to_AMR should produce graphs whithout isolated components'''
		for amr_graph in self.test_graphs:
			result = __test_no_iso__(amr_graph)
			self.assertEqual(1, result)

	def test_has_only_AMR_tags(self):
		'''UD_to_AMR should produce a graph which has only AMR tags'''
		for amr_graph in self.test_graphs:
			# pp.pprint(amr_graph)
			result = __test_only_AMR_tags__(amr_graph)
			self.assertEqual(1, result)

	# def test_every_edge_has_label(self):
	# 	'''UD_to_AMR should produce a graph in which every edge has a label'''
	# 	for amr_graph in self.test_graphs:
	# 		result = __every_edge_has_label__(amr_graph)
	# 		self.assertEqual(1, result)

def __test_no_iso__(amr_graph):
	"""
	Helper function for unit testing module. Takes a ud_graph, checks that it does not have isolated components

	input | amr_graph: dict - an AMR graph in GREW dictionary form
	output | int - 1 if yes, 0 if no
	"""
	node_tuples = __generate_nodetuples__(amr_graph)
	simple_graphrep = __generate_simple_graphrep__(node_tuples)

	# get all the nodes in the amr_graph with at least 1 connection
	nodes_all = set([i[0] for i in node_tuples] + [i[1] for i in node_tuples])

	# get all the head nodes present in the amr_graph (i.e. leaving out all the leaf nodes).
	nodes_heads = set([i for i in simple_graphrep.keys()])

	assert len(simple_graphrep["ROOT"]) == 1, "It appears this graph may have more than 1 root, \
	AMRs can only have a single root. Please check."
		# AMRs should only have 1 root node, so simple_graphrep["ROOT"] should return a list of one
		# element, which contains a str that is the index number for the root word.

	# start searching with "ROOT".
	nodes_to_visit = set(["ROOT"])
	nodes_visited = set()

	# recursion to visit all nodes accessible from "ROOT", stop when nodes_to_visit empty, or
	# if visited nodes matches the list of all nodes in the amr_graph (i.e. all nodes are connected).
	while len(nodes_to_visit) > 0:
		# .pop on a set removes a random element. but this does not matter, we only want to
		# be sure to have visited all head nodes once, so the order of visit does not matter.
		__visiting_node = nodes_to_visit.pop()

		for node1 in simple_graphrep[__visiting_node]:
			# check that node1 is not a leaf node before adding to nodes_to_visit.
			nodes_visited.add(node1)
			if node1 in nodes_heads:
				nodes_to_visit.add(node1)

		nodes_visited.add(__visiting_node)
		if nodes_visited == nodes_all:
			return 1
	return 0 # returns zero if we can't match nodes_visited with nodes_all

def __test_only_AMR_tags__(amr_graph):
	"""
	Helper function for unit testing. Takes an amr_graph, checks that none of the tags are UD tags

	input | amr_graph - an AMR graph in GREW dictionary format
	output | 1 if no UD tags, 0 if there are UD tags
	"""
	for node in amr_graph[0]:
		for relation in amr_graph[0][node][1]:
			if relation[0] in ud_tags:
				# print('UD TAAAAAGS ', relation[0])
				return 0
	return 1

def __generate_nodetuples__(amr_graph):
	'''
	Helper function for __test_is_dag__ and __test_no_iso__ functions. Takes an amr_graph in dictionary form and
	returns a list of all node-pairs in the graph. recall that amr graphs are directed, as
	such the tuples are internally ordered (head, dep)
	input | amr_graph: dict - an AMR graph in GREW dictionary form
	output | list - a list containing all the node pairs (i.e. connected by an edge) in amr_graph
	'''
	nodetuples = []
	# iterate through every word (represented by its index number) in the amr_graph
	for word_idx in amr_graph:
		# in the GREW graph representation, the internal structure for each word
		# is as follows: [{dictionary of features of the word},
		# [list of all dependents of the word[dependency relation, dependent word_idx]]]

		# get to list of all dependents of the word
		for dep in amr_graph[word_idx][1]:
			# get to word_idx for each dependent
			nodetuples.append((word_idx, dep[1]))

	return nodetuples

def __generate_simple_graphrep__(nodetuples):
	'''
	Helper function for __test_is_dag__ and __test_no_iso__ functions. Takes a list of node tuples and transforms
	them into a simplified graph representation. Necessary for the __test_no_iso__ function as the GREW graph dictionary
	transformations does not delete words that are not present in the AMR (so as to allow the surface representation
	of the original sentence to be recovered easily.)
	'''
	simple_graphrep = {}

	for nodetuple in nodetuples:
		head = nodetuple[0]
		dep = nodetuple[1]
		try:
			simple_graphrep[head].append(dep)
		except KeyError:
			simple_graphrep[head] = list(dep)

	return simple_graphrep

if __name__== "__main__":
	unittest.main()


from flask import Blueprint, render_template, request, flash, jsonify, redirect
from flask_login import login_required, current_user
from .models import Note
from . import db
import json

views = Blueprint('views', __name__)

@views.route('/', methods=['GET', 'POST'])
@login_required
def home():
    if request.method == 'POST':
        note = request.form.get('note')#Gets the note from the HTML

        if len(note) < 1:
            flash('Note is too short!', category='error')
        else:
            new_note = Note(data=note, user_id=current_user.id)  #providing the schema for the note
            db.session.add(new_note) #adding the note to the database
            db.session.commit()
            flash('Note added!', category='success')

    return render_template("home.html", user=current_user)

@views.route('/delete-note', methods=['POST'])
def delete_note():
    data = json.loads(request.data)
    noteId = data['noteId']
    note = Note.query.get(noteId)
    if note:
        if note.user_id == current_user.id:
            db.session.delete(note)
            db.session.commit()
            return jsonify({})

@views.route('/update/<int:id>', methods=['GET', 'POST'])
@login_required
def update(id):
    note = Note.query.get_or_404(id)

    if request.method == 'POST':
        note.data = request.form['content']

        try:
            db.session.commit()
            flash('Note updated!', category='success')
            return redirect('/')  # Redireciona para a página inicial após a atualização
        except:
            flash('Error updating note!', category='error')
            return redirect('/update/{}'.format(id))  # Redireciona de volta para a página de atualização em caso de erro
    else:
        return render_template('update.html', note=note, user=current_user)


import numpy as np
import pandas as pd
import torch.optim as optim
import torch.nn.functional as F
import zipfile
from mf_model import MFRecommender
import torch
from sklearn.model_selection import train_test_split
from torch.utils.data import DataLoader,TensorDataset
from torch.utils.data.dataset import random_split
from torchtext.data.functional import to_map_style_dataset
from torch import nn

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

X = df.loc[:,['UserId','ProductId']]
y = df.loc[:,'ratings']

# Split our data into training and test sets
X_train, X_val, y_train, y_val = train_test_split(X,y,random_state=0, test_size=0.2)

def prep_dataloaders(X_train,y_train,X_val,y_val,batch_size):
    # Convert training and test data to TensorDatasets
    trainset = TensorDataset(torch.from_numpy(np.array(X_train)).long(),
                            torch.from_numpy(np.array(y_train)).float())
    valset = TensorDataset(torch.from_numpy(np.array(X_val)).long(),
                            torch.from_numpy(np.array(y_val)).float())

    # Create Dataloaders for our training and test data to allow us to iterate over minibatches
    trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True)
    valloader = torch.utils.data.DataLoader(valset, batch_size=batch_size, shuffle=False)

    return trainloader, valloader

batchsize = 64
trainloader,valloader = prep_dataloaders(X_train,y_train,X_val,y_val,batchsize)

def train_model(model, criterion, optimizer, dataloaders, device, num_epochs=5, scheduler=None):
    model = model.to(device) # Send model to GPU if available
    since = time.time()

    costpaths = {'train':[],'val':[]}

    for epoch in range(num_epochs):
        print('Epoch {}/{}'.format(epoch, num_epochs - 1))
        print('-' * 10)

        # Each epoch has a training and validation phase
        for phase in ['train', 'val']:
            if phase == 'train':
                model.train()  # Set model to training mode
            else:
                model.eval()   # Set model to evaluate mode

            running_loss = 0.0

            # Get the inputs and labels, and send to GPU if available
            for (inputs,labels) in dataloaders[phase]:
                inputs = inputs.to(device)
                labels = labels.to(device)

                # Zero the weight gradients
                optimizer.zero_grad()

                # Forward pass to get outputs and calculate loss
                # Track gradient only for training data
                with torch.set_grad_enabled(phase == 'train'):
                    outputs = model.forward(inputs).view(-1)
                    loss = criterion(outputs, labels)

                    # Backpropagation to get the gradients with respect to each weight
                    # Only if in train
                    if phase == 'train':
                        loss.backward()
                        # Update the weights
                        optimizer.step()

                # Convert loss into a scalar and add it to running_loss
                running_loss += np.sqrt(loss.item()) * labels.size(0)

            # Step along learning rate scheduler when in train
            if (phase == 'train') and (scheduler is not None):
                scheduler.step()

            # Calculate and display average loss and accuracy for the epoch
            epoch_loss = running_loss / len(dataloaders[phase].dataset)
            costpaths[phase].append(epoch_loss)
            print('{} loss: {:.4f}'.format(phase, epoch_loss))

    time_elapsed = time.time() - since
    print('Training complete in {:.0f}m {:.0f}s'.format(
        time_elapsed // 60, time_elapsed % 60))

    return costpaths

dataloaders = {'train':trainloader, 'val':valloader}

n_users = X.loc[:,'userId'].max()+1 # ID value range of users
n_items = X.loc[:,'movieId'].max()+1 # ID value range of movies
model = PMFRecommender(n_users,n_items,embedding_dim=50,rating_range=[0.,5.])

criterion = nn.MSELoss()
lr=0.001
n_epochs=10
wd=1e-3
optimizer = optim.Adam(model.parameters(), lr=lr, weight_decay=wd)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

cost_paths = train_model(model,criterion,optimizer,dataloaders, device,n_epochs, scheduler=None)


from django.shortcuts import render
from datetime import datetime

# Create your views here.
def Django_time(request):
	msg="None"
	date=datetime.now()
	time=int(date.strftime("%H"))
	if time<12:
		msg="Good Morning"
	elif time<16:
		msg="Good Afternoon"
	elif time<20:
		msg="Good Evening"
	else:
		msg="Good Night"
	my_dict={"date":date,'msg':msg}
	return render(request,'fourthapp/display.html',context=my_dict)


# encoding:utf-8
import os
import shutil

def xmllist(i):
    file_path = "F:/jtjf/walk%04d.xml" % (i)
    file_handle=open(file_path, mode='w')
    file_handle.write('
 \n')

    addressPNG = "F:/FantasyWalker/第%d话/"%(i)

    f_list = os.listdir(addressPNG)
    n=0
    for fileNAME in f_list:

        if os.path.splitext(fileNAME)[1] == '.png':
            n += 1
            str1 = u"\t<id>%d</id><url>images/Chapter%d/%s</url> \n" %(n,i,fileNAME)
            #print(str1)
            #str2 = str1.encode('utf-8')
            #print(str2)
            file_handle.write("%s" %(str1))

    file_handle.write('
 \n')
    file_handle.close()
    return n

if __name__ == '__main__':

    for num in range(1,292):
        try:
            page = xmllist(num)
            if page!=8:
                print("第%d话 共%d页 内容非正常规格"%(num,page))
            else:
                print("第%d话 转换完成"%(num))
        except:
            print("第%d话 转换失败"%(num))
            break


class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x >= 0 and x < 10:
            return True
        if x < 0 or x%10 == 0:
            return False
        part = 0
        while x > part:
            part = part*10 + x%10
            x /= 10
            if x/10 == part:
                return True
        return x == part

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x < 0:
            return False
        s = str(x)
        mid = len(s)/2
        left = s[:mid]
        if len(s)&1:
            right = s[mid+1:]
        else:
            right = s[mid:]
        if left == right[::-1]:
            return True
        return False


from pymongo import MongoClient
import csv

client = MongoClient('mongodb://localhost:27017')
db = client.TravelReviews
collection = db.reviews

file = './Datasets/Travel Reviews/tripadvisor_review.csv'

# Open the file and read it
with open(file, 'r') as f:
    documents = []
    batch_size = 100000 # Number of documents to insert at a time
    batch_count = 0 # Number of batches inserted so far
    csv_reader = csv.DictReader(f, delimiter=',')

    for line in csv_reader:
        # Create a document with the following structure:
        # {"_id": n, "good_scores": ["A", "B", "C", ...]}
        # With n identifying the user and A, B, C, ... the good scores that the user gave
        # We arbitrarily consider that a score of 2.5 or more is a good score
        good_score_limit = 2.5
        document = {"items": [i for i, j in list(line.items())[1:] if float(j) >= good_score_limit]}

        documents.append(document)
        batch_count += 1

        # Insert the documents in batches (much faster than one at a time)
        if batch_count == batch_size:
            collection.insert_many(documents)
            batch_count = 0
            documents = []

# Close the connection
client.close()


nStates = 60
nActions = 3

arr = [0.5, 1, 2]
X = 29 #Team Number

states = []
actions = ['SHOOT', 'DODGE', 'RECHARGE']
transition_model = {}
gamma = 0.99
delta = 0.001
penalty = -10/arr[X % 3]
reward = {}

for i in range(0, 5):
    for j in range(0, 3):
        for k in range(0, 4):
            states.append((i, j, k))

for state in states:
    h, a, s = state
    reward[state] = penalty if h != 0 else 10

for state in states:
    """For each of the states in the Markov Decision Process, we assign
    the transition pobabilities to the next possible states according
    to the rules given in the question
    """
    transition = [[], [], []]
    h, a, s = state
    if a > 0:
        if s - 1 >= 0 and h - 1 >= 0:
            transition[0].append((0.5, (h-1, a-1, s-1)))
        if s - 1 >= 0:
            transition[0].append((0.5, (h, a-1, s-1)))
    if s == 2:
        if a + 1 <= 2:
            transition[1].append((0.64, (h, a+1, s-1)))
            transition[1].append((0.16, (h, a+1, s-2)))
        transition[1].append((0.16, (h, a, s-1)))
        transition[1].append((0.04, (h, a, s-2)))
    if s == 1:
        if a + 1 <= 2:
            transition[1].append((0.8, (h, a+1, s-1)))
        transition[1].append((0.2, (h, a, s-1)))
    if s + 1 <= 3:
        transition[2].append((0.8, (h, a, s+1)))
    transition[2].append((0.2, (h, a, s)))

    transition_model[state] = transition

def print_transition_model():
    """For each state of the Markov Decision Process environment, the
    transition model is of the form ACTION: (probability, state), <(probability, state), ...>
    """
    for key, val in transition_model.items():
        print("State: " + str(key))
        print("SHOOT: " + str(val[0]))
        print("DODGE: " + str(val[1]))
        print("RECHARGE: " + str(val[2]) + "\n")

def value_iteration():
    count = 0
    U1 = dict([(s, 0) for s in states])
    R, T, G = reward, transition_model, gamma
    while True:
        print("iteration="+str(count))
        count = count + 1
        U = U1.copy()
        diff = 0
        for s in states:
            U1[s] = R[s] + G * max([sum([p * U[s1] for p, s1 in T[s][a]]) for a in range(0, 3)])
            diff = max(diff, abs(U1[s] - U[s]))
        if diff < delta * (1 - G) / G:
            return U

value_iteration()

Assignment 02B: Value Iteration in Reinforcement Learning/task_1.py


from typing import Dict, Any, Optional

import yaml

StrategyParams = Optional[Dict[str, Any]]

class Config:

    def __init__(self, config_path: str):

        try:
            stream = open(config_path, 'r')  # 'config.yaml' contains a single YAML document.
        except FileNotFoundError:
            raise ValueError(f'Config file missing. Make sure "{config_path}" is present')

        try:
            args = yaml.safe_load(stream)
        except Exception:
            raise ValueError(f'There was a problem parsing the configuration file {config_path}. Make sure syntax is '
                             f'appropriate')

        if 'level' not in args or 'strategy' not in args or 'name' not in args['strategy']:
            raise ValueError(f'There are arguments missing. Make sure "level" and "strategy: name" are present')

        self.level: str = args['level']
        self.strategy: str = args['strategy']['name']
        self.strategy_params: StrategyParams = args['strategy'].get('params', None)
        self.render: bool = args.get('render', True)

    def __repr__(self) -> str:
        return f'Level: {repr(self.level)}. ' \
               f'Strategy: {repr(self.strategy)}. ' \
               f'Strategy Params: {repr(self.strategy_params)}'


'''
Created on 17 Nov 2014

@author: zouyuanrenren
'''
'''
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Follow up:
Can you solve it without using extra space?
'''

'''
Basic idea is to use two pointers:
1. a fast pointer;
2. a slow pointer:
3. when the two pointer meets, the head and slow will have the same distance to the beginning of the cycle
'''

# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    # @param head, a ListNode
    # @return a list node
    def detectCycle(self, head):
        fast = head
        slow = head
        if head == None:
            return None
        while fast != None and fast.next != None:
            fast = fast.next.next
            slow = slow.next
            if fast == slow:
                fast = head
                while fast != slow:
                    fast = fast.next
                    slow = slow.next
                return fast
        return None


import os
import sys
import yaml
import s3fs
from pandas import (
    read_sas,
    DataFrame,
    SparseDtype,
    Series
)
from numpy import (
    linspace,
    ndarray,
    array,
    log
)
from dataclasses import dataclass
from typing import Tuple, Any, Union
from sklearn.model_selection import train_test_split
from scipy.sparse import csc_matrix, _csc
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import (
    classification_report,
    accuracy_score,
    confusion_matrix
)
from sklearn.utils.class_weight import compute_sample_weight
from xgboost import XGBClassifier
if os.getcwd() not in sys.path:
    sys.path.append(os.getcwd())
from src.configs import (
    models_inputs,
    S3_configs
)

_cutoffs = linspace(0.1, 0.9, 17)

_key_rename = {
    "0.0": "active",
    "1.0": "churned",
    "macro avg": "macro avg",
    "weighted avg": "weighted avg"
    }

def import_from_S3(
        params: S3_configs
) -> DataFrame:
    """Connect to S3 bucket and get data

    Args:
        params (S3_configs): S3 config data

    Returns:
        DataFrame: dataframe for modeling
    """
    fs = s3fs.S3FileSystem(
            client_kwargs={'endpoint_url': params.endpoint},
            key=params.key_id,
            secret=params.access_key,
            token=params.token)

    return read_sas(
                fs.open(f"{params.bucket}/{params.path}/commsdata.sas7bdat"),
                format='sas7bdat'
            )

def import_from_local(path) -> DataFrame:
    return read_sas(
                f"{path}/inputs/raw/commsdata.sas7bdat",
                encoding="utf-8").set_index("Customer_ID")

def check_train_frac(input: str) -> bool:
    if input.isdigit():
        return 0 < int(input) < 100
    return False

def save_input_data(key, value) -> None:
    with open(r'./data/app_inputs/sample_input.yaml') as file:
        data = yaml.load(file, Loader=yaml.Loader)
    if data is None:
        data = {key: value}
    else:
        data[key] = value
    with open(r'./data/app_inputs/sample_input.yaml', 'w') as file:
        yaml.dump(data, file)

def train_valid_splitting(
        df: DataFrame, frac: float):
    y = df["churn"]
    X = df.drop("churn", axis=1)
    X_train, X_valid, y_train, y_valid = train_test_split(
                                                X,
                                                y,
                                                test_size=1-frac,
                                                stratify=y,
                                                random_state=42)
    return X_train, X_valid, y_train.values, y_valid.values

def dataframe2sparse(df: DataFrame) -> Tuple[_csc.csc_matrix, list]:
    return (
        csc_matrix(
            df.astype(SparseDtype("int32", 0)).sparse.to_coo()),
        df.columns.tolist())

@dataclass
class Modeling_Data:
    X_train_sparse: _csc.csc_matrix
    X_test_sparse: _csc.csc_matrix
    y_train: ndarray
    y_test: ndarray
    target_name: str = "churn"

Modeling_Data.__module__ = __name__

def quantiles_list(x: Series) -> list:
    list_quantile = [x.min(),
                     x.quantile(0.25),
                     x.quantile(0.5),
                     x.quantile(0.75),
                     x.max()]
    return list_quantile

def indicator_ab(x: float, a: float, b: float):
    if a-0.001 < x <= b:
        return 1
    return 0

def df_skewed_feature(df: DataFrame, feature: str) -> DataFrame:
    df_plot = df[[feature]].copy()
    df_plot[f"log_{feature}"] = df_plot[feature].apply(lambda x: log(1+x))
    return df_plot

def _sample_weight(target_array: ndarray) -> ndarray:
    return compute_sample_weight(
                class_weight='balanced',
                y=target_array
            )

def cm_annotation(prefix: str) -> ndarray:
    """Return a list of labels for a given confusion
       matrix axis

    Args:
        prefix (str): the label of the axis: either real
        or pred for respectively real outcome and
        prediction one

    Returns:
        ndarray: list of the labels with the prefix concatenate
        to the target variable vvalues labels
    """
    return array([f"{prefix}_active", f"{prefix}_churned"])

def performance_measure(
        model: Union[RandomForestClassifier, XGBClassifier],
        X: csc_matrix,
        y: ndarray,
        cutoff: float) -> Tuple[Any]:
    proba = model.predict_proba(X)[:, 1]
    yHat = 1.*(proba > cutoff)
    _report = classification_report(
                    y,
                    yHat,
                    # sample_weight=_sample_weight(y),
                    digits=3,
                    output_dict=True
                )
    _report_keys = list(_report.keys())
    _report_keys.remove('accuracy')
    _report = {_key_rename[key]: _report[key] for key in _report_keys}
    _acc = accuracy_score(
                y,
                yHat,
                # sample_weight=_sample_weight(y)
            )
    _cf_matrix = confusion_matrix(
                    y,
                    yHat,
                    # sample_weight=_sample_weight(y)
                )
    return _report, _acc, DataFrame(
                            data=_cf_matrix,
                            columns=cm_annotation("prediction"),
                            index=cm_annotation("real")
                        )

def model_report(
        model: Union[RandomForestClassifier, XGBClassifier],
        params: models_inputs,
        cutoff: float) -> Tuple[Any]:
    report_train, acc_train, cm_train = performance_measure(
                                            model,
                                            params.X_train,
                                            params.y_train,
                                            cutoff
                                        )
    report_test, acc_test, cm_test = performance_measure(
                                            model,
                                            params.X_test,
                                            params.y_test,
                                            cutoff
                                        )
    return (
        report_train,
        acc_train,
        cm_train,
        report_test,
        acc_test,
        cm_test
    )


"""
Objective: Test regressors.py's functionality
"""

from tests.model_fixture import __data_split_model__, delete_files
import pytest
import collections
import os
from main import ROOT_DIR
# change working directory to
os.chdir(ROOT_DIR)

@pytest.mark.parametrize('algorithm, data, exp, tuned, directory', [('nn', 'Lipo-short.csv', 'exp', False, True)])
def test_nn_get_regressor(__data_split_model__):
    model1 = __data_split_model__
    model1.get_regressor()
    assert model1.estimator, "Can't call for regressor object"
    assert type(model1.fit_params) is dict, "fit params is not a dictionary"
    assert model1.task_type == 'regression'

sklearn_list = ['ada', 'svm', 'rf', 'gdb', 'mlp', 'knn']

@pytest.mark.parametrize('algorithm', sklearn_list)
@pytest.mark.parametrize('data, exp, tuned, directory', [('Lipo-short.csv', 'exp', False, True)])
def test_sklearn_get_regressor(__data_split_model__):
    model1 = __data_split_model__
    model1.get_regressor()
    assert model1.task_type == 'regression'
    assert str(model1.estimator)

sklearn_list.remove('knn')

@pytest.mark.parametrize('algorithm', sklearn_list)
@pytest.mark.parametrize('data, exp, tuned, directory', [('Lipo-short.csv', 'exp', True, True)])
def test_sklearn_hypertune(__data_split_model__):
    model1 = __data_split_model__
    model1.reg()
    model1.make_grid()
    model1.hyperTune()
    assert type(model1.params) is dict
    # delete_files(model1.run_name)

@pytest.mark.parametrize('algorithm, data, exp, tuned, directory', [('nn', 'Lipo-short.csv', 'exp', True, True)])
def test_nn_hypertune(__data_split_model__):
    model1 = __data_split_model__
    model1.reg()
    model1.make_grid()
    model1.hyperTune()
    assert type(model1.params) is dict
    assert os.path.isfile(''.join([model1.run_name, '.h5']))  # Check for PVA graphs
    delete_files(model1.run_name)


def add(a,b):
    result = a + b
    return result

def subtract(a,b):
    result = a - b
    return result

def multiply(a,b):
    result = a * b
    return result

def divide(a,b):
    if b!=0:
        result = a / b
        return result
    else:
        return "Error: Division by zero"

def exponentiation(a,b):
    result = a**b
    return result

def modulus(a, b):
    if a > 0 and b > 0:
        result = ((a/100)* b)
        return result
    else:
        return "Error: Both numbers should be positive for modulus operation"

def get_number(prompt):
    while True:
        try:
            user_input = float(input(prompt))
            return user_input
        except ValueError:
            print("Invalid input. Please enter a number.")

def get_user_input():
    a = float(input("Enter the first number: "))
    b = float(input("Enter the second number: "))
    return a, b

def display_results(a, b):
    print(f"Addition: {add(a, b)}")
    print(f"Subtraction: {subtract(a, b)}")
    print(f"Multiplication: {multiply(a, b)}")
    print(f"Division: {divide(a, b)}")
    print(f"Exponentiation: {exponentiation(a, b)}")
    print(f"Modulus: {modulus(a, b)}")

if __name__ == "__main__":
    a, b = get_user_input()
    display_results(a, b)


"""
Advent of Code: Day 14
"""

all_lines = []
line_no = 0

with open("2022/Day 14/data.txt", "r") as f:
    content = f.read()
    lines = content.splitlines()
    for line in lines:
        all_lines.append(line)

cave = []

x_min = 444-100
x_max = 504+100
y_min = 0 # rock=14
y_max = 161+2

# x_min = 494-10
# x_max = 503+10
# y_min = 0
# y_max = 9+2

for row in range(y_max-y_min+1):
    temp_array = []
    for column in range(x_max-x_min+1):
        temp_array.append(".")
    cave.append(temp_array)

# Set sand gen point
cave[0][500-x_min] = "+"
sand_x = 500-x_min
sand_y = 0

#    EXAMPLE INPUT:
#
#    494,0 -> 503,0 -> 503,4
#    503,9 -> 494,9 -> 494,5 -> 500,5
#
#    SHOULD OUTPUT:
#
#      4     5  5
#      9     0  0
#      4     0  3
#    0 ##########
#    1 .........#
#    2 .........#
#    3 .........#
#    4 .........#
#    5 #######...
#    6 #.........
#    7 #.........
#    8 #.........
#    9 ##########

# Go through and add all the rocks
for rock_formation in all_lines:
    rock_coords = rock_formation.split(" -> ")
    i = 0
    while i != len(rock_coords)-1:
        start_coords = rock_coords[i].split(",")
        start_x = int(start_coords[0]) - x_min
        start_y = int(start_coords[1])
        end_coords = rock_coords[i + 1].split(",")
        end_x = int(end_coords[0]) - x_min
        end_y = int(end_coords[1])
        if start_x != end_x:
            if start_x > end_x:
                inc = -1
            else:
                inc = 1
            for a in range(start_x, end_x, inc):
                cave[end_y][a] = "#"
            cave[end_y][end_x] = "#"
        else:
            if start_y > end_y:
                inc = -1
            else:
                inc = 1
            for b in range(start_y, end_y, inc):
                cave[b][end_x] = "#"
            cave[end_y][end_x] = "#"
        i += 1

# Print out the cave with rocks
print()
for print_row in cave:
    print(print_row)

# Sand simulator
start_print = 0
possible = True
while possible is True:
    # Spawn sand
    cave[sand_y + 1][sand_x] = "o"

    fall_x = sand_x
    fall_y = sand_y

    # Simulate fall
    falling = True
    while falling is True:
        if fall_x == sand_x and fall_y == sand_y:
            changer = "+"
        else:
            changer = "."
        # Check down
        if cave[fall_y + 1][fall_x] == ".":
            cave[fall_y][fall_x] = changer
            fall_y += 1
            cave[fall_y][fall_x] = "o"
        # Check down left
        elif cave[fall_y + 1][fall_x - 1] == ".":
            cave[fall_y][fall_x] = changer
            fall_y += 1
            fall_x -= 1
            cave[fall_y][fall_x] = "o"
        # Check down right
        elif cave[fall_y + 1][fall_x + 1] == ".":
            cave[fall_y][fall_x] = changer
            fall_y += 1
            fall_x += 1
            cave[fall_y][fall_x] = "o"
        else:
            if changer == "+":
                possible = False
            falling = False

    # Print out the cave with rocks and sand
    if start_print >= 10000:
        print()
        for print_row in cave:
            print(print_row)
    start_print += 1

total = 0
for row in cave:
    for column in row:
        if column == "o":
            total += 1

print(total + 1)


__version__ = "$Revision$"
# $Id$

# Needed by test.py, and probably random python utils.
__all__ = [
    "lex",
    "yacc",
    ]

# $Log$
# Revision 1.1  2003-02-14 00:34:06  sandro
# including PLY in this distribution for general ease.
#


with open("dane_2019.txt", "r") as plik:
    lista_danych = []
    for line in plik:
        lista_danych.append(line.replace("\n", ""))

listopad = 0

for pesel in lista_danych:
    if pesel[2] == "1" and pesel[3] == "1" or pesel[2] == "3" and pesel[3] == "1":
        listopad += 1

print(listopad)

with open("odpowiedziPESEL.txt", "a", encoding="UTF-8") as plik:
    plik.write("b)\n Liczba osób urodzonych w listopadzie: ")
    plik.write(str(listopad))


# -*- coding: utf-8 -*-
"""
Created on Thu Feb 23 14:17:20 2017

@author: qwang2
"""
import pandas as pd

# (re)format Dataframe columns
def makestartdatetime(x):
    if x['Time_Start'] == '24:00':
        x['Time_Start'] = '00:00'
    return pd.to_datetime(str(x['Date']) + ' ' + x['Time_Start'])
def makeenddatetime(x):
    if x['Time_End'] == '24:00':
        x['Time_End'] = '00:00'
    return pd.to_datetime(str(x['Date']) + ' ' + x['Time_End'])
def makestopindex(x):
    corridor = [1147466,1147283,14255078,30020765,12347485,1147201,8491741,13973647,30082914,1147026]
    return corridor.index(x['centreline_id'])+0.5
def makeseasons(x):
    if x['month'] in (12,1,2):
        return 'Winter'
    elif x['month'] in (3,4,5):
        return 'Spring'
    elif x['month'] in (6,7,8):
        return 'Summer'
    else:
        return 'Autumn'

def preprocess_scoot(sdata,sdetectors):
    # Merge for detector mapping
    sdata = sdata.merge(sdetectors,left_on='Site',right_on='det')
    # Convert to 15min actual volume detection
    sdata['flow_mean_veh/h'] = sdata['flow_mean_veh/h']//4
    # Take out weekends
    sdata = sdata[(sdata['DOW']!='SA') & (sdata['DOW']!='SU')]
    # Format datetime info
    sdata['time_15'] = sdata['Time_Start'].str[:2].astype(int)*4 + (sdata['Time_Start'].str[-2:].astype(int)//15)
    sdata['month'] = sdata['Date']//100-sdata['Date']//10000*100
    # Formulate stop index along the corridor
    sdata['stopindex'] = sdata.apply(makestopindex,axis=1)
    sdata['Date'] = pd.to_datetime(sdata['Date'].astype(str)).dt.date

    return sdata[['month','time_15','Date','centreline_id','stopindex','flow_mean_veh/h','direction']]

def preprocess_flow(fdata):
    ''' This function formats data for plotting.
        Input dataframe requires columns:
        - count_bin: type datetime
        - centreline_id
        - volume: actual volume
        - dir_bin: direction
        Output dataframe has columns:
        - month, time_15, date
        - centreline_id, stopindex, dir_bin
        - volume'''

    fdata['DOW'] = fdata['count_bin'].dt.dayofweek
    fdata['date'] = fdata['count_bin'].dt.date
    fdata['month'] = fdata['count_bin'].dt.month
    fdata['time_15'] = fdata['count_bin'].dt.hour*4+fdata['count_bin'].dt.minute//15
    fdata['stopindex'] = fdata.apply(makestopindex,axis=1)
    dirmap = {1:'EB', -1:'WB'}
    fdata['direction'] = fdata['dir_bin'].map(dirmap)
    # Filter out weekends
    fdata = fdata[(fdata['DOW']!=0) & (fdata['DOW']!=6)]
    fdata = fdata.loc[:,('month','time_15','date','centreline_id','stopindex','volume','dir_bin', 'direction')]
    return fdata

def get_start_end_month(start_year, end_year, start_month, end_month, year):
    ''' This function returns the start and end month given a year '''
    if year == start_year and start_year != end_year:
        sm = start_month
        em = 13
    elif year == start_year and start_year == end_year:
        sm = start_month
        em = end_month+1
    elif year == end_year:
        sm = 1
        em = end_month+1
    else:
        sm = 1
        em = 13
    return (sm,em)

def fill_missing_values(index,s,start,end):
    ''' This function expands and fills in list s to a specified size (fill missing values with 0)
        index: index of each element in s
        s: list to be filled
        start, end: index of the returned list'''
    ss = []
    i = start
    indexi = 0
    while i <= end:
        if indexi >= len(index):
            ss.append(0)
            i += 1
        elif index[indexi]>i:
            while i<index[indexi]:
                ss.append(0)
                i += 1
        else:
            ss.append(s[indexi])
            indexi += 1
            i += 1

    # fill in gaps
    i = start
    while i <= end:
        if (ss[i] is None or ss[i] == 0):
            j = i
            while (ss[j] is None or ss[j] == 0) and j < end:
                j += 1
            if i == 0:
                while i < j:
                    ss[i] = ss[j]
                    i += 1
            elif j == end:
                v = i-1
                while i <= j:
                    ss[i] = ss[v]
                    i += 1
            else:
                step = (ss[j]-ss[i-1])/(j-i+1)
                while i < j:
                    ss[i] = ss[i-1]+step
                    i += 1
        else:
            i += 1
    return ss


"""test_plotwindow_model.py - tests the plotwindow_model module

Chris R. Coughlin (TRI/Austin, Inc.)
"""

__author__ = 'Chris R. Coughlin'

from controllers import pathfinder
import models.plotwindow_model as model
import models.mainmodel as mainmodel
import models.abstractplugin as abstractplugin
import models.ultrasonicgate as ultrasonicgate
import numpy as np
from matplotlib import cm
import matplotlib.colors
import scipy.signal
import json
import os
import os.path
import random
import unittest

class TestBasicPlotWindowModel(unittest.TestCase):
    """Tests the BasicPlotWindowModel class"""

    def setUp(self):
        self.mock_controller = ""
        self.mock_data_file = ""
        self.basic_model = model.BasicPlotWindowModel(self.mock_controller,
                                                      self.mock_data_file)

    def random_data(self):
        """Returns a list of random data"""
        return [random.uniform(-100, 100) for i in range(25)]

    def test_revert_data(self):
        """Verify data is reverted to original"""
        original_data = np.array(self.random_data())
        self.assertIsNone(self.basic_model.original_data)
        self.assertIsNone(self.basic_model.data)
        self.basic_model.original_data = original_data
        self.basic_model.data = np.array(self.random_data())
        self.basic_model.revert_data()
        self.assertTrue(np.array_equal(self.basic_model.original_data, original_data))
        self.assertTrue(np.array_equal(self.basic_model.original_data,
                             self.basic_model.data))

    def test_get_plugins(self):
        """Verify a list of available plugins is returned"""
        expected_plugin_list = mainmodel.load_plugins()
        expected_plugin_names = [plugin[0] for plugin in expected_plugin_list]
        retrieved_plugin_list = self.basic_model.get_plugins()
        self.assertEqual(len(expected_plugin_list), len(retrieved_plugin_list))
        for plugin in retrieved_plugin_list:
            plugin_name = plugin[0]
            plugin_instance = plugin[1]
            self.assertTrue(plugin_name in expected_plugin_names)
            self.assertTrue(issubclass(plugin_instance, abstractplugin.AbstractPlugin))

    def test_get_plugin(self):
        """Verify a class name and an instance of a plugin is returned"""
        retrieved_plugin_list = self.basic_model.get_plugins()
        for plugin in retrieved_plugin_list:
            plugin_name = plugin[0]
            plugin_instance = plugin[1]
            plg_cls = self.basic_model.get_plugin(plugin_name)
            self.assertEqual(type(plugin_instance), type(plg_cls))

class TestPlotWindowModel(unittest.TestCase):
    """Tests the PlotWindowModel class"""

    def setUp(self):
        self.mock_controller = ""
        self.mock_data_file = ""
        self.model = model.PlotWindowModel(self.mock_controller, self.mock_data_file)

    def random_data(self):
        """Returns a list of random data"""
        return [random.uniform(-100, 100) for i in range(25)]

    def test_load_user_gate_functions(self):
        """Verify load_user_gate_functions returns the user's custom gate functions"""
        expected_gates = mainmodel.load_dynamic_modules(pathfinder.gates_path(), ultrasonicgate.UltrasonicGate)
        retrieved_gates = self.model.load_user_gate_functions()
        self.assertEqual(len(expected_gates), len(retrieved_gates))
        for idx in range(len(expected_gates)):
            self.assertEqual(expected_gates[idx][0], retrieved_gates[idx][0])
            self.assertTrue(issubclass(retrieved_gates[idx][1], ultrasonicgate.UltrasonicGate))

    def test_apply_window(self):
        """Verify apply_window function applies a given gate function and returns an ndarray"""
        #original_data = np.ones(55)
        original_data = np.array(self.random_data())
        for gate in self.model.gates:
            gate_name = gate[0]
            windowed_data = self.model.apply_window(gate_name, original_data, 3, 21)
            self.assertTrue(isinstance(windowed_data, np.ndarray))
            self.assertEqual(original_data.size, windowed_data.size)

    def test_apply_gate(self):
        """Verify apply_gate function"""
        original_data = np.array(self.random_data())
        self.model.original_data = original_data
        for gate in self.model.gates:
            gate_name = gate[0]
            self.model.revert_data()
            start_idx = 2
            end_idx = 4
            winder_cls = model.TwoDManipMixin()
            winder_cls._define_gate_functions()
            expected_data = winder_cls.apply_window(gate_name, self.model.data,
                                                    start_idx, end_idx)
            self.model.apply_gate(gate_name, start_idx, end_idx)
            self.assertTrue(np.array_equal(original_data, self.model.original_data))
            self.assertTrue(np.array_equal(expected_data, self.model.data))

    def test_rectify_full(self):
        """Verify full rectification of data"""
        original_data = np.array(self.random_data())
        self.model.original_data = original_data
        self.model.revert_data()
        self.model.rectify_full()
        self.assertTrue(np.array_equal(np.absolute(original_data), self.model.data))

class TestImgPlotWindowModel(unittest.TestCase):
    """Tests the ImgPlotWindowModel class"""

    def setUp(self):
        self.mock_controller = ""
        self.mock_data_file = ""
        self.mock_read_parameters = {}
        self.model = model.ImgPlotWindowModel(self.mock_controller, self.mock_data_file)

    def random_data(self):
        """Generates a random list of data"""
        return np.array([random.uniform(-100, 100) for i in range(25)])

    def random3D_data(self):
        """Generates a random 3D array of data"""
        raw_array = np.array([random.uniform(-100, 100) for i in range(24)])
        three_d_array = raw_array.reshape((3, 2, 4))
        return three_d_array

    def test_average_detrend(self):
        """Verify mean detrending along an axis"""
        self.model.original_data = self.random_data()
        self.model.revert_data()
        expected_data = scipy.signal.detrend(self.model.original_data, type='constant')
        self.model.detrend_data(0, 'constant')
        self.assertTrue(np.array_equal(expected_data, self.model.data))

    def test_linear_detrend(self):
        """Verify linear detrending along an axis"""
        self.model.original_data = self.random_data()
        self.model.revert_data()
        expected_data = scipy.signal.detrend(self.model.original_data, type='linear')
        self.model.detrend_data(0, 'linear')
        self.assertTrue(np.array_equal(expected_data, self.model.data))

    def test_slice_data(self):
        """Verify a 3D array is replaced by a 2D slice"""
        three_d_array = self.random3D_data()
        self.model.original_data = three_d_array
        self.model.revert_data()
        slice_idx = random.choice(range(three_d_array.shape[2]))
        expected_data = three_d_array[:, :, slice_idx]
        self.model.slice_data(slice_idx)
        self.assertTrue(np.array_equal(expected_data, self.model.data))

    def test_flipud_data(self):
        """Verify data are flipped vertically"""
        self.model.original_data = self.random_data()
        self.model.revert_data()
        expected_data = np.flipud(self.model.data)
        self.model.flipud_data()
        self.assertTrue(np.array_equal(expected_data, self.model.data))

    def test_fliplr_data(self):
        """Verify data are flipped horizontally"""
        three_d_array = self.random3D_data()
        self.model.original_data = np.array(three_d_array)
        self.model.revert_data()
        expected_data = np.fliplr(self.model.data)
        self.model.fliplr_data()
        self.assertTrue(np.array_equal(expected_data, self.model.data))

    def test_rotate_data(self):
        """Verify data are rotated counter-clockwise"""
        self.model.original_data = self.random3D_data()
        self.model.revert_data()
        num_rotations = random.choice((1, 2, 3))
        expected_data = np.rot90(self.model.data, k=num_rotations)
        self.model.rotate_data(num_rotations)
        self.assertTrue(np.array_equal(expected_data, self.model.data))

    def test_transpose_data(self):
        """Verify data are transposed"""
        self.model.original_data = self.random3D_data()
        self.model.revert_data()
        expected_data = self.model.data.T
        self.model.transpose_data()
        self.assertTrue(np.array_equal(expected_data, self.model.data))

    def test_get_colormaps(self):
        """Verify returning a list of colormaps"""
        cmap_choices = [m for m in cm.datad]
        cmap_choices.extend(self.model.get_user_colormaps())
        cmap_choices.sort()
        self.assertListEqual(cmap_choices, self.model.get_colormap_choices())

    def test_get_system_colormaps(self):
        """Verify returning a list of predefined colormaps"""
        cmap_choices = [m for m in cm.datad]
        self.assertListEqual(cmap_choices, self.model.get_system_colormaps())

    def test_get_user_colormaps(self):
        """Verify returning a list of user-defined colormaps"""
        colormap_folder = os.path.join(pathfinder.app_path(), 'colormaps')
        expected_colormaps = []
        for root, dirs, files in os.walk(colormap_folder):
            for fname in files:
                with open(os.path.join(root, fname), "r") as fidin:
                    cmap_dict = json.load(fidin)
                    expected_colormaps.append(cmap_dict.get('name', fname))
        self.assertListEqual(expected_colormaps, self.model.get_user_colormaps(colormap_folder))

    def test_save_colormap(self):
        """Verify saving a colormap"""
        shipped_colormaps_folder = os.path.join(pathfinder.app_path(), 'colormaps')
        shipped_colormaps = os.listdir(shipped_colormaps_folder)
        for shipped_colormap in shipped_colormaps:
            shipped_colormap_file = os.path.join(shipped_colormaps_folder, shipped_colormap)
            with open(shipped_colormap_file, "r") as fidin:
                cmap_dict = json.load(fidin)
                self.model.save_colormap(cmap_dict)
                cmap_file = os.path.join(pathfinder.colormaps_path(), cmap_dict['name'])
                self.assertTrue(os.path.exists(cmap_file))

    def test_get_cmap(self):
        """Verify returning a valid matplotlib Colormap (predefined or user-created)"""
        colormap_folder = os.path.join(pathfinder.app_path(), 'colormaps')
        colormaps = self.model.get_system_colormaps()
        colormaps.extend(self.model.get_user_colormaps(colormap_folder))
        for cmap_name in colormaps:
            cmap = self.model.get_cmap(cmap_name, colormap_folder)
            self.assertTrue(isinstance(cmap, matplotlib.colors.Colormap))

    def test_load_colormap(self):
        """Verify load_colormap returns a valid matplotlib Colormap"""
        colormap_folder = os.path.join(pathfinder.app_path(), 'colormaps')
        colormaps = self.model.get_user_colormaps(colormap_folder)
        for cmap_name in colormaps:
            cmap_file = os.path.join(colormap_folder, cmap_name)
            cmap = self.model.load_colormap(cmap_file)
            self.assertTrue(isinstance(cmap, matplotlib.colors.Colormap))

if __name__ == "__main__":
    random.seed()
    unittest.main()


import torch
import torch.nn as nn
import data
from model import ResNet

from torch.utils.data import Dataset, DataLoader

if __name__ == "__main__":
    trainset = data.TrainTinyImageNet("tiny-imagenet-200/train")
    train_loader = DataLoader(trainset, batch_size=256)

    model = ResNet()
    criterion = nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(), lr=0.1, momentum=0.9)  # Weight decay can be hired.

    for epoch in range(2):
        running_loss = 0.0
        for i, data in enumerate(train_loader):
            inputs, labels = data
            optimizer.zero_grad()

            outputs = model(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()

            running_loss += loss.item()
            print(running_loss/(i+1))
        print("Epoch loss", running_loss)


#!/usr/bin/python3
# -*- coding: utf-8 -*-

import hmac
import sys
from base64 import b64decode
import random
from datetime import datetime
from binascii import unhexlify
import json

import requests
from Crypto.Cipher import AES
from Crypto.Hash import SHA256, SHA384, HMAC

from rngatherd.RandPi.pbkdf2 import PBKDF2

class RandPiClient(object):
    def __init__(self, url, secret, salt="pepper"):
        self.url = url
        self.SHARED_SECRET = secret
        self.SHARED_SALT = salt
        base_key = PBKDF2(self.SHARED_SECRET.encode('utf-8'), self.SHARED_SALT.encode('utf-8'),
                          iterations=32000, digestmodule=SHA384, macmodule=HMAC).read(64)
        self.ENCRYPTION_KEY = base_key[:32]
        self.HMAC_KEY = base_key[32:64]
        random.seed(datetime.now())

    @staticmethod
    def remove_pkcs7_padding(data):
        return data[:-data[-1]]

    def get_random(self, length=64):
        try:
            nonce = "".join([random.choice(list('0123456789abcdef')) for _ in range(2 * int((length + 1) / 2))])
            response = requests.post(self.url, data={
                'length': length,
                'nonce': nonce
            })
            response_data = response.json()
        except OSError:
            return b''
        if 'iv' not in response_data or 'encrypted_data' not in response_data or 'hmac' not in response_data:
            print("Response in a wrong format:\n" + json.dumps(response_data))
            return b''
        if hmac.new(self.HMAC_KEY,
                    b64decode(response_data['encrypted_data']) + unhexlify(nonce),
                    SHA256).digest() != b64decode(response_data['hmac']):
            print("Wrong signature!")
            return b''
        cipher = AES.new(self.ENCRYPTION_KEY, AES.MODE_CBC, b64decode(response_data['iv']))
        data = self.remove_pkcs7_padding(cipher.decrypt(b64decode(response_data['encrypted_data'])))
        return data

if __name__ == "__main__":
    DEMO_SHARED_SECRET = "Mein tolles langes Passwort, das total sicher ist. " + \
                         "Das sieht man an den Sonderzeichen wie / (Slash) oder $ (Dollar). " + \
                         "Außerdem enthält diese Passphrase einfach eine Menge Zeichen, " + \
                         "die ein Angreifer erst mal erraten muss."
    client = RandPiClient("http://127.0.0.1:8000/entropy/random", DEMO_SHARED_SECRET)
    sys.stdout.buffer.write(client.get_random())


from pathlib import Path
import shutil
import os
import stat

def install_hook() -> None:
    hook_path = Path.cwd() / ".git" / "hooks" / "pre-commit"
    if hook_path.exists():
        shutil.move(hook_path, hook_path.with_suffix(".bak")) # type: ignore
        print("Backed up old hook to pre-commit.bak")
    with hook_path.open("w", encoding="utf-8") as f:
        f.write("#!/bin/sh\n")
        f.write("poetry run lint")
    # add +x flag
    st = os.stat(hook_path)
    os.chmod(hook_path, st.st_mode | stat.S_IEXEC)
    print("Hook successfully installed")


from pynput.keyboard import Key, Controller
import time
import pyautogui
import cv2
import numpy as np
import copy
import pyscreenshot as ImageGrab
from datetime import datetime
from mss import mss
from simulation import Simulation

NUMBER_OF_IMAGES = 500
SAMPLE_TIME = 0.01

class Vision():

	def __init__(self):
		self.keyboard = Controller()
		return

	def simulate_space_bar(self):
		#time.sleep(2)
		#for i in range(100):
		self.keyboard.press(" ")
		self.keyboard.release(" ")
	#	time.sleep(0.12)

	def take_screen_shot(self):
		#return ImageGrab.grab(bbox=(740,160, 490, 680))
	#	im.save(fname, 'png')
		mon = {"left": 740, "top": 160, "width": 490, "height":680}
		#mon':0, "top": 740, "left": 160, "width": 490, "height": 680}
		sct = mss()
		img = np.array(sct.grab(mon))
		return img
	#	print(filename)
	#	return pyautogui.screenshot(region=(740,160, 490, 680))

	def get_train_imgs(self):
		screen = []
		for i in range(NUMBER_OF_IMAGES):
			time.sleep(SAMPLE_TIME)
			screen.append(self.take_screen_shot())

		for i in range(NUMBER_OF_IMAGES):
			#image = cv2.cvtColor(np.array(screen[i]), cv2.COLOR_BGR2RGB)
			cv2.imwrite('images/'+str(i)+'.png', screen[i])

	def get_all_parameters(self):
		for i in range(NUMBER_OF_IMAGES):
			self.take_img_parameters(i)

	def take_img_parameters(self, number):
		img = cv2.imread('images/'+str(number)+'.png')
		cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

		lower = np.array([40,50,50])
		upper = np.array([60,255,255])
		mask = cv2.inRange(img, lower, upper)
		green = cv2.bitwise_and(img, img, mask= mask)
		kernel = np.ones((5,5),np.uint8)
		green = cv2.erode(green, kernel,iterations = 1)
		m, green = cv2.threshold(green, 10, 255, cv2.THRESH_BINARY)
		greencolor = np.array([0, 255, 0])
		green = cv2.bitwise_and(green, greencolor)
	#	cv2.imshow('teste', green)
	#	cv2.waitKey(0)
		cv2.imwrite('images/processed/'+str(number)+'.png', green)

	def get_bird(self, filename):
		#deprecated
		'''img = cv2.imread(filename)
		cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

		lower = np.array([10,10,50])
		upper = np.array([40,255,255])
		kernel = np.ones((10,10),np.uint8)
		yellow = cv2.erode(img, kernel,iterations = 1)
		mask = cv2.inRange(yellow, lower, upper)
		yellow = cv2.bitwise_and(img, img, mask= mask)
		m, yellow = cv2.threshold(yellow, 10, 255, cv2.THRESH_BINARY)
		yellowcolor = np.array([0, 255, 255])
		yellow = cv2.bitwise_and(yellow, yellowcolor)
		cv2.imshow('teste', yellow)
		cv2.waitKey(0)
		cv2.imwrite('images/bird.png', yellow)
		#print(len(yellow), len(yellow[0]))
		'''
		try:
			img = cv2.imread(filename)
		except:
			img = filename
		cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
		print(img[10][10])
		#218 338
		lower = np.array([0,0,50])
		upper = np.array([30,255,255])
		kernel = np.ones((0,0),np.uint8)
		yellow = cv2.erode(img, kernel,iterations = 1)
		mask = cv2.inRange(yellow, lower, upper)
		yellow = cv2.bitwise_and(img, img, mask= mask)

		m, yellow = cv2.threshold(yellow, 10, 255, cv2.THRESH_BINARY)
		yellowcolor = np.array([0, 255, 255])
		yellow = cv2.bitwise_and(yellow, yellowcolor)
		#cv2.imshow('teste', yellow)
		#cv2.waitKey(0)
		cv2.imwrite('yellow.png', yellow)

		for i in range(670, 0, -1):
			#print(yellow[i][218])
			if(np.array_equal(yellow[i][218], [0, 255, 255])):
				return i
		return -1

	def get_borders(self, filename):
		img = cv2.imread(filename)
		cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

		lower = np.array([10,0,0])
		upper = np.array([180,255,255])
		kernel = np.ones((10,10),np.uint8)
		yellow = cv2.erode(img, kernel,iterations = 1)
		mask = cv2.inRange(yellow, lower, upper)
		yellow = cv2.bitwise_and(img, img, mask= mask)
		m, yellow = cv2.threshold(yellow, 10, 255, cv2.THRESH_BINARY)
		yellowcolor = np.array([0, 255, 255])
		yellow = cv2.bitwise_and(yellow, yellowcolor)
		#cv2.imshow('teste', yellow)
		#cv2.waitKey(0)
		cv2.imwrite('images/borders.png', yellow)
		#print(len(yellow), len(yellow[0]))

	def find_borders_values(self):
		img = cv2.imread('images/borders.png')
		cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
		px = int(len(img[0])/2)
		py = int(len(img)/2)-100
		#print('img', px, py)

		#print(img[py, px])
		pxleft = copy.copy(px)
		while(np.array_equal(img[py, pxleft], [0,0,0])):
			pxleft -= 1

		pxright = copy.copy(px)
		while(np.array_equal(img[py, pxright], [0,0,0])):
			pxright += 1
		#print(px, pxleft, pxright-pxleft)
		last_px = self.draw_square(pxleft, pxright-pxleft)
		#print('last_px, pxright', last_px, pxright)
		return last_px, pxright

	#before jump -9
	#after jump -89
	#total vertical 60
	#total horizontal 70
	def draw_square(self, left_border, length):
		img = cv2.imread('images/borders.png')
		#print('left_boder, lengh', left_border, length)
		cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
		for x in range(70):
			for y in range(60):
				px = int(left_border+length/2.0-x)+4
				py = int(len(img)/2)-9+y
				img[py, px] = [255, 0,0]
		#cv2.imshow('teste', img)
		#cv2.waitKey(0)
		#print('simultation py', py)
		#print('last_x_pixel', px)
		return px

	def get_pipe_pixel(self, last_px, pxright, filename):
		try:
			img = cv2.imread(filename)
		except:
			img = filename

		cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

		img[570,last_px] = [255,255,255]
		#cv2.imshow('teste', img)
		#cv2.waitKey(0)
		#print('last, pxright', last_px, pxright)
		pipe_pixel = pxright
		#[print(img[int(i),last_px]) for i in range(len(img))]
		for i in range(last_px, pxright):
			if(np.array_equal(img[570, i], [0,255,0])):
				pipe_pixel = i
				break
		#print('pipe', pipe_pixel)
		return pipe_pixel

	def get_distance(self, filename):
		return self.get_pipe_pixel(182, 483, filename)

	# pipe under pixel 318
	# pipe over pixel 295 (total len 23)
	# hole len 73 (295-122)
	def get_center(self, filename):
		try:
			img = cv2.imread(filename)
		except:
			img = filename

		cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
		hole_len = 200
		pipe_inferior_len = 25

		last_px = 182
		pipe_pixel = self.get_pipe_pixel(182, 483, filename)
		#print(pipe_pixel)

		pipe_inferior_pixel = 340
		for i in range(570, 0 ,-1):
			if(np.array_equal(img[i, pipe_pixel], [0,255,0])):
				pipe_inferior_pixel = i
				break
		#cv2.imshow('teste', img)
		#cv2.waitKey(0)
		return pipe_inferior_pixel + pipe_inferior_len + int(hole_len/2)

	def transform_image_green(self):
		cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

		lower = np.array([40,50,50])
		upper = np.array([60,255,255])
		mask = cv2.inRange(img, lower, upper)
		green = cv2.bitwise_and(img, img, mask= mask)
		kernel = np.ones((5,5),np.uint8)
		green = cv2.erode(green, kernel,iterations = 1)
		m, green = cv2.threshold(green, 10, 255, cv2.THRESH_BINARY)
		greencolor = np.array([0, 255, 0])
		green = cv2.bitwise_and(green, greencolor)
		return green

	def play(self, mlp):
		running = True
		chrom = Simulation()
		while(running):
			img = self.take_screen_shot()
			cv2.imwrite('actual.png', img)
			img = cv2.imread('actual.png')
			chrom.set_pos(self.get_bird(img)+30)
			jump = mlp.predict([[self.get_distance(img),
				chrom.get_dist_to_center(self.get_center(img))]])
			if(jump):
				self.simulate_space_bar()
		return


import torch, json, os
from src.utils.handle import get_path_image_file, infer, ratio_element_matrix
from src.utils.constants import SEGMENT_POLYP_MODEL_PATH, POSTGRESQL_CONNECT, DIRTY_IMAGE_INDEX, DIRTY, CLEAN
from src.model.database import Database

postgresql_worker = Database(POSTGRESQL_CONNECT)

class SegmentPolyp:
    def __init__(self, model_path) -> None:
        self.model_path = model_path
        self.torch_device = "cuda" if torch.cuda.is_available() else "cpu"
        self.model = torch.load(
            self.model_path, map_location=self.torch_device)

    def infer_file(self, path_file):
        mask = infer(self.model, path_file)
        return mask

    def ratio_mask_file(self, path_file):
        mask = self.infer_file(path_file)
        ratio_mask = ratio_element_matrix(mask)
        return ratio_mask

def get_ratio_result(message_value_formatted):
    segment_polyp = SegmentPolyp(SEGMENT_POLYP_MODEL_PATH)
    full_path_image = get_path_image_file(message_value_formatted)
    try:
        ratio_result = segment_polyp.ratio_mask_file(full_path_image)
        print(ratio_result)
        if ratio_result >= DIRTY_IMAGE_INDEX:
            result_status = DIRTY
            sql_query_update_ratio_result = f"INSERT INTO image_service_image_has_image_tag_tab (image_id, image_tag_id) VALUES ({message_value_formatted['image']['id']}, {result_status})"
            postgresql_worker.execute_query(sql_query_update_ratio_result)

    except Exception as e:
        print(e)


"""
Django settings for esproxy project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'abcdefg'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ["127.0.0.1"]

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'esauth',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    #'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'esproxy.urls'

WSGI_APPLICATION = 'esproxy.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

LOGIN_URL = '/login.html'

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR, 'templates'),
)

LOGIN_URL = "login.html"

# CAS
INSTALLED_APPS += (
    "django_cas",
)
AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'django_cas.backends.CASBackend',
)
CAS_LOGOUT_COMPLETELY = True
CAS_IGNORE_REFERER = True
CAS_REDIRECT_URL = "/"
CAS_AUTO_CREATE_USERS = True
CAS_GATEWAY = False
CAS_RETRY_LOGIN = True
CAS_SERVER_URL = 'https://cas.corp.com'

KIBANA_DIR = '~/app/kibana'
ELASTICSEARCH_PROXY = "elasticsearch"
ELASTICSEARCH_REAL = "es"
AUTH_CACHE_TIMEOUT = 120

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': os.path.join(BASE_DIR,'django_cache'),
    }
}

try:
    from private import *
except:
    pass


from core import Agent
import random as r

class Fish(Agent.Agent):
    """docstring for Fish"""
    def __init__(self,x,y,env,trace,fishBreedTime):
        super(Fish, self).__init__()
        self.env = env
        self.x = x
        self.y = y
        self.trace = trace
        self.fishBreedTime = fishBreedTime
        self.fishBreedTimeCPT = fishBreedTime
        self.bougera = True
        self.naissance = False
        self.futurX = None
        self.futurY = None
        self.age = 0
        self.color = "green"

    def isFish(self):
        return True

    def randomNextPos(self) :
        lesPas = [-1,0,1]
        lesPas2LeRetour = [-1,1]
        futurX = self.x + r.choice(lesPas)
        if futurX == self.x :
            futurY = self.y + r.choice(lesPas2LeRetour)
        else :
            futurY = self.y + r.choice(lesPas)
        if self.env.torique :
            if futurY == -1 :
                futurY = len(self.env.grille)-1
            if futurY == len(self.env.grille) :
                futurY = 0
            if futurX == -1 :
                futurX = len(self.env.grille[0]) -1
            if futurX == len(self.env.grille[0]) :
                futurX = 0
        else :
            if futurY == -1 :
                futurY = 1
            if futurY == len(self.env.grille) :
                futurY = len(self.env.grille)-2
            if futurX == -1 :
                futurX = 1
            if futurX == len(self.env.grille[0]) :
                futurX = len(self.env.grille[0])-2
        return (futurX,futurY)

    def decide(self) :
        """
        on choisis un pas random et on observe si il y a un obstacle ou pas
        """
        (self.futurX,self.futurY) = self.randomNextPos()
        if self.fishBreedTimeCPT == 0 :
            self.naissance = True
            self.fishBreedTimeCPT = self.fishBreedTime
        else :
            self.fishBreedTimeCPT = self.fishBreedTimeCPT - 1

        if self.env.grille[self.futurY][self.futurX] == None :
            self.bougera = True
        else :
            self.bougera = False

        self.update()

    def update(self) :
        """
        """
        if self.bougera :

            self.env.grille[self.y][self.x] = None

            if self.naissance :
                babyRage = Fish(self.x,self.y,self.env,self.trace,self.fishBreedTime)
                self.env.ajouteAgent(babyRage)
                self.naissance = False

            self.env.grille[self.futurY][self.futurX] = self
            self.x = self.futurX
            self.y = self.futurY

        self.age += 1
        self.color = "blue"

    def cercle(self,fenetre,x, y, r, coul ='black'):
        '''
        tracé d'un cercle de centre (x,y) et de rayon r
        Fonction reprise sur http://python.developpez.com/cours/TutoSwinnen/?page=Chapitre8
        '''
        fenetre.can.create_oval(x-r, y-r, x+r, y+r, outline='black', fill=coul, tag='agent')

    def place_agent(self,fenetre) :
        self.cercle(fenetre, self.x*fenetre.caseX + fenetre.caseX/2, self.y * fenetre.caseY + fenetre.caseY / 2,min(fenetre.caseX,fenetre.caseY)/2 ,coul=self.color)


from telnetlib import SB
import numpy as np
from matplotlib import pyplot as plt
from scipy import interpolate
from scipy.ndimage.filters import uniform_filter1d
from . import loadMIT
from . import toolsMIT as TM
import pandas as pd

def nan_helper(y):
    """Helper to handle indices and logical indices of NaNs.

    Input:
        - y, 1d numpy array with possible NaNs
    Output:
        - nans, logical indices of NaNs
        - index, a function, with signature indices= index(logical_indices),
          to convert logical indices of NaNs to 'equivalent' indices
    Example:
        >>> # linear interpolation of NaNs
        >>> nans, x= nan_helper(y)
        >>> y[nans]= np.interp(x(nans), x(~nans), y[~nans])
    """

    return np.isnan(y), lambda z: z.nonzero()[0]

def ot_time(upr, Lx, dz, dy):
    if np.nansum(upr) == 0:
        mot = otp = otn = 0
        return mot, otp, otn

    negi = np.nonzero(upr < 0)[0]
    posi = np.nonzero(upr > 0)[0]
    maxi = np.nonzero(upr == np.nanmax(upr))[0]
    mini = np.nonzero(upr == np.nanmin(upr))[0]

    if maxi > np.max(negi):
        maxido = -1
    else:
        maxido = negi[np.min(np.nonzero(negi > maxi[0])[0])]

    if maxi < np.min(negi):
        maxiup = 0
    else:
        maxiup = negi[np.max(np.nonzero(negi < maxi[0])[0])] + 1

    if np.max(mini) > np.max(posi):
        minido = -1
    else:
        minido = posi[np.min(np.nonzero(posi > mini[0])[0])]

    if np.min(mini) < np.min(posi):
        miniup = 0
    else:
        miniup = posi[np.max(np.nonzero(posi < mini[0])[0])] + 1

    upos = np.nanmean(upr[maxiup:maxido])
    uneg = np.nanmean(upr[miniup:minido])

    otp = np.abs(Lx / np.abs(upos) / 86400)
    otn = np.abs(Lx / np.abs(uneg) / 86400)
    mot = otn + otp

    return mot, otn, otp

def ot_time_Q(coords, upr, Qout=False):
    dz = coords["dz"][0]
    dy = 10
    dx = coords["dx"][0]
    V = np.sum(coords["hfac"][:, 0:-200] * dx * dy * dz)
    Q = np.nancumsum(upr * dy * dz)
    mot = np.abs(V / np.max(np.abs(Q)) / 86400)
    if Qout:
        return mot, Q
    else:
        return mot

def gadeline(tice=-2, tam=0, sam=35, z=0):

    g = 9.81
    rhonil = 9.998e2
    a0 = -0.0575
    c0 = 0.0901
    b = -7.61e-8
    tfr1 = a0 * 0
    tfr2 = b * rhonil * g * z
    tfr = tfr1 + c0 + tfr2

    # %% Gade Line & T-Freeze
    L = 334e3
    cp = 3.994e3
    ci = 2e3
    gam = ci / cp

    T0 = tice  # ice
    T1 = tfr
    # Solve for T2
    T3 = tam

    TF = T3 - T1
    # S1 no exist
    S2 = sam - 2
    S3 = sam
    dTdS = S3 ** (-1) * (TF + L / cp + gam * (T1 - T0))

    T2 = T3 - dTdS * (S3 - S2)
    return [S2, S3], [T2, T3]

def calc_dist_along(a, b, c=None):
    if c is None:
        c = np.zeros(np.array(np.shape(a)))

    d = np.zeros(np.array(np.shape(a)))

    d[1:] = np.cumsum(np.sqrt((a[1:] - a[0:-1]) ** 2 + (b[1:] - b[0:-1]) ** 2))
    d[0] = 0

    return d

def nanave2d(a, w, axis=0):

    if axis == 0:
        niter = np.shape(a)[1]
        ave = np.zeros(niter)

        for i in np.arange(0, niter):
            if np.nansum(w[:, i]) == 0:
                ave[i] = np.nan
            else:
                ave[i] = np.nansum(a[:, i] * w[:, i]) / np.nansum(w[:, i])
    else:
        niter = np.shape(a)[0]
        ave = np.zeros(niter)

        for i in np.arange(0, niter):
            if np.nansum(w[i, :]) == 0:
                ave[i] = np.nan
            else:
                ave[i] = np.nansum(a[i, :] * w[i, :]) / np.nansum(w[i, :])

    return ave

def plume_diag(coords, var, ret=[], mask="w"):
    """
    calculate Plume diagnostics
    """

    hfac = 1 * coords["hfac"]
    ice = coords["ice"]

    x = coords["x"]
    dx = coords["dx"]
    if len(ice) > len(x):
        ice = ice[: len(x)]

    d = calc_dist_along(x, ice) + x[0]

    z = coords["z"]
    dz = -coords["dz"][0]

    pthresh = 35 * dz
    z_plu = np.arange(0, -pthresh - dz, -dz)

    t = np.nanmean(var["t_all"], axis=0)
    taw = var["tref"][-1]
    tref = var["tref"]
    tamb = t[:, 2000]
    dt = np.zeros(np.shape(t))
    # dt = t - taw
    s = np.nanmean(var["s_all"], axis=0)
    saw = var["sref"][-1]
    sref = var["sref"]
    samb = s[:, 2000]
    ds = np.zeros(np.shape(s))
    for i in np.arange(0, len(x)):
        ds[:, i] = s[:, i] - sref
        dt[:, i] = t[:, i] - tref
    # ds = s - saw
    u = np.nanmean(var["u_all"], axis=0)
    u_all = var["u_all"]
    w_all = var["w_all"]
    w = np.nanmean(var["w_all"], axis=0)

    # fig, ax = plt.subplots(1, 1)
    # cf = ax.contourf(ds)
    # fig.colorbar(cf, ax=ax)
    # fig.show()

    pmask = np.ones(np.shape(t)) * hfac
    for i in np.arange(0, np.shape(t)[1]):
        if ice[i] < 0:
            indi = np.where(z < (ice[i] - pthresh))
            pmask[indi, i] = 0
        else:
            pmask[:, i] = 0

    u, w = loadMIT.uw_ontracer(u, w, coords)

    wthresh = 0.0000
    tthresh = -0.01
    sthresh = -0.01

    if mask == "w":
        pmask[w <= wthresh] = 0
    elif mask == "u":
        pmask[u <= wthresh] = 0
    elif mask == "t":
        pmask[dt >= tthresh] = 0
    elif mask == "s":
        pmask[ds >= sthresh] = 0

    t_plu = np.zeros([int(pthresh / dz) + 1, np.shape(d)[0]]) * np.nan
    dt_plu = np.zeros([int(pthresh / dz) + 1, np.shape(d)[0]]) * np.nan
    s_plu = np.zeros([int(pthresh / dz) + 1, np.shape(d)[0]]) * np.nan
    u_plu = np.zeros([int(pthresh / dz) + 1, np.shape(d)[0]]) * np.nan
    w_plu = np.zeros([int(pthresh / dz) + 1, np.shape(d)[0]]) * np.nan
    h_plu = np.zeros([int(pthresh / dz) + 1, np.shape(d)[0]]) * np.nan
    u_plu_all = (
        np.zeros([np.shape(u_all)[0], int(pthresh / dz) + 1, np.shape(d)[0]]) * np.nan
    )
    w_plu_all = (
        np.zeros([np.shape(w_all)[0], int(pthresh / dz) + 1, np.shape(d)[0]]) * np.nan
    )

    for i in np.arange(np.shape(d)[0]):
        plu = np.where(pmask[:, i] != 0)
        t_plu[: np.shape(plu)[1], i] = t[plu, i]
        dt_plu[: np.shape(plu)[1], i] = dt[plu, i]
        s_plu[: np.shape(plu)[1], i] = s[plu, i]
        u_plu[: np.shape(plu)[1], i] = u[plu, i]
        w_plu[: np.shape(plu)[1], i] = w[plu, i]
        h_plu[: np.shape(plu)[1], i] = u[plu, i]
        for n in range(np.shape(u_all)[0]):
            u_plu_all[n, : np.shape(plu)[1], i] = u_all[n, plu, i]
            w_plu_all[n, : np.shape(plu)[1], i] = w_all[n, plu, i]

    thick = np.nansum(dz * pmask, axis=0)

    if "flx" in ret:
        uflx = u_plu * h_plu * dz * dx
        wflx = w_plu * dx * dx

        flx = np.sqrt(uflx**2 + wflx**2)

        uflx = np.nansum(uflx, axis=0)
        wflx = np.nansum(wflx, axis=0)
        flx = np.nansum(flx, axis=0)

    ret_dic = {
        "d": d,
        "thick": thick,
        "t_plu": t_plu,
        "dt_plu": dt_plu,
        "s_plu": s_plu,
        "u_plu": u_plu,
        "w_plu": w_plu,
        "u_plu_all": u_plu_all,
        "w_plu_all": w_plu_all,
        "z_plu": z_plu,
        "dt": dt,
        "ds": ds,
    }
    if "flx" in ret:
        ret_dic.update(
            {
                "flx": flx,
                "uflx": uflx,
                "wflx": wflx,
            }
        )

    return ret_dic

def entraiment_coeff(w, rho, rho_amb, dx, dz, kappa_z=[], kappa_x=[]):
    drhod = np.zeros(np.shape(rho))
    if kappa_z:
        print("use vertical derivative and horizontal velocity")
        kappa = kappa_z
        for j in range(np.shape(rho)[1]):
            drhod[1:-1, j] = (rho[0:-2, j] - rho[2:, j]) / 2 * -dz
            drhod[0, j] = (rho[1, j] - rho[0, j]) / -dz
            drhod[-1, j] = (rho[-1, j] - rho[-2, j]) / -dz
    if kappa_x:
        kappa = kappa_x
        print("use horizontal derivative/diffusivity and vertical velocity")
        for j in range(np.shape(rho)[0]):
            drhod[j, 1:-1] = (rho[j, 0:-2] - rho[j, 2:]) / 2 * dx
            drhod[j, 0] = (rho[j, 1] - rho[j, 0]) / dx
            drhod[j, -1] = (rho[j, -1] - rho[j, -2]) / dx

    if len([rho_amb]) == 1:
        drho = np.abs(rho_amb - rho)
    elif len(rho_amb) == np.shape(rho)[0]:
        drho = np.zeros(np.shape(rho))
        for i in range(np.shape(rho)[1]):
            drho[:, i] = rho_amb - rho[:, i]
    elif len(rho_amb) == np.shape(rho)[1]:
        drho = np.zeros(np.shape(rho))
        for i in range(np.shape(rho)[0]):
            drho[i, :] = rho_amb - rho[i, :]

    # plt.plot(np.nansum(drhod, axis=0), label="drhodz")
    # plt.plot(np.nansum(w, axis=0), label="u")
    # plt.plot(np.nansum(drho, axis=0), label="delta rho")
    # plt.legend()
    # plt.show()

    drho[np.isnan(drhod)] = np.nan
    Fe = np.nansum(w * drho * dx * dz)
    Fe_x = np.nansum(w * drho * dx * dz, axis=0)
    Fe_x[Fe_x == 0] = np.nan

    Fd = np.nansum(drhod * dx * dz)
    Fd_x = np.nansum(drhod * dx * dz, axis=0)

    # plt.plot(Fd_x, label="Fd")
    # plt.plot(Fe_x, label="Fe")
    # plt.plot(Fd_x[100:] / Fe_x[100:], label="")
    # plt.legend()
    # plt.show()

    alpha = Fd / Fe * kappa
    alpha_x = (Fd_x / Fe_x) * kappa

    entrain = {
        "alpha": alpha,
        "alpha_x": alpha_x,
        "Fe": Fe,
        "Fe_x": Fe_x,
        "Fd": Fd,
        "Fd_x": Fd_x,
    }
    return entrain

def rho_cont(T, S):
    t = np.linspace(min(T), max(T), 99)
    s = np.linspace(min(S), max(S), 100)

    tm, sm = np.meshgrid(t, s)

    t0 = 1
    s0 = 35
    tAlpha = -0.4e-4
    sBeta = 8.0e-4
    rnil = 999.8

    rm = rnil * (1 + tAlpha * (tm - t0) + sBeta * (sm - s0))
    return tm, sm, rm

def extract_mdata(path, filename=[], to_file=False):

    global mdata
    coords = TM.coords
    dx = coords[0]["dx"][-1]
    ice = coords[0]["ice"]
    data = TM.data
    SHIflx = TM.SHIflx

    lam1 = -5.75e-2
    lam2 = 9.01e-2
    lam3 = -7.61e-4
    tAlpha = -0.4e-4
    sBeta = 8.0e-4
    rnil = 999.8

    mdata = {
        "exp": [],
        "T_AW": [],
        "sgd": [],
        "MeltFlux": [],
        "PlumeFlux": [],
        "TF": [],
        "AveMelt": [],
        "Entrainment_u": [],
        "Fe/Fd": [],
    }
    for vi in np.arange(0, np.shape(data)[0]):
        print(path[vi])
        plume = plume_diag(coords[vi], data[vi], ["flx"], mask="u")
        dz = plume["z_plu"][1] - plume["z_plu"][0]
        flx = plume["flx"]
        flxi = np.min([np.nanargmax(flx), 1500])

        taw = data[vi]["tref"][-1]
        saw = data[vi]["sref"][-1]
        raw = (1 + saw * sBeta + taw * tAlpha) * rnil

        t_ice = plume["t_plu"][0, :]
        s_ice = plume["s_plu"][0, :]

        nans, i = nan_helper(s_ice)
        s_ice[nans] = np.interp(i(nans), i(~nans), s_ice[~nans])
        si_fil = uniform_filter1d(s_ice, size=20)

        t_f = lam1 * si_fil + lam2 + ice * lam3
        TF = t_ice - t_f

        melt = np.abs(SHIflx[vi]["fwfx"]) / 1000 * dx

        # entrainment
        rho_plu = (1 + (plume["s_plu"]) * sBeta + (plume["t_plu"]) * tAlpha) * rnil
        kappa_z = 2e-5
        kappa_x = 2.5e-1
        entrain = entraiment_coeff(
            plume["u_plu"], rho_plu, raw, kappa_z=kappa_z, dx=dx, dz=dz
        )

        if "gd0" in path[vi]:
            sgd = int(path[vi][3:6])
            print("sgd = {} percent of control melt".format(sgd))
        else:
            sgd = 0
            print("no sgd")

        cmelt = np.nancumsum(melt)
        amelt = np.nanmean(melt)
        # safe data for fitting
        mdata["exp"].append(path[vi])
        mdata["T_AW"].append(taw)
        mdata["MeltFlux"].append(cmelt[flxi])
        mdata["AveMelt"].append(amelt)
        mdata["PlumeFlux"].append(np.nanmax(flx))
        mdata["TF"].append(np.nanmean(TF[0:flxi]))
        mdata["sgd"].append(sgd)
        mdata["Entrainment_u"].append(entrain["alpha"])
        mdata["Fe/Fd"].append(entrain["Fe"] / entrain["Fd"])

    mdata = pd.DataFrame(mdata)
    mdata.head()

    if to_file:
        mdata.to_csv("{}.csv".format(filename), index=False)

def extract_prdata(path, prx=21e3, to_file=False):

    global prdata
    coords = TM.coords
    z = coords[0]["z"]
    data = TM.data

    prdata = {"z": z}
    prdf = pd.DataFrame(prdata)
    print(prdf.head())
    for vi in range(np.shape(data)[0]):
        pri = np.min(np.where(coords[vi]["x"] >= prx)[0])
        tpr = TM.data[vi]["t_all"][vi, :, pri]
        spr = TM.data[vi]["s_all"][vi, :, pri]

        prdf["t_{}".format(path[vi])] = tpr
        prdf["s_{}".format(path[vi])] = spr

        print(prdf.head())

    if to_file:
        prdf.to_csv("profile_ts_{:.0f}km.csv".format(prx / 1e3), index=False)


import numpy as np
import pylab
import matplotlib.pyplot as plt
f=open('./fine_val_mean_log.txt',"r")
lines=f.readlines()
score=[]
for line in lines:
    row=line.split(',')
    col=[int(x) for x in row]
    score.append(col[1])
plt.plot(score,label='loss')
plt.xlabel('epoch',fontsize=14)
plt.ylabel('error',fontsize=14)
#plt.title('test')
#plt.legend()
pylab.show()


import pandas as pd
import numpy as np
from sklearn.metrics import accuracy_score, roc_auc_score
import matplotlib.pyplot as plt

# Function to plot KS chart
def plot_ks_chart(ks_table):
    """Function to plot KS chart"""
    ks_table['%cum_true_num'] = ks_table['%cum_true'].str.replace('%','').astype('float')
    ks_table['%cum_false_num'] = ks_table['%cum_false'].str.replace('%','').astype('float')
    plt.figure(figsize=(10,6))
    plt.plot(ks_table.index, ks_table['%cum_true_num'], 'g')
    plt.plot(ks_table.index, ks_table['%cum_false_num'], 'r')
    plt.fill_between(ks_table.index, ks_table['%cum_true_num'], ks_table['%cum_false_num'], color="grey", alpha=0.2, hatch='|')
    plt.xticks(ks_table.index)
    plt.title('KS - Accumulated curves')
    plt.legend(['%true', '%false'], loc='lower right')
    plt.xlabel('Decile')
    plt.ylabel('% Accumulated')
    plt.show()

def ks_calculate(model, x_val, y_val, target='good', verbose=True, buckets=10):
    """Function to calculate KS metric"""
    pred = model.predict(x_val)
    proba = model.predict_proba(x_val)
    if isinstance(y_val, np.ndarray):
        y_val = pd.DataFrame(y_val, columns=[target])
    df_val_ks = y_val.reset_index(drop=True)
    df2 = pd.DataFrame(proba, columns=['p0','p'])
    data_ks = df2.join(df_val_ks)
    ks_score, kstable = ks_calc(data=data_ks, target=target, prob="p", verbose=verbose, buckets=buckets)
    if verbose == True:
        print("accuracy_score: ", accuracy_score(y_val.values, pred))
        print("roc_auc_score: ", roc_auc_score(y_true= y_val.values, y_score= proba[:,1]))
        print("ks_score: ", ks_score)
    return ks_score, kstable

def ks_calc(data=None, target=None, prob=None, verbose=True, buckets=10):
    """Support function to calculate KS metric"""
    data['target0'] = 1 - data[target]
    data['bucket'] = pd.qcut(data[prob], buckets)
    grouped = data.groupby('bucket', as_index = False)
    kstable = pd.DataFrame()
    kstable['min_prob'] = grouped.min()[prob]
    kstable['max_prob'] = grouped.max()[prob]
    kstable['true']   = grouped.sum()[target]
    kstable['false'] = grouped.sum()['target0']
    kstable = kstable.sort_values(by="min_prob", ascending=False).reset_index(drop = True)
    kstable['true_rate'] = (kstable.true / data[target].sum()).apply('{0:.2%}'.format)
    kstable['false_rate'] = (kstable.false / data['target0'].sum()).apply('{0:.2%}'.format)
    kstable['%cum_true']=(kstable.true / data[target].sum()).cumsum()
    kstable['%cum_false']=(kstable.false / data['target0'].sum()).cumsum()
    kstable['KS'] = np.round(kstable['%cum_true']-kstable['%cum_false'], 3) * 100

    #Formating
    kstable['%cum_true']= kstable['%cum_true'].apply('{0:.2%}'.format)
    kstable['%cum_false']= kstable['%cum_false'].apply('{0:.2%}'.format)
    kstable.index = range(1,11)
    kstable.index.rename('Decile', inplace=True)
    pd.set_option('display.max_columns', 9)
    if verbose==True:
        print(kstable)
        print("KS is " + str(max(kstable['KS']))+"%"+ " at decile " + str((kstable.index[kstable['KS']==max(kstable['KS'])][0])))
    return round(max(kstable['KS']),4), kstable


import xgboost as xgb
import argparse
import ConfigParser
import pprint
import pickle
from xgboost import XGBClassifier
from sklearn.datasets import load_svmlight_file
from tools.tools import get_predictedResults_from_file,get_csr_labels
from xg_predict_comp import compare_models,compare_roc_auc,compare_pr_auc,compare_confusion_matrix,compare_path_score

# parser
def arg_parser():
    parser = argparse.ArgumentParser()
    parser.add_argument('-c','--conf', required=True)
    parser.add_argument('-o', '--output', required=False)
    return parser.parse_args()

# configure parser
def conf_parser(conf_path,is_verbose=False):
    global models_path
    global model_names_list
    global thres_list
    global marker_list
    global dataset_list
    global dataset_f_list
    global save_path

    cf = ConfigParser.ConfigParser()
    cf.read(conf_path)

    models_path = cf.get('draw','model_paths').split(',')
    model_names_list = cf.get('draw', 'model_names').split(',')
    thres_list = cf.get('draw','thres').split(',')
    thres_list = [float(i) for i in thres_list]
    marker_list = cf.get('draw','markers').split(',')
    dataset_list = cf.get('draw','datasets').split(',')
    dataset_f_list = cf.get('draw','dataset_formats').split(',')
    save_path = cf.get('draw','save_path')
    if is_verbose:
        pprint.pprint(models_path)
        pprint.pprint(model_names_list)
        pprint.pprint(marker_list)
    return

if __name__ == '__main__':

    models_path = None
    model_names_list = None
    thres_list = None
    marker_list = None
    dataset_list = None
    dataset_f_list = None
    label_f_list = None

    arg = arg_parser()
    conf_parser(arg.conf,is_verbose=True)

    labels_list = list()
    pred_scores_list = list()

    for i,f in enumerate(dataset_f_list):
        print('=============================')
        if f  == 'xgboost':
            print(dataset_list[i])
            print(models_path[i])
            X,y = load_svmlight_file(dataset_list[i])
            X = X.todense()
            dtest = xgb.DMatrix(X,label = y)
            labels = dtest.get_label()
            labels = labels.astype(int)
            model_temp = xgb.Booster(model_file=models_path[i])
            labels_pred = model_temp.predict(dtest)

            print(labels_pred)
            labels_list.append(labels)
            pred_scores_list.append(labels_pred)

    measures_all = compare_models(labels_list,pred_scores_list,model_names_list,thres_list)
    compare_roc_auc(fid=1,measures=measures_all,marker_list=marker_list,save_path = save_path)
    compare_pr_auc(fid=2, measures=measures_all, marker_list=marker_list,save_path = save_path)
    compare_roc_auc(fid=3, measures=measures_all,axis_interval=[0,1,0.9,1],marker_list=marker_list,save_path = save_path)
    compare_pr_auc(fid=4, measures=measures_all, axis_interval=[0,1,0.9,1],marker_list=marker_list,save_path = save_path)
    compare_confusion_matrix(measures_all)


from includes import *
import settings
def safe_get(k,dict):
    if k in dict:
        return dict[k]
    return ""
def get_token(job,city):
    response=requests.get('https://zhaopin.baidu.com/quanzhi?query='+quote(job)+'&city='+quote(city),headers=settings.baiduHeaders)
    html=pq(response.text)
    body=html('body')
    body=str(body)
    nekotStr=''
    count=0
    for c in body:
        if c=='\n':
            if count==7:
                break
            count+=1
        else:
            if count==6:
                nekotStr+=c
    token=nekotStr.replace('data["nekot"]','').replace(';','').replace(' ','')[::-1]
    token=token[:-1].replace('"','')
    return token
def get_list(job,city):
    response = requests.get('https://zhaopin.baidu.com/quanzhi?query=' + quote(job) + '&city=' + quote(city),headers=settings.baiduHeaders)
    text=response.text
    html = pq(text)
    body = html('body')
    body = str(body)
    nekotStr = ''
    count = 0
    for c in body:
        if c == '\n':
            if count == 19:
                break
            count += 1
        else:
            if count == 18:
                nekotStr += c

    jsonText=nekotStr.replace('data["list"]','').replace('=','').replace(' ','')
    jsonList=eval(jsonText.replace(';',''))
    return jsonList
def get_details(jobLists):
    totalList=[]
    for job in jobLists:
        #link=job['url'].replace('\\','')
        dict={
            'jobName':job['title'],
            'company':job['officialname'],
            'positon':job['province'],
            'salary':job['ori_salary']
        }
        totalList.append(dict)
    return totalList
def get_page(page,city,job):
    url='https://zhaopin.baidu.com/quanzhi?query='+quote(job)+'&city='+quote(city)
    if page==1:
        jsonList=get_list(job,city)
        return get_details(jsonList)
    else:
        token=get_token(job,city)
        params={
            'query': quote(job),
            'city': quote(city),
            'is_adq': '1',
            'pcmod': '1',
            'token': quote(token),
            'pn': str(page*20),
            'rn': '20'
        }
        url='https://zhaopin.baidu.com/api/qzasync?'
        for k in params:
            url+=k+'='+params[k]
            if k!='rn':
                url+='&'
        response=requests.get(url,headers=settings.baiduHeaders)
        jsonText=json.loads(response.text)
        dataList=safe_get('disp_data',jsonText['data'])
        return get_details(dataList)
def get_all(city,job):
    if city not in settings.baiduCitys:
        raise Exception("未找到该城市")
    totalDict={}
    totalDict['name'] = 'baiduZhaopin'
    totalDict['city'] = city
    totalDict['job'] = job
    totalDict['pages'] = 35
    totalList=[]
    for i in range(1,36):
        print('正在爬取第'+str(i)+'页')
        lists=get_page(page=i,city=city,job=job)
        for item in lists:
            totalList.append(item)
        totalDict['data'] = totalList
    jsonText = json.dumps(totalDict, ensure_ascii=False)
    return jsonText
def run(city,job,page):
    if city not in settings.baiduCitys:
        raise Exception("未找到该城市")
    if page < 1 or page > 35:
        raise Exception('页数小于1')
    totalDict={}
    totalDict['name']='baiduZhaopin'
    totalDict['city']=city
    totalDict['job']=job
    totalDict['pages']=35
    totalDict['data']=get_page(page=page,city=city,job=job)
    jsonText=json.dumps(totalDict,ensure_ascii=False)
    return jsonText


import unittest
from src.id3 import *
from src.loading_data import *

"""
    authors: Rafał Uzarowicz - https://github.com/RafalUzarowicz
"""

class ID3Test(unittest.TestCase):

    def test_entropy(self):
        loader = ID3DatasetLoader()
        loader.load_classic_dataset()
        data = loader.get_dataset()
        id3_algo = ID3(data, "result")

        entropy_windy_true = id3_algo.entropy(data[data["windy"] == "true"])
        entropy_windy_false = id3_algo.entropy(data[data["windy"] == "false"])

        self.assertAlmostEqual(entropy_windy_true, 1.0, places=8)
        self.assertAlmostEqual(entropy_windy_false, 0.5 - (3 / 4) * log2(3 / 4), places=8)

    def test_gain(self):
        loader = ID3DatasetLoader()
        loader.load_classic_dataset()
        data = loader.get_dataset()
        id3_algo = ID3(data, "result")

        gain_windy = id3_algo.gain(data, "windy")

        self.assertAlmostEqual(gain_windy,
                               id3_algo.entropy(data) - 6 / 14 - (8 / 14) * 0.5 + (8 / 14) * (3 / 4) * log2(3 / 4),
                               places=7)

    def test_find_maximum_gain(self):
        loader = ID3DatasetLoader()
        loader.load_classic_dataset()
        data = loader.get_dataset()
        id3_algo = ID3(data, "result")

        gain_max_attr = id3_algo.find_maximum_gain(data)

        self.assertEqual(gain_max_attr, "outlook")

    def test_find_pivot(self):
        loader = ID3DatasetLoader()
        loader.load_classic_with_numbers_dataset()
        data = loader.get_dataset()
        id3_algo = ID3(data, "result")

        pivot_found_int = id3_algo.find_pivot(data, "number_int")
        pivot_found_float = id3_algo.find_pivot(data, "number_float")

        self.assertAlmostEqual(pivot_found_int, 1.85714285714, places=8)
        self.assertAlmostEqual(pivot_found_float, 4.64285714285, places=8)

    def test_find_average_attribute_values_number(self):
        loader = ID3DatasetLoader()
        loader.load_classic_dataset()
        data = loader.get_dataset()
        id3_algo = ID3(data, "result")

        avg_num = id3_algo.find_average_attribute_values_number()

        self.assertAlmostEqual(avg_num, 2.5, places=8)

    def test_count_good(self):
        loader = ID3DatasetLoader()
        loader.load_classic_dataset()
        data = loader.get_dataset()
        id3_algo = ID3(data, "result")

        predictions = id3_algo.predict(data)
        data["pred"] = predictions
        res = count_good(data, id3_algo.target_att)

        self.assertEqual(res, 14)

if __name__ == '__main__':
    unittest.main()


import customtkinter as tk
from pytube import YouTube

def download_video():
    video_url = entry.get()
    try:
        yt = YouTube(video_url)
        stream = yt.streams.get_highest_resolution()
        max_filesize = stream.filesize
        bytes_downloaded = 0
        progress_var = tk.DoubleVar()
        progress_var.set(0)
        progress_bar['maximum'] = max_filesize

        def progress_callback(stream, chunk, bytes_remaining):
            nonlocal bytes_downloaded
            bytes_downloaded += len(chunk)
            progress_var.set(bytes_downloaded)
            root.update_idletasks()

        stream.download(output_path='/YT_DOWNLOADER', filename=stream.default_filename, on_progress=progress_callback)
        progress_var.set(max_filesize)
    except Exception as e:
        print(e)

root = tk.CTk()
root.geometry("360x140")
root.title('YouTube Downloader')
tk.set_appearance_mode("dark")
tk.set_default_color_theme("dark-blue")

label = tk.CTkLabel(root, text='Enter YouTube URL:')
label.pack(pady=3)

entry = tk.CTkEntry(root)
entry.pack(pady=3)

button = tk.CTkButton(root, text='Download', command=download_video)
button.pack(pady=3)

progress_bar = tk.CTkProgressBar(root, orientation='horizontal', width=300,height=10, mode='determinate')
progress_bar.pack(pady=12)

root.mainloop()


import time

import serial as port
import threading
import os

'''
This class takes a object that has an array 'handPos'
and transmits the array via serial port to connected
devices.
'''
# set access privileges of serial ports
# ls /dev/serial/by-path
# sudo chmod 666 /dev/serial/by-path/****

class ArduinoSerial:
    def __init__(self, serial_port:str='/dev/serial/by-path/pci-0000:0b:00.3-usb-0:1.4.3:1.0'):
        # os.chdir(os.path.expanduser("~"))
        self.port = serial_port
        # os.chmod('/dev/serial/by-path/pci-0000:00:14.0-usb-0:2:1.0',666)
        try:
            self.arduinoPort = port.Serial(self.port, 9600, timeout=1)
            while self.read() != "1000":
                time.sleep(0.01)
            time.sleep(0.1)
            print(f"serial connected on {self.port}")
        except Exception as e:
            # pass
            print(f"Nothing is Connected to Serial Port > {self.port} \n {e}")

    def send(self, msg:list):
        return self.arduinoPort.write(msg)

    def read(self):
        received_message = self.arduinoPort.readline()
        received_message = received_message.decode()
        received_message = received_message.strip()
        return received_message

    def clear_input(self):
        self.arduinoPort.flushInput()


from apps.entity.models.entity import Entity
from apps.core.errors import EntityException, CoreException

ERR = 'Entity does not exist'
ERR_TEXT = 'unable to find entity for this agent'
class EntityRepository():

    @staticmethod
    def fetch_by_agent(agent):
        try:
            entity = agent.entity
            entity_dict = entity.to_dict()
            entity_dict.update({'country': entity.country.iso})
            return entity_dict
        except Entity.DoesNotExist:
            raise EntityException(ERR, {'response_code': '100', 'response_text': ERR})

    @staticmethod
    def fetch_by_agent_code(code):
        try:
            return Entity.objects.get(agent__code=code)
        except Entity.DoesNotExist:
            raise EntityException(ERR, {'response_code': '100', 'response_text': ERR})

    @staticmethod
    def fetch_by_account_number(account_number):
        try:
            return Entity.objects.get(account_number=account_number)
        except Entity.DoesNotExist:
            raise EntityException(ERR, {'response_code': '100', 'response_text': ERR})

    @staticmethod
    def fetch_all_ancestors(entity):
        result = [e for e in entity.get_ancestors(include_self=True)]
        return result

    @staticmethod
    def fetch_entity_by_type_and_hierarchy(_entity, entity_type):
        try:
            entity_ancestors = [_e.brand_name for _e in EntityRepository.fetch_all_ancestors(_entity)]
            entity = Entity.objects.get(category=entity_type, brand_name__in=entity_ancestors)
            return entity
        except Exception as err:
            raise CoreException(err, 'unable to fetch right entity')

    @staticmethod
    def to_json(agent_code):
        try:
            entity = Entity.objects.get(agent__code=agent_code)
            data = {}
            data.update({'brand_name': entity.brand_name})
            return data
        except Entity.DoesNotExist:
            raise EntityException(ERR, {'response_code': '100', 'response_text': ERR})

    @staticmethod
    def fetch_by_entity_code(code):
        try:
            entity = Entity.objects.get(code=code)
            return entity, entity.to_dict()
        except Entity.DoesNotExist:
            raise EntityException(ERR, {'response_code': '100', 'response_text': ERR})

    @staticmethod
    def fetch_by_entity_type(type):
        try:
            entity = Entity.objects.get(category=type)
            return entity, entity.to_dict()
        except Entity.DoesNotExist:
            raise EntityException(ERR, {'response_code': '100', 'response_text': ERR})


import random

sec_num=random.randint(1,100)

guess=0
trys=0

print("You will have 10 guesses")
while trys<10:

    trys=trys+1
    guess=int(input("Enter Guess: "))

    if guess==sec_num:
        print("Guessed it right!")
        break
    elif abs(guess-sec_num)<5:
        print("burning warm")
    elif abs(guess-sec_num)<20 and abs(guess-sec_num)>5 :
        print("warm")
    elif guess>1 & guess<100:
        print("cold")
    else:
        print("Invalid Input")

if(guess!=sec_num):
    print("Opps! Ran out of guesses! The number was")
    print(sec_num)