"""
####    ##    ##    ########     ##     ##    ########     ######       ####        #######     ##     ##    ########    ########     ##     ##    ########     ######
 ##     ###   ##    ##     ##    ##     ##       ##       ##    ##     ##  ##      ##     ##    ##     ##       ##       ##     ##    ##     ##       ##       ##    ##
 ##     ####  ##    ##     ##    ##     ##       ##       ##            ####       ##     ##    ##     ##       ##       ##     ##    ##     ##       ##       ##
 ##     ## ## ##    ########     ##     ##       ##        ######      ####        ##     ##    ##     ##       ##       ########     ##     ##       ##        ######
 ##     ##  ####    ##           ##     ##       ##             ##    ##  ## ##    ##     ##    ##     ##       ##       ##           ##     ##       ##             ##
 ##     ##   ###    ##           ##     ##       ##       ##    ##    ##   ##      ##     ##    ##     ##       ##       ##           ##     ##       ##       ##    ##
####    ##    ##    ##            #######        ##        ######      ####  ##     #######      #######        ##       ##            #######        ##        ######

(font banner 3)
"""
## for the dear user
print('getting inputs and outputs......................')

### step_seven_input_path = step_one_to_six_output_path
#step_seven_input_path = r'Data_collection_project/step_one_to_six_output_MAIN.csv'
step_seven_input_path = r'Data_collection_project/step_one_to_six_output_test.csv'

#this is output from step 7
#it includes all the availible_seq data frame  note that there is no seq download yet
#step_seven_output = 'Data_collection_project/all_availible_seq_clean_MAIN(step7 output).csv'
step_seven_output_path = 'Data_collection_project/all_availible_seq_clean(step7 output).csv'

#this is output from step 8
#step_eight_output_path = 'Data_collection_project/all_availible_seq_clean_MAIN(step7 output).csv'
step_eight_output_path = 'Data_collection_project/all_availible_seq_clean(step7 output).csv'

"""
####    ##     ##    ########      #######     ########     ########    ####    ##    ##     ######
 ##     ###   ###    ##     ##    ##     ##    ##     ##       ##        ##     ###   ##    ##    ##
 ##     #### ####    ##     ##    ##     ##    ##     ##       ##        ##     ####  ##    ##
 ##     ## ### ##    ########     ##     ##    ########        ##        ##     ## ## ##    ##   ####
 ##     ##     ##    ##           ##     ##    ##   ##         ##        ##     ##  ####    ##    ##
 ##     ##     ##    ##           ##     ##    ##    ##        ##        ##     ##   ###    ##    ##
####    ##     ##    ##            #######     ##     ##       ##       ####    ##    ##     ######

########        ###        ######     ##    ##       ###        ######      ########     ######
##     ##      ## ##      ##    ##    ##   ##       ## ##      ##    ##     ##          ##    ##
##     ##     ##   ##     ##          ##  ##       ##   ##     ##           ##          ##
########     ##     ##    ##          #####       ##     ##    ##   ####    ######       ######
##           #########    ##          ##  ##      #########    ##    ##     ##                ##
##           ##     ##    ##    ##    ##   ##     ##     ##    ##    ##     ##          ##    ##
##           ##     ##     ######     ##    ##    ##     ##     ######      ########     ######

(font banner 3)
"""

print('importing packages.............................')
# main packages
import pandas as pd
from bs4 import BeautifulSoup
import requests
import re
import time

# using PubMed API
from pymed import PubMed

# using NCBI API
from Bio import Entrez
from Bio import SeqIO
Entrez.email = 'kalhor.kz@gmail.com'

# for downloading from NCBI>assembly
import os

######################
#  estimating time   #
start = time.time()  #
#                    #
######################

"""
 ######  ######## ######## ########      ######  ######## ##     ## ######## ##    ##
##    ##    ##    ##       ##     ##    ##    ## ##       ##     ## ##       ###   ##
##          ##    ##       ##     ##    ##       ##       ##     ## ##       ####  ##
 ######     ##    ######   ########      ######  ######   ##     ## ######   ## ## ##
      ##    ##    ##       ##                 ## ##        ##   ##  ##       ##  ####
##    ##    ##    ##       ##           ##    ## ##         ## ##   ##       ##   ###
 ######     ##    ######## ##            ######  ########    ###    ######## ##    ##
 (font banner 3)
"""

#goal: finding out what are the availible seq data  -----> searching BacDive

#############################################################################
# a def which reads CSV
def read_excell_as_dataFrame(path):
    DataFrame = pd.read_csv(path)
    return DataFrame
first_dataframe = read_excell_as_dataFrame(step_seven_input_path)

# making some links to extract all availible data from BacDive
def gets_ids_give_BacDive_links(first_dataframe):
    link_part_one = r'https://bacdive.dsmz.de/strain/'
    links_list = []
    for i in first_dataframe['ID']:
        link = link_part_one + str(i)
        links_list.append(link)
    return links_list
links_list = gets_ids_give_BacDive_links(first_dataframe)

## to clean the memory
del first_dataframe

########################## extracting from BacDive ############################

def read_html(url):
    response = requests.get(url)

    if response.status_code == 200:
        return response.text
    return None
    #CODE = 200 means the url is availible

my_data_frame = pd.DataFrame()
COUNTER = 0
for url in links_list:
    COUNTER = COUNTER +1
    html_doc = read_html(url)
    if html_doc is None:
        print("Something went wrong!!!  the following url seems to be wrong   ; " , url)
    soup = BeautifulSoup(html_doc, "lxml")

    listm = [url]

    #first step ==> extracting seq data
    tag = "valigntop border padding"
    data = soup.find_all("td", class_= tag)
    for td in data:
        listm.append(td.text)
        print('td is appending ' , COUNTER, '..................')
    # some of the lists contains more than 20 availible seq
    ########################### THIS IS MY HYPER-PARAMETER ##############################
    maximum_lenght_list = 300
    while len(listm) < maximum_lenght_list:
        listm.append('')

    my_data_frame[COUNTER] = pd.Series(listm)

## to clean the memory
del listm
del links_list

# teanspose the dataframe
my_data_frame = my_data_frame.T

# save the dataframe as csv
#print('saving my_data_frame ....................')
#my_data_frame.to_csv(first_output_path)

# this code seperates the sequence colums
print('seperating columns and cleaning data....................')

null = ''
my_new_dataframe = pd.DataFrame()

for i in range(0,len(my_data_frame)):
    print('its runnung...............')
    this_row = []
    for j in range (0, maximum_lenght_list):

        if "tax ID" not in str(my_data_frame.iloc[i][j]):
            this_row.append(my_data_frame.iloc[i][j])

        if "tax ID" in str(my_data_frame.iloc[i][j]):
            while len(this_row)%7 != 0:
                this_row.append(null)
            this_row.append(my_data_frame.iloc[i][j])

########################### THIS IS MY HYPER-PARAMETER ##############################
    maximum_lenght_this_row = 400
    while len(this_row) != maximum_lenght_this_row:
        this_row.append(null)

    my_new_dataframe[i] = pd.Series(this_row)

my_new_dataframe = my_new_dataframe.T

print('datafarme is ready..............')

# this code relocate the tax data
for i in range(0,len(my_new_dataframe)):
    for j in range (0,maximum_lenght_this_row):
        if 'tax' in str(my_new_dataframe.iloc[i][j]):
            my_new_dataframe.iloc[i][j-2] = my_new_dataframe.iloc[i][j]
            my_new_dataframe.iloc[i][j] = ''

print('tax data have been relocated...............')
my_new_dataframe.to_csv(step_seven_output_path)

"""
 ######     ########    ########    ########           ########    ####     ######      ##     ##    ########
##    ##       ##       ##          ##     ##          ##           ##     ##    ##     ##     ##       ##
##             ##       ##          ##     ##          ##           ##     ##           ##     ##       ##
 ######        ##       ######      ########           ######       ##     ##   ####    #########       ##
      ##       ##       ##          ##                 ##           ##     ##    ##     ##     ##       ##
##    ##       ##       ##          ##                 ##           ##     ##    ##     ##     ##       ##
 ######        ##       ########    ##                 ########    ####     ######      ##     ##       ##

(font banner 3)
"""

#############################################################################################

# this is a separate code  #we use it to download file from assembly
def give_error_download_the_assembly_file(erorr):
    # searching to find sequnces for errors
    # searching assembly database in NCBI
    print('downloading from assembly>NCBI   please wait.....................')

    ############################give_accesstion_numbers(id)_return_esummary_record
    def get_id_give_assembly_summary(id):
        """Get esummary for an entrez id"""

        #print('+++++++', id)
        esummary_handle = Entrez.esummary(db="assembly", id=id, report="full")
        esummary_record = Entrez.read(esummary_handle)
        #print('=======',esummary_record)
        return esummary_record
    ##############################################################################

    ############################give_accesstion_numbers(term)_return_links
    def get_assemblies_link_from_accession_number(term):
        """Download genbank assemblies for a given search term.
        Args:
            term: search term, usually organism name
        """
        ###########print('+++++++',term)
        # provide your own mail here # I wrote the email at the begining of the codes
        handle = Entrez.esearch(db="assembly", term=term, retmax="200")
        record = Entrez.read(handle)
        ids = record["IdList"]
        links = []
        for aid in ids:
            summary = get_id_give_assembly_summary(aid)  # get summary
            url = summary["DocumentSummarySet"]["DocumentSummary"][0]["FtpPath_RefSeq"]
            if url == "":
                continue
            label = os.path.basename(url)
            # get the fasta link - change this to get other formats
            link = url + "/" + label + "_genomic.fna.gz"
            link = link.replace("ftp://", "https://")
            links.append(link)

        #############print('=======', links)
        return links
    ######################################################################

    ############################give_accesstion_numbers_return_link
    def get_link(accesstion_number):
        """ Get link from internet and return it."""
        #print('=======', accesstion_numbers)

        # read links from interent

        link = get_assemblies_link_from_accession_number(accesstion_number)

        return link
    ###############################################################

    ################# download a url and save it using path
    def download(url, path):
        """Download file based on url and save file to the path"""
        response = requests.get(url)

        if response.ok:
            print("response is ok file is downloading ... ")
            # start to download file from url.
            with open(path, "wb") as f:
                f.write(response.content)
        else:
            print("Error!", response.status_code)
            return False

        print("File downloaded succusfully.")
        return True
    #######################################################

    # giving the accesstion_numbers
    accesstion_number = erorr

    # read links from internet
    link = get_link(accesstion_number)

    # download of each link's data and save them with path name
    if link != []:
        link = link[0]
        filename = link.split("/")[-1]
        download(link, f"Data_collection_project//downloaded_files_in_step_8//{filename}")

    # download data from each links
#############################################################################################

# now its time to extract the subsequent sequence data using their accession numbers
# this code gets accession_number and gives you Id,Description,Seq
# it will try to get data frome NCBI_nucleotide but if there were no data -
# then it tries to download assemly file using pervious code
def get_accession_number_give_seq_from_NCBI_nucleotide(accession_number):

    print('searching for', accession_number)
    try:
        handle = Entrez.efetch(db = "nucleotide", id = accession_number, rettype = "fasta")
        record = SeqIO.read( handle, "fasta" )
        ID = record.id
        Description = record.description
        Seq = record.seq
        print('sequence found')
    except:
        print(accession_number , ' without any sequnce on NCBI>nucleotide: ')
        give_error_download_the_assembly_file(accession_number)

        #if you want to do anything with .fna.gz file you should do it in here

        ID, Description, Seq = "downloaded","downloaded","downloaded"
    return ID, Description, Seq

#############################################################################################

#@@@@@@@@@ example of what two pervious codes do
#get data from neculotide
#Id,Description,Seq = get_accession_number_give_seq_from_NCBI_nucleotide('AB665079')
#print(Id,Description,Seq)
#downloads from assembly
#Id,Description,Seq = get_accession_number_give_seq_from_NCBI_nucleotide('GCA_002082135')
#print(Id,Description,Seq)

####################### importing the csv ########################
print('importing the csv........................')

# read the input
my_dataframe =  pd.read_csv(step_seven_output_path)

