import joblib
import pandas as pd
import numpy as np
from numpy import array
from pandas import DataFrame
from sklearn.preprocessing import LabelEncoder

def express_work_setup(data: DataFrame) -> DataFrame:
    """Generate all the minimal transformation to work with the Data

    Parameters
    ----------
    data : DataFrame:

    Returns
    -------
    DataFrame
    """
    df = data
    df = (df
          .dropna(subset=['colonia', 'm2_const', 'habitaciones', 'banos', 'autos', 'price', 'link_img', 'agent'])
          .pipe(lambda df_: df_[~((df_.tipo_inmueble == 'Casa') & (df_.m2_terreno.isna()))])
          .pipe(lambda df_: df_[(df_.m2_const > 20) & (df_.m2_const < 5000)])  # Remove values less than 10
          .pipe(lambda df_: df_[df_.price > 100])  # Remove listings below 100
          .pipe(lambda df_: df_[~((df_.tipo_oferta == 'Venta') & (df_.price < 100000))])
          .pipe(lambda df_: df_[df_.price < 100000000])
          .pipe(lambda df_: df_[~((df_.tipo_inmueble == 'Casa') & (df_.m2_terreno <= 50))])  # rm houses with less 50m2
          .pipe(lambda df_: df_[~((df_.tipo_inmueble == 'Casa') & (df_.m2_terreno >= 5000))])
          .pipe(lambda df_: df_[~((df_.habitaciones == 0) | (df_.banos == 0))])
          .pipe(lambda df_: df_[df_.banos < 10])
          .pipe(lambda df_: df_[~((df_.tipo_inmueble == 'Departamento') & (df_.habitaciones > 7))])
          .pipe(lambda df_: df_[df_.habitaciones < 10])
          .pipe(lambda df_: df_[df_.autos < 30])
          .pipe(lambda df_: df_[~((df_.autos == 0) & (df_.colonia.str.contains('Vallarta') == False))])
          # Renta
          .pipe(lambda df_: df_[~((df_.tipo_oferta == 'Renta') & (df_.price > 150000))])
          .assign(
        m2_price=lambda df_: df_.price / df_.m2_terreno,  # price / m2_terreno
        m2_const_price=lambda df_: df_.price / df_.m2_const,  # price / m2_const
        m2_abs_price=lambda df_: df_.price / (df_.m2_terreno + df_.m2_const),  # price / m2_terreno + m2_const
        fecha_pub=lambda df_: pd.to_datetime(df_.fecha_pub),
        descripcion=lambda df_: df_.descripcion.str.replace('"text":', '', regex=True))
          .pipe(lambda df_: df_[~(df_.m2_const_price > 100000)])
          .pipe(lambda df_: df_[~((df_.tipo_inmueble == 'Casa') & (df_.m2_price > 90000))])
          .pipe(create_sector_inmo)
          .astype(
        {'tipo_inmueble': 'category', 'sub_category': 'category', 'tipo_oferta': 'category', 'colonia': 'category',
         'estado': 'category', 'ciudad': 'category',
         'price': 'float32', 'm2_price': 'float32', 'm2_const_price': 'float32', 'm2_abs_price': 'float32',
         'm2_terreno': 'float32', 'm2_const': 'float32',
         'habitaciones': 'int16', 'banos': 'int16', 'autos': 'int16', 'antiguedad': 'float32'})
          .drop(columns=['divisa', 'total_habitaciones', 'link', 'link_img', 'agent'])
          ).reset_index().drop(columns='index')
    return df

def rm_outliers_by_colonia(data: DataFrame, columns: list = None) -> DataFrame:
    """ Remove the Outliers by Colonia in the DataFrame Selected.

    ## Use this list of colonies to Test: ['Puerta de Hierro', 'Valle Real', 'Americana', 'Providencia 1a Secc']

    Parameters
    ----------
    data : DataFrame:
        DataFrame that will be clean for its outliers.
    columns : list:
        List of the name of the features/columns that would remove the outliers.
         (Default value = None)

    Returns
    -------
    DataFrame
    """
    if columns is None:
        columns = ['price', 'm2_const', 'm2_terreno', 'habitaciones', 'banos', 'autos']

    df = data

    # Get Data Skew information Table
    skew_features_tb = identify_skew_features(df, columns)
    # Get Index Outliers
    skew_colonias_name = get_colonies_outliers_name(skew_features_tb)
    # Remove outliers by Colonia
    clean_data = rm_skew_outliers(df, skew_colonias_name, columns)

    return clean_data

def identify_skew_features(data: DataFrame, columns: list) -> DataFrame:
    """Create a table with the Skew Resume to identify the Observation that have one or more Outliers.

    Parameters
    ----------
    data :  DataFrame:

    columns : list:
        List of the name of the features/columns that would remove the outliers.

    groupby :
         (Default value = 'colonia')

    Returns
    -------

    """
    return (data
            .groupby('colonia', observed=True)[columns]
            .skew())

def get_colonies_outliers_name(skew_features_tb: DataFrame) -> array:
    """Create an Array of the Name of the Colonies

    Parameters
    ----------
    skew_features_tb : DataFrame:
        Array of the features name it would be removed the outliers.

    Returns
    -------
    array
    """
    positive_skew_features = skew_features_tb > 1
    negative_skew_features = skew_features_tb < -1
    # Select Positive or Negative Skew data
    skew_features = (
        skew_features_tb[(positive_skew_features) | (negative_skew_features)]
            .notna()  # Identify Only the Skew Observation
        # .any(axis=1)
    ).pipe(lambda df_: df_[df_.any(axis=1)]).index  # Select if any of the row have Outliers

    return skew_features

def rm_skew_outliers(data: DataFrame, skew_colonias_name: array, columns: list) -> DataFrame:
    """Remove the Outliers

    Parameters
    ----------
    data : DataFrame:
        DataFrame that will be clean for its outliers.

    skew_colonias_name : list:
        Array with the name of the Colonies with outliers.
    columns : list:
        List of the name of the features/columns that would remove the outliers.

    Returns
    -------
    DataFrame
    """
    df = data
    # iterate colonia names list
    for colonia in skew_colonias_name:
        # select the colonia subset DataFrame
        colonia_subset = df[df.colonia == colonia]
        # Iterate over the Colums with outliers
        for col in columns:
            # Second outliers Check
            # Positive +
            if colonia_subset[col].skew() > 1:
                # select low and hig quantiles value of the feautre
                low = colonia_subset[col].quantile(0.01)
                hig = colonia_subset[col].quantile(0.95)
                # Select only the outliers
                listings_outliers = colonia_subset.loc[(colonia_subset[col] <= low) | (colonia_subset[col] >= hig)]
                # Subset only not outliers
                df = df[~df.index.isin(listings_outliers.index)]

            # Negative -
            if colonia_subset[col].skew() < -1:
                # select low and hig quantiles value of the feautre
                low = colonia_subset[col].quantile(0.05)
                hig = colonia_subset[col].quantile(0.99)
                # Select only the outliers
                listings_outliers = colonia_subset.loc[(colonia_subset[col] <= low) | (colonia_subset[col] >= hig)]
                # Subset only not outliers
                df = df[~df.index.isin(listings_outliers.index)]

    return df.drop(columns=['sku'])

def set_dummies_features(data):
    df = data

    # Creating Dummi Variables
    offer_dummies = pd.get_dummies(df.tipo_oferta, prefix='dummie', drop_first=True)
    type_dummies = (pd.get_dummies(df.tipo_inmueble, prefix='dummie')
                    .drop(columns='dummie_Departamento'))
    # Concatenate the Tables
    df_result = (pd.concat([df, offer_dummies, type_dummies], axis=1)
                 .rename(columns={'dummie_Venta': 'venta', 'dummie_Casa': 'casa'}))

    return df_result

def colonies_encoder(data, save_file: str = None):
    df = data
    # Create Encoder
    encoder = LabelEncoder().fit(df['colonia'])
    df_result = df.assign(endocer_colonia=encoder.transform(df['colonia']))

    if save_file is not None:
        joblib.dump(encoder, save_file)

    return df_result

def create_sector_inmo(data: DataFrame) -> DataFrame:
    """Create a new Column call it 'sector_inmo' that contains the categories of the listings
            economical sectors.

    The categories are: 'Interés Social', 'Interés Medio', 'Residencial', 'Residencial Plus', 'Premium'.
    Depends on the type of offer (sell or rent) and the price that the label would be selected.

    Parameters
    ----------
    data : DataFrame:

    Returns
    -------

    """
    df = data
    # Create Sector Inmobiliario Column
    categories = ['Interés Social', 'Interés Medio', 'Residencial', 'Residencial Plus', 'Premium']
    df['sector_inmo'] = pd.Series(categories).astype('category')
    # Select Offer Types
    venta = (df.tipo_oferta == 'Venta')
    renta = (df.tipo_oferta == 'Renta')
    # Use a dict to iterate over the strings and subsets.
    offers = {'venta': venta, 'renta': renta}

    for offer_txt, offer_df in offers.items():

        if offer_txt == 'venta':
            interes_social = df.price.lt(1000001)
            interes_medio = df.price.between(1000001, 3000000)
            residencial = df.price.between(3000001, 7000000)
            residencial_plus = df.price.between(7000001, 15000000)
            premium = df.price.gt(15000000)
            sectors = [interes_social, interes_medio, residencial, residencial_plus, premium]

        else:  # renta
            interes_social = df.price.lt(5001)
            interes_medio = df.price.between(5001, 10000)
            residencial = df.price.between(10001, 15000)
            residencial_plus = df.price.between(15001, 30000)
            premium = df.price.gt(30000)
            sectors = [interes_social, interes_medio, residencial, residencial_plus, premium]

        for sector, category in zip(sectors, categories):
            df.loc[sector & offer_df, 'sector_inmo'] = category

    return df

def set_amenidades(data: DataFrame, column: str = 'amenidades') -> DataFrame:
    """Create N number of new Columns of the total amenities in the column 'amenidades' lists.

    This column contains list of amenities, and it is variable. The program don't know how many new columns
    will be created. So this proces is O(n2).

    Parameters
    ----------
    data : DataFrame:

    column : str : (Default value = 'amenidades')
        The column Name that contain the Raw List of amenities.

    Returns
    -------

    """
    df = data.reset_index()

    list_amenidades = list()
    # Unpacking the row amenities
    for i, row_list in enumerate(df[column]):
        if row_list is not None:  # Validate is not empty
            # Create/Add columns amenity value.
            for amenidad in row_list:
                df.loc[i, amenidad] = True
                # Add empty list
                if amenidad not in list_amenidades:
                    list_amenidades.append(amenidad)
    # Add Nan to the empty values to prevente errors with SQL DataBase
    for amenidad in list_amenidades:
        df[amenidad] = df[amenidad].replace(np.nan, False)

    return df


import collections
from typing import List

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def findLeaves(self, root: TreeNode) -> List[List[int]]:
        """
        参数定义
        l/r：左右子树高度
        depth：当前节点离叶子节点高度
        res：哈希表，记录每个深度对应的节点值集合
        思路
        根据题意，一层层将叶子节点删除，叶子节点被删除后其父节点会变为叶子节点，所以我们需要自底向上递归来记录每个节点离叶子节点的高度，将不同深度的节点放入对应的集合即可。
        心得：善于发现题目规律，学会融会贯通，才能触类旁通

        作者：yim-6
        链接：https://leetcode-cn.com/problems/find-leaves-of-binary-tree/solution/python3-zi-di-xiang-shang-di-gui-qiu-she-ma41/
        来源：力扣（LeetCode）
        著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。
        :param root:
        :return:
        """
        # 自底向上递归
        def dfs(root):
            if not root:return 0
            l,r=dfs(root.left),dfs(root.right)
            depth=max(l,r)+1
            res[depth].append(root.val)
            return depth

        res=collections.defaultdict(list)
        dfs(root)
        return [v for v in res.values()]

