from peos import pmlParser
import argparse
import os
import pathlib
from mockDinto import MockDinto
import itertools
from peos.peosSimpleTimeParser import PeosSimpleTimeParser

cd = os.path.dirname(os.path.realpath(__file__))

default_filepath = os.path.join(cd, 'test/complex.pml')

default_csv_filepath = os.path.join(cd, 'DINTO/Mock-DINTO-sample.csv')

class DdiFinder:

    def __init__(self, root):
        self.root = root
        self.ddi_list = self.find_possible_ddis(self.root)
        self.sequential_ddis = []
        self.parallel_ddis = []
        for ddi in self.ddi_list:
            self.find_actual_ddis(self.root, ddi['first_drug'], ddi['second_drug'],
                                  self.convert_time_to_secs(ddi['time_value'], ddi['time_unit']), False)

    def get_sequential_ddis(self):
        data = []
        for ddi in self.sequential_ddis:
            for possible_ddi in self.ddi_list:
                if (ddi["first_drug"] == possible_ddi["first_drug"] and ddi["second_drug"] == possible_ddi["second_drug"]) \
                        or (ddi["first_drug"] == possible_ddi["second_drug"] and ddi["second_drug"] == possible_ddi["first_drug"]):

                    ddi_data = {}
                    ddi_data["drug_one"] = ddi["first_drug"]
                    ddi_data["drug_two"] = ddi["second_drug"]
                    ddi_data["construct"] = ddi["construct"]
                    ddi_data["type"] = possible_ddi["DDI_type"]
                    ddi_data["time"] = possible_ddi["time_value"]
                    ddi_data["time_unit"] = possible_ddi["time_unit"]
                    data.append(ddi_data)
        return data

    def get_parallel_ddis(self):
        data = []
        for ddi in self.parallel_ddis:
            for possible_ddi in self.ddi_list:
                if (ddi["first_drug"] == possible_ddi["first_drug"] and ddi["second_drug"] == possible_ddi["second_drug"]) \
                     or (ddi["first_drug"] == possible_ddi["second_drug"] and ddi["second_drug"] == possible_ddi["first_drug"]):

                    ddi_data = {}
                    ddi_data["drug_one"] = ddi["first_drug"]
                    ddi_data["drug_two"] = ddi["second_drug"]
                    ddi_data["construct"] = ddi["construct"]
                    ddi_data["type"] = possible_ddi["DDI_type"]
                    ddi_data["time"] = possible_ddi["time_value"]
                    ddi_data["time_unit"] = possible_ddi["time_unit"]
                    data.append(ddi_data)
        return data

    # Given two drugs, it finds the closes they can occur in a DDI.
    def find_closest_ddis(self, drugs):
        if len(drugs) < 2:
            print("Error: More than two drugs must be specified to query")
            return

        drug_one = drugs[0]
        drug_two = drugs[1]

        self.ddi_list = self.find_possible_ddis(self.root)
        mock_dinto = MockDinto(filepath=default_csv_filepath, supress=True)
        ddi_drugs = mock_dinto.ddi_query([drug_one, drug_two])
        # Check that a DDI can occur between the two specified drugs.
        if len(ddi_drugs) > 0:
            contains_ddi = False
            # For each possible DDI in the parsed pml tree.
            for ddi in self.ddi_list:
                # If this is the DDI specified by the user, find it's closest time.
                if ddi["first_drug"] == drug_one and ddi["second_drug"] == drug_two \
                     or ddi["first_drug"] == drug_two and ddi["second_drug"] == drug_one:

                    self.sequential_ddis = []
                    self.parallel_ddis = []
                    # Find all the DDIs
                    self.find_actual_ddis(self.root, ddi["first_drug"], ddi["second_drug"],
                                          self.convert_time_to_secs(ddi['time_value'], ddi['time_unit']), True)
                    # Order the DDIs then output the closest

                    # Parallel DDIs have a closest time of 0 and therefore take priority over sequnetial.
                    if len(self.parallel_ddis) > 0:
                        contains_ddi = True
                        closest_ddi_data = {}
                        closest_ddi_data["drug_one"] = drug_one
                        closest_ddi_data["drug_two"] = drug_two
                        closest_ddi_data["time"] = 0
                        closest_ddi_data["construct"] = ""
                        return closest_ddi_data

                    elif len(self.sequential_ddis) > 0:
                        contains_ddi = True
                        closest_ddi = self.sequential_ddis[0]
                        for ddi in self.sequential_ddis:
                            if (ddi["time"] < closest_ddi["time"]):
                                closest_ddi = ddi
                        closest_ddi_data = {}
                        closest_ddi_data["drug_one"] = drug_one
                        closest_ddi_data["drug_two"] = drug_two
                        closest_ddi_data["time"] = closest_ddi["time"]
                        closest_ddi_data["construct"] = closest_ddi["construct"]
                        return closest_ddi_data
                    break

            if not contains_ddi:
                closest_ddi_data = {}
                closest_ddi_data["drug_one"] = drug_one
                closest_ddi_data["drug_two"] = drug_two
                closest_ddi_data["time"] = "infinity"
                closest_ddi_data["construct"] = ""
                return closest_ddi_data
        else:
            print("Specified drugs cannot participate in a ddi")

    def get_drugs_list(self, node):
        drugsList = set()
        if node.children:
            for c in node.children:
                drugsList |= self.get_drugs_list(c)
        elif node.value.lower().startswith('take '):
            drugsList.add(node.value[5:])
        return drugsList

    def find_possible_ddis(self, node):
        mockDinto = MockDinto(filepath=default_csv_filepath, supress=True)
        drugsList = self.get_drugs_list(node)
        ddiDrugs = mockDinto.ddi_query(list(drugsList))
        return ddiDrugs

    def find_actual_ddis(self, node, first_drug, second_drug, minTime, duplicates):
        if node.children:
            # if 2 actions occur in parrelell with conflicting drugs a ddi is possible
            if node.construct == 'branch' or node.construct == 'process':
                contains_first_drug = []
                contains_second_drug = []
                for c in node.children:
                    local_drug_list = self.get_drugs_list(c)
                    if first_drug in local_drug_list:
                        contains_first_drug.append(c.name)
                    if second_drug in local_drug_list:
                        contains_second_drug.append(c.name)
                if contains_first_drug and contains_second_drug and \
                    not((len(contains_first_drug) == 1) and
                        (len(contains_second_drug) == 1) and contains_first_drug[0] == contains_second_drug[0]):
                    parallel_ddi = {}
                    parallel_ddi["first_drug"] = first_drug
                    parallel_ddi["second_drug"] = second_drug
                    parallel_ddi["construct"] = node.name
                    self.parallel_ddis.append(parallel_ddi)
            # sequence ddi check
            elif node.construct == 'sequence' or node.construct == 'iteration':
                for perm in self.permutations(node, first_drug, second_drug):
                    totalTime = self.evaluate_ddi(perm, first_drug, second_drug, minTime)
                    if totalTime >= 0 and totalTime < int(minTime):
                        sequential_ddi = {}
                        sequential_ddi["first_drug"] = first_drug
                        sequential_ddi["second_drug"] = second_drug
                        sequential_ddi["construct"] = node.name
                        sequential_ddi["time"] = totalTime
                        self.sequential_ddis.append(sequential_ddi)

                        if not duplicates:
                            break
            # run check on children nodes
            for c in node.children:
                self.find_actual_ddis(c, first_drug, second_drug, minTime, duplicates)

    def evaluate_ddi(self, path, first_drug, second_drug, minTime):
        if first_drug not in path or second_drug not in path:
            return -1
        last_drug = ''
        timesList = []
        localTimesList = []
        for element in path:
            if element.lower().startswith('wait ') and last_drug != '':
                localTimesList.append(element.lower())
            if element == first_drug:
                if last_drug == first_drug:
                    localTimesList = []
                if last_drug == second_drug:
                    timesList.append(localTimesList)
                    localTimesList = []
                last_drug = first_drug
            if element == second_drug:
                if last_drug == first_drug:
                    timesList.append(localTimesList)
                    localTimesList = []
                if last_drug == second_drug:
                    localTimesList = []
                last_drug = second_drug

        time_parser = PeosSimpleTimeParser()
        sorted_parsed_time = sorted(list(map(time_parser.get_total_time_delay_for_list, timesList)))
        if len(sorted_parsed_time) > 0 and sorted_parsed_time[0] < int(minTime):
            return sorted_parsed_time[0]
        else:
            return -1

    # Converts any standard time unit to seconds.
    def convert_time_to_secs(self, time_value, time_unit):
        if (time_unit == "sec"):
            return time_value
        elif (time_unit == "min"):
            return time_value * 60
        elif (time_unit == "hour"):
            return time_value * 60 * 60
        elif (time_unit == "day"):
            return time_value * 60 * 60 * 24
        elif (time_unit == "week"):
            return time_value * 60 * 60 * 24 * 7

        return -1

    def permutations(self, node, first_drug, second_drug):
        permutationsList = []
        if node.children:
            if node.construct == 'branch' or node.construct == 'process':
                for c in node.children:
                    permutationsList = self.combine_parallel(permutationsList, self.permutations(c, first_drug, second_drug))
            elif node.construct == 'selection':
                for c in node.children:
                    permutationsList = permutationsList + self.permutations(c, first_drug, second_drug)
            else:
                for c in node.children:
                    permutationsList = self.combine_seq(permutationsList, self.permutations(c, first_drug, second_drug))
                if node.construct == 'iteration':
                    morePerms = permutationsList
                    for c in node.children:
                        morePerms = self.combine_seq(morePerms, self.permutations(c, first_drug, second_drug))
                    permutationsList = permutationsList + morePerms
        else:
            if (node.value.lower().startswith('wait ')):
                permutationsList.append([node.value])
            elif node.value.lower().startswith('take ') and (node.value[5:] == first_drug or node.value[5:] == second_drug):
                permutationsList.append([node.value[5:]])
        return permutationsList

    # combines two events in sequence.
    # combine_seq([[abc][bac],[[ef][fe]]):
    #    results:
    #    [[abcef][abcfe][bacef][bacfe]]
    def combine_seq(self, list1, list2):
        result = []
        if not list1:
            return list2
        if not list2:
            return list1
        for lst1 in list1:
            for lst2 in list2:
                result.append(lst1+lst2)
        return result

    # combines to list of lists to generate all of the possible ways to combine the lists within them
    def combine_parallel(self, list1, list2):
        result = []
        if not list1:
            return list2
        if not list2:
            return list1
        for lst1 in list1:
            for lst2 in list2:
                result = result + self.in_order_combinations(lst1, lst2)
        return result

    # eg in_order_combinations(['a','b'], ['c','d'])
    # returns [
    # ['c', 'd', 'a', 'b']
    # ['c', 'a', 'd', 'b']
    # ['c', 'a', 'b', 'd']
    # ['a', 'c', 'd', 'b']
    # ['a', 'c', 'b', 'd']
    # ['a', 'b', 'c', 'd'] ]
    def in_order_combinations(self, lst1, lst2):
        permutations = []
        for locations in itertools.combinations(range(len(lst1) + len(lst2)), len(lst2)):
            result = lst1[:]
            for location, element in zip(locations, lst2):
                result.insert(location, element)
            permutations.append(result)
        return permutations