####################### making index lists ########################
print('making index lists ...............')

list_accession_number_columns = []
for columns in range (3,300,7):
    list_accession_number_columns.append(columns)

list_description_columns = []
for columns in range (7,300,7):
    list_description_columns.append(columns)

list_seq_columns = []
for columns in range (8,300,7):
    list_seq_columns.append(columns)

################ palcing descriptions & sequences #################

for row in range(0,len(my_dataframe)):

    #the following code makes a list with row value
    #for example: list_number_75 = []
    list_name = f"list_number_{row}"
    exec(list_name + " = []")

    for column in range (0,300):

        if column in list_accession_number_columns:
            accession_number = my_dataframe.iloc[row,column]
            if str(accession_number) != 'nan':
                #print(accession_number,'+++++++++++ do sth and find seq')
                Id,Description,Seq = get_accession_number_give_seq_from_NCBI_nucleotide(accession_number)

            if str(accession_number) == 'nan':
                Description = ''
                Seq = ''

        if column in list_description_columns:
            exec(list_name +'.append(Description)')

        if column in list_seq_columns:
            exec(list_name +'.append(Seq)')

        if ((column not in list_description_columns) and (column not in list_seq_columns)):
            exec(list_name +'.append(my_dataframe.iloc[row,column])')

############################## making a datafarame ################################

# dictionary of lists
string = '{'
for i in range (0,len(my_dataframe)):
    string = string + str(i) +': list_number_'+str(i)
    if i != (len(my_dataframe)-1):
        string = string +','
    if i == (len(my_dataframe)-1):
        string = string +'}'

exec('dict ='+ string)
df = pd.DataFrame(dict)
df = df.T

################################ export csv file #################################

df.to_csv(step_eight_output_path)

print('STEP 7 AND 8 are done :) :)  :) .................')
######################################################################
print('mission complete')
######################
#                    #
end = time.time()    #
#                    #
######################
print('estimated time is ', end-start ,' seconds or ',(end-start)/60 , ' minutes or ',(end-start)/3600 , ' hours')
########################################################################

"""
 #######     ########    ##     ##    ########    ########            ######      #######     ########     ########     ######
##     ##       ##       ##     ##    ##          ##     ##          ##    ##    ##     ##    ##     ##    ##          ##    ##
##     ##       ##       ##     ##    ##          ##     ##          ##          ##     ##    ##     ##    ##          ##
##     ##       ##       #########    ######      ########           ##          ##     ##    ##     ##    ######       ######
##     ##       ##       ##     ##    ##          ##   ##            ##          ##     ##    ##     ##    ##                ##
##     ##       ##       ##     ##    ##          ##    ##           ##    ##    ##     ##    ##     ##    ##          ##    ##
 #######        ##       ##     ##    ########    ##     ##           ######      #######     ########     ########     ######

(font banner 3)
"""

###********************************************************************
###this code reads .gz file which is a compressed file
###for example
#import gzip
#input_path = r'Data_collection_project/downloaded_files_in_step_8/GCF_000754095.2_ASM75409v2_genomic.fna.gz'
#def read_gz_files(input_path):
    #with gzip.open(input_path, 'rb') as f:
        #file_content = f.read()
    #return file_content

#print(read_gz_files(input_path))
#**********************************************************************


'''
go列为多个GO，分号间隔；每个都需要查询得到term
gene_swiss_GO.id

'''

input_file1 = open("gene_swiss_GO.id")
input_file2 = open("go_term_class.tab")
out_file = open("gene_GOterm.out","w")

id_term_dict = {}
for line in input_file2:
    line = line.strip()
    GO_id = line.split("\t")[0]
    GO_term = line.split("\t")[1]
    id_term_dict[GO_id] = GO_term

for line in input_file1:
    line = line.strip()
    text_list = line.split("\t")
    gene_id = text_list[0]
    if len(text_list)==2:
        id_term = " "
    else:
        go_id_all = (text_list[2])
        go_id_list = go_id_all.split(";")
        id_term_list = []
        for go_id in go_id_list:
            go_id = go_id.strip()
            if go_id in id_term_dict.keys():
                go_term = id_term_dict[go_id]
            else:
                go_term = ""
            id_plus_term = go_id + "#" +go_term
            id_term_list.append(id_plus_term)

        id_term = (";").join(id_term_list)
    out_file.write(gene_id + "\t"+ id_term + "\n")

out_file.close()


# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def pseudoPalindromicPaths (self, root: Optional[TreeNode]) -> int:
        ans=0
        def dfs(root,d):
            nonlocal ans
            if(root.val in d):
                d[root.val]+=1
            else:
                d[root.val]=1
            if(not root.left and not root.right):
                a=0
                s=0
                for i in d:
                    s+=d[i]
                    if(d[i]%2==1):
                        a+=1
                if((s%2==0 and a==0) or (s%2==1 and a==1)):
                    ans+=1
            if(root.left):
                dfs(root.left,d)
            if(root.right):
                dfs(root.right,d)
            d[root.val]-=1

        dfs(root,{})
        return ans


# Places Amazon Alexa skill developed by Austin Patel

# TODO: handle case in which user just says query without 'nearby'

from urllib.request import Request, urlopen
from urllib.error import HTTPError
from json import loads

# Decrypt code should run once and variables stored outside of the function
# handler so that these are decrypted once per container
# TODO: Fix encryption
from base64 import b64decode
import boto3
import os

# TODO: remove key

# PLACES_API_KEY = boto3.client('kms').decrypt(CiphertextBlob=b64decode(os.environ['places_api_key']))['Plaintext']
# GEOCODING_API_KEY = boto3.client('kms').decrypt(CiphertextBlob=b64decode(os.environ['geocoding_api_key']))['Plaintext']
PLACES_API_KEY, GEOCODING_API_KEY = os.environ['places_api_key'], os.environ['geocoding_api_key']

# Variables
START_QUESTION = 'What would you like to search for nearby?'
HELP_QUESTION = START_QUESTION
NO_QUESTION = ''

HELP_SPEECH = 'This skil will tell you what places are nearby.  Make sure you have a location entered in the Alexa app and allow the location permission.  For example, you could say nearby pizza.  Make sure to say nearby when using this skill.'
UNEXPECTED_ANSWER_SPEECH = 'I was not expecting you to say that right now.'
WELCOME_SPEECH = 'You can ask this skill for nearby places.  For example you can say nearby pizza.'
REQUEST_PERMISSION_SPEECH = 'This Alexa skill does not have permission to access your location, which ' \
                            'is required for this skill to function.  Please go to the Alexa app to enable the ' \
                            'location permission.'
NO_ADDRESS_AVAILABLE_SPEECH = 'You have granted this skill permission to access your location, however ' \
                              'you have not entered a location in the alexa app.  In the settings of the Alexa ' \
                              'app select your Alexa enabled device and add a street address.'
BAD_RESPONSE_CODE_SPEECH = 'This skill was unable to receive data correctly from Google or Amazon '
NEARBY_SPEECH = 'Places close to you are '
NO_PLACES_FOUND_SPEECH = 'No places were found for the query '
NO_SLOT_GIVEN_SPEECH = 'Please include a search query in your request.'

START_INTENT, NEARBY_INTENT = 'Start', 'Nearby'

HELP_INTENT, CANCEL_INTENT, STOP_INTENT = 'AMAZON.HelpIntent', 'AMAZON.CancelIntent', 'AMAZON.StopIntent'
LAUNCH_REQUEST, INTENT_REQUEST, SESSION_ENDED_REQUEST = 'LaunchRequest', 'IntentRequest', 'SessionEndedRequest'

ADDRESS_PERMISSION = 'read::alexa:device:all:address'

FOOD_TYPE_SLOT_NAME = 'FoodType'

OK_STATUS_CODE = 'OK'
ZERO_RESULTS_STATUS_CODE = 'ZERO_RESULTS'
OVER_QUERY_LIMIT_STATUS_CODE = 'OVER_QUERY_LIMIT'
REQUEST_DENIED_STATUS_CODE = 'REQUEST_DENIED'
INVALID_REQUEST_STATUS_CODE = 'INVALID_REQUEST'

NEARBY_PLACE_LIMIT = 5

lat, lng = None, None
event = None
last_question = NO_QUESTION

# Initialization
def on_session_start():
    """ Initializes session """

# Permissions
def request_permission(name, speech_request_response):
    speech_request_response['response']['card'] = {
        "type": "AskForPermissionsConsent",
        "permissions": [
            name
        ]
    }

    return speech_request_response

# Alexa helpers
def get_slot(slot_name):
    """ Finds a value in a given slot """
    return event['request']['intent']['slots'][slot_name]['value']

# Speech
def say(output='', reprompt_text='', title='', should_end_session=True):
    """ Builds a spoken response and will end the session by default """
    output = str(output)
    if reprompt_text == '':
        reprompt_text = output

    return {
        'version': '1.0',
        'sessionAttributes': {},
        'response': {
            'outputSpeech': {
                'type': 'PlainText',
                'text': output
            },
            'reprompt': {
                'outputSpeech': {
                    'type': 'PlainText',
                    'text': reprompt_text
                }
            },
            'shouldEndSession': should_end_session
        }
    }

def question(question_base, extension='', intro=''):
    """ Asks a question and prepares the users response """
    if extension != '':
        extension = ' ' + str(extension)

    question_text = question_base + extension + '?'

    return say(output=intro + ' ' + question_text, reprompt_text=question_text, should_end_session=False)

def welcome():
    """ Welcomes the user with a message and randomly picks a question to ask the user about their number """
    on_session_start()
    return question(START_QUESTION, intro=WELCOME_SPEECH)

def help():
    """ Provides the user with help based on where they are in the program """
    return question(HELP_QUESTION, intro=HELP_SPEECH)

def end():
    """ Terminates the current session """
    return say()

def get_missing_slot():
    """ Support for dialog directives """
    return {
        "response": {
            "directives": [
                {
                    "type": "Dialog.Delegate"
                }
            ],
            "shouldEndSession": False
        },
        "sessionAttributes": {}
    }

# Program specific logic
def question_answer(response):
    """ Handles the users response to a given question and the scenario in which this
    intent is called even though there was no question given

    Parameters:
        response: a string that is the name of an intent
    """
    return say(UNEXPECTED_ANSWER_SPEECH)

def location_manager():
    """ Send permission card to user if they have not given permission to use location """
    if 'permissions' not in event['context']['System']['user']:
        event['context']['System']['user']['permissions'] = {}
    if 'consentToken' not in event['context']['System']['user']['permissions']:
        return request_permission(ADDRESS_PERMISSION, say(REQUEST_PERMISSION_SPEECH, should_end_session=True))

    try:
        address = get_address()
    except HTTPError as e:
        return say(BAD_RESPONSE_CODE_SPEECH + str(e))

    if not address:
        return say(NO_ADDRESS_AVAILABLE_SPEECH)

    try:
        global lat, lng
        lat, lng = get_lat_long(address)
    except HTTPError as e:
        return say(BAD_RESPONSE_CODE_SPEECH + str(e))

    # don't return anything if program successfully got location

# TODO: Handle all response codes
def get_address():
    """ Returns the users address pulled from amazon servers, returns None
    if user has not entered address into the Alexa app and raises an error if
    the response from the server had a bad reponse code. """

    # get user tokens
    consent_token = event['context']['System']['user']['permissions']['consentToken']
    device_id = event['context']['System']['device']['deviceId']
    url = 'https://api.amazonalexa.com/v1/devices/{}/settings/address'.format(device_id)

    # query api
    request = Request(url)
    request.add_header('Authorization', 'Bearer ' + consent_token)
    opened = urlopen(request)

    if opened.getcode() != 200: # Bad response code
        raise HTTPError(opened.getcode())

    response = loads(opened.read())

    if not response['addressLine1']: # Address not entered
        return None

    return ', '.join([response['addressLine1'], response['city'], response['stateOrRegion']])

