#!/usr/bin/env python
#
# Compute metrics to assess the quality of spinal cord images.
#
# This script is "old" and was replaced by process_data.sh. However, the portion specific to niftiweb was kept
# uncommented and requires some digging before being used.
#
# USAGE:
# The script should be launched using SCT's python:
#   PATH_GMCHALLENGE="PATH TO THIS REPOSITORY"
#   ${SCT_DIR}/python/bin/python ${PATH_GMCHALLENGE}/process_data.py
#
# OUTPUT:
#   results.csv: quantitative results in CSV format
#   results.txt: results in txt form to be sent to participant
#
# Authors: Stephanie Alley, Julien Cohen-Adad
# License: https://github.com/neuropoly/gm_challenge/blob/master/LICENSE

# TODO: enable to input suffix for results filename (otherwise, overwriting in process_folder
# TODO: get verbose working (current issue is sys.stdout.isatty()) is False, hence sct.run() is using sct.log with no terminal output

import sys, os, shutil, argparse, pickle, io, argparse
import numpy as np
import pandas as pd
# append path to useful SCT scripts
path_sct = os.getenv('SCT_DIR')
sys.path.append(os.path.join(path_sct, 'scripts'))
import sct_utils as sct
from spinalcordtoolbox.image import Image

def get_parameters():
    parser = argparse.ArgumentParser(description='Compute metrics to assess the quality of spinal cord images. This '
                                                 'script requires two input files of scan-rescan acquisitions, which '
                                                 'will be used to compute the SNR. Other metrics (contrast, sharpness) '
                                                 'will be computed from the first file.')
    parser.add_argument("-i", "--input",
                        help="List here the two nifti files to compute the metrics on, separated by space.",
                        nargs='+',
                        required=True)
    parser.add_argument("-s", "--seg",
                        help="Spinal cord segmentation for the first dataset. If not provided, segmentation will be "
                             "performed.",
                        required=False)
    parser.add_argument("-g", "--gmseg",
                        help="Gray matter segmentation for the first dataset. If not provided, segmentation will be "
                             "performed.",
                        required=False)
    parser.add_argument("-r", "--register",
                        help="{0, 1}. Perform registration between scan #1 and scan #2. Default=1.",
                        type=int,
                        default=1,
                        required=False)
    parser.add_argument("-n", "--num",
                        help="NiftyWeb ID",
                        required=False)
    parser.add_argument("-o", "--output_dir",
                        help="Output directory",
                        required=False)
    parser.add_argument("-v", "--verbose",
                        help="Verbose {0,1}. Default=1",
                        type=int,
                        default=1,
                        required=False)
    args = parser.parse_args()
    return args

#
# def compute_snr_diff(file_data1, file_data2, file_mask):
#     """
#     Compute SNR based on two input data and a mask
#     :param file_data1: image 1
#     :param file_data2: image 2
#     :param file_mask: mask where to compute SNR
#     :return: float: SNR_diff rounded at 2 decimals
#     """
#     print("Compute SNR_diff...")
#     sct.run("sct_image -i " + file_data1 + "," + file_data2 + " -concat t -o data_concat.nii.gz")
#     status, output = sct.run("sct_compute_snr -i data_concat.nii.gz -vol 0,1 -m " + file_mask)
#     # parse SNR info
#     # TODO: run sct_compute_snr as Python module
#     try:
#         outstring = output[output.index("SNR_diff =") + 11:]
#         snr_diff = np.float(outstring[: outstring.index("\n")])
#     except Exception as e:
#         print(e)
#     return round(snr_diff, 2)  # round at 2 decimals
#
#
# def compute_snr_single(file_data, file_mask):
#     """
#     Compute SNR based on a single image and a mask
#     :param file_data: image
#     :param file_mask: mask for ROI_body
#     :return: float: SNR_single rounded at 2 decimals
#     """
#     print("Compute SNR_single...")
#     # Convert image to array
#     data = Image(file_data).data
#     # Convert mask to array
#     wm_mask = Image(file_mask).data
#     # Use mask to select data in ROI_body
#     roi_data = np.where(wm_mask == 1, data, 0)
#     # Compute SNR slice-wise
#     # snr_slices = np.zeros((roi_data.shape[2]))
#     snr_slices = []
#     for i in range(roi_data.shape[2]):
#         ind_nonzero = np.where(roi_data[:, :, i] > 0)
#         # check if there is non-null voxel on this slice
#         if len(ind_nonzero[0]):
#             mean_slice = np.mean(roi_data[:, :, i][ind_nonzero])
#             std_slice = np.std(roi_data[:, :, i][ind_nonzero])
#             snr_slice = mean_slice / std_slice
#             # snr_slices[i] = snr_slice
#             snr_slices.append(snr_slice)
#     snr_single = np.mean(snr_slices)
#     return round(snr_single, 2)
#
#
# def compute_contrast(file_data, file_mask1, file_mask2):
#     """
#     Compute contrast in image between two regions
#     :param file_data: image
#     :param file_mask1: mask for region 1
#     :param file_mask2: mask for region 2
#     :return: float: contrast in percent (rounded at 2 decimals)
#     """
#     print("Compute contrast...")
#     # Get mean value within mask
#     sct.run("sct_extract_metric -i " + file_data + " -f " + file_mask1 + " -method bin -o mean_mask1.pickle")
#     sct.run("sct_extract_metric -i " + file_data + " -f " + file_mask2 + " -method bin -o mean_mask2.pickle")
#     # Retrieve values from saved pickle
#     mean_mask1 = pickle.load(io.open("mean_mask1.pickle"))["Metric value"][0]
#     mean_mask2 = pickle.load(io.open("mean_mask2.pickle"))["Metric value"][0]
#     # Compute contrast in percentage
#     contrast = abs(mean_mask1 - mean_mask2) / min(mean_mask1, mean_mask2) * 100
#     return round(contrast, 2)  # round at 2 decimals
#
#
# def compute_sharpness(file_data, file_mask_gm):
#     """
#     Compute sharpness at GM/WM interface. The mask of GM is dilated, and then subtracted from the GM mask, in order to
#     produce a mask at the GM/WM interfact. This mask is then used to extract the Laplacian value of the image. The
#     sharper the transition, the higher the Laplacian. Note that the Laplacian will also be affected by the underlying
#     WM/GM contrast, hence the WM and GM values need to be normalized before computing the Laplacian.
#     :param file_data:
#     :param file_mask_gm:
#     :return: float: sharpness
#     """
#     print("Compute sharpness...")
#     # Dilate GM mask
#     sct.run("sct_maths -i data1_gmseg.nii.gz -dilate 1 -o data1_gmseg_dil.nii.gz")
#     # Subtract to get mask at WM/GM interface
#     sct.run("sct_maths -i data1_gmseg_dil.nii.gz -sub data1_gmseg.nii.gz -o mask_interface.nii.gz")
#     # Compute Laplacian on image
#     sct.run("sct_maths -i data1.nii.gz -laplacian 0.5,0.5,0 -o data1_lapl.nii.gz")
#     # Normalize WM/GM before computing Laplacian
#     # TODO
#     # Extract Laplacian at WM/GM interface
#     sct.run("sct_extract_metric -i data1_lapl.nii.gz -f mask_interface.nii.gz -o laplacian.pickle")
#     # return
#     return pickle.load(io.open("laplacian.pickle"))["Metric value"][0]

def main(file_input, file_seg, file_gmseg, num=None, register=True, output_dir=None, create_txt_output=False, verbose=1):
    """
    Compute metrics to assess the quality of spinal cord images.
    :param file_data:
    :param file_seg:
    :param file_gmseg:
    :param num:
    :param register: Bool: Register data2 to data1. Could be skipped if data are already registered (e.g. simulations)
    :param output_dir:
    :param create_txt_output: Bool: Create output txt file for Niftyweb server
    :param verbose:
    :return: results: pandas dataframe with results
    """
    #
    # # Params
    # if not output_dir:
    #     output_dir = "./results"
    # file_output = "results"  # no prefix
    # fdata = ['data1.nii.gz', 'data2.nii.gz']
    # fseg = 'data1_seg.nii.gz'
    # fgmseg = 'data1_gmseg.nii.gz'

    # Parse arguments
    # if not args:
    #     args = sys.argv[1:]
    # file_data = args.input
    # file_seg = args.seg
    # file_gmseg = args.gmseg
    # register = args.register
    # num = args.num
    # verbose = args.verbose
    #
    # # Make output dir
    # if not os.path.isdir(output_dir):
    #     os.makedirs(output_dir)
    #
    # # copy to output directory and convert to nii.gz
    # print("Copy data...")
    # sct.copy(file_input[0], os.path.join(output_dir, fdata[0]))
    # if os.path.isfile(file_input[1]):
    #     sct.copy(file_input[1], os.path.join(output_dir, fdata[1]))
    #     run_diff_method = True
    # else:
    #     run_diff_method = False
    # if file_seg is not None:
    #     sct.copy(file_seg, os.path.join(output_dir, fseg))
    # if file_gmseg is not None:
    #     sct.copy(file_gmseg, os.path.join(output_dir, fgmseg))
    #
    # # move to results directory
    # curdir = os.getcwd()
    # os.chdir(output_dir)
    #
    # # Segment spinal cord
    # if file_seg is None:
    #     print("Segment spinal cord...")
    #     sct.run("sct_deepseg_sc -i " + fdata[0] + " -c t2s", verbose=verbose)
    #
    # # Segment gray matter
    # if file_gmseg is None:
    #     print("Segment gray matter...")
    #     sct.run("sct_deepseg_gm -i " + fdata[0], verbose=verbose)

    # # Crop data (for faster processing)
    # print("Crop data (for faster processing)...")
    # sct.run("sct_create_mask -i " + fdata[0] + " -p centerline," + fseg + " -size 35mm", verbose=verbose)
    # fmask = "mask_" + fdata[0]
    # sct.run("sct_crop_image -i " + fdata[0] + " -m " + fmask + " -o " + sct.add_suffix(fdata[0], 'c'))
    # fdata[0] = sct.add_suffix(fdata[0], 'c')
    # sct.run("sct_crop_image -i " + fseg + " -m " + fmask + " -o " + sct.add_suffix(fseg, 'c'))
    # fseg = sct.add_suffix(fseg, 'c')
    # sct.run("sct_crop_image -i " + fgmseg + " -m " + fmask + " -o " + sct.add_suffix(fgmseg, 'c'))
    # fgmseg = sct.add_suffix(fgmseg, 'c')
    #
    # # Generate white matter segmentation
    # print("Generate white matter segmentation...")
    # sct.run("sct_maths -i " + fseg + " -sub " + fgmseg + " -o " + fdata[0] + "_wmseg.nii.gz", verbose=verbose)
    #
    # # Erode white matter mask to minimize partial volume effect
    # # Note: we cannot erode the gray matter because it is too thin (most of the time, only one voxel)
    # print("Erode white matter mask...")
    # sct.run("sct_maths -i " + fdata[0] + "_wmseg.nii.gz -erode 1 -o " + fdata[0] + "_wmseg_erode.nii.gz", verbose=verbose)

    # if run_diff_method and register:
    #     print("Register data2 to data1...")
    #     # Register image 2 to image 1
    #     sct.run("sct_register_multimodal -i " + fdata[1] + " -d " + fdata[0] + " -param step=2,type=im,algo=rigid,metric=MeanSquares,smooth=1,iter=50,slicewise=1 -x nn", verbose=verbose)
    #     # Add suffix to file name
    #     fdata[1] = sct.add_suffix(fdata[1], "_reg")

    # Analysis: compute metrics
    # Initialize data frame for reporting results
    results = pd.DataFrame(0, index=['SNR_diff', 'SNR_single', 'Contrast'], columns=['Metric Value'])

    # Compute metrics
    results.loc['Contrast'] = compute_contrast(fdata[0], fdata[0] + "_wmseg.nii.gz", fgmseg)
    results.loc['SNR_single'] = compute_snr_single(fdata[0], fdata[0] + "_wmseg_erode.nii.gz")
    if run_diff_method:
        results.loc['SNR_diff'] = compute_snr_diff(fdata[0], fdata[1], fdata[0] + "_wmseg_erode.nii.gz")
    # results.loc['Sharpness'] = compute_sharpness("data1.nii.gz", "data1_gmseg.nii.gz")

    # Save DataFrame as CSV
    results.columns = ['']
    results.to_csv(file_output + ".csv")
    print("--> created file: " + file_output + ".csv")

    if create_txt_output:
        # Build text file for user
        results_to_return = open(file_output + ".txt", 'w')
        results_to_return.write('The following metrics were calculated:\n')
        results_to_return.write(results.__repr__())
        results_to_return.write('\n\nA text file containing this information, as well as the image segmentations, are '
                                'available for download through the link below. Please note that these are the intermediate '
                                'results (automatically processed). We acknowledge that manual adjustment of the cord and '
                                'gray matter segmentations might be necessary. Please check carefully, adjust the '
                                'segmentations if necessary, and contact us for recalculating the metrics with the '
                                'corrected segmentations.\n')
        results_to_return.close()
        print("--> created file: " + file_output + ".txt")

    # Package results for daemon
    if num:
        # Create folder for segmentations
        segmentations = 'segmentations'
        if not os.path.exists(segmentations):
            os.makedirs(segmentations)

        # Copy data1_seg.nii.gz and data1_gmseg.nii.gz to segmentations folder
        shutil.copy2("data1_seg.nii.gz", segmentations)
        shutil.copy2("data1_gmseg.nii.gz", segmentations)

        # Rename text file for interaction with daemon
        os.rename(file_output + '.txt', num + '_WMGM.txt')

        # Copy text file containing results to segmentations folder
        shutil.copy2(num + '_WMGM.txt', segmentations)

        # Create ZIP file of segmentation results
        shutil.make_archive(os.path.join(num + '_WMGM'), 'zip', segmentations)

        # Move results files to data directory
        if os.path.isfile(os.path.join(curdir, num + '_WMGM.txt')):
            os.remove(os.path.join(curdir, num + '_WMGM.txt'))
        shutil.move(os.path.join(num + '_WMGM.txt'), os.path.join(curdir, num + '_WMGM.txt'))

        if os.path.isfile(os.path.join(curdir, num + '_WMGM.zip')):
            os.remove(os.path.join(curdir, num + '_WMGM.zip'))
        shutil.move(os.path.join(num + '_WMGM.zip'), os.path.join(curdir, num + '_WMGM.zip'))

    # back to current folder
    os.chdir(curdir)

    return results

