import pandas as pd
from pymystem3 import Mystem
import nltk
import string
import pymorphy2
import logging
import pymongo
import pickle
mystem = Mystem()
punctuation = string.punctuation
morph = pymorphy2.MorphAnalyzer()
category_pattern_dict = dict()
INF = 100*100*100

def get_patterns_from_file(file, db):
    logging.info('Extracting initial patterns from file')
    file = pd.read_excel(file)
    for index, row in file.iterrows():
        if db['patterns'].find({'_id':int(row['id'])}).count() != 0:
            continue
        pattern = dict()
        pattern['_id'] = int(row['id'])
        pattern['string'] = row['pattern']

        arg1, arg2 = dict(), dict()
        arg1['case'] = row['arg1_case'].lower()
        arg1['num'] = row['arg1_num'].lower()
        arg1['pos'] = row['arg1_pos'].lower()
        arg2['case'] = row['arg2_case'].lower()
        arg2['num'] = row['arg2_num'].lower()
        arg2['pos'] = row['arg2_pos'].lower()

        pattern['arg1'] = arg1
        pattern['arg2'] = arg2

        pattern['presicion'] = INF
        pattern['true_detective'] = 0
        pattern['false_detective'] = 0
        # -1 in extracted category id in case when it's our initial pattern
        pattern['extracted_category_id'] = -1
        pattern['used'] = True
        pattern['coocurence_count'] = INF
        pattern['sentences_id'] = list()

        #FIXME think about this features more deeply later
        pattern['iteration_added'] = list()
        pattern['iteration_deleted'] = list()

        db['patterns'].insert_one(pattern)

def get_ontology_from_file(file, db):
    print ('Extracting initial ontology from file')
    file = pd.read_excel(file)
    for index, row in file.iterrows():
        ontology_category = dict()
        category_name = mystem.lemmatize(row['categoryName'])[0]

        if db['ontology'].find({'category_name': category_name}).count() != 0:
            continue
        ontology_category['category_name'] = category_name
        ontology_category['_id'] = db['ontology'].find().count() + 1
        if type(row['seedInstances']) is float:
            ontology_category['instances'] = list()
        else:
            ontology_category['instances'] = row['seedInstances'].split('"')[1::2]

        if type(row['seedExtractionPatterns']) is float:
            ontology_category['extraction_patterns'] = list()
        else:
            ontology_category['extraction_patterns'] = [int(s) for s in row['seedExtractionPatterns'].split(' ') if s.isdigit()]

        ontology_category['promoted_patterns'] = list()
        ontology_category['max_instance_precision'] = 0.0
        ontology_category['max_pattern_precision'] = 0.0

        for instance in ontology_category['instances']:
            promoted_instance = dict()
            promoted_instance['lexem'] = instance
            promoted_instance['_id'] = db['promoted_instances'].find().count() + 1
            promoted_instance['category_name'] = category_name
            promoted_instance['used'] = True
            promoted_instance['precision'] = 1.0
            promoted_instance['extracted_pattern_id'] = -1
            promoted_instance['iteration_added'] = [0]
            promoted_instance['iteration_deleted'] = list()
            # this instances would have the highest precision because was added by default
            promoted_instance['count_in_text'] = INF
            db['promoted_instances'].insert_one(promoted_instance)

        db['ontology'].insert_one(ontology_category)

def process_sentences_from_file(file, db):
    text = open(file, 'r').read()
    sentences = nltk.sent_tokenize(text)
    for s in sentences:
        sentence = dict()
        sentence['_id'] = db['sentences'].find().count() + 1
        sentence['string'] = s
        sentence['words'] = list()

        words = nltk.word_tokenize(s)
        for word in words:
            word_dict = dict()
            word_dict['original'] = word
            if word in punctuation:
                word_dict['punctuation'] = True
                word_dict['lexem'] = word
                sentence['words'].append(word_dict)
                continue

            p = morph.parse(word)
            word_dict['pos'] = p[0].tag.POS
            word_dict['case'] = p[0].tag.case
            word_dict['lexem'] = p[0].normal_form
            word_dict['number'] = p[0].tag.number
            word_dict['punctuation'] = False
            sentence['words'].append(word_dict)

        db['sentences'].insert_one(sentence)
    return

def build_category_index(db):
    # build indexes on existing ontology in existing database sentences and ontology collections
    print ('Building indexes')
    categories = db['ontology'].find()

    for category in categories:
        index = dict()
        index['sentences_id'] = list()
        sentences = db['sentences'].find()
        for sentence in sentences:
            for word in sentence['words']:
                if word['lexem'] == category['category_name']:
                    index['sentences_id'].append(sentence['_id'])
        index['sentences_id'] = list(set(index['sentences_id']))
        index['_id'] = db['indexes'].find().count() + 1
        index['category_name'] = category['category_name']
        db['indexes'].insert_one(index)
        print('Indexes for category %s are builded' % category['category_name'])
    return

def build_pattern_index(db):
    # build index for initial patterns
    print ('Building indxes for patterns')
    sentences = db.sentences.find()
    for sentence in sentences:
        patterns = db.patterns.find()
        for pattern in patterns:
            pattern_words_list = nltk.word_tokenize(pattern['string'])
            arg1_pos, arg2_pos = check_if_pattern_exists_in_sentence(sentence, pattern_words_list)
            if arg1_pos is not None and arg2_pos is not None:
                try:
                    # sometimes it can be out of range error
                    arg1 = sentence['words'][arg1_pos]
                    arg2 = sentence['words'][arg2_pos]
                except:
                    continue
                try:
                    sentences_id = pattern['sentences_id']
                except:
                    sentences_id = list()
                sentences_id.append(sentence['_id'])
                db.patterns.update({'_id': pattern['_id']},
                                    {'$set': {'sentences_id': sentences_id}})

def extract_instances(db, iteration):
    # iterate throw sentences, that contains categories
    # try to find patterns in this sentences
    logging.info("Begin instances extracting")
    category_pattern_dict.clear()
    categories = db['indexes'].find()
    for category in categories:
        for sentence_id in category['sentences_id']:
            sentence = db['sentences'].find_one({'_id': sentence_id})
            patterns = db['patterns'].find({'used': True})
            for pattern in patterns:
                if sentence_id not in pattern['sentences_id']:
                    continue
                if not (pattern['extracted_category_id'] == -1 or pattern['extracted_category_id'] == category['_id']):
                    continue
                pattern_words_list = nltk.word_tokenize(pattern['string'])
                if ')' in pattern_words_list:
                    pattern_words_list.remove(')')
                arg1_pos, arg2_pos = check_if_pattern_exists_in_sentence(sentence, pattern_words_list)
                if arg1_pos is not None and arg2_pos is not None:
                    try:
                        # sometimes it can be out of range error
                        arg1 = sentence['words'][arg1_pos]
                        arg2 = sentence['words'][arg2_pos]
                    except:
                        continue

                    if arg1['lexem'] == category['category_name'] or \
                        arg2['lexem'] == category['category_name']:
                            if arg2['lexem'] == category['category_name']:
                                (arg1, arg2) = (arg2, arg1)
                    else:
                        continue

                    if check_words_for_pattern(arg1, arg2, pattern):
                        item = db['promoted_instances'].find({'category_name': category['category_name'],
                                                                'lexem': arg2['lexem']})
                        if item.count() > 0:
                            item = db['promoted_instances'].find_one({'category_name': category['category_name'],
                                                                      'lexem': arg2['lexem']})
                            count_in_text = item['count_in_text']
                            if count_in_text == 0 or count_in_text is None:
                                count_in_text = 1
                            else:
                                count_in_text += 1
                            db['promoted_instances'].update({'_id': item['_id']},
                                                            {'$set': {'count_in_text': count_in_text}})
                            logging.info('Found excisting instance [%s] for category [%s], with pattern [%s] and [%d] coocurences' % \
                                         (arg2['lexem'], category['category_name'], pattern['string'], count_in_text))

                        else:
                            promoted_instance = dict()
                            promoted_instance['_id'] = db['promoted_instances'].find().count() + 1
                            promoted_instance['lexem'] = arg2['lexem']
                            promoted_instance['category_name'] = category['category_name']
                            promoted_instance['used'] = False
                            promoted_instance['precision'] = 0
                            promoted_instance['extracted_pattern_id'] = pattern['_id']
                            promoted_instance['count_in_text'] = 1
                            promoted_instance['iteration_added'] = list()
                            promoted_instance['iteration_added'].append(iteration)
                            promoted_instance['iteration_deleted'] = list()

                            db['promoted_instances'].insert_one(promoted_instance)
                            logging.info("Found new promoted instance [%s] for category [%s], with pattern [%s]" % \
                                     (promoted_instance['lexem'], category['category_name'], pattern['string']))
                            break
    return

def evaluate_instances(db, iteration):
    logging.info('Begin instances evaluating')
    promoted_instances = db['promoted_instances'].find()
    text_ngrams_dictionary = load_dictionary('ngrams_dictionary_for_instances.pkl')

    for instance in promoted_instances:
        if instance['extracted_pattern_id'] != -1:
            if instance['count_in_text'] == 0:
                continue
            try:
                precision = instance['count_in_text'] / text_ngrams_dictionary[instance['lexem'].lower()]
            except:
                logging.error('Cannot find words %s in ngrams dictionary for instances' % instance['lexem'])
                precision = 0
            if precision > 1:
                precision = 1.0
            logging.info("Precision for promoted instance [%s] for category [%s] updated from [%s] to [%s]" % \
                     (instance['lexem'],
                     instance['category_name'],
                     str(instance['precision']),
                     str(precision)))
            db['promoted_instances'].update({'_id': instance['_id']},
                                        {'$set': {'precision': precision}})

    # for each category we want to have n = 0..20 (will select later) numbers of promoted instances
    # at each iteration we calculate first 20 by precision
    # all that will be out of 20 but was at list earlier will be deleted
    categories = db['ontology'].find()
    for category in categories:
        treshold = db['ontology'].find_one({'_id': category['_id']})['max_instance_precision']
        promoted_instances_for_category = db['promoted_instances'].find({
            'category_name': category['category_name']}).sort('precision', pymongo.DESCENDING)

        new_instances = 0
        deleted_instances = 0
        stayed_instances = 0
        for promoted_instance in promoted_instances_for_category:
            if promoted_instance['extracted_pattern_id'] == -1:
                stayed_instances += 1
                continue
            # first [n] NOT INITIAL instances must be added
            if promoted_instance['precision'] >= treshold:
                if promoted_instance['used']:
                    logging.info("Promoted instance [%s] stayed for category [%s] with precision [%s]" % \
                                 (promoted_instance['lexem'],
                                  promoted_instance['category_name'],
                                  str(promoted_instance['precision'])))
                    stayed_instances += 1
                else:
                    logging.info("Promoted instance [%s] added for category [%s] with precision [%s]" % \
                                 (promoted_instance['lexem'],
                                  promoted_instance['category_name'],
                                  str(promoted_instance['precision'])))
                    new_instances += 1
                    try:
                        iteration_added = promoted_instance['iteration_added']
                    except:
                        iteration_added = list()
                    iteration_added.append(iteration)
                    db['promoted_instances'].update({'_id': promoted_instance['_id']},
                                                {'$set': {'used': True,
                                                          'iteration_added': iteration_added}})
                if category['max_instance_precision'] == 0.0:
                    db['ontology'].update({'_id': category['_id']},
                                          {'$set': {'max_instance_precision': promoted_instance['precision']}})
                    logging.info('Updated category [%s] precision to [%.2f]' % (category['category_name'], promoted_instance['precision']))

            # other instances must be deleted if they are not in first [n]
            else:
                if promoted_instance['used']:
                    logging.info("Promoted instance [%s] deleted for category [%s] with precision [%s]" % \
                                 (promoted_instance['lexem'],
                                  promoted_instance['category_name'],
                                  str(promoted_instance['precision'])))
                    deleted_instances += 1
                    try:
                        iteration_deleted = promoted_instance['iteration_added']
                    except:
                        iteration_deleted = list()
                    iteration_deleted.append(iteration)
                    db['promoted_instances'].update({'_id': promoted_instance['_id']},
                                                    {'$set': {'used': False,
                                                              'iteration_deleted': iteration_deleted}})

        logging.info("Add [%s] new instances, delete [%s], stayed [%d] instances for category [%s]" % \
                     (str(new_instances), str(deleted_instances), stayed_instances, category['category_name']))

    return