def listCreatTree(root, Node_list, i):
    """
    Nodelist = [1, 2, 3, 4, 5]
    Nodelist_strItem = [str(i) for i in Nodelist]
    levelOrder(listCreatTree(None, Nodelist_strItem, 0))
    :param root:
    :param Node_list:
    :param i:
    :return:
    """
    if i < len(Node_list):
        if Node_list[i] == '#':
            return None  # 这里的return很重要
        else:
            root = TreeNode(Node_list[i])
            # 往左递推
            root.left = listCreatTree(root.left, Node_list, 2 * i + 1)  # 从根开始一直到最左，直至为空，
            # 往右回溯
            root.right = listCreatTree(root.right, Node_list, 2 * i + 2)  # 再返回上一个根，回溯右，
            # 再返回根'
            return root  # 这里的return很重要
    return root
if __name__=='__main__':
    print(Solution().findLeaves(listCreatTree(None,[1, 2, 3, 4, 5],0)))


from pathlib import Path
from pprint import PrettyPrinter

import numpy as np

FILENAME = "rosalind_sseq.txt"

pp = PrettyPrinter()

def get_values(data):
    values = dict()
    values["dna_string"] = "".join(data[1].split("\n")[1::])
    values["t"] = "".join(data[2].split("\n")[1::])
    return values

def longest_common_subsequence(a, b, table):
    for row in range(1, len(table)):
        for col in range(1, len(table[row])):
            if b[row - 1] == a[col - 1]:
                table[row][col] = table[row - 1][col - 1] + 1
            else:
                table[row][col] = max(table[row-1][col], table[row][col-1])

    row = len(table) - 1
    col = len(table[0]) - 1
    end = table[row][col]
    indices = list()
    while end != 1 and row > 0 and col > 0:
        left = table[row][col - 1]
        up = table[row - 1][col]
        diag = table[row - 1][col - 1]
        index = np.argmax([left, up, diag])
        if left == diag and diag == up:
            indices.append(col)
            row = row - 1
            col = col - 1
            end = table[row][col]
        else:
            col = col - 1
    indices.append(col)
    indices = indices[::-1]

    result = [str(i) for i in indices]

    return " ".join(result)

def main(filename):
    data_path = Path().absolute().parent.parent / "data"
    full_path = data_path / filename

    values = None
    with open(full_path, 'r') as f:
        data = f.read().rstrip()
        data = data.split(">")
        values = get_values(data)
        f.close()
    # pp.pprint(values)
    a = values["dna_string"]
    b = values["t"]
    table = np.zeros((len(b) + 1, len(a) + 1), dtype=int)
    ans = longest_common_subsequence(a, b, table)
    print(ans)

if __name__ == '__main__':
    main(filename=FILENAME)


from csv import DictReader

from django.core.management.base import BaseCommand
import csv
from csvReader.models import CsvData

class Command(BaseCommand):
    help = "Load data from csvDjango.csv into our CsvData model"
    def handle(self, *args, **options):

        with open(('./csvDjango.csv')) as f:
            reader = csv.reader(f)
            next(reader)
            for row in reader:
                _, created = CsvData.objects.get_or_create(
                    processName = row[0],
                    processId = row[1],
                    )


import itertools

from ConfigSpace.hyperparameters import Constant, NumericalHyperparameter, \
    CategoricalHyperparameter, OrdinalHyperparameter
import numpy as np

from xbbo.configspace.space import DenseConfiguration, deactivate_inactive_hyperparameters
from xbbo.initial_design.base import InitialDesign

class FactorialInitialDesign(InitialDesign):
    """Factorial initial design

    Attributes
    ----------
    configs : typing.List[Configuration]
        List of configurations to be evaluated
        Don't pass configs to the constructor;
        otherwise factorial design is overwritten
    """
    def _select_configurations(self, num=None) -> DenseConfiguration:
        """Selects a single configuration to run

        Returns
        -------
        config: Configuration
            initial incumbent configuration
        """
        params = self.cs.get_hyperparameters()

        values = []
        mid = []
        for param in params:
            if isinstance(param, Constant):
                v = [param.value]
                mid.append(param.value)
            elif isinstance(param, NumericalHyperparameter):
                v = [param.lower, param.upper]
                mid.append(np.average([param.lower, param.upper]))
            elif isinstance(param, CategoricalHyperparameter):
                v = list(param.choices)
                mid.append(param.choices[0])
            elif isinstance(param, OrdinalHyperparameter):
                v = [param.sequence[0], param.sequence[-1]]
                length = len(param.sequence)
                mid.append(param.sequence[int(length / 2)])
            values.append(v)

        factorial_design = itertools.product(*values)

        self.logger.debug("Initial Design")
        configs = [self.cs.get_default_configuration()]
        # add middle point in space
        conf_dict = dict([(p.name, v) for p, v in zip(params, mid)])
        middle_conf = deactivate_inactive_hyperparameters(conf_dict, self.cs)
        configs.append(middle_conf)

        # add corner points
        for design in factorial_design:
            conf_dict = dict([(p.name, v) for p, v in zip(params, design)])
            conf = deactivate_inactive_hyperparameters(conf_dict, self.cs)
            conf.origin = "Factorial Design"
            configs.append(conf)
            self.logger.debug(conf)
        size = len(configs)
        self.logger.debug("Size of factorial design: %d" % (size))
        self.init_budget = size
        return configs