if __name__ == "__main__":
    args = get_parameters()
    main(args.input, args.seg, args.gmseg, num=args.num, register=args.register, output_dir=args.output_dir,
         verbose=args.verbose)


import sys
sys.setrecursionlimit(10**7)

dx = [1, -1, 0, 0, 1, -1, -1, 1]
dy = [0, 0, -1, 1, 1, 1, -1, -1]

def dfs(x, y):
    graph[x][y] = 0

    for i in range(8):
        nx = dx[i] + x
        ny = dy[i] + y

        if 0 <= nx < h and 0 <= ny < w and graph[nx][ny] == 1:
            dfs(nx, ny)

while True:
    w, h = map(int, sys.stdin.readline().split())

    if w == 0 and h == 0:
        break

    graph = []
    count = 0

    for _ in range(h):
        graph.append(list(map(int, sys.stdin.readline().split())))

    for i in range(h):
        for j in range(w):
            if graph[i][j] == 1:
                dfs(i, j)
                count += 1

    print(count)


import a1
import unittest

class TestSwapK(unittest.TestCase):
    """ Test class for function a1.swap_k. """

    def test_one(self):
      """
	Test list with only one element
      """
      L = [1]
      a1.swap_k(L, 1)
      self.assertEquals(L[0],1)

    def test_empty(self):
      """
        Test empty list
      """
      L = []
      a1.swap_k(L, 0)
      self.assertEquals(len(L),0)

    def test_incorrect(self):
      """
	Test that error is raised when we pass incorrect args
      """
      self.assertRaises(IndexError, a1.swap_k, [], -1)
      self.assertRaises(IndexError, a1.swap_k, [1,2], len([1,2])+1)

    def test_swap(self):
      """
	Test that error is raised when we pass incorrect args
      """
      L = [1,2,3]
      expected = [3,2,1]
      a1.swap_k(L, 1)
      self.assertEquals(L,expected)

      L = [1,2,3,4,5,6]
      expected = [5,6,3,4,1,2]
      a1.swap_k(L, 2)
      self.assertEquals(L,expected)

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


from odoo import api, fields, models, _
from odoo.addons import decimal_precision as dp

class ProductTemplate(models.Model):
    _inherit='product.template'

    standard_price = fields.Float(
        'Cost', compute='_compute_standard_price', store=True,
        inverse='_set_standard_price', search='_search_standard_price',
        digits=dp.get_precision('Product Price'), groups="base.group_user",
        help="Cost used for stock valuation in standard price and as a first price to set in average/fifo. "
             "Also used as a base price for pricelists. "
             "Expressed in the default unit of measure of the product. ")


# -*- coding: utf-8 -*-
"""
Created on Sun Sep 12 13:03:16 2021

@author: yixing
"""

class Solution:
    def containsDuplicate(self, nums: List[int]) -> bool:

        dic={}
        for a in nums:
            if a not in dic:
                dic[a]=1
            else:
                return True

        return False


#CODE 4
# check street name ab.
import xml.etree.cElementTree as ET
from collections import defaultdict
import re
import pprint

OSMFILE = "harrisburg_pennsylvania.osm"
street_type_re = re.compile(r'\b\S+\.?$', re.IGNORECASE)

expected = ["Street", "Avenue", "Boulevard", "Drive", "Court", "Place", "Square", "Lane", "Road",
            "Trail", "Parkway", "Commons"]

def audit_street_type(street_types, street_name):
    m = street_type_re.search(street_name)#search to see if street_name is in

    if m:
        street_type = m.group()
        if street_type not in expected:
            street_types[street_type].add(street_name)

def is_street_name(elem):
    return (elem.attrib['k'] == "addr:street")

def audit(osmfile):
    #osm_file = open(osmfile, "r")
    street_types = defaultdict(set)# this dict has value with default type of set
    for _, elem in ET.iterparse(OSMFILE):
         for tag in elem.iter("tag"):
            if is_street_name(tag):
                 audit_street_type(street_types, tag.attrib['v'])

    return street_types

def test():
    st_types = audit(OSMFILE)

    pprint.pprint(dict(st_types))

if __name__ == '__main__':
    test()


# define helper functions for pipeline of data processing
# diffs and rolling averages both require a date column (of date type)
def create_proportion(data, col_name, total_col, output_name=False):
    if not output_name:
        output_name = col_name + '_rate'
    data_copy = data.copy()
    data_copy.loc[:,output_name] = data[col_name] / data[total_col]
    return data_copy

def create_proportions(data, cols, total_col):
    for col_name in cols:
        data = create_proportion(data, col_name, total_col)
    return data

def create_diff(data, col_name, output_name=False):
    if not output_name:
        output_name = col_name + '_diff'
    sorted_data = data.sort_values(by="Date")
    sorted_data.loc[:,output_name] = sorted_data.loc[:,col_name].diff()
    return sorted_data

def create_diffs(data, cols):
    for col_name in cols:
        data = create_diff(data, col_name)
    return data

def create_rolling_average_by_time(data, col_name,  window_offset,
                                   output_name = False, date_col="Date"):
    if not output_name:
        output_name = col_name + '_rolling_avg_' + str(window_offset)
    temp_df = (data[[date_col,col_name]]
           .set_index(date_col)
           .sort_index()
           .rolling(window_offset).mean().reset_index()
           .rename(columns={col_name:output_name})
          )
    #requires day col to be unique!
    data = data.merge(temp_df, how="left", left_on=date_col, right_on=date_col)
    return data

def create_rolling_averages_by_time(data, cols, window_offset):
    for col_name in cols:
        data = create_rolling_average_by_time(data, col_name, window_offset)
    return data


IS_PRINT_ABOUT_DATA = True

IS_PRINT_ON_GOOD_MSGS = True
#if above is true:
PRINT_EVERY_NUM_GOOD_MSGS = 50

NO_MSG_LIMIT = 150
IS_PRINT_ON_NO_FORMULA_MSG = True
#if the above is true:
PRINT_EVERY_NUM_NO_FORMULA_MSGS = 5

IS_TIME_CODE_WITH_TIMER = True

PICKLED_FILE_NAME = 'state.pickle'

if IS_TIME_CODE_WITH_TIMER:
    from timeit import default_timer as timer

import os, sys, pathlib
currentPath = pathlib.Path( os.path.dirname(__file__) )
relativePath = currentPath.parent
sys.path.append(str(relativePath))

from StateEst_Utils.MessagesClass import messages , NoFormulaMessages
from data.Client import ControlClient

import pickle

