import pandas as pd
import numpy as np
import xgboost as xgb
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import pickle
from sklearn import preprocessing
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials

#read an plk data
k = pd.read_pickle('old_items.plk') #to load 123.pkl back to the dataframe df

df=k
df.columns = df.columns.str.replace(' ', '_')
df = k.drop(['info','broker','Name_brocker','Prisutveckling'], axis = 1)

'''
function to clean data from scraper.
    Args:
        df: Data from scraper
'''
def rename_cloums(df):
    df['streetName']=df['streetName'].astype(str)
    df['price']=df['price'].astype(str).astype(float)
    df['Begärt_pris']=df['Begärt_pris'].astype(str).astype(float)
    df['ppk']=df['Pris_per_kvadratmeter'].astype(str).astype(float)
    df['Antal_rum']=df['Antal_rum'].str.replace('rum','')
    df['Antal_rum']=df['Antal_rum'].str.replace(',','.')
    df['Boarea']=df['Boarea'].str.replace(',','.')
    df['date_sold']=df['date_sold'].str.replace(' ','')
    df['date_sold']=df['date_sold'].str.replace('januari','-jan-')
    df['date_sold']=df['date_sold'].str.replace('february','-feb-')
    df['date_sold']=df['date_sold'].str.replace('december','-dec-')
    df['date_sold']=df['date_sold'].str.replace('november','-nov-')
    df['date_sold']=df['date_sold'].str.replace('oktober','-oct-')
    df['date_sold'] = pd.to_datetime(df['date_sold'])
    df['Driftskostnad'].astype(str).astype(float)
    df['Byggår']=df['Byggår'].str.replace('-','')
    df['Byggår'].astype(str)
    df=df.drop(['Pris_per_kvadratmeter'], axis = 1)
    #df['Prisutveckling'] =  pd.to_numeric(df['Prisutveckling'], errors='coerce')
    df['Antal_rum'] = df['Antal_rum'].astype(float)
    df['Boarea'] = df['Boarea'].astype(float)
    df['avgift'] = pd.to_numeric(df['Avgift/månad'], errors='coerce')
    df['Driftskostnad'] = df['Driftskostnad'].astype(float)
    df['Byggår'] = df['Byggår'].astype(float)
    df['ppk'] = df['ppk'].astype(float)
    df['dayofweek'] = df['date_sold'].dt.dayofweek
    df['quarter'] = df['date_sold'].dt.quarter
    df['month'] = df['date_sold'].dt.month
    #df['year'] = df['date_sold'].dt.year
    df['dayofyear'] = df['date_sold'].dt.dayofyear
    #df['dayofmonth'] = df['date_sold'].dt.day
    lbl = preprocessing.LabelEncoder()
    df['encode_street'] = lbl.fit_transform(df['streetName'].fillna('').astype(str))
    df.loc[df['area'].str.contains('Såld den'), 'area'] = 'NA'
    df['area']=df['area'].str.replace(' ','')
    df['area']=df['area'].str.replace('-','/')
    df['encode_area'] = lbl.fit_transform(df['area'].fillna('').astype(str))
    return df
df=rename_cloums(df)

'''
Create traing test data. use inside optimze and traning functions
    Args:
        df: Data from scraper
        size: splitt betewwn train/test, ex 0.2
'''
def create_traning_test_data(df,size):
    # Splitt data in X and Y streetName
    X=df.filter(items=['encode_street','encode_area','Begärt_pris','encode_name','Antal_rum','Boarea','avgift',
                       'Driftskostnad','byggår','dayofweek','quater','month','dayofyear'])
    #target data.
    y=df.filter(items=['price'])
    data_dmatrix = xgb.DMatrix(data=X,label=y)
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=size, random_state=1234567)
    return X_train, X_test, y_train, y_test

'''
Definde grid for optimations serche
'''
space={'max_depth': hp.quniform("max_depth", 3, 10, 1),
       'gamma': hp.uniform('gamma',0,3),
       'reg_alpha' : hp.quniform('reg_alpha', 0,90,1),
       'reg_lambda' : hp.uniform('reg_lambda', 0,40),
       'colsample_bytree' : hp.uniform('colsample_bytree', 0.1,1),
       'min_child_weight' : hp.quniform('min_child_weight', 0, 16, 1),
       'n_estimators': hp.quniform('n_estimators', 300, 3000, 1),
       'learning_rate': hp.loguniform('learning_rate', np.log(0.0001), np.log(1.0)),
       'subsample': hp.uniform('subsample', 0.5, 1.0),
       'early_stopping_rounds': hp.quniform('early_stopping_rounds', 250, 500, 1)}

#######################################
#pre_train hyper optimation for xgboost
#######################################

'''
Prepper xg-boost omtpimatxzion for apparment data
    Args:
        df:DataFarme
        space: grid of hypoeroppt parameters
'''
def hyperparameter_tuning(space):
    reg=xgb.XGBRegressor(max_depth = int(space['max_depth']),
                         gamma = space['gamma'],
                         reg_alpha = int(space['reg_alpha']),
                         reg_lambda = space['reg_lambda'],
                         colsample_bytree = space['colsample_bytree'],
                         min_child_weight = int(space['min_child_weight']),
                         n_estimators = int(space['n_estimators']),
                         learning_rate = space['learning_rate'],
                         subsample = space['subsample'],
                         objective = 'reg:squarederror')

    X_train, X_test, y_train, y_test = create_traning_test_data(df,0.2)
    X_train=X_train.fillna(0)
    y_test=y_test.fillna(0)
    X_train=X_train.fillna(0)
    y_train=y_train.fillna(0)
    evaluation = [( X_train, y_train), ( X_test, y_test)]

    reg.fit(X_train, y_train,
            eval_set=evaluation, eval_metric="rmse",
            early_stopping_rounds=400,verbose=False)

    pred = reg.predict(X_test)
    mse= mean_squared_error(y_test, pred)
    rmse=np.sqrt(mean_squared_error( y_test,pred))
    #change the metric if you like
    return {'loss':rmse, 'status': STATUS_OK }

def run_optimaziation(hyperparameter_tuning,space,evals,save):
    trials = Trials()
    best = fmin(fn=hyperparameter_tuning,
                space=space,
                algo=tpe.suggest,
                max_evals=evals,
                trials=trials)
    print(best)
    if save:
        pickle.dump(best, open("best_param_xgb.p", "wb"))

    return best
#######################################
#optimation for xgboost
#######################################
'''
function to retring wight of the hyperoptimatsion
    Args:
        hyperparameter_tuning: fuction to optimize
        space: grid of hypoeroppt parameters
        evals: number of runs
        save: False/True statet what to save new params
'''
#best=run_optimaziation(hyperparameter_tuning,space,2,False)

'''
Train XG-Boost modeil with optimazet wights from histroul data set
    Arg:
        df: dataset what to predict on
        best: True if what to use best params
'''

def appartmet_model(df,best):
    if best:
        best = pickle.load(open("best_param_xgb.p", "rb"))

    X_train, X_test, y_train, y_test = create_traning_test_data(df,0.2)
    #fit moddel
    xgb_model_opt = xgb.XGBRegressor(objective='reg:squarederror',
                           learning_rate =best['learning_rate'],
                           n_estimators =int(best['n_estimators']),
                           max_depth = int(best['max_depth']),
                           gamma = best['gamma'],
                           reg_alpha = int(best['reg_alpha']),
                           min_child_weight=best['min_child_weight'],
                           colsample_bytree=best['colsample_bytree'])
    xgb_model_opt.fit(X_train, y_train,
            eval_set=[(X_train, y_train), (X_test, y_test)],
            early_stopping_rounds=10000,
            verbose=False)

    # calculate the auc score
    y_pred = xgb_model_opt.predict(X_test)
    feature_important = xgb_model_opt.get_booster().get_score(importance_type='weight')
    keys = list(feature_important.keys())
    values = list(feature_important.values())

    data_feuteres = pd.DataFrame(data=values, index=keys, columns=["score"]).sort_values(by = "score", ascending=False)
    print(data_feuteres)

    # save model to file
    pickle.dump(xgb_model_opt, open("xg_opti.pickle.dat", "wb"))
    print(xgb_model_opt)

    #y_pred = xgb_model.predict(X)
    data_pred = pd.DataFrame(y_pred)
    data_pred = data_pred.reset_index(drop=True)
    data_ytest = pd.DataFrame(y_test)
    data_ytest = data_ytest.reset_index(drop=True)

    result = pd.concat([data_ytest, data_pred], axis=1)
    result.columns = ['price', 'pred_price']
    result['RSME']=np.sqrt(mean_squared_error(result['price'],result['pred_price']))
    result['error'] = (abs(result['price']-result['pred_price'])/result['pred_price'])
    print(result['RSME'].mean())
    print(result['error'].mean())

    return result['error'].mean(), data_feuteres

error,data_feuteres=appartmet_model(df,True)
print('Error',error)
print('After',data_feuteres)

'''
run train moduel on DataFrame
    Arg:
        df: dataset what to predict on
        pre_train_model: Model from prives train model
'''

def run_model(df,pre_train_model):
    #load an pre train moduel
    if pre_train_model:
        loaded_model = pickle.load(open("xg_opti.pickle.dat", "rb"))
    # Splitt data in X and Y streetName
    X=df.filter(items=['encode_street','encode_area','Begärt_pris','encode_name','Antal_rum','Boarea','avgift',
                       'Driftskostnad','byggår','dayofweek','quater','month','dayofyear'])
    yhat = loaded_model.predict(X)
    return yhat

yhat=run_model(df.iloc[-1:],True)
print(df.iloc[-1:])
print(yhat)


import datetime  # to add timestamps on every block in blockchain
import hashlib  # library that is ued to hash the block
import json  # to communicate in json data
# Flask to implement webservices jsonify to see the jsop message/response
# request help us to connect all the nodes of the blockchain together froming the p2p network
from flask import Flask, jsonify, request
# it will help us to verify that all the blockchain have same blockhain or not http requests (used in replace_cahin)
import requests
from uuid import uuid4
from urllib.parse import urlparse

# Building a Blockchain

