import pygame
from math import *
import time
pygame.init()
pygame.display.set_caption("sprite sheet")  # sets the window title
screen = pygame.display.set_mode((1000, 800))  # creates game screen
screen.fill((0,0,0))
clock = pygame.time.Clock() #set up clock

#Variables and stuff (Start)-------------------------------------------------------------------
red_pawn = pygame.image.load('RedPawn.png') #load your spritesheet
white_pawn = pygame.image.load('WhitePawn.png') #load your spritesheet
background = pygame.image.load('Chess board.png')#loads background
white_rook = pygame.image.load('WhiteRook.png')
red_rook = pygame.image.load('RedRook.png')
white_queen = pygame.image.load('WhiteQueen.png')
red_queen = pygame.image.load('RedQueen.png')
white_king = pygame.image.load('WhiteKing.png')
red_king = pygame.image.load('RedKing.png')
white_bishop = pygame.image.load('WhiteBishop.png')
red_bishop = pygame.image.load('RedBishop.png')
red_knight = pygame.image.load('RedKnight.png')
white_knight = pygame.image.load('WhiteKnight.png')

White=(255,255,255)
PIECEWIDTH = 100
checkmate = False #variable to run our game loop
whiteturn = True
hasPicked = False
clicked = False
TurnNum=0
ticks = 1
tickss = 1

t = 600
t2 = 600
pygame.display.set_caption('Show Text')
font = pygame.font.Font('freesansbold.ttf', 32)
#Variables and stuff (End)--------------------------------------------------------------------
#text to screen setup

#text input------------------------------------------
n1 = input('Enter your name player 1:')
n2 = input('Enter your name player 2')
#End of text input----------------------------------

#Pieces start-----------------------------------------------
class pieces:
    def __init__(self,xpos,ypos,type):
        self.xpos = xpos
        self.ypos = ypos
        self.lifted = False
        self.type = type
        self.alive = True
        self.row=0
        self.column=0

    def updatePos(self):
        self.row = int(self.xpos/100)
        self.column = int(self.ypos/100)
        print("my row and column is ", self.row, self.column)
        self.xpos = self.row*100
        self.ypos = self.column*100

    def isClicked(self,x,y):
            global hasPicked
            global TurnNum
            print("checking distance")
            if self.alive == True:
                if hasPicked == False and self.lifted == False:
                    if (sqrt(((self.xpos+50)-x)**2 + ((self.ypos+50)-y)**2)) <= 50:#check if the distance b/t (x, y) and (self.xpos, self.ypos) is LESS than 50
                        self.lifted = True #if true, set lifted to true
                        hasPicked = True
                        TurnNum+=1
                        print("lifted!")
                    else:
                        print("no collision")

    def move(self, x, y):
            if self.lifted == True:#if islifed is true, set self.xpos to x, and self.ypos to y
                self.xpos = x-50
                self.ypos = y-50

    def draw(self):
        if self.alive == True:
            #pawns
            if self.type == "WhitePawn":
                screen.blit(white_pawn, (self.xpos, self.ypos), (0, 0, 100, 100))
            if self.type == "RedPawn":
                screen.blit(red_pawn, (self.xpos, self.ypos), (0, 0, 100, 100))
            #knights
            if self.type == "WhiteKnight":
                screen.blit(white_knight, (self.xpos, self.ypos), (0, 0, 100, 100))
            if self.type == "RedKnight":
                screen.blit(red_knight, (self.xpos, self.ypos), (0, 0, 100, 100))
            #bishop
            if self.type == "WhiteBishop":
                screen.blit(white_bishop, (self.xpos, self.ypos), (0, 0, 100, 100))
            if self.type == "RedBishop":
                screen.blit(red_bishop, (self.xpos, self.ypos), (0, 0, 100, 100))
            #rook
            if self.type == "WhiteRook":
                screen.blit(white_rook, (self.xpos, self.ypos), (0, 0, 100, 100))
            if self.type == "RedRook":
                screen.blit(red_rook, (self.xpos, self.ypos), (0, 0, 100, 100))
            #king
            if self.type == "WhiteKing":
                screen.blit(white_king, (self.xpos, self.ypos), (0, 0, 100, 100))
            if self.type == "RedKing":
                screen.blit(red_king, (self.xpos, self.ypos), (0, 0, 100, 100))
            #queen
            if self.type == "WhiteQueen":
                screen.blit(white_queen, (self.xpos, self.ypos), (0, 0, 100, 100))
            if self.type == "RedQueen":
                screen.blit(red_queen, (self.xpos, self.ypos), (0, 0, 100, 100))

#Pieces End-----------------------------------------------------------

#python list (like a vector)
whitelist = []
redlist = []

#White Pawns
WhitePawn1 = pieces(0, 100, "WhitePawn")
whitelist.append(WhitePawn1)
WhitePawn2 = pieces(100, 100, "WhitePawn")
whitelist.append(WhitePawn2)
WhitePawn3 = pieces(200, 100, "WhitePawn")
whitelist.append(WhitePawn3)
WhitePawn4 = pieces(300, 100, "WhitePawn")
whitelist.append(WhitePawn4)
WhitePawn5 = pieces(400, 100, "WhitePawn")
whitelist.append(WhitePawn5)
WhitePawn6 = pieces(500, 100, "WhitePawn")
whitelist.append(WhitePawn6)
WhitePawn7 = pieces(600, 100, "WhitePawn")
whitelist.append(WhitePawn7)
WhitePawn8 = pieces(700, 100, "WhitePawn")
whitelist.append(WhitePawn8)
#Red Pawns
RedPawn1 = pieces(0, 600, "RedPawn")
redlist.append(RedPawn1)
RedPawn2 = pieces(100, 600, "RedPawn")
redlist.append(RedPawn2)
RedPawn3 = pieces(200, 600, "RedPawn")
redlist.append(RedPawn3)
RedPawn4 = pieces(300, 600, "RedPawn")
redlist.append(RedPawn4)
RedPawn5 = pieces(400, 600, "RedPawn")
redlist.append(RedPawn5)
RedPawn6 = pieces(500, 600, "RedPawn")
redlist.append(RedPawn6)
RedPawn7 = pieces(600, 600, "RedPawn")
redlist.append(RedPawn7)
RedPawn8 = pieces(700, 600, "RedPawn")
redlist.append(RedPawn8)
#White Rooks
WhiteRook1 = pieces(0, 0, "WhiteRook")
whitelist.append(WhiteRook1)
WhiteRook2 = pieces(700, 0, "WhiteRook")
whitelist.append(WhiteRook2)
#Red Rooks
RedRook1 = pieces(0, 700, "RedRook")
redlist.append(RedRook1)
RedRook2 = pieces(700, 700, "RedRook")
redlist.append(RedRook2)
#White Knights
WhiteKnight1 = pieces(100, 0, "WhiteKnight")
whitelist.append(WhiteKnight1)
WhiteKnight2 = pieces(600, 0, "WhiteKnight")
whitelist.append(WhiteKnight2)
#Red Knights
RedKnight1 = pieces(100, 700, "RedKnight")
redlist.append(RedKnight1)
RedKnight2 = pieces(600, 700, "RedKnight")
redlist.append(RedKnight2)
#White Bishops
WhiteBishop1 = pieces(200, 0, "WhiteBishop")
whitelist.append(WhiteBishop1)
WhiteBishop2 = pieces(500, 0, "WhiteBishop")
whitelist.append(WhiteBishop2)
#Red Bishop
RedBishop1 = pieces(200, 700, "RedBishop")
redlist.append(RedBishop1)
RedBishop2 = pieces(500, 700, "RedBishop")
redlist.append(RedBishop2)
#White and Red King
WhiteKing = pieces(300, 0, "WhiteKing")
whitelist.append(WhiteKing)
RedKing = pieces(300, 700, "RedKing")
redlist.append(RedKing)
#White and Red Queen
WhiteQueen = pieces(400, 0, "WhiteQueen")
whitelist.append(WhiteQueen)
RedQueen = pieces(400, 700, "RedQueen")
redlist.append(RedQueen)

#MAP:
map = [[3, 4, 5, 6, 7, 5, 4, 3],

       [2, 2, 2, 2, 2, 2, 2, 2],

       [0, 0, 0, 0, 0, 0, 0, 0],

       [0, 0, 0, 0, 0, 0, 0, 0],

       [0, 0, 0, 0, 0, 0, 0, 0],

       [0, 0, 0, 0, 0, 0, 0, 0],

       [1, 1, 1, 1, 1, 1, 1, 1],

       [8, 9, 10, 11, 12, 10, 9, 8]]

while not checkmate:

    clock.tick(60)
    #print("clicked is ", clicked)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            checkmate = True

        if event.type == pygame.MOUSEBUTTONDOWN:
            if pygame.MOUSEBUTTONDOWN:
                print("click")
                if clicked == False:
                    clicked = True
                elif clicked == True:
                    clicked = False

        if event.type == pygame.MOUSEBUTTONUP:
            print("unclick")
            clicked = False

        if event.type == pygame.MOUSEMOTION: #check if mouse moved
            mousePos = event.pos #refreshes mouse position
          #  print("mouse position: (",mousePos[0]," , ",mousePos[1], ")")

    #COLLISION

    #White updatepos
    for i in range(len(whitelist)):
        whitelist[i].updatePos()
    #red updatepos
    for i in range(len(redlist)):
        redlist[i].updatePos()

    if clicked ==True:

        if whiteturn is True:# if whiteturn is true white pieces go
            #white clicked function
            for i in range(len(whitelist)):
                whitelist[i].isClicked(mousePos[0],mousePos[1])

        if whiteturn is False: # if whiteturn is false red goes
            #red clicked function
            for i in range(len(redlist)):
                redlist[i].isClicked(mousePos[0],mousePos[1])
    if clicked == False:
        hasPicked = False
        #white pieces lifted set down
        for i in range(len(whitelist)):
            whitelist[i].lifted = False
        #red piece lifted set down
        for i in range(len(redlist)):
            redlist[i].lifted = False
    #White pieces move
    for i in range(len(whitelist)):
        whitelist[i].move(mousePos[0],mousePos[1])
    #Red pieces move
    for i in range(len(redlist)):
        redlist[i].move(mousePos[0],mousePos[1])

    #print to screen

    if whiteturn == True:
        ticks +=1
        if ticks%60==0:   #add a ticker variable here, add 1 to it, when it hits 60, decrement t and reset time
            t-=1
            if t == 0:
                gameover = True

    if whiteturn == False:
        tickss +=1
        if tickss%60==0:
            t2-=1
            if t2 == 0:
                gameover = True

    text = font.render(str(t), True, (0,255,0))
    textRect = text.get_rect()
    textRect.center = (900, 0)

    text2 = font.render(str(t2), True, (0,255,0))
    textRect2 = text2.get_rect()
    textRect2.center = (900, 800)

    Name = font.render(str(n1), True, (255,255,255))
    name = Name.get_rect()
    name.center = (900, 100)

    Name2 = font.render(str(n2), True, (255,255,255))
    name2 = Name2.get_rect()
    name2.center = (900, 700)

    for i in range(len(whitelist)):
       for j in range(len(redlist)):
            if whiteturn == False:
                if whitelist[i].row/100 == redlist[j].row/100 and whitelist[i].column/100 == redlist[j].column/100:
                    redlist[j].alive = False
                    print(redlist[j].type, "got killed")
            if whiteturn == True:
                if redlist[j].row/100 == whitelist[i].row/100 and redlist[j].column/100 == whitelist[i].column/100:
                    whitelist[i].alive = False
                    print(whitelist[i].type, "got killed")

    if WhiteKing.alive == False:
        checkmate = True
    if RedKing.alive == False:
        checkmate = True
    # RENDER--------------------------------------------------------------------------------
    screen.fill((0,0,0)) #wipe screen so it doesn't smear
    screen.blit(background, (0,0))

    if TurnNum % 2==0:
        whiteturn=True
        print("white's turn")
    else:
        print("red's turn")
        whiteturn = False
    print("Turn Num:", TurnNum)

    #draws white pieces
    for i in range(len(whitelist)):
            whitelist[i].draw()
    #draws red pieces
    for i in range(len(redlist)):
            redlist[i].draw()

    screen.blit(Name, (900,50))
    screen.blit(Name2, (900,700))
    screen.blit(text, (900, 100))
    screen.blit(text2, (900, 750))
    pygame.display.flip()#this actually puts the pixel on the screen