class ParserObject():

    def __init__(self):
        self._client = ControlClient()
        self._start_client()
        self._message_timeout = 0.000001
        #
        self._data = []   # Here we save all the data for later use
        self._no_msg_counter = 0  # How many times did we fail to read, after  NO_MSG_LIMIT times, we will stop.
        self._good_msg_counter = 0
        self._is_finish = False

    def _start_client(self):
        print(f"starting client")
        self._client.connect(1)
        self._client.set_read_delay(0)
        self._client.start()

    def _process_data( self , formula_state_msg ):
        """unpack data and time:"""
        Data = messages.state_est.FormulaState()
        formula_state_msg.data.Unpack(Data)
        time_in_milisec = formula_state_msg.header.timestamp.ToMilliseconds()

        '''parse data'''
        #time:
        time=time_in_milisec

        # Car State Estimation:
        x  = Data.current_state.position.x
        y  = Data.current_state.position.y
        x_deviation = Data.current_state.position_deviation.x
        y_deviation = Data.current_state.position_deviation.y

        steering_angle = Data.current_state.steering_angle
        steering_angle_deviation = Data.current_state.steering_angle_deviation

        theta = Data.current_state.theta
        theta_deviation = Data.current_state.theta_deviation
        theta_dot = Data.current_state.theta_dot
        theta_dot_deviation = Data.current_state.theta_dot_deviation

        Vx = Data.current_state.velocity.x
        Vy = Data.current_state.velocity.y
        Vx_deviation = Data.current_state.velocity_deviation.x
        Vy_deviation = Data.current_state.velocity_deviation.y

        car_state = {'x' : x , 'y' : y ,  'x_deviation' : x_deviation , 'y_deviation' : y_deviation ,
                     'steering_angle': steering_angle , 'steering_angle_deviation': steering_angle_deviation,
                      'theta': theta , 'theta_deviation': theta_deviation ,
                      'theta_dot':theta_dot , 'theta_dot_deviation': theta_dot_deviation ,
                      'Vx':Vx , 'Vy':Vy , 'Vx_deviation':Vx_deviation , 'Vy_deviation':Vy_deviation}

        distance_to_finish = Data.distance_to_finish

        #Cones:
        left_cones = []
        for cone in Data.left_bound_cones:
            cone_dict = parse_data_from_cone(cone)
            left_cones.append(cone_dict)

        right_cones = []
        for cone in Data.right_bound_cones:
            cone_dict = parse_data_from_cone(cone)
            right_cones.append(cone_dict)

        # Ground Truth if exist:
        '''save data'''
        current_data_dict = {
                            'time':time ,
                            'car_state':car_state ,
                            'distance_to_finish':distance_to_finish ,
                            'left_cones':left_cones,
                            'right_cones':right_cones
                            }
        self._data.append(current_data_dict)

    def _act_on_msg(self):
        self._no_msg_counter = 0
        self._good_msg_counter = self._good_msg_counter + 1
        #Print every PRINT_EVERY_NUM_GOOD_MSGS msgs good:
        if ( (self._good_msg_counter % PRINT_EVERY_NUM_GOOD_MSGS ) == 0 ) and (IS_PRINT_ON_GOOD_MSGS):
            print(f"parsed msg number {self._good_msg_counter:6}")

    def _act_on_no_msg(self):
        self._no_msg_counter = self._no_msg_counter + 1
        #Print error every certein amount of errors.  And only if we want to print
        if ( ( self._no_msg_counter % PRINT_EVERY_NUM_NO_FORMULA_MSGS ) == 0  ) and ( IS_PRINT_ON_NO_FORMULA_MSG ):
            print(f"no state msg {self._no_msg_counter:03}")
        if self._no_msg_counter > NO_MSG_LIMIT:
            self._is_finish = True

    def _parse_messages_one_by_one(self):
        print(f"Parsing Data from Message")
        while not self._is_finish:
            try:
                server_msg = self._client.pop_server_message()
                if server_msg is not None:
                    if server_msg.data.Is(messages.server.ExitMessage.DESCRIPTOR):
                         self._is_finish = True
            except NoFormulaMessages:
                pass
            except Exception as e:
                print(f"{e}")

            try:
                formula_state_msg = self._client.get_formula_state_message(timeout= self._message_timeout )
                self._process_data( formula_state_msg )
                self._act_on_msg()
            except NoFormulaMessages:
                self._act_on_no_msg()
            except Exception as e:
                print(f"{e}")

        #when is_finsied
        return self._data

def ParseDataFromStateEstMessage():
    #Check if existing a pickled data:
    is_exist_pickled , data = load_data(PICKLED_FILE_NAME)
    if is_exist_pickled:
        pass #do nothing
    else:
        #time it:
        if IS_TIME_CODE_WITH_TIMER:
            execution_time_start = timer()
        # extract data from .messages file:
        Parser = ParserObject()
        data = Parser._parse_messages_one_by_one()
        #time it:
        if IS_TIME_CODE_WITH_TIMER:
            execution_time_finish = timer()
            print(f"Parsing took {execution_time_finish - execution_time_start} ms")
        #Save data for faster load, at later time
        save_data(data , PICKLED_FILE_NAME)

    #Print info:
    if IS_PRINT_ABOUT_DATA:
        print(f"Data length is {len(data)}")
    return data

def save_data(data,fileName):
    fullpath = os.path.join( str(currentPath.parent) ,'data',fileName )
    pickle.dump( data, open( fullpath, "wb" ) )
    print(f"Saving data file for later use.")

def load_data(fileName):
    #Try to pickle data:
    try:
        fullpath = os.path.join( str(currentPath.parent) , 'data' , fileName )
        pickled_data =  pickle.load(  open( fullpath, "rb" )  )
        if IS_PRINT_ABOUT_DATA:
            print(f"Using pre-processed data file.")
        is_exist_pickled = True
        data = pickled_data
    # if failed, send to main function to parse from .messages file:
    except Exception as e:
        if e.args[1] == 'No such file or directory':
            if IS_PRINT_ABOUT_DATA:
                print(f"No pre-processed data file.")
            is_exist_pickled = False
            data = None
        else:
            raise e
    return is_exist_pickled , data

def parse_data_from_cone(cone):
    cone_dict = {
                'angle_from_car':cone.alpha,
                'distance_from_Car':cone.r,
                'id': cone.cone_id ,
                'x' :cone.position.x ,
                'y' :cone.position.y ,
                'position_deviation':cone.position_deviation,
                'type':cone.type
                }
    return cone_dict

if __name__ == "__main__":
    data = ParseDataFromStateEstMessage()
    print(f"Done. First element in data is:")
    print(f"{data[0]}")


# advent of code
# response to the challenge by geir owe
# day8 - challenge: https://adventofcode.com/2020/day/8

#start function
def get_all_instructions(boot_file):
    allInstruct = []
    #move all instructions in the boot file into a list
    for instruction in boot_file:
        instruction = instruction.strip()
        allInstruct.append(instruction)
    return allInstruct
#end get_all_instructions function

#start function
def check_if_about_to_loop(newPos, visitedPos, aboutToLoop):
    #check if the position for he instruction has been visited before
    #if so, we are about to loop
    theSet = set(visitedPos)
    if newPos in theSet:
        print()
        print('------ about to loop -------')
        aboutToLoop = True
    else:
        visitedPos.append(newPos)       #store what position we are at; and validate aginst it later
    return visitedPos, aboutToLoop
#end check_if_about_to_loop function

#start function
def jump_around(theInstruction, currentPos, theAccumulator):
    #split at ' ' - the right part cotnains the jump "
    # the left part contains the instruction
    splitInstr = theInstruction.split(' ')
    theAccContent = splitInstr[0].strip()
    theJump = int(splitInstr[1].strip())

    #nop stands for No OPeration - it does nothing. The instruction immediately below it
    # is executed next
    if (theAccContent == 'nop'):
        theJump = 1

    #check if the accumulator value is to be increased
    #if so, increase the accumulator and move to next line
    if theAccContent == 'acc':
        theAccumulator = theAccumulator + theJump
        theJump = 1                 #move on to next instruction

    #calculate new  position due to the jump
    newPos = currentPos + theJump
    if newPos < 0:                  # if this happen it is a bug in the puzle input
        newPos = 0
    return newPos, theAccumulator
#end jump_around function

#read the test puzzle input
#boot_file = open('day8_test_puzzle_input.txt', 'r')
#read the puzzle input
boot_file = open('day8_puzzle_input2.txt', 'r')

#the challenge:
# The moment the program tries to run any instruction a second time, you know it will never terminate.
# Run your copy of the boot code. Immediately before any instruction
# is executed a second time, what value is in the accumulator?
aboutToLoop = False
theAccumulator = 0
calcPos = []

#get all instructions from boot file
allInstructions = get_all_instructions(boot_file)

newPos=0
#execute instructions until loop is detected
while aboutToLoop != True:
    #check if we are about to loop
    calcPos, aboutToLoop = check_if_about_to_loop(newPos, calcPos, aboutToLoop)
    if aboutToLoop:
        print('... what value is in the accumulator? ', theAccumulator)
        print()
        print('... where we have been: ')
        print(calcPos)
        print()
    else:
        #jump to position as specified in instruction and collect accumulator value when relevant
        currentPos = newPos
        newPos, theAccumulator = jump_around(allInstructions[newPos], currentPos, theAccumulator)


from pybullet_utils import bullet_client
import time
import math
import motion_capture_data
from pybullet_envs.deep_mimic.env import humanoid_stable_pd
import pybullet_data
import pybullet as p1
import humanoid_pose_interpolator
import numpy as np

pybullet_client = bullet_client.BulletClient(connection_mode=p1.GUI)

pybullet_client.setAdditionalSearchPath(pybullet_data.getDataPath())
z2y = pybullet_client.getQuaternionFromEuler([-math.pi * 0.5, 0, 0])
#planeId = pybullet_client.loadURDF("plane.urdf",[0,0,0],z2y)
planeId = pybullet_client.loadURDF("plane_implicit.urdf", [0, 0, 0],
                                   z2y,
                                   useMaximalCoordinates=True)
pybullet_client.changeDynamics(planeId, linkIndex=-1, lateralFriction=0.9)
#print("planeId=",planeId)

pybullet_client.configureDebugVisualizer(pybullet_client.COV_ENABLE_Y_AXIS_UP, 1)
pybullet_client.setGravity(0, -9.8, 0)

pybullet_client.setPhysicsEngineParameter(numSolverIterations=10)

mocapData = motion_capture_data.MotionCaptureData()
#motionPath = pybullet_data.getDataPath()+"/data/motions/humanoid3d_walk.txt"
motionPath = pybullet_data.getDataPath() + "/data/motions/humanoid3d_backflip.txt"
mocapData.Load(motionPath)
timeStep = 1. / 600
useFixedBase = False
humanoid = humanoid_stable_pd.HumanoidStablePD(pybullet_client, mocapData, timeStep, useFixedBase)
isInitialized = True

pybullet_client.setTimeStep(timeStep)
pybullet_client.setPhysicsEngineParameter(numSubSteps=2)
timeId = pybullet_client.addUserDebugParameter("time", 0, 10, 0)

def isKeyTriggered(keys, key):
  o = ord(key)
  if o in keys:
    return keys[ord(key)] & pybullet_client.KEY_WAS_TRIGGERED
  return False

animating = False
singleStep = False
humanoid.resetPose()
t = 0
while (1):

  keys = pybullet_client.getKeyboardEvents()
  #print(keys)
  if isKeyTriggered(keys, ' '):
    animating = not animating

  if isKeyTriggered(keys, 'b'):
    singleStep = True

  if animating or singleStep:

    singleStep = False
    #t = pybullet_client.readUserDebugParameter(timeId)
    #print("t=",t)
    for i in range(1):

      #print("t=", t)
      humanoid.setSimTime(t)

      humanoid.computePose(humanoid._frameFraction)
      pose = humanoid._poseInterpolator
      #humanoid.initializePose(pose=pose, phys_model = humanoid._sim_model, initBase=True, initializeVelocity=True)
      #humanoid.resetPose()

      desiredPose = humanoid.computePose(humanoid._frameFraction)

      #desiredPose = desiredPose.GetPose()
      #curPose = HumanoidPoseInterpolator()
      #curPose.reset()

      s = humanoid.getState()
      #np.savetxt("pb_record_state_s.csv", s, delimiter=",")
      maxForces = [
          0, 0, 0, 0, 0, 0, 0, 200, 200, 200, 200, 50, 50, 50, 50, 200, 200, 200, 200, 150, 90, 90,
          90, 90, 100, 100, 100, 100, 60, 200, 200, 200, 200, 150, 90, 90, 90, 90, 100, 100, 100,
          100, 60
      ]

      usePythonStablePD = False
      if usePythonStablePD:
        taus = humanoid.computePDForces(desiredPose, desiredVelocities=None, maxForces=maxForces)
        #Print("taus=",taus)
        humanoid.applyPDForces(taus)
      else:
        humanoid.computeAndApplyPDForces(desiredPose,maxForces=maxForces)

      pybullet_client.stepSimulation()
      t += 1. / 600.

  time.sleep(1. / 600.)