class Blockchain:

    def __init__(self):
        self.chain = []  # our main block chain
        # now we will create the list of transation which will record the all transactions
        self.transactions = []
        # create_block used to create the block in blockchain so it is executed only when the block is mined(meaning it has winnnig proof_of_work=proof)    proof=0 and previous_hash='0' for the genesis block
        self.create_block(proof=0, previous_hash='0')
        # nodes will contains the unique identifier of the address of all nodes in p2p network
        self.nodes = set()  # we have taken set() instead of list because we know that address are randomly generated by uuid4 to avoid duplicacy in it
    # part1

    def create_block(self, proof, previous_hash):
        block = {  # dictionary of python data structure
            'index': len(self.chain)+1,
            'timestamp': str(datetime.datetime.now()),
            'proof': proof,  # works like a nounce of block stops when we reach at or below the target
            'previous_hash': previous_hash,
            'transactions': self.transactions}
        self.transactions = []  # this need to be done bcoz we cant have duplicates lists of transactions in the further blocks so empty the transation that had been added in the block
        self.chain.append(block)
        return block

    def get_previous_block(self):
        return self.chain[-1]

    def proof_of_work(self, previous_proof):
        new_proof = 1
        check_proof = False
        while check_proof is False:
            hash_operation = hashlib.sha256(
                str(new_proof**2-previous_proof**2).encode()).hexdigest()
            if hash_operation[:4] == '0000':
                check_proof = True
            else:
                new_proof += 1
        return new_proof  # it is just a no. corresponding to the game solved by person is having a hash with trailing 4 zeroe's

    # hash of a block is created after generating block thats we have only use previous_hash because its already created
    def hash(self, block):
        encoded_block = json.dumps(block, sort_keys=True).encode()
        return hashlib.sha256(encoded_block).hexdigest()

    def is_chain_valid(self, chain):
        # reference of first block stored genesis block
        previous_block = chain[0]
        block_index = 1  # required for iteration
        while block_index < len(chain):
            block = chain[block_index]  # cuurent block
            # checking weather the refernce stored in property previus_hash is currently matched or not with the hash of previous block using hash function
            if block['previous_hash'] != self.hash(previous_block):
                return False
            previous_proof = previous_block['proof']
            proof = block['proof']
            # verfying the proof of block with the data proof and previous proof it is easy then creating the proof
            hash_operation = hashlib.sha256(
                str(proof**2 - previous_proof**2).encode()).hexdigest()
            # the more is zero's the more is harder to mine the block
            if hash_operation[:4] != '0000':
                return False
            previous_block = block
            block_index += 1
        return True

    # functions used to get add the transactions to the lists
    def add_transaction(self, senders, receiver, amount):
        self.transactions.append({
            'senders': senders,
            'receiver': receiver,
            'amount': amount
        })
        previous_block = self.get_previous_block()
        # +1 beacause before mining the transaction are added so new_block index will be +1 then previous
        return previous_block['index']+1
    # part-1 ends

    # part-3--> dealing with decentarlized application and transactions

    # this function allow us to add different nodes to chain

    def add_node(self, address):  # generating the decentarlized application
        # we need to parse the url before adding it
        parsed_url = urlparse(address)
        # .netloc gives us the unique identifier of the node address removing the unrequired part from it
        self.nodes.add(parsed_url.netloc)

    # this function help us to solve the problem of consensus protocols (competing chain)

    def replace_chain(self):
        # this variable help us to find the length of longest chain among different network
        max_length = len(self.chain)
        longest_chain = None
        network = self.nodes  # this variable will hold the address of all the nodes in network
        for node in network:
            # we know the nodes array will hold only the netlock value in nodes so we are going to use taht and make a request to that node check its length
            # using the requests library we make a requests to that node address ([f'http://{node}/get_chain']  -->  [f'http://127.0.0.5000/get_chain')]
            response = requests.get(f'http://{node}/get_chain')
            if response.status_code == 200:  # this ids the vode chaeck something is received in request
                length = response.json()['length']
                chain = response.json()['chain']
                if length > max_length and self.is_chain_valid(chain):
                    max_length = length
                    longest_chain = chain
            # this will happen in every node of network
        if longest_chain:
            # if this chain is shorter than otherit will be updated
            self.chain = longest_chain
            return True
            # if this chain is only longest in network than return false and no update
        return False
    # part-3 ends
# Mining our Blockchain

app = Flask(__name__)

# Creating a Blockchain
# creating the instance of blockchain
blockchain = Blockchain()

# Mining the blockchain
# create an random and unique address for the node on port 5000
# this is the address used by to send the whale coin when the miner mines the wahle coin
node_address = str(uuid4()).replace('-', '')

# part-2

@app.route('/mine_block', methods=['GET'])
def mine_block():
    previous_block = blockchain.get_previous_block()
    previous_proof = previous_block['proof']
    proof = blockchain.proof_of_work(previous_proof)
    previous_hash = blockchain.hash(previous_block)
    # miners price
    # usually the reciever public address is created when  user generate the wallet and mining pool send the coin after mining the block to miner address present in the bat file which is edited after downloading the software
    blockchain.add_transaction(node_address, 'Bhavjot', 1)
    # when created blockchain is called all the transactions performed will be inserted inside the current created block and when appended in transactions it will be again change to [] empty to avoid the duplicacy
    block = blockchain.create_block(proof, previous_hash)
    response = {'message': 'Congratulations, you just mined a block! 😈😈😈😈😈🤓🤓🤓',  # response is a json data
                'index': block['index'],
                'timestamp': block['timestamp'],
                'proof': block['proof'],
                'previous_hash': block['previous_hash'],
                'transactions': block['transactions']}
    return jsonify(response), 200

# getting all blocks in chain

@app.route('/get_chain', methods=['GET'])
def get_chain():
    response = {
        'chain': blockchain.chain,
        'length': len(blockchain.chain)
    }
    return jsonify(response), 200

# custom message

@app.route('/', methods=['GET'])
def custom_message():
    response = {
        'message': 'Congratulations you are on Whalecoin 🐳🐳🐳🐳🐳🐳'
    }
    return jsonify(response), 200

# part-2 ends
# creating the transactions

@app.route('/add_transactions', methods=['POST'])
def add_transaction():
    # this will help us to extract te post request made in postman like req.params.name in express
    json = request.get_json()
    # this will hep us to check that all the parameters are present or not for adding the transactions
    transaction_keys = ['sender', 'receiver', 'amount']
    if not all(key in json for key in transaction_keys):
        return 'Some elements of the transaction are missing', 400
    index = blockchain.add_transaction(
        json['sender'], json['receiver'], json['amount'])
    # when the block is mined all the transations in lists is added to block
    response = {'message': f'This transaction will be added to Block {index}'}
    return jsonify(response), 201

@app.route('/connect_node', methods=['POST'])
def connect_node():
    json = request.get_json()  # we will get request message send from postman
    # {'nodes':['http://127.0.0.1:5000','http://127.0.0.1:5001','http://127.0.0.1:5003',...]} when adding nodes using add_nodes 127.0.0.1:5001 it will be extracted using netloc
    nodes = json.get('nodes')
    if nodes is None:
        return "No node", 400
    for node in nodes:
        blockchain.add_node(node)  # add our nodes to network
    response = {'message': 'All the nodes are now connected. The Whalecoin 🐳🐳🐳🐳🐳🐳 Blockchain now contains the following nodes:',
                'total_nodes': list(blockchain.nodes)}
    return jsonify(response), 201

# Replacing the chain by the longest chain if needed
# this function will present in every node of blockchain and always checked so that the node remain upadatesd with other blockchains by hitiing replace_chain URL
@ app.route('/replace_chain', methods=['GET'])
def replace_chain():
    # using the above defined function in class
    is_chain_replaced = blockchain.replace_chain()
    if is_chain_replaced:  # means the current blockchain was the shortest one and it is replaced
        response = {'message': 'The nodes had different chains so the chain was replaced by the longest one.',
                    'new_chain': blockchain.chain}
    else:  # means the current blockchain was not the shortest one and it is not replaced
        response = {'message': 'All good. The chain is the largest one.',
                    'actual_chain': blockchain.chain}
    return jsonify(response), 200

# Running the app
# host= '0.0.0.0' specifies that it is available publicily
app.run(host='0.0.0.0', port=5001)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : nn_module.py
# @Author: Gavin
# @Date  : 2018/10/25
# @Desc  :
from torch.autograd import Variable
import torch
from torch import nn

class RNN(nn.Module):
    """
    lstm模型
    """

    def __init__(self, input_size, hidden_size, num_layers):
        super(RNN, self).__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.h_state = None

        self.rnn = nn.LSTM(
            input_size=self.input_size,
            hidden_size=self.hidden_size,
            num_layers=self.num_layers,
            batch_first=True,
        )
        self.out = nn.Linear(hidden_size, 1)

    def forward(self, x, h_state):
        r_out, h_state = self.rnn(x, h_state)

        '''outs = []  # save all predictions
        for time_step in range(r_out.size(1)):  # calculate output for each time step
            outs.append(self.out(r_out[:, time_step, :]))'''
        # outs.append(self.out(r_out[:, -1, :]))
        #return torch.stack(outs, dim=1), h_state
        out = self.out(r_out)
        return out, h_state

def repackage_h_state(h):
    """
    隐藏层解包
    :param h:
    :return:
    """
    if type(h) is torch.Tensor:
        return Variable(h.data)
    else:
        return tuple(repackage_h_state(i) for i in h)


""" This module defines serializers for recipe models """

from chezcosa.recipes.models import Recipe
from rest_framework import serializers

# pylint: disable=too-few-public-methods
class RecipeSerializer(serializers.HyperlinkedModelSerializer):
    """ This class defines the serializer for the recipe model """

    # pylint: disable=too-few-public-methods
    class Meta:
        """ This class lists the fields of the model that should be part of the
        view
        """

        model = Recipe
        fields = (
            'url', 'user', 'created', 'title', 'category', 'ingredients',
            'comment', 'image_url', 'thumb_url'
        )


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010 Radim Rehurek <radimrehurek@seznam.cz>
# Licensed under the GNU LGPL v2.1 - http://www.gnu.org/licenses/lgpl.html

"""
Blei's LDA-C format.
"""

from __future__ import with_statement

from os import path
import logging

from gensim import interfaces, utils
from gensim.corpora import IndexedCorpus
from six.moves import xrange

logger = logging.getLogger('gensim.corpora.bleicorpus')

class BleiCorpus(IndexedCorpus):
    """
    Corpus in Blei's LDA-C format.

    The corpus is represented as two files: one describing the documents, and another
    describing the mapping between words and their ids.

    Each document is one line::

      N fieldId1:fieldValue1 fieldId2:fieldValue2 ... fieldIdN:fieldValueN

    The vocabulary is a file with words, one word per line; word at line K has an
    implicit ``id=K``.
    """

    def __init__(self, fname, fname_vocab=None):
        """
        Initialize the corpus from a file.

        `fname_vocab` is the file with vocabulary; if not specified, it defaults to
        `fname.vocab`.
        """
        IndexedCorpus.__init__(self, fname)
        logger.info("loading corpus from %s" % fname)

        if fname_vocab is None:
            fname_base, _ = path.splitext(fname)
            fname_dir = path.dirname(fname)
            for fname_vocab in [
                        utils.smart_extension(fname, '.vocab'),
                        utils.smart_extension(fname, '/vocab.txt'),
                        utils.smart_extension(fname_base, '.vocab'),
                        utils.smart_extension(fname_dir, '/vocab.txt'),
                        ]:
                if path.exists(fname_vocab):
                    break
            else:
                raise IOError('BleiCorpus: could not find vocabulary file')

        self.fname = fname
        with utils.smart_open(fname_vocab) as fin:
            words = [utils.to_unicode(word).rstrip() for word in fin]
        self.id2word = dict(enumerate(words))

    def __iter__(self):
        """
        Iterate over the corpus, returning one sparse vector at a time.
        """
        lineno = -1
        with utils.smart_open(self.fname) as fin:
            for lineno, line in enumerate(fin):
                yield self.line2doc(line)
        self.length = lineno + 1

    def line2doc(self, line):
        parts = utils.to_unicode(line).split()
        if int(parts[0]) != len(parts) - 1:
            raise ValueError("invalid format in %s: %s" % (self.fname, repr(line)))
        doc = [part.rsplit(':', 1) for part in parts[1:]]
        doc = [(int(p1), float(p2)) for p1, p2 in doc]
        return doc

    @staticmethod
    def save_corpus(fname, corpus, id2word=None, metadata=False):
        """
        Save a corpus in the LDA-C format.

        There are actually two files saved: `fname` and `fname.vocab`, where
        `fname.vocab` is the vocabulary file.

        This function is automatically called by `BleiCorpus.serialize`; don't
        call it directly, call `serialize` instead.
        """
        if id2word is None:
            logger.info("no word id mapping provided; initializing from corpus")
            id2word = utils.dict_from_corpus(corpus)
            num_terms = len(id2word)
        else:
            num_terms = 1 + max([-1] + id2word.keys())

        logger.info("storing corpus in Blei's LDA-C format into %s" % fname)
        with utils.smart_open(fname, 'wb') as fout:
            offsets = []
            for doc in corpus:
                doc = list(doc)
                offsets.append(fout.tell())
                parts = ["%i:%g" % p for p in doc if abs(p[1]) > 1e-7]
                fout.write(utils.to_utf8("%i %s\n" % (len(doc), ' '.join(parts))))

        # write out vocabulary, in a format compatible with Blei's topics.py script
        fname_vocab = utils.smart_extension(fname, '.vocab')
        logger.info("saving vocabulary of %i words to %s" % (num_terms, fname_vocab))
        with utils.smart_open(fname_vocab, 'wb') as fout:
            for featureid in xrange(num_terms):
                fout.write(utils.to_utf8("%s\n" % id2word.get(featureid, '---')))

        return offsets

    def docbyoffset(self, offset):
        """
        Return the document stored at file position `offset`.
        """
        with utils.smart_open(self.fname) as f:
            f.seek(offset)
            return self.line2doc(f.readline())