#end game loop------------------------------------------------------------------------------
pygame.quit()


import logging
import warnings
from typing import List, Tuple

import numpy as np
import pandas as pd
from anndata import AnnData
from mudata import MuData
from pandas.api.types import is_numeric_dtype
from sklearn.neighbors import KNeighborsClassifier

from ..utils import check_transition_rule, get_views_from_structure
from .MultiViewAtlas import MultiViewAtlas, _harmonize_mdata_full

warnings.simplefilter(action="ignore", category=FutureWarning)
warnings.simplefilter(action="ignore", category=UserWarning)

# logger = logging.getLogger(__name__)
# logger.propagate = False
# ch = RichHandler(level=logging.INFO, show_path=False, show_time=False)
# formatter = logging.Formatter("%(message)s")
# ch.setFormatter(formatter)
# logger.addHandler(ch)

def load_query(
    mvatlas: MultiViewAtlas,
    adata_query: AnnData,
) -> MultiViewAtlas:
    """Load query data to full view of multi-view atlas

    Params:
    -------
        mvatlas:
            MultiViewAtlas object
        adata_query:
            AnnData of query data

    Returns:
    --------
        MultiViewAtlas object with mapped query cells in full view
    """
    # Define all current view - next view pairs for assignment
    view_pairs = mvatlas.get_view_pairs()

    # Check if query cells already in mdata
    if adata_query.obs_names.isin(mvatlas.mdata.obs_names).all():
        vdata_full = mvatlas.mdata["full"].copy()
    else:
        vdata_full = concatenate_query(mvatlas, adata_query, "full")
        # Check that at least one mapping from full view is possible with transition rules
        rules_from_full = view_pairs[view_pairs.depth == 0]["transition_rule"].unique()
        missing_rules = []
        for r in rules_from_full:
            try:
                if check_transition_rule(adata_query, r) is None:
                    continue
            except ValueError:
                missing_rules.append(r)
        if len(rules_from_full) == len(missing_rules):
            raise ValueError(
                f"""
                No mapping possible from full view. Please add info on rules {missing_rules} to query dataset
                """
            )

    mdata = mvatlas.mdata.copy()
    mdata.mod["full"] = vdata_full.copy()
    try:
        mdata.update()
    except KeyError:
        mdata.update()
    del mdata.obsm["view_assign"]
    mvatlas_mapped = MultiViewAtlas(mdata, rename_obsm=False)
    mvatlas_mapped.view_transition_rule = mvatlas.view_transition_rule.copy()

    _harmonize_mdata_full(mvatlas_mapped)
    return mvatlas_mapped

def split_query(
    mvatlas_mapped: MultiViewAtlas,
) -> MultiViewAtlas:
    """Assign query data to views in atlas, based on transition rules.

    Params:
    -------
        mvatlas:
            MultiViewAtlas object

    Returns:
    --------
        MultiViewAtlas object with mapped query cells
    """
    # Define all current view - next view pairs for assignment
    view_pairs = mvatlas_mapped.get_view_pairs()

    # Check that load_query was run
    try:
        if sum(mvatlas_mapped.mdata["full"].obs["dataset_group"] == "query") == 0:
            raise ValueError("No query cells in mdata - run load_query first")
    except KeyError:
        raise ValueError("No query cells in mdata - run load_query first")

    vdata_full = mvatlas_mapped.mdata["full"].copy()

    new_view_assign = pd.DataFrame()
    vdata_dict = {}
    vdata_dict["full"] = vdata_full.copy()
    for _, row in view_pairs.iterrows():
        depth = row["depth"]
        current_view = row["parent_view"]
        next_view = row["child_view"]
        try:
            n_query_current = sum(vdata_dict[current_view].obs["dataset_group"] == "query")
        except KeyError:
            n_query_current = 0
        try:
            n_query_next = sum(mvatlas_mapped.mdata[next_view].obs["dataset_group"] == "query")
        except KeyError:
            n_query_next = 0
        # if "dataset_group" in vdata_dict[current_view].obs:
        if n_query_current > 0:
            adata_query = vdata_dict[current_view][vdata_dict[current_view].obs["dataset_group"] == "query"].copy()
            logging.info(f"Assigning to {next_view} from {current_view} with rule {row['transition_rule']}")
            # print(adata_query)
            # print(vdata_dict[current_view])
            # print(mvatlas_mapped.mdata[current_view])
            if n_query_next > 0:
                logging.info(f"Query cells already in {next_view}")
                v_assign = mvatlas_mapped.mdata.obsm["view_assign"][[next_view]]
                vdata_dict[next_view] = mvatlas_mapped.mdata[next_view].copy()
            else:
                adata_query_concat = AnnData(obs=adata_query.obs, obsm=adata_query.obsm, obsp=adata_query.obsp)
                if depth > 0:
                    adata_query_concat = adata_query_concat[
                        new_view_assign.loc[adata_query_concat.obs_names, current_view] == 1
                    ].copy()
                v_assign, next_view_adata = map_next_view(
                    mvatlas_mapped, adata_query_concat, current_view=current_view, next_view=next_view
                )
                vdata_dict[next_view] = next_view_adata.copy()
        else:
            logging.info(f"No query cells in {current_view}")
            v_assign = mvatlas_mapped.mdata.obsm["view_assign"][[next_view]]
            vdata_dict[next_view] = mvatlas_mapped.mdata[next_view].copy()
        new_view_assign = pd.concat([new_view_assign, v_assign], 1)

    new_view_assign = new_view_assign.fillna(0)
    vdata_full.obsm["view_assign"] = new_view_assign.copy()
    vdata_full.uns["view_hierarchy"] = mvatlas_mapped.view_hierarchy
    vdata_dict["full"] = vdata_full
    mdata = MuData({v: vdata_dict[v] for v in get_views_from_structure(mvatlas_mapped.view_hierarchy)})
    mdata.mod["full"] = mdata.mod["full"][mdata.obs_names].copy()
    view_transition_rule = mvatlas_mapped.view_transition_rule.copy()
    # trans_rule = pd.Series(mvatlas.view_transition_rule.values.ravel()).dropna().unique()[0]
    mvatlas_mapped = MultiViewAtlas(mdata, rename_obsm=False, transition_rule=view_transition_rule)
    # mvatlas_mapped.view_transition_rule = view_transition_rule.copy()
    return mvatlas_mapped

def concatenate_query(
    mvatlas: MultiViewAtlas,
    adata_query: AnnData,
    view: str,
    batch_key: str = "dataset_group",
    batch_categories: List[str] = None,
):
    """Concatenate query data to atlas data"""
    if batch_categories is None:
        batch_categories = ["atlas", "query"]

    adata_atlas = mvatlas.mdata[view].copy()
    assert np.intersect1d(adata_atlas.obs_names, adata_query.obs_names).size == 0
    adata_atlas = adata_atlas.concatenate(
        adata_query, batch_key=batch_key, batch_categories=batch_categories, index_unique=None, uns_merge="unique"
    )
    return adata_atlas

def assign_from_similarity(Y_assign_atlas, X_similarity_atlas, X_similarity_query, v_assign, k=10):
    """Assign query cells to atlas views based on similarity to atlas cells.

    Similarity can be derived from metadata annotation or from latent space
    """
    if not isinstance(v_assign, pd.DataFrame):
        raise ValueError("v_assign must be a pandas DataFrame")
    if not v_assign.shape[1] == 1:
        raise ValueError("v_assign must have only one column")

    clf = KNeighborsClassifier(n_neighbors=k, metric="euclidean").fit(X_similarity_atlas, Y_assign_atlas)
    Y_assign_query = clf.predict(X_similarity_query)
    assign_predict = pd.DataFrame(Y_assign_query, columns=v_assign.columns)
    return assign_predict

def map_next_view(
    mvatlas: MultiViewAtlas,
    adata_query: AnnData,
    current_view: str,
    next_view: str,
    batch_key: str = "dataset_group",
    batch_categories: List[str] = None,
) -> Tuple[pd.DataFrame, AnnData]:
    """Assign query cells to next view based on similarity to atlas cells.

    Similarity is defined by the transition rule.
    """
    if batch_categories is None:
        batch_categories = ["atlas", "query"]

    # curr_view_adata = _concatenate_query(mvatlas, adata_query, current_view, batch_key=batch_key, batch_categories=batch_categories)
    curr_view_adata = mvatlas.mdata[current_view].copy()
    next_view_adata = mvatlas.mdata[next_view].copy()

    # Get similarity from transition rule
    if batch_key in curr_view_adata.obs.columns:
        v_assign = mvatlas.mdata.obsm["view_assign"].loc[mvatlas.mdata["full"].obs[batch_key] == batch_categories[0]][
            [next_view]
        ]
        curr_view_adata = curr_view_adata[curr_view_adata.obs[batch_key] == batch_categories[0]].copy()
        # next_view_adata = next_view_adata[next_view_adata.obs[batch_key] == batch_categories[0]].copy()
        # assert "dataset_group" not in next_view_adata.obs.columns
    else:
        v_assign = mvatlas.mdata.obsm["view_assign"].loc[mvatlas.mdata["full"].obs[batch_key] == batch_categories[0]][
            [next_view]
        ]
    transition_rule = mvatlas.view_transition_rule[current_view][next_view]
    if transition_rule is not None:
        try:
            check_transition_rule(adata_query, transition_rule)
        except ValueError:
            logging.warning(
                f"Could not check transition rule {transition_rule} for query data - skipping mapping from {current_view} to {next_view}"
            )
            v_assign_query = pd.DataFrame(0, columns=[next_view], index=adata_query.obs_names)
            v_assign = pd.concat([v_assign, v_assign_query], axis=0)
            return v_assign, next_view_adata
        # Split to next view based on transition rule
        # if transition is in obs
        if transition_rule in adata_query.obs.columns:
            if is_numeric_dtype(adata_query.obs[transition_rule]):
                X_similarity_atlas = curr_view_adata.obs[[transition_rule]].values
                X_similarity_query = adata_query.obs[[transition_rule]].values
            else:
                X_similarity_atlas = pd.get_dummies(curr_view_adata.obs[transition_rule])
                X_similarity_query = pd.get_dummies(adata_query.obs[transition_rule])

                # Check for missing levels
                missing_cols = [x for x in X_similarity_atlas.columns if x not in X_similarity_query.columns]
                if len(missing_cols) > 0:
                    X_similarity_query[missing_cols] = 0
                    X_similarity_query = X_similarity_query[X_similarity_atlas.columns].copy()

                X_similarity_atlas = X_similarity_atlas.values
                X_similarity_query = X_similarity_query.values

        if transition_rule in adata_query.obsm:
            X_similarity_atlas = curr_view_adata.obsm[transition_rule]
            X_similarity_query = adata_query.obsm[transition_rule]
        # X_similarity = curr_view_adata.obsm[transition_rule]
    else:
        raise ValueError(f"No transition rule defined for {current_view} -> {next_view}")

    # Get assignment to next view
    Y_assign_atlas = v_assign.loc[curr_view_adata.obs_names].values.ravel()

    v_assign_query = assign_from_similarity(Y_assign_atlas, X_similarity_atlas, X_similarity_query, v_assign)
    v_assign_query.index = adata_query.obs_names

    v_assign = pd.concat([v_assign, v_assign_query], axis=0)
    next_view_adata = concatenate_query(
        mvatlas,
        adata_query[v_assign_query[next_view] == 1],
        next_view,
        batch_key=batch_key,
        batch_categories=batch_categories,
    )

    return v_assign, next_view_adata


import re
import scrapy

base_url = "https://www.senscritique.com/liste/Cine_2017_sur_un_coin_de_table/1562107"