# TODO: Handle all response codes
def get_lat_long(address):
    """ Convert address into latitude and longitude """
    address = address.replace(' ', '+')

    url = 'https://maps.googleapis.com/maps/api/geocode/json?address={}&key={}'.format(address, GEOCODING_API_KEY)
    response = loads(urlopen(Request(url)).read())

    if response['status'] != OK_STATUS_CODE:
        raise HTTPError(response['status'])

    location = response['results'][0]['geometry']['location']

    return location['lat'], location['lng']

# TODO: handle all response codes
def get_nearby_places(query):
    """ Uses latitude and longitude to get nearby places based on a query """
    query = query.replace(' ', '+')
    url = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={},{}&radius=10000&keyword={}&key={}'.format(lat, lng, query, PLACES_API_KEY)

    response = loads(urlopen(Request(url)).read())

    if response['status'] != OK_STATUS_CODE and response['status'] != ZERO_RESULTS_STATUS_CODE:
        raise HTTPError(response['status'])

    # Return the unique names of nearby places
    return list(set([place['name'] for place in response['results']]))

def nearby_information():
    """ Handles intent for when user asks 'nearby ...' such as 'nearby pizza'
    by saying information about nearby places """
    try:
        slot_value = get_slot(FOOD_TYPE_SLOT_NAME)

        places = get_nearby_places(slot_value)
    except HTTPError as e:
        return say(BAD_RESPONSE_CODE_SPEECH + str(e))
    except KeyError:
        return say(NO_SLOT_GIVEN_SPEECH)

    if len(places) == 0:
        return say(NO_PLACES_FOUND_SPEECH + get_slot(FOOD_TYPE_SLOT_NAME))

    result = ''
    for place in places[:NEARBY_PLACE_LIMIT]:
        result += place + ', '

    return say(NEARBY_SPEECH + result)

# Event handlers and related variables
def handle_intent():
    """ Called when the user specifies an intent for this skill """
    intent_name = event['request']['intent']['name']

    # Handle partially completed intent
    if (event['request']['dialogState'] == 'STARTED' or event['request']['dialogState'] == 'IN_PROGRESS'):
        return get_missing_slot()

    if intent_name in name_to_handler:
        return name_to_handler[intent_name]()
    else:
        return question_answer(intent_name)

def lambda_handler(event_dict, context):
    """ Route the incoming request based on type (LaunchRequest, IntentRequest,
    etc.) The JSON body of the request is provided in the event parameter.
    """
    global event
    event = event_dict

    # Let the location getting process override behavior
    location_response = location_manager()
    if location_response != None:
        return location_response

    if event['session']['new']:
        on_session_start()

    print(event)

    return request_to_handler[event['request']['type']]()

name_to_handler = {HELP_INTENT: help,
                   CANCEL_INTENT: end,
                   STOP_INTENT: end,
                   START_INTENT: welcome,
                   NEARBY_INTENT: nearby_information}

request_to_handler = {LAUNCH_REQUEST: welcome,
                      INTENT_REQUEST: handle_intent,
                      SESSION_ENDED_REQUEST: end}


'''
(This problem is an interactive problem.)

You may recall that an array arr is a mountain array if and only if:

arr.length >= 3
There exists some i with 0 < i < arr.length - 1 such that:
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]
arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
Given a mountain array mountainArr, return the minimum index such that mountainArr.get(index) == target. If such an index does not exist, return -1.

You cannot access the mountain array directly. You may only access the array using a MountainArray interface:

MountainArray.get(k) returns the element of the array at index k (0-indexed).
MountainArray.length() returns the length of the array.
Submissions making more than 100 calls to MountainArray.get will be judged Wrong Answer. Also, any solutions that attempt to circumvent the judge will result in disqualification.

Example 1:

Input: array = [1,2,3,4,5,3,1], target = 3
Output: 2
Explanation: 3 exists in the array, at index=2 and index=5. Return the minimum index, which is 2.
Example 2:

Input: array = [0,1,2,4,2,1], target = 3
Output: -1
Explanation: 3 does not exist in the array, so we return -1.

Constraints:

3 <= mountain_arr.length() <= 104
0 <= target <= 109
0 <= mountain_arr.get(index) <= 109
'''
import unittest
from bisect import bisect

class MountainArray:
   def get(self, index: int) -> int:
       pass

   def length(self) -> int:
       pass

class Solution:
    def findInMountainArray(self, target: int, mountain_arr: 'MountainArray') -> int:
        # find the montain
        start = 0
        end = mountain_arr.length() - 1
        while start < end:
            middle = (start + end) // 2
            middle_prev = middle - 1
            middle_next = middle + 1
            middle_v = mountain_arr.get(middle)
            if middle_v < mountain_arr.get(middle_next):
                start = middle_next
            elif middle_v > mountain_arr.get(middle_prev):
                # montain
                start = middle
                break
            else:
                end = middle - 1
        mountain = mountain_arr.get(start)

        left = mountain_arr.get(0)
        right = mountain_arr.get(mountain_arr.length() - 1)
        if target < min(left, right) or target > mountain:
            return -1
        elif target == left:
            return 0
        elif target == mountain:
            return start
        else:
            # find in left
            ans_left = self.find(target, mountain_arr, 1, start - 1, True)
            if ans_left != -1:
                return ans_left
            # find in right
            ans_right = self.find(target, mountain_arr, start + 1, mountain_arr.length() - 1, False)
            if ans_right != -1:
                return ans_right
            return -1

    def find(self, target: int, mountain_arr: 'MountainArray', start, end, asc):
        if start > end:
            return -1

        while start < end:
            middle = (start + end) // 2
            middle_v = mountain_arr.get(middle)
            if middle_v < target:
                if asc:
                    start = middle + 1
                else:
                    end = middle - 1
            elif middle_v == target:
                return middle
            else:
                if asc:
                    end = middle - 1
                else:
                    start = middle + 1
        return start if mountain_arr.get(start) == target else -1

Leetcode/1095. Find in Mountain Array.py


import os
import numpy as np
import scipy.io as sio
import json

if __name__ == '__main__':
    in_path = '/data01/kalviny/dataset/MOT/2015/imglist/'
    out_path = '/data01/kalviny/dataset/MOT/2015/gt_box/'
    with open(os.path.join(in_path, 'train.json'), 'r') as f:
        data = json.load(f)
        for item in data:
            v_name = item['img'].split('_')[0]
            im_name = item['img'].split('_')[1]
            bbox = item['gt']
            bbox = np.array(bbox, dtype=np.float32)
            if (len(bbox)): bbox = bbox[:, 0:4]
            out_dir = os.path.join(out_path, v_name)
            if not os.path.exists(out_dir): os.mkdir(out_dir)
            sio.savemat(os.path.join(out_dir, '{}.mat'.format(im_name)), {'boxes': bbox})


class model():
	#recibe URL to be consummed in order to bring back a response
	def __init__(self,id):
		self.id = id

	def getData(self):
		#emulate the response from DB
		if self.id=="1":
			data = {}
			data['nombre'] = 'Jose'
			data['edad'] = '15'
			data['nacionalidad'] = 'Mex'
		else:
			data="null"
		#response raw response to the controller
		return data


import requests, re
from bs4 import BeautifulSoup

data=requests.get("https://www.lego.com/en-us/product/the-razor-crest-75331").content
soup=BeautifulSoup(data, 'html.parser')
span=soup.find("h1", {"class":"Markup__StyledMarkup-sc-nc8x20-0 dbPAWk"})
title=span.text
span=soup.find("span", {"class":"Text__BaseText-sc-13i1y3k-0 zkrlj ProductPricestyles__StyledText-sc-vmt0i4-0 tMWye"})
price=span.text
print("Item %s has a price of $ %s" % (title, price))


from vk_api.keyboard import VkKeyboard, VkKeyboardColor

start = VkKeyboard(one_time=False, inline=False)
start.add_button(label='Начать', color=VkKeyboardColor.POSITIVE)

keyboard_city_aprove = VkKeyboard(one_time=False, inline=True)
keyboard_city_aprove.add_button(label='Да', color=VkKeyboardColor.PRIMARY)
keyboard_city_aprove.add_button(label='Нет', color=VkKeyboardColor.PRIMARY)

main_menu = VkKeyboard(one_time=False, inline=False)
main_menu.add_button(label='Погода', color=VkKeyboardColor.PRIMARY)
main_menu.add_button(label='Пробка', color=VkKeyboardColor.PRIMARY)
main_menu.add_line()
main_menu.add_button(label='Афиша', color=VkKeyboardColor.PRIMARY)
main_menu.add_button(label='Валюта', color=VkKeyboardColor.PRIMARY)
main_menu.add_line()
main_menu.add_button(label='Изменить город', color=VkKeyboardColor.SECONDARY)

weather = VkKeyboard(one_time=False, inline=True)
weather.add_button(label='Погода на сегодня', color=VkKeyboardColor.PRIMARY)
weather.add_line()
weather.add_button(label='Погода на завтра', color=VkKeyboardColor.PRIMARY)

afisha = VkKeyboard(one_time=False, inline=True)
afisha.add_button(label='Афиша на сегодня', color=VkKeyboardColor.PRIMARY)
afisha.add_line()
afisha.add_button(label='Афиша на завтра', color=VkKeyboardColor.PRIMARY)

# # №1. Клавиатура с 3 кнопками: "показать всплывающее сообщение", "открыть URL" и изменить меню (свой собственный тип)
# keyboard_1 = VkKeyboard(one_time=False, inline=True)
# # pop-up кнопка
# keyboard_1.add_callback_button(label='Покажи pop-up сообщение', color=VkKeyboardColor.SECONDARY, payload={"type": "show_snackbar", "text": "Это исчезающее сообщение"})
# keyboard_1.add_line()
# # кнопка с URL
# keyboard_1.add_callback_button(label='Откртыть Url', color=VkKeyboardColor.POSITIVE, payload={"type": "open_link", "link": "https://vk.com/dev/bots_docs_5"})
# keyboard_1.add_line()
# # кнопка по открытию ВК-приложения
# keyboard_1.add_callback_button(label='Открыть приложение', color=VkKeyboardColor.NEGATIVE, payload={"type": "open_app", "app_id": 'APP_ID', "owner_id": 'OWNER_ID', "hash": "anything_data_100500"})
# keyboard_1.add_line()
# # кнопка переключения на 2ое меню
# keyboard_1.add_callback_button(label='Добавить красного ', color=VkKeyboardColor.PRIMARY, payload={"type": "my_own_100500_type_edit"})

# # №2. Клавиатура с одной красной callback-кнопкой. Нажатие изменяет меню на предыдущее.
# keyboard_2 = VkKeyboard(one_time=False, inline=True)
# # кнопка переключения назад, на 1ое меню.
# keyboard_2.add_callback_button('Назад', color=VkKeyboardColor.SECONDARY, payload={"type": "my_own_100500_type_edit"})


def count_word(word: str, word_list: list) -> int:
    head, *tail = word_list
    value = int(head == word) # 1 if appear and 0 if it does not.
    last_item = len(tail) == 0
    return value if last_item else value + count_word(word, tail)

def analyze_sentence(sentence: str) -> list:
    sentence = ''.join(c.lower() for c in sentence if c.isalpha() or c == ' ') # Filter only letters and transform to lower.
    word_list = sentence.split()
    output = [] # [{'word': 'foo', 'counter': 2}]
    for w in word_list:
        if w not in [o['word'] for o in output]:
            output.append({
                'word': w,
                'counter': count_word(w, word_list)
            })

    return output

if __name__ == '__main__':
    # sentence = 'hola como como estas'
    # sentence = 'hola hola como                 estas hola como      como      estas     estas hola   hola como   como      '
    sentence = 'Hi how are      things? how are you?. Are you a developer?   I am  also a  developer'
    # sentence = '  h'
    # sentence = '.1.1. . .1.1. .1. 1.1 ..1 ¡¡  $%$b bb·bbbbb % b ·b /bb%b&/b(%&g/$"·%·aa$&·&$1231 '

    output = analyze_sentence(sentence)

    print(f"Sentence: {sentence} \n")
    for o in output:
        print(f"Word: {o['word']}, appears: {o['counter']} times.")


from System import System
import numpy as np