def sharedFlags(subparser):
    subparser.add_argument('-p', '--path', required=False, default=default_filepath, help='Path to PML file.')
    subparser.add_argument('-v', '--verbose', nargs='?', required=False, help='Prints the Tree Representation of the given PML file')
    subparser.add_argument('-m', '--mockdinto', required=False, default=default_csv_filepath, help='Path to CSV file.')

def clargs():
    parser = argparse.ArgumentParser(description='Parses a pml file to search within for DDIs.')
    sub_parser = parser.add_subparsers(dest='cmd')
    sub_parser.required = True

    # Parallel DDIs sub parser
    parallel = sub_parser.add_parser('parallel', help='Performs a query to find the parallel DDIs in the specified PML.')
    sharedFlags(parallel)

    # Sequential DDIs sub parser
    sequential = sub_parser.add_parser('sequential', help='Performs a query to find the sequential DDIs in the specified PML.')
    sharedFlags(sequential)

    # All DDIs sub parser
    all_query = sub_parser.add_parser('all', help='Performs a query to find all the DDIs in the specified PML.')
    sharedFlags(all_query)

    # Closest DDI sub parser
    closest = sub_parser.add_parser('closest', help="Performs a query to find the closest DDI between two drugs")
    closest.add_argument('-d', '--drugs', nargs='+', default=['coke', 'pepsi'], help='Drugs to find the closest interactions between.')
    sharedFlags(closest)

    # Save PML File sub parser
    saving = sub_parser.add_parser('save', help="Extracts content from a given source pml file and saves it to a destnation file.")
    saving.add_argument('--dest', nargs='?', help='Path to Destingation file')
    sharedFlags(saving)

    return parser.parse_args()

if __name__ == '__main__':
    args = clargs()

    if(args.mockdinto):
        default_csv_filepath = args.mockdinto

    pmlp = pmlParser.PmlParser(filename=args.path)
    ddiFinder = DdiFinder(pmlp.tree)

    if(args.verbose):
        print(pmlp)

    if(args.cmd == 'parallel' or args.cmd == 'all'):
        data = ddiFinder.get_parallel_ddis()
        if data == []:
            print("No parallel ddis found")
        else:
            for ddi_data in data:
                print("Parallel DDI found between {drug1} and {drug2} in {construct}. DDI type: {type}, DDI time: {time} {format}.".format(drug1=ddi_data["drug_one"], drug2=ddi_data["drug_two"], construct=ddi_data["construct"], type=ddi_data["type"], time=ddi_data["time"], format=ddi_data["time_unit"]))
    if(args.cmd == 'sequential' or args.cmd == 'all'):
        data = ddiFinder.get_sequential_ddis()
        if data == []:
            print("No sequential ddis found")
        else:
            for ddi_data in data:
                print("Sequential DDI found between {drug1} and {drug2} in {construct}. DDI type: {type}, DDI time: {time} {format}.".format(drug1=ddi_data["drug_one"], drug2=ddi_data["drug_two"], construct=ddi_data["construct"], type=ddi_data["type"], time=ddi_data["time"], format=ddi_data["time_unit"]))
    if(args.cmd == 'closest'):
        closest_ddi_data = ddiFinder.find_closest_ddis(args.drugs)
        if closest_ddi_data['construct'] is not "":
            print("Closest ddi approach for '%s' and '%s' is %s sec in construct '%s'" % (closest_ddi_data["drug_one"], closest_ddi_data["drug_two"], closest_ddi_data["time"], closest_ddi_data["construct"]))
        elif closest_ddi_data['time'] is not "infinity":
            print("Closest ddi approach for '%s' and '%s' is %s sec" % (closest_ddi_data["drug_one"], closest_ddi_data["drug_two"], closest_ddi_data["time"]))
        else:
            print("Closest ddi approach for '%s' and '%s' is %s" % (closest_ddi_data["drug_one"], closest_ddi_data["drug_two"], closest_ddi_data["time"]))
    if(args.cmd == 'save'):
        pmlp.save_pml(args.path, args.dest)


from django.shortcuts import render
from mainapp.models import Product, Partner
from django.views.decorators.http import require_POST
from django.http import JsonResponse
from mainapp.tasks import send_user_email

def main_page_view(request):
    partners = Partner.objects.all()
    products = Product.objects.all()
    return render(request, 'mainapp/index.html', locals())

@require_POST
def send_email(request):
    data = request.POST.copy()
    number = data.get('recipient-phone')
    name = data.get('recipient-name')
    button = data.get('button')
    print(button)
    message = u'Добрый день! На сайт поступила заявка от пользователя {name}. Перезвоните на номер: {number}. Была задействована кнопка: {button}'.format(number=number, name=name, button=button)
    send_user_email.delay(u'Заявка на звонок', message, email=None)
    return JsonResponse({'response': u'Ваши данные отправлены отправлены на сервер'})


"""
  Copyright (C) 2017, AT&T Inc. All rights reserved. Proprietary materials, property of AT&T. For internal use only,
  not for disclosure to parties outside of AT&T or its affiliates.
"""
from __future__ import unicode_literals, print_function
import json
import pathlib
import random

import spacy
from spacy.pipeline import EntityRecognizer
from spacy.gold import GoldParse
import os
#from spacy.pipeline import EntityRecognizer
from src.main.python.com.att.cmlp.common.nlp.extractentities.Entity import *
from src.main.python.com.att.cmlp.common.nlp.extractentities.LanguageName import *
from src.main.python.com.att.cmlp.common.nlp.extractentities.EntityType import *

'''
  This is the Entity Name Extractor using Spacy
  <p>
  This service will provide the list of entity names and entities for the Language
  </p>

  @author mn461x
  @since Jan 23, 2017
  @version $Id$
'''
class EntityExtractorUsingSpacy(object):
    """
    Constructor for the NLTK library

    @param LanguageName: Language name for the text

    """
    def __init__(self, LanguageName):
        self.Language =LanguageName
    """
         Method to extract entities for the given String
         * @param inText
         *            Input text name for entity extraction
    """
    entity_nlp=None

    def entityExtract(self,inText):
        if self.Language == LanguageName.english:
            nlp_lang = spacy.load('en',parser=False)
            EntityExtractorUsingSpacy.entity_nlp = nlp_lang

        arraylistobj =[]
        if nlp_lang != None:
            inputTextData = nlp_lang(inText)

            for entity in inputTextData.ents:

                entityType=entity.label_
                #print(entityType)
                eTypeoutput = None
                if entityType=='ORG':
                    eTypeoutput=EntityType.ORGANIZATION
                elif entityType=='PERSON':
                    eTypeoutput=EntityType.PERSON
                elif entityType=='LOC':
                    eTypeoutput=EntityType.LOCATION
                elif entityType=='DATE':
                    eTypeoutput=EntityType.DATE
                elif entityType=='TIME':
                    eTypeoutput=EntityType.TIME
                elif entityType=='MONEY':
                    eTypeoutput=EntityType.MONEY
                elif entityType=='PERCENT':
                    eTypeoutput=EntityType.PERCENT
                elif entityType=='GPE':
                    eTypeoutput=EntityType.GPE
                elif entityType=='NORP':
                    eTypeoutput=EntityType.NATIONALITY
                elif entityType=='FACILITY':
                    eTypeoutput=EntityType.FACILITY
                elif entityType=='PRODUCT':
                    eTypeoutput=EntityType.PRODUCT
                elif entityType=='EVENT':
                    eTypeoutput=EntityType.EVENT
                elif entityType=='WORK_OF_ART':
                    eTypeoutput=EntityType.ARTS
                elif entityType=='LANGUAGE':
                    eTypeoutput=EntityType.LANGUAGE
                elif entityType=='QUANTITY':
                    eTypeoutput=EntityType.QUANTITY
                elif entityType=='ORDINAL':
                    eTypeoutput=EntityType.ORDINAL
                elif entityType=='CARDINAL':
                    eTypeoutput=EntityType.CARDINAL

                arraylistobj.append(Entity(entity.text, eTypeoutput))

        return arraylistobj

    """
         Method to extract supported  list of entities from NLTK library
    """
    def getEntities(self):
        arraylistobj =[]
        arraylistobj.append(EntityType.ORGANIZATION)
        arraylistobj.append(EntityType.PERSON)
        arraylistobj.append(EntityType.LOCATION)
        arraylistobj.append(EntityType.DATE)
        arraylistobj.append(EntityType.TIME)
        arraylistobj.append(EntityType.MONEY)
        arraylistobj.append(EntityType.PERCENT)
        arraylistobj.append(EntityType.FACILITY)
        arraylistobj.append(EntityType.GPE)
        arraylistobj.append(EntityType.NATIONALITY)
        arraylistobj.append(EntityType.PRODUCT)
        arraylistobj.append(EntityType.EVENT)
        arraylistobj.append(EntityType.LANGUAGE)
        arraylistobj.append(EntityType.ARTS)
        arraylistobj.append(EntityType.QUANTITY)
        arraylistobj.append(EntityType.ORDINAL)
        arraylistobj.append(EntityType.CARDINAL)

        return arraylistobj

    def trainModel(self, train_data, entity_types, modelOutFilePath):

        # Add new words to vocab.
        for raw_text, _ in train_data:
            doc = EntityExtractorUsingSpacy.entity_nlp.make_doc(raw_text)
            for word in doc:
                _ = EntityExtractorUsingSpacy.entity_nlp.vocab[word.orth]

        # Train NER.
        ner = EntityRecognizer(EntityExtractorUsingSpacy.entity_nlp.vocab, entity_types=entity_types)
        for itn in range(5):
            random.shuffle(train_data)
            for raw_text, entity_offsets in train_data:
                doc = EntityExtractorUsingSpacy.entity_nlp.make_doc(raw_text)
                gold = GoldParse(doc, entities=entity_offsets)
                ner.update(doc, gold)

        ner.model.end_training()

        model_dir = pathlib.Path(modelOutFilePath)
        if not model_dir.exists():
            model_dir.mkdir()

        with (model_dir / 'config.json').open('w') as file_:
            json.dump(ner.cfg, file_)
        ner.model.dump(str(model_dir / 'model'))
        if not (model_dir / 'vocab').exists():
            (model_dir / 'vocab').mkdir()
        ner.vocab.dump(str(model_dir / 'vocab' / 'lexemes.bin'))
        with (model_dir / 'vocab' / 'strings.json').open('w', encoding='utf8') as file_:
            ner.vocab.strings.dump(file_)

        return ner