class SC_Spider(scrapy.Spider):
    name = "liste"
    start_urls = [base_url]
    summary_done = False
    current_page = 1

    def parse(self, response):
        if not self.summary_done:
            list_title = re.sub('\s+', ' ', response.css('h1.d-heading1::text').extract_first())
            list_desc = str.join('\n', response.css('div.d-grid')[2].css('p::text').extract())
            yield {'list_title': list_title, 'list_desc': list_desc}
            self.summary_done = True

        list_fragment = response.css('div.elli-content')
        for item in list_fragment:
            title = item.css('a.elco-anchor::text').extract_first()
            try:
                comment = str.join('\n', item.css('div.elli-annotation-content::text').extract())
            except:
                comment = ''
            yield {'title': title, 'comment': comment}

        try:
            if self.current_page > 11:
                raise Exception
            self.current_page += 1
            next_page = response.css('div.eipa-interface')[0].css('li.eipa-page')[min(self.current_page, 7)].css('a.eipa-anchor::attr(href)').extract_first()
            idx = next_page.rfind('-')
            next_page = next_page[:idx+1] + str(self.current_page)
        except:
            next_page = None
        if next_page is not None:
            yield response.follow(next_page, self.parse)


# USAGE
# python extract_features.py --dataset ../datasets/kaggle_dogs_vs_cats/train --output ../datasets/kaggle_dogs_vs_cats/hdf5/features.hdf5
from keras.applications import ResNet50, imagenet_utils
from keras.preprocessing.image import load_img, img_to_array
from sklearn.preprocessing import LabelEncoder
from pyimagesearch.io import HDF5DatasetWriter
from imutils import paths
import numpy as np
import progressbar
import os
import random
import argparse

# construct argument parser and parse the argument
ap = argparse.ArgumentParser()
ap.add_argument('-d', '--dataset', required=True, help='path to input dataset')
ap.add_argument('-o', '--output', required=True, help='path to output hdf5 file')
ap.add_argument('-b', '--buffer-size', type=int, default=1000, help='size of feature extraction buffer')
ap.add_argument('-s', '--batch-size', type=int, default=32, help='batch size of image to be passed through feature extractor')
args = vars(ap.parse_args())

# construct batch size for convenience usr
batch_size = args['batch_size']

# grab the list of image paths and extract labels through them
print('[INFO] loading images...')
image_paths = list(paths.list_images(args['dataset']))
random.shuffle(image_paths)
labels = [image_path.split(os.path.sep)[-1].split('.')[0] for image_path in image_paths]
le = LabelEncoder()
labels = le.fit_transform(labels)

# load model
print('[INFO] loading model...')
model = ResNet50(weights='imagenet', include_top=False, pooling='avg')

# initialize HDF5 dataset writer
dataset = HDF5DatasetWriter((len(labels), 2048), image_paths, data_key='features', buf_size=args['buffer_size'])
dataset.storeClassLabels(le.classes_)

# construct progress bar
widgets = ['Extracting features: ', progressbar.Percentage(), ' ', progressbar.Bar(), ' ', progressbar.ETA()]
pbar = progressbar.ProgressBar(maxval=len(labels), widgets=widgets).start()

# loop over batches of images
for i in range(0, len(labels), batch_size):
    batch_paths = image_paths[i:i+batch_size]
    batch_labels = labels[i:i+batch_size]
    batch_images = []

    # loop over images
    for image_path in batch_paths:
        # load image and convert it to Keras-compatitble array
        image = load_img(image_path, target_size=(224, 224))
        image = img_to_array(image)
        # add extra dimension
        image = np.expand_dims(image, axis=0)
        # preprocess by Resnet50
        image = imagenet_utils.preprocess_input(image)
        # add to batch
        batch_images.append(image)

    # pass batch through Resnet50
    batch_images = np.vstack(batch_images)
    features = model.predict(batch_images, batch_size=batch_size)
    features = features.reshape((len(features), -1))
    # add to hdf5 dataset
    dataset.add(features, batch_labels)
    pbar.update(i)

# close dataset
dataset.close()
pbar.finish()


from django.http import JsonResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from django.urls import reverse

from ..filters.CRPFilter import CRPFilter
from ..forms.CRPForm import UpdateCRPForm, CRPTrackForm, AddCRPForm
from django.forms import modelformset_factory
from datetime import datetime, timedelta
from ..forms.ProductCenterForm import CapacitySchedulingFormCRP
from ..models.CRP import CRPTrack, CRP
from ..models.Finished import Finished
from ..models.OperationList import OperationList
from ..models.ProductCenter import ProductCenter, CapacityScheduling
from ..models.OperationSequence import OperationSequence
from django.contrib import messages

list_crp_props = {
    "title": "CRP",

}

post_crp_props = {
    "title": "Add CRP",

}

views_crp_props = {
    "title": "View CRP",

}

update_crp_props = {
    "title": "Update CRP",
    'btn_name': 'Update'
}

copy_crp_props = {
    "title": "Copy CRP",
    'btn_name': 'Copy'

}

def ListCRP(request):
    if request.method == "GET":
        crp_track = CRPTrack.objects.all()
        list_crp_props['crp_track'] = crp_track
        list_crp_props['filters'] = CRPFilter()
        return render(request, 'PP/planning/crp/list.html', context=list_crp_props)

def FilterCRP(request):
    if request.method == 'POST':
        filter = CRPFilter(request.POST, queryset=CRPTrack.objects.all())
        if filter.is_valid():
            if len(filter.qs.values()) != 0:
                return JsonResponse({
                    'CRPTrack': list(filter.qs.values()),
                    'mass': 'success',
                }, status=200)
            else:
                return JsonResponse({
                    'mass': 'error',
                }, status=200)
        else:
            return JsonResponse({
                'mass': 'error',
            })
    else:
        return JsonResponse({
            'mass': 'error',
        })

def PostCRP(request):
    if request.method == "POST":
        finish = request.POST.get('finished_no')
        order_ref = request.POST.get('order_ref')
        order_qty = request.POST.get('order_qty')

        if request.is_ajax():
            finish = Finished.objects.get(id=finish)
            opl = OperationList.objects.filter(id=finish.operations_list.id).values()
            opsec = OperationSequence.objects.filter(operation_list=finish.operations_list).values()
            crp = []
            for ops in opsec:
                pc = ProductCenter.objects.get(id=ops['production_center_id'])
                cs = CapacityScheduling.objects.filter(ProductCenterId=ops['production_center_id']).last()
                crp.append({
                    'operationSequence': ops['operationSequence'],
                    'productioncneter': pc.product_center_name,
                    'AvalStartDate': cs.Date,
                    'reqdcapunit': ops['reqdcapunit'],
                    'ReqdMcHrByUnit': ops['totaltime'],
                    'AvalStartTime': cs.EndTime,
                    'AvalMcHrOrDay': cs.BalMcOrHour,
                    'NoOfMCByResAval': int(pc.NoOfMByC) - int(cs.NoOfMCAlloctd),
                    'AvalCAPByDay': cs.BalanceCap,
                })

            crp_track_form = CRPTrackForm({'finished': finish,
                                           'operationlist': finish.operations_list.id,
                                           'product_description': finish.product_description,
                                           'order_ref': order_ref,
                                           'order_qty': order_qty
                                           })

            if crp_track_form.is_valid():
                crp_track_form = crp_track_form.save()
                return JsonResponse({
                    'mass': "success",
                    'operation_scheduling': list(opsec),
                    'crps': crp,
                    'CRPTrack': list(CRPTrack.objects.filter(id=crp_track_form.id).values())
                }, status=200)

        operationSequence = request.POST.getlist('operationSequence')
        productioncneter = request.POST.getlist('productioncneter')
        AvalStartDate = request.POST.getlist('AvalStartDate')
        StartDate = request.POST.getlist('StartDate')
        NoOfMCByResAval = request.POST.getlist('NoOfMCByResAval')
        AvalCAPByDay = request.POST.getlist('AvalCAPByDay')
        reqdcapunit = request.POST.getlist('reqdcapunit')
        ReqdCAPByDay = request.POST.getlist('ReqdCAPByDay')
        AvalMcHrOrDay = request.POST.getlist('AvalMcHrOrDay')
        ReqdMcHrByUnit = request.POST.getlist('ReqdMcHrByUnit')
        ReqdMcHour = request.POST.getlist('ReqdMcHour')
        AvalStartTime = request.POST.getlist('AvalStartTime')
        StartTime = request.POST.getlist('StartTime')
        EndTime = request.POST.getlist('EndTime')
        EndDate = request.POST.getlist('EndDate')
        NoOfMcByRes = request.POST.getlist('NoOfMcByRes')
        mc_id_no = request.POST.getlist('mc_id_no')
        crp_track = request.POST.getlist('crp_track')
        mass = False
        if len(operationSequence) != 0:
            for x in range(0, len(operationSequence)):
                crp = AddCRPForm({
                    'crp_track': CRPTrack.objects.get(id=crp_track[x]),
                    'operationSequence': operationSequence[x],
                    "productioncneter": productioncneter[x],
                    "reqdcapunit": reqdcapunit[x],
                    "ReqdMcHrByUnit": ReqdMcHrByUnit[x],
                    "AvalStartDate": AvalStartDate[x],
                    'AvalStartTime': AvalStartTime[x],
                    "NoOfMCByResAval": NoOfMCByResAval[x],
                    "AvalCAPByDay": AvalCAPByDay[x],
                    'AvalMcHrOrDay': AvalMcHrOrDay[x],
                    'StartDate': StartDate[x],
                    'ReqdCAPByDay': ReqdCAPByDay[x],
                    'ReqdMcHour': ReqdMcHour[x],
                    'StartTime': StartTime[x],
                    'EndTime': EndTime[x],
                    'EndDate': EndDate[x],
                    'NoOfMcByRes': NoOfMcByRes[x],
                    'mc_id_no': mc_id_no[x]
                })

                if crp.is_valid():
                    crp = crp.save()
                    crp_tracks = CRPTrack.objects.get(id=crp.crp_track)

                    BalanceCap = int(crp.AvalCAPByDay) - int(crp.ReqdCAPByDay)

                    cs = CapacitySchedulingFormCRP({
                        'ProductCenterId': ProductCenter.objects.get(product_center_name=crp.productioncneter),
                        'Date': crp.AvalStartDate,
                        'AvalCapOrDay': crp.AvalCAPByDay,
                        'CapALlloctdTo': crp_tracks.finished.id,
                        'AlloctdCap': crp.ReqdCAPByDay,
                        'BalanceCap': BalanceCap,
                        'AvalMcOrResHour': crp.AvalMcHrOrDay,
                        'ReqdMcOrResHour': crp.ReqdMcHour,
                        'BalMcOrHour': str(float(crp.AvalMcHrOrDay) - float(crp.ReqdMcHour)),
                        'StartTime': crp.StartTime,
                        'EndTime': crp.EndTime,
                        'NoOfMCAlloctd': crp.NoOfMcByRes,

                    })
                    if cs.is_valid():
                        cs.save()
                    if BalanceCap == 0:
                        AvalStartDates = datetime.strptime(str(crp.AvalStartDate), '%Y-%m-%d')
                        AvalStartDates = AvalStartDates + timedelta(days=1)
                        pc = ProductCenter.objects.get(product_center_name=crp.productioncneter)
                        csl = CapacityScheduling.objects.filter(ProductCenterId=pc.id).first()
                        cs = CapacitySchedulingFormCRP({
                            'ProductCenterId': ProductCenter.objects.get(product_center_name=crp.productioncneter),
                            'Date': AvalStartDates,
                            'AvalCapOrDay': csl.AvalCapOrDay,
                            'CapALlloctdTo': csl.CapALlloctdTo,
                            'AlloctdCap': csl.AlloctdCap,
                            'BalanceCap': csl.BalanceCap,
                            'AvalMcOrResHour': csl.AvalMcOrResHour,
                            'ReqdMcOrResHour': csl.ReqdMcOrResHour,
                            'BalMcOrHour': csl.BalMcOrHour,
                            'StartTime': csl.StartTime,
                            'EndTime': csl.EndTime,
                            'NoOfMCAlloctd': csl.NoOfMCAlloctd,
                        })
                        if cs.is_valid():
                            cs.save()
                    mass = True
                else:
                    mass = False
            if mass:
                messages.success(request, 'CRP Created Successfully.')
            return HttpResponseRedirect(reverse('productionplanning:CRP', args={crp_track[0]}))
        else:
            messages.error(request, 'CRP Not Created Successfully.')

        return render(request, 'PP/Planning/crp/general_data_form.html', context=post_crp_props)
    else:
        return render(request, 'PP/Planning/crp/general_data_form.html', context=post_crp_props)