class Solution:

    def __init__(self, system: System):
        self.system = system

    def build_y_matrix(self):
        self.y_matrix = np.zeros((len(self.system.buses), len(self.system.buses)), dtype='complex')
        self.bus_order = list()

        for element_name, element in self.system.y_matrix.items():

            for row in element.buses:
                for col in element.buses:
                    index_row = self.system.buses[row].index
                    index_col = self.system.buses[col].index

                    self.y_matrix[index_row, index_col] = self.y_matrix[index_row, index_col] + element.y.loc[row, col]

    def print_y_matrix(self):
        print("Y-bus matrix:")
        i = 0
        while i < len(self.system.buses):
            j = 0
            print("\nRow " + str(i + 1))
            while j < len(self.system.buses):
                print(self.y_matrix[i][j])
                j += 1
            i = i + 1


# chat reading

import string

def get_user(line):
	separate = line.split(":", 2)
	user = separate[1].split("!", 1)[0]
	return user.strip()

def get_message(line):
	global message
	try:
		message = (line.split(":", 2))[2]
	except:
		message = ""
	return message.strip()

def get_command(message):
	try:
		cmd = message.split(" ", 2)[0]
	except:
		cmd = ""
	return cmd.strip()

def get_argument(message):
	try:
		arg = message.split(" ", 2)[1]
	except:
		arg = ""
	return arg.strip()

def get_text(message):
	try:
		text = message.split(" ")
		del text[0:2]
		result = ' '.join(text)
	except:
		result = ""
	return result.strip()

def get_quote(message):
	try:
		quote = message.split(" ")
		del quote[0:1]
		quote = ' '.join(quote)
	except:
		quote = ""
	print ("Quote was set to {}".format(quote))
	return quote.strip()

def parse_character(message):
	try:
		player = message.split(" ")
		del player[0:1]
	except:
		player = ""
	print ("Player information parsed.")
	return player.strip()

# def message_handler(message):


import numpy as np
from matplotlib import pyplot as plt
from matplotlib import cm
from matplotlib.patches import Patch
from class_and_func.hawkes_process import exp_thinning_hawkes
from matplotlib import rcParams
import pickle

rcParams['font.family'] = 'serif'

#########
## This script allows to obtain the boxplots that appear in our paper.
## SavedBoxplots files are generated through the error_and_pvalue.py file.
## This file requires pickle.

