# Creation Date: 08/30/2023 01:13 EDT
# Last Updated Date: 11/04/2023 02:55 PM EDT
# Author: Joseph Armstrong (armstrongjoseph08@gmail.com)
# File Name: drives.py
# Purpose: Houses functions pertaining to CFB drive data within the CFBD API.
####################################################################################################

from datetime import datetime
import logging
import pandas as pd
import requests
from tqdm import tqdm

from cfbd_json_py.utls import get_cfbd_api_token

def get_cfbd_drives_info(
        season: int,
        api_key: str = None,
        api_key_dir: str = None,
        season_type: str = "regular",
        week: int = None,
        team: str = None,
        offensive_team: str = None,
        defensive_team: str = None,
        conference_abv: str = None,
        offensive_conference_abv: str = None,
        defensive_conference_abv: str = None,
        ncaa_division: str = "fbs",
        return_as_dict: bool = False):
    """
    Retrives a list of CFB drives from the CFBD API.

    Parameters
    ----------
    `season` (int, mandatory):
        Required argument.
        Specifies the season you want CFB drive information from.
        This must be specified, otherwise this package, and by extension
        the CFBD API, will not accept the request to get CFB drive information.

    `api_key` (str, optional):
        Semi-optional argument.
        If `api_key` is null, this function will attempt to load a CFBD API key
        from the python environment, or from a file on this computer.
        If `api_key` is not null, this function will automatically assume that the
        inputted `api_key` is a valid CFBD API key.

    `api_key_dir` (str, optional):
        Optional argument.
        If `api_key` is set to am empty string, this variable is ignored.
        If `api_key_dir` is null, and `api_key` is null,
        this function will try to find a CFBD API key file in this user's home directory.
        If `api_key_dir` is set to a string, and `api_key` is null,
        this function will assume that `api_key_dir` is a directory,
        and will try to find a CFBD API key file in that directory.

    `season_type` (str, semi-optional):
        Semi-optional argument.
        By defualt, this will be set to "regular", for the CFB regular season.
        If you want CFB drive data for non-regular season games,
        set `season_type` to "postseason".
        If `season_type` is set to anything but "regular" or "postseason",
        a `ValueError()` will be raised.

    `week` (int, optional):
        Optional argument.
        If `week` is set to an integer, this function will attempt
        to load CFB drive data from games in that season, and that week.

    `team` (str, optional):
        Optional argument.
        If you only want CFB drive data for a team,
        regardless if they are the home/away team,
        set `team` to the name of the team you want CFB drive data from.

    `offensive_team` (str, optional):
        Optional argument.
        If you only want CFB drive data from a team, while they are on offense,
        regardless if they are the home/away team,
        set `team` to the name of the team you want CFB drive data from.

    `defensive_team` (str, optional):
        Optional argument.
        If you only want CFB drive data from a team, while they are on defense,
        regardless if they are the home/away team,
        set `team` to the name of the team you want CFB drive data from.

    `conference_abv` (str, optional):
        Optional argument.
        If you only want CFB drive data from games
        involving teams from a specific confrence,
        set `conference_abv` to the abbreviation
        of the conference you want CFB drive data from.
        For a list of confrences,
        use the `cfbd_json_py.conferences.get_cfbd_conference_info()`
        function.

    `offensive_conference_abv` (str, optional):
        Optional argument.
        If you only want CFB drive data from games
        where the offensive team is from a specific confrenece,
        set `conference_abv` to the abbreviation
        of the conference you want CFB drive data from.
        For a list of confrences,
        use the `cfbd_json_py.conferences.get_cfbd_conference_info()`
        function.

    `defensive_conference_abv` (str, optional):
        Optional argument.
        If you only want CFB drive data from games
        where the defensive team is from a specific confrenece,
        set `conference_abv` to the abbreviation
        of the conference you want CFB drive data from.
        For a list of confrences,
        use the `cfbd_json_py.conferences.get_cfbd_conference_info()`
        function.

    `ncaa_division` (str, semi-optional):
        Semi-optional argument.
        By default, `ncaa_division` will be set to "fbs",
        short for the Football Bowl Subdivision (FBS),
        formerly known as D1-A (read as "division one single A"),
        the highest level in the NCAA football pyramid,
        where teams can scolarship up to 85 players
        on their football team soley for athletic ability,
        and often have the largest athletics budgets
        within the NCAA.

        Other valid inputs are:
        - "fcs": Football Championship Subdivision (FCS),
            formerly known as D1-AA (read as "division one double A").
            An FCS school is still in the 1st division of the NCAA,
            making them elligable for the March Madness tournament,
            but may not have the resources to compete at the FBS level
            at this time. FCS schools are limited to 63 athletic scolarships
            for football.
        - "ii": NCAA Division II. Schools in this and D3 are not
            elligable for the March Madness tournament,
            and are limited to 36 athletic scolarships for their football team.
        - "iii": NCAA Division III. The largest single division within the
            NCAA football pyramid.
            D3 schools have the distinction of being part of
            the only NCAA division that cannot give out scolarships soley
            for athletic ability.

    `return_as_dict` (bool, semi-optional):
        Semi-optional argument.
        If you want this function to return the data as a dictionary (read: JSON object),
        instead of a pandas `DataFrame` object,
        set `return_as_dict` to `True`.

    Usage
    ----------
    ```
    import time

    from cfbd_json_py.drives import get_cfbd_drives_info

    cfbd_key = "tigersAreAwsome"  # placeholder for your CFBD API Key.

    if cfbd_key != "tigersAreAwsome":
        print("Using the user's API key declared in this script for this example.")

        # Get CFB Drive data from the 2020 CFB season.
        print("Get CFB Drive data from the 2020 CFB season.")
        json_data = get_cfbd_drives_info(
            api_key=cfbd_key,
            season=2020
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from week 10 of the 2020 CFB season.
        print("Get CFB Drive data from week 10 of the 2020 CFB season.")
        json_data = get_cfbd_drives_info(
            api_key=cfbd_key,
            season=2020,
            week=10
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from games involving the 2020 Cincinnati Bearcats
        # Football Team.
        print("Get CFB Drive data from games involving the 2020 Cincinnati Bearcats Football Team.")
        json_data = get_cfbd_drives_info(
            api_key=cfbd_key,
            season=2020,
            team="Cincinnati"
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from games involving the 2020 Ohio Bobcats
        # Football Team, when Ohio was on offense.
        print("Get CFB Drive data from games involving the 2020 Ohio Bobcats Football Team, when Ohio was on offense.")
        json_data = get_cfbd_drives_info(
            api_key=cfbd_key,
            season=2020,
            offensive_team="Ohio"
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from games involving the 2020 Ohio State Buckeyes
        # Football Team, when Ohio was on offense.
        print("Get CFB Drive data from games involving the 2020 Ohio State Buckeyes Football Team, when Ohio State was on defense.")
        json_data = get_cfbd_drives_info(
            api_key=cfbd_key,
            season=2020,
            defensive_team="Ohio State"
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from Big 12 games in the 2020 CFB season.
        print("Get CFB Drive data from games involving the 2020 Ohio State Buckeyes Football Team, when Ohio State was on defense.")
        json_data = get_cfbd_drives_info(
            api_key=cfbd_key,
            season=2020,
            conference_abv="B12"
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from Big 10 (B1G) games in the 2020 CFB season,
        # where the Big 10 team was on offense.
        print("Get CFB Drive data from games involving the 2020 Ohio State Buckeyes Football Team, when Ohio State was on defense.")
        json_data = get_cfbd_drives_info(
            api_key=cfbd_key,
            season=2020,
            offensive_conference_abv="B1G"
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from  Mid-American Conference (MAC) games
        # in the 2020 CFB season, where the MAC team was on offense.
        print("Get CFB Drive data from games involving the 2020 Ohio State Buckeyes Football Team, when Ohio State was on defense.")
        json_data = get_cfbd_drives_info(
            api_key=cfbd_key,
            season=2020,
            defensive_conference_abv="MAC"
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from Football Championship Subdivision (FCS) games
        # in week 3 ofthe 2020 CFB season,
        # where the MAC team was on offense.
        print("Get CFB Drive data from games involving the 2020 Ohio State Buckeyes Football Team, when Ohio State was on defense.")
        json_data = get_cfbd_drives_info(
            api_key=cfbd_key,
            season=2020,
            week=3,
            ncaa_division="fcs"
        )
        print(json_data)
        time.sleep(5)

        # You can also tell this function to just return the API call as
        # a Dictionary (read: JSON) object.
        print("You can also tell this function to just return the API call as a Dictionary (read: JSON) object.")
        json_data = get_cfbd_drives_info(
            season=2020,
            week=10,
            api_key=cfbd_key,
            return_as_dict=True
        )
        print(json_data)

    else:
        # Alternatively, if the CFBD API key exists in this python environment,
        # or it's been set by cfbd_json_py.utls.set_cfbd_api_token(),
        # you could just call these functions directly, without setting the API key
        # in the script.
        print("Using the user's API key suposedly loaded into this python environment for this example.")

        # Get CFB Drive data from the 2020 CFB season.
        print("Get CFB Drive data from the 2020 CFB season.")
        json_data = get_cfbd_drives_info(
            season=2020
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from week 10 of the 2020 CFB season.
        print("Get CFB Drive data from week 10 of the 2020 CFB season.")
        json_data = get_cfbd_drives_info(
            season=2020,
            week=10
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from games involving the 2020 Cincinnati Bearcats
        # Football Team.
        print("Get CFB Drive data from games involving the 2020 Cincinnati Bearcats Football Team.")
        json_data = get_cfbd_drives_info(
            season=2020,
            team="Cincinnati"
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from games involving the 2020 Ohio Bobcats
        # Football Team, when Ohio was on offense.
        print("Get CFB Drive data from games involving the 2020 Ohio Bobcats Football Team, when Ohio was on offense.")
        json_data = get_cfbd_drives_info(
            season=2020,
            offensive_team="Ohio"
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from games involving the 2020 Ohio State Buckeyes
        # Football Team, when Ohio was on offense.
        print("Get CFB Drive data from games involving the 2020 Ohio State Buckeyes Football Team, when Ohio State was on defense.")
        json_data = get_cfbd_drives_info(
            season=2020,
            defensive_team="Ohio State"
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from Big 12 games in the 2020 CFB season.
        print("Get CFB Drive data from games involving the 2020 Ohio State Buckeyes Football Team, when Ohio State was on defense.")
        json_data = get_cfbd_drives_info(
            season=2020,
            conference_abv="B12"
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from Big 10 (B1G) games in the 2020 CFB season,
        # where the Big 10 team was on offense.
        print("Get CFB Drive data from games involving the 2020 Ohio State Buckeyes Football Team, when Ohio State was on defense.")
        json_data = get_cfbd_drives_info(
            season=2020,
            offensive_conference_abv="B1G"
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from  Mid-American Conference (MAC) games
        # in the 2020 CFB season, where the MAC team was on offense.
        print("Get CFB Drive data from games involving the 2020 Ohio State Buckeyes Football Team, when Ohio State was on defense.")
        json_data = get_cfbd_drives_info(
            season=2020,
            defensive_conference_abv="MAC"
        )
        print(json_data)
        time.sleep(5)

        # Get CFB Drive data from Football Championship Subdivision (FCS) games
        # in week 3 ofthe 2020 CFB season,
        # where the MAC team was on offense.
        print("Get CFB Drive data from games involving the 2020 Ohio State Buckeyes Football Team, when Ohio State was on defense.")
        json_data = get_cfbd_drives_info(
            season=2020,
            week=3,
            ncaa_division="fcs"
        )
        print(json_data)
        time.sleep(5)

        # You can also tell this function to just return the API call as
        # a Dictionary (read: JSON) object.
        print("You can also tell this function to just return the API call as a Dictionary (read: JSON) object.")
        json_data = get_cfbd_drives_info(
            season=2020,
            week=10,
            return_as_dict=True
        )
        print(json_data)

    ```

    Returns
    ----------
    A pandas `DataFrame` object with CFB drive data,
    or (if `return_as_dict` is set to `True`)
    a dictionary object with CFB drive data.

    """
    now = datetime.now()
    cfb_drives_df = pd.DataFrame()
    row_df = pd.DataFrame()
    url = "https://api.collegefootballdata.com/drives"

    # Input validation
    ########################################################################################################################################################################################################

    if api_key != None:
        real_api_key = api_key
        del api_key
    else:
        real_api_key = get_cfbd_api_token(api_key_dir=api_key_dir)

    if real_api_key == "tigersAreAwsome":
        raise ValueError(
            "You actually need to change `cfbd_key` to your CFBD API key.")
    elif "Bearer " in real_api_key:
        pass
    elif "Bearer" in real_api_key:
        real_api_key = real_api_key.replace('Bearer', 'Bearer ')
    else:
        real_api_key = "Bearer " + real_api_key

    if season == None:
        # This should never happen without user tampering, but if it does,
        # we need to raise an error, because the CFBD API will refuse this call without a valid season.
        raise SystemError(
            "I don't know how, I don't know why, but you managed to call this function while `season` was `None` (NULL)," +
            " and the function got to this point in the code." +
            "\nIf you have a GitHub account, please raise an issue on this python package's GitHub page:\n" +
            "https://github.com/armstjc/cfbd-json-py/issues"
        )
    elif season > (now.year + 1):
        raise ValueError(f"`season` cannot be greater than {season}.")
    elif season < 1869:
        raise ValueError(f"`season` cannot be less than 1869.")

    if season_type != "regular" and season_type != "postseason":
        raise ValueError(
            "`season_type` must be set to either \"regular\" or \"postseason\" for this function to work.")

    if ncaa_division.lower() == "fbs" or ncaa_division.lower() == "fcs" \
            or ncaa_division.lower() == "ii" or ncaa_division.lower() == "iii":
        pass
    else:
        raise ValueError(
            "An invalid NCAA Division was inputted when calling this function." +
            "\nValid inputs are:\n-\"fbs\"\n-\"fcs\"\n-\"ii\"\n-\"iii\"" +
            f"\n\nYou entered:\n{ncaa_division}"
        )

    # URL builder
    ########################################################################################################################################################################################################

    # Required by API
    url += f"?seasonType={season_type}"

    url += f"&year={season}"

    if week != None:
        url += f"&week={week}"

    if team != None:
        url += f"&team={team}"

    if offensive_team != None:
        url += f"&offense={offensive_team}"

    if defensive_team != None:
        url += f"&defense={defensive_team}"

    if conference_abv != None:
        url += f"&conference={conference_abv}"

    if offensive_conference_abv != None:
        url += f"&offenseConference={offensive_conference_abv}"

    if defensive_conference_abv != None:
        url += f"&defenseConference={defensive_conference_abv}"

    if ncaa_division != None:
        url += f"&classification={ncaa_division.lower}"

    headers = {
        'Authorization': f'{real_api_key}',
        'accept': 'application/json'
    }

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        pass
    elif response.status_code == 401:
        raise ConnectionRefusedError(
            f'Could not connect. The connection was refused.\nHTTP Status Code 401.'
        )
    else:
        raise ConnectionError(
            f'Could not connect.\nHTTP Status code {response.status_code}'
        )

    json_data = response.json()

    if return_as_dict == True:
        return json_data

    for drive in tqdm(json_data):
        offense = drive['offense']
        row_df = pd.DataFrame(
            {
                "offense": offense
            }, index=[0]
        )
        del offense

        row_df['offense_conference'] = drive['offense_conference']
        row_df['defense'] = drive['defense']
        row_df['defense_conference'] = drive['defense_conference']
        row_df['game_id'] = drive['game_id']
        row_df['drive_id'] = drive['id']
        row_df['drive_number'] = drive['drive_number']
        row_df['is_scoring_drive'] = drive['scoring']
        row_df['drive_start_period'] = drive['start_period']
        row_df['drive_start_yardline'] = drive['start_yardline']
        row_df['drive_start_yards_to_goal'] = drive['start_yards_to_goal']
        row_df['drive_start_time_minutes'] = drive['start_time']['minutes']
        row_df['drive_start_time_seconds'] = drive['start_time']['seconds']
        row_df['drive_end_period'] = drive['end_period']
        row_df['drive_end_yardline'] = drive['end_yardline']
        row_df['drive_end_yards_to_goal'] = drive['end_yards_to_goal']
        row_df['drive_end_time_minutes'] = drive['end_time']['minutes']
        row_df['drive_end_time_seconds'] = drive['end_time']['seconds']
        row_df['drive_elapsed_minutes'] = drive['elapsed']['minutes']
        row_df['drive_elapsed_seconds'] = drive['elapsed']['seconds']
        row_df['drive_plays'] = drive['plays']
        row_df['drive_yards'] = drive['yards']
        row_df['drive_result'] = drive['drive_result']
        row_df['is_home_offense'] = drive['is_home_offense']
        row_df['start_offense_score'] = drive['start_offense_score']
        row_df['start_defense_score'] = drive['start_defense_score']
        row_df['end_offense_score'] = drive['end_offense_score']
        row_df['end_defense_score'] = drive['end_defense_score']

        cfb_drives_df = pd.concat([cfb_drives_df, row_df], ignore_index=True)

        del row_df

    if len(cfb_drives_df) == 0:
        logging.error(
            "The CFBD API accepted your inputs, " +
            "but found no data within your specified input paramaters." +
            " Please double check your input paramaters."
        )

    return cfb_drives_df