if __name__ == '__main__':

    #inText = "One year ago, several hours before cities across the United States started their annual fireworks displays, a different type of fireworks were set off at the European Center for Nuclear Research (CERN) in Switzerland. At 9:00 a.m., physicists announced to the world that they had found something they had been searching for nearly 50 years: the elusive Higgs boson. Today time is 12'o clock morning , on the anniversary of its discovery, are we any closer to figuring out what that particle's true identity is? The Higgs boson is popularly referred to as the God particle, perhaps because of its role in giving other particles their mass. However, it's not the boson itself that gives mass. Back in 1964, Peter Higgs proposed a theory that described a universal field that particles interacted with. Pay 20.403 dollar. Earned 23.8% (percent) amd time is 12.15 morning "
    inText = "Pierre Vinken , 61 years old , will join the board as a nonexecutive director Nov. 29 . Mr . Vinken is chairman of Elsevier N.V. , the Dutch publishing group .Rudolph Agnew , 55 years old and former chairman of Consolidated Gold Fields PLC , was named a director of this British industrial conglomerate."

    entSpc = EntityExtractorUsingSpacy(LanguageName.english)
    outputData = entSpc.entityExtract(inText)
    print("List of entities for the given text :")
    for ent in outputData:
        print ("Entity Name :" , ent.getEntityName(), "Entity Type :" ,ent.getEntityType())
    print("List of available entities in this model :")
    for ent in entSpc.getEntities():
        print(ent)

    train_data = [
        (
            'What is AT&T Inc?',
            [(len('What is '), len('What is AT&T Inc'), 'ORGANIZATION')]
        ),
        (
            'They have DirectTV and UVerse',
            [(len('They have'), len('They have DirectTV'), 'PRODUCT'),
            (len('They have DirectTV and '), len('They have DirectTV and UVerse'), 'PRODUCT')]
        )
    ]

    print(os.getcwd())

    ner=entSpc.trainModel( train_data, ['ORGANIZATION', 'PRODUCT'],'C:\\Works\\common-nlp\\EntityExtractionUsingPython\\src\\main\\resources')

    doc = entSpc.entity_nlp.make_doc('What is AT&T Inc?')
    entSpc.entity_nlp.tagger(doc)
    ner(doc)
    for word in doc:
        print(word.text,  word.ent_type_, word.ent_iob)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 18 17:52:39 2020

@author: liamparker
"""

from sklearn.linear_model import LogisticRegressionCV
from sklearn.utils.testing import ignore_warnings
from sklearn.exceptions import ConvergenceWarning
from keras.utils import to_categorical
from tensorflow.keras import layers
from tensorflow.keras import activations
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D, Flatten, Activation, MaxPooling2D, Dropout
import numpy as np
import time

class model:
    def __init__(self, train, test, train_label, test_label):
        """
        :param train->array: represents the training data set
        :param test->array: represents the validation data set
        :param train_label->list: represents the training label list
        :param test_label->list: represents the validation label list
        """

        self.train     = train
        self.test      = test
        self.trl       = train_label
        self.tel       = test_label

    @ignore_warnings(category=ConvergenceWarning)
    def logistic_regression(self):
        """Logistic regressor model from SKLearn"""
        clf = LogisticRegressionCV(penalty = 'l2', max_iter = 500, solver = 'saga')
        clf.fit(self.train, self.trl)
        score = clf.score(self.test, self.tel)
        return clf, score

    def CNN(self, loadModel, feature):
        """Input CNN code"""
        print(self.train.shape)

        N = self.train.shape[0] + self.test.shape[0]

        train = np.reshape(self.train, (self.train.shape[0], 178, 218, 3))
        test = np.reshape(self.test, (self.test.shape[0], 178, 218, 3))

        y_train = to_categorical(self.trl, num_classes = 2)
        y_test = to_categorical(self.tel, num_classes = 2)

        if loadModel:
            print('loading model')
            model = keras.models.load_model('C:/Users/onale/Desktop/Project/'+feature)
        else:
            #create model
            model = Sequential()

            #convolutional layers
            model.add(Conv2D(filters = 32, kernel_size=3, strides = 1, padding = 'same',
                         input_shape=(178,218,3), activation ='relu'))
            model.add(MaxPooling2D(pool_size=(2,2)))

            model.add(Conv2D(filters = 64, kernel_size=3, strides = 1, activation='relu'))
            model.add(MaxPooling2D(pool_size=(2,2)))

            model.add(Conv2D(filters = 128, kernel_size=3, strides=1, activation='relu'))
            model.add(MaxPooling2D(pool_size=(2,2)))

            model.add(Conv2D(filters = 256, kernel_size=3, strides=1, activation='relu'))
            model.add(MaxPooling2D(pool_size=(2,2)))

            #fully connected layers
            model.add(Flatten())
            model.add(Dense(units = 64))
            model.add(Activation('relu'))
            model.add(Dropout(0.05))

            model.add(Dense(units = 32))
            model.add(Activation('relu'))
            model.add(Dropout(0.05))

            model.add(Dense(2, activation='softmax'))

            #compile model using accuracy as a measure of model performance
            model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

        #train model
        model.fit(train, y_train,validation_data=(test, y_test), epochs=3)
        model.save('C:/Users/onale/Desktop/Project/'+feature)
        return model, score


import redis
import uuid
import time
import json
conn = redis.StrictRedis(host='192.168.0.4')

def get_str(x):
    if type(x) == type(b''):
        return x.decode('utf-8')
    elif x == None:
        return ''
    else:
        return str(x)

def acquire_lock(conn,key,acquire_timeout=10,lock_timeout=20):
    lock_key = 'lock:%s' % key

    lock_id = str( uuid.uuid4() )

    now = time.time()

    while time.time() < now + acquire_timeout:
        if conn.setnx(lock_key,lock_id):
            conn.expire(lock_key,lock_timeout)
            return lock_id
        else:
            time.sleep(0.05)
    return False

def release_lock(conn,key,uid):
    pipeline = conn.pipeline()

    lock_key = 'lock:%s' % key

    while True:
        try:
            pipeline.watch(lock_key)
            if pipeline.get(lock_key) == uid:
                pipeline.multi()

                pipeline.delete(lock_key)

                return True
            else:
                return False
        except redis.exceptions.WatchError:
            pass

def execute_later(conn,funcname,args,kwargs=None,delay=2):
    # 用来上锁
    uid = str( uuid.uuid4() )
    now = time.time()

    data = {
        "id" : uid,
        "funcname" : funcname,
        "args" : args,
        "kwargs" : kwargs
    }

    data = json.dumps(data)

    conn.zadd('delay:',now+delay,data)

def poll_queue(conn):
    while True :
        now = time.time()

        pop = conn.zrange('delay:',0,0,withscores=True)

        if not pop or pop[0][-1] > now :
            time.sleep(1)
        else:
            data = json.loads( get_str(pop[0][0]) )
            _id = data['id']
            funcname = data['funcname']
            args = data['args']
            kwargs = data['kwargs']

            lock_id = acquire_lock(conn,_id)

            if kwargs:
                (globals()[funcname])(*args,**kwargs)
            else:
                (globals()[funcname])(*args)

            release_lock(conn,_id,lock_id)

def test():
    execute_later(conn,'_print',['x'],{},2)

    poll_queue(conn)

if __name__ == '__main__':
    test()


from collections import namedtuple
from django.db import models
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse

from .functions import make_comma_list
from ..common import CommaSeparatedListElement

class ProfileAreaManager(models.Manager):

    # Needed to natural key deserialization
    def get_by_natural_key(self, title):
        return self.get(title=title)

class ProfileArea(models.Model):
    """
    An area of information that we collect about a user (ex. where they've
    lived, who they identify as, what languages they speak).
    """

    title = models.CharField(help_text="Brief title for this area of the profile",
                               max_length=100, unique=True)

    description = models.CharField(help_text="Explanation of this area that "
                                   "can be displayed to the user", max_length=200)

    prefix = models.CharField(help_text="A few words that can go before "
                              "elements in this area to introduce them", max_length=100)

    required = models.BooleanField(help_text="Whether the user is required to provide "
                                     "this information", default=False)

    icon = models.CharField(help_text="FontAwesome icon name that represents this area",
                            max_length=20)

    allow_additions = models.BooleanField(help_text="Whether the user can add "
                                          "their own custom answer for this area",
                                          default=False)

    # Choices for display to the user
    DISPLAY_SELECT2 = 0

    DISPLAY_CHOICES = (
        (DISPLAY_SELECT2, "Display as a select2 field with autocomplete"),
    )
    display_mode = models.PositiveSmallIntegerField(choices=DISPLAY_CHOICES)

    order = models.PositiveSmallIntegerField(default=0)

    class Meta:
        ordering = ["order"]

    # Register custom manager
    objects = ProfileAreaManager()

    def get_privacy_level_choices(self):
        """
        Get the possible privacy level choices for this area. If the area is
        required, then the user must provide information.
        """
        if self.required:
            return PrivacySetting.PRIVACY_LEVEL_CHOICES[1:]
        else:
            return PrivacySetting.PRIVACY_LEVEL_CHOICES

    def get_profile_element_by_name(self, name):
        """
        Gets the ProfileElement in this area with the given name. If it doesn't
        exist and allow_additions is set, then a new ProfileElement will be
        created.
        """
        try:
            return self.profile_element_set.get(name = name)
        except ProfileElement.DoesNotExist as e:
            if self.allow_additions:
                profile_element = ProfileElement(name = name, area = self,
                                                 public = False)
                profile_element.save()
                return profile_element
            else:
                # Re-raise DoesNotExist because this shouldn't happen
                raise

    # Natural key serialization lets us not use explicit PKs in dumps
    def natural_key(self):
        return (self.title,)

    def __str__(self):
        return self.title

    # The following code allows caching of ProfileAreas and ProfileElements so
    # we don't have to access the database every time we need to look at the
    # ProfileAreas
    _profile_areas = None

    @staticmethod
    def get_profile_areas():
        """
        Gets all ProfileAreas. Each ProfileArea has a public_profile_elements
        attribute which is a list of ProfileElements in that area that are
        public. This method caches ProfileAreas/ProfileElements; the database
        is only accessed the first time this method is called.
        """
        if not ProfileArea._profile_areas:
            ProfileArea._profile_areas = ProfileArea.objects.all() # Load ProfileAreas
            for area in ProfileArea._profile_areas:
                # Load public ProfileElements
                area.public_profile_elements = area.profile_element_set.filter(public = True).all()
        return ProfileArea._profile_areas

class ProfileElementManager(models.Manager):

    def get_by_natural_key(self, area, name):
        return self.get(name=name, area=ProfileArea.objects.get_by_natural_key(*area))

class ProfileElement(models.Model):
    """
    A piece of information about a user in a ProfileArea (ex. identifies as a
    Palestinian, speaks Hebrew, or has lived in Israel).
    """

    name = models.CharField(max_length=100)

    area = models.ForeignKey(ProfileArea, related_name="profile_element_set", db_index=True)

    public = models.BooleanField(help_text="Whether this should be given as an "
                                 "option for all users. If allow_additions is "
                                 "set on this element's ProfileArea, then new "
                                 "elements can be added, but will start off "
                                 "not public.", default=True)

    order = models.PositiveSmallIntegerField(default=0)

    # Register custom manager
    objects = ProfileElementManager()

    class Meta:
        unique_together = ('name', 'area')
        ordering = ["area", "order"]

    def natural_key(self):
        return (self.area.natural_key(), self.name)
    # Needed to make sure areas are dumped before these so that the foreign
    # keys refer to something
    natural_key.dependencies = ['profiles.profilearea']

    def __str__(self):
        return "%s: %s" % (self.area.title, self.name)

DisplayProfileArea = namedtuple('DisplayProfileArea', ('icon', 'title',
                                                       'prefix', 'description',
                                                       'list'))
"""
Named tuple intended to be returned from Profile.get_profile_areas. icon, title,
prefix, and description are all taken from the given ProfileArea, while list
is a string containing the ProfileElements in that area separated by commas.
"""

class Profile(models.Model):
    """
    Stores additional information about users, such as their display name.
    """
    user = models.OneToOneField(User, related_name="profile")
    display_name = models.CharField(max_length=50, default="", unique=True)
    description = models.CharField(max_length=160, default="",
                                   help_text = "Short introduction to this profile")
    elements = models.ManyToManyField(ProfileElement)

    reputation = models.IntegerField(default = 1)
    """
    A profile's reputation. It should be equal to 1 + the sum of the
    value of the ReputationEvents that have been applied to the profile.
    While this may be negative, it should never be displayed below 1.
    """

    def update_from_form(self, form):
        """
        Take cleaned data from a profile form and update the user's
        profile accordingly. Note that this saves the profile and all
        associated ProfileElements.
        """

        # Update display name and description, only saving if it's changed
        has_changed = False
        if self.display_name != form.cleaned_data['display_name']:
            self.display_name = form.cleaned_data['display_name']
            has_changed = True
        if self.description != form.cleaned_data['description']:
            self.description = form.cleaned_data['description']
            has_changed = True
        if has_changed:
            self.save()

        current_elements = self.elements.all()
        privacy_settings = self.privacy_setting_set.all()
        for profile_area in ProfileArea.get_profile_areas():
            privacy_setting = None
            privacy_setting_updated = False # Used to see if we need to save
            try:
                # Look for a PrivacySetting for the given ProfileArea
                privacy_setting = next(filter(lambda s: s.area == profile_area, privacy_settings))
            except(StopIteration): # PrivacySetting doesn't exist yet
                privacy_setting = PrivacySetting(profile=self, area=profile_area)
                privacy_setting_updated = True
            form_privacy_level = int(form.cleaned_data['privacy_settings_' + str(profile_area.pk)])
            if privacy_setting.privacy_level != form_privacy_level:
                privacy_setting.privacy_level = form_privacy_level
                privacy_setting_updated = True
            if privacy_setting_updated:
                privacy_setting.save()

            # List of current ProfileElements in this area
            current_area_elements = filter(lambda e: e.area == profile_area,
                                           current_elements)
            if form_privacy_level == PrivacySetting.DONT_COLLECT:
                for current_element in current_area_elements:
                    self.elements.remove(current_element)
            else:
                form_elements_data = form.cleaned_data['profile_area_' +
                                                       str(profile_area.pk)]
                # Go through current elements in this area to see if any need
                # to be removed
                for current_profile_element in current_area_elements:
                    if current_profile_element.name not in form_elements_data:
                        # Element is in profile but not in form
                        self.elements.remove(current_profile_element)
                # Go through elements in form to see if any need to be added
                for form_element_name in form_elements_data:
                    profile_element = profile_area.get_profile_element_by_name(form_element_name)
                    if profile_element not in current_elements:
                        # Element is in form but not in profile
                        self.elements.add(profile_element)

    def get_profile_areas(self, min_privacy_level = None):
        """
        Returns a list of DisplayProfileAreas that can be used in a template to
        render information about a user's profile. min_privacy_level indicates
        the minimum level about which to give information; the default is
        PrivacySetting.ANYONE.
        """

        # We can't declare a default value that hasn't been declared yet
        if min_privacy_level == None:
            min_privacy_level = PrivacySetting.ANYONE

        profile_areas = []
        elements = self.elements.all()
        privacy_settings = self.privacy_setting_set.all()
        for profile_area in ProfileArea.get_profile_areas():
            privacy_setting = list(filter(lambda s: s.area == profile_area, privacy_settings))
            if len(privacy_setting) > 0: # If PrivacySetting exists for this area
                privacy_setting = privacy_setting[0]
                if privacy_setting.privacy_level >= min_privacy_level:
                    elements_in_area = list(filter(lambda e: e.area == profile_area, elements))
                    if len(elements_in_area) > 0: # If there's any info about this area
                        list_els = [CommaSeparatedListElement(content = e.name, bold = True, link = None)
                                    for e in elements_in_area]
                        profile_areas.append(DisplayProfileArea(title = profile_area.title,
                                                                description = profile_area.description,
                                                                prefix = profile_area.prefix,
                                                                icon = profile_area.icon,
                                                                list = list_els))

        return profile_areas

    def get_private_profile(self):
        """
        Shortcut to get_profile_areas(PrivacySetting.ONLY_ME), which
        allows this to be used in templates.
        """
        return self.get_profile_areas(PrivacySetting.ONLY_ME)

    def get_display_reputation(self):
        """
        Returns this user's reputation, unless it is below 1, in which case
        it returns 1.
        """

        return max(1, self.reputation)

    def get_url(self):
        """
        Get the URL to this user's public profile page.
        """

        return reverse('profiles:public_profile',
                       kwargs = {'profile_id': self.id})

    def get_recent_posts(self, n = 3):
        """
        Get the n most recent posts from this user.
        """

        return self.posts.order_by('-date_created')[:n]

    def get_top_posts(self, n = 3):
        """
        Get the n most upvoted posts from this user.
        """

        return self.posts.order_by('-net_vote')[:n]

    def __str__(self):
        return self.display_name

    @staticmethod
    def user_has_profile(user):
        """
        Determine if the user has an associated profile yet.
        """
        try:
            user.profile
            return True
        except(AttributeError):
            return False

class PrivacySetting(models.Model):
    """
    Describes how visible a user's information is in a particular
    ProfileArea.
    """

    profile = models.ForeignKey(Profile, related_name="privacy_setting_set", db_index=True)
    area = models.ForeignKey(ProfileArea, related_name="privacy_setting_set")

    # Privacy levels
    DONT_COLLECT = 0
    ONLY_ME = 100 # Big gaps leave room for more privacy levels in between
    ANYONE = 200
    PRIVACY_LEVEL_CHOICES = (
        (DONT_COLLECT, "Don't collect this information"),
        (ONLY_ME, "Only I can see this"),
        (ANYONE, "Anyone can see this"),
    )
    privacy_level = models.PositiveSmallIntegerField(choices=PRIVACY_LEVEL_CHOICES)


# -*- coding: utf-8 -*-
"""
Created on Thu Sep 13 10:08:31 2018