if __name__ == "__main__":

    prefixed = 6  # Number of parameter sets.

    box = []
    positions1 = []
    positions2 = []
    proportion_list = []
    theta_list = []

    ## Extraction of data

    for file_nb in range(prefixed):
        with open("SavedBoxplots/SavedBoxplots" + str(file_nb+1), 'rb') as saved:

            lambda_0, alpha, beta, max_jumps, iterations, box_approx, box_real, proportion = pickle.load(saved)
            box += [(box_real, box_approx)]

            ## Positions of boxplots
            positions1 += [8 * file_nb + i * 1.1 for i in range(0, prefixed, 2)]
            positions2 += [8 * file_nb + i * 1.1 + 1.0 for i in range(0, prefixed, 2)]
            theta_list += [[lambda_0, alpha, beta]]
            proportion_list += [proportion]

    ## Ordering by increasing proportion of time equal to 0.
    proportion_list = np.array(proportion_list)
    order = np.argsort(proportion_list)
    theta_list = np.array(theta_list)[order, :]
    proportion_list = proportion_list[order]
    box = np.array(box)[order]
    print("Parameter sets: ", theta_list)

    ## Box, positions, labels and colors.
    final_box = np.zeros((100, 1))
    for i in range(2):
        for j in range(len(order)):
            final_box = np.concatenate((final_box, box[j, i, :, :]), axis=1)

    final_box = final_box[:, 1:]

    positions = positions1 + positions2

    label = ["$\lambda_0^{exact}$", "$\\alpha^{exact}$", "$\\beta^{exact}$"]
    labels = [i for j in range(proportion_list.shape[0]) for i in label]
    label = ["$\lambda_0^{approx}$", "$\\alpha^{approx}$", "$\\beta^{approx}$"]
    labels += [i for j in range(proportion_list.shape[0]) for i in label]

    cmap = cm.get_cmap('tab20b')
    colors = [cmap(0.775), cmap(0.175)]

    ## Figure and boxplot

    fig = plt.figure(figsize=[1.5 * 5.3, 1.5 * 3.5], constrained_layout=True)
    gs = fig.add_gridspec(5, prefixed)

    ax = fig.add_subplot(gs[0:4, :])  # Boxplots.

    intensity_ax = []  # Intensity functions.
    for i in range(prefixed):
        intensity_ax += [fig.add_subplot(gs[4:, i])]

    box_plot = ax.boxplot(final_box, positions=positions, labels=labels, patch_artist=True)

    for i, patch in enumerate(box_plot['boxes']):
        patch.set_facecolor(colors[i // (len(box_plot['boxes']) // 2)])

    empty_string_labels = ['' for item in ax.get_xticklabels()]
    ax.set_xticklabels(empty_string_labels)
    parameters_str = ["$\lambda_0$", "$\\alpha$", "$\\beta$"]

    y_lim = ax.set_ylim(bottom=1e-5, top=1e4)

    for i in range(len(positions1)):
        ax.annotate('', xy=(positions1[i] - 0.1, 5e-6), xytext=(positions2[i] + 0.1, 5e-6),
                    arrowprops=dict(arrowstyle='|-|,widthA=0.3,widthB=0.3', facecolor='red'),
                    annotation_clip=False)  # This enables the arrow to be outside of the plot

        ax.annotate(parameters_str[i % 3], xy=(positions1[i] - 0.1, 1.5e-6), xytext=(positions2[i] - 0.8, 1.5e-6),
                    annotation_clip=False)

    legend_str = ['exact', "approx"]
    legend_elements = [Patch(facecolor=colors[i], edgecolor='k', label=legend_str[i]) for i in range(2)]
    ax.legend(handles=legend_elements, loc='best')

    ax.grid()
    ax.set_yscale('log')

    ## Plot intensity examples for each parameter set.

    for i in range(prefixed):
        np.random.seed(1)
        hawkes = exp_thinning_hawkes(theta_list[i, 0], theta_list[i, 1], theta_list[i, 2], max_jumps=max_jumps)
        hawkes.simulate()
        hawkes.plot_intensity(ax=intensity_ax[i], plot_N=False)
        intensity_ax[i].set_xlim(0, 20)
        intensity_ax[i].legend().set_visible(False)
        intensity_ax[i].set_title(str(round(100 * proportion_list[i], 4)) + "%")

    intensity_ax[0].set_ylim(0.475, 0.5001)

    ## Save image as pdf.
    # plt.savefig('intensity_boxplot_log_cropped.pdf', bbox_inches='tight', format="pdf")

    plt.show()


import soundfile as sf
import torch
import os
from scipy.io.wavfile import write
import numpy as np
import librosa
from scipy.special import expi
import librosa.display
import matplotlib.pyplot as plt
MAX_WAV_VALUE = 32768.0

class Accumulator:
    def __init__(self, noisy_audio, low_cut, high_cut, nfft, center, residual, sr, bandpass):
        self.low_cut = low_cut
        self.high_cut = high_cut
        self.center = center
        self.nfft = nfft
        self.stft_noisy = torch_stft(noisy_audio, nfft=nfft, center=center)
        self.residual = residual
        self.sr = sr
        self.bandpass = bandpass
        self.stft_noisy_filt = self.filter_stft()
        self.stft_diff_sum = np.zeros(self.stft_noisy.shape)

    def filter_stft(self):
        stft_full = self.stft_noisy + 0
        stft_full[:self.bandpass, :] = 0 * stft_full[:self.bandpass, :]  # reduce low frequencies
        stft_full[-self.bandpass // 3:, :] = 0 * stft_full[-self.bandpass // 3:, :]  # reduce high frequencies
        return stft_full

    def sum_difference(self, stft, iter_num):
        if iter_num < 48:
            self.stft_prev, self.stft_minus = stft, stft
        else:
            self.stft_minus = np.abs(stft - self.stft_prev)/(stft+np.finfo(float).eps)
            self.stft_diff_sum += self.stft_minus
            self.stft_diff_sum[self.stft_diff_sum < np.percentile(self.stft_diff_sum, self.low_cut)] = \
                np.percentile(self.stft_diff_sum, self.low_cut)
            self.stft_diff_sum[self.stft_diff_sum > np.percentile(self.stft_diff_sum, self.high_cut)] = \
                np.percentile(self.stft_diff_sum, self.high_cut)
            self.stft_prev = stft

    def create_atten_map(self):
        max_mask = self.stft_diff_sum.max()
        min_mask = self.stft_diff_sum.min()
        atten_map = (max_mask - self.stft_diff_sum) / (max_mask - min_mask)
        atten_map[atten_map < self.residual] = self.residual
        self.atten_map = atten_map

    def mmse_lsa(self):
        gamma_mat = (1 - self.atten_map) ** 2
        gamma_mat[gamma_mat < 10**-10] = 10**-10
        gamma_mat = 1 / gamma_mat
        lsa_mask = np.zeros(gamma_mat.shape)
        for it, gamma in enumerate(gamma_mat.transpose()):
            eta = gamma - 1
            eta[eta < self.residual] = self.residual
            v = gamma * eta / (1 + eta)
            gain = np.ones(gamma.shape)
            idx = v > 5
            gain[idx] = eta[idx] / (1 + eta[idx])
            idx = np.logical_and(v <= 5, v > 0)  # and v > 0
            gain[idx] = eta[idx] / (1 + eta[idx]) * np.exp(0.5 * -expi(-v[idx]))
            gain[gain > 1] = 1
            lsa_mask[:, it] = gain
        self.lsa_mask = lsa_mask

    def show_lsa(self):
        plot_stft(self.lsa_mask, 'LSA Mask')

    def show_wiener(self):
        plot_stft(self.lsa_mask, 'Wiener Mask')

    def show_diff_accum(self):
        plot_stft(self.stft_diff_sum, 'Accumulator')

    def show_diff_stft(self):
        plot_stft(self.stft_minus, 'Abs Difference')

    def show_noisy(self):
        plot_stft(np.abs(self.stft_noisy), 'Power Spectrogram of Noisy Sample')

def plot_stft(D, title=''):
    librosa.display.specshow(librosa.amplitude_to_db(D, ref=np.max), y_axis='log')
    plt.title(title)
    plt.colorbar(format='%+2.0f dB')
    plt.tight_layout()
    plt.show()

def load_wav_to_torch(full_path):
    """
    Loads wavdata into torch array
    """
    data, sampling_rate = sf.read(full_path, dtype='int16')
    if len(data.shape) > 1:
        print('Mixing the input wav to a mono file')
        data = data.mean(axis=1)
    return torch.FloatTensor(data.astype(np.float32)), sampling_rate

def write_norm_music(input, filename, sr):
    audio = MAX_WAV_VALUE * input
    wavdata = audio.astype('int16')
    write(filename, sr, wavdata)

def torch_stft(audio, nfft=2048, center=True):
    in_numpy = audio.clone().detach()
    in_numpy = in_numpy.detach().cpu().numpy()
    stft = librosa.stft(in_numpy, n_fft=nfft, center=center)
    return stft

def write_music_stft(stft, filename, sr, center=True):
    in_numpy = librosa.istft(stft, center=center)
    write_norm_music(in_numpy, filename, sr)

def makedirs(outputs_dir):
    if not os.path.exists(outputs_dir):
        print("Creating directory: {}".format(outputs_dir))
        os.makedirs(outputs_dir)


import io
import os
import shutil
import tarfile
import traceback
from abc import ABC, abstractmethod
from typing import Dict

import fsspec
import nbconvert
import nbformat
from nbconvert.preprocessors import CellExecutionError, ExecutePreprocessor

from jupyter_scheduler.models import DescribeJob, JobFeature, Status
from jupyter_scheduler.orm import Job, create_session
from jupyter_scheduler.parameterize import add_parameters
from jupyter_scheduler.utils import get_utc_timestamp

class ExecutionManager(ABC):
    """Base execution manager.
    Clients are expected to override this class
    to provide concrete implementations of the
    execution manager. At the minimum, subclasses
    should provide implementation of the
    execute, and supported_features methods.
    """

    _model = None
    _db_session = None

    def __init__(self, job_id: str, root_dir: str, db_url: str, staging_paths: Dict[str, str]):
        self.job_id = job_id
        self.staging_paths = staging_paths
        self.root_dir = root_dir
        self.db_url = db_url

    @property
    def model(self):
        if self._model is None:
            with self.db_session() as session:
                job = session.query(Job).filter(Job.job_id == self.job_id).first()
                self._model = DescribeJob.from_orm(job)
        return self._model

    @property
    def db_session(self):
        if self._db_session is None:
            self._db_session = create_session(self.db_url)

        return self._db_session

    def process(self):
        """The template method called by the
        Scheduler, backend implementations
        should not override this method.
        """
        self.before_start()
        try:
            self.execute()
        except CellExecutionError as e:
            self.on_failure(e)
        except Exception as e:
            self.on_failure(e)
        else:
            self.on_complete()

    @abstractmethod
    def execute(self):
        """Performs notebook execution,
        custom backends are expected to
        add notebook execution logic within
        this method
        """
        pass

    @classmethod
    @abstractmethod
    def supported_features(cls) -> Dict[JobFeature, bool]:
        """Returns a configuration of supported features
        by the execution engine. Implementors are expected
        to override this to return a dictionary of supported
        job creation features.
        """
        pass

    @classmethod
    def validate(cls, input_path: str) -> bool:
        """Returns True if notebook has valid metadata to execute, False otherwise"""
        return True

    def before_start(self):
        """Called before start of execute"""
        job = self.model
        with self.db_session() as session:
            session.query(Job).filter(Job.job_id == job.job_id).update(
                {"start_time": get_utc_timestamp(), "status": Status.IN_PROGRESS}
            )
            session.commit()

    def on_failure(self, e: Exception):
        """Called after failure of execute"""
        job = self.model
        with self.db_session() as session:
            session.query(Job).filter(Job.job_id == job.job_id).update(
                {"status": Status.FAILED, "status_message": str(e)}
            )
            session.commit()

        traceback.print_exc()

    def on_complete(self):
        """Called after job is completed"""
        job = self.model
        with self.db_session() as session:
            session.query(Job).filter(Job.job_id == job.job_id).update(
                {"status": Status.COMPLETED, "end_time": get_utc_timestamp()}
            )
            session.commit()

class DefaultExecutionManager(ExecutionManager):
    """Default execution manager that executes notebooks"""

    def execute(self):
        job = self.model

        with open(self.staging_paths["input"], encoding="utf-8") as f:
            nb = nbformat.read(f, as_version=4)

        if job.parameters:
            nb = add_parameters(nb, job.parameters)

        ep = ExecutePreprocessor(
            kernel_name=nb.metadata.kernelspec["name"],
            store_widget_state=True,
        )

        try:
            ep.preprocess(nb)
        except CellExecutionError as e:
            raise e
        finally:
            for output_format in job.output_formats:
                cls = nbconvert.get_exporter(output_format)
                output, resources = cls().from_notebook_node(nb)
                with fsspec.open(self.staging_paths[output_format], "w", encoding="utf-8") as f:
                    f.write(output)

    def supported_features(cls) -> Dict[JobFeature, bool]:
        return {
            JobFeature.job_name: True,
            JobFeature.output_formats: True,
            JobFeature.job_definition: False,
            JobFeature.idempotency_token: False,
            JobFeature.tags: False,
            JobFeature.email_notifications: False,
            JobFeature.timeout_seconds: False,
            JobFeature.retry_on_timeout: False,
            JobFeature.max_retries: False,
            JobFeature.min_retry_interval_millis: False,
            JobFeature.output_filename_template: False,
            JobFeature.stop_job: True,
            JobFeature.delete_job: True,
        }

    def validate(cls, input_path: str) -> bool:
        with open(input_path, encoding="utf-8") as f:
            nb = nbformat.read(f, as_version=4)
            try:
                nb.metadata.kernelspec["name"]
            except:
                return False
            else:
                return True

class ArchivingExecutionManager(DefaultExecutionManager):
    """Execution manager that archives all output files in and under the
    output directory into a single archive file

    Notes
    -----
    Should be used along with :class:`~jupyter_scheduler.scheduler.ArchivingScheduler`
    as the `scheduler_class` during jupyter server start.
    """

    def execute(self):
        job = self.model

        with open(self.staging_paths["input"], encoding="utf-8") as f:
            nb = nbformat.read(f, as_version=4)

        if job.parameters:
            nb = add_parameters(nb, job.parameters)

        ep = ExecutePreprocessor(
            kernel_name=nb.metadata.kernelspec["name"],
            store_widget_state=True,
        )

        # Get the directory of the input file
        local_staging_dir = os.path.dirname(self.staging_paths["input"])
        # Directory where side-effect files are written
        run_dir = os.path.join(local_staging_dir, "files")
        os.mkdir(run_dir)

        try:
            ep.preprocess(nb, {"metadata": {"path": run_dir}})
        except CellExecutionError as e:
            pass
        finally:
            # Create all desired output files, other than "input" and "tar.gz"
            for output_format in job.output_formats:
                if output_format == "input" or output_format == "tar.gz":
                    pass
                else:
                    cls = nbconvert.get_exporter(output_format)
                    output, resources = cls().from_notebook_node(nb)
                    f = open(self.staging_paths[output_format], "wb")
                    f.write(bytes(output, "utf-8"))
                    f.close()

            # Create an archive file of the staging directory for this run
            # and everything under it
            fh = io.BytesIO()
            with tarfile.open(fileobj=fh, mode="w:gz") as tar:
                for root, dirs, files in os.walk(local_staging_dir):
                    for file in files:
                        # This flattens the directory structure, so that in the tar
                        # file, output files and side-effect files are side-by-side
                        tar.add(os.path.join(root, file), file)

            archive_filepath = self.staging_paths["tar.gz"]
            with fsspec.open(archive_filepath, "wb") as f:
                f.write(fh.getvalue())

            # Clean up the side-effect files in the run directory
            shutil.rmtree(run_dir)


# importing Libraries
import random

# Check if clause is satisfied or not
def check_satisfied_clause(clause, assignment):

   return any ( assignment[symbol] for symbol in clause )

# Count the no. of clauses satisfied if the symbol is flipped.
def count_satisfied_clauses(symbol, unsatisfied_clauses, assignment):

    assignment[symbol] = not assignment[symbol]
    n = sum(check_satisfied_clause(clause, assignment) for clause in unsatisfied_clauses)
    assignment[symbol] = not assignment[symbol]
    return n

def WalkSat ( clauses , probability , n_flips ):

    symbols = set()
    for clause in clauses:
     symbols.update(clause)
    symbols = list(symbols)
    assignment = { symbol : random.choice ([ True , False ]) for symbol in symbols }
    for i in range ( n_flips ):
        satisfied_clauses = [ clause for clause in clauses if check_satisfied_clause ( clause , assignment )]
        if len ( satisfied_clauses ) == len ( clauses ):
            return assignment
        unsatisfied_clauses = [ clause for clause in clauses if not check_satisfied_clause ( clause , assignment )]
        unsatisfied_clause = random.choice ( unsatisfied_clauses )
        if random.random () < probability:
            symbol = random.choice(unsatisfied_clause)
        else :
            symbol = max(symbols , key = lambda symbol : count_satisfied_clauses(symbol, unsatisfied_clauses, assignment))
        assignment[symbol] = not assignment[symbol]
    return None

def main():

    for i in range(10):

        # Generate random 3-CNF sentences
        m = random.randint(1, 10)
        n = random.randint(1, 10)
        clauses = []
        for j in range(m):
            clause = []
            for k in range(3):
                clause.append(random.randint(1, n))
            clauses.append(clause)
        print("---------------------------------------------------------------------------------------------")
        print("\nITERATION: ",i+1,"\n\nClauses: ", clauses)

        assignment = WalkSat(clauses, 0.5, 100)
        if assignment is None:
            print("# No solution")
        else:
            print("\nFinal Assignment: ", assignment)
            print("Satisfied clauses: \n", [clause for clause in clauses if check_satisfied_clause(clause, assignment)])

main()


# -*- coding: utf-8 -*-
"""
Created on Thu May 30 16:54:15 2019

@author: Tang
"""

'''
题目描述
将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能，但是string不符合数字要求时返回0)，
要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。

输入描述:
输入一个字符串,包括数字字母符号,可以为空

输出描述:
如果是合法的数值表达则返回该数字，否则返回0
 示例1

输入
复制
+2147483647
    1a33

输出
复制
2147483647
    0
'''
def solution(s):
     if len(s)==0 or s=='+' or s=='-':
          return 0
     flag=1
     if s[0]=='-':
          flag=-1
          s=s[1:]
     if s[0]=='+':
          flag=1
          s=s[1:]
     res=[]
     for i in s:
          if i in '1234567890':
               res.append(int(i))
          else:
               return 0

     sum=0
     j=len(res)
     for i in res:
          j-=1
          sum+=i*pow(10,j)
     return flag*sum
print(solution('-123'))

49.把字符串转换成整数.py


"""
overview:
    Actor-Critic法によるPendulumの学習を行う

args:
    各種パラメータ設定値は、本コード中に明記される

output:
    RESULT_PATH に以下の要素が出力される
        - options.csv: バッチ学習のパラメータ設定
        - history.csv: バッチごとの（報酬, 損失関数, TD 誤差）
        - history.png: バッチごとの（報酬, 損失関数, TD 誤差）の可視化

usage-example:
    python3 train.py
"""
import csv
from datetime import datetime
import os
import collections

import gym
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.utils import to_categorical

from agent.actor import Actor
from agent.critic import Critic

# 学習パラメータ
train_config = {
    'num_batches': 40000,
    'gamma': 0.99,
    'interval': 10000,
    'batch_size': 50,
    'multi_step_td':
        True  # True：複数ステップ TD 誤差を計算、False：１ステップ TD 誤差を計算
}

def now_str(str_format='%Y%m%d%H%M'):
    return datetime.now().strftime(str_format)

# バッチ学習のパラメータ設定をファイル出力する関数
def write_options(csv_path, train_config):
    with open(csv_path, 'w') as f:
        fieldnames = ['Option Name', 'Option Value']
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        data = [
            dict(zip(fieldnames, [k, v]))
            for k, v in train_config.items()
        ]
        writer.writerows(data)

# バッチ学習の metrics の推移を可視化する関数
def visualize_history(csv_path,
                      png_path,
                      metrics,
                      window_size=0.1):
    df = pd.read_csv(csv_path)

    if window_size < 1:
        window_size = max(int(len(df) * window_size), 1)
    else:
        window_size = int(window_size)

    rolled = df.rolling(window=window_size,
                        center=True).mean()
    fig = plt.figure(figsize=(12, 3 * len(metrics)))
    axes = fig.subplots(nrows=len(metrics), sharex=True)
    for i, metric in enumerate(metrics):
        ax = axes[i]
        rolled[metric].plot(ax=ax,
                            title=metric,
                            grid=True,
                            legend=True)
        ax.get_legend().remove()
    ax.set_xlabel('batch', fontsize=12)
    fig.suptitle(
        'window size is {0:d}.'.format(window_size))
    fig.savefig(png_path)
    plt.close(fig)

# 学習実行関数を定義
def train(train_config=train_config,
          actions_list=(-1, 1)):
    # 環境モデルの指定
    env = gym.make("Pendulum-v0")
    num_states = env.env.observation_space.shape[0]
    num_actions = len(actions_list)
    print('NUM_STATE_{}'.format(num_states))
    print('NUM_ACTIONS_{}'.format(num_actions))

    # actor と critic それぞれのインスタンスを作成
    actor = Actor(num_states=num_states,
                  actions_list=actions_list)
    critic = Critic(num_states=num_states)

    # 学習を実行
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        _train(sess, env, actor, critic, train_config,
               actions_list)

# バッチ TD 学習の実行関数
def _train(sess, env, actor, critic, train_config,
           actions_list):
    # 学習結果の出力先を指定
    result_dir = './result/{now_str}' \
                 .format(now_str=now_str(str_format='%Y%m%d_%H%M%S'))
    # 結果出力の保存先ディレクトリを作成
    os.makedirs(result_dir, exist_ok=True)
    print('result_dir_{}'.format(result_dir))

    # ログファイル名の指定
    csv_path = os.path.join(result_dir, 'history.csv')
    png_path = os.path.join(result_dir, 'history.png')
    opt_path = os.path.join(result_dir, 'options.csv')
    metrics = ['score', 'loss', 'loss_v']
    header = ['batch'] + metrics

    # バッチ学習の metrics を書き出す csv を作成
    with open(csv_path, 'w') as f:
        writer = csv.writer(f)
        writer.writerow(header)

    # バッチ学習パラメータの設定値を csv に出力
    write_options(opt_path, train_config)

    # バッチ学習パラメータの読み出し
    num_batches = train_config['num_batches']
    batch_size = train_config['batch_size']
    gamma = train_config['gamma']
    interval = train_config['interval']
    multi_step_td = train_config['multi_step_td']

    Step = collections.namedtuple(
        "Step", ["state", "act_onehot", "reward"])
    last_100_score = np.zeros(100)

    print('start_batches...')
    for i_batch in range(1, num_batches + 1):
        state = env.reset()
        batch = []
        score = 0
        steps = 0
        while True:
            steps += 1
            action = actor.predict(sess, state)
            act_onehot = to_categorical(
                actions_list.index(action),
                len(actions_list))
            state_new, reward, done, info = \
                env.step([action])

            # reward clipping
            if reward < -1:
                c_reward = -1
            else:
                c_reward = 1

            score += c_reward

            batch.append(
                Step(state=state,
                     act_onehot=act_onehot,
                     reward=c_reward))
            state = state_new

            if steps >= batch_size:
                break

        value_last = critic.predict(sess, state)[0][0]

        # １バッチ分のサンプリング終了後に TD 誤差を計算
        targets = []
        states = []
        act_onehots = []
        advantages = []
        target = value_last
        for t, step in reversed(list(enumerate(batch))):
            current_value = critic.predict(
                sess, step.state)[0][0]

            # １ステップ先の目標値、または複数ステップ先の目標値を計算
            if multi_step_td:
                target = step.reward + gamma * target
            else:
                target = step.reward + gamma * value_last
                value_last = current_value

            # アドバンテージ関数を１ステップ TD 誤差、
            # または複数ステップ TD 誤差として計算
            advantage = target - current_value
            targets.append([target])
            advantages.append([advantage])
            states.append(step.state)
            act_onehots.append(step.act_onehot)

        # Actor と Critic それぞれの損失関数を計算
        loss = actor.update(sess, states,
                            act_onehots,
                            advantages)
        loss_v = critic.update(sess, states, targets)

        # 直近 100 バッチの報酬および報酬平均を記録
        last_100_score[i_batch % 100] = score
        last_100_score_avg = sum(last_100_score) / min(
            i_batch, 100)

        batch_score_loss = [i_batch, score, loss, loss_v]

        # 学習のmetricを保存するcsvに結果の追記
        with open(csv_path, 'a') as f:
            writer = csv.writer(f)
            writer.writerow(batch_score_loss)

        # 100 ステップごとにログを書き出す
        if i_batch % 100 == 0:
            print(
                'batch_{} score_{} avg_loss_{} avg_td2_{} last100_score_{}'
                .format(i_batch, score, loss, loss_v,
                        last_100_score_avg))
            visualize_history(csv_path, png_path,
                              metrics)

        # 指定した間隔ごとに、actor の重み係数を保存
        if i_batch % interval == 0:
            model_path = os.path.join(
                result_dir,
                'batch_{}.h5'.format(i_batch))
            actor.model.save(model_path)

if __name__ == "__main__":
    train()


import random
from re import A
from tkinter import W

def hello_world():

  f = open("quotes.txt", 'a')
  L = "This is programaticly added qoute \n"
  f.write(L)
  f.close()

  f = open("quotes.txt")
  quotes = f.readlines()
  f.close()

  last = 21
  rnd1 = random.randint(0, last)
  rnd2 = random.randint(0, last)

  print (quotes[rnd1], end="")
  print (quotes[rnd2])

if __name__== "__main__":
  hello_world()


from django.shortcuts import render
from django.http import JsonResponse,HttpResponse
import pymysql
import json
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from arigonggan import models
from django.conf import settings
from apscheduler.schedulers.background import BackgroundScheduler
sched = BackgroundScheduler()
import datetime
now = datetime.datetime.now()
import time

@sched.scheduled_job('cron', hour='09', minute='00', name='disable1')
def seatChangeDisable():
    stime = "09:00:00"
    models.updateSeatDisable(stime)
    print(stime + " disable complete")

@sched.scheduled_job('cron', hour='10', minute='00', name='disable2')
def seatChangeDisable():
    stime = "10:00:00"
    models.updateSeatDisable(stime)
    print(stime + " disable complete")

@sched.scheduled_job('cron', hour='11', minute='00', name='disable3')
def seatChangeDisable():
    stime = "11:00:00"
    models.updateSeatDisable(stime)
    print(stime + " disable complete")

@sched.scheduled_job('cron', hour='12', minute='00', name='disable4')
def seatChangeDisable():
    stime = "12:00:00"
    models.updateSeatDisable(stime)
    print(stime + " disable complete")

@sched.scheduled_job('cron', hour='13', minute='00', name='disable5')
def seatChangeDisable():
    stime = "13:00:00"
    models.updateSeatDisable(stime)
    print("disable complete" + stime)

@sched.scheduled_job('cron', hour='14', minute='00', name='disable6')
def seatChangeDisable():
    stime = "14:00:00"
    models.updateSeatDisable(stime)
    print("disable complete" + stime)

@sched.scheduled_job('cron', hour='15', minute='00', name='disable7')
def seatChangeDisable():
    stime = "15:00:00"
    models.updateSeatDisable(stime)
    print("disable complete" + stime)

@sched.scheduled_job('cron', hour='16', minute='00', name='disable8')
def seatChangeDisable():
    stime = "16:00:00"
    models.updateSeatDisable(stime)
    print("disable complete" + stime)

@sched.scheduled_job('cron', hour='17', minute='00', name='disable9')
def seatChangeDisable():
    stime = "17:00:00"
    models.updateSeatDisable(stime)
    print("disable complete" + stime)

@sched.scheduled_job('cron', hour='18', minute='00', name='disable10')
def seatChangeDisable():
    stime = "18:00:00"
    models.updateSeatDisable(stime)
    print("disable complete" + stime)

sched.start()

@sched.scheduled_job('cron',hour='00', minute = '00', name = 'activate')
def seatChangeActivate():
    models.updateAllSeatActivate()
    print("activate complete")

def signup(userId):
    res = models.userInsert(userId)
    return 0

# (00) session check api
@method_decorator(csrf_exempt,name='dispatch')
def index(request):
    if request.method == 'GET':
        userId = request.session.get('userId')
        return HttpResponse(userId)

# (01) signUp & signIn api
@method_decorator(csrf_exempt,name='dispatch')
def logIn(requset):
    if requset.method == 'POST':
        try:
            data = json.loads(requset.body)
            userId = data['userId']

            # 로그인 전적이 있는 지 확인
            res = models.selectUser(userId)
            if (res==None):
                # 회원가입
                signup(userId)
            # session에 userId 추가
            requset.session['userId'] = userId
        except:
            return JsonResponse({'message':'DB_ERR'},status=400)
        return JsonResponse({'message': 'SUCCESS'}, status=200)

# (02) signOut api
    elif requset.method == 'PATCH':
        try:
            del requset.session['userId']
            return JsonResponse({'message':'SUCCESS'},status=200)
        except: return JsonResponse({'message':'WRONG_User'},status=300)

# (03) add Reservation api
@method_decorator(csrf_exempt,name='dispatch')
def reservation(request):
    if request.method == 'POST':

        # Login Check
        userId = request.session.get('userId')
        if userId=="None":
            return JsonResponse({'message':'WRONG_User'},status=300)
        else:
            data = json.loads(request.body)
            floor = data['floor']
            name = data['name']
            time = data['time']
            scheTIme = str(int(time[0:2])-1)
            seatInfoQuery = (floor,name,time)
            try:
                seat = models.retrieveAvailavleSeat(seatInfoQuery)
                if (seat!=None):
                    models.updateSeatStatus(seat[0])
                    reservationQuery = (userId,seat[0],"deactivation")
                    models.insertReservation(reservationQuery)
                    if(int(time[0:2])==now.hour):
                        infoQuery = ('prebooked', 'deactivation', seat[0], userId)
                        models.updateReservation(infoQuery)
                        scheTIme = now.hour
                        @sched.scheduled_job('cron', year=now.year, month=now.month, day=now.day, hour=scheTIme,minute="10")
                        def seatChangeCanceled():
                            reserveIdQuery = (userId, seat[0], 'prebooked')
                            reserveId = models.retrieveReserveId(reserveIdQuery)
                            if (len(reserveId) != 0):
                                models.autoDelete(reserveId[0])
                    elif(int(scheTIme)==now and now.minute >= 50):
                        infoQuery = ('prebooked', 'deactivation', seat[0], userId)
                        models.updateReservation(infoQuery)
                        scheTIme = int(scheTIme) + 1
                        @sched.scheduled_job('cron', year=now.year, month=now.month, day=now.day, hour=scheTIme,minute="10")
                        def seatChangeCanceled():
                            reserveIdQuery = (userId, seat[0], 'prebooked')
                            reserveId = models.retrieveReserveId(reserveIdQuery)
                            if (len(reserveId) != 0):
                                models.autoDelete(reserveId[0])
                    else:
                        @sched.scheduled_job('cron',year=now.year,month=now.month,day=now.day,hour=scheTIme,minute="50")
                        def seatChangePrebooked():
                            infoQuery = ('prebooked','deactivation',seat[0],userId)
                            models.updateReservation(infoQuery)
                        @sched.scheduled_job('cron', year=now.year, month=now.month, day=now.day, hour=time[0:2],minute="10")
                        def seatChangeCanceled():
                            reserveIdQuery = (userId, seat[0], 'prebooked')
                            reserveId = models.retrieveReserveId(reserveIdQuery)
                            if (len(reserveId) != 0):
                                models.autoDelete(reserveId[0])
                    return JsonResponse({'message': 'SUCCESS'}, status=200)
                else:   return JsonResponse({'message':'이미 예약된 자석 이거나 현재 사용 불가한 자석입니다.'},status=200)
            except: return JsonResponse({'message':'DB_ERR'},status=400)

# (04) retrieve all seat status
@method_decorator(csrf_exempt,name='dispatch')
def seatList(requset):
    try:
        res = models.retrieveAllSeatStatus()
        return JsonResponse({'message': 'SUCCESS','res':res}, status=200)
    except: return JsonResponse({'message':'DB_ERR'},status=400)

# (05) delete Reservation api
@method_decorator(csrf_exempt, name='dispatch')
def delete(request):
    userId = request.session.get('userId')
    if userId==None:
        return JsonResponse({'message':'WRONG_User'},status=300)
    else:
        data = json.loads(request.body)
        floor = data['floor']
        name = data['name']
        time = data['time']
        seatInfo = (floor,name,time)
        try:
            seat = models.retrieveSeatId(seatInfo)
            ReserveInfoQuery = (userId,seat[0])
            reserveId = models.retrievedeleteId(ReserveInfoQuery)
            if reserveId == None:
                return JsonResponse({'message': 'Wrong reservation'}, status=301)
            else:
                models.deleteReservation(reserveId[0])
                models.deleteSeatStatus(seat[0])
                return JsonResponse({'message': 'SUCCESS'}, status=200)
        except:
            return JsonResponse({'message': 'DBERR'}, status=400)

# (06) auto delete Reservation api
@method_decorator(csrf_exempt, name='dispatch')
def autoDelete(request):

    data = json.loads(request.body)
    userId = data['userId']
    floor = data['floor']
    name = data['name']
    time = data['time']
    try:
        queryInfo = (floor, name, time)
        seat = models.retrieveSeatId(queryInfo)
        ReserveInfoQuery = (userId, seat[0])
        reserveId = models.retrievedeleteId(ReserveInfoQuery)
        if reserveId == None:
            return JsonResponse({'message': 'Wrong reservation'}, status=300)
        else:
            models.autoDelete(reserveId[0])
            models.deleteSeatStatus(seat[0])
            return JsonResponse({'message': 'SUCCESS'}, status=200)
    except:
        return JsonResponse({'message': 'DBERR'}, status=400)

# (07) Retrieve User Reservation List
@method_decorator(csrf_exempt, name='dispatch')
def userReservation(request):
    userId = request.session.get('userId')
    if userId==None:
        return JsonResponse({'message':'WRONG_User'},status=300)
    else:
        try:
            reservationList = models.retrieveReserv(userId)
            if len(reservationList) == 0:
                return JsonResponse({'message': '예약 내역이 없습니다'}, status=200)
            else:
                resLIst = []
                i=0
                for item in reservationList:
                    seatInfo = models.retrieveSeatById(reservationList[i][0])
                    tmp = (reservationList[i]+seatInfo)[1:]
                    i+=1;
                    resLIst.append(tmp)
                return JsonResponse({'message': 'SUCCESS','res':resLIst}, status=200)
        except:
            return JsonResponse({'message': 'DBERR'}, status=400)

# (06) auto delete Reservation api
@method_decorator(csrf_exempt, name='dispatch')
def booked(request):
    data = json.loads(request.body)
    userId = request.session.get('userId')
    floor = data['floor']
    name = data['name']
    time = data['time']

    if userId==None:
        return JsonResponse({'message':'WRONG_User'},status=300)

    try:
        info = (floor,name,time)
        seat = models.retrieveSeatId(info)
        ReserveInfoQuery = (userId, seat[0],'prebooked')
        reserveId = models.retrieveReserveId(ReserveInfoQuery)
        if reserveId == None:
            return JsonResponse({'message': 'Wrong reservation'}, status=301)
        else:
            reserveInfo = ('booked','prebooked',seat[0],userId)
            models.updateReservation(reserveInfo)

            return JsonResponse({'message': 'SUCCESS'}, status=200)
    except:
        return JsonResponse({'message': 'DBERR'}, status=400)

# (06) auto delete Reservation api
@method_decorator(csrf_exempt, name='dispatch')
def reserveList(request):

    data = json.loads(request.body)
    userId = request.session.get('userId')
    if userId==None:
        return JsonResponse({'message':'WRONG_User'},status=300)
    try:
        seats = models.checkChangeList(userId)
        return JsonResponse({'message': 'SUCCESS','res' : seats}, status=200)
    except:
        return JsonResponse({'message': 'DBERR'}, status=400)

# (07) disalble seat
@method_decorator(csrf_exempt, name='dispatch')
def disableSeat(request):
    try:
        models.updateAllSeatDisable()
        res = models.retrieveAllSeatStatus()
        return JsonResponse({'message': 'SUCCESS','res':res}, status=200)
    except:
        return JsonResponse({'message': 'DBERR'}, status=400)

# (08) activate seat
@method_decorator(csrf_exempt, name='dispatch')
def activateSeat(request):
    try:
        models.updateAllSeatActivate()
        res = models.retrieveAllSeatStatus()
        return JsonResponse({'message': 'SUCCESS','res':res}, status=200)
    except:
        return JsonResponse({'message': 'DBERR'}, status=400)


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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import os
import urllib
import scrapy
from scrapy.exceptions import DropItem
from scrapy.pipelines.images import ImagesPipeline
from scrapy.pipelines.files import FilesPipeline

class TutorialPipeline(ImagesPipeline):
    def file_path(self, request, response=None, info=None):
        #image_name = request.meta['item']['name'][0].split(' ')[1]
        image_name = request.meta['item']['name'][0]
        if not image_name:
            raise DropItem("fuck name")
        image_guid = request.url.split('/')[-1]
        return '%s/%s' % (image_name,image_guid)
    def get_media_requests(self, item, info):
        for image_url in item['image_urls']:
            yield scrapy.Request(url=image_url,meta={'item': item})

    def item_completed(self, results, item, info):
        image_paths = [x['path'] for ok, x in results if ok]
        #for ok,x in results:
         #   if ok:
          #      image_paths = [x['path']]
        #print('image_paths,,,,,,,',image_paths)
        if not image_paths:
            raise DropItem("Item contains no images")
        #item['image_paths'] = image_paths
        return item

class TutorialFilePipeline(FilesPipeline):
    def get_media_requests(self, item, info):
        for file_url in item['file_urls']:
            print('fuck this')
            yield scrapy.Request(url=file_url,meta={'item': item})

    def item_completed(self, results, item, info):
        file_paths = [x['path'] for ok, x in results if ok]
        if not file_paths:
            raise DropItem("Item contains no files")
        item['file_path'] = file_paths
        return item

    def file_path(self, request, response=None, info=None):
        #image_name = request.meta['item']['name'][0].split(' ')[1]
        name = request.meta['item']['name'][0]
        if not name:
            raise DropItem("fuck name")
        guid = request.url.split('/')[-1]
        return '%s/%s' % (name,guid)


"""
Examples of good plotting practice

In this script, matplotlib is used with preferences set in plt.rc command

Including:
    1. Simple single plot figure with errorbars
    2. Figure with multiplots, 2nd axes split with left-and-right yaxes
    3. Plot with multiple lines, where colours are defined by a colormap
"""
import sys, os
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D  # 3D plotting

plt.rc('figure', figsize=(8, 6), dpi=100, autolayout=True)
plt.rc('lines', marker='o', color='r', linewidth=2, markersize=6)
plt.rc('errorbar', capsize=2)
plt.rc('legend', loc='best', frameon=False, fontsize=16)
plt.rc('axes', linewidth=2, titleweight='bold', labelsize='large')
plt.rc('xtick', labelsize='large')
plt.rc('ytick', labelsize='large')
plt.rc('axes.formatter', limits=(-3, 3), offset_threshold=6)
# Note font values appear to only be set when plt.show is called
plt.rc('font', family='serif', style='normal', weight='bold', size=16,
       serif=['Times New Roman', 'Times', 'DejaVu Serif'])
plt.rcParams["savefig.directory"] = os.path.dirname(__file__)  # Default save directory for figures
# plt.rcdefaults()

# Data
xdata = np.linspace(-180, 180, 100)
ydata = np.sin(np.radians(xdata))
ydata2 = np.cos(np.radians(xdata))

yerror = 0.1 * np.ones(len(ydata))

################Figure 1####################
# Simple figure with single axis           #
############################################
plt.figure()

plt.errorbar(xdata, ydata, yerror, label='sin')
plt.errorbar(xdata, ydata2, yerror, label='cos')

# Horizontal axis line
plt.axhline(0, color='k')

plt.xlim([-180, 180])
plt.ylim([-1.5, 1.5])

# axis labels
plt.legend()
plt.xlabel('Angle [Deg]')
plt.ylabel('Value')
plt.title('A Perfect Plot\n $y = \sin(x)$')
plt.savefig('plots/PerfectPlot_matplotlib_7', dpi=200)  # png

################Figure 2####################
# Figure with 2 subplots + overlapping axes#
############################################
fig, (ax1, ax2) = plt.subplots(nrows=2, ncols=1)

# Figure 1 - Subplot 1 - Axes 1
plt.sca(ax1)
plt.text(-0.15, 1.07, '(a)', transform=plt.gca().transAxes)

plt.errorbar(xdata, ydata, yerror, label='sin')
plt.errorbar(xdata, ydata2, yerror, label='cos')

plt.xlim([-180, 180])
plt.ylim([-1.5, 1.5])

plt.legend()
plt.ylabel('Value')
plt.gca().get_yaxis().set_label_coords(-0.10, 0.5)

# plt.subplots_adjust(left=0.25)
plt.xticks([])  # No xticks on top figure

# Figure 1 - Subplot 2 - Axes 1
plt.sca(ax2)
plt.text(-0.15, 1.07, '(b)', transform=plt.gca().transAxes)

plt.plot(xdata, ydata, 'y-', label='sin')

plt.xlim([-180, 180])
plt.ylim([-1.5, 1.5])

plt.xlabel('Angle [Deg]')
plt.ylabel('sin')
plt.gca().get_yaxis().set_label_coords(-0.10, 0.5)

# Figure 1 - Subplot 2 - Axes 2
ax2b = ax2.twinx()  # Create axis ontop of ax2
plt.plot(xdata, ydata2 * 10, '-', ms=12, lw=4, label='sin')

plt.xlim([-180, 180])
plt.ylim([-15, 15])

plt.ylabel('10*cos')
plt.savefig('plots/PerfectPlot_matplotlib_8', dpi=200)  # png

################Figure 3####################
# Figure with colormapped plots            #
############################################
plt.figure()

# Colormaps: http://matplotlib.org/users/colormaps.html
cmap_name = 'viridis'
cm = plt.get_cmap(cmap_name)
# Note: cm(n) = (r,g,b,a)
#  if n = int, n = 0:255
#  if n = float, n = 0:1

datarange = np.arange(10, 101, 5, dtype=float)
colrange = (datarange - datarange[0]) / (datarange[-1] - datarange[0])
for n in range(len(datarange)):
    col = cm(colrange[n])  # add a small number here to keep values away from minimum colormap (white for hot_r)
    plt.plot(xdata, datarange[n] * ydata, '-', lw=2, color=col,
             label='%3.0f (%5.2f,%5.2f,%5.2f,%5.2f)' % (datarange[n], col[0], col[1], col[2], col[3]))

# Colorbar
sm = plt.cm.ScalarMappable(cmap=cm)
sm.set_array(datarange)
cbar = plt.colorbar(sm)
cbar.set_label('variation [unit]')

plt.xlabel('Angle [Deg]')
plt.ylabel('Value')
plt.title('%s' % cmap_name)
plt.savefig('plots/PerfectPlot_matplotlib_9', dpi=200)  # png

plt.show()


import os
import time
import re
import env

list_name = os.listdir()
list_length = len(list_name)
new_list = []
new_list_updated = []

dot = re.compile(r'\.')
ignoredFileTypes = re.compile(r'.*\.(py|exe|bat|env|git|gitignore|md)\Z')
weirdCharacters = re.compile(r'[^A-Za-z0-9\.]')
numbers = re.compile(r'\d+')
letterAndSpace = re.compile(r'\D+')
defaultText = '\033[3;m'
greenText = '\033[3;32m'
yellowText = '\033[3;33m'
cianText = '\033[3;36m'
redText = '\033[3;31m'

def colorText(color, text):
    colors = env.colors
    if not colors:
        return text
    elif color == 'title':
        return '\033[1;33;45m'+text+defaultText
    elif color == 'green':
        return greenText+text+defaultText
    elif color == 'yellow':
        return yellowText+text+defaultText
    elif color == 'red':
        return redText+text+defaultText
    elif color == 'cian':
        return cianText+text+defaultText
    else:
        return defaultText+text+defaultText

def start():
    print(colorText('title', '  Formateador Automatico de Nombres de Archivos  '))
    time.sleep(1)
    createList()

def createList():
    global list_length, list_name, new_list
    for _file in list_name:
        if not ignoredFileTypes.search(_file) and dot.search(_file):
            new_list.append(_file)
    newListLength = len(new_list)
    _archivo = (' archivo', ' archivos')[newListLength > 0]
    print(colorText('green', '... Cargando '+str(newListLength)+_archivo))
    if newListLength < 1:
        print(colorText('red', 'No hay archivos para renombrar!'))
        end()
        return
    autoRename()
    time.sleep(1)

def autoRename():
    newNameList = []
    clearWord = input(colorText('green', 'Palabra que quieras eliminar: '))
    for _file in new_list:
        nameAndExt = dot.split(_file)
        if len(nameAndExt) < 2:
            print(nameAndExt)
            new_list.remove(_file)
        elif len(nameAndExt) > 2:
            newNameAndExt = ['', '']
            for i in range(len(nameAndExt)-1):
                spacer = (' ', '')[i == 0]
                newNameAndExt[0] = newNameAndExt[0]+spacer+nameAndExt[i]
            newNameAndExt[1] = nameAndExt[len(nameAndExt)-1]
            nameAndExt = newNameAndExt
        cleanName = weirdCharacters.sub(' ', nameAndExt[0])
        startName = startNum(cleanName)
        endName = textName(cleanName, clearWord)
        newName = startName+' - '+endName+'.'+nameAndExt[1]
        newNameList.append(newName)
        print(colorText('cian', 'Nombre original: '+_file))
        print(colorText('yellow', 'Nombre nuevo: '+newName))
    accept(newNameList, new_list)

def startNum(cleanName):
    if numbers.search(cleanName):
        startName = numbers.findall(cleanName)
        if len(startName) > 1:
            print(colorText('cian', 'Posibles numeros en el titulo: '), startName)
            newStartName = input(
                colorText('green', 'Elija el numero correcto: '))
            startName = newStartName
        else:
            startName = startName[0]
    else:
        print(colorText('red', 'No hay ningun numero en el nombre del archivo: '+cleanName))
        startName = input(
            colorText('green', 'Introdusca el numero de este archivo: '))
    startName = int(startName)
    if startName < 10:
        startName = '00'+str(startName)
    elif startName < 100:
        startName = '0'+str(startName)
    else:
        startName = str(startName)
    return startName

def textName(cleanName, clearWord):
    endName = letterAndSpace.findall(cleanName)
    if len(endName) > 1:
        newEndName = ''
        for word in endName:
            newEndName = newEndName+word
        endName = newEndName
    else:
        endName = endName[0]
    if clearWord != '' and re.search(r''+clearWord+'+', endName):
        nameWordsList = re.split(r''+clearWord+'+', endName)
        endName = ''
        for word in nameWordsList:
            endName = endName+word.strip()+' '
    endName = endName.strip()
    endName = endName.title()
    return endName

def accept(newNameList, new_list):
    _accept = input(colorText('green', 'Entra \'ok\' para aceptar: '))
    if _accept != 'ok':
        end(False)
        return
    rename(newNameList, new_list)
    end()

def rename(newNamesList, new_list):
    print(colorText('cian', str(len(newNamesList))+'/'+str(len(new_list))))
    for i in range(len(new_list)):
        os.rename(new_list[i], newNamesList[i])

def end(ok=True):
    time.sleep(1)
    if not ok:
        print(colorText('red', 'Programa detenido por el usuario!'))
        return
    print(colorText('green', '... El programa finalizo con exito!'))

start()


import pygrib
import sys
import pandas as pd
import os
import matplotlib.pyplot as plt
import numpy as np
import scipy.spatial
import scipy.interpolate

from datetime import tzinfo, timedelta, datetime

class UTC(tzinfo):
    def utcoffset(self, dt):
        return timedelta(0)
    def tzname(self, dt):
        return "UTC"
    def dst(self, dt):
        return timedelta(0)
utc = UTC()

tree = None

varlist = ['U component of wind','V component of wind']
vv = ['uvel','vvel']

def read_file(cur):
    F = 'proc/%d.bin' % cur
    if os.path.exists(F):
        print('skipping',F)
        return
    fname = 'data/%d.grb2' % cur
    print('opening',fname)
    f = pygrib.open(fname) # open

    print(list(zip(varlist,vv)))
    assert len(varlist) == len(vv)

    d = f.select()
    #for x in d:
    #    print(x)
    #exit()
    grbs = []
    for x in varlist:
        if type(x) == int:
            grbs.append([d[x-1]])
        else:
            try: grbs.append(f.select(name=x))
            except:
                print('Not including',x)
                grbs.append(None)
            #print(grbs[-1])
    #exit()

    lats, lons = grbs[0][0].latlons()
    print(lats, lons)
    print(lats.shape, lons.shape)
    out = np.zeros((lats.shape[0], lats.shape[1], 8, 2), dtype=np.int16)
    for grb in grbs:
        i = 0
        for row in grb:
            if row['typeOfLevel'] == 'isobaricInhPa' and row.level >= 50 and row.level <= 350:
                j = 0 if row.name.startswith('U') else 1
                if i == 4:
                    print(row.values[42,120])
                    print(lats[42,120], lons[42,120])
                out[:,:,i,j] = row.values*100
                i += 1
    out.flatten().tofile('proc/%d.bin' % cur)
    del f

if __name__ == '__main__':
    files = sorted(map(lambda x: int(x.split('.')[0]), os.listdir('data')))

    for cur in files: read_file(cur)


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

# moduuli komentoriviparametrien käsittelyyn
import argparse
import time
import os
import logging

# luokka uutuusluetteloiden käsittelyyn
from uutuusluettelo import Uutuusluettelo
# luokka kirjanpitämiseen edellisen luettelon luontikerran kirjoista
from kasitellyt import Käsitellyt
# luokka luetteloiden lähettämiseen sähköpostilla
from posti import Postittaja

def kysyPostitetaanko( luettelo ):
    '''Postitus vaiheessa käytettävä funktio

    Jos käyttäjä määrittää komentoriviparametrilla, että häneltä kysytään postitetaanko luettelot,
    käytetään tätä funktiota käyttäjältä luvan kysymiseen jokaisen luettelon postituksesta.'''
    # käyttäjän vastaus kysymykseen
    vastaus = ''
    # kysytään kunnes käyttäjä vastaa k tai e
    while vastaus not in [ 'k', 'e' ]:
        vastaus = input( 'Lähetetäänkö ' +luettelo['tiedosto'] + ' sähköpostilla. Vastaa k tai e: ' )

    return vastaus == 'k'

# määritetään ohjelman komentorivi parametrit
komentorivi = argparse.ArgumentParser()
# määritetään -p valitsin, jolla valitaan miten luetteloiden sähköpostilla lähetyksen kanssa toimitaan
komentorivi.add_argument( '-p', '--posti',
                          choices =  [ 'kyllä', 'ei', 'kysy', 'heti' ],
                          default = 'ei',
                          help = 'Määrittele miten luettelot postitetaan' )
# käsitellään komentoriviparametrit
parametrit = komentorivi.parse_args()

# hakemisto josta ohjelman tiedostot luetaan. Hakemisto on tämän koodi tiedoston hakemisto.
hakemisto = os.path.dirname( os.path.abspath( __file__ )) +'/'

# määritellään ohjelma kirjoittamaan tulosteitaan konsolille ja tiedostoon
# käytetään logging moduulia. Luodaan Logger olio, jota
# käytetään tulosteiden kirjoittamiseen. Määritetään käsittelijät (handler) kirjoitamaan tiedostoon ja konsolille.
loki = logging.getLogger( 'celia-uutuudet' )
# tulostetaan kaikki mahdolliset loki viestit
loki.setLevel( logging.DEBUG )
konsoliKäsittelijä = logging.StreamHandler()
loki.addHandler( konsoliKäsittelijä )
tiedostoKäsittelijä = logging.FileHandler( hakemisto +'uutuushaku.log', encoding = 'utf-8' )
# oletuksena konsolille tulostuu vain viesti. Tiedostoon haluamme myös ajan ja tyypin.
# Tulostettavan viestin muoto määritellään Formatter oliolla
muotoilija = logging.Formatter('%(asctime)s %(levelname)s - %(message)s')
tiedostoKäsittelijä.setFormatter( muotoilija )
loki.addHandler( tiedostoKäsittelijä )

# napataan ohjelman suorituksen aikana syntyvät odottamattomat poikkeukset
try:
    # määritetään mitä uutuusluetteloita käsitellään
    # luettelosta määritetään osoite mistä uutuudet löytyvät
    # tiedosto, johon ne tallennetaan,  luettelon nimi ja luokka, jonka instanssilla luettelo käsitellään
    luettelot = [
        {
            'url': 'https://www.celianet.fi/kirjavinkit/uutuuskirjat-aikuisille/',
            'otsikko': 'Aikuisten uutuudet',
            'tiedosto': 'aikuisten_uutuudet.txt',
            'luettelo': Uutuusluettelo
        },
        {
            'url': 'https://www.celianet.fi/kirjavinkit/lasten-uutuuskirjat/',
            'otsikko': 'Lasten ja nuorten uutuudet',
            'tiedosto': 'lasten_ja_nuorten_uutuudet.txt',
            'luettelo': Uutuusluettelo
        }
    ]

    # jos käyttäjä haluaa postittaa aikaisemmin luodut luettelot ei luoda niitä nyt
    if parametrit.posti != 'heti':
        # olio, josta luetaan edellisen luettelon luonti kerran uusimmat kirjat eri kategorioista, joita vanhempia
        # kirjoja ei lisätä nyt luotaviin luetteloihin.
        # tähän myös tallennetaan tällä hetkellä uusimmat kirjat eri kategorioista, jolloin niitä ei luetteloida ensi kerralla
        # tiedot luetaan ja tallennetaan json muodossa vanhat.json tiedostoon
        vanhat = Käsitellyt( hakemisto +"vanhat.json" )
        # käsitellään jokainen luettelo
        loki.info( 'luodaan luetteloita.' )
        for luettelo in luettelot:
            # luodaan luettelon käsittely luokasta instanssi, jolle annetaan osoite, josta luettelo löytyy, nimi tiedostolle, johon uutuudet tallennetaan,  tiedot käsitellyistä kirjoista,
            # sekä hakemisto luettelotiedostolle
            hakija = luettelo['luettelo']( luettelo['url'], luettelo['tiedosto'], vanhat, hakemisto )
            # haetaan kirjat ja tallennetaan tiedostoon
            hakija.haeKirjat()

        # tallennetaan tämän luettelon eri kategorioiden uusimmat seuraavaa kertaa varten
        vanhat.tallenna()

    # postitetaan luettelot jos käyttäjä niin halusi
    if parametrit.posti in  [ 'kyllä', 'kysy', 'heti' ]:
        # luodaan postin lähettäjä konfiguraation pohjalta
        postittaja = Postittaja( hakemisto )
        # käydään luettelot läpi
        for luettelo in luettelot:
            # postitetaan luettelo heti jos käyttäjä niin halusi muuten kysytään jokaisen luettelon kohdalla haluaako käyttäjä sen lähettää
            if parametrit.posti in [ 'kyllä', 'heti' ]  or kysyPostitetaanko( luettelo ):
                loki.info( 'Postitetaan {}'.format( luettelo['otsikko'] ))
                postittaja.postita( luettelo )
                # odotetaan 5 sekuntia ennen seuraava postitusta
                # nopeasti peräkkäin lähetettävät viestit saattavat aiheuttaa ongelmia THP:n sähköpostilistoilla
                time.sleep( 5 )

except Exception as virhe:
    # kirjoitetaan odottamattoman virheen tiedot lokiin
    loki.exception( "tapahtui odottamaton virhe" )


cnt=1
while True:
    n=int(input())
    if n==0:
        break
    L=[]
    bool=True
    for i in range(n):
        tmp=input()
        adder=[]
        for idx in range(len(tmp)):
            if tmp[idx]==' ':
                adder.append(tmp[:idx])
                adder.append(tmp[idx:].replace(' ',''))
                break
        L.append(adder)
        if 'N' in L[i][1]:
            bool=False
    print('Group '+str(cnt))
    if bool:
        print('Nobody was nasty')
    else:
        for g in range(n):
            for p in range(n-1):
                if L[g][1][p]=='N':
                    atk=L[g-(p+1)][0]
                    dfc=L[g][0]
                    print(atk+' was nasty about '+dfc)
    cnt+=1
    print('')


import pytest
import pathlib

from booking_scraper.cli import run

@pytest.mark.asyncio
async def test_run():
    data_store_file = pathlib.Path("tests/datastore.json")
    config_file = pathlib.Path("config.json")
    send_notifications = True
    kwargs = {}

    # Call the run function with the test arguments
    await run(data_store_file, config_file, send_notifications, **kwargs)