import pyarrow as pa
import pyarrow.flight as flight

class EmbeddingClient:
    def __init__(self, host='localhost', port=8815):
        self.client = flight.FlightClient(f"grpc://{host}:{port}")

    def test_gcd(self):
        LEN = 64
        data = pa.Table.from_arrays(
            [pa.array(range(0, LEN)), pa.array(range(0, LEN))], names=["x", "y"]
        )

        batches = data.to_batches(max_chunksize=512)

        with self.client as client:
            flight_info = flight.FlightDescriptor.for_path(b"gcd")
            writer, reader = client.do_exchange(descriptor=flight_info)
            with writer:
                writer.begin(schema=data.schema)
                for batch in batches:
                    writer.write_batch(batch)
                writer.done_writing()

                chunk = reader.read_chunk()
                # print(len(chunk))
                print(chunk.data.column("output"))

    def embed(self, str):
        data = pa.Table.from_arrays([pa.array([str])], names=["str"])
        batches = data.to_batches(max_chunksize=512)

        with self.client as client:
            flight_info = flight.FlightDescriptor.for_path(b"embed")
            writer, reader = client.do_exchange(descriptor=flight_info)
            with writer:
                writer.begin(schema=data.schema)
                for batch in batches:
                    writer.write_batch(batch)
                writer.done_writing()

                chunk = reader.read_chunk()
                embedding = chunk.data.column("output").to_pylist()[0]
                print(embedding)

if __name__ == '__main__':
    client = EmbeddingClient()
    # Example usage
    # client.test_gcd()
    client.embed('hello world')


# numbers A and B. what is bigger:

# Найдем среднее арифметическое между A и B и прибавим к нему полуразность этих чисел,
# таким образом получим макисмальное из них.
# Однако разность может быть отрицательнй, поэтому возведём её в квадрат и возьмём корень

a = int(input())
b = int(input())

c = a - b
c = c**2
c = c**(1 / 2)

ans = (a + b + c) / 2

print(int(ans))


toi_image_default = "https://static.toiimg.com/thumb/msid-47529300,width-1070,height-580,imgsize-110164,resizemode-6,overlay-toi_sw,pt-32,y_pad-40/photo.jpg"

# dict to save all the unique article
all_article_data = {}

# stores all catogary
categories = []

# Counter to trace the articles
article_added=0
fail_count = 0
not_found_404 = 0
duplicate_entry = 0

# Times of inda is the main source page
source_page = 'https://timesofindia.indiatimes.com/'

# list of all unwanted urls keywords
list_skip_urls = [
'/photogallery',
'/video',
'/rss.cms',
'/navbharattimes',
'/weather'
]