import re
from urllib import parse
import scrapy
from scrapy.http import Request
from scrapy.selector import Selector
from QADoorSpider.items import ArticleItem

class TuikuSpider(scrapy.Spider):
    name = "tuiku"
    # allowed_domains = ["tuicool.com"]
    start_urls = (
        'http://www.tuicool.com/topics/11130000?st=0&lang=1&pn=0',
    )

    headers = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, sdch',
        'Accept-Language': 'zh-CN,zh;q=0.8',
        'Connection': 'keep-alive',
        'Cookie': 'UM_distinctid=15b13ff61121b1-0679fa0202aad5-1421150f-140000-15b13ff611330d; CNZZDATA5541078=cnzz_eid%3D604530066-1490687638-null%26ntime%3D1500176950; _tuicool_session=BAh7C0kiD3Nlc3Npb25faWQGOgZFRkkiJWZjNTc3MjQxMWU0ZjQ5NmU1N2M4NGViNDc3OTRiODZjBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMVpEL29teHRTTnhRR1FIeWwycVlOTjIxNzBFUVJpMHhiY1Vnb2swMUh2b009BjsARkkiDHVzZXJfaWQGOwBGaQMWkgJJIg5yZXR1cm5fdG8GOwBGSSIsaHR0cDovL3d3dy50dWljb29sLmNvbS9hcnRpY2xlcy9CYlVKRmI2BjsARkkiDnVzZXJfY2l0eQY7AEZJIgvlhajlm70GOwBUSSITdXNlcl9jaXR5X2NvZGUGOwBGSSIIYWxsBjsAVA%3D%3D--7424f2078d34e840c9197dd1f58c2db057aab636',
        'Host': 'www.tuicool.com',
        'If-None-Match': 'W/"6da29458c549b4788210314b12c8e718"',
        'Referer': 'http://www.tuicool.com/topics',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
    }

    def start_requests(self):
        for page in range(0, 10):
            if page>1:
                self.headers['Referer'] = 'http://www.tuicool.com/topics/11130000?st=0&lang=1&pn={}'.format(page-1)
            yield Request('http://www.tuicool.com/topics/11130000?st=0&lang=1&pn={}'.format(page), headers=self.headers, callback=self.parse_list)

    def parse_list(self, response):
        selector = Selector(response)
        titles = selector.xpath('//div[@class="title"]/a/@title').extract()
        urls = selector.xpath('//div[@class="title"]/a/@href').extract()
        for title, url in zip(titles, urls):
            q_item = ArticleItem()
            q_item['title'] = title.strip()
            q_item['url'] = parse.urljoin('http://www.tuicool.com/', url)
            # q_item['_id'] = q_item['url'].split('/')[-1]
            yield Request(url=q_item['url'], headers=self.headers, meta={'item': q_item}, callback=self.parse_detail)

    def parse_detail(self, response):
        q_item = response.meta['item']
        selector = Selector(response)
        q_item['pub_date'] = selector.xpath('//span[@class="timestamp"]/text()').extract_first().strip('时间').strip()
        q_item['source'] = selector.xpath('//div[@class="source"]/a[@class="cut cut70"]/text()').extract_first().strip()
        q_item['tags'] = selector.xpath('//span[@class="new-label"]/text()').extract()
        q_item['content'] = selector.xpath('//div[@class="article_body"]').extract_first()
        yield q_item


import streamlit as st
import pandas as pd
import base64
import matplotlib.pyplot as plt
import yfinance as yf
from langchain.agents import create_pandas_dataframe_agent
from langchain.chat_models import ChatOpenAI
from langchain.agents.agent_types import AgentType
import streamlit as st
from langchain.llms import OpenAI
import pandas as pd
import os

st.set_option('deprecation.showPyplotGlobalUse', False)
os.environ["OPENAI_API_KEY"]=st.secrets["OPENAI_API_KEY"]

st.title('Stock price app')

st.markdown("""
This app retrieves the list of the **S&P 500** (from Wikipedia) and its corresponding **stock closing price** (year-to-date)!
* **Data source:** [Wikipedia](https://en.wikipedia.org/wiki/List_of_S%26P_500_companies).
""")

st.sidebar.header('User Input Features')

# Web scraping of S&P 500 data
#
@st.cache_data
# @st.cache
def load_data():
    url = 'https://en.wikipedia.org/wiki/List_of_S%26P_500_companies'
    html = pd.read_html(url, header = 0)
    df = html[0]
    return df

df = load_data()
sector = df.groupby('GICS Sector')

# Sidebar - Sector selection
sorted_sector_unique = sorted( df['GICS Sector'].unique() )
selected_sector = st.sidebar.multiselect('Sector', sorted_sector_unique, sorted_sector_unique)

# Filtering data
df_selected_sector = df[ (df['GICS Sector'].isin(selected_sector)) ]

st.header('Dataset retrieved from Wikipedia')
st.write('Data Dimension: ' + str(df_selected_sector.shape[0]) + ' rows and ' + str(df_selected_sector.shape[1]) + ' columns.')
st.dataframe(df_selected_sector)

# Download S&P500 data
# https://discuss.streamlit.io/t/how-to-download-file-in-streamlit/1806
def filedownload(df):
    csv = df.to_csv(index=False)
    b64 = base64.b64encode(csv.encode()).decode()  # strings <-> bytes conversions
    href = f'Download CSV File'
    return href

st.markdown(filedownload(df_selected_sector), unsafe_allow_html=True)

# https://pypi.org/project/yfinance/

data = yf.download(
        tickers = list(df_selected_sector[:10].Symbol),
        period = "ytd",
        interval = "1d",
        group_by = 'ticker',
        auto_adjust = True,
        prepost = True,
        threads = True,
        proxy = None
    )

# Plot Closing Price of Query Symbol
def price_plot(symbol):
  df = pd.DataFrame(data[symbol].Close)
  df['Date'] = df.index
#   fig,  = plt.subplots()
  plt.fill_between(df.Date, df.Close, color='skyblue', alpha=0.3)
  plt.plot(df.Date, df.Close, color='skyblue', alpha=0.8)
  plt.xticks(rotation=90)
  plt.title(symbol, fontweight='bold')
  plt.xlabel('Date', fontweight='bold')
  plt.ylabel('Closing Price', fontweight='bold')
  return st.pyplot()

num_company = st.sidebar.slider('Number of Companies', 1, 5)

if st.button('Show Plots'):
    st.header('Stock Closing Price')
    for i in list(df_selected_sector.Symbol)[:num_company]:
        price_plot(i)

pandas_df = df

agent = create_pandas_dataframe_agent(OpenAI(temperature=0), df, verbose=True)

agent = create_pandas_dataframe_agent(
    ChatOpenAI(temperature=0.7, model="gpt-3.5-turbo-0613"),
    df,
    verbose=True,
    agent_type=AgentType.OPENAI_FUNCTIONS,
)

with st.form('my_form'):
  text = st.text_area('Enter question on data:', 'What is the data about?')
  submitted = st.form_submit_button('Submit')
  output = agent.run(text)


import os

import pandas as pd
import pytest

import cudf
from cudf import date_range
from cudf.testing._utils import assert_eq

def _get_all_zones():
    zones = []
    for root, dirs, files in os.walk("/usr/share/zoneinfo"):
        for f in files:
            zone_name = ("/".join([root, f])).lstrip("/usr/share/zoneinfo")
            try:
                _ = pd.DatetimeTZDtype("ns", zone_name)
            except Exception:
                continue
            zones.append(zone_name)
    return zones

# NOTE: ALL_TIME_ZONES is a very large list; we likely do NOT want to
# use it for more than a handful of tests
ALL_TIME_ZONES = _get_all_zones()

@pytest.fixture(params=["ns", "us", "ms", "s"])
def unit(request):
    return request.param

@pytest.fixture(
    params=["America/New_York", "Asia/Tokyo", "CET", "Etc/GMT+1", "UTC"]
)
def tz(request):
    return request.param

@pytest.mark.parametrize("zone_name", ALL_TIME_ZONES)
def test_tz_localize(unit, zone_name):
    s = cudf.Series(date_range("2001-01-01", "2001-01-02", freq="1s"))
    s = s.astype(f"<M8[{unit}]")
    s = s.dt.tz_localize(zone_name)
    assert isinstance(s.dtype, pd.DatetimeTZDtype)
    assert s.dtype.unit == unit
    assert str(s.dtype.tz) == zone_name

@pytest.mark.parametrize("zone_name", ALL_TIME_ZONES)
def test_localize_ambiguous(request, unit, zone_name):
    request.applymarker(
        pytest.mark.xfail(
            condition=(zone_name == "America/Metlakatla"),
            reason="https://www.timeanddate.com/news/time/metlakatla-quits-dst.html",  # noqa: E501
        )
    )
    s = cudf.Series(
        [
            "2018-11-04 00:30:00",
            "2018-11-04 01:00:00",
            "2018-11-04 01:30:00",
            "2018-11-04 02:00:00",
            None,
            "2018-11-04 02:30:00",
        ],
        dtype=f"datetime64[{unit}]",
    )
    expect = s.to_pandas().dt.tz_localize(
        zone_name, ambiguous="NaT", nonexistent="NaT"
    )
    got = s.dt.tz_localize(zone_name)
    assert_eq(expect, got)

@pytest.mark.parametrize("zone_name", ALL_TIME_ZONES)
def test_localize_nonexistent(request, unit, zone_name):
    request.applymarker(
        pytest.mark.xfail(
            condition=(zone_name == "America/Grand_Turk"),
            reason="https://www.worldtimezone.com/dst_news/dst_news_turkscaicos03.html",  # noqa: E501
        )
    )
    s = cudf.Series(
        [
            "2018-03-11 01:30:00",
            "2018-03-11 02:00:00",
            "2018-03-11 02:30:00",
            "2018-03-11 03:00:00",
            None,
            "2018-03-11 03:30:00",
        ],
        dtype=f"datetime64[{unit}]",
    )
    expect = s.to_pandas().dt.tz_localize(
        zone_name, ambiguous="NaT", nonexistent="NaT"
    )
    got = s.dt.tz_localize(zone_name)
    assert_eq(expect, got)