Funciones para detección de valores atípicos, duplicados y/o faltantes.
@author: Jergb
"""

import pandas as pd
import sqlalchemy
# import time
from datetime import timedelta

def bajar():
    'Consulta filas de la tabla "mediciones" en la base de datos de la red.'
    global WSN, fila
    fila = str(fila)
    motor = 'mysql+pymysql://root:@localhost:3306/WSN'
    engine = sqlalchemy.create_engine(motor)
    if pd.read_sql_query('select * from medidas where IDMEDIDA ='
                         + fila, engine).empty == 0:
        WSN = WSN.append(pd.read_sql_query('select * from medidas\
                         where IDMEDIDA ='+fila, engine), ignore_index=True)

        if len(WSN) == 1:
            WSN = pd.DataFrame([WSN.iloc[0, :]]).append(WSN, ignore_index=True)
            WSN.loc[0, 'TIME'] = WSN.loc[0, 'TIME'] - timedelta(seconds=60)
            WSN.loc[0, 'NODO'] = 0

        fila = int(fila) + 1
        return

def bajarprueba():
    global WSN, fila
    fila = str(fila)
    motor = 'mysql+pymysql://root:@localhost:3306/WSN'
    engine = sqlalchemy.create_engine(motor)
    if pd.read_sql_query('select * from pruebatabla where ID ='
                         + fila, engine).empty == 0:
        WSN = WSN.append(pd.read_sql_query('select * from pruebatabla\
                         where ID ='+fila, engine), ignore_index=True)

        if len(WSN) == 1:
            WSN = pd.DataFrame([WSN.iloc[0, :]]).append(WSN, ignore_index=True)
            WSN.loc[0, 'TIME'] = WSN.loc[0, 'TIME'] - timedelta(seconds=60)
            WSN.loc[0, 'NODO'] = 0

        fila = int(fila) + 1
        return

def timeTranscurrido(i):
    global WSN
    TiempoTranscurrido = int((WSN.TIME[i] - WSN.TIME[i-1]).seconds / 60)
    TiempoTranscurrido += (WSN.TIME[i]-WSN.TIME[i-1]).days * 24 * 60
    return TiempoTranscurrido

def Nfalt(Ni):
    F = []
    for N in [2, 5]:
        if N not in Ni:
            F.append(N)
    return F

def agregar(row):
    global WSN
    WSN = WSN.append(pd.DataFrame(WSN.iloc[[len(WSN) - 2, len(WSN) - 1], :]),
                     ignore_index=True)
    WSN.loc[len(WSN) - 1:, 'TIME'] = WSN.loc[len(WSN) - 1:, 'TIME']\
        + timedelta(seconds=60)
    WSN.loc[len(WSN) - 2:, 'TIME'] = WSN.loc[len(WSN) - 2:, 'TIME']\
        + timedelta(seconds=60)
    WSN.loc[[len(WSN) - 1, len(WSN) - 2], 'NODO'] = 0
    filtro(row+1)
    filtro(row+2)
    return

def filtro(i):
    global WSN, Duplicados, Faltantes, RIS, Ni, Faltan
    WSN['TIME'] = [pd.Timestamp(x) for x in WSN['TIME']]

    TiempoTranscurrido = timeTranscurrido(i)

    if (TiempoTranscurrido == 0 or (TiempoTranscurrido == 1
       and WSN.NODO[i] == WSN.NODO[i-1]) or Ni == []):
        Ni.append(WSN.NODO[i])

    if ((WSN.TIME[i].minute == WSN.TIME[i+1].minute and WSN.TIME[i].minute
       != WSN.TIME[i+2].minute and WSN.NODO[i] != WSN.NODO[i+1])
       or (WSN.TIME[i-1].minute == WSN.TIME[i].minute and WSN.TIME[i].minute
       != WSN.TIME[i+1].minute and WSN.NODO[i-1] != WSN.NODO[i])):

        RIS = RIS.append(pd.DataFrame([WSN.iloc[i]]), ignore_index=True)

    else:
        if WSN.TIME[i] == WSN.TIME[i-1] and WSN.NODO[i] == WSN.NODO[i-1]:

            Duplicados[WSN.NODO[i]][0] += 1

        elif TiempoTranscurrido >= 1:

            F = Nfalt(Ni)
            Faltan += TiempoTranscurrido - 1
            for F_i in F:

                Faltantes[F_i][0] += 1

            for N in [2, 5]:

                Faltantes[N][0] += TiempoTranscurrido - 1

            F = []
            Ni = []
    return

def tablas(resultados):
    'tablas crea los DataFraMes utilizadas para el analísis de la información.'
    tabla2 = resultados.query('NODO==2').reset_index(drop=True)
    tabla5 = resultados.query('NODO==5').reset_index(drop=True)
    return tabla2, tabla5


from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db.models import Q
from django.shortcuts import render, redirect
from django.urls import reverse
from django.views import generic

from .forms import ArticleForm, CommentForm
from .models import Article, Profile, Comment

class IndexArticleListView(generic.ListView):
    model = Article
    template_name = "blog/article_listing.html"
    paginate_by = 10
    context_object_name = 'article_list'

    def get_queryset(self):
        return Article.objects.filter(deleted_at__isnull=True).order_by('-created_at')

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)
        context["page_title"] = "Latest Articles"
        return context

class PopularArticleListView(generic.ListView):
    model = Article
    template_name = "blog/article_listing.html"
    paginate_by = 10
    context_object_name = 'article_list'

    def get_queryset(self):
        return Article.objects.filter(deleted_at__isnull=True).order_by('hit_count')

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)
        context["page_title"] = "Popular Articles"
        return context

class SearchArticleListView(generic.ListView):
    model = Article
    template_name = "blog/article_listing.html"
    paginate_by = 10
    context_object_name = 'article_list'

    def get_queryset(self):
        query_text = self.request.GET["q"]

        query = Q(deleted_at__isnull=True)

        filter_query = Q(title__contains=query_text)
        filter_query.add(Q(content__contains=query_text), Q.OR)

        query.add(filter_query, Q.AND)

        return Article.objects.filter(query).order_by('created_at')

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)

        context["page_title"] = "Search Results"
        context["search_query"] = self.request.GET["q"]

        return context

class TaggedArticleListView(generic.ListView):
    model = Article
    template_name = "blog/article_listing.html"
    paginate_by = 10
    context_object_name = 'article_list'

    def get_queryset(self):
        return Article.objects.filter(tags__name__exact=self.kwargs["name"]).order_by('created_at')

    def get_context_data(self, *, object_list=None, **kwargs):
        context = super().get_context_data(**kwargs)

        context["page_title"] = "Tagged Articles #" + self.kwargs["name"]

        return context

class ProfileListView(generic.ListView):
    model = Profile
    template_name = "blog/profile_listing.html"
    paginate_by = 10
    context_object_name = 'profile_list'

    def get_queryset(self):
        return Profile.objects.filter(deleted_at__isnull=True).order_by('created_at')

def article_single(request, article_id):
    article_item = Article.objects.get(pk=article_id)
    article_item.hit_count += 1
    article_item.save()

    comments = article_item.comment_set.order_by('-created_at').all()

    comment_form = CommentForm()
    comment_form.fields['article'].initial = article_item.id

    return render(request, 'blog/article_single.html', context={
        "article": article_item,
        "comments": comments,
        "comment_form": comment_form
    })

def profile_single(request, username=None):
    profile = Profile.objects.get(user__username=username)

    return render(request, 'blog/profile_single.html', context={
        "profile": profile
    })

@login_required
def article_create(request, article_id=None):
    action_url = reverse('article_create')

    article_instance = None

    if article_id:
        article_instance = Article.objects.get(pk=article_id)
        action_url = reverse('article_edit', kwargs={'article_id': article_id})

    if request.method == 'POST':
        form = ArticleForm(request.POST)

        if form.is_valid():
            if article_instance:
                article_instance.title = form.cleaned_data["title"]
                article_instance.content = form.cleaned_data["content"]
            else:
                article_instance = Article(
                    profile=request.user.profile_set.first(),
                    title=form.cleaned_data["title"],
                    content=form.cleaned_data["content"],
                )
                article_instance.save()

            article_instance.tags.set(form.cleaned_data["tags"])
            article_instance.save()

            messages.success(request, 'Article saved: ' + str(article_instance))

            return redirect(reverse('article_single', kwargs={'article_id': article_instance.id}))
    else:
        form = ArticleForm(instance=article_instance)

    return render(request, "blog/article_create.html", context={
        "form": form,
        "action_url": action_url
    })

@login_required
def comment_create(request):
    if request.method == 'POST':
        form = CommentForm(request.POST)

        related_article_id = None

        if form.is_valid():
            content = form.cleaned_data["content"]
            related_article_id = form.cleaned_data["article"]

            comment = Comment(
                content=content,
                profile=request.user.profile_set.first(),
                article=Article.objects.get(pk=related_article_id),
                parent=None
            )

            comment.save()
            messages.success(request, 'Comment saved: ' + str(comment))

        return redirect(reverse('article_single', kwargs={'article_id': related_article_id}))

    messages.warning(request, 'Unauthorized entry.')
    return redirect(reverse('index'))

@login_required
def populate_db(request):
    from . import factories

    users = factories.UserFactory.build_batch(40)
    for item in users:
        item.save()

    messages.success(request, '%s users created.' % len(users))

    profiles = factories.ProfileFactory.build_batch(40)
    for item in profiles:
        item.user.save()
        item.user_id = item.user.id
        item.save()

    messages.success(request, '%s profiles created.' % len(profiles))

    articles = factories.ArticleFactory.build_batch(40)
    for item in articles:
        item.profile.user.save()
        item.profile.user_id = item.profile.user.id

        item.profile.save()
        item.profile_id = item.profile.id

        item.save()

        tags = factories.TagFactory.build_batch(3)
        for tag in tags:
            tag.save()
            item.tags.add(tag)

        item.save()

    messages.success(request, '%s article created.' % len(articles))

    return redirect(reverse('index'))


# file: absolute.py
# !/usr/bin/python
import requests
from PyQt6.QtCore import QThread, pyqtSignal
from PyQt6.QtGui import QPixmap
from PyQt6.QtWidgets import QWidget, QLabel, QApplication, QTextEdit, QPushButton, QLineEdit, QFileDialog

import ReadExec
import  sys

class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.open_th = None
        self.th_get_im = None
        self.cwd = None
        self.n = 1
        self.all_page = 0
        self.rd = ReadExec.ReadExec()
        self.photo = QPixmap()
        self.name = "" #旧标题
        self.new_name = "" #新标题
        self.url = "" #图片链接

        self.th = MyThread(self)  # 重写的多线程对象

        self.init_ui()
        self.resize(1000, 500)  # 设置窗口大小
        self.move(100, 0)

    def change_page(self, sigin):
        self.button_next.setEnabled(False)
        self.button_up.setEnabled(False)
        self.img.setText("图片加载中...")
        #print("self.name1=", self.name)
        #print("self.name2=", self.new_name)
        #print("当标题发生改变时，触发保存函数")
        if self.name != self.new_name:
            #print("执行保存")
            self.save_excl()
        #print("sigin=", sigin)
        if sigin == "next":
            #print("执行get_next")
            self.n = self.n + 1
        if sigin == "up":
            #print("执行get_up")
            self.n = self.n - 1
        if sigin == "rest":
            pass
            #print("执行刷新")
        img_url, name = self.rd.get_image(self.n)
        self.name = name
        self.lb_name.setText(name)  # 设置文本内容
        self.page_name.setText(str(self.n))  # 设置当前页码
        self.th_get_im = GetTh(img_url, self.n, self.all_page)
        self.th_get_im.start()  # 启动线程
        self.th_get_im.signal_photo.connect(self.change_img)  # 接受来自线程的图片数据
        self.th_get_im.signal_next.connect(self.change_next)
        self.th_get_im.signal_up.connect(self.change_up)
        self.th_get_im.exit()

    def change(self, msg):
        self.lb_name.setText(msg)

    def change_img(self, msg):
        #print("图片数据:", msg)
        if msg["msg"]=="no":
            self.img.setText("图片加载失败")
        else:
            self.img.setPixmap(msg["im"])  # 设置图片

    def change_next(self, msg):
        self.button_next.setEnabled(msg)

    def change_up(self, msg):
        self.button_up.setEnabled(msg)

    def save_excl(self):
        tx = self.lb_name.toPlainText()
        self.size_name.setText(str(len(tx)))
        self.rd.sava_f(self.n, tx)

    def init_ui(self):
        self.setMaximumSize(1000, 1200)
        # 显示图片
        self.img = QLabel("", self)
        self.img.move(270, 5)  # 位置
        self.img.setFixedSize(250, 250)  # 大小
        self.img.setScaledContents(True)  # 设置图片自适应窗口大小
        # 商品标题
        lb_name1 = QLabel("标题:", self)
        lb_name1.move(20, 270)
        # 标题
        self.lb_name = QTextEdit(self)
        self.lb_name.setPlaceholderText("标题")
        self.lb_name.move(60, 270)
        self.lb_name.setFixedSize(500, 100)  # 大小
        self.lb_name.textChanged.connect(self.th.run)  # 当内容发生变化时触发此信号，
        # 显示标题长度
        self.size_name_t = QLabel("标题长:", self)
        self.size_name_t.move(561, 270)
        self.size_name = QLabel("0", self)
        self.size_name.move(620, 270)
        self.size_name.setFixedSize(40, 20)
        self.size_name.setText(str(len(self.lb_name.toPlainText())))
        # 显示页码
        self.page_t = QLabel("总页码:", self)
        self.page_t.move(563, 320)

        self.page_all = QLabel("0", self)
        self.page_all.move(620, 320)
        self.page_all.setFixedSize(40, 20)

        self.page_t2 = QLabel("当前:", self)
        self.page_t2.move(563, 350)
        self.page_t2.setFixedSize(40, 20)

        self.page_name = QLabel("0", self)
        self.page_name.move(620, 350)
        self.page_name.setFixedSize(40, 20)
        self.page_name.setText(self.page_name.text())
        # 保存按钮
        button_save = QPushButton("保存", self)
        button_save.setFixedSize(100, 50)
        button_save.move(650, 270)
        button_save.clicked.connect(self.save_excl)
        # 刷新
        button_res = QPushButton("刷新", self)
        button_res.setFixedSize(100, 50)
        button_res.move(750, 270)
        button_res.clicked.connect(lambda: self.change_page("rest"))
        # 下一条按钮
        self.button_next = QPushButton("下一条", self)
        self.button_next.setFixedSize(100, 50)
        self.button_next.move(650, 320)
        self.button_next.clicked.connect(lambda: self.change_page("next"))
        # 上一条按钮
        self.button_up = QPushButton("上一条", self)
        self.button_up.setFixedSize(100, 50)
        self.button_up.move(750, 320)
        self.button_up.clicked.connect(lambda: self.change_page("up"))
        # 文件选择区域
        # 标题
        path_name = QLabel("文件:", self)
        path_name.move(20, 456)
        # 单行文本
        self.path_edit = QLineEdit(self)
        self.path_edit.move(60, 450)
        self.path_edit.setFixedSize(500, 30)
        # 选择文件
        self.opt_path_button = QPushButton("选择文件", self)
        self.opt_path_button.clicked.connect(self.select_f)
        self.opt_path_button.move(570, 450)
        # 确定按钮
        self.ok_path_button = QPushButton("确定", self)
        self.ok_path_button.move(660, 450)
        self.ok_path_button.clicked.connect(self.ok_select_f)

        #测试按钮
        #self.test_button=QPushButton("测试",self)
        #self.test_button.move(720,450)
        #self.le=QLineEdit(self)

        self.setGeometry(300, 300, 350, 250)
        self.setWindowTitle('亚马逊商品修改')
        self.show()

    def select_f(self):
        # 获取表格文件地址
        self.ok_path_button.setEnabled(True)
        f_name, _ = QFileDialog.getOpenFileName(self, "选取文件", self.cwd,  # 起始路径
                                                "All Files (*);;Text Files (*.txt)")  # 设置文件扩展名过滤,用双分号间隔
        self.path_edit.setText(f_name)
        #print(f_name)

    def ok_select_f(self):
        path = self.path_edit.text()
        self.open_th = OpenExcel(path)

        # 启动线程用来加载文件
        self.open_th.start()  # 创建线程
        self.open_th.SignalExcel_data.connect(self.get_exec_data)  # 链接信号到函数 用函数接受来自线程的信号
        self.open_th.exit()

    def get_exec_data(self, msg):
        # 获取子线程读取的表格数据
        #print("getExecData  msg=", msg)
        self.rd = msg
        self.page_all.setText(str(self.rd.max_r))
        self.all_page = self.rd.max_r
        self.page_all.setText(str(self.all_page - 3))
        self.button_up.setEnabled(False)
        self.page_name.setText("1")
        self.n = 1
        self.change_page("rest")

class MyThread(QThread):
    def __init__(self, pa=None):
        super().__init__(pa)
        self.pa = pa

    def run(self):
        md = self.pa.lb_name.toPlainText()  # 获取标签上用户输入的内容。
        self.pa.new_name = md
        self.pa.size_name.setText(str(len(md)))  # 将获取的长度赋值给size_name
        pg = self.pa.page_name.text()  # 获取页码数
        self.pa.page_name.setText(str(pg))  # 把页码数设置到页码上

class GetTh(QThread):
    #print("go GetTh")
    signal = pyqtSignal(str)  # 定义信号
    signal_next = pyqtSignal(bool)
    signal_up = pyqtSignal(bool)
    signal_photo = pyqtSignal(dict)

    def __init__(self, img_url, n, all_page):
        super(QThread, self).__init__()
        self.photo = QPixmap()
        self.rd = ReadExec.ReadExec()  # 实例化
        self.img_url = img_url
        self.n = n
        self.all_page = all_page

    # 下载图片，下载完后显示图片
    def run(self):
        #print("go image")
        photo = QPixmap()
        # 获取图片链接和图片地址
        # 获取图片内容
        pho_msg = {"im": photo, "msg": ""}
        try:
            photo.loadFromData(requests.get(self.img_url,timeout=3).content)
            pho_msg = {"im": photo, "msg": "ok"}
            self.signal_photo.emit(pho_msg)  # 把图片发送到主框架并显示出来
        except Exception as e:
            #print(e)
            pho_msg["msg"] = "no"
            self.signal_photo.emit(pho_msg)
            self.exit()
        # 恢复按钮状态
        #print("self.n=", self.n)
        #print("self.all_page - 3=", self.all_page - 3)
        if 1 < self.n < self.all_page - 3:
            self.signal_next.emit(True)
            self.signal_up.emit(True)
        if self.n == self.all_page - 3:
            self.signal_next.emit(False)
            self.signal_up.emit(True)
        if self.n == 1:
            self.signal_next.emit(True)
            self.signal_up.emit(False)
        #print("ok image\n\n")
        self.exit()

# 用来打开表格
class OpenExcel(QThread):
    SignalExcel_data = pyqtSignal(ReadExec.ReadExec)
    RE = ReadExec.ReadExec()  # 实例化

    def __init__(self, path):
        super(OpenExcel, self).__init__()
        self.path = path

    def __del__(self):
        self.wait()

    def run(self):
        #print("使用多线程==", self.path)
        self.RE.open_excl(self.path)  # 执行函数获得一系列数据
        self.SignalExcel_data.emit(self.RE)  # 把实列传入主函数

def main():
    app = QApplication(sys.argv)
    _ = MyApp()
    sys.exit(app.exec())

if __name__ == '__main__':
    main()

工作工具/可视化修改亚马逊表格/main.py


# Copyright (c) 2021 Red Hat
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
from __future__ import absolute_import

import time

from oslo_log import log
import pytest
import testtools

import tobiko
from tobiko import config
from tobiko.openstack import keystone
from tobiko.openstack import neutron
from tobiko.openstack import stacks
from tobiko.shell import ip
from tobiko.shell import iperf3
from tobiko.shell import ping
from tobiko.shell import sh
from tobiko.shell import tcpdump

CONF = config.CONF
LOG = log.getLogger(__name__)

@keystone.skip_unless_has_keystone_credentials()
@neutron.skip_if_is_old_ovn()
class QoSNetworkTest(testtools.TestCase):
    """Tests QoS basic functionality"""

    #: Resources stacks with QoS Policy and QoS Rules and Advanced server
    network = tobiko.required_fixture(stacks.QosNetworkStackFixture)
    policy = tobiko.required_fixture(stacks.QosPolicyStackFixture)
    server = tobiko.required_fixture(stacks.QosServerStackFixture)

    @pytest.mark.flaky(reruns=3, reruns_delay=120)
    def test_ping_dscp(self):
        capture_file = sh.execute('mktemp', sudo=True).stdout.strip()
        interface = ip.get_network_main_route_device(
            self.server.floating_ip_address)

        # IPv4 tcpdump DSCP filters explanation:
        # ip[1] refers to the byte 1 (the TOS byte) of the IP header
        # 0xfc = 11111100 is the mask to get only DSCP value from the ToS
        # As DSCP mark is most significant 6 bits we do right shift (>>)
        # twice in order to divide by 4 and compare with the decimal value
        # See details at http://darenmatthews.com/blog/?p=1199
        dscp_mark = CONF.tobiko.neutron.dscp_mark
        capture_filter = (f"'(ip src {self.server.floating_ip_address} and "
                          f"(ip[1] & 0xfc) >> 2 == {dscp_mark})'")

        # start a capture
        process = tcpdump.start_capture(
            capture_file=capture_file,
            interface=interface,
            capture_filter=capture_filter,
            capture_timeout=60)
        time.sleep(1)
        # send a ping to the server
        ping.assert_reachable_hosts([self.server.floating_ip_address],)
        # stop tcpdump and get the pcap capture
        pcap = tcpdump.get_pcap(process, capture_file=capture_file)
        # check the capture is not empty
        tcpdump.assert_pcap_is_not_empty(pcap=pcap)

    def test_network_qos_policy_id(self):
        """Verify network policy ID"""
        self.assertEqual(self.policy.qos_policy_id,
                         self.network.qos_policy_id)

    def test_server_qos_policy_id(self):
        """Verify server policy ID"""
        self.assertIsNone(self.server.port_details['qos_policy_id'])

    @pytest.mark.flaky(reruns=3, reruns_delay=120)
    def test_qos_bw_limit(self):
        """Verify BW limit using the iperf3 tool"""
        self.server.wait_for_iperf3_server()
        # localhost will act as client
        bandwidth_limit = self.policy.bwlimit_kbps * 1000.
        for attempt in tobiko.retry(timeout=100., interval=5.):
            try:
                iperf3.assert_has_bandwith_limits(
                    address=self.server.ip_address,
                    min_bandwith=bandwidth_limit * 0.9,
                    max_bandwith=bandwidth_limit * 1.1,
                    port=self.server.iperf3_port,
                    download=True)
                break
            except sh.ShellCommandFailed as err:
                if ('unable to connect to server: Connection refused'
                        in str(err)):
                    attempt.check_limits()
                    LOG.debug('iperf command failed because the iperf server '
                              'was not ready yet - retrying...')
                else:
                    raise err


# Copyright (C) 2020 Jørgen S. Dokken
#
# This file is part of DOLFINX_MPC
#
# SPDX-License-Identifier:    LGPL-3.0-or-later

import typing
import numba
from petsc4py import PETSc
import types
from dolfinx import function, fem, MPI
from .assemble_matrix import in_numpy_array, add_diagonal
import numpy

def backsubstitution(mpc, vector, dofmap):

    # Unravel data from MPC
    slave_cells = mpc.slave_cells()
    coefficients = mpc.coefficients()
    masters = mpc.masters_local()
    slave_cell_to_dofs = mpc.slave_cell_to_dofs()
    cell_to_slave = slave_cell_to_dofs.array()
    cell_to_slave_offset = slave_cell_to_dofs.offsets()
    slaves = mpc.slaves()
    masters_local = masters.array()
    offsets = masters.offsets()
    mpc_wrapper = (slaves, slave_cells, cell_to_slave, cell_to_slave_offset,
                   masters_local, coefficients, offsets)
    num_dofs_per_element = dofmap.dof_layout.num_dofs
    index_map = mpc.index_map()
    global_indices = index_map.indices(True)
    backsubstitution_numba(vector, dofmap.list.array(),
                           num_dofs_per_element, mpc_wrapper, global_indices)
    vector.ghostUpdate(addv=PETSc.InsertMode.INSERT,
                       mode=PETSc.ScatterMode.FORWARD)
    return vector

@numba.njit(cache=True)
def backsubstitution_numba(b, dofmap, num_dofs_per_element, mpc,
                           global_indices):
    """
    Insert mpc values into vector bc
    """
    (slaves, slave_cells, cell_to_slave, cell_to_slave_offset,
     masters_local, coefficients, offsets) = mpc
    slaves_visited = numpy.empty(0, dtype=numpy.float64)

    # Loop through slave cells
    for (index, cell_index) in enumerate(slave_cells):
        cell_slaves = cell_to_slave[cell_to_slave_offset[index]:
                                    cell_to_slave_offset[index+1]]
        local_dofs = dofmap[num_dofs_per_element * cell_index:
                            num_dofs_per_element * cell_index
                            + num_dofs_per_element]

        # Find the global index of the slaves on the cell in the slaves-array
        global_slaves_index = []
        for gi in range(len(slaves)):
            if in_numpy_array(cell_slaves, slaves[gi]):
                global_slaves_index.append(gi)

        for slave_index in global_slaves_index:
            slave = slaves[slave_index]
            k = -1
            # Find local position of slave dof
            for local_dof in local_dofs:
                if global_indices[local_dof] == slave:
                    k = local_dof
            assert k != -1
            # Check if we have already inserted for this slave
            if not in_numpy_array(slaves_visited, slave):
                slaves_visited = numpy.append(slaves_visited, slave)
                slaves_masters = masters_local[offsets[slave_index]:
                                               offsets[slave_index+1]]
                slaves_coeffs = coefficients[offsets[slave_index]:
                                             offsets[slave_index+1]]
                for (master, coeff) in zip(slaves_masters, slaves_coeffs):
                    b[k] += coeff*b[master]

def slave_master_structure(V: function.FunctionSpace, slave_master_dict:
                           typing.Dict[types.FunctionType,
                                       typing.Dict[
                                           types.FunctionType, float]],
                           subspace_slave=None,
                           subspace_master=None):
    """
    Returns the data structures required to build a multi-point constraint.
    Given a nested dictionary, where the first keys are functions for
    geometrically locating the slave degrees of freedom. The values of these
    keys are another dictionary, containing functions for geometrically
    locating the master degree of freedom. The value of the nested dictionary
    is the coefficient the master degree of freedom should be multiplied with
    in the multi point constraint.
    Example:
       If u0 = alpha u1 + beta u2, u3 = beta u4 + gamma u5
       slave_master_dict = {lambda x loc_u0:{lambda x loc_u1: alpha,
                                             lambda x loc_u2: beta},
                            lambda x loc_u3:{lambda x loc_u4: beta,
                                             lambda x loc_u5: gamma}}
    """
    slaves = []
    masters = []
    coeffs = []
    offsets = []
    local_min = (V.dofmap.index_map.local_range[0]
                 * V.dofmap.index_map.block_size)
    if subspace_slave is not None:
        Vsub_slave = V.sub(subspace_slave).collapse()
    if subspace_master is not None:
        Vsub_master = V.sub(subspace_master).collapse()
    for slave in slave_master_dict.keys():
        offsets.append(len(masters))
        if subspace_slave is None:
            dof = fem.locate_dofs_geometrical(V, slave) + local_min
            dof_global = numpy.vstack(MPI.comm_world.allgather(dof))[0]

        else:
            dof = fem.locate_dofs_geometrical((V.sub(subspace_slave),
                                               Vsub_slave),
                                              slave)
            for (i, d) in enumerate(dof):
                dof[i] += local_min
            dof_global = numpy.vstack(MPI.comm_world.allgather(dof))[0, 0]
        slaves.append(dof_global)
        for master in slave_master_dict[slave].keys():
            if subspace_master is None:
                dof_m = fem.locate_dofs_geometrical(V, master) + local_min
                dof_m = numpy.vstack(MPI.comm_world.allgather(dof_m))[0]
            else:
                dof_m = fem.locate_dofs_geometrical((V.sub(subspace_master),
                                                     Vsub_master),
                                                    master)
                for (i, d) in enumerate(dof_m):
                    dof_m[i] += local_min
                dof_m = numpy.vstack(MPI.comm_world.allgather(dof_m))[0, 0]

            masters.append(dof_m)
            coeffs.append(slave_master_dict[slave][master])
    offsets.append(len(masters))
    return (numpy.array(slaves), numpy.array(masters),
            numpy.array(coeffs, dtype=numpy.float64), numpy.array(offsets))

def dof_close_to(x, point):
    """
    Convenience function for locating a dof close to a point use numpy
    and lambda functions.
    """
    if point is None:
        raise ValueError("Point must be supplied")
    if len(point) == 1:
        return numpy.isclose(x[0], point[0])
    elif len(point) == 2:
        return numpy.logical_and(numpy.isclose(x[0], point[0]),
                                 numpy.isclose(x[1], point[1]))
    elif len(point) == 3:
        return numpy.logical_and(
            numpy.logical_and(numpy.isclose(x[0], point[0]),
                              numpy.isclose(x[1], point[1])),
            numpy.isclose(x[2], point[2]))
    else:
        return ValueError("Point has to be 1D, 2D or 3D")

def facet_normal_approximation(V, mt, mt_id):
    import dolfinx
    import ufl
    n = dolfinx.FacetNormal(V.mesh)
    nh = dolfinx.Function(V)
    u, v = ufl.TrialFunction(V), ufl.TestFunction(V)
    a = (dolfinx.Constant(V.mesh, 0)*ufl.inner(u, v)*ufl.dx
         + ufl.inner(u, v)*ufl.ds)
    ds = ufl.ds(domain=V.mesh, subdomain_data=mt, subdomain_id=mt_id)
    L = ufl.inner(n, v)*ds

    A = dolfinx.fem.assemble_matrix(a)
    ident_zeros(A)
    A.assemble()
    b = dolfinx.fem.assemble_vector(L)
    ksp = PETSc.KSP().create(V.mesh.mpi_comm())
    ksp.setOperators(A)
    ksp.setType("preonly")
    ksp.getPC().setType("lu")
    ksp.getPC().setFactorSolverType("mumps")
    ksp.solve(b, nh.vector)

    return nh

def ident_zeros(A):
    """
    Find all rows in a matrix that is zero, and add a 1 on the diagonal
    """
    assert A.size[0] == A.size[1]
    A.assemble()
    o_range = A.getOwnershipRange()
    rows = []
    for i in range(o_range[1]-o_range[0]):
        indices, values = A.getRow(o_range[0]+i)
        absrow = sum(abs(values))
        if absrow < 1e-6:
            rows.append(o_range[0] + i)
    add_diagonal(A.handle, numpy.array(rows))


# coding: utf-8
from pyquery import PyQuery as q
from collections import OrderedDict
from tools import *
from logging import warn

PROPERTIES_REFERENCE = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-product-property-reference.html'

def property_name_from_href(href):
    href = str(href)
    href, _ = href.rsplit(".", 1)
    _, href = href.rsplit("/", 1)
    return href

def property_ref_from_href(href):
    return {
        '$ref':
        '#/definitions/property_types/%s' % property_name_from_href(href)
    }

def get_type(dd_):
    dd = dd_('p').filter(lambda x: q(this).text().startswith('Type'))
    t = dd.text().lower()
    if 'type : string' in t:
        return {'type': 'string'}
    if 'list of strings' in t:
        return {'type': 'array', 'items': {'type': 'string'}}
    if 'type : integer' in t:
        return {'type': 'integer'}
    if 'type : boolean' in t:
        return {'type': 'boolean'}
    if dd('a'):
        return property_ref_from_href(dd('a').attr('href'))
    if dd_('.type') and len(dd_('.type')):
        if (dd_('.type').text() == 'AWS::EC2::SecurityGroup' and
                'list of' in t):
            return {'type': 'array', 'items': {'type': 'string'}}

    warn('Could not parse resource property type: "%s"', dd_.html())
    return {}

all_properties = all_resource_properties_hrefs()

def set_resource_properties(res_type):
    all = all_resource_hrefs()
    h = get_pq(all[res_type])
    schema = load()
    dl = h('#divContent .variablelist dl').eq(0)
    resources = resources_dict(schema)
    pairs = zip(dl('dt'), dl('dd'))
    pairs = [(q(dt), q(dd)) for dt, dd in pairs]
    shortcut = resources[res_type]['properties']
    shortcut['Properties'] = OrderedDict()
    shortcut['Properties']['properties'] = OrderedDict(
        (dt.text(), get_type(dd))
        for dt, dd in pairs
    )
    required = [
        k.text()
        for k, v
        in pairs
        if v('p').filter(lambda i: 'Required : Yes' in q(this).text())
    ]
    if required:
        shortcut['Properties']['required'] = required
        resources[res_type]['required'] = ['Properties']
    return schema

def all_properties():
    h = get_pq(PROPERTIES_REFERENCE)
    res = OrderedDict(
        (
            property_name_from_href(q(a).attr("href")),
            {
                "title": " ".join(a.text.split()),
                "description": q(a).attr("href")
            }
        ) for a in h('#divContent li a'))
    return res


import socket
import random
import string

from Crypto.Cipher import AES

# Porneste discutia cu generatorul pentru a lua cheia si vectorul de initializare

def generator_discussion():
    s = socket.socket()
    host = socket.gethostname()
    port = 111

    s.connect((host, port))
    K = s.recv(1024)
    iv = s.recv(1024)
    s.close()

    return K, iv

# Discuta cu nodurile A si B pentru a le oferi cheile criptate pentru
# criptarea/decriptare mesajului (in functie de modul de operare dorit)

def A_B_discussion():
    s = socket.socket()
    host = socket.gethostname()
    port = 112

    s.bind((host, port))

    s.listen()
    if True:
        c, adr = s.accept()
        print("Got conection with node A")
        mesaj = c.recv(1024)
        if mesaj == b"ecb":
            print("\nTrimitem cheia criptata k1 lui A:  ", ciphertext_k1)
            c.send(ciphertext_k1)
        else:
            print("\nTrimitem cheia criptata k2 lui A:  ", ciphertext_k2)
            c.send(ciphertext_k2)
        c.close()

    if True:
        c, adr = s.accept()
        print("Got conection with node B")
        mesaj = c.recv(1024)
        if mesaj == b"ecb":
            print("\nTrimitem cheia criptata k1 lui B:  ", ciphertext_k1)
            c.send(ciphertext_k1)
        else:
            print("\nTrimitem cheia criptata k1 lui B:  ", ciphertext_k1)
            c.send(ciphertext_k2)
        c.close()

    s.close()

# Realizeaza cheile k1(modul ECB) si k2(modul CFB) random

def random_key():
    k1 = str.encode(
        ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=key_length // 8)))
    k2 = str.encode(
        ''.join(random.choices(string.ascii_letters + string.digits + string.punctuation, k=key_length // 8)))
    return k1, k2

# Cripteaza cheile k1 si k2 pentru a le trimite mai departe nodurilor A si B
# (atunci cand sunt cerute)

def encrypt_k1(k):
    key_encrypt = AES.new(K, AES.MODE_ECB)
    ciphertext = key_encrypt.encrypt(k)
    return ciphertext

def encrypt_k2(k):
    iv_encrypt = AES.new(K, AES.MODE_ECB)
    cipher = iv_encrypt.encrypt(iv)
    ciphertext = bytes([_a ^ _b for _a, _b in zip(cipher, k)])
    return ciphertext

key_length = 128

K, iv = generator_discussion()
print("Am preluat valorile generate:  ", K, iv)

k1, k2 = random_key()
print("\nGeneram cheile k1 si k2:  ", k1 , k2)

ciphertext_k1 = encrypt_k1(k1)
ciphertext_k2 = encrypt_k2(k2)

A_B_discussion()


import RBHD
import requests
import pandas as pd
from datetime import datetime
from datetime import timezone
import time

count1 = time.time()

data = RBHD.rbhd_login_and_gather()

orders = data['orders'].sort_values('Date',ascending=False).reset_index().drop('index',axis = 1)

modify_order = pd.read_csv('order_modify.csv')
modify_order['Date'] = modify_order['Date'].astype('datetime64[ns]')

orders = pd.concat([orders,modify_order]).sort_values('Date',ascending=False)

orders.to_csv("orders_cache.csv")
dividends = data['dividends']
dividends.to_csv("dividends_cache.csv")

import Quote_Gatherer

all_quote = Quote_Gatherer.return_all_quotes(orders)
price_table = Quote_Gatherer.generate_price_table(all_quote)

all_quote.to_csv('all_quote_cache.csv')
price_table.to_csv('price_table_cache.csv')

#the following section add in "underlying","split","dividend", and "option expire" items to the orders

def get_underlying(instrument):
    if instrument['Instrument_Type'] == "Option":
        return instrument["Ticker"][:-15]
    else:
        return instrument["Ticker"]

orders['Underlying'] = orders.apply(lambda instrument: get_underlying(instrument),axis = 1)

day1list = pd.DataFrame(orders.groupby('Underlying').min()['Date'])
for i in day1list.index:
    ticker_i = i
    date1 = day1list.loc[i,"Date"].strftime("%Y-%m-%d")
    date2 = datetime.today().strftime("%Y-%m-%d")
    token = open('finnhub_token.txt','r').read()
    r = requests.get('https://finnhub.io/api/v1/stock/split?symbol=' + ticker_i + '&from=' + date1 + "&to=+" + date2 + '&token=' + token)

    for j in r.json():
        orders = orders.append(pd.Series({"Ticker":ticker_i,
            "Date":j['date'],
            "Price/Share":0.0,
            "Share":0.0,
            "Transaction_Type":"SPLIT",
            "Transaction_Amount":0.0,
            "Split_Factor":j['toFactor']/j['fromFactor'],
            "Instrument_Type":"Equity",
            "Underlying":ticker_i
        }),ignore_index=True)
orders['Date'] = orders['Date'].astype('datetime64[ns]')
orders = orders.sort_values("Date").reset_index().drop("index",axis=1)

dividends['amount'] = dividends['amount'].astype('float')
dividends = dividends[dividends['state']=="paid"]
for i in dividends.index:
    div = dividends.loc[i,:]
    orders = orders.append(pd.Series({"Ticker":div['Ticker'],
        "Date":div['paid_at'],
        "Price/Share":0.0,
        "Share":0.0,
        "Transaction_Type":"DIVIDEND",
        "Transaction_Amount":div['amount'],
        "Instrument_Type":"Equity",
        "Underlying":div['Ticker']
    }),ignore_index=True)

today_date = datetime.today()
day1list_option = pd.DataFrame(orders[orders['Instrument_Type']=="Option"].groupby('Ticker').min()['Date'])
for i in day1list_option.index:
    expire_date = datetime.strptime((i[-15:-13] + i[-13:-11] + i[-11:-9] +"1730" ), '%y%m%d%H%M')
    if today_date > expire_date:
        orders = orders.append(pd.Series({"Ticker":i,
            "Date":expire_date,
            "Price/Share":0.0,
            "Share":0.0,
            "Transaction_Type":"EXPIRE",
            "Transaction_Amount":0.0,
            "Instrument_Type":"Option",
            "Underlying":i[0:-15]
        }),ignore_index=True)

orders.sort_values("Date").to_csv("all_transactions.csv")

count2 = time.time()
print("All files have been cached. Spent: " + str(round(count2 - count1,2)) + "s.")


import math
import pandas as pd
import numpy as np
from tqdm import tqdm

def result2list(tmp_value):
    """
    辅助函数，将series按照索引返回的单个值或者pd.series转换成list
    :param tmp_value:
    :return:
    """
    if (type(tmp_value) != pd.core.series.Series):
        tmp_value = np.array([tmp_value])
    else:
        tmp_value = tmp_value.values
    return tmp_value

class Metric():

    def __init__(self,type,train_UM,test_UM,Recommender,N=3,K=3):
        self.type = type
        self.train_users = set(train_UM.index.values)
        self.test_users = set(test_UM.index.values)
        self.train_um = train_UM
        self.test_um = test_UM
        self.Recommender = Recommender
        self.N = N
        self.K = K
        self.recs = self.getRec()

    # 为test中的每个用户进行推荐
    def getRec(self):
        recs = {}
        for user in tqdm(self.test_users):
            rank = self.Recommender.pred(user,self.N,self.K)
            recs[user] = rank
        return recs

    # 定义精确率指标计算方式
    def precision(self):
        all, hit = 0, 0
        for user in self.test_users:
            test_items = set(result2list(self.test_um.loc[user]))
            rank = self.recs[user]
            for item in rank:
                if item in test_items:
                    hit += 1
            all += len(rank)
        return round(hit / all * 100, 2)

    # 定义召回率指标计算方式
    def recall(self):
        all, hit = 0, 0
        for user in self.test_users:
            test_items = set(result2list(self.test_um.loc[user]))
            rank = self.recs[user]
            for item in rank:
                if item in test_items:
                    hit += 1
            all += len(test_items)
        return round(hit / all * 100, 2)

    # 定义覆盖率指标计算方式
    def coverage(self):
        all_item, recom_item = set(), set()
        for user in self.test_users:
            for item in self.train_um.loc[user]:
                all_item.add(item)
            rank = self.recs[user]
            for item in rank:
                recom_item.add(item)
        return round(len(recom_item) / len(all_item) * 100, 2)

    # 定义新颖度指标计算方式
    def popularity(self):
        # 计算物品的流行度
        item_pop = {}
        for user in self.train_users:
            for item in self.train_um.loc[user]:
                if item not in item_pop:
                    item_pop[item] = 0
                item_pop[item] += 1

        num, pop = 0, 0
        for user in self.test_users:
            rank = self.recs[user]
            for item in rank:
                # 取对数，防止因长尾问题带来的被流行物品所主导
                pop += math.log(1 + item_pop[item])
                num += 1
        return round(pop / num, 6)

    def eval(self):
        metric = {'Precision': self.precision(),
                  'Recall': self.recall(),
                  'Coverage': self.coverage(),
                  'Popularity': self.popularity()}
        print('Metric:', metric)
        return metric


import os
import click
import inspect
from itertools import chain
from nornir_cli.common_commands import _doc_generator
from nornir_cli import __version__

CMD_FOLDERS = ["common_commands", "custom_commands"]

PACKAGE_NAME = "nornir_cli"

SETTINGS = {
    "nornir_scrapli": {
        "NetconfScrape": "scrapli_netconf.driver",
        "NetworkDriver": "scrapli.driver",
        "GenericDriver": "scrapli.driver",
    },
    "nornir_netmiko": {
        "BaseConnection": "netmiko",
        "file_transfer": "netmiko",
    },
}

PARAMETER_TYPES = {
    str: click.STRING,
    type(None): click.STRING,
    int: click.INT,
    float: click.FLOAT,
    bool: click.BOOL,
}

# options callback
# def callback(ctx, param, value):
#     if value != param.get_default(ctx):
#         ctx.obj["kwargs"].update(dict([[param.name, _json_loads([value])[0]]]))
#         ctx.obj["parameters"].append({ctx.obj["original"]: ctx.obj["kwargs"]})
#         ctx.obj["generator"] = (
#             dict(list(item.items())) for item in ctx.obj["parameters"]
#         )

# get original function from original modules (check SETTINGS)
def get_sources(plugin, l):
    try:
        for key, value in SETTINGS[plugin].items():
            m = lambda: None
            m.sub = __import__(value, fromlist=[key])
            o = getattr(m.sub, key, None)
            source_f = [f for f in o.__dict__.keys() if not f.startswith("_")]
            command = sorted(
                [i for i in source_f if l[l.find("_") + 1 : :] in i], key=len
            )
            if command:
                return getattr(o, command[0], None)
            elif inspect.isfunction(o):
                return o
    except KeyError:
        return

def _get_cmd_folder(cmd_folders):
    for cmd_folder in cmd_folders:
        yield os.path.abspath(os.path.join(os.path.dirname(__file__), cmd_folder))

def _get_cli(path, cmd_name, cmd_folder):
    if f"cmd_{cmd_name}.py" in os.listdir(cmd_folder):
        obj = __import__(f"{path}.cmd_{cmd_name}", None, None, ["cli"])
        return obj.cli

# mini factory for the production of classes for our plugins
# https://click.palletsprojects.com/en/7.x/commands/?highlight=multi%20command#custom-multi-commands
def class_factory(name, plugin, BaseClass=click.Group):
    def list_commands(self, ctx):
        ctx.obj[plugin] = __import__(plugin, fromlist=["tasks"])
        return [
            filename[4:-3]
            for filename in os.listdir(next(_get_cmd_folder(["common_commands"])))
            if filename.endswith(".py") and filename.startswith("cmd_")
        ] + list(ctx.obj[plugin].tasks.__all__)

    def list_custom_commands(self, ctx):
        cmd_folders = _get_cmd_folder(CMD_FOLDERS)
        return [
            filename[4:-3]
            for filename in chain(*map(os.listdir, cmd_folders))
            if filename.endswith(".py") and filename.startswith("cmd_")
        ]

    def get_command(self, ctx, cmd_name):
        ctx.obj["kwargs"] = {}
        try:
            # init, filter, show_inventory, etc
            command = _get_cli(
                f"{PACKAGE_NAME}.common_commands",
                cmd_name,
                next(_get_cmd_folder(["common_commands"])),
            )
            if command:
                return command

            # nornir-plugin commands
            plugin_command = _get_cli(
                f"{PACKAGE_NAME}.plugin_commands",
                "common",
                next(_get_cmd_folder(["plugin_commands"])),
            )

            ctx.obj[plugin] = __import__(plugin, fromlist=["tasks"])
            ctx.obj["original"] = getattr(ctx.obj[plugin].tasks, cmd_name, None)

            # decorate the command and cover it with click.Options
            return decorator(plugin, ctx)(plugin_command)
        except (ImportError, AttributeError):
            return

    def get_custom_command(self, ctx, cmd_name):
        try:
            for abs_path, rel_path in zip(_get_cmd_folder(CMD_FOLDERS), CMD_FOLDERS):
                command = _get_cli(f"{PACKAGE_NAME}.{rel_path}", cmd_name, abs_path)
                if command:
                    return command
        except (ImportError, AttributeError):
            return

    newclass = type(
        name,
        (BaseClass,),
        {
            "list_commands": list_commands if plugin else list_custom_commands,
            "get_command": get_command if plugin else get_custom_command,
        },
    )
    return newclass

# dynamically create a class for plugin/group and inherit it
def dec(param=None):
    def wrapper(f):
        return init_nornir_cli.group(cls=scls, chain=True)(f)

    scls = class_factory("LazyClass", param)
    return wrapper

#
def decorator(plugin, ctx):
    def wrapper(f):
        # methods with a large and complex __doc__ :(
        method_exceptions = ("send_interactive",)

        short_help = obj_or.__doc__.split("\n")[1].strip(", ., :")

        f.__doc__ = "\n".join(list(_doc_generator(obj_or.__doc__)))

        if obj_or.__name__ in method_exceptions:
            f.__doc__ = f"{short_help}\n" + "\n".join(
                list(
                    _doc_generator(obj_or.__doc__[obj_or.__doc__.find("    Args:") : :])
                )
            )

        cmd = click.command(name=obj_or.__name__, short_help=short_help)(f)

        click.option(
            "--pg_bar",
            is_flag=True,
            show_default=True,
        )(cmd)
        click.option(
            "--show_result/--no_show_result",
            default=True,
            show_default=True,
        )(cmd)

        # get original function from the main module
        original_function = get_sources(plugin, obj_or.__name__)
        sig0 = inspect.signature(obj_or)
        p = dict(sig0.parameters)
        if original_function:
            sig = inspect.signature(original_function)
            k = dict(sig.parameters)
            p.update(k)
        all_dict = {
            key: value
            for key, value in p.items()
            if key not in ["self", "task", "args", "kwargs"]
        }
        # dynamically generate options
        for k, v in all_dict.items():
            default_value = str(v.default) if not isinstance(v.default, type) else None
            click.option(
                "--" + k,
                default=default_value,
                show_default=True,
                required=False if default_value else True,
                # expose_value=False,
                # callback=callback,
                # is_eager=True,
                type=PARAMETER_TYPES.setdefault(type(v.default), click.STRING),
            )(cmd)
            # last original functions with arguments
            ctx.obj["queue_parameters"][obj_or].update({k: v.default})
        # list of dictionaries with original function (key) and set of arguments (value)
        ctx.obj["queue_functions"].append(ctx.obj["queue_parameters"])
        # ctx.obj["queue_functions"] in the form of a generator expression
        ctx.obj["queue_functions_generator"] = (
            func_param for func_param in ctx.obj["queue_functions"]
        )
        return cmd

    # get original function from Nornir plugin
    obj_or = ctx.obj["original"]

    ctx.obj["queue_parameters"] = {}

    ctx.obj["queue_parameters"][obj_or] = {}

    return wrapper

@click.group()
@click.version_option(version=__version__)
@click.pass_context
def init_nornir_cli(ctx):
    """
    Nornir CLI

    Orchestrate your Inventory and start Tasks and Runbooks
    """

    ctx.ensure_object(dict)
    # list of dictionaries with original function (key) and set of arguments (value)
    ctx.obj["queue_functions"] = []
    # last original functions with arguments
    ctx.obj["queue_parameters"] = {}

@dec("nornir_netmiko")
def nornir_netmiko():
    """
    nornir_netmiko plugin
    """
    pass

@dec("nornir_scrapli")
def nornir_scrapli():
    """
    nornir_scrapli plugin
    """
    pass

@dec("nornir_napalm.plugins")
def nornir_napalm():
    """
    nornir_napalm plugin
    """
    pass

@dec()
def custom():
    """
    custom nornir runbooks
    """
    pass