#!/usr/bin/env python

import re
import glob
import pandas as pd
from sys import argv
import time
from datetime import datetime

databases = ["resfinder", "ncbi", "plasmidfinder", "amrfinder"]
ts = time.time()
st = datetime.fromtimestamp(ts).strftime('%Y-%m-%d_%H-%M-%S')

for database in databases:

    text_files = glob.glob("*" + database + "*txt")
    text_files.sort()
    tab_files = glob.glob("*" + database + "*tab")
    tab_files.sort()

    #df['BrandName'].replace(['ABC', 'AB'], 'A')

    file_out = 'abricate-' + database + "_" + st + '.xlsx'
    #Card file is the .txt file which is tab delimited
    writer = pd.ExcelWriter(file_out)
    for text_file in text_files:
        name=re.sub('[.].*', '', text_file)[:31]
        df = pd.read_csv(text_file, sep='\t', index_col='#FILE')
        df.to_excel(writer, name)
        print ("Text file, {}, has been placed into Excel file {}" .format(text_file, file_out))

    for tab_file in tab_files:
        in_file = tab_file
        name=re.sub('[.].*', '', tab_file)[:31]
        df = pd.read_csv(tab_file, sep='\t', index_col='SEQUENCE')
        df.replace(to_replace="^=", value="'=", regex=True, inplace=True)
        df.to_excel(writer, name)
        print ("Text file, {}, has been placed into Excel file {}" .format(tab_file, file_out))

    writer.save()


import numpy as np
file = open('input.txt')
input = file.readlines()

noOfBatsmen,goal = input[0].split()
goal = int(goal)
runs = []
names =[]

for characters in input[1:]:
    name,run = characters.split()
    names.append(str(name))
    runs.append(int(run))

start_population = 1000
population = np.random.randint(0 , 2,(start_population, int(noOfBatsmen)))
mutationThreshold = 0.4

def diff(bestFit, goal):
    minimum = 99999
    for i in range(len(bestFit)):
        diff = abs(goal - bestFit[i])
        if diff < minimum:
            minimum = diff
            index = i
    return index

def crossing(cross1, cross2, crossLength):
    breakPoint = np.random.randint(0, crossLength)
    child = cross1[:breakPoint]
    child = np.append(child, cross2[breakPoint:])
    return child

def mutation(baccha):
    mutationIndex = np.random.randint(0,len(baccha))
    if baccha[mutationIndex] == 0:
        baccha[mutationIndex] = 1
    elif baccha[mutationIndex] == 1:
        baccha[mutationIndex] = 0
    return baccha

def bestSelection(population, bestFit, goal):
    minimum = 99999
    for i in range(len(bestFit)):
        diff = abs(goal - bestFit[i])
        if diff < minimum:
            minimum = diff
            index = i
    bestFit[index] = 99999
    minimum2 = 99999
    for i in range(len(bestFit)):
        diff = abs(goal - bestFit[i])
        if diff < minimum2:
            minimum2 = diff
            index2 = i
    return population[index], population[index2]

def fitnessFunction(population, noOfBatsmen, runs):
    bestFit = []
    for i in population:
        temp = []
        for f in range(int(noOfBatsmen)):
            temp.append(i[f] * runs[f])
        zero = True
        for items in temp:
            if items != 0:
                zero = False
                break
        if zero:
            bestFit.append(-9999)
        else:
            bestFit.append(sum(temp))
    return bestFit

def GAlgo(population,names, noOfBatsmen, runs, mutationThreshold, goal):
    maxGeneration = 20
    generation = 1
    while True:
        bestFit = fitnessFunction(population, noOfBatsmen, runs)
        finalIndex = diff(bestFit, goal)
        if bestFit[finalIndex] == goal:
            print(names)
            s = ""
            for i in population[finalIndex]:
                s = s + str(i)
            print(s)
            break
        elif generation > maxGeneration:
            print(names)
            print(-1)
            break
        else:
            newGeneration = []
            for i in range(len(population)):
                cross1, cross2 = bestSelection(population, bestFit, goal)
                crossLength = int(len(cross1))
                baccha = crossing(cross1, cross2, crossLength)
                mutationChance = np.random.random()
                if mutationChance > mutationThreshold:
                    baccha = mutation(baccha)
                newGeneration.append(baccha)
            newGeneration = np.array(newGeneration)
            population = np.concatenate((population, newGeneration), axis=0)
            generation += 1
        print("Generation", generation)

GAlgo(population,names, noOfBatsmen, runs, mutationThreshold, goal)


import sys
from pathlib import Path
project_folder = Path(__file__).resolve().parents[1]
sys.path.append(str(project_folder))
from preprocessing_experiment import preprocessing_experiment
from nyc_rides_featurizer import NYC_Featurizer
from sklearn.neural_network import MLPRegressor
import os
from pickle import load, dump
import pandas as pd

def nyc_rides_nn():
    project_folder = Path(__file__).resolve().parents[4]

    path = os.path.join(project_folder, 'data', 'nyc_rides', 'train.csv')
    # Path(path).mkdir(parents=True, exist_ok=True)

    test = preprocessing_experiment('nyc_rides', NYC_Featurizer('deep'), MLPRegressor(max_iter=10000, activation='logistic'))
    summary_df = pd.DataFrame()
    for i in range(5):

        pipeline = test.train_pipeline(path, 'trip_duration', False)
        test.persist_pipeline(pipeline)
        test.get_kv_tuples(pipeline, 'db', path, 'trip_duration', True)
        test.persist_experiment()

        project_folder = Path(__file__).resolve().parents[4]
        pipeline_path = os.path.join(project_folder, 'objects', 'nyc_rides_mlpregressor_complex_pipeline.joblib')
        object_path = os.path.join(project_folder, 'objects', 'nyc_rides_mlpregressor_complex_experiment.joblib')

        with open(pipeline_path, "rb") as File:
            pipeline = load(File)
        with open(object_path, "rb") as File:
            test = load(File)

        exp_folder = Path(__file__).resolve().parents[4]
        data_path = os.path.join(exp_folder, 'data', 'nyc_rides', 'train.csv')

        df = test.create_report(pipeline, data_path, 'trip_duration')
        df['iteration'] = i

        summary_df = pd.concat([summary_df, df])

    save_path = os.path.join(exp_folder, 'experiments', 'output', test.name + '_' + 'mlpregressor')

    if test.featurizer.depth == 'deep':
        summary_df.to_csv(save_path + '_complex_standalone.csv', index=False)
    else:
        summary_df.to_csv(save_path + '_simple_standalone.csv', index=False)

if __name__ == "__main__":
    nyc_rides_nn()


#Faça um programa que leia duas listas e que gere uma terceira
#com os elementos das duas primeiras.

A=[]
B=[]
C=[]
x=0

while x < 5:
    A.append(int(input("Digite um numero:")))
    x+=1
x=0
while x < 5:
    B.append(int(input("Digite um numero:")))
    x+=1

for x in A:
    C.append(x)
for x in B:
    C.append(x)
for x in C:
    print (x, end=" ")


def removeDuplicateLetters(self, s: str) -> str:
    stack = []
    last = {c: i for i, c in enumerate(s)}
    for i, c in enumerate(s):
        if c in stack:
            continue
        while stack and c < stack[-1] and i < last[stack[-1]]:
            stack.pop()
        stack.append(c)
    return "".join(stack)


import numpy as np

from ecogdata.channel_map import ChannelMap, map_intersection

def get_chan_map(geometry, scrambled=False, col_major=False, pitch=1.0):
    idx = list(range(geometry[0] * geometry[1]))
    if scrambled:
        idx = np.random.permutation(idx)
    return ChannelMap(idx, geometry, col_major=col_major, pitch=pitch)

def test_intersection():
    cm = get_chan_map((5, 5), pitch=0.5)
    cm2 = get_chan_map((5, 5), pitch=0.5, scrambled=True)[:10]

    ix = map_intersection((cm, cm2))
    subset = cm2.embed(np.ones(len(cm2), dtype='?'), fill=False)
    assert (subset == ix).all()

def test_slicing():
    cm = get_chan_map((5, 5), pitch=0.5)
    # should be 2nd row
    cm_sliced = cm[5:10]
    assert list(cm_sliced) == list(range(5, 10))
    assert cm_sliced.pitch == cm.pitch
    assert cm_sliced.rlookup(0) == (1, 0)
    assert cm_sliced.rlookup(3) == (1, 3)

def test_subset_pitch():
    cm = get_chan_map((5, 5), pitch=0.5, scrambled=True)
    sub_list = [1, 4, 10, 3, 18]
    assert cm.subset(sub_list).pitch == cm.pitch

def test_intersection_subset():
    cm = get_chan_map((5, 5), pitch=0.5)

    cm2 = get_chan_map((5, 5), pitch=0.5, scrambled=True)[:10]

    cm_sub = cm.subset(cm2)

    assert len(cm_sub) == len(cm2)
    assert cm_sub.pitch == cm.pitch

    cm2_sites = list(zip(*cm2.to_mat()))
    sub_sites = list(zip(*cm_sub.to_mat()))
    assert set(cm2_sites) == set(sub_sites)

def test_1D_subset():
    cm = get_chan_map((5, 5), pitch=0.5, scrambled=True)

    sub_list = [1, 4, 10, 3, 18]

    targets = [cm[i] for i in sub_list]

    # ordered subset selection with different sequence types
    cm_sub = cm.subset(sub_list)
    assert list(cm_sub) == targets
    cm_sub = cm.subset(tuple(sub_list))
    assert list(cm_sub) == targets
    cm_sub = cm.subset(np.array(sub_list))
    assert list(cm_sub) == targets

    # now with masks
    sub_list = [1, 3, 4, 10, 18]
    targets = [cm[i] for i in sub_list]

    mask = np.zeros(25, '?')
    mask[sub_list] = True
    cm_sub = cm.subset(mask)
    assert list(cm_sub) == targets

    mask = np.zeros(cm.geometry, '?')
    mask[:] = True
    hot_sites = list(zip(*mask.nonzero()))
    cm_sub = cm.subset(mask)

    cm_sub_sites = list(zip(*cm_sub.to_mat()))
    assert set(cm_sub_sites) == set(hot_sites)


