import os
import json
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from glob import glob
import imageio
import imgaug as ia
import imgaug.augmenters as iaa
from imgaug.augmentables.segmaps import SegmentationMapOnImage
import json
from pycocotools import mask
from skimage import measure

def augmentations(image, image_mask, sequence, size, save_original):
    """Do specified number of augmentations with sequence augmenter.
    Args:
        image: the image to augment
        image_mask: initial binary mask (0 - background, 1 - foreground)
        sequnce: Sequential object to call for augmentations
        size: number of different augmented images to make
        save_original: boolean, whether or not leave the original image
    Returns:
        list of tuples (new image, new mask (0 - background, 1 - foreground)) for augmented images
    """
    segmap = SegmentationMapOnImage(image_mask, shape=image.shape, nb_classes=2)
    augs = [sequence(image=image, segmentation_maps=segmap) for _ in range(size - int(save_original))]
    if save_original:
        augs = [(image, segmap)] + augs
    augs = [(image, segmap.get_arr_int().astype(np.uint8)) for image, segmap in augs]
    return augs

def mask_to_annotation(image_id, image_mask):
    """Convert image binary mask to json annotation.
    Args:
        image_id: 'image_id' and 'id' fields to write to annotation
        image_mask: binary mask (0 - background, 1 - foreground)
    Returns:
        annotation dict
    """
    ground_truth_binary_mask = image_mask
    fortran_ground_truth_binary_mask = np.asfortranarray(ground_truth_binary_mask)
    encoded_ground_truth = mask.encode(fortran_ground_truth_binary_mask)
    ground_truth_area = mask.area(encoded_ground_truth)
    ground_truth_bounding_box = mask.toBbox(encoded_ground_truth)
    contours = measure.find_contours(ground_truth_binary_mask, 0.5)

    annotation = {
            "segmentation": [],
            "area": ground_truth_area.tolist(),
            "iscrowd": 0,
            "image_id": image_id,
            "bbox": ground_truth_bounding_box.tolist(),
            "category_id": 1,
            "id": image_id
        }

    for contour in contours:
        contour = np.flip(contour, axis=1)
        segmentation = contour.ravel().tolist()
        annotation["segmentation"].append(segmentation)

    return annotation

def add_to_dataset(dataset, image, image_mask, image_id):
    """Add the image and its annotation to the dataset.
    Args:
        dataset: the dataset to modify
        image: the image to add to the dataset
        image_mask: binary mask (0 - background, 1 - foreground)
        image_id: 'image_id' and 'id' fields to write to annotation
    Returns:
        Nothing
    """
    dataset['images'].append({"license": 0, "file_name": f"{image_id:08}.jpg",
                               "width": 512, "height": 512, "id": image_id})
    dataset['annotations'].append(mask_to_annotation(image_id, image_mask))


window_height = 600
window_width = 800
player_movespeed = 1/2
player_name = "Player1"
highscores_filename = "scores.pickle"
default_level = 25
difficulty = default_level
max_level = 40
min_level = 10
fps = 60
colors = {
            "Black": (0, 0, 0),
            "White": (255, 255, 255),
            "Red": (255, 0, 0),
            "Green": (0, 255, 0),
            "Blue": (0, 0, 255),
            "Grey": (100, 100, 100)
        }


from torch.utils.data import Dataset
import torchvision.transforms.functional as TF
import torchvision as tv
import torch

from PIL import Image
import numpy as np
import random
import os
import spacy

from .utils import nested_tensor_from_tensor_list, read_json, read_txt

MAX_DIM = 299

def under_max(image):
    if image.mode != 'RGB':
        image = image.convert("RGB")

    shape = np.array(image.size, dtype=np.float)
    long_dim = max(shape)
    scale = MAX_DIM / long_dim

    new_shape = (shape * scale).astype(int)
    image = image.resize(new_shape)

    return image

class RandomRotation:
    def __init__(self, angles=[0, 90, 180, 270]):
        self.angles = angles

    def __call__(self, x):
        angle = random.choice(self.angles)
        return TF.rotate(x, angle, expand=True)

train_transform = tv.transforms.Compose([
    RandomRotation(),
    tv.transforms.Lambda(under_max),
    tv.transforms.ColorJitter(brightness=[0.5, 1.3], contrast=[
                              0.8, 1.5], saturation=[0.2, 1.5]),
    tv.transforms.RandomHorizontalFlip(),
    tv.transforms.ToTensor(),
    tv.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])