def test_delocalize(unit, tz):
    psr = pd.Series(
        pd.date_range("2001-01-01", "2001-01-02", freq="1s")
    ).astype(f"datetime64[{unit}]")
    sr = cudf.from_pandas(psr)

    expect = psr.dt.tz_localize(tz).dt.tz_localize(None)
    got = sr.dt.tz_localize(tz).dt.tz_localize(None)
    assert_eq(expect, got)

def test_delocalize_naive():
    # delocalizing naive datetimes should be a no-op
    psr = pd.Series(["2001-01-01"], dtype="datetime64[ns]")
    sr = cudf.from_pandas(psr)

    expect = psr.dt.tz_localize(None)
    got = sr.dt.tz_localize(None)
    assert_eq(expect, got)

@pytest.mark.parametrize(
    "from_tz", ["Europe/London", "America/Chicago", "UTC"]
)
@pytest.mark.parametrize(
    "to_tz", ["Europe/London", "America/Chicago", "UTC", None]
)
def test_convert(from_tz, to_tz):
    ps = pd.Series(pd.date_range("2023-01-01", periods=3, freq="H"))
    gs = cudf.from_pandas(ps)
    ps = ps.dt.tz_localize(from_tz)
    gs = gs.dt.tz_localize(from_tz)
    expect = ps.dt.tz_convert(to_tz)
    got = gs.dt.tz_convert(to_tz)
    assert_eq(expect, got)

def test_convert_from_naive():
    gs = cudf.Series(cudf.date_range("2023-01-01", periods=3, freq="H"))
    with pytest.raises(TypeError):
        gs.dt.tz_convert("America/New_York")

@pytest.mark.parametrize(
    "data,original_timezone,target_timezone",
    [
        # DST transition:
        (["2023-03-12 01:30:00"], "America/New_York", "America/Los_Angeles"),
        # crossing the international date line:
        (["2023-05-17 23:30:00"], "Pacific/Auckland", "America/Los_Angeles"),
        # timezone with non-integer offset:
        (["2023-05-17 12:00:00"], "Asia/Kolkata", "Australia/Eucla"),
        # timezone with negative offset:
        (["2023-05-17 09:00:00"], "America/Los_Angeles", "Pacific/Auckland"),
        # conversion across multiple days:
        (["2023-05-16 23:30:00"], "America/New_York", "Asia/Kolkata"),
        # timezone with half-hour offset:
        (["2023-05-17 12:00:00"], "Asia/Kolkata", "Australia/Adelaide"),
        # timezone conversion with a timestamp in the future:
        (["2025-01-01 00:00:00"], "America/New_York", "Europe/London"),
        # timezone conversion with a timestamp in the past:
        (["2000-01-01 12:00:00"], "Europe/Paris", "America/Los_Angeles"),
        # timezone conversion with a timestamp at midnight:
        (["2023-05-17 00:00:00"], "Asia/Tokyo", "Europe/Paris"),
    ],
)
def test_convert_edge_cases(data, original_timezone, target_timezone):
    ps = pd.Series(data, dtype="datetime64[s]").dt.tz_localize(
        original_timezone
    )
    gs = cudf.Series(data, dtype="datetime64[s]").dt.tz_localize(
        original_timezone
    )
    expect = ps.dt.tz_convert(target_timezone)
    got = gs.dt.tz_convert(target_timezone)
    assert_eq(expect, got)


import datetime, jwt, time
from app.dao.userDao import UserDao
from flask import jsonify
from .. import common
class Auth():
    @staticmethod
    def encode_auth_token(user_id, login_time):
        """
        生成认证Token
        :param user_id: int
        :param login_time: int(timestamp)
        :return: string
        """
        try:
            payload = {
                'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, seconds=10),
                'iat': datetime.datetime.utcnow(),
                'iss': 'ken',
                'data': {
                    'id':user_id,
                    'login_time': login_time
                }
            }
            return jwt.encode(
                payload,
                'secret',
                algorithm='HS256'
            )
        except Exception  as e:
            return e

    @staticmethod
    def decode_auth_token(auth_token):
        """
        验证Token
        :param auth_token:
        :return: integer|string
        """
        try:
            payload = jwt.decode(auth_token, 'secret', options= {'verify_exp':False})
            if ('data' in payload and 'id' in payload['data']):
                return payload
            else:
                raise jwt.InvalidTokenError

        except jwt.ExpiredSignatureError:
            return "Token过期"
        except jwt.InvalidTokenError:
            return "无效的Token"

    def authenticate(self, username, password):
        """
        用户登录，登录成功返回token，写将登录时间写入数据库；登录失败返回失败原因
        :param password:
        :return: json
        """
        userDao = UserDao()
        user = userDao.search(username)
        if (user is None):
            return jsonify(common.falseReturn('', '找不到用户'))
        else:
            if (user.password == password):
                login_time = int(time.time())
                token = self.encode_auth_token(user.username, login_time)
                return jsonify(common.trueReturn(token.decode(), '登陆成功'))
            else:
                return jsonify(common.falseReturn('', '密码不正确'))

    def identify(self, request):
        """
        用户鉴权
        :return: list
        """
        auth_header = request.headers.get('Authorization')
        if (auth_header):
            auth_tokenArr = auth_header.split(" ")
            if (not auth_tokenArr or auth_tokenArr[0]!= 'JWT' or len(auth_tokenArr) != 2 ):
                result = common.falseReturn('','请传递正确的验证头信息')
            else:
                auth_token = auth_tokenArr[1]
                payload = self.decode_auth_token(auth_token)
                if not isinstance(payload, str):
                    userDao = UserDao()
                    user = userDao.search(payload['data']['id'])
                    if (user is None):
                        result = common.falseReturn('', '找不到该用户信息')
                    else:
                        result = common.trueReturn('', '请求成功')
                else:
                    result = common.falseReturn('', payload)
        else:
            result = common.falseReturn('','没有提供认证token')
        return result


import json

# DEFAULT FILEPATH
default_filepath = "data/default_save.json"

def load(filepath: str=default_filepath):
    """ Load json data from filepath """
    new_data = None
    with open(filepath, 'r') as in_file:
        new_data = json.load(in_file)
    return new_data

def save(save_data, filepath: str=default_filepath):
    """ Save data into a json at given filepath """
    with open(filepath, 'w') as out_file:
        json.dump(save_data, out_file, indent=4)
    return True


import snscrape.modules.twitter as sntwitter
import pandas as pd
query = "Uk"
#query = "from:anyasorchigozie since:2023-01-01 until:2023-05-31"
#query = "from:sterlinghelp"
tweets =[]
limits = 50

for tweet in sntwitter.TwitterSearchScraper(query).get_items():
    # print(vars(tweet))
    # break
    if len(tweets) == limits:
        break
    else:
        tweets.append([tweet.date, tweet.user.username, tweet.user.displayname, tweet.content])

df = pd.DataFrame(tweets, columns=['Date', 'User', 'Display Name','Tweet'])
df.to_csv("aaa.csv")
print(df)


"""
data_set1 = [11,22,33,44,55]

# Loop or Iterate over this data list using a Loop
for value1 in data_set1:
    print(value1)

# Loop or Iterate over this data list using an Iterator
# Create an iterator using the iter function
itr = iter(data_set1)
print("----")
# Call the print to display the next value
print(itr.__next__())
# some code logic
print(itr.__next__())
# some code logic
# some code logic
# some code logic
print(itr.__next__())
# some code logic
# some code logic
print(itr.__next__())
print(itr.__next__())
print(itr.__next__())
"""

class EvenOddNumbers:

    def __init__(self, max):
        self.max = max
        self.number = 0

    # Return the custom __next__ value specified within this function
    def __next__(self):

        # Increment the self.number  once everytime the __next__() is called
        self.number += 1

        # Exit when the iterations reach the MAX defined in the constructor
        if self.number > self.max:
            print("Max Number of Iterations reached: ", self.max)
        else:

            # Check if the CURRENT number is EVEN or ODD and report it
            if self.number%2 != 0:
                print(str(self.number),"is Odd Number")
            else:
                print(str(self.number),"is Even Number")

x = EvenOddNumbers(6)
x.__next__()
x.__next__()
x.__next__()
x.__next__()
x.__next__()
x.__next__()
x.__next__()
x.__next__()
x.__next__()
x.__next__()
x.__next__()
x.__next__()
x.__next__()
x.__next__()


import socket

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

server_ip = socket.gethostname()
server_port = 1234
server_addr = (server_ip, server_port)

msg = "Hello from client."

print(f"UDP server IP: {server_ip}")
print(f"UDP server port: {server_port}")

sock.sendto(msg.encode(), server_addr)

msgFromServer = sock.recvfrom(1024)

print(f"Message recieved from server: {msgFromServer[0].decode()}")


# import cv2
import numpy as np
# from skimage.transform import resize
from tensorflow.keras.models import model_from_json
from tensorflow.keras.preprocessing.image import img_to_array
from tensorflow.keras import backend as kb
from PIL import Image

# loading model architecture and weights
def get_model(model_architecture_path, model_weights_path):
    global model

    # Model reconstruction from JSON file
    with open(model_architecture_path, 'r') as f:
        model = model_from_json(f.read())

    # Load weights into the model
    model.load_weights(model_weights_path)
    print(" * model loaded!")

def transform_image(image, target_size=(224, 224)):
    # image = PIL image
    image_array = np.asarray(image.convert("RGB"))
    image_array = image_array / 255.
    image_array = np.resize(image_array, target_size)
    return image_array

# def preprocess_image(image, target_size=(224, 224)):
#     # image = PIL imageload_image
#     if image.mode != "RGB":
#         image = image.convert("RGB")
#     image = image.resize(target_size)
#     image = img_to_array(image) / 225.
#     image = np.expand_dims(image, axis=0)
#     return image

def preprocess_image(image, target_size=(224, 224, 3)):
    image_array = np.asarray(image.convert("RGB"))
    image_array = image_array / 255.
    image_array = np.resize(image_array, target_size)
    image_tensor = np.expand_dims(image_array, axis=0)
    return image_tensor

def get_output_layer(model, layer_name):
    layer_dict = dict([(layer.name, layer) for layer in model.layers])
    layer = layer_dict[layer_name]
    return layer

# def heatmap(model, orginal_image, transformed_image, predicted_class_index):
#     # build tensorflow computational graph
#     # class_weight.shape = (1024, 14)
#     class_weights = model.layers[-1].get_weights()[0]
#     final_conv_layer = get_output_layer(model, "bn")
#     get_output = kb.function([model.layers[0].input], [
#                              final_conv_layer.output, model.layers[-1].output])
#     [conv_outputs, predictions] = get_output([np.array([transformed_image])])
#     # conv_outputs:  (7, 7, 1024) and predictions: (1, 14)
#     conv_outputs = conv_outputs[0, :, :, :]
#
#     # Create the class activation map.
#     cam = np.zeros(dtype=np.float32, shape=(conv_outputs.shape[:2]))
#     for i, w in enumerate(class_weights[predicted_class_index]):
#         cam += w * conv_outputs[:, :, i]
#
#     # merge the original image and the class activation map
#     cam /= np.max(cam)
#     cam = cv2.resize(cam, orginal_image.shape[:2])
#     heatmap = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET)
#     heatmap[np.where(cam < 0.2)] = 0
#     heatmap = heatmap * 0.5 + orginal_image
#     heatmap = (heatmap / np.max(heatmap)) * 255  # normalizing
#
#     cv2.imwrite("output/heatmap.jpg", heatmap)
#     return heatmap