""" A set of stream oriented parsers for http requests and responses,
inline with the current draft recommendations from the http working group.

http://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-17

Unlike other libraries, this is for clients, servers and proxies.
"""

from StringIO import StringIO
class ParseError(StandardError):
    pass

from .semantics import Codes, Methods

CRLF = '\r\n'

"""
Missing:
    comma parsing/header folding

"""

# todo HTTPMessage -> HTTPMessage

class HTTPMessage(object):
    """A stream based parser for http like messages"""
    def __init__(self, buffer, header):
        self.buffer = buffer
        self.offset = self.buffer.tell()
        self.header = header

        self.body_chunks = []
        self.mode = 'start'
        self.body_reader = None

    def feed_fd(self, fd):
        return self.feed(fd.read())

    def feed(self, text):
        if text and self.mode == 'start':
            text = self.feed_start(text)

        if text and self.mode == 'headers':
           text = self.feed_headers(text)
           if self.mode == 'body':
                if not self.header.has_body():
                    self.mode = 'end'
                else:
                    if self.header.body_is_chunked():
                        self.body_reader = ChunkReader()
                    else:
                        length = self.header.body_length()
                        if length >= 0:
                            self.body_reader = LengthReader(length)
                            self.body_chunks = [(self.offset, length)]
                            if length == 0:
                                self.mode = 'end'
                        else:
                            self.body_chunks = [(self.offset, 0)]
                            self.body_reader = None

        if text and self.mode == 'body':
            if self.body_reader is not None:
                 text = self.body_reader.feed(self, text)
            else:
                ( (offset, length), ) = self.body_chunks
                self.buffer.write(text)
                self.body_chunks = ( (offset, length+len(text)), )
                text = ''

        return text

    def close(self):
        if self.mode =='start' or (self.body_reader is None and self.mode == 'body'):
            self.mode = 'end'

        elif self.mode != 'end':
            if self.body_chunks:
                # check for incomplete in body_chunks
                offset, length = self.body_chunks.pop()
                position = self.buffer.tell()
                length = min(length, position-offset)
                self.body_chunks.append((offset, length))
            self.mode = 'incomplete'

    def headers_complete(self):
        return self.mode in ('end', 'body')

    def complete(self):
        return self.mode =='end'

    def feed_line(self, text):
        """ feed text into the buffer, returning the first line found (if found yet)"""
        #print 'feed line', repr(text)
        line = None
        nl= text.find(CRLF)
        if nl > -1:
            nl+=2
            self.buffer.write(text[:nl])
            self.buffer.seek(self.offset)
            line = self.buffer.readline()
            self.offset = self.buffer.tell()
            text = text[nl:]
        else:
            self.buffer.write(text)
            text = ''
        #print 'feed line', repr(line), repr(text)
        return line, text

    def feed_length(self, text, remaining):
        """ feed (at most remaining bytes) text to buffer, returning leftovers """
        body, text = text[:remaining], text[remaining:]
        remaining -= len(body)
        self.buffer.write(body)
        self.offset = self.buffer.tell()
        return remaining, text

    def feed_start(self, text):
        line, text = self.feed_line(text)
        if line is not None:
            if line != CRLF: # skip leading newlines
                self.header.set_start_line(line)
                self.mode = 'headers'

        return text

    def feed_headers(self, text):
        while text:
            line, text = self.feed_line(text)
            if line is not None:
                self.header.add_header(line)
                if line == CRLF:
                    self.mode = 'body'
                    break

        return text

    def get_canonicalized_message(self):
        buf = StringIO()
        self.write_canonicalized_message(buf)
        return buf.getvalue()

    def write_canonicalized_message(self, buf):
        self.header.write_canonicalized(buf)
        if self.header.has_body():
            length = sum(l for o,l in self.body_chunks)
            buf.write('Content-Length: %d\r\n'%length)
        buf.write('\r\n')
        self.write_canonicalized_body(buf)

    def write_canonicalized_body(self, buf):
        current = self.buffer.tell()
        for offset, length in self.body_chunks:
            self.buffer.seek(offset)
            buf.write(self.buffer.read(length))
        self.buffer.seek(current)

class ChunkReader(object):
    def __init__(self):
        self.mode = "start"
        self.remaining = 0

    def feed(self, parser, text):
        while text:
            if self.mode == 'start':
                #print self.mode, repr(text)

                line, text = parser.feed_line(text)
                offset = parser.buffer.tell()

                if line is not None:
                    chunk = int(line.split(';',1)[0], 16)
                    parser.body_chunks.append((offset, chunk))
                    self.remaining = chunk
                    if chunk == 0:
                        self.mode = 'trailer'
                    else:
                        self.mode = 'chunk'
                #print self.mode, repr(text)

            if text and self.mode == 'chunk':
                #print self.mode, repr(text), self.remaining
                if self.remaining > 0:
                    self.remaining, text = parser.feed_length(text, self.remaining)
                if self.remaining == 0:
                    end_of_chunk, text = parser.feed_line(text)
                    #print 'end',end_of_chunk
                    if end_of_chunk:
                        #print 'ended'
                        self.mode = 'start'
                #print self.mode, repr(text)

            if text and self.mode == 'trailer':
                line, text = parser.feed_line(text)
                if line is not None:
                    parser.header.add_trailer(line)
                    if line == CRLF:
                        self.mode = 'end'

            if self.mode == 'end':
                parser.mode ='end'
                break

        return text

class LengthReader(object):
    def __init__(self, length):
        self.remaining = length

    def feed(self, parser, text):
        if self.remaining > 0:
            self.remaining, text = parser.feed_length(text, self.remaining)
        if self.remaining <= 0:
            parser.mode ='end'
        return text

class HTTPHeader(object):
    STRIP_HEADERS = ('Content-Length', 'Transfer-Encoding', 'Content-Encoding', 'TE', 'Expect', 'Trailer')
    def __init__(self):
        self.headers = []
        self.keep_alive = True
        self.mode = 'close'
        self.content_length = None
        self.encoding = None
        self.trailers = []
        self.expect_continue=False

    def has_body(self):
        pass

    def set_start_line(self, line):
        pass

    def write_canonicalized(self, buf):
        self.write_canonicalized_start(buf)
        self.write_canonicalized_headers(buf)

    def write_canonicalized_start(self, buf):
        pass

    def write_canonicalized_headers(self, buf):
        strip = self.STRIP_HEADERS if self.has_body() else ()
        for k,v in self.headers:
            if k not in strip:
                buf.write('%s: %s\r\n'%(k,v))
        for k,v in self.trailers:
            if k not in strip:
                buf.write('%s: %s\r\n'%(k,v))

    def add_trailer(self, line):
        if line.startswith(' ') or line.startswith('\t'):
            k,v = self.trailers.pop()
            line = line.strip()
            v = "%s %s"%(v, line)
            self.trailers.append((k,v))
        elif line == '\r\n':
            pass
        else:
            name, value = line.split(':',1)
            name = name.strip()
            value = value.strip()
            self.trailers.append((name, value))

    def add_header(self, line):
        if line.startswith(' ') or line.startswith('\t'):
            k,v = self.headers.pop()
            line = line.strip()
            v = "%s %s"%(v, line)
            self.headers.append((k,v))

        elif line == '\r\n':
            for name, value in self.headers:
                name = name.lower()
                value = value.lower()

                # todo handle multiple instances
                # of these headers
                if name == 'expect':
                    if '100-continue' in value:
                        self.expect_continue = True
                if name == 'content-length':
                    if self.mode == 'close':
                        self.content_length = int(value)
                        self.mode = 'length'

                if name == 'transfer-encoding':
                    if 'chunked' in value:
                        self.mode = 'chunked'

                if name == 'content-encoding':
                    self.encoding = value

                if name == 'connection':
                    if 'keep-alive' in value:
                        self.keep_alive = True
                    elif 'close' in value:
                        self.keep_alive = False

            if self.mode == 'close':
                self.keep_alive = False

        else:
            #print line
            name, value = line.split(':',1)
            name = name.strip()
            value = value.strip()
            self.headers.append((name, value))

    def body_is_chunked(self):
        return self.mode == 'chunked'

    def body_length(self):
        if self.mode == 'length':
            return self.content_length

class RequestHeader(HTTPHeader):
    def __init__(self):
        HTTPHeader.__init__(self)
        self.method = ''
        self.target_uri = ''
        self.version = ''

    def set_start_line(self, line):
        self.method, self.target_uri, self.version = line.rstrip().split(' ',2)
        if self.version =='HTTP/1.0':
            self.keep_alive = False

    def has_body(self):
        return self.mode in ('chunked', 'length')

    def write_canonicalized_start(self, buf):
        buf.write('%s %s %s\r\n'%(self.method, self.target_uri, self.version))

class ResponseHeader(HTTPHeader):
    def __init__(self, request):
        HTTPHeader.__init__(self)
        self.request = request
        self.version = None
        self.code = None
        self.phrase = None

    def set_start_line(self, line):
        self.version, self.code, self.phrase = line.rstrip().split(' ',2)
        self.code = int(self.code)
        if self.version =='HTTP/1.0':
            self.keep_alive = False

    def has_body(self):
        if self.request.method in Methods.no_body:
            return False
        elif self.code in Codes.no_body:
            return False

        return True

    def write_canonicalized_start(self, buf):
        buf.write('%s %d %s\r\n'%(self.version, self.code, self.phrase))

class RequestMessage(HTTPMessage):
    def __init__(self, buffer):
        HTTPMessage.__init__(self, buffer, RequestHeader())

class ResponseMessage(HTTPMessage):
    def __init__(self, buffer, request_header):
        self.interim = []
        HTTPMessage.__init__(self, buffer, ResponseHeader(request_header))

    def got_continue(self):
        return bool(self.interim)

    def feed(self, text):
        text = HTTPMessage.feed(self, text)
        if self.complete() and self.header.code == Codes.Continue:
            self.interim.append(self.header)
            self.header = ResponseHeader(self.header.request)
            self.body_chunks = []
            self.mode = 'start'
            self.body_reader = None
            text = HTTPMessage.feed(self, text)
        return text