# endclass BleiCorpus


import sqlite3

INIT_DB = [
	"""
	CREATE TABLE `Task` (
		`id`	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		`title`	TEXT NOT NULL,
		`created`	TEXT NOT NULL DEFAULT (datetime('now')),
		`deadline`	TEXT,
		`start`	TEXT NOT NULL DEFAULT (datetime('now')),
		`priority`	INTEGER NOT NULL DEFAULT 1,
		`done`	TEXT,
		`context`	INTEGER NOT NULL REFERENCES Context(id) ON DELETE CASCADE
	);
	""",
	"""
	CREATE TABLE `Context` (
		`id`	INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
		`path`	TEXT NOT NULL UNIQUE,
		`priority`	INTEGER NOT NULL DEFAULT 1,
		`visibility`	TEXT NOT NULL DEFAULT 'normal'
	);
	""",
	"""
	INSERT INTO `Context` (path) VALUES ('')
	""",
	"""
	CREATE INDEX `PathIndex` ON `Context` (`path` ASC);
	"""
]

def main():
	conn = sqlite3.connect('data.sqlite')
	for stmt in INIT_DB:
		conn.execute(stmt)

if __name__ == '__main__':
	main()


import os
from shutil import copy2

def p2j():
    # get current working directory (cwd)
    cwd = os.getcwd()

    # create log directory if not already in existence -------------------------------------
    log_dir = cwd + "/logs/"
    dir_exist = os.path.exists(log_dir)

    if not dir_exist:
        os.mkdir(log_dir)

    # create a directory called images is it doesn't already exist -------------------------
    image_dir = cwd + "/images/"
    dir_exist = os.path.exists(image_dir)

    if not dir_exist:
        os.mkdir(image_dir)

    # create a directory called backup_logs if it doesn't exist ---------------------------
    backup_dir = cwd + "/backup_logs/"
    dir_exist = os.path.exists(backup_dir)

    if not dir_exist:
        os.mkdir(backup_dir)

    # check if any log files exist ---------------------------------------------------------
    for FILE in os.listdir(log_dir):

        # if the file is a log file
        if FILE.endswith(".log"):

            # copy log to backup folder
            copy2(log_dir + FILE, backup_dir)

            # find jpeg start and end
            log = open(log_dir + FILE, 'rb')
            bytes = log.read()
            start = bytes.find(b'\xFF\xD8')
            end = bytes.find(b'\xFF\xD9') + 1

            remainder = bytes[start:end]

            # store image in image_dir as .jpeg
            temp = FILE[:].replace('.log', '.jpeg')
            output = image_dir + temp
            image = open(output, 'wb')
            image.write(remainder)

            # delete log file
            os.remove(log_dir + FILE)

        else:
            # delete log file
            os.remove(log_dir + FILE)


from src.patterns.command import SellStockOrder, BuyStockOrder, Agent, Stock

def test_command():
    stock = Stock()
    sell_stock_order = SellStockOrder(stock)
    buy_stock_order = BuyStockOrder(stock)

    agent = Agent()
    agent.place_order(sell_stock_order)
    agent.place_order(buy_stock_order)

    assert len(agent._order_quque) == 2


"""
Given an array of integers and an integer k, find out whether there are two distinct indices
i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
"""