def predict(model, image, target_size=(224, 224, 3), show_heatmap=False):
    processed_image = preprocess_image(image, target_size)
    prediction = model.predict(processed_image).tolist()
    # if show_heatmap:
    #     original_image = np.asarray(image.convert("RGB"))
    #     transformed_image = transform_image(image)
    #     predicted_class_index = np.argmax(prediction)
    #     heatmap_img = heatmap(model, original_image,
    #                           transformed_image, predicted_class_index)
    #     return (prediction, heatmap_img)
    # else:
    return prediction


import pandas as pd, numpy as np, os, sys, re, joblib

try:
    from fbprophet import Prophet
except Exception as exp:
    print(exp)

import data.io_utils as io_utils
import data.constants as constants

TIME_STAMP_FORMAT = '%d-%m-%Y-%H-%M-%S'

def prepare_data(df, log_flag = True):

    print("Running data prep for model build- Log transform?:", log_flag)

    if "Australia" in df.columns:

        confirmed = df[['Date','Australia']]

    else:

        confirmed = df[['Date','Confirmed']]

    confirmed.columns = ["ds", "y"]

    if log_flag:

        confirmed["y"] = np.log(confirmed["y"].astype("float"))

        confirmed["y"] = confirmed["y"].replace([np.inf, -np.inf], np.nan)#.dropna()

    confirmed.dropna(inplace=True)

    return confirmed

def fit_model(historical_data):

    m = Prophet(interval_width=0.95, daily_seasonality=True)

    m.fit(historical_data)

    return m

def get_predictions_dataframe(model, horizon=7, log_flag=True):
    print("creating forecasts..")

    future = model.make_future_dataframe(periods=7)

    future_confirmed = future.copy() # for non-baseline predictions later on

    preds = model.predict(future.tail(horizon))[["yhat", "yhat_lower", "yhat_upper"]]

    if log_flag:

        for x in preds.columns:

            if (x == "ds") | (x == "actual"):

                continue

            preds[x] = np.round(np.exp(preds[x]))
    else:

        for x in preds.columns:

            if (x == "ds") | (x == "actual"):

                continue

            preds[x] = np.round(preds[x])

    preds["date"] = future["ds"].tail(horizon).tolist()

    preds.columns = ["confirmed", "lower_bound", "upper_bound", "date"]

    history_df = model.history[["ds", "y"]]

    if log_flag:

        history_df["y"] = np.round(np.exp(history_df["y"]))

    history_df.columns = ["date", "confirmed"]

    history_df["lower_bound"] = history_df["confirmed"]

    history_df["upper_bound"] = history_df["confirmed"]

    df = pd.concat([history_df, preds], axis=0)

    #preds.loc[:55, "lower_bound"] = np.nan

    #preds.loc[:55, "upper_bound"] = np.nan

    return df

def get_ensembled_forecast(log_df, linear_df):

    print("averaging predictions from log and linear models")

    linear_df_sub = linear_df.loc[linear_df["date"].isin(log_df["date"].tolist()), :].reset_index(drop=True).copy()

    log_df = log_df.reset_index(drop=True).copy()

    averaged_result = linear_df_sub.copy()

    for x in averaged_result.columns:

        if (x=="date") | (x=="actual"):

            continue

        averaged_result[x] = ((averaged_result[x] + log_df[x]) / 2).astype(int)

    return averaged_result

def get_forecasts(country, horizon=7):

    stale = io_utils.check_staleness()

    if stale:

        build_all_models()

        forecast(horizon=horizon)

    try:

        forecasted_data = read_forecast(country)

    except:

        #Force rebuild ignoring stale check

        try:

            print("Force building models, ignored staleness")

            build_all_models()

            forecast(horizon=horizon)

            forecasted_data = read_forecast(country)

        except Exception as exc:
            print("Error in getting forecasts", exc)

    return forecasted_data

def forecast(country_data=None, horizon=7):

    if country_data==None:

        country_data = io_utils.load_data()

    for country in country_data.countries:

        try:

            model_log = load_model(country, country_data.timestamp, log_flag=True)

            model_linear = load_model(country, country_data.timestamp, log_flag=False)

        except:

            try:
                #if any model is not present/unreadable force rebuild with the same time stamp
                print("Force rebuilding model for {}".format(country))

                build_model(country, log_flag=True)

                build_model(country, log_flag=False)

                model_log = load_model(country, country_data.timestamp, log_flag=True)

                model_linear = load_model(country, country_data.timestamp, log_flag=False)

            except Exception as exc:

                print("Error in forecasting", exc)

        predictions_log = get_predictions_dataframe(model_log, horizon=horizon, log_flag=True)

        predictions_linear = get_predictions_dataframe(model_linear, horizon=horizon, log_flag=False)

        predictions = get_ensembled_forecast(predictions_log, predictions_linear)

        write_predictions(predictions, country, country_data.timestamp, logname=None)

        write_predictions(predictions_log, country, country_data.timestamp, logname="log")

        write_predictions(predictions_linear, country, country_data.timestamp, logname="linear")

def build_model(country, log_flag=True):

    countries_data = io_utils.load_data()

    hist_data = countries_data.historical_country_data

    hist_data_c = hist_data.loc[hist_data.index == country]

    prep_hist = prepare_data(hist_data_c, log_flag)

    print("building model for " + country)

    model = fit_model(prep_hist)

    print("Model build successful")

    model.stan_backend.logger = None

    if log_flag:

        logname = "log"

    else:

        logname = "linear"

    filename = os.path.join(constants.MODELS_DIR,
                            "model_{}_{}_t_{}.joblib".format(logname,
                                                             country, countries_data.timestamp.strftime(TIME_STAMP_FORMAT)))

    print("Saving model to:", filename)

    joblib.dump(model, filename)

def load_model(country, timestamp, log_flag=True):

    if log_flag:

        logname = "log"

    else:

        logname = "linear"

    fname = os.path.join(constants.MODELS_DIR, "model_{}_{}_t_{}.joblib".format(logname,
                                                                                country,
                                                                                timestamp.strftime(TIME_STAMP_FORMAT)))

    print("Loading model from:", fname)

    model = joblib.load(fname)

    return model

def write_predictions(df, country, timestamp, logname=None):

    if logname==None:

        fname = os.path.join(constants.OUTPUTS_DIR,
                             "predictions_{}_t_{}.csv".format(country, timestamp.strftime(TIME_STAMP_FORMAT)))
    else:

        fname = os.path.join(constants.OUTPUTS_DIR,
                             "predictions_{}_{}_t_{}.csv".format(logname,
                                                              country, timestamp.strftime(TIME_STAMP_FORMAT)))

    df.to_csv(fname, index=False)

    return fname

def read_forecast(country="Australia"):

    timestamp = io_utils.get_latest_timestamp()

    fname = os.path.join(constants.OUTPUTS_DIR,
                         "predictions_{}_t_{}.csv".format(country, timestamp.strftime(TIME_STAMP_FORMAT)))

    return pd.read_csv(fname)

def build_all_models():

    io_utils.fetch_data()

    data = io_utils.load_data()

    try:

        for country in data.countries:

            build_model(country, log_flag=False)

            build_model(country, log_flag=True)

    except Exception as exc:
        print(exc)

    return


import numpy as np
from scipy.stats import norm, truncnorm

def _em_step_body_(args):
    """
    Does a step of the EM algorithm, needed to dereference args to support parallelism
    """
    return _em_step_body(*args)

def _em_step_body(Z, r_lower, r_upper, sigma, num_ord_updates=1):
    """
    Iterate the rows over provided matrix
    """
    num, p = Z.shape
    Z_imp = np.copy(Z)
    C = np.zeros((p,p))
    for i in range(num):
        try:
            c, z_imp, z = _em_step_body_row(Z[i,:], r_lower[i,:], r_upper[i,:], sigma)
        except:
            np.savetxt("Z.txt", Z)
            print(Z)
        Z_imp[i,:] = z_imp
        Z[i,:] = z
        C += c
    return C, Z_imp, Z

def _em_step_body_row(Z_row, r_lower_row, r_upper_row, sigma, num_ord_updates=1):
    """
    The body of the em algorithm for each row
    Returns a new latent row, latent imputed row and C matrix, which, when added
    to the empirical covariance gives the expected covariance
    Args:
        Z_row (array): (potentially missing) latent entries for one data point
        r_lower_row (array): (potentially missing) lower range of ordinal entries for one data point
        r_upper_row (array): (potentially missing) upper range of ordinal entries for one data point
        sigma (matrix): estimate of covariance
        num_ord (int): the number of ordinal columns

    Returns:
        C (matrix): results in the updated covariance when added to the empircal covariance
        Z_imp_row (array): Z_row with latent ordinals updated and missing entries imputed
        Z_row (array): inpute Z_row with latent ordinals updated
    """
    Z_imp_row = np.copy(Z_row)
    p = Z_imp_row.shape[0]
    num_ord = r_upper_row.shape[0]
    C = np.zeros((p,p))

    obs_indices = np.where(~np.isnan(Z_row))[0]

    missing_indices = np.setdiff1d(np.arange(p), obs_indices)
    ord_in_obs = np.where(obs_indices < num_ord)[0]
    ord_obs_indices = obs_indices[ord_in_obs]
    # obtain correlation sub-matrices
    # obtain submatrices by indexing a "cartesian-product" of index arrays
    sigma_obs_obs = sigma[np.ix_(obs_indices,obs_indices)]
    sigma_obs_missing = sigma[np.ix_(obs_indices, missing_indices)]
    sigma_missing_missing = sigma[np.ix_(missing_indices, missing_indices)]

    if len(missing_indices) > 0:
        tot_matrix = np.concatenate((np.identity(len(sigma_obs_obs)), sigma_obs_missing), axis=1)
        intermed_matrix = np.linalg.solve(sigma_obs_obs, tot_matrix)
        sigma_obs_obs_inv = intermed_matrix[:, :len(sigma_obs_obs)]
        J_obs_missing = intermed_matrix[:, len(sigma_obs_obs):]
    else:
        sigma_obs_obs_inv = np.linalg.solve(sigma_obs_obs, np.identity(len(sigma_obs_obs)))

    # initialize vector of variances for observed ordinal dimensions
    var_ordinal = np.zeros(p)

    # OBSERVED ORDINAL ELEMENTS
    # when there is an observed ordinal to be imputed and another observed dimension, impute this ordinal
    if len(obs_indices) >= 2 and len(ord_obs_indices) >= 1:
        for update_iter in range(num_ord_updates):
            # used to efficiently compute conditional mean
            sigma_obs_obs_inv_Z_row = np.dot(sigma_obs_obs_inv, Z_row[obs_indices])
            for ind in range(len(ord_obs_indices)):
                j = obs_indices[ind]
                not_j_in_obs = np.setdiff1d(np.arange(len(obs_indices)),ind)
                v = sigma_obs_obs_inv[:,ind]
                new_var_ij = np.asscalar(1.0/v[ind])

                new_mean_ij = Z_row[j] - new_var_ij*sigma_obs_obs_inv_Z_row[ind]
                mean, var = truncnorm.stats(
                    a=(r_lower_row[j] - new_mean_ij) / np.sqrt(new_var_ij),
                    b=(r_upper_row[j] - new_mean_ij) / np.sqrt(new_var_ij),
                    loc=new_mean_ij,
                    scale=np.sqrt(new_var_ij),
                    moments='mv')
                if np.isfinite(var):
                    var_ordinal[j] = var
                    if update_iter == num_ord_updates - 1:
                        C[j,j] = C[j,j] + var
                if np.isfinite(mean):
                    Z_row[j] = mean

    # MISSING ELEMENTS
    Z_obs = Z_row[obs_indices]
    Z_imp_row[obs_indices] = Z_obs
    if len(missing_indices) > 0:
        Z_imp_row[missing_indices] = np.matmul(J_obs_missing.T,Z_obs)
        # variance expectation and imputation
        if len(ord_obs_indices) >= 1 and len(obs_indices) >= 2 and np.sum(var_ordinal) > 0:
            cov_missing_obs_ord = J_obs_missing[ord_in_obs].T * var_ordinal[ord_obs_indices]
            C[np.ix_(missing_indices, ord_obs_indices)] += cov_missing_obs_ord
            C[np.ix_(ord_obs_indices, missing_indices)] += cov_missing_obs_ord.T
            C[np.ix_(missing_indices, missing_indices)] += sigma_missing_missing - np.matmul(J_obs_missing.T, sigma_obs_missing) \
                                                           + np.matmul(cov_missing_obs_ord, J_obs_missing[ord_in_obs])
        else:
            C[np.ix_(missing_indices, missing_indices)] += sigma_missing_missing - np.matmul(J_obs_missing.T, sigma_obs_missing)
    return C, Z_imp_row, Z_row