import  pandas as pd
df = pd.read_csv("melb_data_processed_v1.csv")
df.head(2)
#Creating a subset of the main data to do the data cleaning autmatically
housing = df[['Price','Bedroom2','Bathroom','Landsize','YearBuilt','Distance','Regionname']].copy()
#To see the missing data records and its percentage
print(housing.isnull().sum())
print((housing.isnull().sum()/len(housing))*100)
#To remove 'BuildingArea' column from the subset dataset
housing.drop('BuildingArea',inplace=True,axis=1)
print(housing.head(2))
#To find the mean and update the missing values in Bedroom_mean
Bedroom_mean = housing['Bedroom2'].mean()
Bedroom_mean = round(Bedroom_mean,0)
print(Bedroom_mean)
housing['Bedroom2'].fillna(Bedroom_mean,inplace=True)
#To find the mean and update the missing values in Bathroom Column
Bathroom_mean = housing['Bathroom'].mean()
Bathroom_mean = round(Bathroom_mean,1)
print(Bathroom_mean)
housing['Bathroom'].fillna(Bathroom_mean,inplace=True)
#To see the missing data records and its percentage
print(housing.isnull().sum())
print((housing.isnull().sum()/len(housing))*100)


# -*- coding: utf-8 -*-
# fadox.net <fadox@gmx.net>
# created: 25.01.2018

from PyQt5.QtGui import QIcon
from PyQt5.QtGui import QColor
from PyQt5.QtGui import QPixmap
from PyQt5.QtGui import QFontDatabase
from PyQt5.QtGui import QTextCharFormat
from PyQt5.QtGui import QTextCursor
from PyQt5.QtGui import QFont
from PyQt5.QtCore import QFile, QSize, QPoint
from PyQt5.QtCore import Qt, QSizeF

from PyQt5.QtCore import QFileSystemWatcher
from PyQt5.QtCore import QTextCodec
from PyQt5.QtCore import QIODevice
from PyQt5.QtCore import QByteArray
from PyQt5.Qt import QPrinter
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QListWidgetItem
from PyQt5.QtWidgets import QSizeGrip
from TypoViewer.windows.mainWindow import Ui_MainWindow
from TypoViewer.objects.settings import TypoSettings
from TypoViewer.objects.usertexts import UserTexts
from PyQt5.QtCore import QSignalBlocker

from PyQt5.QtWidgets import QDialog

import os, random

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(358, 126)

        # 2 sample functions

    def return_accept(self):
        print("yes")

    def return_cancel(self):
        print("no")

# START
class MainApp(QMainWindow):
    supportedFileFormats = ['docx']

    def __init__(self, parent=None):
        self.settings = TypoSettings().getSettings()
        self.user_texts = UserTexts().getTexts()

        self.text_font_size = "128"
        #self.text_background_color = "#FFFEEE"
        self.text_color = "#FF0000"

        #os.environ['QT_HARFBUZZ'] = 'old'
        super(MainApp, self).__init__(parent)
        self.observer = QFileSystemWatcher()
        self.createUi()

        self.userTextIds = [0]

    def convertToUnicode(self, source, target):
        """
        wordFile = KuConv.load(source, 'Word2007')
        convertedFile = KuConv.convert(wordFile, 'Word2007')
        isConverted = KuConv.write(convertedFile, target,'Word2007')
        return isConverted
        """
        pass

    def addToQueue(self, fileName):
        if fileName:
            info = self.getFileInfo(fileName)
            if info['typ'] in self.supportedFileFormats:
                tmpIndex = self.exisstInTable(info)
                if tmpIndex is not None:
                    self.setListItemStatus(tmpIndex, '0')
                else:
                    self.addToTable(info)

    def setListItemStatus(self, index, value):
        row = self.ui.listWidget.item(index)
        data = row.data(Qt.UserRole)
        data['status'] = value
        color = QColor(Qt.green)
        color.setAlpha(int(value) / 2)
        row.setData(Qt.BackgroundRole, color)
        row.setData(Qt.UserRole, data)

    def addToTable(self, fileData):
        nameColumn = QListWidgetItem(fileData['fileName'])
        iconPath = ':/icons/' + fileData['typ'] + '.png'
        itemIcon = QIcon()
        itemIcon.addPixmap(QPixmap(iconPath), QIcon.Normal, QIcon.Off)
        nameColumn.setIcon(itemIcon)
        fileData['status'] = '0'
        nameColumn.setData(Qt.UserRole, fileData)

        color = QColor(Qt.green)
        color.setAlpha(int(fileData['status']))
        nameColumn.setData(Qt.BackgroundRole, color)

        self.ui.listWidget.addItem(nameColumn)

    def exisstInTable(self, info):
        filesCount = self.ui.listWidget.count()
        for index in range(filesCount):
            row = self.ui.listWidget.item(index)
            data = row.data(Qt.UserRole)
            if data['fileName'] == info['fileName']:
                if data['path'] == info['path']:
                    return index

    def createUi(self):
        self.ui = Ui_MainWindow()
        self.fontsDB = QFontDatabase()
        self.ui.setupUi(self)
        self.move(50, 50)
        self.setAcceptDrops(True)
        self.last_font = None

        # transparent
        self.setAttribute(Qt.WA_DeleteOnClose, True)
        self.setAttribute(Qt.WA_TranslucentBackground, True)
        #self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint)

        self.setStyleSheet("background: transparent; border: none;")
        self.oldPos = self.pos()

        seizGrip = QSizeGrip(self)
        self.ui.sldTrans.setValue(99)
        self.text_background_color = "#333333"
        #self.text_background_color = "transparent"
        self.text_color = "#FFFEEE"

        # disable observe checkbox on start
        self.ui.chkObserve.setEnabled(False)
        self.ui.chkHB.setEnabled(False)

        self.setAcceptDrops(True)
        self.ui.textEdit.setAcceptDrops(False)

        self.ui.cmbTextSamples.currentIndexChanged.connect(self.setSampleText)
        self.ui.cmbFontSize.currentIndexChanged.connect(self.setFontSize)
        self.observer.fileChanged.connect(self.setTextFont)
        self.ui.textEdit.textChanged.connect(self.saveUserText)
        self.ui.btnPdbExport.clicked.connect(self.exportToPdf)
        self.ui.sldTrans.valueChanged.connect(self.adjustTransparent)
        self.ui.chkHB.stateChanged.connect(self.showHbViewBar)

        #self.ui.textEdit

        self.resize(self.settings.value("size", QSize(270, 225)))
        self.move(self.settings.value("pos", QPoint(50, 50)))
        self.setCustomSampleText()
        self.refreshTextStyle()
        try:
            self.text_font_size = self.settings.value("last_font_size", self.text_font_size)
            self.last_font = self.settings.value("last_font")
            if self.last_font:
                #self.setTextFont(self.last_font, int(self.text_font_size))
                pass
        except:
            pass
    def showHbViewBar(self, e):
        print('########')

        dialog = QDialog()
        dialog.ui = Ui_Dialog()
        dialog.ui.setupUi(dialog)
        dialog.exec_()
        dialog.show()

    def adjustTransparent(self,e):
        self.setWindowOpacity(e/100.0)

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()

    def mouseMoveEvent(self, event):
        delta = QPoint (event.globalPos() - self.oldPos)
        #print(delta)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = event.globalPos()

    def exportToPdf(self):
        printer = QPrinter(QPrinter.PrinterResolution)
        printer.setOutputFormat(QPrinter.PdfFormat)
        printer.setPaperSize(QPrinter.A4)
        printer.setOutputFileName('export_sample.pdf')
        document = self.ui.textEdit.document()
        size = QSizeF(printer.paperRect().size())
        document.setPageSize(size)
        document.print(printer)
        print('###')

    def getSelectedSampleTextId(self):
        selected = self.ui.cmbTextSamples.currentIndex()
        #print(selected)
        return selected

    def saveUserText(self):
        blocker = QSignalBlocker(self.ui.textEdit)
        selected = self.getSelectedSampleTextId()
        if selected in self.userTextIds:
            self.user_texts.setValue("text" + str(selected), self.ui.textEdit.toPlainText())
        m = 1

    def setCustomSampleText(self, id = 0):
        blocker = QSignalBlocker(self.ui.textEdit)
        vals = self.user_texts.value('text'+str(id), 'Write your Text here ..')
        self.ui.textEdit.setText(vals)
        return True

    def setSampleText(self, index):
        if index == 0:
            self.setCustomSampleText()
            return True
        try:
            sample_name = self.ui.cmbTextSamples.itemText(index)
            stream = QFile(":/samples/"+ sample_name +".txt")
            if stream.open(QFile.ReadOnly):
                sample_text = str(stream.readAll(), 'utf-8')
                stream.close()
            else:
                print(stream.errorString())

            self.ui.textEdit.setText(sample_text)
        except:
            print("error on opining sample text resource file")

        self.refreshTextStyle()
        """
        begin = 1
        end = 1000

        fmt = QTextCharFormat()
        fmt.setFontUnderline(True)
        fmt.setUnderlineStyle(QTextCharFormat.WaveUnderline)
        fmt.setUnderlineColor(Qt.red)

        cursor = QTextCursor(self.ui.textEdit.document())
        cursor.movePosition(QTextCursor.Start)

        for i in range (1, 100):
            cursor.movePosition(QTextCursor.StartOfWord)
            cursor.movePosition(QTextCursor.EndOfWord, QTextCursor.KeepAnchor)
            print()
            print('###')
            print(cursor.selectedText())
            cursor.setCharFormat(fmt)
            ff = cursor.charFormat().font()
            h = cursor.charFormat().font().StyleHint
            hh = cursor.charFormat().font().StyleHint()

            print(cursor.charFormat().font().family())
            print(cursor.charFormat().font().defaultFamily())

            cursor.movePosition(QTextCursor.NextWord, QTextCursor.MoveAnchor)
"""

    def setTextFont(self, fileName, size=92):
        print(size)

        bin = self.readFontFileAsBinary(fileName)
        QFontDatabase.removeAllApplicationFonts()
        #id = QFontDatabase.addApplicationFont(fileName)
        id = QFontDatabase.addApplicationFontFromData(bin)
        try:
            family = QFontDatabase.applicationFontFamilies(id)[0]
        except:
            return "Not found"
        font = QFontDatabase.font(self.fontsDB, family, 'bold', size)
        font.setStrikeOut(False)
        self.ui.textEdit.setFont(font)
        self.ui.textEdit.setCurrentFont(font)
        self.ui.chkObserve.setEnabled(True)
        self.ui.chkHB.setEnabled(True)
        return family

    def readFontFileAsBinary(self, path):
        f = QFile(path)
        f.open(QIODevice.ReadOnly | QIODevice.Unbuffered)

        try:
            content = f.readAll()
        finally:
            f.close()
        return content

    def setFontSize(self, index):
        fontSize = self.ui.cmbFontSize.itemText(index)
        self.text_font_size = fontSize
        if self.last_font:
            #self.setTextFont(self.last_font, int(fontSize))
            self.ui.textEdit.font().setPointSize(int(fontSize))

        self.refreshTextStyle()

    def refreshTextStyle(self):
        style = "background-color:%s;color:%s;font-size:%spt;" %\
                (self.text_background_color,self.text_color,self.text_font_size)
        print(style)
        self.ui.textEdit.setStyleSheet(style)

    def setActiveFontLabel(self, fontName):
        self.ui.lblActiveFont.setText(fontName)
        return True

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()
            event.accept()
        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls():
            event.acceptProposedAction()
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            urls = event.mimeData().urls()
            for url in urls:
                fileName = url.toLocalFile()
                self.observer.addPath(fileName)
                self.last_font = fileName
                fontName = self.setTextFont(fileName)
                if fontName:
                    self.setActiveFontLabel(fontName)
            event.accept()
        else:
            event.ignore()
    def closeEvent(self, e):
        self.settings.setValue("size", self.size())
        self.settings.setValue("pos", self.pos())
        self.settings.setValue("last_font", self.last_font)
        self.settings.setValue("last_font_size", self.text_font_size)
        e.accept()