class Solution(object):
    def containsNearbyDuplicate(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: bool
        """
        hash_table = {}
        for i in range(len(nums)):
            if nums[i] in hash_table and i - hash_table[nums[i]] <= k:
                return True
            else:
                hash_table[nums[i]] = i
        return False

if __name__ == '__main__':
    mySol = Solution()
    x = [1, 2, 1]
    y = 2
    print('input: %s and %s' % (x, y))
    print('output: %s' % mySol.containsNearbyDuplicate(x, y))


import itertools
from functools import reduce

import coreapi
import coreschema
from django.conf import settings
from django.db.models import Q
from django.utils import timezone
from django_filters import rest_framework as filters
from guardian.shortcuts import get_objects_for_user
from rest_framework import mixins, viewsets, parsers, status
from rest_framework.decorators import action
from rest_framework.exceptions import NotFound, ValidationError, PermissionDenied
from rest_framework.response import Response

from alerts import engine
from alerts.models import (
    Ticket, TicketLog, Broadcast, TicketComment, PublicAlertMessage,
    AuthorizationRequest, AlertDisconnection, ManualAlertLog,
)
from alerts.modules.base_states import ALERT_STATES, EVENT_STATES, AUTHORIZATION_LEVELS, ALL_TICKET_STATES
from alerts.modules.conditions import get_next_authorization
from api.v1.handlers.alert_utils import get_ticket_bucket
from api.v1.serializers.ticket_serializers import (
    TicketListSerializer,
    TicketSerializer,
    TicketLogSerializer,
    BroadcastSerializer,
    TicketCommentSerializer,
    TicketArchiveActionSerializer,
    PublicAlertMessageSerializer,
    AuthorizationRequestSerializer,
    AuthorizationRequestTicketSerializer,
    AuthorizationRequestCreateSerializer,
    AuthorizationRequestUpdateSerializer,
    AlertDisconnectionSerializer,
    ManualAlertLogSerializer
)
from api.v1.serializers.user_serializers import UserSerializer, serialize_author
from base.filters import FilterListBackend
from base.permissions import Authenticated, UserPermissionChecker
from base import schemas
from documents.models import Document
from documents.utils import serialize_docs_for_upload, upload_docs
from documents.views import DocumentReadOnlyViewSet, DocumentUploadMixin
from remotes.dispatch import send_simple_message, send_simple_smc_message
from targets.mixins import NestedInTargetMixin
from targets.models import Target

class TicketFilter(filters.FilterSet):
    open = filters.CharFilter(
        method='open_filter',
        help_text="Tickets still open",
    )
    archived = filters.BooleanFilter(
        help_text="Archived tickets, which might or might not be open",
    )
    evaluable = filters.BooleanFilter(
        help_text="Evaluable tickets, which might or might not be open",
    )
    module = filters.CharFilter(
        help_text="The exact name of the module specification",
    )
    level = filters.NumberFilter(method='level_filter')
    level_lte = filters.NumberFilter(method='level_filter')
    level_lt = filters.NumberFilter(method='level_filter')
    level_gte = filters.NumberFilter(method='level_filter')
    level_gt = filters.NumberFilter(method='level_filter')
    group = filters.CharFilter(
        method='group_filter',
        help_text="The comma-separated groups the ticket belongs to",
    )

    class Meta:
        model = Ticket
        fields = ('open', 'archived', 'evaluable', 'module')

    def open_filter(self, queryset, name, value):
        if name == 'open' and value is not None and \
                value.lower() in ('true', 'yes', '1', 'y'):
            return queryset.exclude(state__exact=Ticket.TicketState.CLOSED)
        if name == 'open' and value is not None and \
                value.lower() in ('false', 'no', '0', 'n'):
            return queryset.filter(state__exact=Ticket.TicketState.CLOSED)
        return queryset

    def level_filter(self, queryset, name, value):
        op = {
            'level': '',
            'level_lte': '__lte',
            'level_lt': '__lt',
            'level_gte': '__gte',
            'level_gt': '__gt',
        }.get(name)
        if op is None:
            return queryset
        return queryset.filter(**{f'result_state__level{op}': int(value)})

    def group_filter(self, queryset, name, value):
        if name != 'group':
            return queryset
        groups = frozenset(filter(bool, (value or "").split(",")))
        if not groups:
            return queryset
        return queryset.filter(reduce(
            lambda acc, q: acc & q,
            map(lambda g: Q(groups__contains=f"/{g}/"), groups),
        ))

class TicketsView(viewsets.ModelViewSet):
    permission_classes = (Authenticated,)
    filter_backends = [FilterListBackend]
    filterset_class = TicketFilter
    serializer_class = TicketListSerializer

    def has_target_perm(self, target, codename):
        checker = getattr(self, '_checker', None)
        if checker is None:
            checker = UserPermissionChecker(self.request.user)
            checker.prefetch_perms(Target.objects.all())
            setattr(self, '_checker', checker)
        return checker.has_perm(codename, target)

    def get_queryset(self):
        perms = [
            f'targets.ticket.{state}.read'
            for state in ALL_TICKET_STATES
        ]
        targets = get_objects_for_user(
            user=self.request.user,
            perms=perms,
            klass=Target.objects.all(),
            any_perm=True
        )
        if len(targets) > 0:
            ticket_filters = []
            for target in targets:
                state_filters = [
                    Q(state__startswith=state)
                    for state in ALL_TICKET_STATES
                    if self.has_target_perm(target, f'targets.ticket.{state}.read')
                ]
                ticket_filters.append(
                    Q(target=target) & reduce(lambda acc, q: acc | q, state_filters)
                )
            queryset = Ticket.objects.filter(
                reduce(lambda acc, q: acc | q, ticket_filters)
            ).prefetch_related('target')
        else:
            queryset = Ticket.objects.none()
        return queryset

class TicketView(NestedInTargetMixin, viewsets.ReadOnlyModelViewSet):
    permission_classes = (Authenticated,)
    filter_backends = [FilterListBackend]
    filterset_class = TicketFilter

    def get_queryset(self):
        state_filters = [
            Q(state__startswith=state)
            for state in ALL_TICKET_STATES
            if self.has_target_perm(f'ticket.{state}.read')
        ]
        if len(state_filters) > 0:
            queryset = Ticket.objects.filter(
                target=self.target
            ).prefetch_related(
                'target'
            ).filter(
                reduce(lambda acc, q: acc | q, state_filters)
            )
        else:
            queryset = Ticket.objects.none()
        return queryset

    def get_serializer_class(self):
        if self.action == 'list':
            return TicketListSerializer
        return TicketSerializer

    def retrieve(self, request, *args, **kwargs):
        instance = self.get_object()
        state = instance.state_group
        serializer = TicketSerializer(instance, context={
            "children": self.has_target_perm(f'ticket.{state}.children.read'),
            "logs": self.has_target_perm(f'ticket.{state}.log.read'),
            "event_data": self.has_target_perm(f'ticket.{state}.event_data.read'),
        })
        return Response(serializer.data)

    @action(methods=["post"], detail=True, serializer_class=TicketArchiveActionSerializer)
    def archive(self, request, **kwargs):
        ticket = self.get_object()
        action_serializer = TicketArchiveActionSerializer(
            ticket,
            data=request.data,
            context=self.get_serializer_context(),
        )
        action_serializer.is_valid(raise_exception=True)
        action_serializer.save()
        ticket.refresh_from_db()
        return Response(data=self.get_serializer_class()(ticket).data)

class NestedInTicketMixin(NestedInTargetMixin):
    _ticket = None

    def resolve_ticket(self):
        pk = self.kwargs.get("ticket_pk")
        ticket = Ticket.objects.filter(target=self.target, pk=pk).first()
        if ticket is None or not self.has_target_perm(f'ticket.{ticket.state_group}.read'):
            raise NotFound(detail="Ticket not found.")
        return ticket

    @property
    def ticket(self):
        if self._ticket is None:
            self._ticket = self.resolve_ticket()
        return self._ticket

    def has_ticket_perm(self, suffix):
        return self.has_target_perm(f'ticket.{self.ticket.state_group}.{suffix}')

class TicketLogView(NestedInTicketMixin, viewsets.ReadOnlyModelViewSet):
    permission_classes = (Authenticated,)
    serializer_class = TicketLogSerializer
    parser_classes = (parsers.JSONParser, parsers.MultiPartParser)

    def get_queryset(self):
        if not self.has_ticket_perm('log.read'):
            raise PermissionDenied()
        return self.ticket.logs.all()

    def get_serializer_context(self):
        ctx = super().get_serializer_context()
        return {**ctx, "kwargs": self.kwargs}

    def create(self, request, *args, **kwargs):
        if not self.has_ticket_perm('log.write'):
            raise PermissionDenied()

        serializer = self.serializer_class(data=request.data)
        serializer.is_valid(raise_exception=True)
        message = serializer.validated_data.pop('message')
        log = serializer.save(
            ticket=self.ticket,
            author=serialize_author(request.user),
            meta={'message': message}
        )
        folder = self.ticket.get_document_folder('log', log.id)
        for file in request.FILES.values():
            log.documents.add(
                Document.objects.create(
                    folder=folder,
                    file=file,
                    name=str(file),
                    uploaded_by=request.user
                )
            )
        return Response(data=self.serializer_class(log).data, status=201)

class TicketLogDocumentView(NestedInTicketMixin, DocumentReadOnlyViewSet):
    def get_queryset(self):
        if not self.has_ticket_perm('log.read'):
            raise PermissionDenied()
        try:
            authorization = AuthorizationRequest.objects.get(
                ticket=self.ticket, pk=self.kwargs.get("authorization_pk"),
            )
        except AuthorizationRequest.DoesNotExist:
            raise NotFound(detail="AuthorizationRequest not found.")
        return authorization.documents.all()

class BroadcastView(mixins.CreateModelMixin, viewsets.ReadOnlyModelViewSet):
    permission_classes = (Authenticated,)
    serializer_class = BroadcastSerializer
    queryset = Broadcast.objects.all()

    def get_queryset(self):
        return self.queryset.filter(
            ticket=self.kwargs.get("ticket_pk"),
            ticket__target__canonical_name=self.kwargs.get("target_canonical_name"),
        )

    def get_serializer_context(self):
        ctx = super().get_serializer_context()
        return {**ctx, "kwargs": self.kwargs}

    def create(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data, context=self.get_serializer_context())
        serializer.is_valid(raise_exception=True)
        broadcast = serializer.save()
        return Response(data=self.serializer_class(broadcast).data, status=201)

class AuthorizationRequestViewSchema(schemas.CustomSchema):

    @schemas.serializers.post('/authorization/')
    def authorization_fields(self):
        return [
            coreapi.Field(
                name='action',
                required=True,
                location='body',
                schema=coreschema.String(
                    description='Action for which the authorization will be created (close, escalate, archive)',
                ),
            ),
            coreapi.Field(
                name='to_state',
                required=False,
                location='body',
                schema=coreschema.String(
                    description='escalation target state, required only when action=escalate',
                ),
            ),
        ]

class TicketAuthorizationRequestView(NestedInTicketMixin, mixins.CreateModelMixin, viewsets.ReadOnlyModelViewSet):
    permission_classes = (Authenticated,)
    serializer_class = AuthorizationRequestSerializer
    schema = AuthorizationRequestViewSchema.as_schema()

    def get_authorization_read_filters(self):
        state = self.ticket.state_group
        if state in ALERT_STATES:
            states = ALERT_STATES
        elif state in EVENT_STATES:
            states = EVENT_STATES
        else:
            raise ValidationError('Ticket state group unknown')

        permissions = []
        for level in AUTHORIZATION_LEVELS:
            permissions.extend([
                f'close.authorization.{level}.read',
                f'archive.authorization.{level}.read'
            ])
        permissions.extend([
            f'escalate.{to_state}.authorization.{level}.read'
            for to_state, level in itertools.product(states, AUTHORIZATION_LEVELS)
            if state != to_state
        ])

        readable_authorizations = [
            f'ticket.{self.ticket.state_group}.{perm[:-5]}'
            for perm in permissions
            if self.has_ticket_perm(perm)
        ]
        if len(readable_authorizations) > 0:
            return reduce(
                lambda acc, q: acc | q,
                [
                    Q(authorization=auth)
                    for auth in readable_authorizations
                ]
            )

        return None

    def get_queryset(self):
        auth_filters = self.get_authorization_read_filters()
        if auth_filters is None:
            raise PermissionDenied()
        return self.ticket.authorizations.filter(auth_filters)

    def create(self, request, *args, **kwargs):
        ticket = self.ticket
        _action = request.data.get('action', '')
        if _action == 'close':
            conditions = ticket.close_conditions
        elif _action == 'archive':
            conditions = ticket.archive_conditions
        elif _action == 'escalate':
            to_state = request.data.get('to_state', None)
            if to_state is None:
                raise ValidationError({'to_state': 'required'})
            if to_state not in ticket.escalate_conditions:
                raise ValidationError({'to_state': f'no escalate to {to_state} conditions'})
            conditions = ticket.escalate_conditions[to_state]
        else:
            raise ValidationError({'action': f'Action {_action} not supported'})
        authorization = get_next_authorization(conditions, request.user)
        if authorization is None:
            raise ValidationError({'action': 'No new authorization required'})
        if not self.has_target_perm(f'{authorization}.request'):
            raise PermissionDenied()
        if self.get_queryset().filter(
                status=AuthorizationRequest.Status.PENDING,
                authorization=authorization
        ).exists():
            raise ValidationError({'action': 'there is a pending authorization request'})
        auth_request = AuthorizationRequest.objects.create(
            authorization=authorization,
            ticket=ticket,
            created_by=serialize_author(request.user)
        )
        # Creating a new log entry
        log = TicketLog.new_request_entry(
            ticket=ticket,
            author=request.user,
            authorization_string=authorization
        )
        log.save()

        if settings.STACK_IS_SML and '.authority-' in authorization:
            send_simple_smc_message(
                'alerts.ticket.authorization.create',
                AuthorizationRequestCreateSerializer(auth_request).data
            )
        else:
            # update ticket conditions
            engine.update_ticket_conditions(ticket)
        return Response(data=AuthorizationRequestSerializer(auth_request).data, status=201)

    def _resolve_authorization_request(self, request, authorization_request, new_status):
        ticket = self.ticket
        if authorization_request.status != AuthorizationRequest.Status.PENDING:
            raise ValidationError(f'only pending authorizations can be {new_status}')
        serializer = AuthorizationRequestSerializer(
            authorization_request,
            data=request.data,
        )
        serializer.is_valid(raise_exception=True)
        serializer.save(
            resolved_by=serialize_author(request.user),
            resolved_at=timezone.now(),
            status=new_status
        )
        authorization_request.refresh_from_db()
        ticket_id = authorization_request.ticket.id
        folder = f'documents/ticket/{ticket_id}/authorization/{authorization_request.id}/'
        for file in request.FILES.values():
            authorization_request.documents.add(
                Document.objects.create(
                    folder=folder,
                    file=file,
                    name=str(file),
                    uploaded_by=request.user,
                    meta={'request_id': {'value': authorization_request.id}}
                )
            )
        # Creating a new log entry
        log = TicketLog.new_authorization_entry(
            ticket=ticket,
            author=request.user,
            authorization_request=authorization_request,
            new_status=new_status,
        )
        log.save()
        for doc in authorization_request.documents.all():
            log.documents.add(doc)

        if not settings.STACK_IS_SML and authorization_request.origin != settings.NAMESPACE:
            remote = authorization_request.ticket.target.remote
            if remote is not None:
                documents = list(serialize_docs_for_upload(authorization_request.documents.all(), folder))
                bucket = get_ticket_bucket(authorization_request.ticket)
                upload_docs(documents, bucket)
                send_simple_message(
                    'alerts.ticket.authorization.update',
                    body=AuthorizationRequestUpdateSerializer(
                        authorization_request,
                        context={'documents': documents}
                    ).data,
                    exchange=remote.exchange
                )
            else:
                raise ValidationError(f'target remote is missing')
        else:
            # update ticket conditions
            engine.update_ticket_conditions(authorization_request.ticket)
        return Response(data=AuthorizationRequestSerializer(authorization_request).data)

    @action(
        methods=["post"],
        detail=True,
        parser_classes=(parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser),
        serializer_class=AuthorizationRequestSerializer,
    )
    def approve(self, request, **kwargs):
        auth_request = self.get_object()
        if not self.has_target_perm(f'{auth_request.authorization}.resolve'):
            raise PermissionDenied()
        new_status = AuthorizationRequest.Status.APPROVED
        return self._resolve_authorization_request(request, auth_request, new_status)

    @action(
        methods=["post"],
        detail=True,
        parser_classes=(parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser),
        serializer_class=AuthorizationRequestSerializer,
    )
    def deny(self, request, **kwargs):
        auth_request = self.get_object()
        if not self.has_target_perm(f'{auth_request.authorization}.resolve'):
            raise PermissionDenied()
        new_status = AuthorizationRequest.Status.DENIED
        return self._resolve_authorization_request(request, auth_request, new_status)

class AuthorizationRequestDocumentView(DocumentReadOnlyViewSet):
    # TODO use nested in ticket view and check authorization read permissions
    def get_queryset(self):
        try:
            authorization = AuthorizationRequest.objects.get(
                ticket=self.kwargs.get("ticket_pk"),
                ticket__target__canonical_name=self.kwargs.get("target_canonical_name"),
                pk=self.kwargs.get("authorization_pk"),
            )
        except AuthorizationRequest.DoesNotExist:
            raise NotFound(detail="AuthorizationRequest not found.")
        return authorization.documents.all()

class AuthorizationRequestView(viewsets.ReadOnlyModelViewSet):
    permission_classes = (Authenticated,)
    serializer_class = AuthorizationRequestTicketSerializer
    # TODO fix this queryset based on permissions
    queryset = AuthorizationRequest.objects.all()

class TicketCommentView(NestedInTicketMixin, viewsets.ModelViewSet):
    serializer_class = TicketCommentSerializer

    def get_queryset(self):
        state_group = self.ticket.state_group
        read_perms = [
            (
                f'ticket.{state_group}.close_report_comment.read',
                TicketComment.CommentType.CLOSE_REPORT
            ),
            (
                f'ticket.{state_group}.alert_management_comment.read',
                TicketComment.CommentType.ALERT_MANAGEMENT
            ),
            (
                f'ticket.{state_group}.alert_complementary_comment.read',
                TicketComment.CommentType.ALERT_COMPLEMENTARY
            ),
            (
                f'ticket.{state_group}.event_management_comment.read',
                TicketComment.CommentType.EVENT_MANAGEMENT
            )
        ]
        type_filters = [
            Q(comment_type=c_type)
            for c_perm, c_type in read_perms
            if self.has_target_perm(c_perm)
        ]
        if len(type_filters) > 0:
            queryset = self.ticket.comments.filter(
                reduce(lambda acc, q: acc | q, type_filters)
            )
        else:
            raise PermissionDenied()
        return queryset

    def perform_create(self, serializer):
        comment = serializer.save(ticket=self.ticket, created_by=UserSerializer(self.request.user).data)
        log = TicketLog.new_comment_entry(
            ticket=self.ticket,
            author=self.request.user,
            meta={
                "comment": comment.content,
                "description": "comment",
                "comment_id": comment.id,
                "comment_type": comment.comment_type
            },
        )
        log.save()
        folder = self.ticket.get_document_folder('comment', comment.id)
        for file in self.request.FILES.values():
            doc = Document.objects.create(
                folder=folder,
                file=file,
                name=str(file),
                uploaded_by=self.request.user
            )
            comment.documents.add(doc)
            log.documents.add(doc)
        engine.update_ticket_conditions(self.ticket)
        return log

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data
        types = list(c_type for c_type, _ in TicketComment.CommentType.choices)
        c_type = data.get('comment_type', 'None')
        if c_type not in types or not self.has_ticket_perm(f'{c_type}_comment.write'):
            raise PermissionDenied()
        self.perform_create(serializer)
        return Response(serializer.data, status=status.HTTP_201_CREATED)

class TicketCommentDocumentView(NestedInTicketMixin, DocumentReadOnlyViewSet):
    def get_queryset(self):
        comment_pk = self.kwargs.get('comment_pk')
        comment = self.ticket.comments.filter(pk=comment_pk).first()
        if comment is None:
            raise NotFound(detail="Ticket comment not found.")
        if not self.has_ticket_perm(f'{comment.comment_type}_comment.read'):
            raise PermissionDenied()
        queryset = comment.documents.all()
        return queryset

class PublicAlertMessageFilter(filters.FilterSet):
    alert_type = filters.CharFilter(method='alert_type_filter')
    scope = filters.CharFilter(method='scope_filter')

    class Meta:
        model = PublicAlertMessage
        fields = ['alert_type', 'scope']

    def alert_type_filter(self, queryset, name, value):
        if name == 'alert_type' and value is not None:
            return queryset.filter(**{name: value, })
        return queryset

    def scope_filter(self, queryset, name, value):
        if name == 'scope' and value is not None:
            return queryset.filter(**{name: value, })
        return queryset

class PublicAlertMessageView(viewsets.ModelViewSet):
    serializer_class = PublicAlertMessageSerializer
    filter_backends = [FilterListBackend]
    filter_class = PublicAlertMessageFilter

    def get_queryset(self):
        target_canonical_name = self.kwargs.get('target_canonical_name')
        try:
            target = Target.objects.get(canonical_name=target_canonical_name)
        except Target.DoesNotExist:
            raise NotFound(detail="Target not found.")
        queryset = target.alert_messages.all()
        return queryset

    def perform_create(self, serializer):
        target_canonical_name = self.kwargs.get('target_canonical_name')
        try:
            target = Target.objects.get(canonical_name=target_canonical_name)
        except Target.DoesNotExist:
            raise NotFound(detail="Target not found.")
        serializer.save(target=target, created_by=UserSerializer(self.request.user).data)

class AlertDisconnectionFilter(filters.FilterSet):
    scope = filters.CharFilter(method='scope_filter')

    class Meta:
        model = AlertDisconnection
        fields = ['scope']

    def scope_filter(self, queryset, name, value):
        if name == 'scope' and value is not None:
            return queryset.filter(**{name: value, })
        return queryset

class AlertDisconnectionsView(viewsets.ModelViewSet):
    permission_classes = (Authenticated,)
    filter_backends = [FilterListBackend]
    filterset_class = AlertDisconnectionFilter
    serializer_class = AlertDisconnectionSerializer
    queryset = AlertDisconnection.objects.all()

class TargetAlertDisconnectionsView(NestedInTargetMixin, viewsets.ModelViewSet):
    permission_classes = (Authenticated,)
    filter_backends = [FilterListBackend]
    filter_class = AlertDisconnectionFilter
    serializer_class = AlertDisconnectionSerializer

    def get_queryset(self):
        queryset = self.target.alert_disconnections.all()
        return queryset

    def perform_create(self, serializer):
        target_canonical_name = self.kwargs.get('target_canonical_name')

        # scope = serializer.data
        # if not self.has_target_perm(f'ticket.{scope}.alert_disconnection.write'):
        #     raise PermissionDenied()

        try:
            target = Target.objects.get(canonical_name=target_canonical_name)
        except Target.DoesNotExist:
            raise NotFound(detail="Target not found.")
        serializer.save(target=target, created_by=UserSerializer(self.request.user).data)

class AlertDisconnectionDocumentView(DocumentReadOnlyViewSet, DocumentUploadMixin, mixins.DestroyModelMixin):
    permission_classes = (Authenticated,)

    def get_queryset(self):
        disconnection_pk = self.kwargs.get('disconnection_pk')
        try:
            disconnection = AlertDisconnection.objects.get(pk=disconnection_pk)
        except AlertDisconnection.DoesNotExist:
            raise NotFound(detail="Alert disconnection not found.")
        queryset = disconnection.documents.all()
        return queryset

    def perform_document_create(self, file_obj, description, meta, request, type):
        doc = super().perform_document_create(file_obj, description, meta, request, type)
        disconnection_pk = self.kwargs.get('disconnection_pk')
        try:
            disconnection = AlertDisconnection.objects.get(pk=disconnection_pk)
        except AlertDisconnection.DoesNotExist:
            raise NotFound(detail="Alert disconnection not found.")
        disconnection.documents.add(doc)
        return doc

    def get_folder(self):
        disconnection_pk = self.kwargs.get('disconnection_pk')
        return f'disconnection/{disconnection_pk}'

class TargetManualAlertLogsView(NestedInTicketMixin, viewsets.ModelViewSet):
    permission_classes = (Authenticated,)
    filter_backends = [FilterListBackend]
    # filter_class = ManualAlertLog
    serializer_class = ManualAlertLogSerializer

    def get_queryset(self):
        queryset = self.target.manual_alerts.all()
        return queryset

    def perform_create(self, serializer):
        # scope = serializer.data
        # if not self.has_target_perm(f'ticket.{scope}.alert_disconnection.write'):
        #     raise PermissionDenied()

        serializer.save(target=self.target, ticket=self.ticket, created_by=UserSerializer(self.request.user).data)

class ManualAlertLogDocumentView(DocumentReadOnlyViewSet, DocumentUploadMixin, mixins.DestroyModelMixin):
    permission_classes = (Authenticated,)

    def get_queryset(self):
        manual_pk = self.kwargs.get('manual_pk')
        try:
            manual_alert_log = ManualAlertLog.objects.get(pk=manual_pk)
        except ManualAlertLog.DoesNotExist:
            raise NotFound(detail="Manual alert log not found.")
        queryset = manual_alert_log.documents.all()
        return queryset

    def perform_document_create(self, file_obj, description, meta, request):
        doc = super().perform_document_create(file_obj, description, meta, request)
        manual_pk = self.kwargs.get('manual_pk')
        try:
            manual_alert_log = ManualAlertLog.objects.get(pk=manual_pk)
        except AlertDisconnection.DoesNotExist:
            raise NotFound(detail="Manual alert log not found.")
        manual_alert_log.documents.add(doc)
        return doc

    def get_folder(self):
        manual_pk = self.kwargs.get('manual_pk')
        return f'manual/{manual_pk}'


import json
import os

from django.core.urlresolvers import reverse
from django.db import connections
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings

from healthcheck.contrib.django.status_endpoint import views

class StatusEndpointViewsTestCase(TestCase):
    urls = 'healthcheck.contrib.django.status_endpoint.urls'

    def setUp(self):
        self.factory = RequestFactory()

    @override_settings(
        STATUS_CHECK_DBS=True,
        STATUS_CHECK_FILES=('/etc/quiesce',)
    )
    def test_default_checks(self):
        request = self.factory.get(reverse(views.status))
        response = views.status(request)
        self.assertEqual(response.status_code, 200)

    @override_settings(
        STATUS_CHECK_DBS=True,
        STATUS_CHECK_FILES=()
    )
    def test_dont_check_files(self):
        request = self.factory.get(reverse(views.status))
        response = views.status(request)
        response_json = json.loads(response.content.decode())
        self.assertTrue(
            "quiesce file doesn't exist" not in response_json)
        self.assertTrue(
            'Django Databases Health Check' in response_json)
        db_names = response_json['Django Databases Health Check']['details']
        self.assertTrue(
            all(connection.alias in db_names
                for connection in connections.all()))
        self.assertEqual(response.status_code, 200)

    @override_settings(
        STATUS_CHECK_DBS=False,
        STATUS_CHECK_FILES=()
    )
    def test_no_checks_raises_200(self):
        request = self.factory.get(reverse(views.status))
        response = views.status(request)
        response = {
            'content': json.loads(response.content.decode()),
            'status': response.status_code,
        }

        expected_response = {
            'content': 'There were no checks.',
            'status': 200,
        }

        self.assertEqual(response, expected_response)

    @override_settings(
        STATUS_CHECK_DBS=False,
        STATUS_CHECK_FILES=('/usr/bin/env',)
    )
    def test_failed_check(self):
        request = self.factory.get(reverse(views.status))
        response = views.status(request)
        response = {
            'content': json.loads(response.content.decode()),
            'status': response.status_code,
        }

        expected_response = {
            'content': {
                "quiesce file doesn't exist": {
                    'details': {
                        '/usr/bin/env': 'FILE EXISTS'
                    },
                    'status': 'FAILED'
                }
            },
            'status': 500,
        }

        self.assertEqual(response, expected_response)


from hatch.template import File
from hatch.utils.fs import Path
from hatchling.__about__ import __version__
from hatchling.metadata.spec import DEFAULT_METADATA_VERSION

from ..new.feature_no_src_layout import get_files as get_template_files
from .utils import update_record_file_contents

def get_files(**kwargs):
    metadata_directory = kwargs.get('metadata_directory', '')
    package_root = kwargs.get('package_root', '')

    files = []
    for f in get_template_files(**kwargs):
        if str(f.path) == 'LICENSE.txt':
            files.append(File(Path(metadata_directory, 'licenses', f.path), f.contents))

    pth_file_name = f"_{kwargs['package_name']}.pth"
    loader_file_name = f"_editable_impl_{kwargs['package_name']}.py"
    files.append(File(Path(pth_file_name), f"import _editable_impl_{kwargs['package_name']}"))
    files.append(
        File(
            Path(loader_file_name),
            f"""\
from editables.redirector import RedirectingFinder as F
F.install()
F.map_module({kwargs['package_name']!r}, {package_root!r})""",
        )
    )
    files.append(
        File(
            Path(metadata_directory, 'WHEEL'),
            f"""\
Wheel-Version: 1.0
Generator: hatchling {__version__}
Root-Is-Purelib: true
Tag: py2-none-any
Tag: py3-none-any
""",
        )
    )
    files.append(
        File(
            Path(metadata_directory, 'METADATA'),
            f"""\
Metadata-Version: {DEFAULT_METADATA_VERSION}
Name: {kwargs['project_name']}
Version: 0.0.1
License-File: LICENSE.txt
Requires-Dist: editables~=0.3
""",
        )
    )

    record_file = File(Path(metadata_directory, 'RECORD'), '')
    update_record_file_contents(record_file, files, generated_files={pth_file_name, loader_file_name})
    files.append(record_file)

    return files


import numpy as np
import os
import sys
import pickle
import cv2
import json

global cascaded

def varnorm(img):
    img = np.asarray(img)
    # mean = np.sum(np.sum(img))/576
    mean = np.mean(img)
    # std = np.sqrt(mean**2 - np.sum(np.sum(np.multiply(img,img)))/576)
    std = np.std(img)
    # print("std",std)
    img = ((img-mean)/float(std))

    return img

file2 = open("Model_Files/cascade.pkl", "rb")
cascaded = pickle.load(file2)

def det_face(integimg,coor):

    global cascaded
    f_score = np.zeros(len(integimg))

    for jj, vv in enumerate(cascaded):
        stage_v = np.zeros(len(integimg))
        is_face = np.zeros(len(integimg))
        for j,v in enumerate(vv[0]):
            if v[2][2] == 0:
                ir = v[2][0][0]
                ic = v[2][0][1]
                iir = v[2][1][0]
                iic = v[2][1][1]
                if len(integimg)==1:
                    fv = integimg[ir-1,ic-1] - integimg[ir-1,iic] + 2*integimg[ir+((iir-ir)//2),iic] - integimg[iir,iic] + integimg[iir,ic-1] - 2*integimg[ir+((iir-ir)//2),ic-1]
                else:
                    fv = integimg[:,ir-1,ic-1] - integimg[:,ir-1,iic] + 2*integimg[:,ir+((iir-ir)//2),iic] - integimg[:,iir,iic] + integimg[:,iir,ic-1] - 2*integimg[:,ir+((iir-ir)//2),ic-1]
            elif v[2][2] == 1:
                ir = v[2][0][0]
                ic = v[2][0][1]
                iir = v[2][1][0]
                iic = v[2][1][1]
                if len(integimg)==1:
                    fv = -integimg[ir-1,ic-1] + 2*integimg[ir-1,ic+((iic-ic)//2)] - integimg[ir-1,iic] + integimg[iir,iic] - 2*integimg[iir,ic+((iic-ic)//2)] + integimg[iir,ic-1]
                else:
                    fv = -integimg[:,ir-1,ic-1] + 2*integimg[:,ir-1,ic+((iic-ic)//2)] - integimg[:,ir-1,iic] + integimg[:,iir,iic] - 2*integimg[:,iir,ic+((iic-ic)//2)] + integimg[:,iir,ic-1]
            elif v[2][2] == 2:
                ir = v[2][0][0]
                ic = v[2][0][1]
                iir = v[2][1][0]
                iic = v[2][1][1]
                if len(integimg)==1:
                    fv = -integimg[ir-1,ic-1] + 2*integimg[(ir+(iir-ir)//3),ic-1] - 2*integimg[(ir+2*((iir-ir)//3)+1),ic-1] + integimg[iir,ic-1] + integimg[ir-1,iic] - 2*integimg[(ir+(iir-ir)//3),iic] + 2*integimg[(ir+2*((iir-ir)//3)+1),iic] - integimg[iir,iic]
                else:
                    fv = -integimg[:,ir-1,ic-1] + 2*integimg[:,(ir+(iir-ir)//3),ic-1] - 2*integimg[:,(ir+2*((iir-ir)//3)+1),ic-1] + integimg[:,iir,ic-1] + integimg[:,ir-1,iic] - 2*integimg[:,(ir+(iir-ir)//3),iic] + 2*integimg[:,(ir+2*((iir-ir)//3)+1),iic] - integimg[:,iir,iic]
            elif v[2][2] == 3:
                ir = v[2][0][0]
                ic = v[2][0][1]
                iir = v[2][1][0]
                iic = v[2][1][1]
                if len(integimg) ==1:
                    fv = -integimg[ir-1,ic-1] + 2*integimg[ir-1,(ic+((iic-ic)//3))] - 2*integimg[ir-1,ic+(2*(iic-ic)//3)+1] + integimg[ir-1,iic] + integimg[iir,ic-1] - 2*integimg[iir,(ic+((iic-ic)//3))] + 2*integimg[iir,ic+(2*((iic-ic)//3))+1] - integimg[iir,iic]
                else:
                    fv = -integimg[:,ir-1,ic-1] + 2*integimg[:,ir-1,(ic+((iic-ic)//3))] - 2*integimg[:,ir-1,ic+(2*(iic-ic)//3)+1] + integimg[:,ir-1,iic] + integimg[:,iir,ic-1] - 2*integimg[:,iir,(ic+((iic-ic)//3))] + 2*integimg[:,iir,ic+(2*((iic-ic)//3))+1] - integimg[:,iir,iic]
            elif v[2][2] == 4:
                ir = v[2][0][0]
                ic = v[2][0][1]
                iir = v[2][1][0]
                iic = v[2][1][1]
                if len(integimg)==1:
                    fv = -integimg[ir-1,ic-1] + 2*integimg[ir-1,ic+((iic-ic)//2)] - integimg[ir-1,iic] + 2*integimg[ir+((iir-ir)//2),ic-1] - 4*integimg[ir+((iir-ir)//2),ic+((iic-ic)//2)] +2*integimg[ir+((iir-ir)//2),iic] - integimg[iir,ic-1] + 2* integimg[iir,ic+((iic-ic)//2)] - integimg[iir,iic]
                else:
                    fv = -integimg[:,ir-1,ic-1] + 2*integimg[:,ir-1,ic+((iic-ic)//2)] - integimg[:,ir-1,iic] + 2*integimg[:,ir+((iir-ir)//2),ic-1] - 4*integimg[:,ir+((iir-ir)//2),ic+((iic-ic)//2)] +2*integimg[:,ir+((iir-ir)//2),iic] - integimg[:,iir,ic-1] + 2* integimg[:,iir,ic+((iic-ic)//2)] - integimg[:,iir,iic]
            ffv = np.zeros(len(integimg))
            ffv[v[3]*fv > v[3]*v[4]*np.ones(len(integimg))] = 1
#             if v[3]*fv > v[3]*v[4]:
#                 ffv = 1
#             else:
#                 ffv = 0
#             alpha = np.log(1/v[1])
            alphas = np.ones(len(integimg))*np.log(1/v[1])

            stage_v = stage_v + np.multiply(alphas,ffv)
        th = np.ones(len(integimg))*vv[1]
        is_face[stage_v>th] = 1
#         print(stage_v.shape, th.shape, f_score.shape)
        f_score = f_score + (stage_v - th)
#         print(len(integimg), len(is_face))
        integimg = integimg[is_face==1]
        f_score = f_score[is_face==1]
        coor = coor[is_face==1]
#         print(len(integimg))
    return (integimg,f_score,coor)

folder = sys.argv[1]
# folder = "test_images"
rs = 4
final_jason = []
count = 0
for filename in os.listdir(folder):
    count += 1
    print("count", count)
    print(filename)
    img = cv2.imread(os.path.join(folder, filename), cv2.IMREAD_GRAYSCALE)

    img = cv2.resize(img, (0,0), fx=1/rs, fy=1/rs)
    num_r = img.shape[0]
    num_c = img.shape[1]
    # pr_fd_thr = 0
    # cr_fd_thr = 0
    scale = 5
    # scale = 1

    f_score_l = []
    coor_list = []
    swl = []
    while scale < 130:
        sub_win_len = int(scale*24/rs)
        print(scale)

        sub_win_arr = []
        coor = []
        for ir in range(num_r-(sub_win_len-1)):
            for ic in range(num_c-(sub_win_len-1)):
                sub_win = img[ir:ir+sub_win_len,ic:ic+sub_win_len]
                sub_win = cv2.resize(sub_win, (24,24))
                sub_win = varnorm(sub_win)
                sub_win = cv2.integral(sub_win)
                sub_win_arr.append(sub_win)
                coor.append((ir,ic))

        sub_win_arr = np.asarray(sub_win_arr)
    #     print(sub_win_arr.shape)
        coor = np.asarray(coor)
        if len(sub_win_arr) < 1:
            break
        (face_sw, f_score, coor) = det_face(sub_win_arr,coor)
        print(coor.shape,"coor shape")
        print(f_score.shape,"f_score shape")
        f_score_l += list(f_score)
        coor_list += list(coor)
        swl += [sub_win_len]*len(coor)
        scale = scale*5

        f_score_list = np.asarray(f_score_l)
        max_f_score = np.max(f_score_list)
        max_f_score_ind = np.argmax(f_score_list)
#         print(max_f_score_ind)
#         print(max_f_score)
        T = 0.965
        final_faces = []
        for ii,vv in enumerate(f_score_list):
            if vv/max_f_score > T:
                final_faces.append(ii)

        for hh in final_faces:
            final_jason.append({"iname": filename, "bbox": [int(coor_list[hh][0]*rs), int(coor_list[hh][1]*rs), int(swl[hh]*rs), int(swl[hh]*rs)]})
#             cv2.rectangle(img0,(coor_list[hh][1]*rs,coor_list[hh][0]*rs),(coor_list[hh][1]*rs+swl[hh]*rs,coor_list[hh][0]*rs+swl[hh]*rs),(255, 0, 0),1)
output_json = "results.json"
#dump json_list to result.json
with open(output_json, 'w') as f:
    json.dump(final_jason, f)
#         cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
#         cv2.imshow('image', img0)
#         cv2.waitKey(5000)
#         cv2.destroyAllWindows()


# -*- coding: utf-8 -*-
"""
Created on Mon Jul 15 09:11:01 2019

@author: TRAORE Cheick A. D. Gabriel pour utilisation personnelle et non commerciale
"""
import cv2
#from FACTORISATION import factorisation
import matplotlib.pyplot as plt
from MAILLAGE import maillage
from DEFIACCU import defAccu
from COMPTAGE import compt
import math
import time

import scipy.ndimage.filters as filters
import scipy.ndimage as ndimage

img=cv2.imread('Lena-Canny.png')
def select_recogRf(a,n,img):
    d1=time.time()
    global A
    global C
    #print(img.shape)
    Nl=img.shape[0]
    Nc=img.shape[1]
    tab, tabool=maillage(Nl,Nc)
    h=tab[0][0]*tab[0][1]
    l=tab[1][0]*tab[1][1]
    d=tab[0][0]*tab[1][0]
    theta_max=math.pi
    r_max=(Nl**2+Nc**2)**0.5
    acc, r_dim, theta_dim=defAccu(tab,tabool)
    for x in range(0,h-tab[0][0],tab[0][0]):
        for y in range(0,l-tab[1][0],tab[1][0]):
            #A=(x,y)
            #C=(x+tab[0][1],y+tab[1][1])
            print(compt((x,y),(x+tab[0][0],y+tab[1][0]),n,img))
            if (compt((x,y),(x+tab[0][0],y+tab[1][0]),n,img)/d)>=a:
                for z in range(x,x+tab[0][1]+1):
                    for k in range(y,y+tab[1][1]+1):
                        if img[z,k,0] ==n:
                            for itheta in range(theta_dim):
                                theta = 1.0 * itheta * theta_max / theta_dim
                                r = z* math.cos(theta) + k * math.sin(theta)
                                ir = r_dim * ( 1.0 * r ) / r_max
                                ir,itheta=round(ir),round(itheta)
                                acc[ir,itheta] = acc[ir,itheta] + 1
    #premier Si
    if tabool[0]==1 and tabool[1]==0:
        x=h
        for y in range(0,l):
            if img[x,y,0]==n:
                for itheta in range(theta_dim):
                    theta = 1.0 * itheta * theta_max / theta_dim
                    r = z* math.cos(theta) + k * math.sin(theta)
                    ir = r_dim * ( 1.0 * r ) / r_max
                    ir,itheta=round(ir),round(itheta)
                    acc[ir,itheta] = acc[ir,itheta] + 1
    #deuxieme Si
    if tabool[0]==0 and tabool[1]==1:
        y=l
        for x in range(0,h):
            if img[x,y,0]==n:
                for itheta in range(theta_dim):
                    theta = 1.0 * itheta * theta_max / theta_dim
                    r = z* math.cos(theta) + k * math.sin(theta)
                    ir = r_dim * ( 1.0 * r ) / r_max
                    ir,itheta=round(ir),round(itheta)
                    acc[ir,itheta] = acc[ir,itheta] + 1
    #dernier Si
    if tabool[0]==1 and tabool[1]==1:
        x=h
        for y in range(0,l):
            if img[x,y,0]==n:
                for itheta in range(theta_dim):
                    theta = 1.0 * itheta * theta_max / theta_dim
                    r = z* math.cos(theta) + k * math.sin(theta)
                    ir = r_dim * ( 1.0 * r ) / r_max
                    ir,itheta=round(ir),round(itheta)
                    acc[ir,itheta] = acc[ir,itheta] + 1
        y=l
        for x in range(0,h):
            if img[x,y,0]==n:
                for itheta in range(theta_dim):
                    theta = 1.0 * itheta * theta_max / theta_dim
                    r = z* math.cos(theta) + k * math.sin(theta)
                    ir = r_dim * ( 1.0 * r ) / r_max
                    ir,itheta=round(ir),round(itheta)
                    acc[ir,itheta] = acc[ir,itheta] + 1
    #---------------------------------------------------------------------
    f2=time.time()

    plt.imshow(acc,origin='lower')
    plt.xlim(0,theta_dim)
    plt.ylim(0,r_dim)
    print('en seconde',f2-d1)
    #plt.savefig('alpha=0 s=100',bbox_inches='tight')
    #----------------------------------------------étape 3------------------------
    #Recherche des maxima dans la matrice d'accumulation

    neighborhood_size = 40
    threshold = 150        #seuil

    data_max = filters.maximum_filter(acc, neighborhood_size)
    maxima = (acc == data_max)

    data_min = filters.minimum_filter(acc, neighborhood_size)
    diff = ((data_max - data_min) > threshold)
    maxima[diff == 0] = 0

    labeled, num_objects = ndimage.label(maxima)
    slices = ndimage.find_objects(labeled)

    x, y = [], []
    for dy,dx in slices:
        x_center = (dx.start + dx.stop - 1)/2
        x+=[x_center]
        y_center = (dy.start + dy.stop - 1)/2
        y+=[y_center]
    #-----------------------------étape 4-------------------------------------
   #Construire les droites détectées
    line_index = 0
    for i,j in zip(y, x):
        r = round( (i * r_max ) / r_dim,1)
        theta = round( (j * theta_max) / theta_dim,1)
        fig, ax = plt.subplots()
        ax.imshow(img)
        ax.autoscale(False)

        px = []
        py = []
        for i in range(-Nc,Nc,1):
            px+=[ math.cos(-theta) * i - math.sin(-theta) * r ]
            py+=[ math.sin(-theta) * i + math.cos(-theta) * r ]

        ax.plot(px,py, linewidth=5)
      #  plt.savefig("image_line1_"+ "%02d" % line_index +".png",bbox_inches='tight')
        plt.show()

        line_index = line_index + 1
    ff=time.time()
    print('Le temps final est: ',ff-d1,'Nombre de droites',line_index)


import smtplib
import ssl
from email.mime.text import MIMEText
from email.utils import formataddr
from email.mime.multipart import MIMEMultipart  # New line
from email.mime.base import MIMEBase  # New line
from email import encoders  # New line
from PIL import Image
import os
import time
import win32com.client as win32

def sender(receiver_emails, receiver_names, name_file):

    # User configuration
    sender_email = "pl@avtosojuz.ua"
    sender_name = "pl"

    receiver_emails = [receiver_emails]
    receiver_names = [receiver_names]
    filename2 = name_file

    # Email body
    email_body = """
{}
""".format("")

    for receiver_email, receiver_name in zip(receiver_emails, receiver_names):
            # Configurating user's info
            msg = MIMEMultipart()
            msg['To'] = formataddr((receiver_name, receiver_email))
            msg['From'] = formataddr((sender_name, sender_email))
            msg['Subject'] = '' + receiver_name

            msg.attach(MIMEText(email_body, 'html'))

            try:
                # Open PDF file in binary mode
                with open(filename2, "rb") as attachment:
                                part = MIMEBase("application", "octet-stream")
                                part.set_payload(attachment.read())

                # Encode file in ASCII characters to send by email
                encoders.encode_base64(part)

                # Add header as key/value pair to attachment part
                part.add_header(
                        "Content-Disposition",
                        f"attachment; filename= {filename2}",
                )
                msg.attach(part)
            except Exception as e:
                    break

            try:
                    # Creating a SMTP session | use 587 with TLS, 465 SSL and 25
                    server = smtplib.SMTP('10.10.10.47', 25)
                    # Encrypts the email
                    # We log in into our Google account
                    # Sending email from sender, to receiver with the email body
                    server.sendmail(sender_email, receiver_email, msg.as_string())
            except Exception as e:
                    break
            #finally:
                    #server.quit()
                   # time.sleep(20)

def add_attachments(to_address, filename):
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.To = '{}'.format(to_address)
    mail.Subject = ''
    mail.Body = ''
    mail.HTMLBody = '

'  # this field is optional

    # To attach a file to the email (optional):
    attachment = filename
    print(attachment)
    mail.Attachments.Add(attachment)
    mail.display()


# Copyright (c) 2015, Alphamonak Solutions Ltd.
# License: GNU General Public License v3. See license.txt

from __future__ import unicode_literals
import redapp

def execute():
	for si in redapp.db.sql_list("""select name
		from `tabSales Invoice`
		where ifnull(update_stock,0) = 1 and docstatus = 1 and exists(
			select name from `tabSales Invoice Item` where parent=`tabSales Invoice`.name and
				ifnull(so_detail, "") != "")"""):

		invoice = redapp.get_doc("Sales Invoice", si)
		invoice.update_qty()


import tensorflow as tf
import numpy as np

#用 tf.histogram_summary() 显示神经层的变化图表
def add_layer(inputs,in_size,out_size,n_layer,activation_function=None):
    layer_name='layer%s'%n_layer
    with tf.name_scope('layer'):
        with tf.name_scope('weights'):  #权重的变化
            Weight = tf.Variable(tf.random_normal([in_size,out_size]),name='W')
            tf.summary.histogram(layer_name+'/weights',Weight)

        with tf.name_scope('biases'):  #置差的变化
            biases = tf.Variable(tf.zeros([1,out_size]) + 0.1)
            tf.summary.histogram(layer_name+'/biases',biases)

    with tf.name_scope('Wx_plus_b'):  #计算结果的变化
        Wx_plus_b = tf.matmul(inputs,Weight) + biases
        if activation_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activation_function(Wx_plus_b)

        tf.summary.histogram(layer_name + '/outputs', outputs)
        return outputs

# 1.训练的数据
x_data = np.linspace(-1,1,300)[:, np.newaxis]
noise = np.random.normal(0, 0.05, x_data.shape)
y_data = np.square(x_data) - 0.5 + noise

# 2.定义节点准备接收数据
xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1])

#3.创建两层神经网络
l1 = add_layer(xs, 1, 10,1, activation_function=tf.nn.relu)
prediction = add_layer(l1, 10, 1,2, activation_function=None)

#4.用tf.scalar_summary() 设置loss的变化图
with tf.name_scope('loss'):
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))
    tf.summary.scalar('loss',loss)

#5.调用优化器:
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

#6.初始化变量
init = tf.global_variables_initializer()
sess = tf.Session()

#7.打包所以训练图
merged = tf.summary.merge_all()                    #总结所有的训练图
writer = tf.summary.FileWriter('logs/',sess.graph) #储存

#8.训练
sess.run(init)
for i in range(1000):
    sess.run(train_step,feed_dict={xs: x_data, ys: y_data})
    if i % 50 == 0:
        result = sess.run(merged,feed_dict={xs:x_data,ys:y_data})   #运行合并得到数据
        writer.add_summary(result,i)                                #储存数据


def greedy_cow_transport(cowlist, weightlimit):
    result = []
    sortedcowlist = [[a,b] for a, b in sorted(cowlist.items(), key = lambda item: item[1], reverse = True)]
    print(sortedcowlist)
    while len(sortedcowlist)>0:
        total = 0
        cargo = []
        indexestopop = []
        for i in range(len(sortedcowlist)):
            cow, weight = sortedcowlist[i]
            if weight + total <= weightlimit:
                cargo.append(cow)
                total += weight
                indexestopop.append(i)
        indexestopop.sort(reverse=True)
        print(indexestopop)
        for x in indexestopop:
            sortedcowlist.pop(x)
        result.append(cargo)
    return result

# print(greedy_cow_transport({'MooMoo': 85, 'Milkshake': 75, 'Patches': 60, 'Louis': 45, 'Horns': 50,
#                             'Miss Bella': 15, 'Lotus': 10, 'Clover': 5, 'Polaris': 20,
#                             'Muscles': 65}, 100))

print(greedy_cow_transport({'Luna': 41, 'Willow': 59, 'Rose': 42, 'Coco': 59, 'Betsy': 39, 'Abby': 28,
                            'Starlight': 54, 'Buttercup': 11}, 120))


#! coding:utf-8
import os
import sys

from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtMultimediaWidgets import *

Signal = pyqtSignal
Slot = pyqtSlot

__all__ = ["pVideoPlayer"]

class pVideoPlayer(QWidget):
    def __init__(self, url=None, parent=None):
        QWidget.__init__(self)
        self.setWindowTitle('Video Player')

        # Phonon Objects
        # ***************
        self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.videoWidget = QVideoWidget()
        self.videoWidget.setMinimumHeight(128)

        self.mediaPlayer.setVideoOutput(self.videoWidget)

        # Timer
        # *****
        self.timer = QTimer()
        self.timer.setInterval(1000 / 30)
        self.timer.timeout.connect(self.tock)
        self.timer.start()

        # UI 生成
        # ***************
        self.setupUI()

        # 再生ファイル
        self.url = url
        if url is not None:
            self.set_url(self.url)

    def setupUI(self):

        """UI生成シーケンス.
        可視性を高めるため別に記述
        """

        # Ctrl UI
        self.btn_start = QPushButton('PLAY', self)
        self.btn_start.clicked.connect(self._handle_BtnStart)

        # Seek Bar
        # =========
        self.seek_slider = QSlider(Qt.Horizontal, self)
        # Seek
        self.mediaPlayer.positionChanged.connect(self.seek_slider.setValue)
        self.mediaPlayer.durationChanged.connect(self.seek_slider.setMaximum)
        self.seek_slider.sliderMoved.connect(self.mediaPlayer.setPosition)

        # Volume Bar
        # =========
        self.volume_slider = QSlider(Qt.Horizontal, self)
        self.volume_slider.setRange(0, 100)
        self.volume_slider.setValue(80)
        self.mediaPlayer.setVolume(self.volume_slider.value())
        # Signal Connect
        self.volume_slider.sliderMoved.connect(self.mediaPlayer.setVolume)
        self.mediaPlayer.volumeChanged.connect(self.volume_slider.setValue)

        # Time Label
        self.time_label = QLabel(self)
        self.time_label.setAlignment(Qt.AlignRight | Qt.AlignCenter)

        # Status Label
        self.status_label = QLabel(self)
        self.status_label.setAlignment(Qt.AlignRight | Qt.AlignCenter)
        self.status_label.setText("Status")

        # Layout
        layout = QGridLayout(self)
        # 一段目
        layout.addWidget(self.videoWidget, 0, 0, 1, 4)
        # 二段目
        layout.addWidget(self.btn_start, 1, 0)
        layout.addWidget(self.volume_slider, 1, 1)
        layout.addWidget(self.time_label, 1, 2)
        layout.addWidget(self.status_label, 1, 3)
        # 三段目
        layout.addWidget(self.seek_slider, 2, 0, 1, 4)

        # Signal
        self.mediaPlayer.stateChanged.connect(self._handle_StateChanged)

    def _handle_StateChanged(self, status):

        """MediaPlayerの状態遷移時のコールバック
        ボタンの文字を変更している．
        """

        if (status == QMediaPlayer.PlayingState):
            self.btn_start.setText('PAUSE')
        elif (status != QMediaPlayer.PausedState):
            self.btn_start.setText('PLAY')
        elif (status != QMediaPlayer.StoppedState):
            self.btn_start.setText('PLAY')
        if status == QMediaPlayer.LoadingMedia:
            self.setStatusInfo("Loading...")
            pass
        elif status == QMediaPlayer.StalledMedia:
            self.setStatusInfo("Media Stalled")
            pass
        elif status == QMediaPlayer.EndOfMedia:
            QApplication.alert(self)
            pass
        elif status == QMediaPlayer.InvalidMedia:
            # self.displayErrorMessage()
            pass
        else:
            self.setStatusInfo("-")
            pass

    def setStatusInfo(self, msg):
        self.status_label.setText(msg)

    def _handle_BtnStart(self):

        """再生/停止を行うボタンのコールバック"""
        if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
            self.mediaPlayer.pause()
        else:
            self.mediaPlayer.play()

    def set_url(self, url):

        """外部から再生ファイルをセット.
        ファイルがセットされると状態遷移しStateChangedがコールされる"""

        assert os.path.exists(url)
        url = QUrl.fromLocalFile(url)
        self.url = url
        contents = QMediaContent(url)
        self.mediaPlayer.setMedia(contents)

    def currentTimeMS(self):
        return self.mediaPlayer.position()

    def currentTiemPersent(self):
        return self.mediaPlayer.position() / self.mediaPlayer.duration()

    @Slot()
    def tock(self):
        """一定時間ごとに呼び出される"""
        self.time = self.mediaPlayer.position()
        # print(self.time)
        time = self.time
        time = time / 1000.
        h = int(time / 3600.)
        m = int((time - 3600 * h) / 60.)
        s = int(time - 3600 * h - m * 60)
        self.time_label.setText('%02d:%02d:%02d' % (h, m, s))

    @Slot()
    def restart(self):
        self.seek_slider.setValue(0)
        self.mediaPlayer.setPosition(0)

def main():
    VIDEO_PATH = "./data/test.avi"
    app = QApplication(sys.argv)
    window = pVideoPlayer(url=VIDEO_PATH)
    window.show()
    # It's exec_ because exec is a reserved word in Python
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()


# a = input()
# M = 3

tab= [9,8,7,4,3,1,8]
poczatek = 0
koniec = 7
suma = 0
iloczyn = 1
for i in range(poczatek,koniec):
    suma = suma+tab[i]
    iloczyn = iloczyn * tab[i]

print(suma)
print(iloczyn)


import torch
from torch import nn
import torch.nn.functional as F
from fastai import *
from fastai.vision import *
from fastai.layers import *

MAX_LENGTH = 1000

class SpeechModel(Module):
    "Contains an Encoder and a Decoder"
    def __init__(self,
                 encoder_output_size,
                 lang,
                 device=None,
                 teacher_forcing_pct=0.5):

        # select device
        self.device = device
        if device is None:
            if torch.cuda.is_available():
                self.device = torch.device("cuda:0")

        self.lang = lang
        self.vocab_size = len(lang)
        self.teacher_forcing_pct = teacher_forcing_pct

        self.encoder = CNNEncoder(encoder_output_size)
        self.embedding = nn.Embedding(self.vocab_size, encoder_output_size)
        self.pos_enc = PositionalEncoding(encoder_output_size, max_len=MAX_LENGTH)

        self.decoder = TransformerDecoder(encoder_output_size)
        self.ffn = nn.Linear(encoder_output_size, self.vocab_size)

        # put model on device
        if self.device is not "cpu":
            self.cuda()

    def forward(self, spectrogram, l=MAX_LENGTH):
        """
        spectrogram shape: [B x C x W x H]
        """

        spectrogram = spectrogram.to(self.device)
        batch_size = spectrogram.size(0)

        # run encoder to transform audio
        # to a vector in latent space
        context = self.encoder(spectrogram)
        print("context:", context.shape)

        # prepare input tokens for decoder
        sos = self.lang.t2i["<SOS>"]
        pad = self.lang.t2i["<PAD>"]

        # TODO: the input is all empty...?
        input_tokens = torch.LongTensor([[sos] + [pad] * (l-1)] * batch_size).to(self.device)
        print("input_tokens:", input_tokens.shape)

        # embed input token
        embedded = self.embedding(input_tokens)

        # apply positional encoding
        embedded_pos_enc = self.pos_enc(embedded)
        embedded_pos_enc = embedded_pos_enc.permute(1, 0, 2)
        print("embedded_pos_enc:", embedded_pos_enc.shape)

        # feed into decoder
        output_decoder = self.decoder(embedded_pos_enc, context)
        print("output_decoder:", output_decoder.shape)

        # ffn net
        output_tokens = self.ffn(output_decoder)

        # reshape output to [B x T x D]
        output_tokens = output_tokens.permute(1, 0, 2)
        print("output_tokens:", output_tokens.shape)
        return output_tokens

    def decode_greedy(self, spectrogram, **kwargs):
        output_tokens = self.forward(spectrogram, **kwargs)
        output_tokens = torch.log_softmax(output_tokens, dim=-1)

        with torch.no_grad():
            output_tokens = output_tokens.cpu().numpy()
            print(output_tokens)

            out = []
            for batch in output_tokens:

                sentence = ""
                for t in batch:
                    idx = t.argmax()
                    sentence += self.lang.i2t[idx]

                out.append(sentence)
            return out

    def predict(self):
        steps = 0
        while not tokens[-1] == eos and not steps > MAX_LENGTH:

            # NO teacher forcing here
            input_token = tokens[-1]

            # embed input token
            embedded = self.embedding(input_token)

            # apply positional encoding
            embedded = self.pos_enc(embedded)

            # feed to decoder
            decoder_output = self.decoder(embedded, context)
            decoder_output = self.linear(decoder_output)
            decoder_output_sm = F.log_softmax(decoder_output, dim=-1)

            print("decoder_output_sm:", decoder_output_sm.shape)
            _, decoder_output_token = decoder_output_sm.max(0)

            # append to already created tokens
            print("decoder_output_token:", decoder_output_token.shape)
            tokens.append(decoder_output_token)

        return tokens

class CNNEncoder(Module):
    "Encodes an audio spectrogram into a latent state representation"
    def __init__(self, output_size, base_model=models.resnet34):
        self.output_size = output_size
        self.model = create_cnn_model(base_model, output_size)

    def forward(self, spectrogram):
        context = self.model(spectrogram)
        return context

class TransformerDecoder(Module):
    "TransformerDecoder Module"
    def __init__(self, input_size, n_heads=2, num_layers=2):
        self.decoder_layer = nn.TransformerDecoderLayer(input_size, n_heads)
        self.decoder = nn.TransformerDecoder(self.decoder_layer, num_layers)
        self.forward = self.decoder.forward

class PositionalEncoding(Module):

    def __init__(self, d_model, dropout=0.1, max_len=5000):
        self.dropout = nn.Dropout(p=dropout)

        pe = torch.zeros(max_len, d_model)
        position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
        div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) / d_model))
        pe[:, 0::2] = torch.sin(position * div_term)
        pe[:, 1::2] = torch.cos(position * div_term)
        pe = pe.unsqueeze(0).transpose(0, 1)
        self.register_buffer('pe', pe)

    def forward(self, x):
        x = x + self.pe[:x.size(0), :]
        return self.dropout(x)


import dash
import dash_core_components as dcc
import dash_html_components as html
import dill
import pandas as pd
import plotly.graph_objs as go
import plotly.offline as py

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

report = dill.load(open("temp2", "rb"))

#add styling for the plots

app.layout = html.Div([

        html.H1(children='Results',
                style={
                    'textAlign': 'center'
                }
                ),

        html.Div(children='''
        Results of the optimisation process

        The dataset that was given as an input has been processed and multiple clustering algorithms have been applied.

        The smaller the error of he performance measure the better.
        ''', style={
            'textAlign': 'center'
        }),

    html.Div([
        dcc.Graph(
                figure=report.ranks
        )],
        style={'float': 'right',
               "width" : 700}),

    html.Div([
        dcc.Dropdown(
        id = "dropdown",
        options = [
            {"label": "K-Means Clustering", "value": "KMeans"},
            {"label": "Birch  Clustering", "value": "Birch"},
            {"label": "Graph-based spectral Clustering", "value": "SpectralClustering"},
            {"label": "Graph-based affinity Clustering", "value": "AffinityPropagation"},
            {"label": "Distribution-based Mean Shift Clustering", "value": "MeanShift"},
            {"label": "Agglomerative  Clustering", "value": "AgglomerativeClustering"},
            {"label": "Density Based  Clustering", "value": "DBSCAN"},
        ],
        value = "KMeans"
    ), dcc.RadioItems(
        id = "radio-items",
        options= [
            {"label": "3D Exploratory Plot", "value": "plot3D"},
            {"label": "2D Exploratory Plot", "value": "plot2D"}
        ],
        value = "plot2D",
        labelStyle={'display': 'inline-block'}
    )], style={'float': 'middle', 'display': 'inline-block'}),

    html.Div([
        dcc.Graph(
        id='scatter_plt'
    )], style = {"float": "left",
                 "width": 1000})
], style={'float': 'middle', 'display': 'inline-block'})

@app.callback(
    dash.dependencies.Output("scatter_plt", 'figure'),
    [dash.dependencies.Input('dropdown', 'value'),
     dash.dependencies.Input("radio-items", "value")])

def update_output(value_drp, value_radio):
    return report.tracker[value_drp][value_radio]

if __name__ == '__main__':
    app.run_server(debug= True, host = "localhost", port = 8000)