def AddCRP(request, id):
    crp_track = CRPTrack.objects.get(id=id)
    crp = CRP.objects.filter(crp_track=crp_track.id)
    post_crp_props['crp_track'] = crp_track
    post_crp_props['crp'] = crp
    return render(request, 'PP/Planning/crp/add_crp.html', context=post_crp_props)

def ViewCRP(request, id):
    crp_track = CRPTrack.objects.get(id=id)
    crp = CRP.objects.filter(crp_track=crp_track.id)
    views_crp_props['crp_track'] = crp_track
    views_crp_props['crp'] = crp
    return render(request, 'PP/Planning/crp/views.html', context=views_crp_props)

def UpdateCRP(request, id):
    crp_track = CRPTrack.objects.get(id=id)
    crp = CRP.objects.filter(crp_track=crp_track.id)
    update_crp_props['crp_track'] = crp_track
    update_crp_props['crp'] = crp
    CRPFoemSet = modelformset_factory(CRP, form=UpdateCRPForm, extra=0)
    formset = CRPFoemSet(queryset=crp)
    update_crp_props['formset'] = formset
    if request.POST:
        formset = CRPFoemSet(request.POST, queryset=crp)
        if formset.has_changed():
            if formset.is_valid():
                formset.save()
                messages.success(request, 'CRP Update Successfully.')
                return HttpResponseRedirect(reverse('productionplanning:UpdateCRP', args={crp_track.id}))
            else:
                messages.error(request, 'CRP Form is not valid.')
                return HttpResponseRedirect(reverse('productionplanning:UpdateCRP', args={crp_track.id}))
        else:
            messages.error(request, 'Value is not Change.')
            return HttpResponseRedirect(reverse('productionplanning:UpdateCRP', args={crp_track.id}))
    else:
        return render(request, 'PP/Planning/crp/update_crp.html', context=update_crp_props)

def CopyCRP(request, id):
    crp_track = CRPTrack.objects.get(id=id)
    crp = CRP.objects.filter(crp_track=crp_track.id)
    copy_crp_props['crp_track'] = crp_track
    copy_crp_props['crp'] = crp
    CRPFoemSet = modelformset_factory(CRP, form=UpdateCRPForm, extra=0)
    formset = CRPFoemSet(queryset=crp)
    copy_crp_props['formset'] = formset
    if request.POST:
        mass = []
        crp_trackForm = CRPTrackForm(data={
            'finished': crp_track.finished,
            'operationlist': crp_track.operationlist,
            'product_description': crp_track.product_description,
            'order_ref': crp_track.order_ref,
            'order_qty': crp_track.order_qty,
        })
        if crp_trackForm.is_valid():
            crp_trackForm = crp_trackForm.save()
            for x in crp:
                dirs = {
                    'crp_track': CRPTrack.objects.get(id=crp_trackForm.id),
                    'operationSequence': x.operationSequence,
                    'productioncneter': x.productioncneter,
                    'AvalStartDate': x.AvalStartDate,
                    'StartDate': x.StartDate,
                    'reqdcapunit': x.reqdcapunit,
                    'ReqdMcHrByUnit': x.ReqdMcHrByUnit,
                    'AvalStartTime': x.AvalStartTime,
                    'AvalMcHrOrDay': x.AvalMcHrOrDay,
                    'NoOfMCByResAval': x.NoOfMCByResAval,
                    'AvalCAPByDay': x.AvalCAPByDay,
                    'ReqdCAPByDay': x.ReqdCAPByDay,
                    'ReqdMcHour': x.ReqdMcHour,
                    'StartTime': x.StartTime,
                    'EndTime': x.EndTime,
                    'EndDate': x.EndDate,
                    'NoOfMcByRes': x.NoOfMcByRes,
                    'mc_id_no': x.mc_id_no,
                }
                crp_form = UpdateCRPForm(data=dirs)
                print(2,crp_form.is_valid())
                if crp_form.is_valid():
                    crp_form = crp_form.save(commit=False)
                    crp_form.crp_track = CRPTrack.objects.get(id=crp_trackForm.id)
                    crp_form.save()
                    mass.append(True)
                else:
                    mass.append(False)
            if len(mass) == mass.count(True):
                messages.success(request, '{} to {} Copy Successfully.'.format(id, crp_trackForm.id))
                return redirect('/productionplanning/crp/')
            else:
                messages.error(request, 'Copy Not Successfully.')
                return redirect('/productionplanning/crp/')
        else:
            messages.error(request, "CRP Form isn't Valid.")
            return redirect('/productionplanning/crp/')

    else:
        return render(request, 'PP/Planning/crp/update_crp.html', context=copy_crp_props)

def DeleteCRP(request, id):
    crp = CRPTrack.objects.get(id=id)
    crp.delete()
    messages.error(request, '{} Delete Successfully.'.format(id))
    return redirect('/productionplanning/crp/')


import requests
from bs4 import BeautifulSoup
import csv
from os import getcwd

print('\n'*3)
print("         /~________~\  .----------. (| L0L Pull |) '----------'  \_~~~~~~~~_/ ")
print('\n'*3)
print("                                                 .: Author- Jean Paul :.      ")
print('\n'*3)

current_directory=getcwd()
print("               Your Current Working Directory is  : \n",current_directory)
outpath=current_directory+"\\l0lB@S_Output.csv"
myFile = open(outpath, 'a+',newline='')
csv_out = csv.writer(myFile)
csv_out.writerow(["Name","BInary_Link","Function","Code(s)"])
url="https://lolbas-project.github.io"
r=requests.get(url)
print('_'*60)
print("Connected to LOLBAS: ",url)
print('\n'*3)
soup=BeautifulSoup(r.content)

for tag in soup.find_all('a','bin-name'):
    hrefs=tag['href'].replace("//","")
    urls=url+hrefs
    for ur in [urls]:
        nr=requests.get(ur)
        sups=BeautifulSoup(nr.content, "html.parser")
        print('_'*60)
        funclist=[[t.text for t in f.find_all('a')] for f in sups.find_all('ul','function-list')]
        code=[i.text for i in sups.find_all('code')]
        print("Working on binary",tag.text," at location ",ur," ::::Contains ",funclist)
    csv_out.writerow([tag.text,urls,funclist,code])


import os
import fnmatch
import datetime
from lxml import etree
from openpyxl import Workbook
from itertools import count
try:
    # Python 2
    from itertools import izip
except ImportError:
    # Python 3
    izip = zip

def save_test_result_to_excel_file():
    tc_name_list = []
    tc_status_list = []
    tc_error_list = []
    tc_tag_list = []
    xml_files = get_all_xml_files()
    for each_xml_file in xml_files:
        context = etree.iterparse(each_xml_file, events=('end',))
        for event, elem in context:
            if elem.tag == 'test':
                tc_name_attrib = elem.attrib['name']
                tc_name_list.append(tc_name_attrib)
                tc_status_list.append(get_test_status_path(
                    elem, tc_name_attrib)[0].attrib['status'])
                tc_error_list.append(get_test_status_path(elem, tc_name_attrib)[0].text)
                tc_tag_list.append(get_test_tags(elem, tc_name_attrib))
                elem.clear()
                while elem.getprevious() is not None:
                    del elem.getparent()[0]
        del context

    date_stamp = '{:%Y-%m-%d-%H%M%S}'.format(datetime.datetime.now())
    result_file = 'test_result-' + date_stamp + '.xlsx'
    wb = Workbook()
    ws = wb.worksheets[0]
    ws['A1'] = 'Test Case'
    ws['B1'] = 'Status'
    ws['C1'] = 'Error Details'
    ws['D1'] = 'Tags'
    ws['E1'] = 'Comments'
    for i, name, status, error, tags in izip(count(), tc_name_list, tc_status_list, tc_error_list, tc_tag_list):
        ws['A' + str(i + 2)] = name
        ws['B' + str(i + 2)] = status
        ws['C' + str(i + 2)] = error
        ws['D' + str(i + 2)] = ', '.join(tags)
    ws.auto_filter.ref = 'A1:E1'
    wb.save(filename=result_file)

def get_test_status_path(elem, tc_name_attrib):
    if "'" in tc_name_attrib:
        tc_name_quoted = '"%s"' % tc_name_attrib
        return elem.xpath("//test[@name=" + tc_name_quoted + "]/status")
    else:
        return elem.xpath("//test[@name='" + tc_name_attrib + "']/status")

def get_test_tags(elem, tc_name_attrib):
    if "'" in tc_name_attrib:
        tc_name_quoted = '"%s"' % tc_name_attrib
        return elem.xpath("//test[@name=" + tc_name_quoted + "]/tags/tag/text()")
    else:
        return elem.xpath("//test[@name='" + tc_name_attrib + "']/tags/tag/text()")

def get_all_xml_files():
    xml_files = []
    for root, dirnames, filenames in os.walk('.'):
        for filename in fnmatch.filter(filenames, '*.xml'):
            xml_files.append(os.path.join(root, filename))
    return xml_files

if __name__ == '__main__':
    save_test_result_to_excel_file()


import json
import random

for i in range(20):
  num = random.randint(0, 7)
  cow = "Cow"
  if num == 6 or num == 7:
    cow = "Not Cow"
  data = {"name": "Cowpoly #{0}".format(i),
    "tokenID": i, "description":  "Cowpoly is an experimental NFT collection created for educational purpose only. Credits for all artwork go to original creators.",
    "image": "https://github.com/eddiekaung/cowpolyNFT/raw/main/images/{0}.jpg".format(num),
    "attributes": [
      {"trait_type": "Type", "value": cow}
    ]}
  with open('{0}.json'.format(i), 'w') as f:
      json.dump(data, f)


#-*- coding: utf-8 -*-
"""

@author:		Jordi Fernández
@contact:		jordi.fernandez@whads.com
@organization:	Whads/Accent SL
@since:			September 2010
"""
import cherrypy
from cocktail.events import when
from cocktail import schema
from cocktail.translations import translations
from cocktail.controllers import context
from woost.models import Extension, Document, Template, User
from woost.models.permission import DeletePermission, ModifyPermission

translations.define("MailerExtension",
    ca = u"Enviament de documents per correu electrònic",
    es = u"Envio de documentos por correo electrónico",
    en = u"Sending documents by email"
)

translations.define("MailerExtension-plural",
    ca = u"Enviament de documents per correu electrònic",
    es = u"Envio de documentos por correo electrónico",
    en = u"Sending documents by email"
)

class MailerExtension(Extension):

    def __init__(self, **values):
        Extension.__init__(self, **values)
        self.extension_author = u"Whads/Accent SL"
        self.set("description",
            u"""Permet enviar documents per correu electrònic.""",
            "ca"
        )
        self.set("description",
            u"""Permite enviar documents por correo electrónico.""",
            "es"
        )
        self.set("description",
            u"""Allows send documents by email.""",
            "en"
        )

    def _load(self):

        from woost.controllers.notifications import notify_user
        from woost.controllers.backoffice.basebackofficecontroller import \
            BaseBackOfficeController
        from woost.controllers.backoffice.itemcontroller import \
            ItemController

        from woost.extensions.mailer import (
            sendemailaction,
            createmailingaction,
            strings
        )
        from woost.extensions.mailer.mailing import Mailing, \
            RunningMailingError
        from woost.extensions.mailer.sendemailcontroller import \
            SendEmailController

        ItemController.send_email = SendEmailController

        Template.add_member(
            schema.Boolean(
                "per_user_customizable",
                default = False,
                listed_by_default = False
            )
        )
        Template.members_order.append("per_user_customizable")

        User.add_member(
            schema.Collection(
                "mailingLists",
                items = "woost.extensions.mailer.mailinglist.MailingList",
                bidirectional = True,
                listed_by_default = False
            )
        )

        @when(BaseBackOfficeController.exception_raised)
        def handle_exception_raised(event):
            if isinstance(
                event.exception,
                RunningMailingError
            ):
                notify_user(translations(event.exception), "error")
                raise cherrypy.HTTPRedirect(event.source.contextual_uri())

        # Disable interactive features from rendered pages when rendering
        # static content
        from woost.controllers.cmscontroller import CMSController

        @when(CMSController.producing_output)
        def disable_user_controls(event):
            if context.get("sending_email", False):
                event.output["show_user_controls"] = False