app = QApplication([])
#font= QFont("Courier New")
#font.setStyleHint(QFont.Monospace)
#app.setFont(font)

from PyQt5.QtWidgets import QStyleFactory
app.setStyle(QStyleFactory.create('Fusion'))
win = MainApp()

win.show()

app.exec_()


POP_SIZE = 2000     # cohort population size
SIM_LENGTH = 50     # length of simulation (years)
ALPHA = 0.05        # significance level for calculating confidence intervals
DELTA_T = 1         # years (length of time step, how frequently you look at the patient)
DISCOUNT_RATE = 0.03

# transition matrix
TRANS_MATRIX = [
    [0.75,  0.15,   0.0,    0.1],   # Well
    [0,     0.0,    1.0,    0.0],   # Stroke
    [0,     0.25,   0.55,   0.2],   # Post-Stroke
    [0.0,   0.0,    0.0,    1.0],   # Dead
    ]

# anticoagulation relative risk in reducing stroke incidence and stroke death while in “Post-Stroke”
RR_STROKE = 0.65
# anticoagulation relative risk in increasing mortality due to bleeding is 1.05.
RR_BLEEDING = 1.05

# annual cost of anticoagulation drug
DRUG_COST = 2000

# annual cost
COST_STATE = [
    0,      # Well
    0,      # Stroke
    200,    # Post-Stroke
    0       # Dead
]
# cost of stroke
COST_STROKE = 5000

# health utility
HEALTH_UTILITY = [
    1.0,     # Well
    0.8865,  # Stroke
    0.9,     # Post-Stroke
    0.0      # Dead
]


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import migrations, models
import taggit.managers

class Migration(migrations.Migration):

    dependencies = [
        ('library', '0008_item_tags'),
    ]

    operations = [
        migrations.AlterField(
            model_name='item',
            name='tags',
            field=taggit.managers.TaggableManager(verbose_name='Tags', blank=True, to='tags.Tag', help_text='A comma-separated list of tags.', through='tags.TaggedItem'),
        ),
    ]


import tkinter as tk

root=tk.Tk()
root.title("calculator")
root.geometry("405x405")
root.configure(background="black")
root.iconbitmap(r'ic.ico')

def clears():
    eny.delete(0,"end")
eny=tk.Entry(root,fg="white",bg="black",width=59,borderwidth=21)
eny.grid(rowspan=4,columnspan=4)
nine=tk.Button(root,text="9",width=10,height=3,fg="black",bg="grey",font=('Arial', 12),command=lambda : eny.insert("end","9"))
nine.grid(row=4,column=0,sticky="w",pady=10)
eight=tk.Button(root,text="8",width=10,height=3,fg="black",bg="grey",font=12,command=lambda : eny.insert("end","8"))
eight.grid(row=4,column=1,sticky="w",pady=10)
seven=tk.Button(root,text="7",width=10,height=3,fg="black",bg="grey",font=12,command=lambda : eny.insert("end","7"))
seven.grid(row=4,column=2,sticky="w",pady=10)
plus=tk.Button(root,text="+",width=10,height=3,fg="black",bg="goldenrod",font=12,command=lambda : eny.insert("end","+"))
plus.grid(row=4,column=3,sticky="w",pady=10)
six=tk.Button(root,text="6",width=10,height=3,fg="black",bg="grey",font=12,command=lambda : eny.insert("end","6"))
six.grid(row=5,column=0,sticky="w",pady=10)
five=tk.Button(root,text="5",width=10,height=3,fg="black",bg="grey",font=12,command=lambda : eny.insert("end","5"))
five.grid(row=5,column=1,sticky="w",pady=10)
four=tk.Button(root,text="4",width=10,height=3,fg="black",bg="grey",font=12,command=lambda : eny.insert("end","4"))
four.grid(row=5,column=2,sticky="w",pady=10)
minus=tk.Button(root,text="-",width=10,height=3,fg="black",bg="goldenrod",font=12,command=lambda : eny.insert("end","-"))
minus.grid(row=5,column=3,sticky="w",pady=10)
three=tk.Button(root,text="3",width=10,height=3,fg="black",bg="grey",font=12,command=lambda : eny.insert("end","3"))
three.grid(row=6,column=0,sticky="w",pady=10)
two=tk.Button(root,text="2",width=10,height=3,fg="black",bg="grey",font=12,command=lambda : eny.insert("end","2"))
two.grid(row=6,column=1,sticky="w",pady=10)
one=tk.Button(root,text="1",width=10,height=3,fg="black",bg="grey",font=12,command=lambda : eny.insert("end","1"))
one.grid(row=6,column=2,sticky="w",pady=10)
mul=tk.Button(root,text="*",width=10,height=3,fg="black",bg="goldenrod",font=12,command=lambda : eny.insert("end","*"))
mul.grid(row=6,column=3,sticky="w",pady=10)
zero=tk.Button(root,text="0",width=10,height=3,fg="black",bg="grey",font=12,command=lambda : eny.insert("end","0"))
zero.grid(row=7,column=1,sticky="w",pady=10)
clear=tk.Button(root,text="CLS",width=10,height=3,fg="white",bg="#383A3A",font=12,command=clears)
clear.grid(row=7,column=0,sticky="w",pady=10)
point=tk.Button(root,text="=",width=10,height=3,fg="white",bg="#383A3A",font=12,command=lambda : eny.insert("end","="))
point.grid(row=7,column=2,sticky="w",pady=10)
div=tk.Button(root,text="/",width=10,height=3,fg="black",bg="goldenrod",font=12,command=lambda : eny.insert("end","/"))
div.grid(row=7,column=3,sticky="w",pady=10)
clears()
root.mainloop()


from flask import Flask, render_template
# dude, you forget to set the BluePrint "/examples"!
app = Flask(__name__, static_url_path='/static')
# make it possible to run flask app by .py file name
# static is a norm, includes is a older one, but static is a predetermined built-in folder for flask to find js and data

import math

invoice = {
    "customer": "BigCo",
    "performances": [
        {"playID": "Hamlet", "audience": 55},
        {"playID": "As You Like It", "audience": 35},
        {"playID": "Othello", "audience": 40}
    ]
}

plays = {
    "Hamlet": {"name": "Hamlet", "type": "tragedy"},
    "As You Like It": {"name": "As You Like It", "type": "comedy"},
    "Othello": {"name": "Othello", "type": "tragedy"}
}
# put all needed data here

def format_as_dollars(amount):
    return f"${amount:0,.2f}"
# used for formatting

def get_amount_for_performance(perf):
    play = plays[perf['playID']]
    if play['type'] == "tragedy":
        this_amount = 40000
        if perf['audience'] > 30:
            this_amount += 1000 * (perf['audience'] - 30)
    elif play['type'] == "comedy":
        this_amount = 30000
        if perf['audience'] > 20:
            this_amount += 10000 + 500 * (perf['audience'] - 20)

        this_amount += 300 * perf['audience']

    else:
        raise ValueError(f'unknown type: {play["type"]}')

    return this_amount
# used for calculating cost for each performance

def get_total_amount():
    total_amount = 0
    for perf in invoice['performances']:
        total_amount += get_amount_for_performance(perf)
    return total_amount
# add all cost for performances

def get_volume_credits():
    volume_credits = 0
    for perf in invoice['performances']:
        volume_credits += max(perf['audience'] - 30, 0)
        if "comedy" == plays[perf['playID']]["type"]:
            volume_credits += math.floor(perf['audience'] / 5)
    return volume_credits
# calculate volume credits

@app.route('/statement')
def statement():
    return render_template('statement.html', customer = invoice["customer"], performances = invoice["performances"], plays = plays, format_as_dollars = format_as_dollars, get_amount_for_performance = get_amount_for_performance, get_total_amount=get_total_amount(), volume_credits=get_volume_credits())
# render
# just write a function which writes html in statement.py file!

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