import tkinter as tk
from tkinter import *
from tkinter import ttk
import tkinter.font as tkFont
from subprocess import call
from PIL import Image, ImageTk
import fileinput
import home
from q2 import q
import playlist
class discover:
    count=0
    def __init__(self, root):
        #setting title
        #root.title("Dashboard")
        #setting window size
        discover.ln = ImageTk.PhotoImage(file="pics/l.png")
        discover.read1 = tk.Button(d.second_frame,image=discover.ln)
        discover.read2 = tk.Button(d.second_frame,image=discover.ln)
        discover.read3 = tk.Button(d.second_frame,image=discover.ln)
        discover.read4 = tk.Button(d.second_frame,image=discover.ln)
        discover.read5 = tk.Button(d.second_frame,image=discover.ln)
        discover.read6 = tk.Button(d.second_frame,image=discover.ln)
        discover.read7 = tk.Button(d.second_frame,image=discover.ln)
        discover.read8 = tk.Button(d.second_frame,image=discover.ln)
        discover.read9 = tk.Button(d.second_frame,image=discover.ln)
        discover.read10 = tk.Button(d.second_frame,image=discover.ln)
        discover.read11 = tk.Button(d.second_frame,image=discover.ln)
        discover.read12 = tk.Button(d.second_frame,image=discover.ln)
        discover.read13 = tk.Button(d.second_frame,image=discover.ln)
        discover.read14 = tk.Button(d.second_frame,image=discover.ln)
        discover.read15 = tk.Button(d.second_frame,image=discover.ln)
        discover.read16 = tk.Button(d.second_frame,image=discover.ln)
        discover.read17 = tk.Button(d.second_frame,image=discover.ln)
        discover.read18 = tk.Button(d.second_frame,image=discover.ln)

        discover.bg = ImageTk.PhotoImage(file="N_Pics/lib2.jpg")
        l1= Label(root,image=discover.bg)
        l1.place(x=0,y=0,width=1600,height=1600)
        width=1600
        height=900
        screenwidth = root.winfo_screenwidth()
        screenheight = root.winfo_screenheight()
        alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        #root.geometry(alignstr)
        #root.resizable(width=True, height=True)
        discover.co = ImageTk.PhotoImage(file="pics/c.jfif")
        ft = tkFont.Font(family='TimesNewRoman',size=10)
        coin = tk.Label(root,image=discover.co)
        coin.place(x=780,y=90,width=80,height=30)
        num = tk.Label(root,text=d.coins,font=ft)
        num.place(x=870,y=90,width=30,height=30)
        num.configure(background="white")

        home.im2 = ImageTk.PhotoImage(file="N_Pics/button copy.png")
        homeButton = tk.Button(root, image=home.im2)
        homeButton["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        homeButton["font"] = ft
        homeButton["fg"] = "#000000"
        homeButton["justify"] = "center"
        homeButton["text"] = "Home"
        homeButton.place(x=100,y=75,width=112,height=40)
        homeButton["command"]=self.homeButton_command

        home.im3 = ImageTk.PhotoImage(file="N_Pics/button-2 copy.png")
        playlistButton = tk.Button(root,image= home.im3)
        playlistButton["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        playlistButton["font"] = ft
        playlistButton["fg"] = "#000000"
        playlistButton["justify"] = "center"
        playlistButton["text"] = "My Playlist"
        playlistButton.place(x=374+100,y=75,width=165,height=40)
        playlistButton["command"] = self.playlistButton_command

        home.im4 = ImageTk.PhotoImage(file="N_Pics/button-3 copy.png")
        disButton = tk.Button(root,image= home.im4)
        disButton["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        disButton["font"] = ft
        disButton["fg"] = "#000000"
        disButton["justify"] = "center"
        disButton["text"] = "Discover"
        disButton.place(x=374*2+300,y=75,width=160,height=40)

        moreButton=tk.Button(root)
        moreButton["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        moreButton["font"] = ft
        moreButton["fg"] = "#000000"
        moreButton["justify"] = "center"
        moreButton["text"] = "More"
        moreButton.place(x=374*3+280,y=20,width=70,height=30)
        moreButton["command"] = self.moreButton_command

        discover.b1 = ImageTk.PhotoImage(file="N_Pics/The Egg.jpg")
        book1ImageButton=tk.Button(root,image=discover.b1)
        book1ImageButton.place(x=10,y=330,width=250,height=310)
        book1ImageButton["command"] = self.book1ImageButton_command
        a=10
        b=330
        c=250

        discover.meat = ImageTk.PhotoImage(file="pics/m2.JPG")
        book2ImageButton=tk.Button(root,image=discover.meat)
        book2ImageButton.place(x=a+c+60,y=b,width=185,height=270)
        book2ImageButton["command"] = self.book2ImageButton_command
        a=a+c+60
        c=185

        book3ImageButton=tk.Button(root)
        book3ImageButton["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        book3ImageButton["font"] = ft
        book3ImageButton["fg"] = "#000000"
        book3ImageButton["justify"] = "center"
        book3ImageButton["text"] = "a story without title"
        discover.b3 = ImageTk.PhotoImage(file="N_Pics/A story without title.png")
        book3ImageButton = tk.Button(root, image=discover.b3)
        book3ImageButton.place(x=a+c+60,y=b,width=200,height=270)
        book3ImageButton["command"] = self.book3ImageButton_command
        a=a+c+60
        c=200

        book4ImageButton=tk.Button(root)
        book4ImageButton["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        book4ImageButton["font"] = ft
        book4ImageButton["fg"] = "#000000"
        book4ImageButton["justify"] = "center"
        book4ImageButton["text"] = "A Tent In Agony"
        discover.b4 = ImageTk.PhotoImage(file="N_Pics/A Tent In Agony.jpg")
        book4ImageButton = tk.Button(root, image=discover.b4)
        book4ImageButton.place(x=a+c+60,y=b,width=200,height=270)
        book4ImageButton["command"] = self.book4ImageButton_command
        a=a+c+50
        c=200

        book5ImageButton=tk.Button(root)
        book5ImageButton["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        book5ImageButton["font"] = ft
        book5ImageButton["fg"] = "#000000"
        book5ImageButton["justify"] = "center"
        book5ImageButton["text"] = "A Terrible Thing Has Happened"
        discover.b5 = ImageTk.PhotoImage(file="N_Pics/A Terrible Thing Has Happened.jpg")
        book5ImageButton = tk.Button(root, image=discover.b5)
        book5ImageButton.place(x=a+c+60,y=b,width=200,height=270)
        book5ImageButton["command"] = self.book5ImageButton_command
        a=a+c+50
        c=200

        book6ImageButton=tk.Button(root)
        book6ImageButton["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        book6ImageButton["font"] = ft
        book6ImageButton["fg"] = "#000000"
        book6ImageButton["justify"] = "center"
        book6ImageButton["text"] = "Case Closed"
        discover.b6 = ImageTk.PhotoImage(file="N_Pics/Case Closed.jpg")
        book6ImageButton = tk.Button(root, image=discover.b6)
        book6ImageButton.place(x=a+c+60,y=b,width=200,height=270)
        book6ImageButton["command"] = self.book6ImageButton_command
        a=a+c+50
        c=200
        b=800
        book7ImageButton=tk.Button(root,text="destiny")
        discover.b7 = ImageTk.PhotoImage(file="N_Pics/destiny.jpg")
        book7ImageButton = tk.Button(root, image=discover.b7)
        book7ImageButton.place(x=10,y=b,width=250,height=310)
        book7ImageButton["command"] = self.book7ImageButton_command
        a=10
        b=800
        c=250

        book8ImageButton=tk.Button(root,text="Fall Of the Stars")
        discover.b8 = ImageTk.PhotoImage(file="N_Pics/Fall Of the Stars.jpg")
        book8ImageButton = tk.Button(root, image=discover.b8)
        book8ImageButton.place(x=a+c+60,y=b,width=185,height=270)
        book8ImageButton["command"] = self.book8ImageButton_command
        a=a+c+60
        c=185

        book9ImageButton=tk.Button(root)
        book9ImageButton["bg"] = "#efefef"
        ft =tkFont.Font(family='Times',size=10)
        book9ImageButton["font"] = ft
        book9ImageButton["fg"] = "#000000"
        book9ImageButton["justify"] = "center"
        book9ImageButton["text"] = "Infinity "
        discover.b9 = ImageTk.PhotoImage(file="N_Pics/Infinity.jpg")
        book9ImageButton = tk.Button(root, image=discover.b9)
        book9ImageButton.place(x=a+c+60,y=b,width=200,height=270)
        book9ImageButton["command"] = self.book9ImageButton_command
        a=a+c+60
        c=200

        book10ImageButton=tk.Button(root)
        book10ImageButton["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        book10ImageButton["font"] = ft
        book10ImageButton["fg"] = "#000000"
        book10ImageButton["justify"] = "center"
        book10ImageButton["text"] = "Margaret's Perfume"
        discover.b10 = ImageTk.PhotoImage(file="N_Pics/Margaret's Perfume2.jpg")
        book10ImageButton = tk.Button(root, image=discover.b10)
        book10ImageButton.place(x=a+c+60,y=b,width=200,height=270)
        book10ImageButton["command"] = self.book10ImageButton_command
        a=a+c+50
        c=200

        book11ImageButton=tk.Button(root)
        book11ImageButton["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        book11ImageButton["font"] = ft
        book11ImageButton["fg"] = "#000000"
        book11ImageButton["justify"] = "center"
        book11ImageButton["text"] = "Mind Games"
        discover.b11 = ImageTk.PhotoImage(file="N_Pics/Mind Games.jpg")
        book11ImageButton = tk.Button(root, image=discover.b11)
        book11ImageButton.place(x=a+c+60,y=b,width=200,height=270)
        book11ImageButton["command"] = self.book11ImageButton_command
        a=a+c+50
        c=200

        book12ImageButton=tk.Button(root)
        book12ImageButton["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        book12ImageButton["font"] = ft
        book12ImageButton["fg"] = "#000000"
        book12ImageButton["justify"] = "center"
        book12ImageButton["text"] = "Scared to Death to Live"
        discover.b12 = ImageTk.PhotoImage(file="N_Pics/Scared to Death to Live.jpg")
        book12ImageButton = tk.Button(root, image=discover.b12)
        book12ImageButton.place(x=a+c+60,y=b,width=200,height=270)
        book12ImageButton["command"] = self.book12ImageButton_command
        a=a+c+50
        c=200

        book13ImageButton=tk.Button(root,text="the aged mother")
        discover.b13 = ImageTk.PhotoImage(file="N_Pics/the aged mother.jpg")
        book13ImageButton = tk.Button(root, image=discover.b13)
        book13ImageButton.place(x=10,y=1200,width=250,height=310)
        book13ImageButton["command"] = self.book13ImageButton_command
        a=10
        b=1200
        c=250

        book14ImageButton=tk.Button(root,text="The Busy Over Gate")
        discover.b14 = ImageTk.PhotoImage(file="N_Pics/The Busy Over Gate.jpg")
        book14ImageButton = tk.Button(root, image=discover.b14)
        book14ImageButton.place(x=a+c+60,y=b,width=185,height=270)
        book14ImageButton["command"] = self.book14ImageButton_command
        a=a+c+60
        c=185

        book15ImageButton=tk.Button(root)
        book15ImageButton["bg"] = "#efefef"
        ft =tkFont.Font(family='Times',size=10)
        book15ImageButton["font"] = ft
        book15ImageButton["fg"] = "#000000"
        book15ImageButton["justify"] = "center"
        book15ImageButton["text"] = "the golden windows"
        discover.b15 = ImageTk.PhotoImage(file="N_Pics/the golden windows.jpg")
        book15ImageButton = tk.Button(root, image=discover.b15)
        book15ImageButton.place(x=a+c+60,y=b,width=200,height=270)
        book15ImageButton["command"] = self.book15ImageButton_command
        a=a+c+60
        c=200

        book16ImageButton=tk.Button(root)
        book16ImageButton["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        book16ImageButton["font"] = ft
        book16ImageButton["fg"] = "#000000"
        book16ImageButton["justify"] = "center"
        book16ImageButton["text"] = "the last dream of olad oak"
        discover.b16 = ImageTk.PhotoImage(file="N_Pics/the last dream of olad oak.jpg")
        book16ImageButton = tk.Button(root, image=discover.b16)
        book16ImageButton.place(x=a+c+60,y=b,width=200,height=270)
        book16ImageButton["command"] = self.book16ImageButton_command
        a=a+c+50
        c=200

        if d.coins>200:

            book17ImageButton=tk.Button(root)
            book17ImageButton["bg"] = "#efefef"
            ft = tkFont.Font(family='Times',size=10)
            book17ImageButton["font"] = ft
            book17ImageButton["fg"] = "#000000"
            book17ImageButton["justify"] = "center"
            book17ImageButton["text"] = "The Last Gift"
            discover.b17 = ImageTk.PhotoImage(file="N_Pics/The Last Gift.jpg")
            book17ImageButton = tk.Button(root, image=discover.b17)
            book17ImageButton.place(x=a+c+60,y=b,width=200,height=270)
            book17ImageButton["command"] = self.book17ImageButton_command
        else:
            book17ImageButton=tk.Button(root)
            book17ImageButton["bg"] = "#efefef"
            ft = tkFont.Font(family='Times',size=10)
            book17ImageButton["font"] = ft
            book17ImageButton["fg"] = "#000000"
            book17ImageButton["justify"] = "center"
            book17ImageButton["text"] = "Unlocked at 200+ Coins"
            book17ImageButton.place(x=a+c+60,y=b,width=200,height=270)

        a=a+c+50
        c=200
        if d.coins>300:

            book18ImageButton=tk.Button(root)
            book18ImageButton["bg"] = "#efefef"
            ft = tkFont.Font(family='Times',size=10)
            book18ImageButton["font"] = ft
            book18ImageButton["fg"] = "#000000"
            book18ImageButton["justify"] = "center"
            book18ImageButton["text"] = "The Luck of Roaring Camp"
            discover.b18 = ImageTk.PhotoImage(file="N_Pics/The Luck of Roaring Camp.jpg")
            book18ImageButton = tk.Button(root, image=discover.b18)
            book18ImageButton.place(x=a+c+60,y=b,width=200,height=270)
            book18ImageButton["command"] = self.book18ImageButton_command
        else:
            book18ImageButton=tk.Button(root)
            book18ImageButton["bg"] = "#efefef"
            ft = tkFont.Font(family='Times',size=10)
            book18ImageButton["font"] = ft
            book18ImageButton["fg"] = "#000000"
            book18ImageButton["justify"] = "center"
            book18ImageButton["text"] = "Unlocked at 300+ Coins"
            #discover.b18 = ImageTk.PhotoImage(file="N_Pics/The Luck of Roaring Camp.jpg")
            #book18ImageButton = tk.Button(root, image=discover.b18)
            book18ImageButton.place(x=a+c+60,y=b,width=200,height=270)
        a=a+c+50
        c=200

        close=tk.Button(root)
        close["bg"] = "#efefef"
        ft = tkFont.Font(family='Times',size=10)
        close["font"] = ft
        close["fg"] = "#000000"
        close["justify"] = "center"
        close["text"] = " X "
        close.place(x=1490,y=0,width=30,height=30)
        close.configure(background="red")
        close["command"] = self.close_command

    def homeButton_command(self):
        d.root.destroy()
        h=home.r(d.username)
        #exit()

    def playlistButton_command(self):
        d.root.destroy()
        pl=playlist.p(d.username)
        #exit()
    def close_command(self):
        exit()

    def playe(self,name):
        pl = q(name)

    def check(self,bname):
        s="userdocs/"+d.username+"books.txt"
        try:
            file = open(s,"r")
            f=0
            for i in file:
                if i.strip()==bname:
                    f=1
                    break
            if f!=1:
                file=open(s,"a")
                file.write(bname+"\n")
                file.close()

        except:
            file = open(s,"x")
            file=open(s,"a")
            file.write(bname+"\n")
            file.close()

    def inc(self):
        d.coins+=20
        ft = tkFont.Font(family='TimesNewRoman',size=10)

        num = tk.Label(d.second_frame,text=d.coins,font=ft)
        num.place(x=870,y=90,width=30,height=30)
        num.configure(background="white")
        filename = "login_credentials.txt"

        with fileinput.FileInput(filename,inplace = True,backup=".bak") as f:
            flag=0
            for i,line in enumerate(f):
                if flag==11:
                    print(line,end='')
                    continue
                if flag==0:

                    print(line, end ='')
                    if line.strip()==d.username:
                        flag=1
                        continue

                if flag==1:
                    flag+=1
                    print(line, end ='')
                    continue
                if flag==2:
                    print(d.coins, end ='\n')
                    flag=11
                    continue

    def book1ImageButton_command(self):
        name="The Egg"
        discover.read1 = tk.Button(d.second_frame,image=discover.ln)

        discover.read1["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        discover.read1.place(x=75,y=640,width=100,height=25)
        self.forget(1)

    def book2ImageButton_command(self):
        name="They're made out of meat"

        discover.read2 = tk.Button(d.second_frame,image=discover.ln)
        discover.read2.place(x=360,y=600,width=100,height=25)
        discover.read2["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(2)

    def book3ImageButton_command(self):
        name="a story without title"
        discover.read3 = tk.Button(d.second_frame,image=discover.ln)
        discover.read3.place(x=610,y=600,width=100,height=25)
        discover.read3["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(3)

    def book4ImageButton_command(self):
        name="A Tent In Agony"
        discover.read4 = tk.Button(d.second_frame,image=discover.ln)
        discover.read4.place(x=870,y=600,width=100,height=25)
        discover.read4["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(4)

    def book5ImageButton_command(self):
        name="A Terrible Thing Has Happened"
        discover.read5 = tk.Button(d.second_frame,image=discover.ln)
        discover.read5.place(x=1125,y=600,width=100,height=25)
        discover.read5["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(5)

    def book6ImageButton_command(self):
        name="Case Closed"
        discover.read6 = tk.Button(d.second_frame,image=discover.ln)
        discover.read6.place(x=1375,y=600,width=100,height=25)
        discover.read6["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(6)

    def book7ImageButton_command(self):
        name="destiny"
        discover.read7 = tk.Button(d.second_frame,image=discover.ln)
        discover.read7.place(x=75,y=1110,width=100,height=25)
        discover.read7["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(7)

    def book8ImageButton_command(self):
        name="Fall Of the Stars"

        discover.read8 = tk.Button(d.second_frame,image=discover.ln)
        discover.read8.place(x=360,y=1070,width=100,height=25)
        discover.read8["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(8)

    def book9ImageButton_command(self):
        name="Infinity"

        discover.read9 = tk.Button(d.second_frame,image=discover.ln)
        discover.read9.place(x=610,y=1070,width=100,height=25)
        discover.read9["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(9)

    def book10ImageButton_command(self):
        name="Margaret's Perfume"

        discover.read10 = tk.Button(d.second_frame,image=discover.ln)
        discover.read10.place(x=880,y=1070,width=100,height=25)
        discover.read10["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(10)

    def book11ImageButton_command(self):
        name="Mind Games"

        discover.read11 = tk.Button(d.second_frame,image=discover.ln)
        discover.read11.place(x=1135,y=1070,width=100,height=25)
        discover.read11["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(11)

    def book12ImageButton_command(self):
        name="Scared to Death to Live"

        discover.read12 = tk.Button(d.second_frame,image=discover.ln)
        discover.read12.place(x=1375,y=1070,width=100,height=25)
        discover.read12["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(12)

    def book13ImageButton_command(self):
        name="the aged mother"

        discover.read13 = tk.Button(d.second_frame,image=discover.ln)
        discover.read13.place(x=75,y=1510,width=100,height=25)
        discover.read13["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(13)

    def book14ImageButton_command(self):
        name="The Busy Over Gate"

        discover.read14 = tk.Button(d.second_frame,image=discover.ln)
        discover.read14.place(x=360,y=1470,width=100,height=25)
        discover.read14["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(14)

    def book15ImageButton_command(self):
        name="the golden windows"

        discover.read15 = tk.Button(d.second_frame,image=discover.ln)
        discover.read15.place(x=610,y=1470,width=100,height=25)
        discover.read15["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(15)

    def book16ImageButton_command(self):
        name="the last dream of olad oak"

        discover.read16 = tk.Button(d.second_frame,image=discover.ln)
        discover.read16.place(x=880,y=1470,width=100,height=25)
        discover.read16["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(16)

    def book17ImageButton_command(self):
        name="The Last Gift"

        discover.read17 = tk.Button(d.second_frame,image=discover.ln)
        discover.read17.place(x=1135,y=1470,width=100,height=25)
        discover.read17["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(17)

    def book18ImageButton_command(self):
        name="The Luck of Roaring Camp"

        discover.read18 = tk.Button(d.second_frame,image=discover.ln)
        discover.read18.place(x=1375,y=1470,width=100,height=25)
        discover.read18["command"] = lambda:[self.inc(),self.playe(name),self.check(name)]
        self.forget(18)

    def forget(self,n):
        if n!=1:
            discover.read1.place_forget()
        if n!=2:
            discover.read2.place_forget()
        if n!=3:
            discover.read3.place_forget()
        if n!=4:
            discover.read4.place_forget()
        if n!=5:
            discover.read5.place_forget()
        if n!=6:
            discover.read6.place_forget()
        if n!=7:
            discover.read7.place_forget()
        if n!=8:
            discover.read8.place_forget()
        if n!=9:
            discover.read9.place_forget()
        if n!=10:
            discover.read10.place_forget()
        if n!=11:
            discover.read11.place_forget()
        if n!=12:
            discover.read12.place_forget()
        if n!=13:
            discover.read13.place_forget()
        if n!=14:
            discover.read14.place_forget()
        if n!=15:
            discover.read15.place_forget()
        if n!=16:
            discover.read16.place_forget()
        if n!=17:
            discover.read17.place_forget()
        if n!=18:
            discover.read18.place_forget()

    def moreButton_command(self):

        lb=tk.Listbox(d.second_frame)
        lb["borderwidth"] = "1px"
        ft = tkFont.Font(family='Times',size=12)
        lb["font"] = ft
        lb["fg"] = "#333333"
        lb["justify"] = "center"
        lb.place(x=374*3+280,y=55,width=87,height=90)
        lb["listvariable"] = "lb"
        lb.delete(0, tk.END)
        lb.insert(1,"Coins")
        lb.insert(2,"Best of 2021")
        lb.insert(3,"About Us")
        lb.insert(4,"Logout")
        lb.bind("<Double-Button-1>",self.double)

    def double(self,event):
        widget = event.widget
        selection=widget.curselection()
        if selection[0]==3:
            d.root.destroy()
            call(["python","main.py"])

class d:
    def __init__(self,username):
        d.username = username
        file = open("login_credentials.txt","r")
        flag=0
        d.coins=0
        for i,line in enumerate(file):
            if flag==0:
                if line.strip()==username:
                    flag=1
                    continue
            if flag==1:
                flag+=1
                continue
            if flag==2:
                d.coins = int(line.strip())
                break
        file.close()
        d.root = Tk()
        main_frame = Frame(d.root)
        main_frame.pack(fill=BOTH, expand=1)

        # Create A Canvas
        my_canvas = Canvas(main_frame)
        my_canvas.pack(side=LEFT, fill=BOTH, expand=1)

        # Add A Scrollbar To The Canvas
        my_scrollbar = ttk.Scrollbar(main_frame, orient=VERTICAL, command=my_canvas.yview)
        my_scrollbar.pack(side=RIGHT, fill=Y)

        # Configure The Canvas
        my_canvas.configure(yscrollcommand=my_scrollbar.set)
        my_canvas.bind('<Configure>', lambda e: my_canvas.configure(scrollregion = my_canvas.bbox("all")))

        # Create ANOTHER Frame INSIDE the Canvas
        d.second_frame = Frame(my_canvas)

        # Add that New frame To a Window In The Canvas
        my_canvas.create_window((0,0), window=d.second_frame, anchor="nw")
        dis = discover(d.second_frame)
        Label(d.second_frame).grid(row=0, column=0, pady=780, padx=1600)

        d.root.attributes('-fullscreen',True)

        d.root.mainloop()


import re

import bleach
import markdown
from bleach_allowlist import markdown_tags, markdown_attrs
from django import template
from django.utils.safestring import mark_safe

from bookmarks import utils
from bookmarks.models import UserProfile

register = template.Library()

@register.simple_tag(takes_context=True)
def update_query_string(context, **kwargs):
    query = context.request.GET.copy()

    # Replace query params with the ones from tag parameters
    for key in kwargs:
        query.__setitem__(key, kwargs[key])

    return query.urlencode()

@register.simple_tag(takes_context=True)
def add_tag_to_query(context, tag_name: str):
    params = context.request.GET.copy()

    # Append to or create query string
    if params.__contains__('q'):
        query_string = params.__getitem__('q') + ' '
    else:
        query_string = ''
    query_string = query_string + '#' + tag_name
    params.__setitem__('q', query_string)

    return params.urlencode()

@register.simple_tag(takes_context=True)
def remove_tag_from_query(context, tag_name: str):
    params = context.request.GET.copy()
    if params.__contains__('q'):
        # Split query string into parts
        query_string = params.__getitem__('q')
        query_parts = query_string.split()
        # Remove tag with hash
        tag_name_with_hash = '#' + tag_name
        query_parts = [part for part in query_parts if str.lower(part) != str.lower(tag_name_with_hash)]
        # When using lax tag search, also remove tag without hash
        profile = context.request.user_profile
        if profile.tag_search == UserProfile.TAG_SEARCH_LAX:
            query_parts = [part for part in query_parts if str.lower(part) != str.lower(tag_name)]
        # Rebuild query string
        query_string = ' '.join(query_parts)
        params.__setitem__('q', query_string)

    return params.urlencode()

@register.simple_tag(takes_context=True)
def replace_query_param(context, **kwargs):
    query = context.request.GET.copy()

    # Create query param or replace existing
    for key in kwargs:
        value = kwargs[key]
        query.__setitem__(key, value)

    return query.urlencode()

@register.filter(name='hash_tag')
def hash_tag(tag_name):
    return '#' + tag_name

@register.filter(name='first_char')
def first_char(text):
    return text[0]

@register.filter(name='remaining_chars')
def remaining_chars(text, index):
    return text[index:]

@register.filter(name='humanize_absolute_date')
def humanize_absolute_date(value):
    if value in (None, ''):
        return ''
    return utils.humanize_absolute_date(value)

@register.filter(name='humanize_relative_date')
def humanize_relative_date(value):
    if value in (None, ''):
        return ''
    return utils.humanize_relative_date(value)

@register.tag
def htmlmin(parser, token):
    nodelist = parser.parse(('endhtmlmin',))
    parser.delete_first_token()
    return HtmlMinNode(nodelist)

class HtmlMinNode(template.Node):
    def __init__(self, nodelist):
        self.nodelist = nodelist

    def render(self, context):
        output = self.nodelist.render(context)

        output = re.sub(r'\s+', ' ', output)

        return output

@register.simple_tag(name="markdown", takes_context=True)
def render_markdown(context, markdown_text):
    # naive approach to reusing the renderer for a single request
    # works for bookmark list for now
    if not ('markdown_renderer' in context):
        renderer = markdown.Markdown(extensions=['fenced_code', 'nl2br'])
        context['markdown_renderer'] = renderer
    else:
        renderer = context['markdown_renderer']

    as_html = renderer.convert(markdown_text)
    sanitized_html = bleach.clean(as_html, markdown_tags, markdown_attrs)

    return mark_safe(sanitized_html)


from __future__ import unicode_literals
import pyodbc
import sqlite3 as db
import datetime

def getitemsquickbooklistID():
    db_filename = 'db.sqlite3'
    cn2 = db.connect(db_filename)
    cursor2 = cn2.cursor()

    cursor2.execute("""Select QuickbookListID FROM pricequotation_item""")

    items_quickbooklistIDs = ()
    items_quickbooklistIDs = tuple(row[0] for row in cursor2.fetchall())

    cursor2.close()
    cn2.close()
    return items_quickbooklistIDs

def getpricelevelQuickbookListID():
    db_filename = 'db.sqlite3'
    cn2 = db.connect(db_filename)
    cursor2 = cn2.cursor()
    cursor2.execute("""Select QuickbookListID from pricequotation_pricelevel""")

    pricelevels_quickbooklistIDs = tuple(row[0] for row in cursor2.fetchall())

    #cursor2.execute("""Select QuickbookListID from pricequotation_pricelevel""")

    cursor2.close()
    cn2.close()
    return pricelevels_quickbooklistIDs

def getpricelevelperitem(items_quickbooklistIDs, pricelevels_quickbooklistIDs):
    cn1 = pyodbc.connect('DSN=QuickBooks Data;')
    cursor1 = cn1.cursor()

    db_filename = 'db.sqlite3'
    cn2 = db.connect(db_filename)
    cursor2 = cn2.cursor()

    cursor1.execute("""SELECT A.ListID ItemID, A.Name, A.FullName, A.SalesDesc, B.ListID PriceLevelListID, B.Name PriceLevelName, B.PriceLevelPerItemCustomPrice
FROM ItemInventoryAssembly A INNER JOIN PriceLevelPerItem B ON A.ListID = B.PriceLevelPerItemItemRefListID WHERE A.ListID IN %s AND B.ListID IN %s""" % (
    str(items_quickbooklistIDs), str(pricelevels_quickbooklistIDs)))

    params = []
    params = list((str(row.Name), str(row.FullName), str(row.SalesDesc), str(row.ItemID), str(row.PriceLevelName),
                   str(row.PriceLevelListID), float(row.PriceLevelPerItemCustomPrice)) for row in cursor1.fetchall())

    cursor2.execute("Delete FROM pricequotation_joinpricelevelperitem")
    cn2.commit()

    cursor2.executemany("""INSERT INTO pricequotation_joinpricelevelperitem (name, fullName, description, itemlistid, pricelevelname, pricelevellistid, customprice)
        VALUES (?,?,?,?,?,?,?)""", params)
    cn2.commit()

    cursor1.close()
    cn1.close()

    cursor2.close()
    cn2.close()

def updatepricelevelperitem():
    db_filename = 'db.sqlite3'
    cn2 = db.connect(db_filename)
    cursor2 = cn2.cursor()

    cursor2.execute("""select a.id item_id, a.name item_name, b.id pricelevel_id, b.name pricelevel_name, c.customprice
    from pricequotation_item a INNER JOIN pricequotation_joinpricelevelperitem c ON a.QuickbookListID = c.itemlistid
    INNER JOIN pricequotation_pricelevel b ON b.QuickbookListId = c.pricelevellistid""")

    params = list((int(row[0]), int(row[2]), float(row[4])) for row in cursor2.fetchall())

    cursor2.execute("""DELETE FROM pricequotation_pricelevelperitem""")
    cn2.commit()

    cursor2.executemany(
        """INSERT INTO pricequotation_pricelevelperitem (item_id, pricelevel_id, customprice) VALUES (?,?,?)""", params)
    cn2.commit()

    cursor2.close()
    cn2.close()

def updateitem():
    cn1 = pyodbc.connect('DSN=QuickBooks Data;')
    cursor1 = cn1.cursor()

    db_filename = 'db.sqlite3'
    cn2 = db.connect(db_filename)
    cursor2 = cn2.cursor()

    cursor2.execute("SELECT QuickbookListID FROM pricequotation_item")
    listid = tuple((row[0]) for row in cursor2.fetchall())

    cursor1.execute("SELECT ListID, SalesPrice, SalesDesc FROM ItemInventoryAssembly WHERE ListID IN %s" %str(listid))

    itemlist = tuple((float(row.SalesPrice), str(row.SalesDesc), str(row.ListID)) for row in cursor1.fetchall())

    cursor2.executemany("UPDATE pricequotation_item SET salesprice = ?, description = ? WHERE QuickbookListID = ?",
                        itemlist)
    cn2.commit()

    cursor1.close()
    cn1.close()
    cursor2.close()
    cn2.close()

starttime = datetime.datetime.now()
updateitem()
getpricelevelperitem(getitemsquickbooklistID(), getpricelevelQuickbookListID())
updatepricelevelperitem()
endtime = datetime.datetime.now()

print (endtime - starttime)


from Laban.LabanUtils import AbstractLabanAnalyzer
from Laban.LabanUtils import AbstractAnalysis
import utils.kinect.angleExtraction as ae
import utils.MovingAverage as ma
import numpy as np
import utils.kinect.jointsMap as jm

class AdvanceAndRetreate(AbstractAnalysis.AbstractAnalysis):

    def getPositiveAndNegetive(self):
        return 'Advance', 'Retreate'

    #def wrapper(self, lineInFloats, headers, jointsIndices):
     #   return ae.jointsMovementInDirection(lineInFloats, jointsIndices, [0,1,0])

    def extract(self, fileName):
        f = open(fileName, 'r')
        headers = f.readline().split()
        headers = jm.getFileHeader(headers)
        jointsIndices = self.extractor.getJointsIndices(headers)
        frontDirections = []
        centers = []
        sholderVec = []
        for line in f:
            lineInFloats=[float(v) for v in line.split()]
            centers.append(ae.calcJointsAverage(lineInFloats, jointsIndices))
            shouldersVecOnXZPlan = ae.getVecBetweenJoints(headers, lineInFloats,\
                                   'ShoulderRight_X', 'ShoulderLeft_X')
            #sholderVec.append(shouldersVecOnXZPlan)
            frontDirections.append([shouldersVecOnXZPlan[2], 0 , -shouldersVecOnXZPlan[0]])
            #frontDirections.append([1,0,0])
        frontDirections = zip(*ma.partsmovingAverage(zip(*frontDirections), 50, 1))
        movingDirections = np.diff(centers, axis=0)
        movingDirections = zip(*ma.partsmovingAverage(zip(*movingDirections), 50, 1))
        advancements = []
        for front, move in zip(frontDirections[:-1], movingDirections):
            if np.abs(front[0])/np.abs(front[2]) > 2:
                front = [ae.length(front),0,0]
            product = np.dot(front, move)
            advancements.append(product)
        return advancements
        """
        advancements = ma.movingAverage(advancements, 50, 1)
        advancements = pu.normalizeVector(advancements)
        clustersByPercentilies = spike.clusterByPercemtile(advancements,1000, 75)
        clustersByPercentilies = pu.smoothOutliers(clustersByPercentilies)
        plt.figure()
        plt.plot(clustersByPercentilies)
        #advancements = pu.normalizeVector(advancements)
        plt.plot(advancements)
        ranges = self.prepareRanges(clustersByPercentilies)
        self.printConfedence(advancements, ranges, 'Advancing', 'Retreating')
        plt.show()
        """
def analyze(inputFile):
    extractor = AbstractLabanAnalyzer.getExtractor(inputFile)
    analysis = AdvanceAndRetreate(extractor)
    return analysis.extract(inputFile)


import argparse
import json
import os
from os.path import join

import torch
import torch.utils.data as data
import torchvision.utils as vutils
from torchvision import transforms
from attgan import AttGAN
from data import check_attribute_conflict
from helpers import Progressbar
from utils import find_model
from dataset import MultiCelebADataset

def parse(args=None):
    parser = argparse.ArgumentParser()
    parser.add_argument('--experiment_name', dest='experiment_name', required=True)
    parser.add_argument('--test_int', dest='test_int', type=float, default=1.0)
    parser.add_argument('--num_test', dest='num_test', type=int)
    parser.add_argument('--load_epoch', dest='load_epoch', type=str, default='latest')
    parser.add_argument('--custom_img', action='store_true')
    parser.add_argument('--custom_data', type=str, default='./data/custom')
    parser.add_argument('--custom_attr', type=str, default='./data/list_attr_custom.txt')
    parser.add_argument('--gpu', action='store_true')
    parser.add_argument('--multi_gpu', action='store_true')
    parser.add_argument('--input_path', type=str, default='/home/psdz/money/duibihisd/re1/4')
    return parser.parse_args(args)

args_ = parse()
print(args_)

with open(join('output', args_.experiment_name, 'setting.txt'), 'r') as f:
    args = json.load(f, object_hook=lambda d: argparse.Namespace(**d))

args.test_int = args_.test_int
args.num_test = args_.num_test
args.gpu = args_.gpu
args.load_epoch = args_.load_epoch
args.multi_gpu = args_.multi_gpu
args.custom_img = args_.custom_img
args.custom_data = args_.custom_data
args.custom_attr = args_.custom_attr
args.n_attrs = len(args.attrs)
args.betas = (args.beta1, args.beta2)
args.input_path = args_.input_path

print(args)
output_path = join('output', args.experiment_name, 'sample_testing')
os.makedirs(output_path, exist_ok=True)

transform = transforms.Compose([
            transforms.Resize(128),
            transforms.ToTensor(),
            transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]),

attgan = AttGAN(args)
attgan.load(find_model(join('output', args.experiment_name, 'checkpoint'), args.load_epoch))
progressbar = Progressbar()

if os.path.isfile(args.input_path):
    inputs = [args.input_path]
else:
    inputs = [os.path.join(args.input_path, file_name) for file_name in os.listdir(args.input_path)]

attgan.eval()

with torch.no_grad():
    for input in inputs:
        x = transform(Image.open(input).convert('RGB')).unsqueeze(0).cuda()
        c = E(x)
        c_trg = c
        for j in range(len(steps)):
            step = steps[j]
            if step['type'] == 'latent-guided':
                if step['seed'] is not None:
                    torch.manual_seed(step['seed'])
                    torch.cuda.manual_seed(step['seed'])

                z = torch.randn(1, noise_dim).cuda()
                s_trg = M(z, step['tag'], step['attribute'])

            elif step['type'] == 'reference-guided':
                reference = transform(Image.open(step['reference']).convert('RGB')).unsqueeze(0).cuda()
                s_trg = F(reference, step['tag'])

            c_trg = T(c, s_trg, step['tag'])

        x_trg = G(c_trg)
        vutils.save_image(((x_trg + 1)/ 2).data, os.path.join(opts.output_dir, f'{os.path.basename(input)}_output_{i}.jpg'), padding=0)

for i in range(len(attrs_default)):
    img_a, att_a = next(dataset.gen(i, False))
    img_b, att_b = next(dataset.gen(i, True))
    img_a = img_a.cuda() if args.gpu else img_a
    att_a = att_a.cuda() if args.gpu else att_a
    att_a = att_a.type(torch.float)
    img_b = img_b.cuda() if args.gpu else img_b
    att_b = att_b.cuda() if args.gpu else att_b
    #att_b = att_b.type(torch.float)
    with torch.no_grad():
        for j in range(30):
             ide = torch.randperm(len(att_a))
             att_b = att_b[ide].contiguous()
             img_b = img_b[ide]
             att_b = att_b.type(torch.float)
             _, zs_a = attgan.G(img_a, mode='enc')
             _, zs_b = attgan.G(img_b, mode='enc')
             h1_a, h2_a = torch.split(zs_a, 512, dim=1)
             h1_b, h2_b = torch.split(zs_b, 512, dim=1)
             z_b, gen2_b = attgan.classify(zs_b, att_b)
             z_a, gen2_a = attgan.classify(zs_a, att_a)
             h_a1b2 = torch.cat([h1_a, gen2_b], dim=1)
             h_b1a2 = torch.cat([h1_b, gen2_a], dim=1)
             img_fake_a = attgan.G(h_a1b2, mode='dec')
             img_fake_b = attgan.G(h_b1a2, mode='dec')
             viz_images = torch.stack([img_a,img_b,img_fake_a], dim=1)
             viz_images = viz_images.view(-1, *list(img_a.size())[1:])
             vutils.save_image(viz_images,
                                  '%s/niter_%03d_%03d.png' % (output_path, i, j),
                                  nrow=3 * 4,
                                  normalize=True,  range=(-1., 1.))


import asyncio
from datetime import datetime, timedelta
from discord.ext import commands, tasks
from notification import notification

check_reminder_interval = 1.0
interval_lookup = {
    "second": 1,
    "minute": 60,
    "hour": 3600,
    "day": 86400,
    "week": 604800,
    "month": 2419200
}
special_interval_dictionary = {
    "daily": 86400,
    "weekly": 604800,
    "monthly": 2419200,
    "annually": 29030400
}

class NotificationCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.notificationList = {}
        self.notificationListLock = asyncio.BoundedSemaphore()
        self.check_reminder.start()

    @tasks.loop(seconds=check_reminder_interval)
    async def check_reminder(self):
        """
        Process if any reminders need to be sent.
        Loops every check_reminder_interval to check if any reminders need to be sent.
        Compares if the last time the message has been sent has elapsed the requested interval.
        """
        for _, reminder in self.notificationList.items():
            if reminder.started and (reminder.lastRunTime is None
                                     or reminder.lastRunTime + timedelta(seconds=reminder.runInterval)
                                     < datetime.now()):
                await reminder.send_reminder()

    @commands.command()
    async def create(self, ctx, *args):
        """
        Create a new reminder.
        Ex. !create test 5 minutes "Remind me please!" "https://google.com (OPTIONAL)"
        Ex. !create test daily "YYYY-MM-dd 13:00:00" "Remind me please!" "https://google.com (OPTIONAL)"
        :param ctx: The context from which a message was received.
        :param args: passed list of arguments to the create method.
        :return: The reminder object that was created.
        """
        if len(args) < 1:
            await ctx.send("Invalid number of arguments.")
            return -1

        async with self.notificationListLock:
            if args[0] is not None and args[0] in self.notificationList:
                await ctx.send("Reminder _{0}_ already exists".format(args[0]))
                return -1

        is_special_time_unit = False
        interval = 0
        if len(args) >= 2:
            is_special_time_unit = args[1] in special_interval_dictionary
            interval = parse_interval(args[1], args[2] if len(args) >= 3 else None)

        if interval < 0:
            await ctx.send("Invalid interval provided")
            return -1

        reminder = notification()
        reminder.ctx = ctx
        reminder.runInterval = interval
        if len(args) >= 3 and is_special_time_unit:
            # This parameter must be an interval at the time of day
            try:
                parsed_time = datetime.strptime(args[2], '%Y-%m-%d %H:%M:%S')
                reminder.lastRunTime = datetime.now()\
                    .replace(hour=parsed_time.hour, minute=parsed_time.minute, second=parsed_time.second)
                if reminder.lastRunTime < datetime.now():
                    await ctx.send("Please enter a valid date that's not in the past!")
                    return -1
            except ValueError:
                await ctx.send("Please enter a date to run the reminder in the 24-hour format YYYY-mm-dd HH:MM:SS")
                return -1

        if len(args) >= 4:
            reminder.notification_text = args[3]
        else:
            # any other case, text is not set
            reminder.notification_text = None

        if len(args) >= 5:
            reminder.notification_link = args[4]
        else:
            # any other case, link is not set
            reminder.notification_link = None

        async with self.notificationListLock:
            self.notificationList[args[0]] = reminder
        await ctx.send("Reminder _{0}_ was successfully created.".format(args[0]))
        return reminder

    @commands.command()
    async def delete(self, ctx, arg):
        """
        Delete a reminder that was created.
        :param ctx: The context from where the message was sent.
        :param arg: The key of the reminder to delete.
        """
        if await self.is_key_valid(ctx, arg):
            async with self.notificationListLock:
                del self.notificationList[arg]
            await ctx.send("Reminder _{0}_ was successfully deleted.".format(arg))

    @commands.command()
    async def list(self, ctx):
        """
        List all reminders that have been created.
        :param ctx: The context from where the message was sent..
        """
        if len(self.notificationList) > 0:
            async with self.notificationListLock:
                await ctx.send(
                    wrap_code_block(
                        "\n".join("{0}\t\t{1}".format(i + 1, key) for i, key in enumerate(self.notificationList))))
        else:
            await ctx.send("There are no notifications.")

    @commands.command()
    async def status(self, ctx, arg):
        """
        Retrieve the current status of the specified reminder.
        :param ctx: the context of where the command was received.
        :param arg: the key of the notification to get the status of.
        """
        if await self.is_key_valid(ctx, arg):
            reminder = await self.get_reminder(arg)
            interval_unit = get_time_unit(reminder.runInterval)
            await ctx.send(wrap_code_block(
                "Name: {0}\r\n"
                "Current Text: {1}\r\n"
                "Current Link: {2}\r\n"
                "Running Interval: {3} {4}(s)\r\n"
                "Running: {5}\r\n"
                "Last Ran: {6}".format(arg,
                                       reminder.notification_text if reminder.notification_text is not None else "N/A",
                                       reminder.notification_link if reminder.notification_link is not None else "N/A",
                                       round(reminder.runInterval / interval_lookup[interval_unit], 2),
                                       interval_unit,
                                       reminder.started,
                                       reminder.lastRunTime if reminder.lastRunTime is not None else "N/A")))

    @commands.command()
    async def start(self, ctx, arg):
        """
        Start sending notifications for the specified key.
        Ex: !Start "some_key"
        :param ctx: the context of where the command was received.
        :param arg: the key of the notification to start.
        :return: a boolean indicating if the notification was successfully started.
        """
        if await self.is_key_valid(ctx, arg):
            reminder = await self.get_reminder(arg)
            if reminder.notification_text is None:
                await ctx.send("You must set a notification text for _{0}_\r\n"
                               "Ex. !text {0} \"Hello World\"".format(arg))
                return False
            elif reminder.runInterval is None or reminder.runInterval <= 0:
                await ctx.send("You must set a valid run interval for _{0}_\r\n"
                               "Ex. !interval {0} 10 seconds".format(arg))
                return False
            else:
                await reminder.set_ctx(ctx)
                await reminder.set_started(True)
                await ctx.send("Reminder _{0}_ was started.".format(arg))
                return True

        return False

    @commands.command()
    async def stop(self, ctx, arg):
        """
        Stop sending notifications for the specified key.
        Ex. !stop "some_key"
        :param ctx: the context of where the command was received.
        :param arg: the key of the notification to stop.
        :return: a boolean indicating if the notification was successfully stopped.
        """
        if await self.is_key_valid(ctx, arg):
            reminder = await self.get_reminder(arg)
            await reminder.set_started(False)
            await ctx.send("Reminder _{0}_ was stopped.".format(arg))
            return True

        return False

    @commands.command()
    async def link(self, ctx, *args):
        """
        Set the link at the specified notification.
        Ex. !link "some_key" "https://google.com".
        :param ctx: the context of where the command was received.
        :param args: the key of the notification to set the link.
        :return: a boolean indicating if the notification link was successfully set.
        """
        if await self.is_key_valid(ctx, args[0]):
            reminder = await self.get_reminder(args[0])
            await reminder.set_link(args[1])
            await ctx.send("Updated link for _{0}_.".format(args[0]))
            return True

        return False

    @commands.command()
    async def text(self, ctx, *args):
        """
        Set the text at the specified notification.
        Ex. !link "some_key" "some_text"
        :param ctx: The context of where the command was received.
        :param args: The key of the notification to set the text.
        :return: a boolean indicating if the notification text was successfully set.
        """
        if await self.is_key_valid(ctx, args[0]):
            reminder = await self.get_reminder(args[0])
            await reminder.set_text(args[1])
            await ctx.send("Updated text for _{0}_.".format(args[0]))
            return True

        return False

    @commands.command()
    async def interval(self, ctx, *args):
        """
        Set the interval of the specified reminder.
        :param ctx: The context of where the command was received.
        :param args: The arguments containing the key to update and the specified interval to set it to.
        :return: The interval that it was set to.
        """
        if await self.is_key_valid(ctx, args[0]):
            interval = parse_interval(args[1], args[2]) if len(args) >= 3 else -1
            if interval < 0:
                await ctx.send("Invalid interval provided\r\n"
                               "Ex. !interval 10 seconds")
                return -1

            reminder = await self.get_reminder(args[0])
            await reminder.set_interval(interval)
            await ctx.send("Interval for {0} updated.".format(args[0]))
            return interval

    @commands.command()
    async def test(self, ctx, arg):
        """
        Send a test message to yourself.
        :param ctx: The context in which the message was sent
        """
        if await self.is_key_valid(ctx, arg):
            reminder = await self.get_reminder(arg)
            await reminder.send_reminder(ctx, False)

    @check_reminder.before_loop
    async def before_check_reminder_loop(self):
        """
        Ensure that the bot is ready before we begin looping
        """
        await self.bot.wait_until_ready()

    async def is_key_valid(self, ctx, key):
        """
        Check if a key already exists and send a message if it does not.
        :param ctx: The context of where to send the message.
        :param key: The requested key to check if it exists/does not exist.
        :return: A boolean indicating if a the key is valid or not.
        """
        async with self.notificationListLock:
            if key in self.notificationList:
                return True
            else:
                await ctx.send("Reminder _{0}_ does not exist.".format(key))
                return False

    async def get_reminder(self, key):
        """
        Get the reminder at the specified key.
        :param key: The key to search in the dictionary.
        :return: The reminder object at the specified key.
        """
        async with self.notificationListLock:
            return self.notificationList[key]

def parse_interval(interval, interval_unit):
    """
    Parses a provided interval to see if it is valid.
    Also returns the associated unit that is formatted correctly.
    :param interval: The interval string that was entered.
    :param interval_unit: The unit of time provided for the interval.
    :return: The actual interval based off the time factor.
    """
    if interval in special_interval_dictionary:
        return special_interval_dictionary[interval]

    time_factor = 1
    if interval_unit is not None:
        time_unit = interval_unit.lower().rstrip('s')
        if time_unit in interval_lookup:
            time_factor = interval_lookup[time_unit]
    try:
        parsed_interval = float(interval)
        if parsed_interval * time_factor >= check_reminder_interval:
            return parsed_interval * time_factor
        else:
            return -1
    except ValueError:
        return -1

def wrap_code_block(message):
    """
    Wrap a message in a discord code block
    :param message: The message to wrap
    :return: A message wrapped in a code block
    """
    return "```\r\n{0}\r\n```".format(message)

def get_time_unit(interval):
    """
    Get the associated time unit that fits the interval best
    :param interval: The interval in seconds
    :return: The unit of time that best fits
    """
    for key in reversed(interval_lookup):
        if interval / interval_lookup[key] >= 1:
            return key

    return "second"


import socketserver
import threading
import time
import socket
import re
import os
import json

with open('server.json', encoding='utf-8') as file:
    conf = json.load(file)

HOST = conf['HOST']  # Задаем адрес диспетчра
PORT = conf['PORT']  # Задаем порт диспетчера
ServerAddress = (HOST, PORT)
TIMEOUT = conf['TIMEOUT']  # время ожидания ответа от калькулятора

calc_ready = set()  # глобальная очередь свободныx калькуляторов
TURN = []  # глобальная очередь запросов от клиентов

# создаем Log файл при первом запуске
if os.path.isfile('server.log') is not True:
    with open('server.log', 'a', encoding='utf-8') as f_obj:
        string = f'{time.ctime()}: Первый запуск\n'
        f_obj.write(string)

# функция записи лога
def log_insert(log):
    with open('server.log', 'a', encoding='utf-8') as f_obj:
        string = f'{time.ctime()}: {log}\n'
        f_obj.write(string)

class MyUDPRequestHandler(socketserver.BaseRequestHandler):

    def message(self):
        message = bytes.decode(self.request[0])
        return message

    def add_ready(self):
        message = self.message()
        found_str = 'ready'
        found_address = r'\"[a-z0-9.]+\"\, [0-9]+'
        result = re.search(found_str, message)
        if result:
            add_port = re.findall(found_address, message)[0]
            address, port = add_port.split(',')
            correct_address = address.split('"')[1]
            port = int(port)
            free_calc = (correct_address, port)
            calc_ready.add(free_calc)
            return True
        else:
            self._add_turn()   # если пришел не ready, то добавляем его в очередь запросо
            return False

    def _add_turn(self):
        message = self.request[0]
        sock = self.request[1]
        address = self.client_address
        thread = {'msg': message, 'sock': sock, 'address': address}
        TURN.append(thread)
        log_message = f'Дата:{time.ctime()} от {self.client_address} сообщение {self.request[0]}'
        # log_insert(log_message)
        print(log_message)

    def handle(self):
        ready = self.add_ready()  # если пришел ready, то добавляем его в очередь свободных калькуляторов

        if ready is not True:
            while len(TURN) > 0 and len(calc_ready) > 0:
                thread = TURN.pop(0)
                datagram = thread['msg']
                sock = thread['sock']
                address = thread['address']
                #  цикл на тот случай если не придет ответ от вычислителя и переслать запрос повторно другому свободному
                while len(calc_ready) > 0:
                    udp_forward = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
                    try:
                        calc = calc_ready.pop()
                    except KeyError:
                        TURN.insert(0, thread)
                        continue
                    try:
                        udp_forward.sendto(datagram, calc)
                        udp_forward.settimeout(0.05)
                        ok = udp_forward.recvfrom(1024)  # Ожидаем 'ок' от вычислителя, если не получаем что шлем запрос другому свободному
                        if ok[0] == b'ok':
                            udp_forward.settimeout(TIMEOUT)
                            answer = udp_forward.recvfrom(1024)
                            sock.sendto(answer[0], address)
                            calc_ready.add(calc)
                            log_message = f'дата: {time.ctime()} отправленно {address} сообщение {answer[0]}'
                            # log_insert(log_message)
                            print(log_message)
                            break
                        else:
                            continue
                    except Exception as e:
                        if len(calc_ready) == 0:
                            TURN.insert(0, thread)
                        # log_insert(e)
                        print(f'{time.ctime()}; error: {e}')  # пишем всю эту хрень в лог
                time.sleep(0.01)

UDPServerObject = socketserver.ThreadingUDPServer(ServerAddress, MyUDPRequestHandler)
server_thread = threading.Thread(target=UDPServerObject.serve_forever())
server_thread.daemon = True
server_thread.start()


import os

from gi.repository import GLib, Gtk

from gaphor.abc import Service
from gaphor.core import event_handler
from gaphor.ui import APPLICATION_ID
from gaphor.ui.event import FileLoaded, FileSaved

class RecentFiles(Service):
    def __init__(self, event_manager, recent_manager=None):
        self.event_manager = event_manager
        self.recent_manager = recent_manager or Gtk.RecentManager.get_default()

        self.event_manager.subscribe(self._on_filename_changed)

    def shutdown(self):
        self.event_manager.unsubscribe(self._on_filename_changed)

    @event_handler(FileLoaded, FileSaved)
    def _on_filename_changed(self, event):
        filename = event.filename
        if not filename:
            return
        uri = GLib.filename_to_uri(os.path.abspath(filename))
        meta = Gtk.RecentData()
        meta.app_name = APPLICATION_ID
        meta.app_exec = f"{GLib.get_prgname()} %u"
        meta.mime_type = "application/x-gaphor"
        self.recent_manager.add_full(uri, meta)


#import libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

#read data
df = pd.read_csv('who_suicide_statistics.csv')

#check for null
print(df.isnull().sum())

print(df.info())

plt.figure(figsize=(25 , 25))
# sns.heatmap(df.isnull())
# plt.show()

#check percent of the missing the data
print(df.isnull().sum() / df.shape[0] *100)

#gives the column names of null values
null_var = df.isnull().sum() / df.shape[0] *100
drop_col = null_var[null_var>2 ].keys()
print(drop_col)

# Remove columns having null values
print(df.drop(columns=drop_col))
data = df.drop(columns=drop_col)
# sns.heatmap(data.isnull())
# plt.show()

#drop rows
data2= df.dropna()
print(data2.isnull().sum())

# sns.distplot(df['population'])
# sns.distplot(data2['population'])
# plt.show()

df3 = data2.select_dtypes(include=['int64','float64']).columns
# print(df3)

num_val = ['year', 'suicides_no', 'population']

for i,var in enumerate(num_val):
    plt.subplot(9,4,i+1)
    sns.distplot(df[var], bins=20)
    sns.distplot(data2[var], bins=20)
# plt.show()

df4 = data2.select_dtypes(include=['object']).columns
# print(df4)
# print(data2.shape[0])
df5 = df['country'].value_counts() / data2.shape[0] *100
print(data2['country'].value_counts() / data2.shape[0] *100)
print(df5)

print(pd.concat([df['country'].value_counts() / data2.shape[0] *100 , data2['country'].value_counts() / data2.shape[0] *100] , axis=1 , keys = ['country_org' , 'country_clean']))


from __future__ import absolute_import, division, print_function
from cctbx import geometry_restraints
import cctbx.geometry_restraints.flags
import cctbx.geometry_restraints.manager
import cctbx.geometry_restraints.lbfgs
from cctbx import xray
from cctbx import crystal
import cctbx.crystal.coordination_sequences
from cctbx import sgtbx
from cctbx.array_family import flex
from scitbx import matrix as mx
import scitbx.lbfgs
from libtbx.str_utils import format_value
from itertools import count
import sys
from six.moves import range

if (1):
  flex.set_random_seed(0)

class restraint_parameters(object):

  def __init__(self, distance_ideal, weight):
    self.distance_ideal = distance_ideal
    self.weight = weight

restraint_parameters_si_o = restraint_parameters(1.61, 2.0)
restraint_parameters_o_si_o = restraint_parameters(2.629099, 0.41)
restraint_parameters_si_o_si = restraint_parameters(3.070969, 0.2308)

def setup_bond_params_table(structure, bond_sym_table):
  scatterers = structure.scatterers()
  t = geometry_restraints.bond_params_table(scatterers.size())
  for i_seq,bond_sym_dict in enumerate(bond_sym_table):
    for j_seq in bond_sym_dict.keys():
      i_seqs = [i_seq, j_seq]
      i_seqs.sort()
      scattering_types = [scatterers[i].scattering_type for i in i_seqs]
      scattering_types.sort()
      if (scattering_types == ["Si", "Si"]):
        params = restraint_parameters_si_o_si
      elif (scattering_types == ["O", "Si"]):
        params = restraint_parameters_si_o
      elif (scattering_types == ["O", "O"]):
        params = restraint_parameters_o_si_o
      else:
        raise AssertionError("Unknown scattering type pair.")
      if (j_seq not in t[i_seq]):
        t[i_seq][j_seq] = geometry_restraints.bond_params(
          distance_ideal=params.distance_ideal,
          weight=params.weight)
      else:
        prev_params = t[i_seq][j_seq]
        assert abs(prev_params.distance_ideal - params.distance_ideal) < 1.e-8
        assert abs(prev_params.weight - params.weight) < 1.e-8
  return t

def setup_nonbonded_params():
  p = geometry_restraints.nonbonded_params()
  d = p.distance_table
  d.setdefault("Si")["Si"] = 3.1
  d.setdefault("Si")["O"] = 1.5
  d.setdefault("O")["O"] = 2.0
  return p

class add_oxygen(object):

  def __init__(self, si_structure, si_si_sym_table):
    self.structure = si_structure.deep_copy_scatterers()
    bond_sym_table = crystal.pair_sym_table(si_si_sym_table.size())
    sites_frac = si_structure.sites_frac()
    i_oxygen = count(1)
    for i_seq,pair_sym_dict in enumerate(si_si_sym_table):
      site_frac_i = mx.col(sites_frac[i_seq])
      for j_seq,sym_ops in pair_sym_dict.items():
        assert j_seq >= i_seq
        for rt_mx_ji in sym_ops:
          site_frac_ji = mx.col(rt_mx_ji * sites_frac[j_seq])
          bond_center = (site_frac_i + site_frac_ji) / 2
          i_seq_o = self.structure.scatterers().size()
          self.structure.add_scatterer(xray.scatterer(
            label="O%d"%next(i_oxygen),
            site=bond_center))
          bond_sym_table[i_seq].setdefault(i_seq_o).append(
            sgtbx.rt_mx(1,1))
          bond_sym_table[j_seq].setdefault(i_seq_o).append(
            rt_mx_ji.inverse_cancel())
          bond_sym_table.append(crystal.pair_sym_dict())
    self.bond_sym_table = bond_sym_table.tidy(
      site_symmetry_table=self.structure.site_symmetry_table())

def make_o_si_o_sym_table(si_o_structure, si_o_bond_sym_table):
  scatterers = si_o_structure.scatterers()
  si_o_full_sym_table = si_o_bond_sym_table.full_connectivity(
    site_symmetry_table=si_o_structure.site_symmetry_table())
  o_si_o_sym_table = crystal.pair_sym_table(si_o_full_sym_table.size())
  for i_seq,pair_sym_dict in enumerate(si_o_full_sym_table):
    if (scatterers[i_seq].scattering_type != "Si"): continue
    jr_list = []
    for j_seq,sym_ops in pair_sym_dict.items():
      if (scatterers[j_seq].scattering_type != "O"): continue
      for rt_mx_ji in sym_ops:
        jr_list.append((j_seq,rt_mx_ji))
    for i_jj1 in range(0,len(jr_list)-1):
      jr1 = jr_list[i_jj1]
      i_seq = jr1[0]
      rt_mx_jr1_inv = jr1[1].inverse()
      for i_jj2 in range(i_jj1+1,len(jr_list)):
        jr2 = jr_list[i_jj2]
        j_seq = jr2[0]
        rt_mx_jr21 = rt_mx_jr1_inv.multiply(jr2[1])
        if (i_seq <= j_seq):
          o_si_o_sym_table[i_seq].setdefault(j_seq).append(rt_mx_jr21)
        else:
          o_si_o_sym_table[j_seq].setdefault(i_seq).append(rt_mx_jr21.inverse())
  return o_si_o_sym_table.tidy(
    site_symmetry_table=si_o_structure.site_symmetry_table())

class distance_and_repulsion_least_squares:

  def __init__(self,
        si_structure,
        distance_cutoff,
        nonbonded_distance_cutoff=None,
        nonbonded_buffer=1,
        nonbonded_repulsion_function_type="gaussian",
        nonbonded_max_residual_bond_stretch_factor=1.0,
        n_trials=1,
        n_macro_cycles=2,
        max_exceptions_handled=10,
        connectivities=None,
        out=None,
        dev=False):
    assert nonbonded_repulsion_function_type in ["gaussian", "cos", "prolsq"]
    assert n_trials > 0
    assert n_macro_cycles > 0
    assert max_exceptions_handled >= 0
    if (out is None): out = sys.stdout
    si_structure.show_summary(f=out).show_scatterers(f=out)
    print(file=out)
    out.flush()
    def get_si_si_sym_table():
      si_asu_mappings = si_structure.asu_mappings(
        buffer_thickness=distance_cutoff)
      asu_table = crystal.pair_asu_table(asu_mappings=si_asu_mappings)
      asu_table.add_all_pairs(distance_cutoff=distance_cutoff)
      si_si_sym_table = asu_table.extract_pair_sym_table()
      si_pair_counts = si_structure.pair_sym_table_show_distances(
        pair_sym_table=si_si_sym_table,
        out=out)
      if (connectivities is not None):
        assert list(si_pair_counts) == connectivities
      print(file=out)
      return si_si_sym_table, si_pair_counts
    si_si_sym_table, si_pair_counts = get_si_si_sym_table()
    out.flush()
    si_o = add_oxygen(
      si_structure=si_structure,
      si_si_sym_table=si_si_sym_table)
    si_o.structure.show_summary(f=out).show_scatterers(f=out)
    si_o_sst = si_o.structure.site_symmetry_table()
    print(file=out)
    out.flush()
    si_o_pair_counts = si_o.structure.pair_sym_table_show_distances(
      pair_sym_table=si_o.bond_sym_table,
      out=out)
    n_si = si_pair_counts.size()
    n_si_o = si_o_pair_counts.size()
    assert si_o_pair_counts[:n_si].all_eq(si_pair_counts)
    assert si_o_pair_counts[n_si:].count(2) == n_si_o-n_si
    print(file=out)
    out.flush()
    o_si_o_sym_table = make_o_si_o_sym_table(
      si_o_structure=si_o.structure,
      si_o_bond_sym_table=si_o.bond_sym_table)
    o_si_o_pair_counts = si_o.structure.pair_sym_table_show_distances(
      pair_sym_table=o_si_o_sym_table,
      out=out)
    assert o_si_o_pair_counts[:n_si].all_eq(0)
    if (si_pair_counts.count(4) == n_si):
      assert o_si_o_pair_counts[n_si:].all_eq(6)
    print(file=out)
    out.flush()
    shell_sym_tables = crystal.coordination_sequences.shell_sym_tables(
      full_pair_sym_table=si_o.bond_sym_table.full_connectivity(
        site_symmetry_table=si_o_sst),
      site_symmetry_table=si_o_sst,
      max_shell=3)
    if (1):
      shell_sym_tables[0].add_pair_sym_table_in_place(other=si_si_sym_table)
    if (1):
      shell_sym_tables[0].add_pair_sym_table_in_place(other=o_si_o_sym_table)
    shell_sym_tables = [_.tidy(site_symmetry_table=si_o_sst)
      for _ in shell_sym_tables]
    bond_params_table = setup_bond_params_table(
      structure=si_o.structure,
      bond_sym_table=shell_sym_tables[0])
    nonbonded_params = setup_nonbonded_params()
    nonbonded_types = flex.std_string()
    for scatterer in si_o.structure.scatterers():
      nonbonded_types.append(scatterer.scattering_type)
    if (nonbonded_repulsion_function_type == "gaussian"):
      nonbonded_function = geometry_restraints.gaussian_repulsion_function(
        max_residual=bond_params_table.mean_residual(
          bond_stretch_factor=nonbonded_max_residual_bond_stretch_factor))
      if (nonbonded_distance_cutoff is None):
        nonbonded_distance_cutoff = 7
    elif (nonbonded_repulsion_function_type == "cos"):
      nonbonded_function = geometry_restraints.cos_repulsion_function(
        max_residual=bond_params_table.mean_residual(
          bond_stretch_factor=nonbonded_max_residual_bond_stretch_factor))
    else:
      nonbonded_function = geometry_restraints.prolsq_repulsion_function()
    geometry_restraints_manager = geometry_restraints.manager.manager(
      crystal_symmetry=si_o.structure,
      site_symmetry_table=si_o_sst,
      bond_params_table=bond_params_table,
      shell_sym_tables=shell_sym_tables,
      nonbonded_params=nonbonded_params,
      nonbonded_types=nonbonded_types,
      nonbonded_function=nonbonded_function,
      nonbonded_distance_cutoff=nonbonded_distance_cutoff,
      nonbonded_buffer=nonbonded_buffer,
      max_reasonable_bond_distance=100)
    minimized = None
    for i_trial in range(n_trials):
      for i_exceptions_handled in range(max_exceptions_handled+1):
        trial_structure = si_o.structure.deep_copy_scatterers()
        if (i_trial > 0):
          n_scatterers = trial_structure.scatterers().size()
          trial_structure.set_sites_cart(flex.vec3_double(flex.random_double(
            size=n_scatterers*3)*10-5))
          trial_structure.apply_symmetry_sites()
        trial_minimized = []
        trial_sites_cart = None
        for i_macro_cycle in range(n_macro_cycles):
          if (trial_sites_cart is not None):
            trial_structure.set_sites_cart(sites_cart=trial_sites_cart)
            trial_structure = trial_structure.random_shift_sites(
              max_shift_cart=0.2)
            trial_structure.apply_symmetry_sites()
          trial_sites_cart = trial_structure.sites_cart()
          geometry_restraints_flags = geometry_restraints.flags.flags(
            bond=True,
            nonbonded=((i_macro_cycle % 2) != (n_macro_cycles % 2)))
          if (not dev):
            try:
              m = geometry_restraints.lbfgs.lbfgs(
                sites_cart=trial_sites_cart,
                correct_special_position_tolerance=1.0,
                geometry_restraints_manager=geometry_restraints_manager,
                geometry_restraints_flags=geometry_restraints_flags,
                lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
                  max_iterations=100),
                lbfgs_exception_handling_params=
                  scitbx.lbfgs.exception_handling_parameters(
                    ignore_line_search_failed_step_at_lower_bound=True))
            except RuntimeError as lbfgs_error:
              if (i_trial == 0): raise
              if (not str(lbfgs_error).startswith(
                    "Bond distance > max_reasonable_bond_distance: ")): raise
              m = None
              break
            else:
              trial_minimized.append(m)
              trial_structure.set_sites_cart(sites_cart=trial_sites_cart)
          else:
            m = dev_lbfgs(
              sites_cart=trial_sites_cart,
              geometry_restraints_manager=geometry_restraints_manager,
              geometry_restraints_flags=geometry_restraints_flags,
              lbfgs_termination_params=scitbx.lbfgs.termination_parameters(
                max_iterations=100),
              lbfgs_exception_handling_params=
                scitbx.lbfgs.exception_handling_parameters(
                  ignore_line_search_failed_step_at_lower_bound=True))
            trial_minimized.append(m)
            trial_structure.set_sites_cart(sites_cart=trial_sites_cart)
        if (m is not None):
          break
      else:
        raise RuntimeError(
          "max_exceptions_handled=%d exceeded: %s" % (
            max_exceptions_handled, str(lbfgs_error)))
      ftr = trial_minimized[-1].final_target_result
      pair_proxies = geometry_restraints_manager.pair_proxies(
        sites_cart=trial_sites_cart)
      min_nonbonded_distance = flex.min_default(
        pair_proxies.nonbonded_proxies.deltas(sites_cart=trial_sites_cart),
        None)
      print("i_trial, bond, nonbonded, min distance: %d, %.6g, %.6g, %s" % (
          i_trial,
          ftr.bond_residual_sum,
          ftr.nonbonded_residual_sum,
          format_value(format="%.4g", value=min_nonbonded_distance)), file=out)
      out.flush()
      if (minimized is None or       minimized[-1].final_target_result.target
                             > trial_minimized[-1].final_target_result.target):
        minimized = trial_minimized
        minimized_structure = trial_structure
        best_i_trial = i_trial
    assert minimized is not None
    for im,m in enumerate(minimized):
      print(file=out)
      print("Energies at start of %d. minimization:" % (im+1), file=out)
      m.first_target_result.show(f=out)
      print(file=out)
      print("Energies at end of %d. minimization:" % (im+1), file=out)
      m.final_target_result.show(f=out)
    print(file=out)
    print("Final target value (i_trial=%d): %.6g" % (
      best_i_trial, minimized[-1].final_target_result.target), file=out)
    if (minimized[-1].final_target_result.target > 0.1):
      print("WARNING: LARGE final target value: %.6g" % (
        minimized[-1].final_target_result.target), file=out)
    print(file=out)
    minimized_structure.pair_sym_table_show_distances(
      pair_sym_table=shell_sym_tables[0],
      out=out)
    print(file=out)
    sites_cart = minimized_structure.sites_cart()
    pair_proxies = geometry_restraints_manager.pair_proxies(
      sites_cart=sites_cart)
    pair_proxies.bond_proxies.show_sorted(
      by_value="residual",
      sites_cart=sites_cart,
      site_labels=[scatterer.label
        for scatterer in minimized_structure.scatterers()],
      f=out)
    print(file=out)
    pair_proxies.nonbonded_proxies.show_histogram_of_model_distances(
      sites_cart=sites_cart,
      f=out)
    print(file=out)
    out.flush()
    self.geometry_restraints_manager = geometry_restraints_manager
    self.start_structure = si_o.structure
    self.minimized_structure = minimized_structure

class dev_target_result(object):

  def __init__(O, target, n_bond_proxies, bond_residual_sum):
    O.target = target
    O.n_bond_proxies = n_bond_proxies
    O.bond_residual_sum = bond_residual_sum
    O.n_nonbonded_proxies = None
    O.nonbonded_residual_sum = 0

  def show(O, f=None, prefix=""):
    if (f is None): f = sys.stdout
    print(prefix+"target: %.6g" % O.target, file=f)
    if (O.n_bond_proxies is not None):
      print(prefix+"  bond_residual_sum (n=%d): %.6g" % (
        O.n_bond_proxies, O.bond_residual_sum), file=f)
    if (O.n_nonbonded_proxies is not None):
      print(prefix+"  nonbonded_residual_sum (n=%d): %.6g" % (
        O.n_nonbonded_proxies, O.nonbonded_residual_sum), file=f)

class dev_lbfgs(object):

  __slots__ = [
    "grm", "x", "proxies", "minimizer",
    "first_target_result",
    "final_target_result"]

  def __init__(O,
        sites_cart,
        geometry_restraints_manager,
        geometry_restraints_flags,
        lbfgs_termination_params,
        lbfgs_exception_handling_params):
    O.grm = geometry_restraints_manager
    cs = O.grm.crystal_symmetry
    sst = O.grm.site_symmetry_table
    sites_frac = cs.unit_cell().fractionalize(sites_cart)
    O.x = sst.pack_coordinates(sites_frac=sites_frac)
    O.proxies = geometry_restraints.shared_bond_simple_proxy()
    for i_seq,pair_sym_dict in enumerate(O.grm.shell_sym_tables[0]):
      for j_seq,sym_ops in pair_sym_dict.items():
        assert i_seq <= j_seq
        bond_params = O.grm.bond_params_table[i_seq][j_seq]
        for rt_mx_ji in sym_ops:
          O.proxies.append(geometry_restraints.bond_simple_proxy(
            i_seqs=[i_seq, j_seq],
            rt_mx_ji=rt_mx_ji,
            params=bond_params))
    def get_target_result():
      f, _ = O.compute_functional_and_gradients()
      return dev_target_result(
        target=f,
        n_bond_proxies=O.proxies.size(),
        bond_residual_sum=f)
    O.first_target_result = get_target_result()
    import scitbx.lbfgs
    O.minimizer = scitbx.lbfgs.run(
      target_evaluator=O,
      termination_params=lbfgs_termination_params,
      exception_handling_params=lbfgs_exception_handling_params)
    f, _ = O.compute_functional_and_gradients()
    O.final_target_result = get_target_result()

  def compute_functional_and_gradients(O):
    cs = O.grm.crystal_symmetry
    sst = O.grm.site_symmetry_table
    sites_frac = sst.unpack_coordinates(packed_coordinates=O.x)
    sites_cart = cs.unit_cell().orthogonalize(sites_frac)
    g_cart = flex.vec3_double(sites_cart.size(), (0,0,0))
    f = geometry_restraints.bond_residual_sum(
      unit_cell=cs.unit_cell(),
      sites_cart=sites_cart,
      proxies=O.proxies,
      gradient_array=g_cart)
    g_frac = g_cart * cs.unit_cell().orthogonalization_matrix()
    g = sst.pack_gradients(g_frac=g_frac)
    return f, g