#!/usr/bin/env python3

n = int(input())
abc = list(map(int, input().split()))
a, b, c = sorted(abc) #昇順
ans = 10**6
for ci in range(10000):
    for bi in range(10000-ci):
        if n-bi*b-ci*c >= 0:
            q, r = divmod(n-bi*b-ci*c, a)
            if r == 0:
                ans = min(ans, q+bi+ci)

print(ans)


"""
The environement where the obstacles can move
"""
from collections import deque
import numpy as np
import matplotlib.pyplot as plt

from obstacle import Wall

class DynamicEnvironment:
    """
    Class implementing a simple dynamic bounded 2D world, containing square
    obstacles placed in an ordered manner in order to always permit passage
    from the bottom to the top of the space.

    Attributes
    ----------
    dimensions : tuple
        (dim_x, dim_y) The x and y dimension of the rectangular world.
    obstacles : list
        List of walls, instances of the Wall class.
    moving : bool
        If the position of the holes in the walls is variying over time.
    center : list
        The coordinates of the center of the current scene.

    Methods
    -------
    plot
        Draws the environnement using matplotlib.
    is_free
        Returns False if a point is within an obstacle or outside of the
        boundaries of the environnement. Checks at a specific instant.
    random_free_space
        Selects an element of the free space, and returns it. Does not check at
        a specific instant.
    update
        Moves the center based on the position of the vehicle within the
        environment.
    """

    def __init__(self, dimensions, nb_walls, moving=False):
        self.moving = moving
        self.dimensions = dimensions
        # nb_walls represent the number of walls on the screen
        self.obstacles = deque()
        for i in range(nb_walls * 2):
            self.obstacles.append(Wall(dimensions[0],
                                       dimensions[1]*(0.1+i/nb_walls),
                                       thickness=5, moving=moving))
        self.center = [dimensions[0]/2, dimensions[1]/2]

    def plot(self, time=0, close=False, display=True):
        """
        Creates a figure and plots the environement on it.

        Parameters
        ----------
        time : float
            The instant at which the environment must be drawn.
        close : bool
            If the plot needs to be automaticaly closed after the drawing.
        display : bool
            If the view pops up or not (used when generating many images)
        """
        plt.ion() if display else plt.ioff()
        plt.figure()
        for obstacle in self.obstacles:
            obstacle.plot(time)
        plt.gca().set_xlim(self.center[0]-self.dimensions[0]/2,
                           self.center[0]+self.dimensions[0]/2)
        plt.gca().set_ylim(self.center[1]-self.dimensions[1]/2,
                           self.center[1]+self.dimensions[1]/2)
        if close:
            plt.close()

    def is_free(self, x, y, time=0):
        """
        Returns False if a point is within an obstacle or outside of the
        boundaries of the environnement.
        """

        if x < self.center[0]-self.dimensions[0]/2\
        or x > self.center[0]+self.dimensions[0]/2\
        or y < self.center[1]-self.dimensions[1]/2\
        or y > self.center[1]+self.dimensions[1]/2:
            return False
        for obstacle in self.obstacles:
            if obstacle.colides(x, y, time):
                return False
        return True

    def random_free_space(self):
        """
        Returns a randomly selected point in the free space.
        """

        x = np.random.rand() * self.dimensions[0]
        y = (np.random.rand() - 0.5) * self.dimensions[1] + self.center[1]
        while not self.is_free(x, y):
            x = np.random.rand() * self.dimensions[0]
            y = (np.random.rand() - 0.5) * self.dimensions[1] + self.center[1]
        return x, y, np.random.rand()*np.pi*2

    def update(self, position):
        """
        Refreshs the active walls as well as the position of the camera.

        Parameters
        ----------
        position : float
            The position of the vehicle in the environment.
        """

        # every time an obstacles gets out, we add one on the other side
        self.center = [self.center[0], position[1]+40]
        for wall in self.obstacles:
            if wall.bottom_y + wall.thickness < \
               self.center[1]-self.dimensions[1]:
                self.obstacles.append(Wall(self.dimensions[0],
                                           position[1]+50+self.dimensions[1],
                                           thickness=5, moving=self.moving))
                self.obstacles.popleft()
                break


#! /usr/bin/env python

# this is the predictor function, that runs the prediction for a graph

# imports
import pickle, features, out
from random import shuffle
from pybrain.tools.shortcuts import buildNetwork
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer

# some const
ACCEPTED = 1
NOTACCEPTED = -1

class Predictor():

  """ The predictor for a node """

  def __init__(self, netFile):

    """ Initalize a predictor from with a neuronal network. If the neuronalNetFile is None, an "empty" predictor to train is returned! """

    # init the features
    self.features = []
    functions = dir(features)
    for function in functions:
      if function.startswith('feat'):
        self.features.append(getattr(features,function))

    # init the network
    if netFile:
      # ok, load the predictor
      f = open(netFile,'r')
      self.net = pickle.load(f)
      f.close()
    else:
      # create an empty predictor
      cFeatures = len(self.features)
      self.net = buildNetwork( cFeatures, cFeatures + 1, 3, 2)

  def runprediction(self, querySequence, graph):

    """ this function marks all nodes in the graph as accepted, if the prediction they are predicted to be positive, Note, that node.accpeted should be false for all nodes.
    This function set the node.accpeted = True, for all nodes that are accepted by the predictor. """
#    def acceptNodeAndParentNodes(graph, node):
#      node.accepted = True
#      stack = graph.getParents(node)
#      while len(stack) != 0:
#        cNode = graph.getHpoTermById(stack.pop())
#        cNode.accepted = True
#        stack.extend(graph.getParents(cNode))

    for cNodeID, cNode in graph.hpoTermsDict.iteritems():
      # ok, get the node to predict
      out.writeDebug("Perform prediction for node: {}".format(cNode.id))
      # get all features for the current node
      featuresValue = []
      for feature in self.features:
        featuresValue.append(feature(self, cNode, graph, querySequence))
      # ok, now run the neuronal network
      predictionResult = self.net.activate(featuresValue)
      out.writeDebug("Prediction result for node {} = {}".format(cNode.id, predictionResult))
      # check the prediction result
      # difference should be between -2 (lowest confidence) and 2 (highest confidence)
      # ideally, the predictionResult is (1,-1) for accepted, (-1,1) for not accepted
      confidence =  predictionResult[0] - predictionResult[1]
      out.writeDebug("Prediction result: {}".format(confidence))
      # ok, set accepted attribute to confidence
      cNode.accepted = confidence

  def trainprediction(self, data=None, biased=False, maxEpochs = 10000):
    """Trains the neural network with the provided trainings data and returns true, if the training was successful"""
    if not data:
      out.writeDebug('No training data! The net stays initialized with random weights!')
      return False

    #create supervised data set from the training nodes
    ds = SupervisedDataSet(len(self.features), 2)
    reduced_dataset = [set([]),set([])]
    for node, target in data:
      featuresValue = []
      for feature in self.features:
        featuresValue.append(feature(self, node, None, node.querySequence))

      if target:
        reduced_dataset[0].add(tuple(featuresValue+[ACCEPTED, NOTACCEPTED]))
      else:
        reduced_dataset[1].add(tuple(featuresValue+[NOTACCEPTED, ACCEPTED]))

    for posInstance, negInstance in zip(reduced_dataset[0],reduced_dataset[1]):
      ds.addSample(posInstance[:-2],posInstance[-2:])
      ds.addSample(negInstance[:-2],negInstance[-2:])

    if biased:
      ds = SupervisedDataSet(len(self.features), 2)
      for instance in reduced_dataset[0]:
        ds.addSample(instance[:-2],instance[-2:])
      for instance in reduced_dataset[1]:
        ds.addSample(instance[:-2],instance[-2:])
    out.writeDebug('Start training neural net with %s training examples. Dataset bias is set to %s'%(len(ds), biased ))
    trainer = BackpropTrainer(self.net, ds)
    trainer.trainUntilConvergence(maxEpochs = maxEpochs)

    return True

  def saveNeuronalNetwork(self, fileName):

    """ save the neuronal network to a file """

    fileObj = open(fileName, "w+")
    pickle.dump(self.net, fileObj)
    fileObj.close()

if __name__ == "__main__":
  pass


import os
from bs4 import BeautifulSoup
import requests

p_count = 1
for i in range(1,7):
    url = 'https://www.nature.com/natmachintell/volumes/1/issues/' + str(i)
    r = requests.get(url)
    soup = BeautifulSoup(r.content, 'html.parser')

    articles = soup.find(id = 'Research-content')
    articles = articles.find_all(class_ = 'cleared')
    for article in articles:
        title = article.select('h3 a')[0].text
        title = title.replace('\n','')
        title = title.lstrip()
        t_url = 'http://www.nature.com' + article.select('h3 a')[0]['href']
        abstract = article.select('div p')[1].text

        print(str(p_count) + '. ' + title)
        print(t_url)
        print(abstract)
        print()
        p_count += 1
    x = 1


"""
For additional samples, visit the Alexa Skills Kit Getting Started guide at
http://amzn.to/1LGWsLG
"""

from __future__ import print_function
import time
import json
import datetime
import traceback
import botocore
import urllib2
import boto3
s3 = boto3.client('s3')
s3w = boto3.resource('s3')