def getNum(n):
    if n % 2 != 0:
        k = int(n / 2 + 1)
        return int(pow(2, pow(2, k - 1)))
    else:
        k = int(n / 2)
        return int(pow(2, pow(3, k - 1)))

n = int(input())

t_list = []
ans_list = []
for i in range(n):
    t = int(input())
    ans = getNum(t)
    t_list.append(t)
    ans_list.append(ans)
    print(ans)


# -*- coding: utf-8 -*-
"""
Created on Fri Dec  6 09:46:53 2019

@author: Vikash
"""

# -*- coding: utf-8 -*-
import pandas as pd
import array
import itertools
import numpy as np
import statistics

def bin():

    filename = str(input("Which file do you want?"))
    if not ".csv" in filename:
        filename += ".csv"
        file=pd.read_csv(filename)

    print("COLUMN NAMES \n")
    l=file.columns.values
    c=0
    for i in file:
        c=c+1
        print(c, "-" , i)

    colname=int(input("ENTER ID OF COLUMN :"))
    print("YOU HAVE SELECTED COLUMN :",l[colname-1])
    targetcol=file[l[colname-1]]
    n=targetcol.count()
    print("NUMBER OF DATA", n)

    if type(targetcol.iloc[0]) == str:
        print("THIS FUNCTION CAN BE APPLIED ON INTEGER TYPE ONLY ")
        return

    row_no = n
    id1 = 0
    list_col = []

    for row in itertools.islice(targetcol,0,row_no):
        list_col.append(row)
        id1+=1;
    array = np.array(list_col)

    array = np.sort(array)
    print("THE SORTED ARRAY IS:")
    print(array)

    number=int(input("ENTER THE NUMBER OF BINS:"))
    bin_num=number
    binsize=(int)(n/bin_num)
    bin1=array[0:n]
    i=1

    if (len(array)%bin_num != 0):
           print("Invalid Bin Number")

    bins = np.split(array,bin_num)

    print("TOTAL NUMBER OF BINS :\n\n\n",bins,"\n")
    print("VALUE OF BIN BY BOUNDARY(Min-Max):",number)
    for j in range(1,number+1):
             bin=bin1[(binsize*(j-1)):(binsize*j)]
             binmin=bin.min()
             binmax=bin.max()
             print("Bin",j,":","[",binmin,",",binmax,"]")

print(bin())


from django.shortcuts import render

# Create your views here.
def listarDirectores(request):
    datos = {
        "autor": "Djanguito",
        "directores": [
            {"nombre": "Quentin Tarantino", "imagen": "img/quentin.png", "retirado": True},
            {"nombre": "James Gunn", "imagen": "img/james.png", "retirado": False}
        ]
    }
    return render(request, 'segunda_aplicacion/listar_directores.html', datos)

def listarActores(request):
    return render(request, 'segunda_aplicacion/lista_actores.html')


from datetime import date
from logging import getLogger

from django.conf import settings
from easy_thumbnails.exceptions import InvalidImageFormatError
from sentry_sdk import capture_exception, capture_message, push_scope

from merchants.models import PaymentProduct
from merchants.verkkokauppa.product.exceptions import CreateOrUpdateAccountingError
from merchants.verkkokauppa.product.requests import (
    create_or_update_accounting,
    create_product,
)
from merchants.verkkokauppa.product.types import (
    CreateOrUpdateAccountingParams,
    CreateProductParams,
)
from reservation_units.pricing_updates import update_reservation_unit_pricings
from reservation_units.utils.reservation_unit_payment_helper import ReservationUnitPaymentHelper
from tilavarauspalvelu.celery import app
from utils.image_cache import purge

logger = getLogger(__name__)

@app.task(name="update_reservation_unit_pricings")
def _update_reservation_unit_pricings() -> None:
    today = date.today()

    logger.info(f"Updating reservation unit pricing with date {today}")
    num_updated = update_reservation_unit_pricings(today)
    logger.info(f"Updated {num_updated} reservation units with date {today}")

@app.task(
    name="refresh_reservation_unit_product_mapping",
    autoretry_for=(TypeError,),
    max_retries=5,
    retry_backoff=True,
)
def refresh_reservation_unit_product_mapping(reservation_unit_pk) -> None:
    from reservation_units.models import ReservationUnit

    reservation_unit = ReservationUnit.objects.filter(pk=reservation_unit_pk).first()
    if reservation_unit is None:
        capture_message(
            f"Unable to refresh reservation unit product mapping. Reservation unit not found: {reservation_unit_pk}",
            level="warning",
        )
        return

    payment_merchant = ReservationUnitPaymentHelper.get_merchant(reservation_unit)

    if ReservationUnitPaymentHelper.requires_product_mapping_update(reservation_unit):
        params = CreateProductParams(
            namespace=settings.VERKKOKAUPPA_NAMESPACE,
            namespace_entity_id=reservation_unit.pk,
            merchant_id=payment_merchant.id,
        )
        api_product = create_product(params)
        payment_product, _ = PaymentProduct.objects.update_or_create(
            id=api_product.product_id, defaults={"merchant": payment_merchant}
        )

        ReservationUnit.objects.filter(pk=reservation_unit_pk).update(payment_product=payment_product)

        refresh_reservation_unit_accounting.delay(reservation_unit_pk)

    # Remove product mapping if merchant is removed
    if reservation_unit.payment_product and not payment_merchant:
        ReservationUnit.objects.filter(pk=reservation_unit_pk).update(payment_product=None)

@app.task(
    name="refresh_reservation_unit_accounting",
    autoretry_for=(TypeError,),
    max_retries=5,
    retry_backoff=True,
)
def refresh_reservation_unit_accounting(reservation_unit_pk) -> None:
    from reservation_units.models import ReservationUnit

    reservation_unit = ReservationUnit.objects.filter(pk=reservation_unit_pk).first()
    if reservation_unit is None:
        capture_message(
            f"Unable to refresh reservation unit accounting data. Reservation unit not found: {reservation_unit_pk}",
            level="warning",
        )
        return

    accounting = ReservationUnitPaymentHelper.get_accounting(reservation_unit)

    if reservation_unit.payment_product and accounting:
        params = CreateOrUpdateAccountingParams(
            vat_code=accounting.vat_code,
            internal_order=accounting.internal_order,
            profit_center=accounting.profit_center,
            project=accounting.project,
            operation_area=accounting.operation_area,
            company_code=accounting.company_code,
            main_ledger_account=accounting.main_ledger_account,
            balance_profit_center=accounting.balance_profit_center,
        )
        try:
            accounting = create_or_update_accounting(reservation_unit.payment_product.id, params)
        except CreateOrUpdateAccountingError as err:
            with push_scope() as scope:
                scope.set_extra("details", "Unable to refresh reservation unit accounting data")
                scope.set_extra("reservation-unit-id", reservation_unit_pk)
                capture_exception(err)

@app.task(name="update_reservation_unit_image_urls")
def update_urls(pk: int | None = None):
    from reservation_units.models import ReservationUnitImage

    images = ReservationUnitImage.objects.filter(image__isnull=False)

    if pk:
        images = images.filter(pk=pk)

    for image in images:
        try:
            image.large_url = image.image["large"].url
            image.medium_url = image.image["medium"].url
            image.small_url = image.image["small"].url
            image.save(update_urls=False)

        except InvalidImageFormatError as err:
            capture_exception(err)

@app.task(name="purge_image_cache")
def purge_image_cache(image_path: str):
    purge(image_path)


import numpy as np
import os.path as op
from numpy.testing import assert_array_almost_equal
from nose.tools import assert_true, assert_false, assert_equal

import mne
from mne import io, Epochs, read_events, pick_types
from mne.time_frequency import single_trial_power
from mne.time_frequency.tfr import cwt_morlet, morlet, tfr_morlet

raw_fname = op.join(op.dirname(__file__), '..', '..', 'io', 'tests', 'data',
                    'test_raw.fif')
event_fname = op.join(op.dirname(__file__), '..', '..', 'io', 'tests',
                      'data', 'test-eve.fif')

def test_morlet():
    """Test morlet with and without zero mean"""
    Wz = morlet(1000, [10], 2., zero_mean=True)
    W = morlet(1000, [10], 2., zero_mean=False)

    assert_true(np.abs(np.mean(np.real(Wz[0]))) < 1e-5)
    assert_true(np.abs(np.mean(np.real(W[0]))) > 1e-3)

def test_time_frequency():
    """Test time frequency transform (PSD and phase lock)
    """
    # Set parameters
    event_id = 1
    tmin = -0.2
    tmax = 0.5

    # Setup for reading the raw data
    raw = io.Raw(raw_fname)
    events = read_events(event_fname)

    include = []
    exclude = raw.info['bads'] + ['MEG 2443', 'EEG 053']  # bads + 2 more

    # picks MEG gradiometers
    picks = pick_types(raw.info, meg='grad', eeg=False,
                            stim=False, include=include, exclude=exclude)

    picks = picks[:2]
    epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks,
                    baseline=(None, 0))
    data = epochs.get_data()
    times = epochs.times
    nave = len(data)

    freqs = np.arange(6, 20, 5)  # define frequencies of interest
    n_cycles = freqs / 4.

    # Test first with a single epoch
    power, itc = tfr_morlet(epochs[0], freqs=freqs, n_cycles=n_cycles,
                            use_fft=True, return_itc=True)

    power, itc = tfr_morlet(epochs, freqs=freqs, n_cycles=n_cycles,
                            use_fft=True, return_itc=True)

    print(itc)  # test repr
    print(itc.ch_names) # test property
    itc = itc + power # test add
    itc = itc - power # test add
    itc -= power
    itc += power

    power.apply_baseline(baseline=(-0.1, 0), mode='logratio')

    assert_true('meg' in power)
    assert_true('grad' in power)
    assert_false('mag' in power)
    assert_false('eeg' in power)

    assert_equal(power.nave, nave)
    assert_equal(itc.nave, nave)
    assert_true(power.data.shape == (len(picks), len(freqs), len(times)))
    assert_true(power.data.shape == itc.data.shape)
    assert_true(np.sum(itc.data >= 1) == 0)
    assert_true(np.sum(itc.data <= 0) == 0)

    power, itc = tfr_morlet(epochs, freqs=freqs, n_cycles=2, use_fft=False,
                            return_itc=True)

    assert_true(power.data.shape == (len(picks), len(freqs), len(times)))
    assert_true(power.data.shape == itc.data.shape)
    assert_true(np.sum(itc.data >= 1) == 0)
    assert_true(np.sum(itc.data <= 0) == 0)

    Fs = raw.info['sfreq']  # sampling in Hz
    tfr = cwt_morlet(data[0], Fs, freqs, use_fft=True, n_cycles=2)
    assert_true(tfr.shape == (len(picks), len(freqs), len(times)))

    single_power = single_trial_power(data, Fs, freqs, use_fft=False,
                                      n_cycles=2)

    assert_array_almost_equal(np.mean(single_power), power.data)

    power_pick = power.pick_channels(power.ch_names[:10:2])
    assert_equal(len(power_pick.ch_names), len(power.ch_names[:10:2]))
    assert_equal(power_pick.data.shape[0], len(power.ch_names[:10:2]))
    power_drop = power.drop_channels(power.ch_names[1:10:2])
    assert_equal(power_drop.ch_names, power_pick.ch_names)
    assert_equal(power_pick.data.shape[0], len(power_drop.ch_names))

    mne.equalize_channels([power_pick, power_drop])
    assert_equal(power_pick.ch_names, power_drop.ch_names)
    assert_equal(power_pick.data.shape, power_drop.data.shape)