# -*- coding: utf-8 -*-
import math

from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect
from django.contrib import messages
# Create your views here.
from django.urls import reverse_lazy

from create_profile.forms import CreateFormsB
from create_profile.models import CreateNeighbour, CreateBusiness, LikeModel, LikeBusinesModel, AllCities#, \
    #Profession  # , LikeEndModelNe

def index(request):
    # Главная
    if request.user.is_authenticated:

        # при авторизации сообщение пользоваиелям у кого встречные лайки соседей
        all_likes = LikeModel.objects.filter(like_second=request.user).values('like_first')
        for all_likes_s in all_likes:
            # print('all_likes-Neighb')
            # print(all_likes)
            step_one = []
            step_two = []
            if len(all_likes) > 0:
                # print('шаг1 нашел')
                step_one = all_likes_s
                # шаг 2
                all_likes_two = LikeModel.objects.filter(like_first=request.user).values('like_second')
                if len(all_likes_two) > 0:
                    # print('шаг 2 нашел')
                    # print(all_likes_two)
                    step_two = all_likes_two
                    # ii = int(0)
                    # print('step_one :', step_one)
                    # print('step_two :', step_two)
                    for step_two_s in step_two:
                        if step_two_s['like_second'] == step_one['like_first']:

                            messages.success(request, 'Вас лайкнул пользователь: ' + step_two_s['like_second'])
                            email_second = LikeModel.objects.filter(like_second=step_two_s['like_second']).values('email_second')
                            messages.success(request, ' Напишите ему: ' + email_second[0]['email_second'])

        all_likes_b = LikeBusinesModel.objects.filter(like_second=request.user).values()
        for all_likes_s_b in all_likes_b:
            step_one_b = []
            step_two_b = []
            if len(all_likes_b) > 0:
                if len(all_likes_b) > 0:
                    step_one_b = all_likes_s_b
                    all_likes_b_two = LikeBusinesModel.objects.filter(like_first=request.user).values('like_second')
                    if len(all_likes_b_two) > 0:
                        step_two_b = all_likes_b_two

                        for all_likes_s_b_s in step_two_b:
                            if all_likes_s_b_s['like_second'] == step_one_b['like_first']:
                                messages.success(request, 'Вас лайкнул бизнес пользователь: ' + all_likes_s_b_s['like_second'])
                                email_second_b = LikeBusinesModel.objects.filter(like_second=all_likes_s_b_s['like_second']).values('email_second')
                                messages.success(request, 'Почта для связи: ' + email_second_b[0]['email_second'])

        return render(request, 'index.html',)
    else:
        return render(request, 'index.html', )

@login_required(login_url=reverse_lazy('login'))
def all_filter_search(request):
    # выводим все анкеты
    list_tmp_13 = 0

    allCities = AllCities.objects.all()
    catalog_filter = CreateNeighbour.objects.values_list('id',
                                                          'name',
                                                          'gender',
                                                          'gender_neighb',
                                                          # 'sel_city',
                                                          # 'sel_distr',
                                                          'presence_animals',
                                                          'presence_flat',
                                                          'attitude_animals',
                                                          'attitude_smok',
                                                          'about_me',
                                                          'image',
                                                          'user_neig',
                                                          'cities',
                                                          'regin',)

    tmp_append_goods = []
    tmp_categ_name = []
    tmp_categ_name_list = []
    # формируем масив лдя прорисовки в шблоне
    for all_goods_s in catalog_filter:
        tmp_append_goods.append(all_goods_s)
        for tmp_s in tmp_append_goods:

            p = CreateNeighbour(gender=tmp_s[2])
            gender = p.get_gender_display()

            p = CreateNeighbour(gender_neighb=tmp_s[3])
            gender_neighb = p.get_gender_neighb_display()

            # p = CreateNeighbour(sel_city=tmp_s[4])
            # sel_city = p.get_sel_city_display()
            #
            # p = CreateNeighbour(sel_distr=tmp_s[5])
            # sel_distr = p.get_sel_distr_display()

            p = CreateNeighbour(attitude_animals=tmp_s[6])
            attitude_animals = p.get_attitude_animals_display()

            p = CreateNeighbour(attitude_smok=tmp_s[7])
            attitude_smok = p.get_attitude_smok_display()

            cities_name = AllCities.objects.filter(name=tmp_s[11]).values('name')
            distr_name = AllCities.objects.filter(name=tmp_s[12]).values('name')
            # cities_b = p.get_cities_b_display()
            # print('allCities_name')
            #
            # print(cities_name[0]['name'])
            # print(distr_name[0]['name'])

            list_tmp = list(tmp_s)
            list_tmp[2] = gender
            list_tmp[3] = gender_neighb
            # list_tmp[4] = sel_city
            # list_tmp[5] = sel_distr
            list_tmp[6] = attitude_animals
            list_tmp[7] = attitude_smok

            list_tmp[11] = cities_name[0]['name']
            list_tmp[12] = distr_name[0]['name']
            user_like = LikeModel.objects.filter(first_id=tmp_s[10]).filter(second_id=request.user.id).values_list('id') # лайки в шаблоне
            flag = 0
            if len(user_like) > 0:
                flag = '1'
            else:
                flag = '0'
            list_tmp.append(flag)
            list_tmp = tuple(list_tmp)

            for list_tmp_s in [list_tmp]:
                tmp_categ_name_list = list_tmp_s
        tmp_categ_name.append(tmp_categ_name_list) # формируем массив со всеми анкетами
        # print('allCities')
        # print(allCities)

    return render(request, 'all_neighb.html', {'serxh_filer': tmp_categ_name, 'allCities': allCities,})

@login_required(login_url=reverse_lazy('login'))
def all_busines(request):
    # весь бизнес
    # формируем массив со всеми анкетами
    distinr = CreateBusiness.objects.order_by('category_bus').values('category_bus').distinct()
    print('distin254')
    print(distinr)
    allCities = AllCities.objects.all()
    form = CreateFormsB()
    search_filer = CreateBusiness.objects.values_list('name_bus',
                                                       # 'sel_city',
                                                       # 'sel_distr',
                                                       'category_bus',
                                                       'about_me',
                                                       'image',
                                                       'user_bus',
                                                       'id',
                                                       'cities',
                                                       'regin',)

    tmp_append_goods = []
    tmp_categ_name = []
    tmp_categ_name_list = []
    for all_goods_s in search_filer:
        tmp_append_goods.append(all_goods_s)
        for tmp_s in tmp_append_goods:

            # p = CreateBusiness(sel_city=tmp_s[1])
            # sel_city = p.get_sel_city_display()
            #
            # p = CreateBusiness(sel_distr=tmp_s[2])
            # sel_distr = p.get_sel_distr_display()

            # cities_name_b = AllCities.objects.filter(id=tmp_s[8]).values('name')
            # distr_name_b = AllCities.objects.filter(id=tmp_s[9]).values('name')
            # search_filer_pro = Profession.objects.filter(id=tmp_s[10]).values('name')
            # cities_b = p.get_cities_b_display()
            # print('search_filer_pro')
            # print(search_filer_pro[0]['name'])
            list_tmp = list(tmp_s)
            # list_tmp[1] = sel_city
            # list_tmp[2] = sel_distr
            # list_tmp[8] = cities_name_b[0]['name']
            # list_tmp[9] = distr_name_b[0]['name']
            # list_tmp[10] = search_filer_pro[0]['name']
            # list_tmp[11] = search_filer_pro[0]['name']
            # лайки
            user_like = LikeBusinesModel.objects.filter(first_id=tmp_s[4]).filter(second_id=request.user.id).values_list('id')
            flag = 0
            if len(user_like) > 0:
                flag = '1'
            else:
                flag = '0'
            list_tmp.append(flag)
            list_tmp = tuple(list_tmp)
            for list_tmp_s in [list_tmp]:
                tmp_categ_name_list = list_tmp_s

            # AllCities.cities_b.all()
        tmp_categ_name.append(tmp_categ_name_list)# формируем массив со всеми анкетами

    return render(request, 'all_busines.html', {'serxh_filer': tmp_categ_name, 'allCities': allCities, 'form': form, 'distinr': distinr})

def likes(request):
    # likes заносим в таблицу лайкующего и хозяйна анкеты
    test_user = User.objects.filter(id=request.POST.get('first')).values('username', 'email')
    nearly_final = LikeModel(like_first=test_user[0]['username'], email_first=test_user[0]['email'],
                             like_second=request.POST.get('second'), email_second=request.POST.get('email'),
                             first_id=request.POST.get('first'), second_id=request.POST.get('second_id'),
                             id_ancket=request.POST.get('id_ancket'),)
    nearly_final.save(force_insert=True)
    return redirect('/all_neighb/')

def likes_busines(request):
    # likes заносим в таблицу лайкующего и хозяйна анкеты
    test_user = User.objects.filter(id=request.POST.get('first')).values('username', 'email')
    nearly_final = LikeBusinesModel(like_first=test_user[0]['username'], email_first=test_user[0]['email'],
                                    like_second=request.POST.get('second'), email_second=request.POST.get('email'),
                                    first_id=request.POST.get('first'), second_id=request.POST.get('second_id'),
                                    id_ancket=request.POST.get('id_ancket'),)
    nearly_final.save(force_insert=True)
    return redirect('/all_busines/')

def statistics(request):
    # статистика

    count_us = User.objects.values('username', 'email')

    count_users = (len(count_us))
    all_likes = LikeModel.objects.values()
    count_stat = len(all_likes)/2
    all_likes_n = math.ceil(count_stat)
    all_likes_b = LikeBusinesModel.objects.values()
    count_stat = len(all_likes_b)/2
    all_likes_bus = math.ceil(count_stat)
    # for count_us_s in count_us:
    print(count_us[0]['username'])
    tmp =count_us[0]['username']
    all_likes_count = LikeModel.objects.filter(like_second=count_us[0]['username']).count()
    all_likes_count_two = LikeModel.objects.filter(like_first=count_us[0]['username']).count()

    if all_likes_count > 0 and all_likes_count_two > 0:
        # print('tmp')
        # print(tmp)
        all_likes_count_two_two = LikeModel.objects.filter(like_first=count_us[0]['username']).values()
        # print('all_likes_count_two_two ')
        # print(all_likes_count_two_two[0]['like_first'])
        # save_end_like = LikeEndModelNe(like_first=all_likes_count_two_two[0]['like_first'],
        #                                email_first=all_likes_count_two_two[0]['email_first'],
        #                                like_second=all_likes_count_two_two[0]['like_second'],
        #                                email_second=all_likes_count_two_two[0]['email_second'],)
        # print('save_end_like')
        # print(save_end_like)
        # save_end_like.save()
        # print(all_likes_count, all_likes_count_two)

    return render(request, 'statistics.html', {'count_users':count_users, 'all_likes_n': all_likes_n, 'all_likes_bus': all_likes_bus})

def tree_form(request):
    # по айди города получаем районы
    dict1 = {}
    # print(request.GET.get('id'))
    city_id = request.GET.get('id')
    all_child = AllCities.objects.filter(parent_id=city_id).values('name', 'id')
    # print('all_child')
    # print(all_child)
    dict1['data']=list(all_child)# словарь в словаре для передачи json
    # print('dict1')
    # print(dict1)
    return JsonResponse(dict1)