def get_s3(sid):
    track_data = {}
    try:
        obj = s3w.Object('pet-tracker-alexa', ''+sid+'.json')
        json_input = obj.get()['Body'].read()

        track_data = json.loads(json_input)

    except:
        track_data = {}
        print(traceback.format_exc())

    return track_data

def put_s3(sid, intent, pet_type):

    track_data = get_s3(sid)

    if intent not in track_data:
        track_data[intent] = {}

    track_data[intent][pet_type] = time.time()

    track = json.dumps(track_data, ensure_ascii=False)
    s3w.Bucket('pet-tracker-alexa').put_object(Key=''+sid+'.json', Body=track)

# --------------- Helpers that build all of the responses ----------------------

def build_speechlet_response(title, output, reprompt_text, should_end_session):
    return {
        'outputSpeech': {
            'type': 'PlainText',
            'text': output
        },
        'card': {
            'type': 'Standard',
            'title': title,
            'text': output + "\n.\nContribute to phrases, fix bugs and submit idea requests on GitHub. Search for PetTrackerAlexa."
        },
        'reprompt': {
            'outputSpeech': {
                'type': 'PlainText',
                'text': reprompt_text
            }
        },
        'shouldEndSession': should_end_session
        # ,
        # TODO: FIGURE THIS OUT
        # "directives": [{
        #     "type": "Dialog.ConfirmIntent",
        #     "updatedIntent": {
        #         "name": "WalkPet",
        #         "confirmationStatus": "NONE",
        #         "slots": {
        #             "PetType": {
        #                 "name": "PetType",
        #                 "value": "lizard",
        #                 "confirmationStatus": "NONE"
        #             }
        #         }
        #     }
        # }]
    }

def build_response(session_attributes, speechlet_response):
    return {
        'version': '1.0',
        'sessionAttributes': session_attributes,
        'response': speechlet_response
    }

# --------------- Functions that control the skill's behavior ------------------

def get_welcome_response():
    session_attributes = {}
    card_title = "Welcome"
    speech_output = "Welcome to Pet Tracker. " \
                    "Tell me what activity you want to track with which pet, " \
                    "For example: \"I fed the dog\" or \"I am walking the cat\"."
    # If the user either does not reply to the welcome message or says something
    # that is not understood, they will be prompted again with this text.
    reprompt_text = "Tell me what activity you want to track with which pet, " \
                    "For example: \"I fed the dog\" or \"I am walking the cat\"."
    should_end_session = False
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))

def say_good_bye():
    session_attributes = {}
    card_title = "Good bye!"
    speech_output = "Good bye!"
    reprompt_text = None
    should_end_session = True
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))

def handle_session_end_request():
    card_title = "Session Ended"
    speech_output = "Thank you for using Pet Tracker. " \
                    "Have a nice day! "
    # Setting this to true ends the session and exits the skill.
    should_end_session = True
    return build_response({}, build_speechlet_response(
        card_title, speech_output, None, should_end_session))

def save_user_data(intent, session):

    intent_name = intent['name']
    sid = session['user']['userId']

    should_end_session = False
    pet_type = ''
    speech_output = ''
    reprompt_text = None
    pet_action = ''

    if 'PetAction' in intent['slots'] and 'value' in intent['slots']['PetAction']:
        pet_action = intent['slots']['PetAction']['value']

    if 'PetType' in intent['slots'] and 'value' in intent['slots']['PetType']:
        pet_type = intent['slots']['PetType']['value']

        if intent_name == 'WalkPet':
            speech_output = "I have saved that you walked the " + pet_type
            put_s3( sid, intent_name, pet_type )

        elif intent_name == 'FeedPet':
            speech_output = "I have saved that you fed the " + pet_type
            put_s3( sid, intent_name, pet_type )

        elif intent_name == 'PetMeds':
            speech_output = "I have saved that you gave the " + pet_type + " its medication"
            put_s3( sid, intent_name, pet_type )

        should_end_session = True

    else:
        speech_output = "I'm not sure what is going on. " \
                        "Please try again."
        reprompt_text = "I'm not sure what is happening. " \
                        "Please try again."

    session_attributes = get_s3(sid)

    return build_response(session_attributes, build_speechlet_response(
        'Action tracked', speech_output, reprompt_text, should_end_session))

def get_all_user_data(intent, session):

    should_end_session = True
    reprompt_text = None
    intent_name = intent['name']
    sid = session['user']['userId']
    pet_type = ''
    pet_output = ''
    session_attributes = get_s3(sid)

    speech_output = 'Here is everything I know'

    if 'PetType' in intent['slots'] and 'value' in intent['slots']['PetType']:
        pet_type = intent['slots']['PetType']['value']

        pet_output += ' about the ' + pet_type

    speech_output += pet_output + ': '

    for action in session_attributes:
        if action == 'WalkPet':
            actionWord = 'walked'
        elif action == 'FeedPet':
            actionWord = 'fed'
        elif action == 'PetMeds':
            actionWord = 'given its medication'

        for pet in session_attributes[action]:
            if pet_type == '' or pet_type == pet:
                if pet_type == pet:
                    speech_output += 'It was '
                else:
                    speech_output += 'The ' + pet + ' was '

                speech_output += actionWord + ' ' + say_time(session_attributes[action][pet]) + '. '

    speech_output += 'That is all I know' + pet_output + '.'

    return build_response(session_attributes, build_speechlet_response(
        'What I Know', speech_output, reprompt_text, should_end_session))

def say_time( action_time ):

    seconds = (int(time.time()) - int(action_time))
    minutes = int(seconds)/60 % 60
    hours = int(seconds)/60/60 % 24
    days = int(seconds)/60/60/24

    output = ''
    time_frame = ''
    if days > 0:
        time_frame += str(days) + " day"
        if days != 1:
            time_frame += "s"
        time_frame += " "

    if hours > 0:
        time_frame += str(hours) + " hour"
        if hours != 1:
            time_frame += "s"
        time_frame += " "

    if time_frame != '':
        time_frame += "and "

    output += time_frame + str(minutes) + " minute"
    if minutes != 1:
        output += "s"
    output += " ago"

    return output

def get_user_data(intent, session):

    should_end_session = False
    reprompt_text = None
    intent_name = intent['name']
    sid = session['user']['userId']
    pet_type = ''
    pet_action = ''
    pet_meds = ''
    speech_output = ''
    intentAction = ''

    if 'PetType' in intent['slots'] and 'value' in intent['slots']['PetType']:
        pet_type = intent['slots']['PetType']['value']

    if 'PetAction' in intent['slots'] and 'value' in intent['slots']['PetAction']:
        pet_action = intent['slots']['PetAction']['value']

    if 'Meds' in intent['slots'] and 'value' in intent['slots']['Meds']:
        pet_meds = intent['slots']['Meds']['value']

    track_data = get_s3(sid)
    session_attributes = track_data

    if pet_type != '' and ( pet_action != '' or pet_meds != '' ):
        if pet_action == 'feed' or pet_action == 'fed':
            intentAction = 'FeedPet'
            actionWord = 'fed'
            speech_output = "The  " + pet_type + " was " + actionWord + " "
        elif pet_action == 'walk' or pet_action == 'walked':
            intentAction = 'WalkPet'
            actionWord = 'walked'
            speech_output = "The " + pet_type + " was " + actionWord + " "
        elif 'med' in pet_meds:
            intentAction = 'PetMeds'
            actionWord = 'medication'
            speech_output = "The " + pet_type + " last got its " + actionWord + " "

        if intentAction != '':
            if intentAction in track_data and pet_type in track_data[intentAction]:
                action_time = track_data[intentAction][pet_type]

                speech_output += say_time( action_time )

                should_end_session = True
            else:
                speech_output = 'I do not know when you last ' + actionWord + ' the ' + pet_type
                should_end_session = True

    if speech_output == '':
        speech_output = "I'm not sure what you want. I heard something about "
        if pet_type != '':
            speech_output += pet_type + " "

        if pet_action != '':
            speech_output += pet_action + " "

        if pet_meds != '':
            speech_output += pet_meds + " "

        speech_output += ". Please try again."

    # Setting reprompt_text to None signifies that we do not want to reprompt
    # the user. If the user does not respond or says something that is not
    # understood, the session will end.
    return build_response(session_attributes, build_speechlet_response(
        'Action response', speech_output, reprompt_text, should_end_session))

# --------------- Events ------------------

def on_session_started(session_started_request, session):
    """ Called when the session starts """

    print("on_session_started requestId=" + session_started_request['requestId']
          + ", sessionId=" + session['sessionId'])

def on_launch(launch_request, session):
    """ Called when the user launches the skill without specifying what they
    want
    """

    print("on_launch requestId=" + launch_request['requestId'] +
          ", sessionId=" + session['sessionId'])
    # Dispatch to your skill's launch
    return get_welcome_response()

def on_intent(intent_request, session):
    """ Called when the user specifies an intent for this skill """

    print("on_intent requestId=" + intent_request['requestId'] +
          ", sessionId=" + session['sessionId'])

    intent = intent_request['intent']
    intent_name = intent_request['intent']['name']

    # Dispatch to your skill's intent handlers
    if intent_name == "WalkPet" or intent_name == "FeedPet" or intent_name == "PetMeds":
        return save_user_data(intent, session)
    elif intent_name == "AskPet":
        return get_user_data(intent, session)
    elif intent_name == "AskAll":
        return get_all_user_data(intent, session)
    # elif intent_name == "MyNameIs":
    #     return save_user_name(intent, session)
    elif intent_name == "ThankYou":
        return say_good_bye()
    elif intent_name == "AMAZON.HelpIntent":
        return get_welcome_response()
    elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent":
        return handle_session_end_request()
    else:
        raise ValueError("Invalid intent")

# def save_user_name(intent, session):
    #

def on_session_ended(session_ended_request, session):
    """ Called when the user ends the session.

    Is not called when the skill returns should_end_session=true
    """
    print("on_session_ended requestId=" + session_ended_request['requestId'] +
          ", sessionId=" + session['sessionId'])
    # add cleanup logic here

# --------------- Main handler ------------------

def lambda_handler(event, context):
    """ Route the incoming request based on type (LaunchRequest, IntentRequest,
    etc.) The JSON body of the request is provided in the event parameter.
    """
    print("event.session.application.applicationId=" +
          event['session']['application']['applicationId'])

    """
    Uncomment this if statement and populate with your skill's application ID to
    prevent someone else from configuring a skill that sends requests to this
    function.
    """
    # if (event['session']['application']['applicationId'] !=
    #         "amzn1.echo-sdk-ams.app.[unique-value-here]"):
    #     raise ValueError("Invalid Application ID")

    if event['session']['new']:
        on_session_started({'requestId': event['request']['requestId']}, event['session'])

    if event['request']['type'] == "LaunchRequest":
        return on_launch(event['request'], event['session'])
    elif event['request']['type'] == "IntentRequest":
        return on_intent(event['request'], event['session'])
    elif event['request']['type'] == "SessionEndedRequest":
        return on_session_ended(event['request'], event['session'])