def check_if_pattern_exists_in_sentence(sentence, pattern_words_list):
    # check if pattern is in the sentence and return arg1/arg2 positions
    # FIXME now look into only one-word arguments, need to extend
    pattern_words_list.remove('arg1')
    pattern_words_list.remove('arg2')
    arg1_pos, arg2_pos = None, None

    for i in range(0, (len(sentence['words']) - len(pattern_words_list)) + 1):
        flag = True
        for j in range(0, len(pattern_words_list)):
            if sentence['words'][i]['original'] != pattern_words_list[j]:
                flag = False
                break
            i += 1
        if not flag:
            continue

        arg1_pos = i - len(pattern_words_list) - 1
        arg2_pos = arg1_pos + len(pattern_words_list) + 1
        break

    return(arg1_pos, arg2_pos)

def check_words_for_pattern(arg1, arg2, pattern):
    # check if arguments parameters are the same as in pattern
    try:
        if arg1['case'] == pattern['arg1']['case'] and \
                (arg1['number'] == pattern['arg1']['num'] or pattern['arg1']['num'] == 'all') and \
                        arg1['pos'].lower() == pattern['arg1']['pos'].lower() and \
                        arg2['case'] == pattern['arg2']['case'] and \
                (arg2['number'] == pattern['arg2']['num'] or pattern['arg2']['num'] == 'all') and \
                        arg2['pos'].lower() == pattern['arg2']['pos'].lower():
            return True
    except:
        pass
    return False

def extract_patterns(db, iteration = 1):
    logging.info('Begin pattern extraction')
    categories = db['indexes'].find()
    for category in categories:
        for sentence_id in category['sentences_id']:
            instances = db['promoted_instances'].find({'category_name': category['category_name'],
                                                       'used': True})
            sentence = db['sentences'].find_one({'_id': sentence_id})
            for instance in instances:
                if check_word_in_sentence(sentence, instance['lexem']) != -1:
                    arg1_pos = check_word_in_sentence(sentence, category['category_name'])
                    arg2_pos = check_word_in_sentence(sentence, instance['lexem'])

                    if abs(arg1_pos - arg2_pos) >= 5:
                        # choose the patterns not more than 5 words im sum with arg1/arg2
                        continue

                    # just because we have different types of patterns we need to check this conditions
                    # to form the pattern string
                    if arg1_pos < arg2_pos:
                        pattern_string = 'arg1 '
                        for i in range(arg1_pos + 1, arg2_pos):
                            pattern_string += sentence['words'][i]['original']
                            pattern_string += ' '
                        pattern_string += 'arg2'
                    else:
                        pattern_string = 'arg2 '
                        for i in range(arg2_pos + 1, arg1_pos):
                            pattern_string += sentence['words'][i]['original']
                            pattern_string += ' '
                        pattern_string += 'arg1'

                    # FIXME add this case when finding instances
                    if '(' in pattern_string and ')' not in pattern_string:
                        pattern_string += ' )'

                    if pattern_string == 'arg1 arg2' or pattern_string == 'arg2 arg1':
                        continue

                    promoted_pattern = dict()
                    promoted_pattern['arg1'] = dict()
                    promoted_pattern['arg1']['num'] = sentence['words'][arg1_pos]['number']
                    promoted_pattern['arg1']['case'] = sentence['words'][arg1_pos]['case']
                    promoted_pattern['arg1']['pos'] = sentence['words'][arg1_pos]['pos']

                    promoted_pattern['arg2'] = dict()
                    promoted_pattern['arg2']['num'] = sentence['words'][arg2_pos]['number']
                    promoted_pattern['arg2']['case'] = sentence['words'][arg2_pos]['case']
                    promoted_pattern['arg2']['pos'] = sentence['words'][arg2_pos]['pos']

                    #TODO also need to check arg1/arg2 conditions
                    if db['patterns'].find({'string': pattern_string,
                                            'extracted_category_id': category['_id'],
                                            'arg1': promoted_pattern['arg1'],
                                            'arg2': promoted_pattern['arg2']}).count() > 0:

                        found_pattern = db['patterns'].find_one({'string': pattern_string,
                                                                    'extracted_category_id': category['_id'],
                                                                    'arg1': promoted_pattern['arg1'],
                                                                    'arg2': promoted_pattern['arg2']})
                        coocurence_count = found_pattern['coocurence_count']
                        coocurence_count += 1
                        sentences_id = found_pattern['sentences_id']
                        sentences_id.append(sentence_id)
                        db['patterns'].update({'_id': found_pattern['_id']},
                                                    {'$set': {'coocurence_count': coocurence_count,
                                                              'sentences_id': sentences_id}})

                        logging.info('Updating excisting pattern [%s] for category [%s] found for instance [%s] with [%d] coocurences' % \
                                     (found_pattern['string'], category['category_name'], instance['lexem'], found_pattern['coocurence_count']))

                    elif db['patterns'].find({'string': pattern_string,
                                            'extracted_category_id': -1}).count() > 0:
                        logging.info('Found initial pattern [%s], skipping' % pattern_string)
                        continue
                    else:
                        promoted_pattern['_id'] = db['patterns'].find().count() + 1
                        promoted_pattern['iteration_added'] = [iteration]
                        promoted_pattern['iteration_deleted'] = list()
                        promoted_pattern['used'] = False
                        promoted_pattern['extracted_category_id'] = category['_id']
                        promoted_pattern['coocurence_count'] = 1
                        promoted_pattern['string'] = pattern_string
                        promoted_pattern['precision'] = 0

                        #FIXME think about this metrics later
                        promoted_pattern['true_detective'] = 0
                        promoted_pattern['false_detective'] = 0

                        # TODO think about the situation, when the pattern found with different 'num' field in words,
                        # TODO but the same conditions for everything else

                        promoted_pattern['sentences_id'] = list()
                        promoted_pattern['sentences_id'].append(sentence_id)
                        db['patterns'].insert_one(promoted_pattern)
                        logging.info('Found new pattern [%s] for category [%s] found for instance [%s]' % \
                                     (promoted_pattern['string'], category['category_name'], instance['lexem']))
                        break
    return

def evaluate_patterns(db, iteration):
    logging.info('Begin patterns evaluation')
    patterns = db['patterns'].find()
    text_ngrams_dictionary = load_dictionary('ngrams_dictionary.pkl')
    for pattern in patterns:
        if pattern['extracted_category_id'] == -1:
            continue
        pattern_string = pattern['string']
        pattern_tokens= nltk.word_tokenize(pattern_string)
        pattern_tokens.remove('arg1')
        pattern_tokens.remove('arg2')
        if ')' in pattern_tokens:
            pattern_tokens.remove(')')
        pattern_string = ''
        for token in pattern_tokens:
            pattern_string += token.lower()
            pattern_string += ' '
        pattern_string = pattern_string[:-1]
        if pattern['coocurence_count'] == 0:
            continue
        try:
            precision = pattern['coocurence_count'] / text_ngrams_dictionary[pattern_string]
        except:
            logging.error('Cannot find words %s in ngrams_dict' % pattern_string)
            precision = 0
        if precision > 1:
            precision = 1.0
        db['patterns'].update({'_id': pattern['_id']},
                                    {'$set': {'precision': precision}})

    categories = db['ontology'].find()
    for category in categories:
        treshold = db['ontology'].find_one({'_id': category['_id']})['max_pattern_precision']
        promoted_patterns_for_category = db['patterns'].find({
            'extracted_category_id': category['_id']}).sort('precision', pymongo.DESCENDING)
        new_patterns, deleted_patterns, stayed_patterns = 0, 0, 0
        for promoted_pattern in promoted_patterns_for_category:
            if promoted_pattern['extracted_category_id'] == -1:
                continue
            if promoted_pattern['precision'] >= treshold:
                if promoted_pattern['used']:
                    logging.info("Promoted pattern [%s] stayed for category [%s] with precision [%s]" % \
                                 (promoted_pattern['string'],
                                  category['category_name'],
                                  str(promoted_pattern['precision'])))
                    stayed_patterns += 1
                else:
                    logging.info("Promoted pattern [%s] added for category [%s] with precision [%s]" % \
                                 (promoted_pattern['string'],
                                  category['category_name'],
                                  str(promoted_pattern['precision'])))
                    new_patterns += 1
                    try:
                        iteration_added = promoted_pattern['iteration_added']
                    except:
                        iteration_added = list()
                    iteration_added.append(iteration)
                    db['patterns'].update({'_id': promoted_pattern['_id']},
                                                    {'$set': {'used': True,
                                                              'iteration_added': iteration_added}})

                if category['max_pattern_precision'] == 0.0:
                    db['ontology'].update({'_id': category['_id']},
                                          {'$set': {'max_pattern_precision': promoted_pattern['precision']}})
                    logging.info('Updated category [%s] precision to [%.2f]' % \
                                 (category['category_name'], promoted_pattern['precision']))
            else:
                if promoted_pattern['used']:
                    logging.info("Promoted instance [%s] deleted for category [%s] with precision [%s]" % \
                                 (promoted_pattern['string'],
                                  category['category_name'],
                                  str(promoted_pattern['precision'])))
                    deleted_patterns += 1
                    try:
                        iteration_deleted = promoted_pattern['iteration_deleted']
                    except:
                        iteration_deleted = list()
                    db['patterns'].update({'_id': promoted_pattern['_id']},
                                                    {'$set': {'used': False,
                                                              'iteration_deleted': iteration_deleted}})
        logging.info("Add [%d] new patterns, delete [%d], stayed [%d] patterns for category [%s]" % \
                     (new_patterns, deleted_patterns, stayed_patterns, category['category_name']))
    return

def load_dictionary(file):
    with open(file, 'rb') as f:
        obj = pickle.load(f)
    return obj

def check_word_in_sentence(sentence, lexem):
    # help to find the word lexem in the sentence and return its position if it exists
    pos = 0
    for word in sentence['words']:
        if lexem == word['lexem']:
            return pos
        pos += 1
    return -1

def zero_coocurence_count(db):
    logging.info('Reser coocurence counts for instances/patterns')
    instances = db['promoted_instances'].find()
    patterns = db['patterns'].find()

    for instance in instances:
        db['promoted_instances'].update({'_id': instance['_id']},
                                    {'$set': {'count_in_text': 0}})

    for pattern in patterns:
        db['patterns'].update({'_id': pattern['_id']},
                                {'$set': {'coocurence_count': 0}})


import numpy as np
import matplotlib.pyplot as plt

a = np.array([0.313,0.365,0.423,0.363,0.439,0.525,0.423,0.525,0.651]).reshape(3,3)

#显示图片
plt.imshow(a,interpolation='nearest',cmap='bone',origin='upper')
plt.colorbar(shrink=0.9)

plt.xticks(())
plt.yticks(())
plt.show()


from collections import defaultdict
import Source.PygameGraphics as pygame

def draw():
    pygame.start_draw()
    for entity in images:
        entity.image.draw(entity)
    pygame.finish_draw()

def add(entity):
    images.append(entity)

images = []

odd_image = pygame.Image('../Data/Odd.png')


import math
import sys

import pygame

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)