def tree_form_b(request):
    dict2 = {}
    print(request.GET.get('id'))
    city_id = request.GET.get('id')
    all_child = AllCities.objects.filter(parent_id=city_id).values('name', 'id')
    print('all_child_b-450')
    print(all_child)
    # return HttpResponse('1')

    dict2['data']=list(all_child)
    print('dict1_b-456')
    print(dict2)
    # return JsonResponse({'all_child': all_child, 'status': 'ok', 'dict1': dict1})
    return JsonResponse(dict2)
    # return HttpResponse({'all_child': all_child,})

def search_fetch(request):
    # по айди города, района и пол соседа получаем анкты
    print('distr-461')
    print(request.GET.get('id'))

    print('seacrprof464')
    print(request.GET.get('distr'))
    print(request.GET)
    cities_name = AllCities.objects.filter(id=request.GET.get('id')).values('name')
    distr_name = AllCities.objects.filter(id=request.GET.get('distr')).values('name')
    catalog_filter=CreateNeighbour.objects.filter(cities=cities_name[0]['name'],
                                                  regin=distr_name[0]['name'],
                                                  gender=request.GET.get('ender')).\
                                                                values_list('id',
                                                                             'name',
                                                                             'gender',
                                                                             'gender_neighb',
                                                                             # 'sel_city',
                                                                             # 'sel_distr',
                                                                             'presence_animals',
                                                                             'presence_flat',
                                                                             'attitude_animals',
                                                                             'attitude_smok',
                                                                             'about_me',
                                                                             'image',
                                                                             'user_neig',
                                                                             'cities',
                                                                             'regin',)
    print('catalog_filter')
    print(catalog_filter)
    tmp_append_goods = []
    tmp_categ_name = []
    tmp_categ_name_list = []
    # формируем масив лдя прорисовки в шблоне
    for all_goods_s in catalog_filter:
        tmp_append_goods.append(all_goods_s)
        for tmp_s in tmp_append_goods:

            p = CreateNeighbour(gender=tmp_s[2])
            gender = p.get_gender_display()

            p = CreateNeighbour(gender_neighb=tmp_s[3])
            gender_neighb = p.get_gender_neighb_display()

            # p = CreateNeighbour(sel_city=tmp_s[4])
            # sel_city = p.get_sel_city_display()
            #
            # p = CreateNeighbour(sel_distr=tmp_s[5])
            # sel_distr = p.get_sel_distr_display()

            p = CreateNeighbour(attitude_animals=tmp_s[4])
            attitude_animals = p.get_attitude_animals_display()

            p = CreateNeighbour(attitude_smok=tmp_s[5])
            attitude_smok = p.get_attitude_smok_display()

            # cities_name = AllCities.objects.filter(id=tmp_s[11]).values('name')
            # distr_name = AllCities.objects.filter(id=tmp_s[12]).values('name')

            list_tmp = list(tmp_s)
            list_tmp[2] = gender
            list_tmp[3] = gender_neighb
            # list_tmp[4] = sel_city
            # list_tmp[5] = sel_distr
            list_tmp[4] = attitude_animals
            list_tmp[5] = attitude_smok

            list_tmp[11] = cities_name[0]['name']
            list_tmp[12] = distr_name[0]['name']
            user_like = LikeModel.objects.filter(first_id=tmp_s[10]).filter(second_id=request.user.id).values_list('id') # лайки в шаблоне
            flag = 0
            if len(user_like) > 0:
                flag = '1'
            else:
                flag = '0'
            list_tmp.append(flag)
            list_tmp = tuple(list_tmp)

            for list_tmp_s in [list_tmp]:
                tmp_categ_name_list = list_tmp_s
        tmp_categ_name.append(tmp_categ_name_list) # формируем массив со всеми анкетами
        print('allCities')
        print(tmp_categ_name)
    return render(request, 'filtr_neig.html', {'filter_ne':tmp_categ_name})
    # return JsonResponse(dict1)

def search_fetch_b(request):
    # dict1 = {}

    # filter_ne= CreateNeighbour.objects.filter(cities_id=request.GET.get('id')).values()
    # print('filter_ne')
    # print(filter_ne)
    print('distr-513')
    print(request.GET.get('id'))

    print('seacrprof546')
    print(request.GET.get('distr'))
    print('seacrprof-548')
    print(request.GET.get('prof'))
    # dict1['data']=list(filter_ne)
    allCities = AllCities.objects.all()
    # catalog_filter = CreateNeighbour.objects. \
    # catalog_filter=CreateNeighbour.objects.filter(cities_id=request.GET.get('id'),districts_tree=request.GET.get('distr'),gender=request.GET.get('ender')). \, category_bus=request.GET.get('prof'))\
    cities_name = AllCities.objects.filter(id=request.GET.get('id')).values('name')
    distr_name = AllCities.objects.filter(id=request.GET.get('distr')).values('name')
    search_filer = CreateBusiness.objects.filter(cities=cities_name[0]['name'],
                                                 regin=distr_name[0]['name'],
                                                 category_bus=request.GET.get('prof')).values_list('name_bus',
                                                                                # 'sel_city',
                                                                                # 'sel_distr',
                                                                                'category_bus',
                                                                                'about_me',
                                                                                'image',
                                                                                'user_bus',
                                                                                'id',
                                                                                'cities',
                                                                                'regin',)

    print('search_filer-561')
    print(search_filer)
    tmp_append_goods = []
    tmp_categ_name = []
    tmp_categ_name_list = []
    for all_goods_s in search_filer:
        tmp_append_goods.append(all_goods_s)
        for tmp_s in tmp_append_goods:

            # p = CreateBusiness(sel_city=tmp_s[1])
            # sel_city = p.get_sel_city_display()
            #
            # p = CreateBusiness(sel_distr=tmp_s[2])
            # sel_distr = p.get_sel_distr_display()

            list_tmp = list(tmp_s)
            # list_tmp[1] = sel_city
            # list_tmp[2] = sel_distr
            user_like = LikeBusinesModel.objects.filter(first_id=tmp_s[4]).filter(second_id=request.user.id).values_list('id')
            flag = 0
            if len(user_like) > 0:
                flag = '1'
            else:
                flag = '0'
            list_tmp.append(flag)
            list_tmp = tuple(list_tmp)
            for list_tmp_s in [list_tmp]:
                tmp_categ_name_list = list_tmp_s
        tmp_categ_name.append(tmp_categ_name_list)# формируем массив со всеми анкетами
        print('tmp_categ_name-598')
        print(tmp_categ_name)
    return render(request, 'test_busines.html', {'serxh_filer': tmp_categ_name})
    # return JsonResponse(dict1)


import numpy as np
import random as rnd
import matplotlib.pyplot as plt

CORDS = []
avaibleNodes = []
border = 30

x0y0 = [0,0]

CORDS.append(x0y0)

avaibleNodes.append([x0y0[0]+1,x0y0[1]])
avaibleNodes.append([x0y0[0],x0y0[1]+1])
avaibleNodes.append([x0y0[0]-1,x0y0[1]])
avaibleNodes.append([x0y0[0],x0y0[1]-1])

def AddMass():
	global CORDS, avaibleNodes
	tempCords = []
	NavaibleNodes = len(avaibleNodes)
	rand = rnd.randint(0,NavaibleNodes-1)
	tempCords = avaibleNodes[rand]
	CORDS.append(tempCords)
	avaibleNodes.remove(tempCords)
	newNodes = []
	UpdateANodes(tempCords)
	for node in avaibleNodes:
		if node in CORDS:
			avaibleNodes.remove(node)

def UpdateANodes(point):
	global avaibleNodes, CORDS
	newNodes = []
	newNodes.append([point[0]+1,point[1]])
	newNodes.append([point[0],point[1]+1])
	newNodes.append([point[0]-1,point[1]])
	newNodes.append([point[0],point[1]-1])
	for newNode in newNodes:
		if newNode in CORDS:
			newNodes.remove(newNode)
	for newNode in newNodes:
		if newNode in avaibleNodes:
			newNodes.remove(newNode)
	for newNode in newNodes:
		avaibleNodes.append(newNode)

counter = 0

while True:
	counter +=1
	AddMass()
	if CORDS[-1][0] >= border or CORDS[-1][1] >= border or CORDS[-1][0] <= -border or CORDS[-1][1] <= -border:
		break

CordsX = []
CordsY = []

aNodesX = []
aNodesY = []

for cord in CORDS:
	CordsX.append(cord[0])
	CordsY.append(cord[1])

for node in avaibleNodes:
	aNodesX.append(node[0])
	aNodesY.append(node[1])

COLORS = [
		 (1,0,0),
		 (1,127/255,0),
		 (1,1,0),
		 (0,1,0),
		 (0,0,1),
		 (75/255,0,130/255),
		 (148/255,0,211/255)
		 ]

plt.rcParams['axes.facecolor']='black'
for i in range(7):
	plt.plot(CordsX[i*int(counter/7):(i+1)*int(counter/7)],CordsY[i*int(counter/7):(i+1)*int(counter/7)],'.',color=COLORS[i])
plt.plot(aNodesX,aNodesY,'.',color='white')
plt.show()


import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np

img= cv.imread("cat/cat.png")
#cv.imshow("cat",img)
def rescale(frame, scale=0.5):
    width = int(frame.shape[1]*scale)
    height = int(frame.shape[0]*scale)
    dims=(width,height)
    return cv.resize(frame, dims, interpolation=cv.INTER_AREA)
resize_image=rescale(img)
surf = cv.xfeatures2d.SURF_create(800)
cv.imshow('Image',resize_image)

cv.waitKey(0)


from typing import List

LAND = '1'
WATER = '0'

# TODO: Review a superior solutions

def overlaps(min1, max1, min2, max2):
    overlap = max(0, min(max1, max2) - max(min1, min2))
    if overlap > 0:
        return True
    if min1 == min2 or min1 == max2 or max1 == min2 or max1 == max2:
        return True
    if (min1 > min2 and max1 < max2) or (min2 > min1 and max2 < max1):
        return True
    return False

print(overlaps(0, 2, 1, 1))

# Definition for a Bucket.
class Bucket:
    def __init__(self, identifiers: List[int]):
        self.destination = None
        self.identifiers = set(identifiers)

    def hasDestination(self) -> bool:
        return self.destination != None

    def getDestination(self):
        if not self.hasDestination():
            return self
        return self.destination.getDestination()

    def combine(self, bucket):
        otherDestination = bucket.getDestination()
        thisDestination = self.getDestination()
        uniqueIdentifiers = otherDestination.identifiers | thisDestination.identifiers
        newBucket = Bucket(uniqueIdentifiers)
        otherDestination.destination = newBucket
        thisDestination.destination = newBucket

        return newBucket

    def contains(self, identifier: int) -> bool:
        return identifier in self.getDestination().identifiers