import requests
import pprint
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
import json
import sys
import time
from apiModels import Animal, app, db, lat_list, long_list, states

def getAnimal():
    max_page = 1
    max_per_page = 4
    has_photo = "true"

    # example call:
    # https://api.inaturalist.org/v1/observations?photos=true&taxon_is_active=true&lat=44.500000&lng=-89.500000&page=1&per_page=4&order=desc&order_by=created_at
    # https://api.inaturalist.org/v1/observations?photos=true&taxon_is_active=true&hrank=species&lrank=subspecies&lat=44.500000&lng=-100.000000&page=1&per_page=4&order=desc&order_by=created_at
    for i in range(0, len(states)):

        if i != 0 and i % 5 == 0:
            time.sleep(300)

        max_per_page = 4
        url = (
            "https://api.inaturalist.org/v1/observations?photos="
            + has_photo
            + "&lat="
            + str(lat_list[i])
            + "&lng="
            + str(long_list[i])
            + "&page="
            + str(max_page)
            + "&per_page="
            + str(max_per_page)
            + "&order=desc&order_by=created_at"
        )
        response = requests.get(url)
        # print(url)

        if response.status_code != 200:
            print(url)
            print("URL not found")
            continue

        jsonRes = json.loads(response.text)

        limit = len(jsonRes["results"])

        if limit < max_per_page:
            max_per_page = limit

        for j in range(0, max_per_page):
            try:

                if jsonRes["results"][j]["taxon"] == None:
                    continue

                info_id = jsonRes["results"][j]["taxon"]["id"]

                # example call:
                # https://api.inaturalist.org/v1/taxa/3017

                description_url = "https://api.inaturalist.org/v1/taxa/" + str(info_id)

                description_response = requests.get(description_url)

                if description_response.status_code != 200:
                    print("Description URL not found")
                    continue

                summary = json.loads(description_response.text)

                info_location = states[i]
                info_scientificName = jsonRes["results"][j]["taxon"]["name"]

                if (
                    jsonRes["results"] == None
                    or len(jsonRes["results"]) == 0
                    or jsonRes["results"][j]["taxon"] == None
                    or jsonRes["results"][j]["taxon"]["default_photo"] == None
                ):
                    continue

                # print(info_scientificName)
                info_picURL = jsonRes["results"][j]["taxon"]["default_photo"][
                    "medium_url"
                ]
                info_commonName = jsonRes["results"][j]["taxon"][
                    "preferred_common_name"
                ]
                info_numObser = jsonRes["results"][j]["taxon"]["observations_count"]
                info_description = summary["results"][0]["wikipedia_summary"]
                info_isExtinct = jsonRes["results"][j]["taxon"]["extinct"]
                info_rank = jsonRes["results"][j]["taxon"]["rank"]
                info_lastSighting = jsonRes["results"][j]["time_observed_at"]
                info_taxonName = jsonRes["results"][j]["taxon"]["iconic_taxon_name"]
                info_ancestry = jsonRes["results"][j]["taxon"]["ancestry"].replace(
                    "/", ", "
                )

                exists = Animal.query.filter_by(animal_id=info_id).scalar() is not None

                # print (exists)

                if not exists and None not in (
                    info_id,
                    info_location,
                    info_scientificName,
                    info_picURL,
                    info_commonName,
                    info_numObser,
                    info_description,
                    info_isExtinct,
                    info_rank,
                    info_lastSighting,
                    info_taxonName,
                    info_ancestry,
                ):
                    db.session.add(
                        Animal(
                            animal_id=info_id,
                            animal_location=info_location,
                            animal_scientificName=info_scientificName,
                            animal_picURL=info_picURL,
                            animal_commonName=info_commonName,
                            animal_numObser=info_numObser,
                            animal_description=info_description,
                            animal_isExtinct=info_isExtinct,
                            animal_rank=info_rank,
                            animal_lastSighting=info_lastSighting,
                            animal_taxonName=info_taxonName,
                            animal_ancestry=info_ancestry,
                        )
                    )
                    db.session.commit()
                    print("Commit: " + states[i])
                    time.sleep(10)

            except KeyError:
                print("KeyError")
                pass

if __name__ == "__main__":
    getAnimal()


""" Protocol Buffer Breaking Change Detector

This tool is used to detect "breaking changes" in protobuf files, to
ensure proper backwards-compatibility in protobuf API updates. The tool
can check for breaking changes of a single API by taking 2 .proto file
paths as input (before and after) and outputting a bool `is_breaking`.

The breaking change detector creates a temporary directory, copies in
each file to compute a protobuf "state", computes a diff of the "before"
and "after" states, and runs the diff against a set of rules to determine
if there was a breaking change.

The tool is currently implemented with buf (https://buf.build/)
"""

from pathlib import Path
from typing import List

from tools.api_proto_breaking_change_detector.buf_utils import check_breaking, pull_buf_deps
from tools.api_proto_breaking_change_detector.detector_errors import ChangeDetectorError

class ProtoBreakingChangeDetector(object):
    """Abstract breaking change detector interface"""

    def run_detector(self) -> None:
        """Run the breaking change detector to detect rule violations

        This method should populate the detector's internal data such
        that `is_breaking` does not require any additional invocations
        to the breaking change detector.
        """
        pass

    def is_breaking(self) -> bool:
        """Return True if breaking changes were detected in the given protos"""
        pass

    def get_breaking_changes(self) -> List[str]:
        """Return a list of strings containing breaking changes output by the tool"""
        pass

class BufWrapper(ProtoBreakingChangeDetector):
    """Breaking change detector implemented with buf"""

    def __init__(
            self,
            path_to_changed_dir: str,
            git_ref: str,
            git_path: str,
            subdir: str = None,
            buf_path: str = None,
            config_file_loc: str = None,
            additional_args: List[str] = None) -> None:
        """Initialize the configuration of buf

        This function sets up any necessary config without actually
        running buf against any proto files.

        BufWrapper takes a path to a directory containing proto files
        as input, and it checks if these proto files break any changes
        from a given initial state.

        The initial state is input as a git ref. The constructor expects
        a git ref string, as well as an absolute path to a .git folder
        for the repository.

        Args:
            path_to_changed_dir {str} -- absolute path to a directory containing proto files in the after state
            buf_path {str} -- path to the buf binary (default: "buf")
            git_ref {str} -- git reference to use for the initial state of the protos (typically a commit hash)
            git_path {str} -- absolute path to .git folder for the repository of interest

            subdir {str} -- subdirectory within git repository from which to search for .proto files (default: None, e.g. stay in root)
            additional_args {List[str]} -- additional arguments passed into the buf binary invocations
            config_file_loc {str} -- absolute path to buf.yaml configuration file (if not provided, uses default buf configuration)
        """
        if not Path(path_to_changed_dir).is_dir():
            raise ValueError(f"path_to_changed_dir {path_to_changed_dir} is not a valid directory")

        if Path.cwd() not in Path(path_to_changed_dir).parents:
            raise ValueError(
                f"path_to_changed_dir {path_to_changed_dir} must be a subdirectory of the cwd ({ Path.cwd() })"
            )

        if not Path(git_path).exists():
            raise ChangeDetectorError(f'path to .git folder {git_path} does not exist')

        self._path_to_changed_dir = path_to_changed_dir
        self._additional_args = additional_args
        self._buf_path = buf_path or "buf"
        self._config_file_loc = config_file_loc
        self._git_ref = git_ref
        self._git_path = git_path
        self._subdir = subdir
        self._final_result = None

        pull_buf_deps(
            self._buf_path,
            self._path_to_changed_dir,
            config_file_loc=self._config_file_loc,
            additional_args=self._additional_args)

    def run_detector(self) -> None:
        self._final_result = check_breaking(
            self._buf_path,
            self._path_to_changed_dir,
            git_ref=self._git_ref,
            git_path=self._git_path,
            subdir=self._subdir,
            config_file_loc=self._config_file_loc,
            additional_args=self._additional_args)

    def is_breaking(self) -> bool:
        if not self._final_result:
            raise ChangeDetectorError("Must invoke run_detector() before checking if is_breaking()")

        final_code, final_out, final_err = self._final_result
        final_out, final_err = '\n'.join(final_out), '\n'.join(final_err)

        if final_err != "":
            raise ChangeDetectorError(f"Error from buf: {final_err}")

        if final_code != 0:
            return True
        if final_out != "":
            return True
        return False

    def get_breaking_changes(self) -> List[str]:
        _, final_out, _ = self._final_result
        return filter(lambda x: len(x) > 0, final_out) if self.is_breaking() else []