class MazeRenderer:

    def __init__(self, window_size, cell_n, title):
        pygame.init()
        # Window Parameters
        self.cell_n = cell_n
        self.square_size = math.ceil(window_size / cell_n)
        self.window_size = cell_n*self.square_size
        # Create surface
        self.display_surf = pygame.display.set_mode((self.window_size, self.window_size), 0, 32)
        pygame.display.set_caption(title)
        # Cells Grid
        self.grid = self.createGrid(cell_n)

    def createGrid(self, cell_n):
        return [[pygame.Rect(i * self.square_size,
                             j * self.square_size,
                             self.square_size,
                             self.square_size) for j in range(cell_n)] for i in range(cell_n)]

    def render_maze(self, maze):
        # Background
        self.display_surf.fill(WHITE)
        # self.render_grid()
        # Fill Wall Cells
        for i in range(self.cell_n):
            for j in range(self.cell_n):
                if maze[i][j] == 'X':
                    pygame.draw.rect(self.display_surf, BLACK, self.grid[i][j], 0)  # WALLs
                elif maze[i][j] == 'A':
                    pygame.draw.rect(self.display_surf, YELLOW, self.grid[i][j], 0)  # START
                elif maze[i][j] == 'B':
                    pygame.draw.rect(self.display_surf, GREEN, self.grid[i][j], 0)  # FINISH

    def render_grid(self):
        # Render grid:
        for i in range(self.cell_n):
            for j in range(self.cell_n):
                pygame.draw.rect(self.display_surf, BLACK, self.grid[i][j], 1)  # GRID

    def render_solution(self, solution_lst):
        for pos in solution_lst[1:-1]:  # cut of start and end position
            pygame.draw.rect(self.display_surf, RED, self.grid[pos[0]][pos[1]], 0)  # PATH

    def show(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
            pygame.display.update()


from rest_framework.test import APITestCase

from newsletter.models import SubscribedEmails
from newsletter.serializers import SubscribedEmailsSerializer

class TestNewsletterSerializer(APITestCase):
    """ Test Newsletter Serializer """

    def setUp(self):
        self.valid_payload = {
            "email": "email@example.com"
        }
        self.invalid_payload = {
            "email": ""
        }

        self.sub_emails1 = SubscribedEmails.objects.create(
            **self.valid_payload
        )
        self.serializer = SubscribedEmailsSerializer(
            instance=self.sub_emails1
        )

    def test_contains_expected_fields(self):
        """ Test that the serializer contains the necessary fields """
        data = self.serializer.data
        self.assertEqual(set(data.keys()), set(
                ['id', 'email', 'timestamp']
            )
        )

    def test_invalid_serializer_payload(self):
        """ Test that the serializer errors if I omit email field """
        serializer = SubscribedEmailsSerializer(
            data = self.invalid_payload
        )
        self.assertFalse(serializer.is_valid())
        self.assertEqual(set(serializer.errors), set(['email']))

    def test_fields_content(self):
        """
            Test that the content of the serializer fields corresponds to
            the payload, which created the instance
        """
        data = self.serializer.data
        self.assertEqual(data['email'], self.valid_payload['email'])


import sys
sys.path.insert(0, '../_libs/')
import NEMO_tools as Nts

import glob
import scipy.io
import numpy as np
import netCDF4 as nc
import NEMO_tools as Nts

def interp_xy(x0, y0, data, x1, y1):
    data_int1 = Nts.reporj_NEMOgrid(x0, y0, data, x1, y1, method='linear')
    data_int2 = Nts.reporj_NEMOgrid(x0, y0, data, x1, y1, method='nearest')
    id1 = np.logical_and(np.isnan(data_int1), np.abs(x1)>90)
    id2 = np.logical_and(np.isnan(data_int1), (y1)>61)
    data_int1[id1]=data_int2[id1]
    data_int1[id2]=data_int2[id2]
    return data_int1

x = np.linspace(-180, 180, 180)
y = np.linspace(60, 90, 60)
lon, lat = np.meshgrid(x, y)

Ba_FName = '/ocean/yingkai/GEOTRACES/Simulations/BARIUM01_1m_20020101_20140103_ptrc_T.nc'
U_FName = '/ocean/yingkai/GEOTRACES/FORCING/ANHA4/vozocrtx_monmean.nc'
V_FName = '/ocean/yingkai/GEOTRACES/FORCING/ANHA4/vomecrty_monmean.nc'

ptrc_obj = nc.Dataset(Ba_FName)
nav_lat = ptrc_obj.variables['nav_lat'][:]
nav_lon = ptrc_obj.variables['nav_lon'][:]

u_obj = nc.Dataset(U_FName)
v_obj = nc.Dataset(V_FName)

Ba = np.zeros([144, 50, 60, 180])
u = np.zeros([144, 60, 180])
v = np.zeros([144, 60, 180])

for i in range(144):
    print('Time {}'.format(i))
    u[i, :, :]  = interp_xy(nav_lon, nav_lat, u_obj.variables['vozocrtx'][i, :, :], lon, lat)
    v[i, :, :]  = interp_xy(nav_lon, nav_lat, v_obj.variables['vomecrty'][i, :, :], lon, lat)
    for j in range(50):
        Ba[i, j, :, :] = interp_xy(nav_lon, nav_lat, ptrc_obj.variables['Ba'][i, j, :, :], lon, lat)

save_var = { 'lon': lon, 'lat': lat, 'Ba': Ba, 'u': u, 'v': v}
scipy.io.savemat('../_data/Exchange/REMAP_Ba_UV.mat', mdict=save_var)


import os
import shutil
import unittest

import requests_mock

import image_grabber
import exceptions

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
MOCK_DIR = os.path.join(BASE_DIR, 'mocks')
TESTS_OUTPUT_DIR = os.path.join(BASE_DIR, 'tmp')

class ImageGrabberTestCase(unittest.TestCase):
    HOST = 'http://kitty.com'

    def setUp(self):
        os.makedirs(TESTS_OUTPUT_DIR, exist_ok=True)

    def tearDown(self):
        shutil.rmtree(TESTS_OUTPUT_DIR, ignore_errors=True)

    def test_image_grabber__valid_source_url_and_valid_destination_path__then_two_images_in_output_dir(self):

        first_image_less_than_100_100 = '90_90_1.jpg'
        second_image_less_than_100_100 = '90_90_2.jpg'

        # easter egg
        first_image_greater_than_100_100 = 'murmur2.jpg'
        second_image_greater_than_100_100 = 'murmur3.jpg'

        mock_html = f'''

        '''

        page_url = self.HOST + '/some_path'
        with requests_mock.Mocker() as mock:
            mock.get(page_url, text=mock_html)
            mock.get(
                self.get_mock_url(first_image_less_than_100_100),
                content=self.get_bytes_from_mock_file(first_image_less_than_100_100),
            )
            mock.get(
                self.get_mock_url(second_image_less_than_100_100),
                content=self.get_bytes_from_mock_file(second_image_less_than_100_100),
            )
            mock.get(
                self.get_mock_url(first_image_greater_than_100_100),
                content=self.get_bytes_from_mock_file(first_image_greater_than_100_100),
            )
            mock.get(
                self.get_mock_url(second_image_greater_than_100_100),
                content=self.get_bytes_from_mock_file(second_image_greater_than_100_100),
            )

            image_grabber.grab_images(page_url, TESTS_OUTPUT_DIR)

        output_dir_files = os.listdir(TESTS_OUTPUT_DIR)
        self.assertIn(first_image_greater_than_100_100, output_dir_files)
        self.assertIn(second_image_greater_than_100_100, output_dir_files)

    @staticmethod
    def get_bytes_from_mock_file(mock_filename) -> bytes:
        with open(os.path.join(MOCK_DIR, mock_filename), mode='rb') as mock_file:
            return mock_file.read()

    @classmethod
    def get_mock_url(cls, mock_filename) -> str:
        return f'{cls.HOST}/{mock_filename}'

    def test_image_grabber__valid_source_url_and_invalid_destination_path__then_catch_GrabberException(self):
        invalid_path = 'blabla'
        with self.assertRaises(exceptions.ImageGrabberException) as context:
            image_grabber.grab_images(self.HOST, invalid_path)

        self.assertIn(f'Path {invalid_path} doesn\'t exists', context.exception.args)

    def test_image_grabber__invalid_source_url_with_missing_schema__then_catch_GrabberException(self):
        with self.assertRaises(exceptions.ImageGrabberException) as context:
            image_grabber.grab_images('kitty.com', TESTS_OUTPUT_DIR)

        self.assertIn(f'Invalid URL kitty.com', context.exception.args)

    def test_image_grabber__invalid_source_url_with_missing_hostname__then_catch_GrabberException(self):
        with self.assertRaises(exceptions.ImageGrabberException) as context:
            image_grabber.grab_images('http://', TESTS_OUTPUT_DIR)

        self.assertIn(f'Invalid URL http://', context.exception.args)


class TrieNode:
    def __init__(self):
        self.next = collections.defaultdict(TrieNode)
        self.ending_word = -1
        self.palindrome_suffixes = []

class Solution:
    def palindromePairs(self, words):

        # Create the Trie and add the reverses of all the words.
        trie = TrieNode()
        for i, word in enumerate(words):
            word = word[::-1] # We want to insert the reverse.
            current_level = trie
            for j, c in enumerate(word):
                # Check if remainder of word is a palindrome.
                if word[j:] == word[j:][::-1]:# Is the word the same as its reverse?
                    current_level.palindrome_suffixes.append(i)
                # Move down the trie.
                current_level = current_level.next[c]
            current_level.ending_word = i

        # Look up each word in the Trie and find palindrome pairs.
        solutions = []
        for i, word in enumerate(words):
            current_level = trie
            for j, c in enumerate(word):
                # Check for case 3.
                if current_level.ending_word != -1:
                    if word[j:] == word[j:][::-1]: # Is the word the same as its reverse?
                        solutions.append([i, current_level.ending_word])
                if c not in current_level.next:
                    break
                current_level = current_level.next[c]
            else: # Case 1 and 2 only come up if whole word was iterated.
                # Check for case 1.
                if current_level.ending_word != -1 and current_level.ending_word != i:
                    solutions.append([i, current_level.ending_word])
                # Check for case 2.
                for j in current_level.palindrome_suffixes:
                    solutions.append([i, j])
        return solutions


import random
from itertools import groupby
import operator

SYMBOLS = ['♠', '♢', '♡', '♣']
SUIT = ['Hearts', 'Spades', 'Diamonds']
RANK = ['A', '2', '3', '4', '5']
RANK_VALUE = {'A': 1, '2': 2, '3': 3, '4': 4, '5': 5}
SUIT_SYMBOLS = {'Hearts': '♡', 'Spades': '♠', 'Diamonds': '♢'}

class Player:
    def __init__(self, name, character="None", turn="None", bluff_prob="None", call_prob="None", belief_model="None"):
        self.stash = list()
        self.grouped_stash = None
        self.name = name
        self.character = character
        self.turn = turn
        self.bluff_prob = bluff_prob
        self.call_prob = call_prob
        self.belief_model = belief_model

    def set_bluff_prob(self, bluff_prob):
        self.bluff_prob = bluff_prob

    def set_call_prob(self, call_prob):
        self.call_prob = call_prob

    def addTo_hand(self, card):
        try:
            self.stash.append(card)
            if len(self.stash) == 5:
                self.stash.sort(key=operator.attrgetter('rank'))
                self.grouped_stash = [list(v) for k, v in groupby(self.stash, key=operator.attrgetter('rank'))]
                # self.stash.sort(key=operator.attrgetter('rank'))
            if len(self.stash) > 5:
                raise ValueError('ERROR: Player cannot have more than 14 cards during turn')
        except ValueError as err:
            print(err.args)

    def removeFrom_hand(self, card):
        self.stash.remove(card)

    def play_Player(self, previous_card):
        random_card = random.choice(self.stash)
        print(self.name + " played " + random_card.rank + SUIT_SYMBOLS[random_card.suit])
        self.stash.remove(random_card)

        if (len(self.stash) == 0):
            return True
        else:
            return False

    def play_allrank(self, new_round, game_obj):
        max_len = 0
        max_list = 0
        if new_round == 0: #if new round
            for i in self.grouped_stash:  # find the rank with maximum cards
                if len(i) > max_len:
                    max_len = len(i)
                    max_list = i
        else:
            for i in self.grouped_stash:
                if game_obj.played_deck[-1][0].rank == i[0].rank:
                    max_list = i
                    break
        if max_list == 0:
            return 0
        self.grouped_stash.remove(max_list)
        game_obj.played_deck.append(max_list)
        for card in max_list:
            print(self.name + " played " + card.rank + SUIT_SYMBOLS[card.suit])
        for card in max_list:
            self.stash.remove(card)
        return 1

class CredulousPlayer(Player):
    def __init__(self, name):
        super().__init__(name, character="Credulous")

    def play(self, new_round, game_obj):
        played = self.play_allrank(new_round, game_obj)
        if played == 0:  #card not found, player must pass
            print(self.name + "passed")
            return 0
        else:
            print("End of turn for " + self.name)
            print("\n")
            return 1


import socket
import os
import time
from multiprocessing import Process

def f(x):
	time.sleep(5)

	print (x)

if __name__ == "__main__":

	x = {
	"1": 2,
	"3": 4,
	"5": 6
	}
	p = Process(target = f, args = (x, ))
	p.start()
	x["6"] = 8

	p.join()
	'''
	port = 8888
	server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	server.bind(("localhost", port))
	server.listen(0)

	while True:
		print ("lalala")
		connection, address = server.accept()
		connection.send(bytes("test: {}".format(connection.recv(1024).decode('utf-8')), 'utf-8'))
	connection.close()
	'''


import math

# Intersection algorithm: Finds the intersection between two functions given a known bound leftBound < intersection < rightBound
# Method:
    # 1. Set the "guess" point to halfway between the bounds.
    # 2. If both functions are nearly equal (their difference is zero) at this guess point, stop the algorithm.
    # 3. Otherwise, if their difference is negative, set the smaller bound to the guess point and repeat from 1.
    # 3 cont. If their difference is positive, set the larger bound to the guess point and repeat from 1.

def Intersection(func1, func2, leftBound, rightBound, dx = 1e-6):
    val = (leftBound + rightBound) / 2
    smaller = leftBound
    larger = rightBound
    diff = func1(val) - func2(val)
    while math.fabs(diff) > dx:
        if diff < 0:
            smaller = val
        else:
            larger = val
        val = (smaller + larger) / 2
        diff = func1(val) - func2(val)
    return val, func1(val)

if __name__ == '__main__':
    f = lambda x: x * x
    g = lambda x: math.sin(x)
    leftBound, rightBound = (0, 10)
    print(Intersection(f, g, leftBound, rightBound))


#! /usr/bin/python
#! -*-coding:utf-8-*-
import sys, os, subprocess, traceback
import requests
import lxml, time, platform
from lxml import html
from bs4 import BeautifulSoup
from selenium import webdriver

from time import sleep
# Графика для приложения
# import PyQt5 QtCore and QtGui modules
from PyQt5 import uic
from PyQt5.QtCore import QByteArray, QBuffer, QSettings, QFile, QIODevice, QVariant, QTimer
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.Qt import *
from PyQtX import QtCore

# Библиотеки для взаимодействия с БД/
import mysql.connector
from mysql.connector import Error
from mysql.connector.constants import ClientFlag

from threading import Thread

#------------------------------------------| ThreadSites |-----------------------------------------#
class ThreadSite(QThread):
    def __init__(self, ui, widget, signalChanPage):
        QThread.__init__(self)
        self.ui = ui
        self.widget = widget
        self.signalChangePage = signalChanPage
        options = webdriver.FirefoxOptions()
        options.add_argument("-headless")

        if platform.system() == "Windows":
            self.browser = webdriver.Firefox(options=options, executable_path=r'geckodriver.exe')
        elif platform.system() == "Linux":
            self.browser = webdriver.Firefox(options=options, executable_path=r'geckodriver')

    def checkText(self, txt):
        x = txt.find("://")
        if x == -1:
            return txt
        text = ""
        for i in range(0, txt.__len__()):
            if i > x + 2:
                text += txt[i]
        txt = text
        return txt

    def checkURL(self, txt):
        try:
            request = requests.get("http://"+txt)
        except:
            return False
        if request.status_code != 200:
            return False
        else:
            return True

    def collectDataFromURL(self, page, searched):
        typesetting = lxml

    def run(self):
        try:
            while True:
                if self.checkURL(self.ui.TabWidget.widget(self.ui.TabWidget.indexOf(self.widget)).lineedit.text()):
                    self.ui.TabWidget.widget(self.ui.TabWidget.indexOf(self.widget)).lineedit.setStyleSheet(" color: green; ")
                    self.browser.get("http://" + self.widget.lineedit.text())
                    self.signalChangePage.emit(BeautifulSoup(self.browser.page_source, "html.parser").prettify(), self.widget)
                else:
                    self.ui.TabWidget.widget(self.ui.TabWidget.indexOf(self.widget)).lineedit.setStyleSheet(" color: red; ")
                #print(self.ui.TabWidget.tabText(self.ui.TabWidget.indexOf(self.widget)))
                try:
                    self.ui.TabWidget.widget(self.ui.TabWidget.indexOf(self.widget)).lineedit.setText(self.checkText(self.widget.lineedit.text()))
                except:
                    print("Ошибка в редактировании ссылки на вкладке: " + str(self.ui.TabWidget.tabText(self.ui.TabWidget.indexOf(self.widget))))
                sleep(10)
        except:
            print("Ошибка браузера, с которого производится парсинг.")

    def __del__(self):
        try:
            self.browser.quit()
        except:
            pass

    # region LOADFILES
    def qsettings(self):
        try:
            self.dataHistory = QSettings(self.pathDataHistory, QSettings.IniFormat)
            self.history = {}
            for i in self.dataHistory.allKeys():
                self.history[int(i)] = self.dataHistory.value(str(i))
        except:
            self.history = {}
            QMessageBox.question(self, 'Предупреждение!', "Инициализация ПУСТЫХ ДАННЫХ об ИСТОРИИ.", QMessageBox.Ok)

        try:
            self.dataSites = QSettings(self.pathDataSites, QSettings.IniFormat)
            self.sites = {}
            for i in self.dataSites.allKeys():
                self.sites[int(i)] = self.dataSites.value(str(i))
        except:
            self.sites = {}
            QMessageBox.question(self, 'Предупреждение!', "Инициализация ПУСТЫХ ДАННЫХ о САЙТАХ.", QMessageBox.Ok)
    # endregion

#-------------------------------------------| QWidgets |-------------------------------------------#
# region WIDGETSETTINGS
class SettingsDBWidget(QDialog):
    def __init__(self, db, parent=None):
        QWidget.__init__(self, parent)
        self.infoBase = db
        self.initSettings()

    # region initUI
    def initSettings(self):
        self.setWindowTitle('Настройки подключения к БД')
        self.setWindowIcon(QIcon('../Images/icon_server.png'))

        self.defSettingsDBWidget, Base = uic.loadUiType('SettingsDBWidget.ui')

        self.SettingsDBWidget = self.defSettingsDBWidget()
        self.SettingsDBWidget.setupUi(self)

        self.show()
    # endregion
# endregion

# region TABWIDGET
class NewWidget(QWidget):
    def __init__(self):
        super().__init__()
        layout = QVBoxLayout()

        self.textedit = QTextEdit()
        self.textedit.setObjectName("text")
        self.lineedit = QLineEdit()
        self.lineedit.setObjectName("line")

        layout.addWidget(self.lineedit)
        layout.addWidget(self.textedit)

        self.setLayout(layout)
# endregion

#-------------------------------------------| MainForm |-------------------------------------------#
#region MAINFORM
class MainWindow(QMainWindow):
    signalChangePage = pyqtSignal(str, object)
    signalSelectedTextEdit = pyqtSignal(int, int, object)

    def __init__(self):
        super().__init__()
        self.initUI()

        # region HEADER

        # region FILE
        self.ui.exportSites.triggered.connect(self.exportSites)
        self.ui.importSites.triggered.connect(self.importSites)
        #
        self.ui.exportHistory.triggered.connect(self.exportHistory)
        self.ui.importHistory.triggered.connect(self.importHistory)
        # endregion

        # region SITES
        self.ui.addSiteAction.triggered.connect(self.addSite)
        #
        self.ui.delCurrentSite.triggered.connect(self.delCurrentSite)
        self.ui.delAllWorkSites.triggered.connect(self.delAllSites)
        # endregion

        # region HISTORY
        self.ui.returnSite.triggered.connect(self.returnSite)
        #
        self.ui.delCurrentHistory.triggered.connect(self.delCurrentHistory)
        self.ui.delAllHistories.triggered.connect(self.delAllHistories)
        # endregion

        # region SETTINGS
        self.ui.setDataBase.triggered.connect(self.setDataBase)
        #
        self.ui.saveSettingsWindow.triggered.connect(self.saveSettingsWindow)
        #
        self.ui.restoreSettingsWindow.triggered.connect(self.restoreSettingsWindow)
        self.ui.removeSettingsWindow.triggered.connect(self.removeSettingsWindow)
        # endregion

        # region MECHANISMS
        self.ui.StartStopParse.clicked.connect(self.StartStopParse)
        self.signalChangePage.connect(self.changePage)
        self.signalSelectedTextEdit.connect(self.selectedTextEdit)

        self.ui.ComboSites.currentIndexChanged.connect(self.changeNumTab)
        self.ui.ComboHistory.currentIndexChanged.connect(self.checkIndex)
        self.ui.TabWidget.currentChanged.connect(self.setCurrentIndexTab)

        #self.ui.threadToSites = QTimer()
        #self.ui.threadToSites.timeout.connect(self.startThreadsAndSites)

        # deb
        self.ui.testadd.triggered.connect(self.teSt)
        self.ui.current.triggered.connect(self.testCurrent)

        self.ui.timerTabToSites = QTimer()
        self.ui.timerTabToSites.timeout.connect(self.tabsToSites)
        self.ui.timerTabToSites.start(100)

        #self.ui.debug = QTimer()
        #self.ui.debug.timeout.connect(self.debuggerTimer)
        #self.ui.debug.start(3000)
        # endregion

        # endregion

        self.checkIndex()

        for i in range(0, self.ui.TabWidget.count()):
            self.ui.TabWidget.widget(i).textedit.clear()

        #self.addSite()
        #self.ui.ComboHistory.addItem('1')

    # region initUI
    def initUI(self):
        self.pathSettings = "../Data/settings.conf"
        self.pathDataSites = "../Data/dataSites.conf"
        self.pathDataHistory = "../Data/dataHistory.conf"

        self.ThreadsSites = {}
        self.infoBase = {}

        self.defMainForm, Base = uic.loadUiType("MainWindow.ui")

        self.ui = self.defMainForm()
        self.ui.setupUi(self)

        self.qsettings()

        self.syncDataToFile(False)

        self.show()
    # endregion

    def keyPressEvent(self, event):
        if self.ui.TabWidget.currentIndex == -1:
            return
        if (event.key() == QtCore.Qt.Key_Enter) | (event.key() != QtCore.Qt.Key_Shift) & (event.key() != QtCore.Qt.Key_Meta) & (event.key() != QtCore.Qt.Key_Control):
            self.changeTextNumSite()

    def closeEvent(self, event):
        self.dataSettings.sync()
        self.dataHistory.sync()
        self.dataSites.sync()
        self.syncDataToFile(True)
        if (self.ThreadsSites.__len__() != 0):
            self.StartStopParse()
        if not Exit:
            subprocess.Popen("./MainWindow.py 1", shell=True)
            print("restarted")
        elif Exit:
            print("exited")

    # region LOADFILES
    def qsettings(self):
        try:
            self.dataSettings = QSettings(self.pathSettings, QSettings.IniFormat)
            self.restoreGeometry(self.dataSettings.value('Window/G'))
            self.restoreState(self.dataSettings.value('Window/S'))
        except:
            self.ui = self.defMainForm()
            self.ui.setupUi(self)

            self.saveSettingsWindow()
            QMessageBox.question(self, 'Предупреждение!', "Загрузка невозможна, возможно программа открывается впервые.", QMessageBox.Ok)

        try:
            self.dataHistory = QSettings(self.pathDataHistory, QSettings.IniFormat)
            self.history = {}
            for i in self.dataHistory.allKeys():
                self.history[int(i)] = self.dataHistory.value(str(i))
        except:
            self.history = {}
            QMessageBox.question(self, 'Предупреждение!', "Инициализация ПУСТЫХ ДАННЫХ об ИСТОРИИ.", QMessageBox.Ok)

        try:
            self.dataSites = QSettings(self.pathDataSites, QSettings.IniFormat)
            self.sites = {}
            for i in self.dataSites.allKeys():
                self.sites[int(i)] = self.dataSites.value(str(i))
        except:
            self.sites = {}
            QMessageBox.question(self, 'Предупреждение!', "Инициализация ПУСТЫХ ДАННЫХ о САЙТАХ.", QMessageBox.Ok)
    # endregion

    # region DEB
    def teSt(self):
        print(self.ThreadsSites)

    def testCurrent(self):
        #self.ui.timerTabToSites.stop()
        print(str(self.dataSites.allKeys()) + " : " + str(self.dataHistory.allKeys()))
        print(str(self.sites) + " : " + str(self.history))

    def debuggerTimer(self):
        for i in self.dataSettings.allKeys():
            if (i == 'Window/G') and (i == 'Window/S'):
                try:
                    print(i + " : " + str(QTextStream(self.dataSettings.value(i)).readAll()))
                except:
                    pass
    # endregion

    # region WORKEDCODE

    # region FILE FUNC
    def exportSites(self):
        result = QFileDialog.getSaveFileName(parent=None, caption="Экспорт сайтов", directory='../ExportedSites/', filter='Файлы настроек (*.conf)\nВсе файлы (*)', options=QFileDialog.DontUseNativeDialog)[0]
        if QFile.exists(result):
            QFile.remove(result)
        self.dataSites = QSettings(result, QSettings.IniFormat)
        for i in self.sites:
            self.dataSites.setValue(str(i), self.sites[i])
        self.dataSites = QSettings(self.pathDataSites, QSettings.IniFormat)

    def importSites(self):
        result = QFileDialog.getOpenFileName(parent=None, caption="Импорт сайтов", directory='../ExportedSites/', filter='Файлы настроек (*.conf)\nВсе файлы (*)', options=QFileDialog.DontUseNativeDialog)[0]
        self.delAllSites()
        self.dataSites = QSettings(result, QSettings.IniFormat)
        for i in self.dataSites.allKeys():
            self.sites[int(i)] = self.dataSites.value(str(i))
        self.returnAllSites(self.sites, {})
        self.dataSites = QSettings(self.pathDataSites, QSettings.IniFormat)

    def exportHistory(self):
        result = QFileDialog.getSaveFileName(parent=None, caption="Экспорт истории", directory='../ExportedHistories/', filter='Файлы настроек (*.conf)\nВсе файлы (*)', options=QFileDialog.DontUseNativeDialog)[0]
        if QFile.exists(result):
            QFile.remove(result)
        self.dataHistory = QSettings(result, QSettings.IniFormat)
        for i in self.history:
            self.dataHistory.setValue(str(i), self.history[i])
        self.dataHistory = QSettings(self.pathDataHistory, QSettings.IniFormat)

    def importHistory(self):
        result = QFileDialog.getOpenFileName(parent=None, caption="Импорт истории", directory='../ExportedHistories/', filter='Файлы настроек (*.conf)\nВсе файлы (*)', options=QFileDialog.DontUseNativeDialog)[0]
        self.delAllHistories()
        self.dataHistory = QSettings(result, QSettings.IniFormat)
        for i in self.dataHistory.allKeys():
            self.history[int(i)] = self.dataHistory.value(str(i))
        self.returnAllSites({}, self.history)
        self.dataHistory = QSettings(self.pathDataHistory, QSettings.IniFormat)
    # endregion

    # region SITES FUNC
    def addSite(self):
        tab = NewWidget()
        for i in range(0, self.ui.ComboSites.count() + 1):
            if self.ui.ComboSites.findText(str(i+1)) == -1:
                self.ui.TabWidget.addTab(tab, str(i + 1))
                self.ui.ComboSites.addItem(str(i + 1))
                self.sites[i+1] = ("", "")
                break

    def delCurrentSite(self):
        if self.ui.ComboSites.currentIndex() != -1:
            if self.ui.ComboHistory.findText(self.ui.ComboSites.currentText()) != -1:
                self.ui.ComboHistory.removeItem(self.ui.ComboHistory.findText(self.ui.ComboSites.currentText()))

            self.ui.ComboHistory.addItem(self.ui.ComboSites.itemText(self.ui.ComboSites.currentIndex()), self.ui.ComboSites.currentText())

            i_s = -1
            i_t = -1

            for i in range(0, self.ui.TabWidget.count()):
                if self.ui.TabWidget.tabText(i) == self.ui.ComboSites.currentText():
                    i_t = i
                if self.ui.TabWidget.tabText(self.ui.TabWidget.currentIndex()) == self.ui.ComboSites.itemText(i):
                    i_s = i

            self.history[int(self.ui.ComboSites.itemText(i_s))] = (self.ui.TabWidget.widget(i_t).lineedit.text(), self.ui.TabWidget.widget(i_t).textedit.toPlainText())

            self.ui.TabWidget.removeTab(i_t)
            self.ui.ComboSites.removeItem(i_s)

            #for i in range(0, self.ui.TabWidget.count()):
            #    print(self.ui.ComboSites())

    def delAllSites(self):
        self.ui.timerTabToSites.stop()
        if self.ui.TabWidget.currentIndex() != -1:
            self.ui.ComboSites.setCurrentIndex(-1)
            self.ui.TabWidget.setCurrentIndex(-1)
            for i in self.sites.keys():
                if self.ui.ComboHistory.findText(str(i)) != -1:
                    self.ui.ComboHistory.removeItem(self.ui.ComboHistory.findText(str(i)))
            for i in self.sites.keys():
                self.ui.ComboHistory.insertItem(i, str(i))
                self.history[i] = self.sites.get(i)

            self.ui.TabWidget.clear()
            self.ui.ComboSites.clear()
            self.sites = {}
        self.ui.timerTabToSites.start()
    # endregion

    # region HISTORIES FUNC
    def returnSite(self):
        if self.ui.ComboHistory.currentIndex() == -1:
            return 0
        if self.ui.ComboSites.findText(self.ui.ComboHistory.currentText()) != -1:
            self.ui.ComboSites.removeItem(self.ui.ComboSites.findText(self.ui.ComboHistory.currentText()))
        for i in range(0, self.ui.TabWidget.count()):
            if self.ui.TabWidget.tabText(i) == self.ui.ComboHistory.currentText():
                self.ui.TabWidget.removeTab(i)
                break

        widget = NewWidget()
        widget.lineedit.setText(self.history[int(self.ui.ComboHistory.currentText())][0])
        widget.textedit.setText(self.history[int(self.ui.ComboHistory.currentText())][1])

        #self.ui.TabWidget.insertTab(int(self.ui.ComboHistory.currentText()), list(self.history.values())[self.ui.ComboHistory.currentIndex()], list(self.history.keys())[self.ui.ComboHistory.currentIndex()])
        self.ui.TabWidget.insertTab(int(self.ui.ComboHistory.currentText())-1, widget, self.ui.ComboHistory.currentText())
        self.ui.ComboSites.insertItem(int(self.ui.ComboHistory.currentText())-1, self.ui.ComboHistory.currentText())
        for i in range(0, self.ui.TabWidget.count()):
            if self.ui.TabWidget.tabText(i) == self.ui.ComboHistory.currentText():
                self.ui.TabWidget.setCurrentIndex(i)

    def delCurrentHistory(self):
        if self.ui.ComboHistory.currentIndex() != -1:
            del self.history[int(self.ui.ComboHistory.currentText())]
            self.ui.ComboHistory.removeItem(self.ui.ComboHistory.currentIndex())

    def delAllHistories(self):
        self.history.clear()
        self.ui.ComboHistory.clear()

    # endregion

    # region SETTINGS FUNC
    def StartStopParse(self):
        if self.ui.StartStopParse.text() == "Старт":
            self.startThreadsAndSites()
            self.ui.StartStopParse.setText("Стоп")
        else:
            self.removeThreadsAndSites()
            self.ui.StartStopParse.setText("Старт")
            for i in range(0, self.ui.TabWidget.count()):
                self.ui.TabWidget.widget(i).lineedit.setStyleSheet("")

    def restoreSettingsWindow(self):
        status = QMessageBox.question(self, 'Внимание!', "Вы точно хотите сделать СБРОС НАСТРОЕК?", QMessageBox.Ok | QMessageBox.Cancel)
        if status == QMessageBox.Ok:
            self.dataSettings.remove("Window/G")
            self.dataSettings.remove("Window/S")
            self.syncDataToFile(True)
        else:
            return
        status = QMessageBox.question(self, 'Внимание!', "Вы хотите ЗАКРЫТЬ ПРИЛОЖЕНИЕ для применения настроек?", QMessageBox.Ok | QMessageBox.Cancel)
        if status == QMessageBox.Ok:
            global Exit
            Exit = False
            self.close()

    def saveSettingsWindow(self):
        self.dataSettings.setValue('Window/G', self.saveGeometry())
        self.dataSettings.setValue('Window/S', self.saveState())

    def removeSettingsWindow(self):
        status = QMessageBox.question(self, 'Внимание!', "Вы точно хотите сделать СБРОС ВСЕХ НАСТРОЕК?", QMessageBox.Ok | QMessageBox.Cancel)
        if status == QMessageBox.Ok:
            os.remove(self.pathSettings)
            self.qsettings()
            self.syncDataToFile(True)
        else:
            return
        status = QMessageBox.question(self, 'Внимание!', "Вы хотите ЗАКРЫТЬ ПРИЛОЖЕНИЕ для применения настроек?", QMessageBox.Ok | QMessageBox.Cancel)
        if status == QMessageBox.Ok:
            global Exit
            Exit = False
            self.close()

    def setDataBase(self):
        self.SettingsDBWidget = SettingsDBWidget(self.infoBase)
    # endregion

    # region MECHANISMS FUNC

    # region Signals
    @pyqtSlot(str, object)
    def changePage(self, textPage, widget):
        cursor = self.ui.TabWidget.widget(self.ui.TabWidget.indexOf(widget)).textedit.textCursor()
        begin = cursor.selectionStart()
        end = cursor.selectionEnd()

        self.ui.TabWidget.widget(self.ui.TabWidget.indexOf(widget)).textedit.setPlainText(str(textPage))

        cursor.setPosition(begin, QTextCursor.MoveAnchor)
        cursor.setPosition(end, QTextCursor.KeepAnchor)
        self.ui.TabWidget.widget(self.ui.TabWidget.indexOf(widget)).textedit.setTextCursor(cursor)

        text = str(self.ui.TabWidget.widget(self.ui.TabWidget.indexOf(widget)).textedit.toPlainText())
        tree = html.fromstring(text)
        try:
            print(tree.xpath('.//h2')[0])
        except:
            pass

        ltext = QLabel()
        text = str(self.ui.TabWidget.widget(self.ui.TabWidget.indexOf(widget)).textedit.toPlainText()).split("\n")
        ltext.setText(str(text))
        self.ui.statusbar.insertWidget(0, ltext)

    @pyqtSlot(int, int, object)
    def selectedTextEdit(self, begin, end, textedit):
        pass
    # endregion

    def removeThreadsAndSites(self):
        try:
            for i in self.ThreadsSites.keys():
                if self.ThreadsSites[i].isRunning():
                    self.ThreadsSites[i].terminate()
                    self.ThreadsSites[i].wait()
            print(self.ThreadsSites)
            for i in self.ThreadsSites.keys():
                if self.ThreadsSites[i].isRunning():
                    print("running: " + str(i))
                if self.ThreadsSites[i].isFinished():
                    print("finished: " + str(i))
        except Exception as e:
            print('Ошибка:\n', traceback.format_exc())

    def startThreadsAndSites(self):
        try:
            for i in range(0, self.ui.TabWidget.count()):
                self.ThreadsSites[int(self.ui.TabWidget.tabText(i))] = ThreadSite(self.ui, self.ui.TabWidget.widget(i), self.signalChangePage)
                self.ThreadsSites[int(self.ui.TabWidget.tabText(i))].start()
        except Exception as e:
            print('Ошибка:\n', traceback.format_exc())

    def changeTextParse(self, str, obj):
        widget = self.ui.TabWidget.widget(self.ui.TabWidget.indexOf(obj))
        widget.textedit.setText(str)

    def tabsToSites(self):
        self.sites = {}
        for i in range(0, self.ui.TabWidget.count()):
            self.sites[int(self.ui.TabWidget.tabText(i))] = (self.ui.TabWidget.widget(i).lineedit.text(), self.ui.TabWidget.widget(i).textedit.toPlainText())
        #print(str(self.sites) + " : " + str(self.history))

    def changeTextNumSite(self):
        try:
            int(self.ui.ComboSites.currentText())
            self.ui.ComboSites.setItemText(self.ui.ComboSites.findText(self.ui.TabWidget.tabText(self.ui.TabWidget.currentIndex())), str(int(self.ui.ComboSites.currentText())))
            self.ui.TabWidget.setTabText(self.ui.TabWidget.currentIndex(), self.ui.ComboSites.currentText())
        except Exception:
            #self.ui.ComboSites.setItemText(self.ui.ComboSites.currentIndex(), self.ui.TabWidget.tabText(self.ui.ComboSites.currentIndex()))
            self.ui.ComboSites.removeItem(self.ui.ComboSites.findText(self.ui.ComboSites.currentText()))
            QMessageBox.question(self, 'Предупреждение!', "Введите число, а не букву или симол в поле ввода номера вкладки.", QMessageBox.Ok)

    def changeNumTab(self):
        self.checkIndex()
        #print(self.ui.ComboSites.findText(self.ui.TabWidget.tabText(self.ui.TabWidget.currentIndex())))
        for i in range(0, self.ui.TabWidget.count()):
            if self.ui.TabWidget.tabText(i) == self.ui.ComboSites.currentText():
                self.ui.TabWidget.setCurrentIndex(i)
        #print(self.ui.TabWidget.currentIndex())

    def setCurrentIndexTab(self):
        if (self.ui.TabWidget.currentIndex() != -1) & (self.ui.ComboSites.currentIndex() != -1):
            self.ui.ComboSites.setCurrentIndex(self.ui.ComboSites.findText(self.ui.TabWidget.tabText(self.ui.TabWidget.currentIndex())))

    def checkIndex(self):
        if self.ui.ComboSites.currentIndex() == -1:
            self.ui.ComboSites.setEditable(False)
        else:
            self.ui.ComboSites.setEditable(True)

        if self.ui.ComboHistory.currentIndex() == -1:
            self.ui.ComboHistory.setEditable(False)
        else:
            self.ui.ComboHistory.setEditable(True)

    def returnAllSites(self, values1, values2):
        for i in values1:
            widget = NewWidget()
            widget.lineedit.setText(self.sites[int(i)][0])
            widget.textedit.setText(self.sites[int(i)][1])

            self.ui.TabWidget.insertTab(int(i) - 1, widget, str(i))
            self.ui.ComboSites.insertItem(int(i) - 1, str(i))
        for i in values2:
            self.ui.ComboHistory.insertItem(int(i) - 1, str(i))

    def syncDataToFile(self, operation):
        if operation:
            for i in self.dataSites.allKeys():
                self.dataSites.remove(str(i))
            for i in self.dataHistory.allKeys():
                self.dataHistory.remove(str(i))
            for i in self.sites:
                self.dataSites.setValue(str(i), self.sites[i])
            for i in self.history:
                self.dataHistory.setValue(str(i), self.history[i])
            #print(str(self.dataSites.allKeys()) + " : " + str(self.dataHistory.allKeys()))
            #print(str(self.sites) + " : " + str(self.history))
        else:
            for i in self.dataSites.allKeys():
                self.sites[int(i)] = self.dataSites.value(str(i))
            for i in self.dataHistory.allKeys():
                self.history[int(i)] = self.dataHistory.value(str(i))
            self.returnAllSites(self.sites, self.history)

    # endregion

    # endregion

#--------------------------------------------------------------------------------------------------#
# endregion

Exit = True

def Main():
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    #sys.setrecursionlimit(50000)
    Main()


def run_multivar_decod(analyzed_effect, gat):
    from config import (myload, mysave, subjects, conditions, tmin, tmax,
                            decim_factor, main_effects, combine_conds)
    import mne
    import numpy as np
    from mne.decoding import GeneralizingEstimator, SlidingEstimator,get_coef, LinearModel
    from sklearn.preprocessing import StandardScaler
    from sklearn.model_selection import ShuffleSplit
    from sklearn.linear_model import LogisticRegression
    from sklearn.pipeline import make_pipeline
    from mne.epochs import EpochsArray
    from matplotlib import pyplot as plt
    from simple_decod_plot import gat_plot
    c_ids = conditions(analyzed_effect, ids=True)

    for sbj in subjects:
        epoch = myload(typ='epochs_merged', sbj=sbj, preload=True)
        epoch.crop(tmin, tmax)
        clf = make_pipeline(StandardScaler(),  LinearModel(LogisticRegression(
                                             C=0.07,
                                            penalty='l2')))
        if analyzed_effect in main_effects.keys():
            combine_conds(epoch, c=analyzed_effect)
        epoch = epoch.copy()[c_ids] #select events to decode
        epoch.decimate(decim_factor)
        epoch.equalize_event_counts(epoch.event_id)
        X = epoch.get_data(); y = epoch.events[:,-1]
        scores = []
        cv = ShuffleSplit(n_splits=10, test_size=0.2)
        if gat:
            estimator = GeneralizingEstimator(clf, scoring='roc_auc', n_jobs=-1)
            typ = 'decod'
        else:
            estimator = SlidingEstimator(clf, scoring='roc_auc', n_jobs=-1)
            typ = 'decod_td'
        for train, test in cv.split(X, y):
            estimator.fit(X[train], y[train])
            scores.append(estimator.score(X[test], y[test]))
        coef = get_coef(estimator, 'patterns_', inverse_transform=True)
        evoked_weights = mne.EvokedArray(coef, epoch.info, tmin=epoch.times[0])

        scores = np.array(scores).mean(axis = 0) #fold-wise
        mysave(scores, typ, sbj=sbj, c=analyzed_effect, overwrite=True)
        mysave(evoked_weights, typ='decod_td_weights', sbj=sbj, c=analyzed_effect,\
                overwrite=True)
        del epoch


from setuptools import setup, find_packages
import sys
import os

py3 = sys.version_info.major >= 3

version = '0.0'

requires = [
    "setuptools",
    "zope.interface",
    "venusian"
]

tests_require = [
    "testfixtures"
]

long_description = "\n".join(open(f).read() for f in  ["README.rst", "CHANGES.txt"])

setup(name='pyramid_bubbling',
      version=version,
      description="",
      long_description=long_description,
      classifiers=[
        "Programming Language :: Python",
        "Programming Language :: Python :: 3.3",
        "Framework :: Pyramid",
        ],
      keywords='',
      author='podhmo',
      url='',
      license='MIT',
      packages=find_packages('src'),
      package_dir = {'': 'src'},
      namespace_packages=[],
      include_package_data=True,
      zip_safe=False,
      install_requires=requires,
      tests_require=tests_require,
      extras_require={
          "testing": tests_require,
      },
)


#names = ['alex']
#names2 = ["jack","rain","rachel","4","5"]
#names.clesr()
#names.extend(names2) #扩展
#
#names = names + names2
#names2.reverse()#反转，

#print(names,names2)
#names2.sort() #数字与字母不可以排序,需要加引号'ｓｔｒ'
#names2.copy()
#print(names2) #按ＡＳＣＩＩ码顺序排序

name = "Alex LI"
name2 = name
print(name,name2)

name = "ALEX LI"
print(name,name2)

#查看内存地址　id(variavle)


class Solution(object):
    def spiralOrder(self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        if not matrix or not matrix[0]:
            return []
        m, n = len(matrix), len(matrix[0])
        x, y, dx, dy = 0, 0, 1, 0
        res = []
        while len(res) < m*n:
            res.append(matrix[y][x])
            matrix[y][x] = ''
            if  (x+dx) % n != x+dx or (y+dy)%m != y+dy or matrix[y+dy][x+dx] == '':
                dx, dy = -dy, dx
            x, y = x+dx, y+dy
        return res


from django.core.management.base import BaseCommand
from django.db import transaction

from waldur_mastermind.billing import models

class Command(BaseCommand):
    help = "Create or update price estimates based on invoices."

    def handle(self, *args, **options):
        with transaction.atomic():
            for model in models.PriceEstimate.get_estimated_models():
                for instance in model.objects.all():
                    estimate, _ = models.PriceEstimate.objects.get_or_create(
                        scope=instance
                    )
                    estimate.update_total()
                    estimate.save()


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

# =============================================================================
# Developer : Shashank Sharma(shashankrnr32@gmail.com)
# License : MIT License
# Year : 2019
# =============================================================================
# =============================================================================
# Description:
#     Plot MFCC Colormap
# =============================================================================

import speech_tools as est
import numpy as np
import argparse
import matplotlib.pyplot as plot
from matplotlib import cm

parser = argparse.ArgumentParser(description = 'Plot colormap of MFCC vectors')

parser.add_argument('-i', nargs = '?', required = True, type = str, help = 'Input File (.wav)')
parser.add_argument('-o', nargs = '?', type = str, help = 'Output Image File (Optional)')
parser.add_argument('-flen', nargs = '?', type = float, default = 0.02, help = 'Frame Length in seconds (0.02)')
parser.add_argument('-fshift', nargs = '?', type = float, default = 0.01, help = 'Frame Shift in seconds (0.01)')
parser.add_argument('-order', nargs = '?', type = int, default = 13, help = 'MFCC vector order (13)')
args = parser.parse_args()

#Find MFCC Array
mfcc_array = est.mfcc(input_file = args.i, frame_length = args.flen, frame_shift = args.fshift,
                      order = args.order)

#Transpose
mfcc_array= np.swapaxes(mfcc_array, 0 ,1)

#Show in colormap
plot.imshow(mfcc_array, interpolation='nearest',
                cmap=cm.magma, origin='lower', aspect = 'auto')

plot.title('MFCC {}'.format(args.i.split('/')[-1]))
plot.xlabel('Frames')
plot.ylabel('Index')
if args.o:
    plot.savefig(args.o, dpi = 360)
else:
    plot.show()


from absl import flags
from tensorflow.keras.layers import Dropout, Conv1D, Input, Lambda
from tensorflow.keras import Model
import tensorflow as tf

from segmentation_models import Unet
from segmentation_models.losses import bce_jaccard_loss
from segmentation_models.metrics import f1_score, iou_score

import myFlags
FLAGS = flags.FLAGS

def build_seg_model(input_shape=(224, 224, 81)):
    model = Unet('resnet18', encoder_weights=None, classes=2,
                 activation='softmax', input_shape=input_shape)
    model.compile('Adam', loss=bce_jaccard_loss,
                  metrics=[iou_score, f1_score])
    return model

def build_pixel_mlp_class_model(nb_classes, input_shape=(1024,81), loss_weight=0.):
    inputs = Input(input_shape)
    #x = Lambda(lambda x: K.squeeze(x, axis=-1))(inputs)

    # MLP for all pixels
    x = Conv1D(FLAGS.mlp_nodes, 1, strides=1)(inputs)
    x = Dropout(0.5)(x)
    x = Conv1D(FLAGS.mlp_nodes, 1, strides=1, activation="relu")(x)
    x = Dropout(0.25)(x)
    x = Conv1D(FLAGS.penultimate_nodes, 1, strides=1,
           activity_regularizer=VarRegularizer(loss_weight), name="penultimate")(x)
    x = Dropout(rate=0.25)(x)
    x = Conv1D(nb_classes, 1, strides=1, activation="softmax", name="predictions")(x)
    preds = Lambda(lambda x: tf.reduce_mean(x, axis=1))(x)

    model = Model(inputs=inputs, outputs=preds)
    model.compile(loss='categorical_crossentropy',
                  optimizer="Adam", metrics=['accuracy'])
    return model

@tf.keras.utils.register_keras_serializable(package='Custom', name='var')
class VarRegularizer(tf.keras.regularizers.Regularizer):
    def __init__(self, loss_weight=0.):
        self.loss_weight =  loss_weight

    def __call__(self, x):
        return self.loss_weight * tf.math.reduce_sum(tf.math.reduce_variance(x, axis=0))

    def get_config(self):
        return {'var': float(self.loss_weight)}


# -*- coding: utf-8 -

import numpy as np

#Given two points, travel from point1 to point2, then rotate
#(anticlockwise) by theta, and proceed a distance r to
#a third point. Return coords of that point.

#Useful for plotting coupler points or end
#points of bent links.
#point1, point2 and cpoint are 2-element arrays
def coupler(point1,point2,r,theta):
      delta = point2-point1
      phi = np.arctan2(delta[1],delta[0])
      psi = phi+theta
      rx = r*np.cos(psi)
      ry = r*np.sin(psi)
      cpoint = point2+np.array([rx,ry])
      return cpoint


from ch02 import kNN, matplot
from numpy import *
import matplotlib
import matplotlib.pyplot as plt

# kNN.datingClassTest()
#
# kNN.classifyPerson()
# 获取数据集
# datingDataMat, datingLabels = kNN.file2matrix('datingTestSet2.txt')
# # 将数据集归一化
# normMat, ranges, minVals = kNN.autoNorm(datingDataMat)
# matplot.showScatterFig(normMat, datingLabels)

# testVector = kNN.img2vector("testDigits/0_13.txt")
# print(testVector[0, :31])
# kNN.handwritingclasstest()

dataSet, labels = kNN.createSimpleDataSet()
print(dataSet)

print(dataSet.sum(axis=1))

sortindex = dataSet.sum(axis=1).argsort()
sortindex = reversed(sortindex)
for i in sortindex:
    print(i)

label = kNN.classify0([1, 5], dataSet, ['a', 'a', 'b', 'b'], 2)
print(label)

dataSet, labels = kNN.file2matrix('datingTestSet2.txt')
print(dataSet)
print(labels)

fig = plt.figure()
ax = fig.add_subplot(111)
print(labels)
ax.scatter(dataSet[:, 1], dataSet[:, 2], 15.0 * array(labels), 15.0 * array(labels))
plt.show()

normDataSet, ranges, minvals = kNN.autoNorm(dataSet)

kNN.datingClassTest()

kNN.handwritingclasstest()


import cv2 # importing cv2 liberary

cam = cv2.VideoCapture(0)

count = 0

while True:
    ret, img = cam.read()

    cv2.imshow("Test", img)

    if not ret:
        break

    k=cv2.waitKey(1)

    if k%256==27:
        #For Esc key
        print("Close")
        break
    elif k%256==32:
        #For Space key

        print("Image "+str(count)+"saved")
        file='img/img'+str(count)+'.jpg'
        cv2.imwrite(file, img)
        count +=1

cam.release
cv2.destroyAllWindows


# uncompyle6 version 3.7.4
# Python bytecode 3.4 (3310)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /usr/local/lib/python3.4/dist-packages/quickcut/__init__.py
# Compiled at: 2015-09-13 16:55:53
# Size of source mod 2**32: 10075 bytes
import os, subprocess, sys, shutil, pkg_resources, datetime as dt
from pathlib import Path
import pysrt
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QLabel, QPushButton, QMessageBox
from quickcut.widgets import Picker, MinuteSecondEdit, BiggerMessageBox
__author__ = 'Edward Oubrayrie'
package = Path(__file__).parent.name
__version__ = pkg_resources.get_distribution(package).version
icon_path = pkg_resources.resource_filename('quickcut', 'quickcut.png')
print(icon_path)

def packagekit_install(pack='ffmpeg'):
    """
    Equivalent of:
     qdbus org.freedesktop.PackageKit /org/freedesktop/PackageKit
           org.freedesktop.PackageKit.Modify.InstallPackageNames
           0 ffmpeg  "show-confirm-search,hide-finished"
    Or:
     qdbus org.freedesktop.PackageKit /org/freedesktop/PackageKit
           org.freedesktop.PackageKit.Query.IsInstalled 0 ffmpeg
    See also (dbus) http://www.freedesktop.org/software/PackageKit/pk-faq.html#session-methods
    Doc: http://blog.fpmurphy.com/2013/11/packagekit-d-bus-abstraction-layer.html
    """
    from PyQt5.QtDBus import QDBusConnection
    from PyQt5.QtDBus import QDBusInterface
    bus = QDBusConnection.sessionBus()
    service_name = 'org.freedesktop.PackageKit'
    service_path = '/org/freedesktop/PackageKit'
    interface = 'org.freedesktop.PackageKit.Query.IsInstalled'
    install = QDBusInterface(service_name, service_path, interface, bus)
    reply = install.call(0, pack, 'show-confirm-search,hide-finished')
    print(reply.arguments())
    interface = 'org.freedesktop.PackageKit.Modify.InstallPackageNames'
    install = QDBusInterface(service_name, service_path, interface, bus)
    reply = install.call(0, pack, 'show-confirm-search,hide-finished')
    print(reply.arguments())

def duration(start: dt.time, stop: dt.time) -> dt.timedelta:
    return dt.datetime.combine(dt.date.min, stop) - dt.datetime.combine(dt.date.min, start)

def timedelta_str(d: dt.timedelta) -> str:
    assert d.days == 0
    hours, remainder = divmod(d.seconds, 3600)
    minutes, seconds = divmod(remainder, 60)
    return '%02d:%02d:%02d' % (hours, minutes, seconds)

def duration_str(h_m_s_start: [
 int, int, int], h_m_s_stop: [int, int, int]):
    return timedelta_str(duration(dt.time(*h_m_s_start), dt.time(*h_m_s_stop)))

def video_cut(vid_in, vid_out, ss, to, d, parent):
    if os.path.isfile(vid_out):
        msg = '{} already exists\n\nDo you want to replace it ?'.format(vid_out)
        video_ret = QMessageBox.warning(parent, 'File exists', msg, defaultButton=QMessageBox.Cancel)
        if video_ret == QMessageBox.Cancel:
            return
        try:
            os.remove(vid_out)
        except OSError as e:
            msg = 'Cannot write {}, system returned {}.\n\nChange output file name and retry,'.format(vid_out, str(e))
            QMessageBox.critical(parent, 'Wrong file', msg)
            return

    video_ret = 0
    if os.path.isfile(vid_in):
        ffmpeg = shutil.which('ffmpeg')
        avconv = shutil.which('avconv')
        exe = ffmpeg or avconv
        if not exe:
            msg = 'Install ffmpeg or avconv'
            QMessageBox.critical(parent, 'Missing dependency', msg)
            return
        if exe == avconv:
            stop = [
             '-t', d]
        else:
            stop = [
             '-to', to]
        command = [ffmpeg, '-nostdin', '-noaccurate_seek',
         '-i', vid_in,
         '-ss', ss,
         stop[0], stop[1],
         '-vcodec', 'copy',
         '-acodec', 'copy',
         vid_out]
        print(command)
        p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr = p.communicate()
        video_ret = p.poll()
        if video_ret != 0:
            msg = 'Error {:d} occured. Check video file or see details.'.format(video_ret)
            dmsg = '\n\n{}\n\n{}\n\n{}'.format(stdout.decode(), '_' * 30, stderr.decode())
            err_dialog = BiggerMessageBox(QMessageBox.Critical, 'Error during video cut', msg, parent=parent)
            err_dialog.setDetailedText(dmsg)
            err_dialog.exec()
    return video_ret

class Main(QtWidgets.QWidget):

    def __init__(self):
        super(Main, self).__init__()
        self.video_pick = Picker('Open video', filters='Videos (*.mp4 *.mpg *.avi);;All files (*.*)')
        self.subtitle_pick = Picker('Open subtitle', filters='SubRip Subtitles (*.srt);;All files (*.*)')
        self.save_pick = Picker('Save as', check_exists=False, check_writable=True)
        self.start = MinuteSecondEdit(self)
        self.stop = MinuteSecondEdit(self)
        icon_ok = self.style().standardIcon(QtWidgets.QStyle.SP_DialogOkButton)
        self.ok_btn = QPushButton(icon_ok, 'Do it !', self)
        self.init()

    def init(self):
        self.video_pick.textChanged.connect(self.video_changed)
        for w in (self.video_pick, self.subtitle_pick, self.start, self.stop, self.save_pick):
            w.textChanged.connect(self.doit_controller)

        times = QtWidgets.QHBoxLayout()
        times.addWidget(self.start)
        times.addWidget(self.stop)
        times.addStretch(1)
        self.ok_btn.setEnabled(False)
        self.ok_btn.clicked.connect(self.do_it)
        icon_quit = self.style().standardIcon(QtWidgets.QStyle.SP_DialogCancelButton)
        quit_btn = QPushButton(icon_quit, 'Quit', self)
        quit_btn.clicked.connect(exit)
        hbox = QtWidgets.QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(self.ok_btn)
        hbox.addWidget(quit_btn)
        grid = QtWidgets.QGridLayout()
        grid.setSpacing(10)
        grid.addWidget(QLabel('Video:'), 1, 0)
        grid.addWidget(self.video_pick, 1, 1)
        grid.addWidget(QLabel('Subtitles:'), 2, 0)
        grid.addWidget(self.subtitle_pick, 2, 1)
        grid.addWidget(QLabel('Start / Stop (HHMMSS):'), 3, 0)
        grid.addLayout(times, 3, 1)
        grid.addWidget(QLabel('Output:'), 4, 0)
        grid.addWidget(self.save_pick, 4, 1)
        grid.addLayout(hbox, 5, 1)
        self.setLayout(grid)

    @QtCore.pyqtSlot()
    def video_changed(self, *args, **kw):
        p = self.video_pick.get_text()
        if p:
            self.subtitle_pick.set_text(str(Path(p).with_suffix('.srt')))

    @QtCore.pyqtSlot()
    def doit_controller(self, *args, **kw):
        ok = lambda w: w.hasAcceptableInput()
        self.ok_btn.setEnabled((ok(self.video_pick) or ok(self.subtitle_pick)) and ok(self.start) and ok(self.stop) and ok(self.save_pick))

    def do_it(self):
        vid_in = self.video_pick.get_text()
        vid_out = self.save_pick.get_text() + os.path.splitext(vid_in)[1]
        ss = self.start.get_time()
        to = self.stop.get_time()
        d = duration_str(self.start.get_h_m_s(), self.stop.get_h_m_s())
        video_ret = video_cut(vid_in, vid_out, ss, to, d, self)
        if video_ret == 0:
            sbt_out = self.cut_subtitle()
            opn = shutil.which('xdg-open')
            if vid_out and os.path.isfile(vid_out):
                f = vid_out
            else:
                if sbt_out and os.path.isfile(sbt_out):
                    f = sbt_out
                else:
                    msg = ''
                    QMessageBox.warning(self, 'no file was generated', msg, defaultButton=QMessageBox.NoButton)
                    return
            if opn:
                subprocess.Popen([opn, f])

    def cut_subtitle(self):
        sbt_in = self.subtitle_pick.get_text()
        if os.path.isfile(sbt_in):
            sbt_out = self.save_pick.get_text() + os.path.splitext(sbt_in)[1]
            h1, m1, s1 = self.start.get_h_m_s()
            h2, m2, s2 = self.stop.get_h_m_s()
            subs = pysrt.open(sbt_in, error_handling=pysrt.ERROR_LOG)
            print('Decoded {} with {} items'.format(sbt_out, len(subs)))
            part = subs.slice(starts_after={'hours': h1,  'minutes': m1,  'seconds': s1}, ends_before={'hours': h2,  'minutes': m2,  'seconds': s2})
            print('Sliced {} items'.format(len(part)))
            part.shift(seconds=-duration(dt.time(h1, m1, s1), dt.time(h2, m2, s2)).total_seconds())
            part.save(path=sbt_out)
            print('Successfully written', sbt_out)
            return sbt_out

def main():
    app = QtWidgets.QApplication(sys.argv)
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap(icon_path), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    app.setWindowIcon(icon)
    w = Main()
    w.normalGeometry()
    w.setWindowTitle('QuickCut')
    w.show()
    sys.exit(app.exec())

if __name__ == '__main__':
    main()


#进度条
import  sys,time

def progress_bar():
    for i in range(0,200):
        print("\r",end="")
        print("download progress:{}%".format(i),"@"*(i//2),end="")
        sys.stdout.flush()
        time.sleep(0.05)
progress_bar()


import requests
import time
from bs4 import BeautifulSoup
from pymongo import MongoClient
import argparse

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--url')
args = parser.parse_args()

url = args.url

global hashtag
with open('HASHTAG_FILE2.txt') as f:
    hashtag = f.read()[1:7]
    equipe1 = hashtag[:3]
    equipe2 = hashtag[3:]

#hashtag = "SENCOL"

var = True

client = MongoClient('localhost', 27017)
db = client['WorldCup']
collection_Score = db[hashtag+'_Score']

collection_Score.update_one({"Equipe":hashtag[:3]}, {"$set": {"Score":0}}, upsert=True)
collection_Score.update_one({"Equipe":hashtag[3:]}, {"$set": {"Score":0}}, upsert=True)

while var == True:
    res = requests.get(url).content
    soup = BeautifulSoup(res, 'html.parser')
    score = soup.find("span",attrs={"class":"fi-s__scoreText"}).text
    score = score.replace("\r\n","")

    if ":" in score:
        score1 , score2 = 0, 0

    else:
        score1 = int(score.split("-")[0])
        score2 = int(score.split("-")[-1])

    for i in list(collection_Score.find()):
        if i["Equipe"] == equipe1 :
            if i["Score"] != score1 :
                collection_Score.update_one({"Equipe":hashtag[:3]}, {"$set": {"Score":score1}}, upsert=True)

        if i["Equipe"] == equipe2 :
            if i["Score"] != score1 :
                collection_Score.update_one({"Equipe":hashtag[3:]}, {"$set": {"Score":score2}}, upsert=True)

    time.sleep(60)


import numpy as np
import matplotlib.pyplot as plt

import seaborn as sns

# Based on personal preference, can alter the seaborn settings
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728',
          '#9467bd', '#8c564b', '#e377c2', '#7f7f7f',
          '#bcbd22', '#17becf']

sns.set(style='whitegrid', palette=colors, rc={'axes.labelsize': 16}) # overrides matplotlib settings for plots

# Load in data
xa_low = np.loadtxt('data/xa_low_food.csv', comments='#')
xa_high = np.loadtxt('data/xa_high_food.csv', comments='#')

# Set up nice bin widths
bins = np.arange(1600, 2501, 50)

# Set up plot
fig, ax = plt.subplots(1, 1) # 1 row, 1 column
_ = ax.set_xlabel('cross sectional area (µm$^2$)')
_ = ax.set_ylabel('count')

# "Paint" the data
_ = ax.hist((xa_low, xa_high), bins=bins)

# Add a legend
ax.legend(('low', 'high'), loc='upper right');

# Save the figure
fig.savefig('practice_histogram.pdf')

# Display the plot in a figure
plt.show()


num = int(input("몇번 문제?"))
if num == 1 :
    print('1번')
    for i in range(2,8) :
        for a in range(1,i) :
            print(a, end='')
        print('')

elif num == 2 :
    print('2번')
    for i in range(1,6) :
        for a in range(0,i) :
            a = '*'
            print(a, end='')
        print('')

elif num == 3 :
    print('3번')
    for i in range(1,4) :
        for a in range(0,i) :
            a = '*'
            print(a, end='')
        print('')

elif num == 4 :
    for i in range(1,6) :
        for a in range(i,6) :
            a = '*'
            print(a, end='')
        print('')

elif num == 5 :
    for dan in range(2,10) :
        for i in range(1,10) :
            print(dan,' * ', i,' = ', (dan*i))
        print('')

elif num == 6 :
    value = input('숫자를 입력하세요 : ')
    dan = int(value)
    for i in range(1,10) :
        print(dan,' * ', i,' = ', (dan*i))
    print('')

elif num == 7 :
    value = input('숫자를 입력하세요 : ')
    a = int(value)
    sum = 0
    for b in range(0,a+1) :
        sum += b
    print(sum)

elif num == 8 :
    value = int(input('숫자를 입력하세요 : '))
    if value%3 == 0 :
        print(value,'은(는) 3의 배수입니다')
    elif value%3 != 0 :
        print(value,'은(는) 3의 배수가 아닙니다')

elif num == 9 :
    sum = 0
    for i in range(1, 11) :
        if i%2 != 0 and i%3 != 0 :
            print(i, end='\t')
            sum += i
    print('')
    print(sum)

elif num == 10 :
   for i in range(1, 7):
       for a in range(1, 7):
           if (i + a) == 4 :
               print(i, a)

elif num == 11 :
    for i in range(1,10):
        print(3,'*',i,'=',(3*i), end=' ')

elif num == 12 :
    for dan in range(2, 10):
        for i in range(1, 10):
            print(dan,'*',i,'=',(dan*i),end=' ')
        print('')


import datetime,logging
from os import path
from sqlalchemy import func
from flask import render_template, Blueprint,redirect, url_for, flash
from flask_wtf import FlaskForm
from server.models import db,Log,User
from server.forms import LoginForm, RegisterForm

main_blueprint = Blueprint(
    'main',
    __name__,
    template_folder='../templates/main'
)

@main_blueprint.route('/')
def index():
    return render_template(
        'index.html'
    )

@main_blueprint.route('/login', methods=['GET','POST'])
def login():
    form = LoginForm()

    if form.validate_on_submit():
        flash("Your have been logged in.", category="success")
        user = User.query.filter_by(username=form.username.data).first()
        logging.info('Login Successful')
        return render_template(
            'user.html',
            user=user
        )
    return render_template('login.html', form = form)

@main_blueprint.route('/logout',methods=['GET','POST'])
def logout():
    return render_template(
            'index.html',
    )

@main_blueprint.route('/register',methods=['GET','POST'])
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        new_user = User(form.username.data)
        new_user.setPassword(form.password.data)

        db.session.add(new_user)
        db.session.commit()

        flash("Your user has been created, please login.", category="success")
        return redirect(url_for('.login'))

    return render_template('register.html',form=form)


def distance(a, precision):
	return abs(float(a)-float(round(a,precision)))

#make list of lines to remove
hroutput = open("hroutput", 'r')
i=0
count = 0
liste=[]
for line in hroutput:
	value = float(line)
	#print value, abs (value-round(value,1))
	if (abs (value-round(value,1) > 0.025)):
		liste.append(i)
		count+=1

	i+=1

#remove from biginput
biginput = open("biginput", 'r')
filinput = open("filinput", 'w')

i=-1
filinput.write(str(500-count)+'\n')
for line in biginput:
	if (i>=0):
		if not(i in liste):
			filinput.write(line)

	i+=1

#print "COUNT = ",count


from fabric.contrib.files import exists,sed,append
from fabric.api import cd, env, local, run
import random

REPO_URL = 'https://github.com/StrongRoy/sites.git'

def deploy():
    site_folder = f'/home/{env.user}/sites/{env.host}'

    run(f'mkdir -p {site_folder}')
    with cd(site_folder):
        _get_latest_source()
        _update_virtualenv()
        _update_static_files()
        _update_database()

def _get_latest_source():
    if exists('.git'):
        run('git fetch')
    else:
        run(f'git clone {REPO_URL} .')
    current_commit = local("git log -n 1 --format=%H", capture=True)
    run(f'git reset --hard {current_commit}')

# def _update_settings():
#     settings_path = './source/superlists/settings.py'
#     sed(settings_path,"DEBUG = True","DEBUG = False")
#     sed(settings_path,
#     'ALLOWED_HOSTS = .+$',f'ALLOWED_HOSTS = ["{env.host}"]'
#     )
#     secret_key_file =  './source/superlist/secret_key.py'
#     if not exists(secret_key_file):
#         chars = '(53^65d5(rupgyk59e6fvqk_*b9+9-b=5fv&y$rp159u9(vc-h'
#         key = ''.join(random.SystemRandom().choice(chars) for _ in range(50))
#         append(secret_key_file,f'SECRET_KEY = "{key}"')
#     append(settings_path,'\nfrom .secret_key import SECRET_KEY')

def _update_virtualenv():
    if not exists('virtualenv/bin/pip'):
        run(f'python3.6 -m venv virtualenv')
    run('./virtualenv/bin/pip install -r ./source/requirements.txt')

def _update_static_files():
    run('./virtualenv/bin/python ./source/manage.py collectstatic --noinput')

def _update_database():
    run('./virtualenv/bin/python ./source/manage.py migrate --noinput')


############################################################################
# CONFIDENTIAL
#
# Copyright (c) 2020 Qualcomm Technologies International, Ltd.
#   %%version
#
############################################################################

from ..caa.caa_app import CAAApp

class ChargerCaseApp(CAAApp):
    """
    Container for analysis classes representing code specific to the charger
    case app.
    """
    @property
    def subcomponents(self):
        subcmpts = CAAApp.subcomponents.fget(self)
        subcmpts.update({
            # Add charger_case-specific subcomponents here
            # "subcmt" : "_subcmpt"
            })
        return subcmpts

    # TEMPLATE CODE FOR NEW SUBCOMPONENTS
    #@property
    #def subcmpt(self):
    #    try:
    #        self._subcmpt
    #    except AttributeError:
    #        self._subcmpt = Subcmpt(self.env, self._core, self)
    #         or
    #        self._subcmpt = self.create_component_variant((SubcmptFlavour1,
    #                                                       SubcmptFlavour2),
    #                                                       self.env, self._core, self)
    #    return self._subcmpt