class Solution:
    '''
    Given a 2d grid map of '1's (land) and '0's (water), count the number of islands.
    An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.
    You may assume all four edges of the grid are all surrounded by water.

    '''
    def numIslands(self, grid: List[List[str]]) -> int:
        if len(grid) < 1:
            return 0

        nextRowIsland = 1
        rowIslands = {}
        currentRowIslandStart = None
        '''
        Here we are generating row islands that we will then be pairing with adjacent row islands to form
        groups that we will then combine into the true islands that are needed to get the correct answer
        '''
        for rowIndex, row in enumerate(grid):
            lastSpot = WATER
            lengthOfRow = len(row)
            rowIslands[rowIndex] = []
            for spotIndex, spot in enumerate(row):
                if lastSpot == WATER and spot == LAND:
                    currentRowIslandStart = spotIndex

                if spotIndex + 1 >= lengthOfRow and spot == LAND:
                    rowIslands[rowIndex].append((nextRowIsland, currentRowIslandStart, spotIndex))
                    nextRowIsland += 1
                    currentRowIslandStart = None
                elif spot == WATER and currentRowIslandStart != None:
                    rowIslands[rowIndex].append((nextRowIsland, currentRowIslandStart, spotIndex - 1))
                    nextRowIsland += 1

                if spot == WATER:
                    currentRowIslandStart = None

                lastSpot = spot

        nextGroup = 1
        maxRowIndex = len(grid)
        rowIslandsToGroups = {}
        for rowNumber in [rowNumber for rowNumber in range(maxRowIndex)]:
            for rowIslandNumber, startIndex, endIndex in rowIslands[rowNumber]:
                rowIslandsToGroups[rowIslandNumber] = []
                if rowNumber == 0:
                    rowIslandsToGroups[rowIslandNumber].append(nextGroup)
                    nextGroup += 1
                    continue
                for prevRowIslandNumber, prevStartIndex, prevEndIndex in rowIslands[rowNumber - 1]:
                    if overlaps(prevStartIndex, prevEndIndex, startIndex, endIndex):
                        for groupNumber in rowIslandsToGroups[prevRowIslandNumber]:
                            rowIslandsToGroups[rowIslandNumber].append(groupNumber)
                if len(rowIslandsToGroups[rowIslandNumber]) == 0:
                    rowIslandsToGroups[rowIslandNumber].append(nextGroup)
                    nextGroup += 1

        groupBuckets = {}
        allBuckets = []
        for rowIslandNumber in range(1, nextRowIsland):
            relatedGroups = rowIslandsToGroups[rowIslandNumber]
            for group in relatedGroups:
                if (groupBuckets.get(group, None)) == None:
                    newGroupBucket = Bucket([group])
                    groupBuckets[group] = newGroupBucket
                    allBuckets.append(newGroupBucket)
            relatedBuckets = [groupBuckets[group] for group in relatedGroups]
            firstBucket = relatedBuckets[0]
            for group in relatedGroups:
                if not firstBucket.contains(group):
                    newCombinedBucket = firstBucket.combine(groupBuckets[group])
                    allBuckets.append(newCombinedBucket)

        return len([resultBucket for resultBucket in allBuckets if not resultBucket.hasDestination()])

solver = Solution()

# 1
# inputGrid = [
#     '11110',
#     '11010',
#     '11000',
#     '00000',
# ]

# 3
# inputGrid = [
#     '11000',
#     '11000',
#     '00100',
#     '00011',
# ]

# 1
# inputGrid = [
#     '11011',
#     '10001',
#     '10001',
#     '11111',
# ]

# 5
# inputGrid = [
#     '101',
#     '010',
#     '101',
# ]

# 1
inputGrid = [
    '111',
    '010',
    '010',
]

print(solver.numIslands(inputGrid))


from django.shortcuts import get_object_or_404, render, redirect, reverse
from django.http import HttpResponseRedirect
from .models import UserNotification

# Create your views here.

def notification_visited(request, pk):
    notification = get_object_or_404(UserNotification, pk=pk)
    question_id = notification.related_question.pk
    notification.delete()
    return HttpResponseRedirect(reverse('posts:question_page', args=(question_id,)))

def delete(request, pk):
    get_object_or_404(UserNotification, pk=pk).delete()
    return HttpResponseRedirect('')


"""
Library and Wrapper for SI7021 temperature/humidity sensor.
    Based on https://github.com/chrisbalmer/micropython-si7021, which implements a micropython driver.
    Extensions: Added a wrapper function for getting directly the values
"""

from time import sleep
import sensors
import logging

class CRCError(Exception):
    'Data failed a CRC check.'
    pass

class Si7021(object):
    'Driver for the Si7021 temperature sensor.'
    SI7021_DEFAULT_ADDRESS = 0x40
    SI7021_MEASTEMP_NOHOLD_CMD = bytearray([0xF3])
    SI7021_MEASRH_NOHOLD_CMD = bytearray([0xF5])
    SI7021_RESET_CMD = bytearray([0xFE])
    SI7021_ID1_CMD = bytearray([0xFA, 0x0F])
    SI7021_ID2_CMD = bytearray([0xFC, 0xC9])
    I2C_WAIT_TIME = 0.025

    def __init__(self, i2c, address=SI7021_DEFAULT_ADDRESS):
        'Initialize an Si7021 sensor object.'
        self.i2c = i2c
        self.address = address
        self.serial, self.identifier = self._get_device_info()

    @property
    def temperature(self):
        'Return the temperature in Celcius.'
        temperature = self._get_data(self.SI7021_MEASTEMP_NOHOLD_CMD)
        celcius = temperature * 175.72 / 65536 - 46.85
        return celcius

    @temperature.setter
    def temperature(self, value):
        raise AttributeError('can\'t set attribute')

    @property
    def relative_humidity(self):
        'Return the relative humidity as a percentage. i.e. 35.59927'
        relative_humidity = self._get_data(self.SI7021_MEASRH_NOHOLD_CMD)
        relative_humidity = relative_humidity * 125 / 65536 - 6
        return relative_humidity

    @relative_humidity.setter
    def relative_humidity(self, value):
        raise AttributeError('can\'t set attribute')

    def reset(self):
        'Reset the sensor.'
        self.i2c.writeto(self.address, self.SI7021_RESET_CMD)
        sleep(self.I2C_WAIT_TIME)

    def _get_data(self, command):
        'Retrieve data from the sensor and verify it with a CRC check.'
        data = bytearray(3)
        self.i2c.writeto(self.address, command)
        sleep(self.I2C_WAIT_TIME)

        self.i2c.readfrom_into(self.address, data)
        value = self._convert_to_integer(data[:2])

        verified = self._verify_checksum(data)
        if not verified:
            raise CRCError('Data read off i2c bus failed CRC check.',
                           data[:2],
                           data[-1])
        return value

    def _get_device_info(self):
        '''Get the serial number and the sensor identifier. The identifier is
        part of the bytes returned for the serial number.

        '''
        # Serial 1st half
        self.i2c.writeto(self.address, self.SI7021_ID1_CMD)
        id1 = bytearray(8)
        sleep(self.I2C_WAIT_TIME)
        self.i2c.readfrom_into(self.address, id1)

        # Serial 2nd half
        self.i2c.writeto(self.address, self.SI7021_ID2_CMD)
        id2 = bytearray(6)
        sleep(self.I2C_WAIT_TIME)
        self.i2c.readfrom_into(self.address, id2)

        combined_id = bytearray([id1[0], id1[2], id1[4], id1[6],
                                 id2[0], id2[1], id2[3], id2[4]])

        serial = self._convert_to_integer(combined_id)
        identifier = self._get_device_identifier(id2[0])

        return serial, identifier

    def _convert_to_integer(self, bytes_to_convert):
        'Use bitwise operators to convert the bytes into integers.'
        integer = None
        for chunk in bytes_to_convert:
            if not integer:
                integer = chunk
            else:
                integer = integer << 8
                integer = integer | chunk
        return integer

    def _get_device_identifier(self, identifier_byte):
        '''Convert the identifier byte to a device identifier. Values are based
        on the information from page 24 of the datasheet.

        '''
        if identifier_byte == 0x00 or identifier_byte == 0xFF:
            return 'engineering sample'
        elif identifier_byte == 0x0D:
            return 'Si7013'
        elif identifier_byte == 0x14:
            return 'Si7020'
        elif identifier_byte == 0x15:
            return 'Si7021'
        else:
            return 'unknown'

    def _verify_checksum(self, data):
        ''''Verify the checksum using the polynomial from page 19 of the
        datasheet.

        x8 + x5 + x4 + 1 = 0x131 = 0b100110001

        Valid Example:
        byte1: 0x67 [01100111]
        byte2: 0x8c [10001100]
        byte3: 0xfc [11111100] (CRC byte)

        '''
        crc = 0
        values = data[:2]
        checksum = int(data[-1])
        for value in values:
            crc = crc ^ value
            for _ in range(8, 0, -1):
                if crc & 0x80: #10000000
                    crc <<= 1
                    crc ^= 0x131 #100110001
                else:
                    crc <<= 1
        if crc != checksum:
            return False
        else:
            return True

def convert_celcius_to_fahrenheit(celcius):
    'Convert a Celcius measurement into a Fahrenheit measurement.'
    return celcius * 1.8 + 32

def get_reading(sda_pin, scl_pin, vcc_pin=None):
    """ Returns temperature/humidity/serial reading, for given I2C SCL/SDA and VCC pins """
    from machine import I2C

    sensors.set_sensor_power_on(vcc_pin)

    # initialization & measurement
    i2c = I2C(0, pins=(sda_pin, scl_pin))
    # check for i2c scan
    # method 1: run scan and wait for non-empty list: this seems to hang for more than 500 secs if power supply is not right
    i2c_scan = i2c.scan()

    temp = None
    hum = None

    # method 2 : read a dummy byte from device id 64 and if there is no device identified it will raise an exception. Seems much faster
    try:
        i2c.readfrom(0x40, 1) # here we assume our device has id 0x40 (64)
        # if we reach this point we can go on with reading values
        sensor = Si7021(i2c)
        # get values
        temp = sensor.temperature
        hum = sensor.relative_humidity
    except Exception as e:
        logging.exception(e, "Exception raised in I2C {}")

    # disable sensor and supply to sensor
    i2c.deinit()

    sensors.set_sensor_power_off(vcc_pin)

    return(temp, hum)


poppler_path=r"C:\Users\ahmed\Downloads\New folder\Release-22.04.0-0\poppler-22.04.0\Library\bin"

pdf_path = r"D:\certifiaction\freelancing.pdf"

from pdf2image import convert_from_path
pages=convert_from_path(pdf_path=pdf_path,poppler_path=poppler_path)

import os

saving_folder=r"D:\certifiaction"
c=1
for page in pages:
   img_name=f"img-{c}.png"
   page.save(os.path.join(saving_folder,img_name),"png")
   c+=1


import logging

from threading import Thread

class BasicComponent(Thread):
    """
    Basic class, more elaborated components will herit from this class.
    """
    def __init__(self, ID, dest_endpoint, mode, queue):
        #Assert validity of the parameters received
        endpoint_above, endpoint_below = dest_endpoint

        if not (mode == "server" or mode == "client"):
            raise Exception("Malformed input")

        if not isinstance(ID, int) or \
                not isinstance(endpoint_below, int) or \
                not isinstance(endpoint_above, int):
            raise Exception("Malformed input")

        self.queue = queue
        self.dest_above = endpoint_above
        self.dest_below = endpoint_below
        self.ID = ID
        self.mode = mode
        self.incomming_events = {}
        self.condition = queue.condition

        #Init the parent class
        Thread.__init__(self)

    def create_payload(self, content):
        payload = {}
        payload['event_ID'] = 1
        payload['stream_ID'] = 1
        payload['timestamp'] = 1.0
        payload['valid_for'] = 1
        payload['content'] = content

        return payload

    #Method to create an event
    def create_event(self, event_type, endpoint, payload = "payload"):
        if not (endpoint == self.dest_above or endpoint == self.dest_below):
            raise Exception("Malformed input")
        try:
            event = self.queue.create_event(self.ID, endpoint, event_type)
            event.set_payload(payload)
        except Exception as e:
            logging.error(str(e))

        return event

    #Method to send an event to the queue
    def send_event(self, event):
        self.condition.acquire()
        rsp = self.queue.add(event)
        self.condition.notify_all()
        self.condition.release()
        return rsp

    #Method to process an event retrieved from the queue,
    #Must me implemented in child class
    def process_events(self):
        pass

    #Method to get events, depends on what this component do (peek, get or ...)
    def get_events(self):
        empty = True
        prev = self.queue.preview()
        for key, value in prev.items():
            if value.dest_ID == self.ID:
                self.incomming_events[key] = self.queue.get(self.ID, key)
                empty = False
        return empty

    #Method used to create the thread for a component
    def run(self):
        while True:
            self.condition.acquire()
            #Loop until it get events related to its component ID
            while self.get_events():
                #Wait to receive a notification for a change in the queue
                self.condition.wait()
            #Events received, processing
            try:
                self.process_events()
            except Exception as e:
                logging.warning(str(e))
            #Release the underlying lock
            self.condition.release()