val_transform = tv.transforms.Compose([
    tv.transforms.Lambda(under_max),
    tv.transforms.ToTensor(),
    tv.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

# spacy_eng = spacy.load("en")
# def tokenize_eng(text):
#     return [tok.text for tok in spacy_eng.tokenizer(text)]
english = torch.load("english.pth")

class CocoCaption(Dataset):
    def __init__(self, root, ann, max_length, limit, transform=train_transform, mode='training', tokenizer=english):
        super().__init__()

        self.root = root
        self.transform = transform
        self.annot = [self._process(val)
                      for val in ann.split("\n")]
        if mode == 'validation':
            self.annot = self.annot
        if mode == 'training':
            self.annot = self.annot[: limit]

        self.english = english
        self.max_length = max_length + 1

    def _process(self, val):
        image_id = val.split()[0]
        caption = val.split()[1:]
        caption = " ".join(caption)
        return (image_id, caption)

    def caption_encoder(self, caption):
        tokens = english.tokenize(caption)
        tokens = ["<sos>"] + tokens + ["<eos>"]
        numbs = english.numericalize([tokens])
        numbs = numbs.numpy().T[0]
        caption = np.zeros(self.max_length)
        caption[:len(numbs)] = numbs

        cap_mask = np.ones(self.max_length)
        cap_mask[:len(numbs)] = 0
        return caption, cap_mask.astype(bool)

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

    def __getitem__(self, idx):
        image_id, caption = self.annot[idx]
        image = Image.open(os.path.join(self.root, image_id))

        if self.transform:
            image = self.transform(image)
        image = nested_tensor_from_tensor_list(image.unsqueeze(0))

        caption, cap_mask = self.caption_encoder(caption)

        return image.tensors.squeeze(0), image.mask.squeeze(0), caption, cap_mask

def build_dataset(config, mode='training'):
    if mode == 'training':
        train_dir = os.path.join(config.dir, 'train2017')
        train_file = 'train_captions.txt'
        data = CocoCaption(train_dir, read_txt(
            train_file), max_length=config.max_position_embeddings, limit=config.limit, transform=train_transform, mode='training', tokenizer=english)
        return data

    elif mode == 'validation':
        val_dir = os.path.join(config.dir, 'val2017')
        val_file = 'val_captions.txt'
        data = CocoCaption(val_dir, read_txt(
            val_file), max_length=config.max_position_embeddings, limit=config.limit, transform=val_transform, mode='validation', tokenizer = english)
        return data

    else:
        raise NotImplementedError(f"{mode} not supported")


# SYSTEM IMPORTS
from collections import Counter
from tqdm import tqdm
from typing import List, Set
import numpy as np

# PYTHON PROJECT IMPORTS
from .covertree import CoverTree, CoverTreeNode

DyadicCellType = "DyadicCell"

class DyadicCell(object):
    def __init__(self,
                 node: CoverTreeNode,
                 scale: int,
                 dataset_idx: int) -> None:
        self.parent: DyadicCellType = None
        self.children: List[DyadicCellType] = list()
        self.dataset_idx: int = dataset_idx
        self.scale: int = scale

        self.pt_idxs: np.ndarray = self.get_idxs(node, scale)

    def get_idxs(self,
                 node: CoverTreeNode,
                 max_scale: int) -> np.ndarray:
        C: List[CoverTreeNode] = [node]
        idxs: Set[int] = {node.pt_idx}
        for scale in range(max_scale, -1, -1):
            C_next: List[CoverTreeNode] = sum([c.get_children(scale) for c in C], [])
            idxs.update([c.pt_idx for c in C_next])
            C = C_next
        return np.array(list(idxs), dtype=int)

    def add_child(self,
                  child: DyadicCellType) -> DyadicCellType:
        self.children.append(child)
        child.parent = self

        return self

def unique(c) -> bool:
    return Counter(c).get(True, 0) == 1

class DyadicCellTree(object):
    def __init__(self) -> None:
        self.root: DyadicCell = None
        self.num_nodes: int = 0
        self.datasets: List[np.ndarray] = list()
        self.base: float = None
        self.num_levels: int = None

    def from_covertree(self,
                       tree: CoverTree) -> "DyadicCellTree":
        self.datasets = tree.datasets
        self.base = tree.base
        self.num_levels = tree.max_scale - tree.min_scale + 1

        layer_nodes: List[CoverTreeNode] = [tree.root]
        layer_cells: List[DyadicCell] = [DyadicCell(tree.root, tree.max_scale,
                                                    tree.root.dataset_idx)]

        self.root = layer_cells[0]

        for scale in reversed(range(tree.min_scale, tree.max_scale+1)):
            next_layer_nodes: List[CoverTreeNode] = list()
            next_layer_cells: List[DyadicCell] = list()

            for parent_node, parent_cell in zip(layer_nodes, layer_cells):
                for child_node in parent_node.get_children(scale):
                    next_layer_nodes.append(child_node)

                    child_cell: DyadicCell = DyadicCell(child_node, scale-1,
                                                        child_node.dataset_idx)
                    parent_cell.add_child(child_cell)
                    next_layer_cells.append(child_cell)
                    self.num_nodes += 1

            print("number of children at scale %s: %s" % (scale, len(next_layer_nodes)))

            layer_nodes = next_layer_nodes
            layer_cells = next_layer_cells
        return self

    def check_covering_invariant(self,
                                 parent_cell: DyadicCell,
                                 child_cell: DyadicCell,
                                 scale: int) -> bool:

        covering_check: bool = True
        for parent_idx in parent_cell.pt_idxs:
            for child_idx in child_cell.pt_idxs:
                covering_check = covering_check and \
                    (((self.datasets[parent_cell.dataset_idx][parent_idx] -
                       self.datasets[child_cell.dataset_idx][child_idx]) ** 2)
                    .sum()**(1/2) <= self.base**scale)

        return covering_check

    def check_tree(self) -> None:
        layer_cells: List[DyadicCell] = [self.root]

        for level in range(self.num_levels):
            print("num pts at level %s: " % level, np.unique(np.hstack([c.pt_idxs
                for c in layer_cells])).shape[0])

            next_layer_cells: List[DyadicCell] = sum([c.children for c in layer_cells], [])

            """
            if len(next_layer_cells) > 0:
                # only check invariants while children exist

                # check nesting
                idxs_in_current_layer = np.unique(np.hstack([c.pt_idxs
                                                             for c in layer_cells]))
                idxs_in_next_layer = np.unique(np.hstack([c.pt_idxs
                                                          for c in next_layer_cells]))
                nesting: bool = np.all(np.isin(idxs_in_current_layer, idxs_in_next_layer))
                                # idxs_in_next_layer <= idxs_in_current_layer

                # check covering
                covering: bool = all(self.check_covering_invariant(q, p, q.scale)
                                     for q in layer_cells for p in next_layer_cells)

                # if not nesting:
                #     raise ValueError("ERROR: nesting fails at scale %s" % q.scale)
                # if not covering:
                #     raise ValueError("ERROR: covering fails at scale %s" % q.scale)
            """

            layer_cells = next_layer_cells


print("IMPORT LOAD LVM")

from io import BytesIO
import numpy as np
import loadfiles.datatype as data_type

# def load(filename):
#    return genfromtxt(filename, delimiter='\t')

def load_file( filename):

    print("LOAD LVM FILE")
    raw_data = open(filename).read()

    str_between_info_data = "X_Value	Untitled	Comment"

    index_start_data = raw_data.find(str_between_info_data)

    str_full_info = raw_data[0:index_start_data + str_between_info_data.__len__()]

    str_full_data = raw_data[index_start_data + str_between_info_data.__len__():raw_data.__len__()].replace(',', '.')
    finaldata = np.genfromtxt(BytesIO(str_full_data.encode()), delimiter='\t')

    thz = data_type.data_thz(finaldata, str_full_info)
    print(444)
    return thz


# coding=utf-8
# Copyright 2018-2020 EVA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pandas as pd
import unittest

from src.models.inference.outcome import Outcome

class OutcomeTest(unittest.TestCase):
    def test_get_attribute_should_return_new_outcome(self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        expected = Outcome(data[["A"]], identifier='A')
        self.assertEqual(outcome.A, expected)

    def test_equals_should_return_true_if_alreast_one_value_equal(self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        self.assertTrue(getattr(outcome, 'A') == 1)

    def test_equals_should_return_false_frame_if_no_value_in_data_frame_equal(
            self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        self.assertFalse(outcome.A == 4)

    def test_gt_should_return_false_if_no_value_gt_given(
            self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        self.assertFalse(outcome.A > 4)

    def test_gt_should_return_true_if_atleast_one_value_gt_given(
            self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        self.assertTrue(outcome.A > 2)

    def test_gte_should_return_false_if_no_value_gte_given(
            self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        self.assertFalse(outcome.A >= 4)

    def test_gte_should_return_true_if_atleast_one_value_gte_given(
            self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        self.assertTrue(outcome.A >= 3)

    def test_lt_should_return_false_if_no_value_lt_given(
            self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        self.assertFalse(outcome.A < 0)

    def test_lt_should_return_true_if_atleast_one_value_lt_given(
            self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        self.assertTrue(outcome.A < 4)

    def test_lte_should_return_false_if_no_value_lte_given(
            self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        self.assertFalse(outcome.A < 1)

    def test_lte_should_return_true_if_atleast_one_value_lte_given(
            self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        self.assertTrue(outcome.A <= 3)

    def test_ne_should_return_false_if_value_present_atleast_once(
            self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        self.assertFalse(outcome.A != 1)

    def test_ne_should_return_true_if_value_not_present(
            self):
        data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
        outcome = Outcome(data, identifier='A')
        self.assertTrue(outcome != 4)


# coding:utf8
# 引入flask
from flask import Flask, render_template, request
import json
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired
from flask_sqlalchemy import SQLAlchemy

from sqlalchemy import text

import MySQLdb
# 创建
app = Flask(__name__)

# mysql+pymysql://账号:密码@127.0.0.1:3306/数据库
app.config[
    "SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:123456@127.0.0.1:3306/movie"
# # 跟踪修改
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)

db2 = MySQLdb.connect('127.0.0.1', 'root', '123456', 'movie', 3306, charset='utf8')

app.secret_key = 'PreByter'

# 定义路由
@app.route('/', methods=['GET', 'POST'])
def index():
    # print('666')
    # sql = text("SELECT * FROM `country`")
    # res = db.engine.execute(sql)
    # for e in res:
    #     app.logger.info(e)
    return '
app.py
'

@app.route('/country', methods=['GET', 'POST'])
def get_log():
    sql = text("SELECT * FROM `admin`")
    res = db.engine.execute(sql)
    # cursor = db2.cursor()
    # cursor.execute("SELECT * FROM `admin`")
    # res = cursor.fetchall()
    data = {"msg": 'success', "state": '1', 'data': []}
    print(res)
    app.logger.info(res)
    # for e in res:
    #     data["data"].append({'id': e[0], 'name': e[1]})
    # return json.dumps(res)
    # return data
    return '
country
'

@app.route('/city', methods=['GET', 'POST'])
def get_city():
    sql = text("SELECT COLUMN_NAME,column_comment from INFORMATION_SCHEMA.Columns where `role`")

    res = db.engine.execute(sql)
    app.logger.info(res)
    # for e in res:
    #     app.logger.info(e)
    return '
city
'

@app.errorhandler(404)
def page_not_found(error):
    app.logger.error(error)
    return '
404
'

# 开启服务
if __name__ == "__main__":
    # 删除表

    # 创建表
    # db.create_all()
    # host 可以更改运行端口
    app.run(host='127.0.0.1', debug=True)


# %% [markdown]
# # Problem Set 1
# %%
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio.SeqUtils import GC
import numpy as np

# %% Code Setup
my_seq = Seq("AGTACGCAACTGGTA")
my_seq2 = Seq("AGTACACGCGCGCTGGTA", IUPAC.unambiguous_dna)
my_prot = Seq("AGTACACTGGTA", IUPAC.protein)

# %% [markdown]
# Complete the codes below

# %%
# my_seq's variable type, my_prot's variable type
type_of_my_seq = type(my_seq)
type_of_my_prot = type(my_prot)

# %%
# Join sequences my_seq and my_seq2 into my_seq3.
my_seq3 = my_seq + my_seq2

# %%
# Find the number of base T in my_seq
t_myseq = my_seq.count('T')

# %%
# Find the GC content of a DNA given the function GC
gc_myseq = GC(my_seq)

# %%
# Find the geometric mean of base A in my_seq and my_seq2
gmean = (my_seq.count('A') * my_seq2.count('A'))**0.5

# %%
# Find the abundance of A, T, C and G in my_seq. Report the answer as a list of abundances.
abundance_my_seq = [my_seq.count('A'), my_seq.count('T'), my_seq.count('C'),my_seq.count('G')]

# %%
# Find the abundance of A, T, C and G in my_seq. Report the answer as a numpy array of abundances.
abundance_my_seq_array = np.array(abundance_my_seq)

# %%
# Find the abundance of A, T, C and G in my_seq. Report the answer as a dictionary of abundances.
abundance_my_seq_dict = {'A':my_seq.count('A'),
                        'T':my_seq.count('T'),
                        'C':my_seq.count('C'),
                        'G':my_seq.count('G')}

# %%
# Transcribe my_seq to mRNA using the method .reverse_complement()
transcript_my_seq = my_seq.reverse_complement()

# %%
# Flip 5' and 3' end of my_seq
flipped_my_seq = my_seq[::-1]

# %% [markdown]
# # Troubleshooting exercise
# The following code convert a DNA sequence to its protein sequence and displays the consensus sequence. Can you identify the errors and correct it?

# %%
# Import additional libraries
from Bio.Blast import NCBIWWW, NCBIXML
from Bio import SeqIO
import logomaker
import matplotlib.pyplot as plt
import pandas as pd

# %% [markdown]
# Perform a blast search of accession number [AY527219](https://www.ncbi.nlm.nih.gov/nuccore/AY527219). Saves the blast results in `blast_results.xml`

# %%
#result_handle = NCBIWWW.qblast("blastn", "nt", "AY527219")
#with open("blast_results.xml", "w") as out_handle:
#    out_handle.write(result_handle.read())

#result_handle.close()

# %% [markdown]
# Load blast results.
#
# **Spot the error!**

# %%
result_handle = open('blast_results.xml') # Correct answer
blast_record = NCBIXML.read(result_handle)

# %% [markdown]
# The following code snippet extract the first 120 base pairs from the matching genes.
# **Spot the error!**

# %%
# Extract first 120 bases of matching sequences.
base_pos1 = 0 # Correct answer
base_pos2 = 120 # Correct answer
matches = []
for alignment in blast_record.alignments:
    for hsp in alignment.hsps:
        matches.append(hsp.sbjct[base_pos1:base_pos2])

pd.DataFrame(matches)

# %% [markdown]
# Translate the codon to their respective amino acids.

# %%
# Convert gene to protein sequences
matches_prot = []
expected_length_primary_sequence = 40 # Correct answer
for sequence in matches:
    pri_seq = Seq(sequence).translate() # Translate sequence
    try:
        assert len(pri_seq) == expected_length_primary_sequence
    except:
        raise AssertionError('The protein length obtained is different from the expected length. Change accordingly')
    matches_prot.append(list(pri_seq))

# Check if
matches_df = pd.DataFrame(matches_prot)
matches_df

# %% [markdown]
# Count the number of each type of amino acid at each position.

# %%
prot_aa = []
for ind, pri_seq in matches_df.iteritems():
    aa_abundance = pri_seq.value_counts()
    prot_aa.append(aa_abundance)

prot_df = pd.DataFrame(prot_aa).fillna(0)
prot_df

# %%
# Display Consensus Sequence
crp_logo = logomaker.Logo(prot_df, figsize=(10,2), color_scheme='chemistry')

# %% [markdown]
# Identify the error.

# %%
# style and show figure
crp_logo.ax.set_xlabel('Percentage')
crp_logo.ax.set_title('Primary consensus sequence')
crp_logo.fig
crp_logo.fig.show()
# %%


from django.shortcuts import render

from django.utils import timezone
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.utils import timezone

from .models import Doctors, WorkTime, Entries

def index(request):
    doctors_list = Doctors.objects.order_by('doctor_name')[:]
    work_time_list = WorkTime.objects.order_by('id')[:]
    entries_list = Entries.objects.filter(entry_date__gte=timezone.now())[:]
    template = loader.get_template('entries/index.html')
    context = RequestContext(request, {
        'doctors_list': doctors_list,
        'work_time_list': work_time_list,
        'entries_list' : entries_list,
    })
    return HttpResponse(template.render(context))

def save(request):
    if request.method == 'POST':
        entry = Entries(
            create_date = timezone.now(),
            entry_time = WorkTime.objects.get(id=request.POST['entry_time']),
            entry_date = request.POST['entry_date'],
            doctor_name = Doctors.objects.get(id=request.POST['doctor_name']),
            client_name = request.POST['client_name'],
        )
        entry.save()
        return HttpResponse('запись сохранена...')
    else:
        return HttpResponse('запись не сохранена...')


from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
import uvicorn

from . import predict, records, visualize

description = """
An API for accessing predictive data and visualizations for [Family Promise of Spokane]\
(https://www.familypromiseofspokane.org).

To use these interactive docs:
- Click on an endpoint below
- Click the **Try it out** button
- Edit the Request body or any parameters
- Click the **Execute** button
- Scroll down to see the Server response Code & Details
"""

app = FastAPI(
    title='DS API',
    description=description,
    docs_url='/',
)

app.include_router(records.router, tags=['Database'])
app.include_router(predict.router, tags=['Machine Learning'])
app.include_router(visualize.router, tags=['Visualization'])

app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=['*'],
    allow_headers=['*'],
)

if __name__ == '__main__':
    uvicorn.run(app)


"""
Unit tests for Monitor class.
"""
from __future__ import absolute_import
import os
import subprocess
import unittest
import sqlite3
import desc.monitor

class MonitorTestCase(unittest.TestCase):

    def setUp(self):
        """
        Create a test dataset as an sqlite db for exercising
        monitor class.
        """
        self._test_file = 'my_test_sqlite.db'
        connection = sqlite3.connect(self._test_file)
        connection.execute("create table CcdVisit (ccdVisitId, project)")
        self._obsHistIDs = (359, 123, 59025, 430201, 430201)
        self._projectID = "'Twinkles Run1.1'"
        for obsHistID in self._obsHistIDs:
            connection.execute("""insert into CcdVisit values
                                  (%i, %s)""" % (obsHistID, self._projectID))
        other_projectID = "'Twinkles Run3'"
        other_obsHistIDs = (340298, 2342, 1550)
        for obsHistID in other_obsHistIDs:
            connection.execute("""insert into CcdVisit values
                                  (%i, %s)""" % (obsHistID, other_projectID))
        connection.commit()
        connection.close()

        self.dbConn = desc.monitor.DBInterface(database=self._test_file,
                                               host=None, port=None,
                                               driver='sqlite')
        self.monitor = desc.monitor.Monitor(self.dbConn)

    def tearDown(self):
        "Clean up test file."
        os.remove(self._test_file)

    def test_db_load(self):
        """
        Simple test that monitor loads with active dbConnection.
        """
        self.assertEqual(self.monitor.num_visits, 5)

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


import sv
import vtk

def convert_ug_to_polydata(mesh):
    '''
    Convert mesh to polydata.
    '''
    geometry_filter = vtk.vtkGeometryFilter()
    geometry_filter.SetInputData(mesh)
    geometry_filter.Update()
    mesh_polydata = geometry_filter.GetOutput()

    triangle_filter = vtk.vtkTriangleFilter()
    triangle_filter.SetInputData(mesh_polydata)
    triangle_filter.Update()
    polydata = triangle_filter.GetOutput()
    return polydata

def create_contour_geometry(renderer, contour):
    """
    Create geometry for the contour points and control points.
    """
    coords = contour.get_contour_points()
    num_pts = len(coords)

    ## Create contour geometry points and line connectivity.
    #
    points = vtk.vtkPoints()
    points.SetNumberOfPoints(num_pts)
    lines = vtk.vtkCellArray()
    lines.InsertNextCell(num_pts+1)
    n = 0
    for pt in coords:
        points.SetPoint(n, pt[0], pt[1], pt[2])
        lines.InsertCellPoint(n)
        n += 1
    #_for pt in coords
    lines.InsertCellPoint(0)

    geom = vtk.vtkPolyData()
    geom.SetPoints(points)
    geom.SetLines(lines)
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(geom)
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetLineWidth(2.0)
    actor.GetProperty().SetColor(0.0, 0.6, 0.0)
    renderer.AddActor(actor)

    ## Add center point.
    #
    center = contour.get_center()
    num_pts = 1
    points = vtk.vtkPoints()
    vertices = vtk.vtkCellArray()
    pid = points.InsertNextPoint(center)
    vertices.InsertNextCell(1)
    vertices.InsertCellPoint(pid)
    points_pd = vtk.vtkPolyData()
    points_pd.SetPoints(points)
    points_pd.SetVerts(vertices)
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(points_pd)
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetColor(1.0, 0.0, 0.0)
    actor.GetProperty().SetPointSize(5)
    renderer.AddActor(actor)

    ## Add control points.
    #
    coords = contour.get_control_points()
    num_pts = len(coords)
    points = vtk.vtkPoints()
    vertices = vtk.vtkCellArray()
    for pt in coords:
        pid = points.InsertNextPoint(pt)
        vertices.InsertNextCell(1)
        vertices.InsertCellPoint(pid)
    #_for pt in coords
    points_pd = vtk.vtkPolyData()
    points_pd.SetPoints(points)
    points_pd.SetVerts(vertices)
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(points_pd)
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetColor(1.0, 0.0, 0.0)
    actor.GetProperty().SetPointSize(5)
    # renderer.AddActor(actor)

def display(renderer_win):
    interactor = vtk.vtkRenderWindowInteractor()
    interactor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
    interactor.SetRenderWindow(renderer_win)
    interactor.Start()

def add_geom(renderer, polydata, color=[1.0, 1.0, 1.0], wire=False, edges=False):
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputData(polydata)
    mapper.SetScalarVisibility(False)
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetColor(color[0], color[1], color[2])
    actor.GetProperty().SetPointSize(5)

    if wire:
        actor.GetProperty().SetRepresentationToWireframe()
        actor.GetProperty().SetLineWidth(1.0)

    if edges:
        actor.GetProperty().EdgeVisibilityOn();

    renderer.AddActor(actor)

def add_sphere(renderer, center, radius, color=[1.0, 1.0, 1.0], wire=False):
    sphere = vtk.vtkSphereSource()
    sphere.SetCenter(center[0], center[1], center[2])
    sphere.SetRadius(radius)
    sphere.SetPhiResolution(16)
    sphere.SetThetaResolution(16)
    sphere.Update()
    sphere_pd = sphere.GetOutput()
    add_geom(renderer, sphere_pd, color, wire)

def init_graphics(win_width, win_height):
    '''
    Create renderer and graphics window.
    '''
    renderer = vtk.vtkRenderer()
    renderer_win = vtk.vtkRenderWindow()
    renderer_win.AddRenderer(renderer)
    renderer.SetBackground(0.8, 0.8, 0.8)
    renderer_win.SetSize(win_width, win_height)
    renderer_win.Render()
    renderer_win.SetWindowName("SV Meshing")
    return renderer, renderer_win


# -*- coding: utf-8 -*-
"""
Created on Tue Jul 21 22:13:38 2020

@author: yash1
"""

# Loading required libraries
import json
import numpy as np
import cv2
from matplotlib import pyplot as plt
from read_roi import read_roi_file
from read_roi import read_roi_zip
from PIL import Image, ImageDraw
import itertools
from scipy.ndimage.measurements import label
import skimage.measure as sk
import pandas as pd

# Opening annotated json file from Qupath
annots_jash = open('VAN0008Jash.json')
annots_leah = open('VAN0008Leah.json')

# convert into dictionary object
data_jash = json.load(annots_jash)
data_leah = json.load(annots_leah)

# saving all coordinates in a list
coords_jash=[]
coords_leah=[]

for i in data_jash:
    coords_jash.extend(i['geometry']['coordinates'])

for i in data_leah:
    coords_leah.extend(i['geometry']['coordinates'])

# Opening annotated .roi object from ImageJ
# to handle decompression bomb
Image.MAX_IMAGE_PIXELS = None

#reading roi files
rois_sumeet = read_roi_zip("VAN0008Sumeet.zip")
rois_yash = read_roi_zip("VAN0008Yash.zip")

# Read corresponding images
img = cv2.imread('VAN0008-RK-403-100-PAS_registered.ome.jpg')

# Visualize glom size of Jash annotation
# Create binary mask array while iterating over the glomeruli points
jash_array = np.zeros(img.shape[:2])
leah_array = np.zeros(img.shape[:2])

for i in coords_jash:
    pts_jash = np.array(i, np.int32)
    pts_jash = pts_jash.reshape((-1,1,2))
    cv2.fillPoly(jash_array,[pts_jash],1)

for i in coords_leah:
    pts_leah = np.array(i, np.int32)
    pts_leah = pts_leah.reshape((-1,1,2))
    cv2.fillPoly(leah_array,[pts_leah],1)

# Get properties of Jash annotation of Glomeruli
jash_annots = (jash_array*255).astype('uint8')
jash_label = sk.label(jash_annots)
jash_prop = sk.regionprops(label_image=jash_label)

jash_table = pd.DataFrame(index = range(len(jash_prop)), columns = ['area','major_axis','minor_axis'])
for i in range(len(jash_prop)):
    jash_table.iloc[i,0] = jash_prop[i].area
    jash_table.iloc[i,1] = jash_prop[i].major_axis_length
    jash_table.iloc[i,2] = jash_prop[i].minor_axis_length

jash_table1 = jash_table.copy()

jash_table1['major_axis'] = jash_table['major_axis'].astype('float64')
jash_table1['minor_axis'] = jash_table['minor_axis'].astype('float64')
jash_table1['area'] = jash_table['area'].astype('float64')

# Histogram plots
plt.hist(jash_table['major_axis'],bins=20)
plt.savefig('./Viz_jash/hist_max_len.jpg')

plt.hist(jash_table['minor_axis'],bins=20)
plt.savefig('./Viz_jash/hist_min_len.jpg')

plt.hist(jash_table['area'],bins=20)
plt.savefig('./Viz_jash/hist_area.jpg')

# Box plots
plt.boxplot(jash_table['major_axis'])
plt.savefig('./Viz_jash/box_max_len.jpg')

plt.boxplot(jash_table['minor_axis'])
plt.savefig('./Viz_jash/box_min_len.jpg')

plt.boxplot(jash_table['area'])
plt.savefig('./Viz_jash/box_area.jpg')

# Get properties of Leah annotation of Glomeruli
leah_annots = (leah_array*255).astype('uint8')
leah_label = sk.label(leah_annots)
leah_prop = sk.regionprops(label_image=leah_label)

leah_table = pd.DataFrame(index = range(len(leah_prop)), columns = ['area','major_axis','minor_axis'])
for i in range(len(leah_prop)):
    leah_table.iloc[i,0] = leah_prop[i].area
    leah_table.iloc[i,1] = leah_prop[i].major_axis_length
    leah_table.iloc[i,2] = leah_prop[i].minor_axis_length

leah_table1 = leah_table.copy()

leah_table1['major_axis'] = leah_table['major_axis'].astype('float64')
leah_table1['minor_axis'] = leah_table['minor_axis'].astype('float64')
leah_table1['area'] = leah_table['area'].astype('float64')

leah_table1.describe()

# Histogram plots
plt.hist(leah_table['major_axis'],bins=20)
plt.savefig('./Viz_leah/hist_max_len.jpg')

plt.hist(leah_table['minor_axis'],bins=20)
plt.savefig('./Viz_leah/hist_min_len.jpg')

plt.hist(leah_table['area'],bins=20)
plt.savefig('./Viz_leah/hist_area.jpg')

# Box plots
plt.boxplot(leah_table['major_axis'])
plt.savefig('./Viz_leah/box_max_len.jpg')

plt.boxplot(leah_table['minor_axis'])
plt.savefig('./Viz_leah/box_min_len.jpg')

plt.boxplot(leah_table['area'])
plt.savefig('./Viz_leah/box_area.jpg')

# Visualize glom size of Yash annotation
# Create binary mask array while iterating over the glomeruli points
yash_array = np.zeros(img.shape[:2])
sumeet_array = np.zeros(img.shape[:2])

yash_img = Image.fromarray(yash_array)

yash_draw = ImageDraw.Draw(yash_img)

for key in rois_yash.keys():
    left = rois_yash[key]['left']
    top = rois_yash[key]['top']
    width = rois_yash[key]['width']
    height = rois_yash[key]['height']

    centreX = int(left + (width/2))
    centreY = int(top + (height/2))
    widthX = int(width/2)
    heightY = int(height/2)

    bbox = (centreX-widthX,centreY-heightY,centreX+widthX,centreY+heightY)
    yash_draw.ellipse(bbox, fill='white')

yash_array1 = np.array(yash_img)
yash_array1[yash_array1 > 0] = 1

sumeet_img = Image.fromarray(sumeet_array)

sumeet_draw = ImageDraw.Draw(sumeet_img)

for key in rois_sumeet.keys():
    left = rois_sumeet[key]['left']
    top = rois_sumeet[key]['top']
    width = rois_sumeet[key]['width']
    height = rois_sumeet[key]['height']

    centreX = int(left + (width/2))
    centreY = int(top + (height/2))
    widthX = int(width/2)
    heightY = int(height/2)

    bbox = (centreX-widthX,centreY-heightY,centreX+widthX,centreY+heightY)
    sumeet_draw.ellipse(bbox, fill='white')

sumeet_array1 = np.array(sumeet_img)
sumeet_array1[sumeet_array1 > 0] = 1

# Get properties of Yash annotation of Glomeruli
yash_annots = (yash_array1*255).astype('uint8')
yash_label = sk.label(yash_annots)
yash_prop = sk.regionprops(label_image=yash_label)

yash_table = pd.DataFrame(index = range(len(yash_prop)), columns = ['area','major_axis','minor_axis'])
for i in range(len(yash_prop)):
    yash_table.iloc[i,0] = yash_prop[i].area
    yash_table.iloc[i,1] = yash_prop[i].major_axis_length
    yash_table.iloc[i,2] = yash_prop[i].minor_axis_length

yash_table1 = yash_table.copy()

yash_table1['major_axis'] = yash_table['major_axis'].astype('float64')
yash_table1['minor_axis'] = yash_table['minor_axis'].astype('float64')
yash_table1['area'] = yash_table['area'].astype('float64')

yash_table1.describe()

# Histogram plots
plt.hist(yash_table['major_axis'],bins=20)
plt.savefig('./Viz_yash/hist_max_len.jpg')

plt.hist(yash_table['minor_axis'],bins=20)
plt.savefig('./Viz_yash/hist_min_len.jpg')

plt.hist(yash_table['area'],bins=20)
plt.savefig('./Viz_yash/hist_area.jpg')

# Box plots
plt.boxplot(yash_table['major_axis'])
plt.savefig('./Viz_yash/box_max_len.jpg')

plt.boxplot(yash_table['minor_axis'])
plt.savefig('./Viz_yash/box_min_len.jpg')

plt.boxplot(yash_table['area'])
plt.savefig('./Viz_yash/box_area.jpg')

# Get properties of Sumeet annotation of Glomeruli
sumeet_annots = (sumeet_array1*255).astype('uint8')
sumeet_label = sk.label(sumeet_annots)
sumeet_prop = sk.regionprops(label_image=sumeet_label)

sumeet_table = pd.DataFrame(index = range(len(sumeet_prop)), columns = ['area','major_axis','minor_axis'])
for i in range(len(sumeet_prop)):
    sumeet_table.iloc[i,0] = sumeet_prop[i].area
    sumeet_table.iloc[i,1] = sumeet_prop[i].major_axis_length
    sumeet_table.iloc[i,2] = sumeet_prop[i].minor_axis_length

sumeet_table1 = sumeet_table.copy()

sumeet_table1['major_axis'] = sumeet_table['major_axis'].astype('float64')
sumeet_table1['minor_axis'] = sumeet_table['minor_axis'].astype('float64')
sumeet_table1['area'] = sumeet_table['area'].astype('float64')

sumeet_table1.describe()

# Histogram plots
plt.hist(sumeet_table['major_axis'],bins=20)
plt.savefig('./Viz_sumeet/hist_max_len.jpg')

plt.hist(sumeet_table['minor_axis'],bins=20)
plt.savefig('./Viz_sumeet/hist_min_len.jpg')

plt.hist(sumeet_table['area'],bins=20)
plt.savefig('./Viz_sumeet/hist_area.jpg')

# Box plots
plt.boxplot(sumeet_table['major_axis'])
plt.savefig('./Viz_sumeet/box_max_len.jpg')

plt.boxplot(sumeet_table['minor_axis'])
plt.savefig('./Viz_sumeet/box_min_len.jpg')

plt.boxplot(sumeet_table['area'])
plt.savefig('./Viz_sumeet/box_area.jpg')

# Reading predicted binary mask for VAN0008
img_pred = cv2.imread('VAN0008_pred.jpg',cv2.IMREAD_GRAYSCALE)
img_pred[img_pred <=246 ] = 0
img_pred[img_pred > 246 ] = 1

# Get properties of predicted annotation
pred_annots = (img_pred*255).astype('uint8')
pred_label = sk.label(pred_annots)
pred_prop = sk.regionprops(label_image=pred_label)

pred_table = pd.DataFrame(index = range(len(pred_prop)), columns = ['area','major_axis','minor_axis'])
for i in range(len(pred_prop)):
    pred_table.iloc[i,0] = pred_prop[i].area
    pred_table.iloc[i,1] = pred_prop[i].major_axis_length
    pred_table.iloc[i,2] = pred_prop[i].minor_axis_length

# Histogram plots
plt.hist(pred_table['major_axis'],bins=20)
plt.savefig('./Viz_preds/hist_max_len.jpg')

plt.hist(pred_table['minor_axis'],bins=20)
plt.savefig('./Viz_preds/hist_min_len.jpg')

plt.hist(pred_table['area'],bins=20)
plt.savefig('./Viz_preds/hist_area.jpg')

# Box plots
plt.boxplot(pred_table['major_axis'])
plt.savefig('./Viz_preds/box_max_len.jpg')

plt.boxplot(pred_table['minor_axis'])
plt.savefig('./Viz_preds/box_min_len.jpg')

plt.boxplot(pred_table['area'])
plt.savefig('./Viz_preds/box_area.jpg')


def add(a,b):
	print(a+b)
def sub(a,b):
	print(a-b)
def mul(a,b):
	print(a*b)
def div(a,b):
	print(a//b)

menu = '''
0: '덧셈'
1: '뺄셈'
2: '곱셈'
3: '나눗셈'
4: '프로그램 종료'
'''

while True:
	print(menu)
	number = input('숫자를 고르세요:')
	if int(number) == 0:
		a = int(input('첫번째 숫자:'))
		b = int(input('두번째 숫자:'))
		add(a,b)
	elif int(number) == 1:
		a = int(input('첫번째 숫자:'))
		b = int(input('두번째 숫자:'))
		sub(a,b)
	elif int(number) == 2:
		a = int(input('첫번째 숫자:'))
		b = int(input('두번째 숫자:'))
		mul(a,b)
	elif int(number) == 3:
		a = int(input('첫번째 숫자:'))
		b = int(input('두번째 숫자:'))
		div(a,b)
	elif int(number) == 4:
		print('good bye')
		break
	else:
		print('잘못된 입력입니다.')


import socket

HOST = '0.0.0.0'
PORT = 5000

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
data = s.recv(1024)
s.close()

with open('f.mp4', 'wb') as fh:
     fh.write(data)


import os
import logging

from types import GeneratorType
from itertools import chain

from nose.tools import *

from docx import api
from docx import text

logger = logging.getLogger('docx_converter.tests.docx_run')

DOC = api.Document(os.path.join(
    os.path.dirname(__file__),
    'data/run.docx'
))

def test_get_elements_type():
    result = DOC.paragraphs[0].runs[0].get_elements()
    assert_equals(type(result), GeneratorType)
    assert_equals([type(e) for e in result], [text.Text])

def test_element_classes():
    result = set()
    for p in DOC.paragraphs:
        for r in p.runs:
            for el in r.get_elements():
                result.add(type(el))
    expected = set([text.Text, text.LineBreak, text.EndnoteReference, text.FootnoteReference, text.Tab])
    assert_equals(result, expected)


#Dictionary and counter in Python to find winner of election

  #input =['john','johnny','jackie','johnny',
   #         'john','jackie','jamie','jamie',
    #        'john','johnny','jamie','johnny',
    #        'john']
    #winner(input)
from collections import Counter

def election(names):

    # convert list of candidates into dictionary
    # output will be likes candidates = {'A':2, 'B':4}
    votes = Counter(names)

    # create another dictionary and it's key will
    # be count of votes values will be name of
    # candidates
    winner = {}

    for value in votes.values():

        # initialize empty list to each key to
        # insert candidate names having same
        # number of votes
        winner[value] = []

    for (key,value) in votes.items():
        winner[value].append(key)

    # sort keys in descending order to get maximum
    # value of votes
    maxVote = sorted(winner.keys(),reverse=True)[0]

    # check if more than 1 candidates have same
    # number of votes. If yes, then sort the list
    # first and print first element
    if len(winner[maxVote])>1:
        print (sorted(winner[maxVote])[0])
    else:
        print (winner[maxVote][0])

# Driver program
if __name__ == "__main__":
    names =['nil','nilesh','nil','nilesh','nil','kalpesh','tejas','kishan']
    election(names)


# -*- coding: utf-8 -*-
import scrapy
import re
import hashlib
from datetime import datetime
from amazones.items import AmazonesItem
from mysqlconn import mysqldbclass

headers = {
            'authority':'www.amazon.es',
    'method':'GET',
    'path':'/s?k=xyz',
    'scheme':'https',
    'accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
    'accept-encoding':'gzip, deflate, br',
    'accept-language':'en-GB,en;q=0.9,en-US;q=0.8,ar;q=0.7,es;q=0.6,ru;q=0.5,ja;q=0.4',
    'cache-control':'no-cache',
    'pragma':'no-cache',
    'sec-fetch-mode':'navigate',
    'sec-fetch-site':'none',
    'sec-fetch-user':'?1',
    'upgrade-insecure-requests':'1',
    'user-agent':'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36',
        }

class amazonespider(scrapy.Spider):
    name = "amazonesoffers"

    def start_requests(self):
        self.dbclass = mysqldbclass()
        asinscodes, self.sellersDict = self.dbclass.mysqldbextract('priceRecord')
        for asin in asinscodes:
            offerurl = 'https://www.amazon.es/gp/offer-listing/' + asin
            yield scrapy.Request(offerurl, callback=self.parse, headers = headers, meta = {'asin': asin})

    def parse(self, response):
        def url_hash(url):
            if not url.startswith('http'):
                return False
            url = url.encode('utf-8')
            return hashlib.md5(url).hexdigest()
        item = AmazonesItem()
        asin = response.meta['asin']
        yield scrapy.Request('https://www.amazon.es/dp/' + asin , callback=self.parse_images, headers = headers, meta = {'asin': asin})
        item['crw_slr_prd_id'] = asin
        item['crw_product_name'] = response.xpath('//title[contains(.,"Amazon.es: Opciones de compra")]/text()').extract_first().replace('Amazon.es: Opciones de compra: ','')
        item['crw_product_brand'] = response.xpath('//div [@id="olpProductByline"]/text()').extract_first()
        item['crw_product_brand'] = re.sub(r'\sde ','',item['crw_product_brand'])
        item['crw_image_url'] = response.xpath('//*[@id="olpProductImage"]//img/@src').extract_first()
        # extracting all offers
        offerslist = response.xpath('//*[@class="a-row a-spacing-mini olpOffer"]')
        for offer in offerslist:
            item['crw_pvr_price'] = ''
            item['crw_sale_price'] = offer.xpath('.//*[@class="a-offscreen"]/text()').extract_first().split()[-1].replace(',','.')
            try:
                item['shippingprice'] = offer.xpath('.//*[@class="olpShippingPrice"]/text()').extract_first().split()[-1].replace(',','.')
            except:
                item['shippingprice'] = offer.xpath('.//*[@class="olpShippingPrice"]/text()').extract_first()
            item['Condition'] = offer.xpath('.//*[@class="a-size-medium olpCondition a-text-bold"]/text()').extract_first()
            item['sellername'] = offer.xpath('.//*[@class="a-spacing-none olpSellerName"]//a/text() | .//*[@class="a-spacing-none olpSellerName"]/img/@alt').extract_first()
            if item['sellername'] == 'Amazon.es':
                item['sellerurl'] = 'Amazon.es'
            else:
                item['sellerurl'] = 'https://www.amazon.es' + offer.xpath('.//*[@class="a-spacing-none olpSellerName"]//a/@href').extract_first()
            if item['sellername'] == 'Amazon.es':
                item['crw_product_url'] = 'https://www.amazon.es/dp/' + asin + '?m='  + 'A1AT7YVPFBWXBL'
            else:
                item['crw_product_url'] = 'https://www.amazon.es/dp/' + asin + '?m='  + item['sellerurl'].split('seller=')[-1]
            item['crw_product_url_hash'] = url_hash(item['crw_product_url'])
            if item['sellername'] == 'Amazon.es':
                item['slr_mkt_id'] = 'A1RKKUPIHCS9HS'
            else:
                item['slr_mkt_id'] = item['sellerurl'].split('seller=')[-1]

            item['creation_date'] = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            item['offerpage'] = response.request.url
            item['recordtype'] = 'priceRecord'
            item['crw_mkt_id'] = 3
            item['process_name'] = self.name.upper()
            if item['slr_mkt_id'] in self.sellersDict:
                #this seller is in our DB already
                print ("this seller is in our DB already", item['slr_mkt_id'])
                item['crw_seller_id'] = self.sellersDict[item['slr_mkt_id']]
            else:
                print ("this seller is NOT in our DB!", item['slr_mkt_id'])
                item['crw_seller_id'] = self.dbclass.seller_api(item['slr_mkt_id'],item['sellername'],item['sellerurl'])
                self.sellersDict.update({item['slr_mkt_id']:item['crw_seller_id']})

            #cleaning extracted text from extra whitespaces
            for key, value in item.items():
                try:
                    item[key] = re.sub(r'\n+', ' ', value)
                    item[key] = re.sub(r'\t+', ' ', value)
                    item[key] = re.sub(r'\s+', ' ', value)
                    item[key] = item[key].strip()
                except:
                    pass

            yield item
        #crawling next pages
        nextpage = response.xpath('//*[@class="a-last"]/a/@href').extract_first()
        if nextpage:
            yield scrapy.Request( 'https://www.amazon.es' + nextpage,callback=self.parse, headers = headers, meta = {'asin': asin})

    def parse_images(self, response):
        #add ean, sku to amazon products
        # dimension
        # weight

        asin = response.request.url.split('/')[-1]
        skudraft = response.xpath('//b[contains(text(),"Referencia del fabricante")]/following-sibling::text()').extract_first()
        eandraft = response.xpath('//b[contains(text(),"de modelo del producto")]/following-sibling::text()').extract_first()
        print ('skudraft')
        print (skudraft)
        print ('eandraft')
        print (eandraft)
        print ('*'*50)
        print ('&'*50)
        if eandraft:
            try:
                eanint = int(eandraft.strip())
                if len(eandraft.strip()) == 13:
                    ean =eandraft.strip()
                else:
                    if skudraft is None:
                        sku = eandraft.strip()
                        ean = None
                    else:
                        ean = eandraft.strip()
            except:

                if skudraft is None:
                    sku = eandraft.strip()
                    ean = None
                else:
                    ean = eandraft.strip()
        else:
            ean = None

        if skudraft:
            try:
                skuint = int(skudraft.strip())
                if len(skudraft.strip()) == 13:
                    ean = skudraft.strip()
                    if sku is not None:
                        sku = None
                else:
                    sku = skudraft.strip()
            except:
                sku = skudraft.strip()
        else:
            if 'sku' not in locals():
                sku = None
        print (eandraft,'eandraft')
        print (ean,'ean')
        print (skudraft, 'skudraft')
        print (sku, 'sku')
        print ('*'*50)
        print ('&'*50)
        dimensionsdraft = response.xpath('//b[contains(text(),"Dimensiones del producto")]/following-sibling::text()').extract_first()
        try:
            dimensions = dimensionsdraft.split(' ; ')[0]
        except:
            dimensions = None
        try:
            weight = dimensionsdraft.split(' ; ')[-1]
        except:
            weight = None
        if weight:
            if 'x' in weight:
                dimensions = weight
                weight = None
        script = response.xpath('//*[@id="imageBlock_feature_div"]/script/text()').extract_first()
        images = re.findall('hiRes":"([^"]*)',script)
        if len(images) > 0:
            images = ';'.join(images)
            self.dbclass.save_images(asin, images,sku,ean,dimensions,weight)
        else:
            images = re.findall('large":"([^"]*)',script)
            if len(images) > 0:
                images = ';'.join(images)
                self.dbclass.save_images(asin, images,sku,ean,dimensions,weight)

Scrapy project with mysql and mongodb pipeline/amazones/spiders/offers.py


from collections import deque

n, m, t = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
rotations = [list(map(int, input().split())) for _ in range(t)]

def rotate(ls, dir, k):
    n = len(ls)
    if dir == 0:
        ls = ls[n-k:] + ls[:n-k]
    if dir == 1:
        ls = ls[k:] + ls[:k]
    return ls

def erase(x, y):
    global visited
    global a
    dx = [1, -1, 0, 0]
    dy = [0, 0, 1, -1]
    q = deque()

    sx, sy = x, y
    q.append((x, y))
    ls = []
    while q:
        x, y = q.popleft()
        for k in range(4):
            nx, ny = x + dx[k], (y+dy[k])%m #여기 나누기 4로 했었음... 열길이 m으로 했어야함
            if nx<0 or nx>=n:
                continue
            if a[nx][ny] == a[x][y] and not visited[nx][ny]:
                ls.append((nx, ny))
                visited[nx][ny] = True
                q.append((nx, ny))
    if ls:
        for i, j in ls:
            a[i][j] = 0
        a[sx][sy] = 0
        return True
    else:
        return False

for ro in rotations:
    x, dir, k = ro
    for i in range(x-1, n, x):
        a[i] = rotate(a[i], dir, k)
    #다돌린후에
    visited = [[False] * m for _ in range(n)]
    erased = False
    empty = True
    for i in range(n):
        for j in range(m):
            if a[i][j] != 0 and not visited[i][j]:
                empty = False
                visited[i][j] = True
                if erase(i, j):
                    erased = True #i,j 탐색하면서 어느 한곳에서라도 지워진게 있다면 True처리

    if not erased and not empty: #원판에 수가 남아있는데 지워지지 않은 경우
        s = []
        nonempty = []
        for i in range(n):
            for j in range(m):
                if a[i][j] != 0:
                    nonempty.append((i,j))
                    s.append(a[i][j])
        mean_ = sum(s) / len(s)
        for i, j in nonempty:
            if a[i][j] > mean_:
                a[i][j] -= 1
            elif a[i][j] < mean_: #eilf 쓰는거 매우중요..if썻다가 디버깅 한참..
                a[i][j] += 1

s = 0
for i in range(n):
    s += sum(a[i])
print(s)


import BruteForce
import pandas as pd
import numpy as np
if __name__ == "__main__":

    df = pd.read_table('CCLE_gene_expression.txt')
    Cell_lines = list(df.columns.values) #all cancer line information
    Breast_Cell_lines = ['gene_id','transcript_ids']

    #Other Cell Lines....
    for i in range(len(Cell_lines)):
        if BruteForce.BF(Cell_lines[i],'BREAST'):   # BREAST pattern match
            Breast_Cell_lines.append(Cell_lines[i])

    print(Breast_Cell_lines) #see all the breast cancer lines
    print(len(Breast_Cell_lines))

    f = df[Breast_Cell_lines]
    f.to_csv('CCLE_gene_expression_train(v1).txt', sep='\t',index=False)

    #-----------------------------   ---------------------------------#

    Breast_Cell_lines = ['gene_id','transcript_ids']
    df = pd.read_table('CCLE_gene_expression_train(v1).txt',sep = '\t')
    gene_id_raw = df[Breast_Cell_lines]
    mat1 = np.array(gene_id_raw)

    df2 = pd.read_table('CCLE_RNAseq_genes_rpkm_20180929.txt')
    Breast_Cell_lines = ['Name','Description']
    gene_id_done = df2[Breast_Cell_lines]
    mat2 = np.array(gene_id_done)

    for j in range(mat1.shape[0]):
        print(j)
        for i in range(mat2.shape[0]):
            if mat1[j][0] == mat2[i][0]:
                mat1[j][1] = mat2[i][1]
                #print("1")
                break
            else:
                mat1[j][1] = 0

    mat1 = pd.DataFrame(mat1)
    df['transcript_ids'] = mat1[1]
    print(df.head())
    df.to_csv('CCLE_gene_expression_train(v2).txt', sep='\t',index=False)

    df = pd.read_table('CCLE_gene_expression_train(v2).txt', sep = '\t')
    k = df.drop('gene_id',axis = 1)
    k.to_csv('CCLE_gene_expression_train(v2).txt', sep = '\t',index=False)

    df = pd.read_csv('Census_allThu Jul 30 07_07_22 2020.csv')
    kk = df['Gene Symbol']
    kk = np.array(kk)
    kk = kk.tolist()
    kk = set(kk)

    df = pd.read_table('CCLE_gene_expression_train(v2).txt', sep = '\t')
    gene = df['Gene']
    gene = np.array(gene)
    gene = gene.tolist()
    gene = set(gene)

    combine = list(kk & gene)

    df2 = pd.read_table('CCLE_gene_expression_train(v2).txt', sep = '\t', index_col = 0)
    #print(df2['Gene'][1])

    print(df2.head())
    ff = df2.loc[combine]
    print(ff.shape[0])
    ff.to_csv('CCLE_gene_expression_train(v3).txt', sep='\t',index=True)


length = int(input("Wprowadź długość przyprostokątnej: "))
listA = []
for i in range(1, length):
    listA.append(i)

lengthOfTheLastOne = len(listA)
for y in range(lengthOfTheLastOne):
    print("*" + " " * y + "*")
print("*" * length)

print(lengthOfTheLastOne)


#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
"""
Generic view function decorators

AUTHOR:
    lambdalisue[Ali su ae] (lambdalisue@hashnote.net)

License:
    The MIT License (MIT)

    Copyright (c) 2012 Alisue allright reserved.

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to
    deal in the Software without restriction, including without limitation the
    rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
    sell copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.

"""
from __future__ import with_statement
import copy
import datetime
from functools import wraps
from django.http import Http404
from django.utils.decorators import available_attrs
from django.core.exceptions import PermissionDenied

from utils import redirect_to_login
from utils import get_object_from_date_based_view
from utils import get_object_from_list_detail_view

__all__ = ['permission_required']

def permission_required(perm, queryset=None, login_url=None, raise_exception=False):
    def wrapper(view_func):
        @wraps(view_func, assigned=available_attrs(view_func))
        def inner(request, *args, **kwargs):
            _kwargs = copy.copy(kwargs)
            # overwrite queryset if specified
            if queryset:
                _kwargs['queryset'] = queryset

            # get object from view
            if 'date_field' in _kwargs:
                fn = get_object_from_date_based_view
            else:
                fn = get_object_from_list_detail_view
            if fn.validate(request, *args, **_kwargs):
                obj = fn(request, *args, **_kwargs)
            else:
                # required arguments is not passed
                obj = None

            if not request.user.has_perm(perm, obj=obj):
                if raise_exception:
                    raise PermissionDenied
                else:
                    return redirect_to_login(request, login_url)
            return view_func(request, *args, **_kwargs)
        return inner
    return wrapper


import cv2
import numpy as np
import tensorflow as tf, sys
import time
import pickle
from sklearn.svm import SVC, LinearSVC
import tensorflow.python.platform
from tensorflow.python.platform import gfile
from sklearn.svm import SVC, LinearSVC
import matplotlib.pyplot as plt
from sklearn.externals import joblib

#fonction create graph + extraction

def create_graph():
    with gfile.FastGFile('retrained_graph_smartphones.pb', 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        _ = tf.import_graph_def(graph_def, name='')

create_graph()

def extract_features(list_images,label):
    nb_features = 2048
    features = np.empty((len(list_images),nb_features))
    labels = []

    with tf.Session() as sess:

        next_to_last_tensor = sess.graph.get_tensor_by_name('pool_3:0')

        for ind, image in enumerate(list_images):
            if (ind%100 == 0):
                print('Processing %s...' % (image))

            if not gfile.Exists(image):
                tf.logging.fatal('File does not exist %s', image)

            image_data = gfile.FastGFile(image, 'rb').read()
            predictions = sess.run(next_to_last_tensor,
            {'DecodeJpeg/contents:0': image_data})
            features[ind,:] = np.squeeze(predictions)
            labels.append(label)

        return features, labels

#load training model
clf = joblib.load('svm.pkl')

#open camera
font = cv2.FONT_HERSHEY_SIMPLEX
flux= cv2.VideoCapture(0)
counter = 0

#Prise de photo
_ ,frame = flux.read()

list_prediction = []
image = []
utc = time.time()

while (True):
    _ ,img = flux.read()
    cv2.rectangle(frame,(170,50),(510,450),(0,205,0),2)
    cv2.imshow('Image', frame)

    key = cv2.waitKey(1)
    _ ,frame = flux.read()

    now = time.time()
    #if key == ord('s'):

    for i in range(5):
        time.sleep(0.5)
        while now - utc >= 0.5 and counter <5:
            _ ,img_predict = flux.read()
            img_predict = img_predict[50:450, 170:510]
            path = 'test' + "_" + str(counter) + ".jpg"
            cv2.imwrite(path, img_predict)
            counter += 1
            utc = now
            image.append([path])
            print(image)

    '''if key == ord('s'):
        image = []
        for i in range(5):

            _ ,img_predict = flux.read()
            img_predict = img_predict[50:450, 170:510]
            now = time.time()
            if now-utc >= 2:
                print(now-utc)
                path = 'test' + "_" + str(counter) + ".jpg"
                cv2.imwrite(path, img_predict)
                counter += 1
                utc = now
                image.append([path])
            print(image)'''

   # counter = 0

    '''if key == ord("q") or counter == 5:
        break
        #cv2.putText(img_predict,str(y_pred),(0,25), font, 1 , (0,0,0), 2, cv2.LINE_AA)

        #cv2.imshow('Prediction', img_predict)'''

    if key == 0x0d:
        for img in image:

            features,labels = extract_features(img,'test')

            X = features
            y_pred = clf.predict(X)

            list_prediction.append(y_pred[0])

        print(list_prediction)

        ring1=0
        ring2=0
        ring3=0
        ring4=0
        ring5=0
        ring6=0
        ring7=0
        ring8=0

        for prediction in list_prediction:

            if prediction =='Huawei_P9':

                ring1 += 1

            if prediction == 'Iphone_7':

                ring2 += 1

            if prediction == 'Meizu_m3s':

                ring3 += 1

            if prediction == 'Samsung_S4':

                ring4 += 1

            if prediction == 'Samsung_S6_Edge':

                ring5 += 1

            if prediction == 'Iphone_5':

                ring6 += 1

            if prediction == 'Huawei_P8_Lite':

                ring7 += 1

            if prediction == 'autre':

                ring8 += 1

        last_prediction =''

        def final_prediction(ring,produit):

             if ring == max(rings):

                 last_prediction = produit

                 print('Final prediction :' + last_prediction )

                 if sum(rings) !=  0:

                     confidence = ring/(sum(rings))*100

                     print(confidence)

                     if confidence > 50 :

                         cv2.putText(img_predict,last_prediction,(0,25), font, 1 , (0,0,0), 2, cv2.LINE_AA)

                         #cv2.putText(img_predict,str(confidence) + '%' ,(0,380), font, 1 , (0,0,0), 2, cv2.LINE_AA)

                         cv2.imshow('Prediction', img_predict)

                     else:

                         print('La reconnaissance ne peut aboutir, merci de recommencer !')

        rings= [ring1, ring2, ring3, ring4, ring5,ring6,ring7,ring8]

        print(rings)

        final_prediction(ring1,'Huawei_P9')
        final_prediction(ring2,'Iphone_7' )
        final_prediction(ring3,'Meizu_m3s')
        final_prediction(ring4,'Samsung_S4')
        final_prediction(ring5,'Samsung_S6_Edge')
        final_prediction(ring6,'Iphone_5')
        final_prediction(ring7,'Huawei_P8_Lite')
        final_prediction(ring8,'autre')

        list_prediction=[]

    if key == 0x1b:

        cv2.destroyAllWindows()

        flux.release()
        break


import random
import shutil
import socket
import sys
import time
import traceback
import math
from threading import Thread, Event, Lock

from keychain.Network.P2P_Connection import P2P_Connection
from keychain.Network.Protocol import Protocol, Protocol_Constant
from keychain.Network.Protocol.Bitcoin_Messages import GetBlock_Message, Addr_Message, Inv_Message, MemPool_Message, \
    GetData_Message, GetAddr_Message
from keychain.Network.Util import *

COLUMNS, ROWS = shutil.get_terminal_size(fallback=(80, 24))

def print_there(x, y, text):
    global COLUMNS, ROWS
    empty_line = ""
    for i in range(0, COLUMNS):
        empty_line += " "

    sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (x + 1, 0, empty_line))
    sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (x + 1, y + 1, text))
    sys.stdout.flush()

PEERS_MANAGEMENT_LOCKER = Lock()

class Network_Manager(Thread):

    def __init__(self, src_ip, src_port, src_service, network, bootstrap, blockchain_manager):
        Thread.__init__(self)

        """
                PEER INFORMATIONS
        """
        self._src_ip = src_ip
        self._src_port = src_port
        self._src_service = src_service
        self._network = network

        """
                PEER POOL INFORMATIONS
        """

        self._connections = dict()

        self._peers_pool = set(bootstrap)

        self._unactive_peers = dict()

        """
                CONNECTION RECEPTION MANAGEMENT
        """

        addr_type = Protocol.get_ip_type(self._src_ip)

        if addr_type == "ipv4":
            self._listener_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        elif addr_type == "ipv6":
            self._listener_socket = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)

        self._listener_socket.settimeout(Network_Constant.SOCKET_TIMEOUT)

        self._listener_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self._listener_socket.bind((self._src_ip, self._src_port))

        self._listener_thread = Thread(target=self.listener_thread, args=())

        """
                BLOCKCHAIN MANAGEMENT
        """

        self._blockchain_manager = blockchain_manager

        self.block_asked = False

        self.block_asked_list = dict()

        """
                MONITORING MANAGEMENT
        """

        self._topology_information = dict()

        """
                THREAD MANAGEMENT
        """

        self.__stop_event = Event()

        """
                ERROR MANAGEMENT
        """

        self._traceback_list = list()
    """
            GETTERS
    """

    def monitorIsTrue(self):
        return True

    def get_nb_connection(self):
        return len(self._connections)

    def get_connected_peers(self):
        connected_peers = list()

        for peer_info, connection in self._connections.copy().items():
            connected_peers.append(peer_info[0])

        return connected_peers
    """
            THREAD MANAGEMENT
    """

    def run(self):
        self.display_info("Network Manager is starting ...", 6)

        try:

            self._listener_thread.start()

            start = time.time()

            while not self.__stop_event.isSet():

                # Check if enough connection are alive
                if (time.time() - start) > Network_Constant.TIMEOUT_RETRY_ASK_NEW_PEER:

                    self.put_back_unactive_in_pool(Network_Constant.TIMEOUT_RETRY_UNACTIVE_PEER)

                    self.connect_to_peers()

                    start = time.time()

                self.remove_dead_peers()
                self.remove_dead_block_queries()

        except Exception:
            self.error_recording()
            self.kill()

    def kill(self):
        self.__stop_event.set()

        for connection in self._connections.values():
            connection.kill()

    def join(self, **kwargs):
        self.__stop_event.set()

        self._listener_thread.join()

        for connection in self._connections.values():
            connection.join()

        Thread.join(self)

    def kill_connections(self):
        self.__stop_event.set()

        for connection in self._connections.values():
            connection.kill()

    def join_connections(self):
        for connection in self._connections.values():
            connection.join()

    def kill_listener_thread(self):
        self.__stop_event.set()

    def join_listener_thread(self):
        self.__stop_event.set()

        self._listener_thread.join()

    def is_alive(self) -> bool:
        return not self.__stop_event.is_set()

    """
            INCOMING CONNECTION QUERIES MANAGEMENT
    """

    def listener_thread(self):
        try:
            while not self.__stop_event.isSet():
                self.listen()

        except Exception:
            self.error_recording()
            self.kill()

        self.__stop_event.set()

        self._listener_socket.close()

    def listen(self):
        global PEERS_MANAGEMENT_LOCKER
        connection = None

        try:
            self._listener_socket.listen(0)

            (peer_socket, (node_ip, node_port)) = self._listener_socket.accept()

            peer_socket.settimeout(Network_Constant.SOCKET_TIMEOUT)

            peer_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

            connection = P2P_Connection(self._src_ip, self._src_port, self._src_service, node_ip, node_port,
                                        Protocol_Constant.NODE_NETWORK, peer_socket, self._network, self)

            connection.start()

            connection.wait_for_handshake()

            PEERS_MANAGEMENT_LOCKER.acquire()

            self._connections[(node_ip, node_port, connection.get_node_service(), connection.get_handshake_time())] \
                = connection

            for ip, port, service, timestamp in self._unactive_peers.keys():
                if node_ip == ip:
                    del self._unactive_peers[(ip, port, service, timestamp)]
                    break

            PEERS_MANAGEMENT_LOCKER.release()

        except socket.timeout:
            if connection is not None:
                connection.join()

        except HandShakeFailureException:
            if connection is not None:
                connection.join()

    """
            PEERS MANAGEMENT
    """

    def connect(self, node_ip, node_port, node_service, node_timestamp):
        peer_socket = None
        connection = None
        # self.display_info(("Connection to " + str(node_ip) + " - " + str(node_port)))

        try:
            peer_socket = self.create_socket(node_ip, node_port)

            connection = P2P_Connection(self._src_ip, self._src_port, self._src_service, node_ip, node_port,
                                        node_service, peer_socket, self._network, self)

            connection.start()

            connection.bitcoin_handshake()

            self._connections[(node_ip, node_port, node_service, connection.get_handshake_time())] = connection

            self.query_peers(connection)

        except ConnectionException:
            if connection is not None:
                connection.join()

            if peer_socket is not None:
                peer_socket.close()

            self._unactive_peers[(node_ip, node_port, node_service, node_timestamp)] = time.time()

        self.display_info(("Connected to " + str(len(self._connections)) + " peer, "
                               + str(len(self._unactive_peers)) + " unactive peers, "
                               + str(len(self._peers_pool)) + " peers in the pool."), 7)

        self._peers_pool.remove((node_ip, node_port, node_service, node_timestamp))

    def create_socket(self, node_ip, node_port):

        try:
            addr_type = Protocol.get_ip_type(node_ip)

        except Protocol.UnknownIPAddressTypeException:
            raise ConnectionException("The IP Address provided is neither a valid IPv6 nor a valid IPv4 Address")

        socket_node = None

        if addr_type == "ipv4":
            socket_node = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        elif addr_type == "ipv6":
            socket_node = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)

        socket_node.settimeout(Network_Constant.SOCKET_TIMEOUT)

        socket_node.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

        socket_node.bind((self._src_ip, 0))

        try:
            if addr_type == "ipv4":
                socket_node.connect((node_ip, node_port))
            elif addr_type == "ipv6":
                socket_node.connect((node_ip, node_port, 0, 0))

        except socket.timeout as err:
            socket_node.close()
            raise SocketTimeoutException(str(err))
        except socket.error as err:
            socket_node.close()
            raise BrokenSocketException(str(err))

        return socket_node

    def is_known(self, ip, port, service, timestamp):
        if ((ip, port, service, timestamp) in self._peers_pool) \
                or ((ip, port, service, timestamp) in self._connections.copy()) \
                or ((ip, port, service, timestamp) in self._unactive_peers):
            return True
        else:
            return False

    def put_back_unactive_in_pool(self, timeout):
        nb_unactive = len(self._unactive_peers)
        if nb_unactive > 0:
            ordered_connections = sorted(self._unactive_peers, key=self._unactive_peers.get)

            if time.time() - self._unactive_peers[ordered_connections[0]] > timeout:

                i = 0
                while i < nb_unactive and time.time() - self._unactive_peers[ordered_connections[i]] > timeout \
                        and not self.__stop_event.is_set():
                    self._unactive_peers.pop(ordered_connections[i])

                    self._peers_pool.add(ordered_connections[i])

                    i = i + 1

        self.display_info(("Connected to " + str(len(self._connections)) + " peer, "
                               + str(len(self._unactive_peers)) + " unactive peers, "
                               + str(len(self._peers_pool)) + " peers in the pool."), 7)

    def remove_dead_peers(self):
        # Check if all connections are still alive
        for node_ip, node_port, node_service, node_timestamp in self._connections.copy():
            if not self._connections[(node_ip, node_port, node_service, node_timestamp)].isAlive():
                self._connections.pop((node_ip, node_port, node_service, node_timestamp))

                self._unactive_peers[(node_ip, node_port, node_service, node_timestamp)] = time.time()

        self.display_info(("Connected to " + str(len(self._connections)) + " peer, "
                               + str(len(self._unactive_peers)) + " unactive peers, "
                               + str(len(self._peers_pool)) + " peers in the pool."), 7)

    def connect_to_peers(self, nb_connection_missing=math.inf):
        # Connect to the peer in the pool
        i = 0
        while i < nb_connection_missing and len(self._peers_pool) > 0 and not self.__stop_event.is_set():
            node_ip, node_port, node_service, node_timestamp = next(iter(self._peers_pool))

            self.connect(node_ip, node_port, node_service, node_timestamp)

            i = i + 1

    def query_peers(self, connection=None):
        getaddr_msg = GetAddr_Message.GetAddr_Message()

        if connection is None:
            self.broadcast(getaddr_msg)
        else:
            self.send(connection, getaddr_msg)

    def remove_dead_block_queries(self):
        for block_hash, timestamp in self.block_asked_list.copy().items():
            if time.time() - timestamp > Network_Constant.QUERY_TIMEOUT:
                del self.block_asked_list[block_hash]

    def add_peer_to_pool(self, peer_list):
        for ip, (port, service, timestamp) in peer_list.items():
            if not self.is_known(ip, port, service, timestamp):
                self._peers_pool.add((ip, port, service, timestamp))

        self.display_info(("Connected to " + str(len(self._connections)) + " peer, "
                               + str(len(self._unactive_peers)) + " unactive peers, "
                               + str(len(self._peers_pool)) + " peers in the pool."), 7)

    def send_peers(self, connection):
        all_peers = set.union(self._peers_pool, set(self._unactive_peers.keys()), set(self._connections.keys()))

        # Remove the IP of the peer that ask for peers.
        ip, port, service, timestamp = connection.get_node_info()

        if (ip, port, service, timestamp) not in all_peers:
            raise BrokenConnectionException("BrokenConnection Exception : "
                                            "The peer is not considered as active anymore.")

        all_peers.remove((ip, port, service, timestamp))

        nb_peers_to_send = int(len(all_peers) / 10) + 1

        if nb_peers_to_send > 1000:
            tmp = random.sample(all_peers, 1000)
        elif len(all_peers) > nb_peers_to_send:
            tmp = random.sample(all_peers, nb_peers_to_send)
        else:
            return

        if len(tmp) > 0:
            peers_to_send = dict()
            for node_ip, node_port, node_service, node_timestamp in tmp:
                peers_to_send[node_ip] = (node_port, node_service, node_timestamp)

            addr_msg = Addr_Message.Addr_Message(peers_to_send)

            self.send(connection, addr_msg)

    """
            INV MESSAGE TREATMENT
    """

    def get_interesting_blocks_hash(self, block_hashes):

        interesting_block_hashes = set()

        for block_hash in block_hashes:
            if not self._blockchain_manager.isBlockHashKnown(block_hash):
                interesting_block_hashes.add(block_hash)

        return interesting_block_hashes

    def get_interesting_tx_hash(self, tx_hashes):
        interesting_tx_hashes = set()

        for tx_hash in tx_hashes:
            if not self._blockchain_manager.isTransactionHashKnown(tx_hash):
                interesting_tx_hashes.add(tx_hash)

        return interesting_tx_hashes

    def treat_inv_packet(self, connection, inv_msg, inv_msg_size):

        block_hashes, tx_hashes = inv_msg.get_available_objects()

        interesting_block_hashes = self.get_interesting_blocks_hash(block_hashes)
        interesting_tx_hashes = self.get_interesting_tx_hash(tx_hashes)

        inv_list = list()

        for block_hash in interesting_block_hashes:

            # Not to query two time_to_crawl the same block.
            if block_hash not in self.block_asked_list:
                inv_list.append((Protocol_Constant.INV_VECTOR_MSG_BLOCK, block_hash))
                connection.request_data(("block", block_hash))

                self.block_asked_list[block_hash] = time.time()

            self.add_topology_monitoring_information(connection, ("block", block_hash), time.time(), inv_msg_size)

        for tx_hash in interesting_tx_hashes:
            inv_list.append((Protocol_Constant.INV_VECTOR_MSG_TX, tx_hash))
            connection.request_data(("tx", tx_hash))

            self.add_topology_monitoring_information(connection, ("tx", tx_hash), time.time(), inv_msg_size)

        getdata_msg = GetData_Message.GetData_Message(inv_list)

        self.send(connection, getdata_msg)

        self.block_asked = False

    """
            GETDATA MESSAGE TREATMENT
    """

    def send_data(self, connection, tx_hashes, block_hashes):

        self.display_info(("Sending " + str(len(tx_hashes)) + " transactions and " + str(len(block_hashes))
                               + " blocks to " + connection.get_node_info()[0] + "..."), 6)

        for tx_hash in tx_hashes:
            transaction = self._blockchain_manager.getTransactionFromHash(tx_hash)

            if transaction is not None:
                tx_msg = transaction_to_tx_message(transaction)

                self.send(connection, tx_msg)

        blocks = list()

        for block_hash in block_hashes:
            block = self._blockchain_manager.getBlockFromHash(block_hash)

            if block is not None:
                blocks.append(block)

        for block in sorted(blocks):
            block_msg = block_to_block_message(block)

            self.send(connection, block_msg)

    """
            TX MESSAGE TREATMENT
    """

    def treat_tx_message(self, connection, tx_msg, tx_msg_size):

        self.display_info(("Tx Message Received from " + connection.get_node_info()[0] + "."), 6)

        transaction = tx_message_to_transaction(tx_msg)

        self._blockchain_manager.add_transaction(transaction)

        self._blockchain_manager.log_transaction_pool(self._src_ip)

        data_requested = connection.get_data_requested()

        if ("tx", transaction.__hash__()) in data_requested:
            connection.remove_data_requested("tx", transaction.__hash__())

        else:
            self.relay_transactions([transaction], except_info=[connection.get_node_info()[0]])

            self.add_topology_monitoring_information(connection, ("tx", transaction.__hash__()), time.time(),
                                                         tx_msg_size)

    def get_transaction_pool(self):
        self.display_info("Querying Transaction Pool ...", 6)

        mempool_msg = MemPool_Message.MemPool_Message()

        self.broadcast(mempool_msg)

        self.display_info("Transaction Pool queried.", 6)

    def treat_mempool(self, connection, mempool_msg):
        transaction_pool_hashes = self._blockchain_manager.getTransactionPool().getTransactionPoolHashes()

        inv_list = list()

        i = 0
        for transaction_hashes in transaction_pool_hashes:
            if i > Network_Constant.MAX_NUMBER_OF_OBJECT_IN_INV_MESSAGE:
                break

            inv_list.append((Protocol_Constant.INV_VECTOR_MSG_TX, transaction_hashes))

            i = i + 1

        inv_msg = Inv_Message.Inv_Message(inv_list)

        self.send(connection, inv_msg)

    """
            BLOCK MESSAGE TREATMENT
    """

    def treat_block_message(self, connection, block_msg, block_msg_size):

        self.display_info(("Block Message Received from " + connection.get_node_info()[0] + "."), 6)

        block = block_message_to_block(block_msg)

        ret = self._blockchain_manager.add_block(block)

        if block.getBlockHash() in self.block_asked_list:
            # Ask for removing this block of the queried blocks (already received)
            self.block_asked_list[block.getBlockHash()] = time.time() - 2 * Network_Constant.QUERY_TIMEOUT

        if ret == "invalid block index":
            self.get_blockchain(connection, block=False)

        self._blockchain_manager.log_blockchain(self._src_ip)

        data_requested = connection.get_data_requested()

        if ("block", block.getBlockHash()) in data_requested:
            connection.remove_data_requested("block", block.getBlockHash())

        else:
            self.relay_blocks([block], except_info=[connection.get_node_info()[0]])

            self.add_topology_monitoring_information(connection, ("block", block.getBlockHash()),
                                                         time.time(), block_msg_size)

    def get_blockchain(self, connection=None, block=True):

        self.display_info("Querying Blockchain ...", 6)

        block_locator_list = self.get_block_locator()

        version = Network_Constant.PROTOCOL_VERSION

        getblock_msg = GetBlock_Message.GetBlock_Message(version, block_locator_list)

        if connection is None:
            for co in self._connections.values():
                self.send(co, getblock_msg)

        else:
            self.send(connection, getblock_msg)

        if len(self._connections) > 0 or connection is not None:
            self.block_asked = True

            while not self.__stop_event.isSet() and block is True \
                    and ((self.block_asked is True) or (len(self.block_asked_list) > 0)):
                continue

        self.display_info("Blockchain queried.", 6)

    def get_block_locator(self):
        block_locator = list()
        blocks = self._blockchain_manager.getBlockchain().getBlocks()

        i = 0
        while i < len(blocks):
            block_hash = blocks[i].getBlockHash()

            block_locator.append(block_hash)

            if i < 10:
                i = i + 1
            else:
                i = i * 2

        block_locator.reverse()

        return block_locator

    def get_block_from_block_locator(self, block_locators, hash_stop):
        blockchain = self._blockchain_manager.getBlockchain()

        init_block = None
        for block_locator in block_locators:
            if blockchain.containsBlock(block_locator):
                init_block = blockchain.getBlockFromHash(block_locator)

        blocks = list()
        if init_block is not None:
            i = init_block.getIndex()

            block_hash = init_block.getBlockHash()

            i = i + 1

            while i < blockchain.length() and block_hash != hash_stop:
                block = blockchain.getBlock(i)

                blocks.append(block)

                block_hash = block.getBlockHash()

                i = i + 1

        return blocks

    def treat_getblock(self, connection, getblock_msg):

        self.display_info(("GetBlock Message Received from " + connection.get_node_info()[0] + "."), 6)

        block_locator = getblock_msg.get_block_locator_list()
        hash_stop = getblock_msg.get_hash_stop()

        blocks = self.get_block_from_block_locator(block_locator, hash_stop)

        self.relay_blocks_to_peer(connection, blocks)

    """
            GETHEADER MESSAGE TREATMENT
    """

    def get_header(self, connection):
        pass

    def treat_getheader(self, connection, getheader_msg):
        pass

    """
            BROADCAST OR RELAY OF MINED BLOCKS
    """

    def broadcast_blocks(self, blocks):
        self.display_info("Broadcast of blocks.", 6)

        for block in sorted(blocks):
            block_msg = block_to_block_message(block)

            self.broadcast(block_msg)

    def relay_blocks(self, blocks, except_info=None):

        self.display_info("Relay of blocks.", 6)

        inv_list = list()

        for block in sorted(blocks):
            inv_list.append((Protocol_Constant.INV_VECTOR_MSG_BLOCK, block.getBlockHash()))

        inv_msg = Inv_Message.Inv_Message(inv_list)

        self.broadcast(inv_msg, except_info=except_info)

    def relay_blocks_to_peer(self, connection, blocks):
        inv_list = list()

        for block in sorted(blocks):
            inv_list.append((Protocol_Constant.INV_VECTOR_MSG_BLOCK, block.getBlockHash()))

        inv_msg = Inv_Message.Inv_Message(inv_list)

        self.send(connection, inv_msg)

    def send_blocks(self, connection, blocks):

        self.display_info(("Sending Blocks to " + connection.get_node_info()[0] + "."), 6)

        for block in sorted(blocks):
            block_msg = block_to_block_message(block)

            self.send(connection, block_msg)

    """
            BROADCAST OR RELAY OF TRANSACTIONS
    """

    def broadcast_transactions(self, transactions):

        self.display_info("Broadcast of Transactions.", 6)

        for transaction in transactions:
            tx_msg = transaction_to_tx_message(transaction)

            self.broadcast(tx_msg)

    def relay_transactions(self, transactions, except_info=None):

        self.display_info("Relay of Transactions.", 6)

        inv_list = list()

        for transaction in transactions:
            inv_list.append((Protocol_Constant.INV_VECTOR_MSG_TX, transaction.__hash__()))

        inv_msg = Inv_Message.Inv_Message(inv_list)

        self.broadcast(inv_msg, except_info=except_info)

    def relay_transactions_to_peer(self, connection, transactions):

        self.display_info("Relay of Transactions.", 6)

        inv_list = list()

        for transaction in transactions:
            inv_list.append((Protocol_Constant.INV_VECTOR_MSG_TX, transaction.__hash__()))

        inv_msg = Inv_Message.Inv_Message(inv_list)

        self.send(connection, inv_msg)

    def send_transactions(self, connection, transactions):
        self.display_info(("Sending Transactions to " + connection.get_node_info()[0] + "."), 6)

        for transaction in transactions:
            tx_msg = transaction_to_tx_message(transaction)

            self.send(connection, tx_msg)

    """
            GENERAL MESSAGE MANAGEMENT
    """

    def send(self, connection, msg):
        if connection.is_handshake_done() and connection.isAlive():
            connection.send_msg(msg)

    def broadcast(self, msg, except_info=None):
        for peer_info, connection in self._connections.copy().items():
            if not (except_info is not None and peer_info[0] in except_info) \
                    and connection.is_handshake_done() and connection.isAlive():
                connection.send_msg(msg)

    """
            MONITORING MANAGEMENT
    """

    def get_round_trip_time(self, connection=None):
        if connection is None:
            round_trip_time = dict()

            peers = self._connections.copy()

            for peer_info, connection in peers.items():
                connection.ask_alive(block=True)

            for peer_info, connection in peers.items():
                ret = None

                while ret is None and not self.__stop_event.is_set():
                    ret = connection.get_last_round_trip_time()

                round_trip_time[peer_info[0]] = ret

                return round_trip_time
        else:
            connection.ask_alive(block=True)

            ret = None

            while ret is None and not self.__stop_event.is_set():
                ret = connection.get_last_round_trip_time()

            return ret

    def add_topology_monitoring_information(self, connection, info, timestamp, msg_size):
        #############################################################################
        #                           TOPOLOGY INFORMATIONS                           #
        #                                                                           #
        #  - For each broadcasted information, keep:                                #
        #       1. Node IP                                                          #
        #       2. Most recent sent time_to_crawl (received time_to_crawl - round_trip_time)          #
        #       (the earliest moment at which the peer received this information)   #
        #                                                                           #
        #############################################################################

        round_trip_time = self.get_round_trip_time(connection)
        node_ip = connection.get_node_info()[0]

        per_byte_rtt = round_trip_time/Ping_Message.Ping_Message(0).getMessageSize()

        sent_time = timestamp - (per_byte_rtt * msg_size) / 2

        if info in self._topology_information and node_ip not in self._topology_information[info]:
            self._topology_information[info][node_ip] = sent_time

        else:
            self._topology_information[info] = dict()
            self._topology_information[info][node_ip] = sent_time

    def get_topology_monitoring_information(self):
        return self._topology_information.copy()

    def remove_topology_monitoring_information(self, info_list):
        for info in info_list:
            if info in self._topology_information:
                del self._topology_information[info]

    def display_info(self, text, x):
        print_there(x, 0, ("Network Manager " + self._src_ip + " : " + str(text)))

    def error_recording(self):
        directory = "Log/"

        if not os.path.exists(directory):
            os.makedirs(directory)

        directory = "Log/Log_Peer_" + self._src_ip + "/"

        if not os.path.exists(directory):
            os.makedirs(directory)

        stdout = open(("Log/Log_Peer_" + self._src_ip + "/Network_Manager_Log.txt"), "a")

        ex_type, ex, tb = sys.exc_info()
        traceback.print_exception(ex_type, ex, tb, file=stdout)
        stdout.write('\n\n')

        stdout.close()

        self._traceback_list.append((ex_type, ex, tb))

    def get_tracebacks(self):
        return self._traceback_list


# -*- encoding: utf-8 -*-

"""
bug备注：
    致命bug:
        1.第160行：print(id)大概率多出几位
        2.视频【BV1Cp4y1X7AF】调试时出现-3楼的情况
"""

# Here put the improt lib
import json
import re
import time
import hashlib
import requests
from difflib import SequenceMatcher

def logo():
    print('                     //')
    print('         \\\         //')
    print('          \\\       //')
    print('    ##3333333333333333333333##')
    print('    ## 22222222222222222222 ##    ________   ___   ___        ___   ________   ___   ___        ___')
    print('    ## hh                hh ##   |\   __  \ |\  \ |\  \      |\  \ |\   __  \ |\  \ |\  \      |\  \\')
    print('    ## hh    //    \\\    hh ##   \ \  \|\ /_\ \  \\\ \  \     \ \  \\\ \  \|\ /_\ \  \\\ \  \     \ \  \\')
    print('    ## hh   //      \\\   hh ##    \ \   __  \\\ \  \\\ \  \     \ \  \\\ \   __  \\\ \  \\\ \  \     \ \  \\')
    print('    ## hh                hh ##     \ \  \|\  \\\ \  \\\ \  \____ \ \  \\\ \  \|\  \\\ \  \\\ \  \____ \ \  \\')
    print('    ## hh      wwww      hh ##      \ \_______\\\ \__\\\ \_______\\\ \__\\\ \_______\\\ \__\\\ \_______\\\ \__\\')
    print('    ## hh                hh ##       \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|')
    print('    ## 22222222222222222222 ##        【exit】退出')
    print('    ##3333333333333333333333##        【达拉崩吧】说明')
    print('\n')

def explain():
    print('Instruction Book')
    print('奖号依据：程序运行时刻的微博热搜词top1=>MD5加密=>奖号')
    print('奖券号：楼层数+昵称+id=>MD5加密=>奖券号')
    print('抽奖时取奖券号最接近奖号的一位')

def getAv(code):  # 获取视频av号
    url = f'https://www.bilibili.com/video/{video_code}'
    response = requests.get(url)
    response.encoding = 'utf-8'
    html = response.text
    av_code = re.findall(
        r'<meta data-vue-meta="true" property="og:url" content="https://www.bilibili.com/video/av(.*?)/">', html)[0]
    return av_code

def getData(av_code, page_num=1, sort=0): #评论区username&id
    url = f'https://api.bilibili.com/x/v2/reply?callback=jQuery1720266519964639113_1586919135667&pn={page_num}&type=1&oid={av_code}&sort={sort}&_=1586919138435'
    # url = f'https://api.bilibili.com/x/v2/reply?callback=jQuery1720266519964639113_1586919135667&pn=1&type=1&oid=540177402&sort=0&_=1586919138435'
    # pn 页码
    # oid 视频id
    # sort == 0 按时间排序
    # sort == 2 按热度排序
    response = requests.get(url)
    response.encoding = 'utf-8'
    html = json.loads(response.text)
    page_data = html['data']['page']
    floor = page_data['count']
    pageNum = page_data['count'] // page_data['size'] + 1
    # print(pprint.pprint(html['data']['replies']))

    cmMap = {}  # 第xxl楼：{'usr_name':xxxxx,'id':xxxxx}
    for num in range(1, pageNum + 1):
        dir_url = f'https://api.bilibili.com/x/v2/reply?callback=jQuery1720266519964639113_1586919135667&pn={num}&type=1&oid={av_code}&sort={sort}&_=1586919138435'
        # dir_url = f'https://api.bilibili.com/x/v2/reply?callback=jQuery1720266519964639113_1586919135667&pn={num}&type=1&oid=540177402&sort=0&_=1586919138435'
        response_d = requests.get(dir_url)
        response_d.encoding = 'utf-8'
        html_d = json.loads(response_d.text)
        for i in html_d['data']['replies']:
            user_data = {'user_name': i['member']
                         ['uname'], 'id': i['member']['mid']}
            # print(i['member']['mid'] + ':' + i['member']['uname'])
            # print(user_data)
            cmMap[str(floor)] = user_data
            floor -= 1
        # print(num)
        # print(dir_url)
        time.sleep(0.5)
    #去重复
    re_list=[]
    for key,value in cmMap.items():
        for k,v in cmMap.items():
            if k in re_list:
                continue
            if key != k and value == v:
                re_list.append(k)
        # print(re_list)
    del_list = re_list[:-1]
    # print(del_list)
    for i in del_list:
        del cmMap[i]
    # print(cmMap)
    return cmMap

def  weiboHot(): #微博热词
    url = 'https://s.weibo.com/top/summary?cate=realtimehot'
    reponse = requests.get(url)
    reponse.encoding = 'utf-8'
    html = reponse.text
    hot_tbody = re.findall(r'
(.*?)
',html,re.S)[0]
    hot_top = re.findall(r'
(.*?)
',hot_tbody,re.S)[1]
    hot_word = re.findall(r'target="_blank">(.*?)',hot_top,re.S)[0]
    return hot_word

def hashMd5(args): #哈希算法
    hash_md5 = hashlib.md5()
    hashMd5 = hash_md5.update(args.encode('utf-8'))#hash.hexdigest（）
    hash_code = hash_md5.hexdigest()
    return hash_code

def similarity(a, b):
    return SequenceMatcher(None, a, b).ratio()

if __name__ == "__main__":
    logo()
    while True:
        video_code = input('BV号:')
        if video_code=='达拉崩吧':
            explain()
        elif video_code=='exit':
            break
        elif len(video_code)==12:
            max_num = 0
            hot_word = weiboHot()
            hash_code_hot = hashMd5(hot_word)

            print(time.asctime( time.localtime(time.time())))

            print('微博热搜top1:'+ hot_word)

            print('哈希值:' + hash_code_hot)

            av_code = getAv(video_code)

            cm_dict = getData(av_code)

            for key,value in cm_dict.items():
                # print(key,value)
                userName = value['user_name']
                uid = value['id']
                cm_word = key + userName + uid
                # print(cm_word)
                hash_code_cm = hashMd5(cm_word)
                # print(hash_code_cm)
                # print(f'\r{hash_code_cm}' , end = '')

                if similarity(hash_code_hot,hash_code_cm) > max_num:
                    max_num = similarity(hash_code_hot,hash_code_cm)
                    lucky_code = hash_code_cm
                    lucky_name = userName
                    lucky_id = uid
                    # print('\n*************************')
                    # print(lucky_code + '--'+ str(max_num) +'--')
                    # print('*************************')
                    # print(f'\r{lucky_code}|相似度:{max_num}', end = '')
                    # print(f'\r***LUCKY MAN***')
                    print('\r第%s楼  NAME:%s  ID:%s' %(key,lucky_name,lucky_id),end='')
                    # print(lucky_name,lucky_id)
                # print(f'\r{cm_word}' , end = '')
                time.sleep(0.05)
            # print('\r')
            print('\n',lucky_name,lucky_id)
        else:
            print('BV号有误')