if __name__ == '__main__':
    pass


import cv2
import matplotlib.pyplot as plt
import numpy as np
import selectivesearch
from predictors import resnet152
from utils import selectors, nms
import xml.etree.ElementTree as ET
import random

boxes = {}

def getImgReady(img, show=False):
    if img is None:
         return None
    if show:
         plt.imshow(img)
         plt.axis('off')
    # convert into format (batch, RGB, width, height)
    img = cv2.resize(img, (224, 224))
    img = np.swapaxes(img, 0, 2)
    img = np.swapaxes(img, 1, 2)
    img = img[np.newaxis, :]
    return img

def addBox(x, y, w, h, prob, label):
    x1 = x
    x2 = x + w
    y1 = y
    y2 = y + h
    if not boxes.has_key(label):
        boxes[label] = [[x1, y1, x2, y2, prob]]
    else:
        boxes[label].append([x1, y1, x2, y2, prob])

def drawGroundTruth(imgName, img):
    xmlTree = ET.parse(annotationsDir + '{}.xml'.format(imgName.split('.')[0]))  # reads corresponding XML file
    for object in xmlTree.findall('object'):
        name = object.find('name').text
        name = nameDict[name]

        bndbox = object.find('bndbox')
        xmin = int(float(bndbox.find('xmin').text))     #reads coordinates
        ymin = int(float(bndbox.find('ymin').text))
        xmax = int(float(bndbox.find('xmax').text))
        ymax = int(float(bndbox.find('ymax').text))

        rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,
                             fill=False, edgecolor=(0, 1, 0), linewidth=3.5)
        plt.gca().add_patch(rect)
        plt.gca().text(xmin, ymin - 2, 'Ground Truth:{:s}'.format(name),
                       bbox=dict(facecolor=(0, 1, 0), alpha=0.5), fontsize=12, color='white')

def getNameDict(filename):
    dic = {}
    with open(filename, 'r') as f:
        lines = f.readlines()
        for line in lines:
            line = line.strip('\n')
            key = line.split(' ')[0]
            val = line.split(' ')[1]
            dic[key] = val
    return dic

annotationsDir = '../Data/ImageNet/ILSVRC2012/val-Annotations/'
imgsDir = '../Data/ImageNet/ILSVRC2012/img_val/'

nameDict = getNameDict('../synset.txt')

imgName = selectors.selectImg(imgsDir)
imgPath = imgsDir + imgName
img = cv2.cvtColor(cv2.imread(imgPath), cv2.COLOR_BGR2RGB)

plt.figure(imgName.split('.')[0])
plt.imshow(img)
plt.axis('off')

img_label, regions = selectivesearch.selective_search(img, scale = 500, sigma = 0.9, min_size = 500)
for i, region in enumerate(regions):    #rect:x y w h
    x = region['rect'][0]
    y = region['rect'][1]
    w = region['rect'][2]
    h = region['rect'][3]

    croppedImg = img[y:y + h,x:x + w]
    croppedImg = getImgReady(croppedImg)
    prob, label = resnet152.predict(croppedImg)

    if prob < 0.2:  #ignore low probability boxes
        continue

    addBox(x, y, w, h, prob, label)

for label in boxes:
    color = (random.random(), random.random(), random.random())
    indexes = nms.nms(np.array(boxes[label]), 0.3)
    for i in indexes:
        x1 = boxes[label][i][0]
        y1 = boxes[label][i][1]
        x2 = boxes[label][i][2]
        y2 = boxes[label][i][3]
        prob = boxes[label][i][4]

        rect = plt.Rectangle((x1, y1), x2 - x1, y2 - y1,
                             fill=False, edgecolor=color, linewidth=3.5)
        plt.gca().add_patch(rect)
        plt.gca().text(x1, y1 - 2, '{:s} {:.3f}'.format(label, prob),
                       bbox=dict(facecolor=color, alpha=0.5), fontsize=12, color='white')

drawGroundTruth(imgName, img)

plt.show()


import os
import glob
import PIL.Image as I

def load_cityscapes_sem_seg_dict(image_dir, gt_dir):
    """
    Args:
        image_dir (str): path to the raw dataset. e.g., "~/cityscapes/leftImg8bit/train".
        gt_dir (str): path to the raw annotations. e.g., "~/cityscapes/gtFine/train".
    Returns:
        list[dict]: a list of dict, each has "file_name" and
            "sem_seg_file_name".
    """
    ret = []
    for image_file in glob.glob(os.path.join(image_dir, "**/*.png")):
        suffix = "leftImg8bit.png"
        assert image_file.endswith(suffix)
        prefix = image_dir

        label_file = gt_dir + image_file[len(prefix): -len(suffix)] + "gtFine_labelTrainIds.png"
        assert os.path.isfile(
            label_file
        ), "Please generate labelTrainIds.png with cityscapesscripts/preparation/createTrainIdLabelImgs.py"

        w, h = I.open(label_file).size
        ret.append(
            {
                'file_name': image_file,
                'sem_seg_file_name': label_file,
                'height': h,
                'width': w
            }
        )
    return ret


from django.contrib.auth.backends import RemoteUserBackend
from django.core.exceptions import ImproperlyConfigured
from django.conf import settings
from django.contrib.auth import get_user_model

class GroupEnabledRemoteUserBackend(RemoteUserBackend):
    def authenticate(self, user, remote_groups):
        user.remote_groups = remote_groups
        user.is_superuser = self._is_superuser(user)
        user.is_staff = self._is_staff(user)
        return user

    def _is_superuser(self, user):
        superuser_groups = getattr(settings, 'CUMULUS_SUPERUSER_GROUPS', [])
        superusers = getattr(settings, 'CUMULUS_SUPERUSERS', [])
        result = user.username in superusers or \
            set(user.remote_groups).intersection(set(superuser_groups))
        return bool(result)

    def _is_staff(self, user):
        staff_groups = getattr(settings, 'CUMULUS_STAFF_GROUPS', [])
        staff_users = getattr(settings, 'CUMULUS_STAFF_USERS', [])
        result = user.username in staff_users or \
            set(user.remote_groups).intersection(set(staff_groups))
        return bool(result)


#%%
import nltk
import vaderSentiment
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import numpy as np
import json
import glob
import errno
import pandas
import random
import requests
from bs4 import BeautifulSoup
import news_web_scraping as NWS

#%%
# FUNCTIONS

# input: text (string)
# output: average sentiment of the text and a list containing a tuple of each sentence with its corresponding sentiment
# sentiment is rating between -4 (EXTREAMLY negative) and 4 (EXTREAMLY positive)
def calcSentiment(text):
    analyzer = SentimentIntensityAnalyzer()
    sentence_list = nltk.sent_tokenize(text)
    sentiment_list = []
    if not sentence_list:
        return sentence_list, sentiment_list
        #raise ValueError('no sentences in the text given -> 0/0')
    for sentence in sentence_list:
            vs = analyzer.polarity_scores(sentence)
            compound_sentiment = round(vs["compound"]*4, 4)
            sentiment_list = sentiment_list + [compound_sentiment]
    return sentence_list, sentiment_list

def get_json_text(json_file):
    json_string = json_file.read()
    json_dict = json.loads(json_string)
    return json_dict["text"]

def split_toParagraphs(article_text):
    paragraph_list = article_text.split("\n")
    paragraph_list = [paragraph.strip(' ') for paragraph in paragraph_list]
    paragraph_list = list(filter(None, paragraph_list))
    return paragraph_list

def paragraph_analysis(paragraph_list):
    all_para_sentiments = []
    all_para_sentences = []
    for paragraph in paragraph_list:
        para_sentences, para_sentiments = calcSentiment(paragraph)
        if para_sentiments:
            para_sentiment_avg = np.mean(np.asarray(para_sentiments))
            all_para_sentiments = all_para_sentiments + [para_sentiment_avg]
            all_para_sentences = all_para_sentences + [para_sentences]
    return all_para_sentences, all_para_sentiments

# preforms sentiment analysis on each article in a list of articles
def article_list_analysis(article_list):
    article_sentence_list = []
    sentence_sentiment_list = []
    avg_article_sentiment_list = []
    for article_text in article_list:
        if article_text:
            article_text = str(article_text)
            sentence_list, sentiment_list = calcSentiment(article_text)
            if sentiment_list:
                article_sentiment = np.mean(np.asarray(sentiment_list))
                article_sentence_list = article_sentence_list + [sentence_list]
                sentence_sentiment_list = sentence_sentiment_list + [sentiment_list]
                avg_article_sentiment_list = avg_article_sentiment_list + [article_sentiment]
    return article_sentence_list, sentence_sentiment_list, avg_article_sentiment_list

def politi_buzz_analysis(path):
    files = glob.glob(path)
    article_list = []
    for file_name in files:
        try:
            with open(file_name, 'r') as json_file:
                article_text = get_json_text(json_file)
                article_list = article_list + [article_text]
        except IOError as exc:
            if exc.errno != errno.EISDIR:
                raise
    return article_list_analysis(article_list)

def BBC_analysis(path):
    files = glob.glob(path)
    article_list = []
    for file_name in files:
        try:
            with open(file_name, 'r') as txt_file:
                article_text = txt_file.read()
                article_list = article_list + [article_text]
        except IOError as exc:
            if exc.errno != errno.EISDIR:
                raise
    return article_list_analysis(article_list)

def kaggle_Fact_Fake_analysis(path):
    df = pandas.read_csv(path)
    fake_df = df.loc[df['label'] == 1]
    real_df = df.loc[df['label'] == 0]
    article_list_fake = fake_df['text'].values.tolist()
    article_list_real = real_df['text'].values.tolist()
    article_list_fake = random.sample(article_list_fake, 100)  # needed as 13000 takes too long
    article_list_real = random.sample(article_list_real, 100)  # needed as 13000 takes too long
    fake_sentence_list, sentence_sentiment_fake_list, article_sentiment_fake_list = article_list_analysis(article_list_fake)
    real_sentence_list, sentence_sentiment_real_list, article_sentiment_real_list = article_list_analysis(article_list_real)
    return article_sentiment_fake_list, fake_sentence_list, sentence_sentiment_fake_list, article_sentiment_real_list, real_sentence_list, sentence_sentiment_real_list

def avg_var_calculation(sentiment_list, factOrFake, news_source):
    sentiment_array = np.asarray(sentiment_list)
    sentiment_avg = np.mean(sentiment_array)
    sentiment_var = np.var(sentiment_array)
    print(factOrFake + " article avg " + news_source + ": " + str(sentiment_avg))
    print(factOrFake + " article var " + news_source + ": " + str(sentiment_var))
    return sentiment_array, sentiment_avg, sentiment_var

def cal_article_abs_sentiment(list_list):
    mean_list = []
    for list in list_list:
        mean = np.mean(np.absolute(np.asarray(list)))
        mean_list = mean_list + [mean]
    return np.asarray(mean_list)

def kaggle_mult_news_analysis(path, publication):
    df = pandas.read_csv(path)
    df_publication = df.loc[df['publication'] == publication]
    article_list = df_publication['content'].values.tolist()
    article_list = random.sample(article_list, 100)  # needed as otherwise it takes too long
    sentence_list, sentence_sentiments, article_sentiments = article_list_analysis(article_list)
    return sentence_list, sentence_sentiments, article_sentiments

#def kaggel_Fake_analysis(path):
#    df = pandas.read_csv(path)
#    article_list = df['text'].values.tolist()
#    art_sub_list = random.sample(article_list, 100)  # needed as 13000 takes too long
#    sentence_list, sentence_sentiment_list, article_sentiment_tot_list = article_list_analysis(art_sub_list)
#    return article_sentiment_tot_list, sentence_list, sentence_sentiment_list

# scrapes NYT articles and preforms sentiment analysis on them
def NYT_scrape_SA(url):
    title_list, article_list = NWS.NYT_page_scrape(url)
    sentence_list, sentence_sentiments, article_sentiments = article_list_analysis(article_list)
    return title_list, sentence_list, sentence_sentiments, article_sentiments