import numpy as np
import matplotlib.pyplot as plt

from advection.problems import *
from advection.advectiveFluxes import *
import mesh.patch as patch
from util import profile

class Simulation:

    def __init__(self, problem_name, rp, timers=None):
        """
        Initialize the Simulation object for linear advection.

        Parameters
        ----------
        problem_name : str
            The name of the problem we wish to run.  This should
            correspond to one of the modules in advection/problems/
        rp : RuntimeParameters object
            The runtime parameters for the simulation
        timers : TimerCollection object, optional
            The timers used for profiling this simulation
        """

        self.rp = rp
        self.cc_data = None

        self.SMALL = 1.e-12

        self.problem_name = problem_name

        if timers == None:
            self.tc = profile.TimerCollection()
        else:
            self.tc = timers

    def initialize(self):
        """
        Initialize the grid and variables for advection and set the initial
        conditions for the chosen problem.
        """

        # setup the grid
        nx = self.rp.get_param("mesh.nx")
        ny = self.rp.get_param("mesh.ny")

        xmin = self.rp.get_param("mesh.xmin")
        xmax = self.rp.get_param("mesh.xmax")
        ymin = self.rp.get_param("mesh.ymin")
        ymax = self.rp.get_param("mesh.ymax")

        my_grid = patch.Grid2d(nx, ny,
                               xmin=xmin, xmax=xmax,
                               ymin=ymin, ymax=ymax, ng=4)

        # create the variables

        # first figure out the boundary conditions -- we need to translate
        # between the descriptive type of the boundary specified by the
        # user and the action that will be performed by the fill_BC routine.
        # Usually the actions can vary depending on the variable, but we
        # only have one variable.
        xlb_type = self.rp.get_param("mesh.xlboundary")
        xrb_type = self.rp.get_param("mesh.xrboundary")
        ylb_type = self.rp.get_param("mesh.ylboundary")
        yrb_type = self.rp.get_param("mesh.yrboundary")

        bc = patch.BCObject(xlb=xlb_type, xrb=xrb_type,
                            ylb=ylb_type, yrb=yrb_type)

        my_data = patch.CellCenterData2d(my_grid)

        my_data.register_var("density", bc)

        my_data.create()

        self.cc_data = my_data

        # now set the initial conditions for the problem
        exec(self.problem_name + '.init_data(self.cc_data, self.rp)')

    def timestep(self):
        """
        Compute the advective timestep (CFL) constraint.  We use the
        driver.cfl parameter to control what fraction of the CFL
        step we actually take.
        """

        cfl = self.rp.get_param("driver.cfl")

        u = self.rp.get_param("advection.u")
        v = self.rp.get_param("advection.v")

        # the timestep is min(dx/|u|, dy|v|)
        xtmp = self.cc_data.grid.dx/max(abs(u),self.SMALL)
        ytmp = self.cc_data.grid.dy/max(abs(v),self.SMALL)

        dt = cfl*min(xtmp, ytmp)

        return dt

    def preevolve(self):
        """
        Do any necessary evolution before the main evolve loop.  This
        is not needed for advection
        """
        pass

    def evolve(self, dt):
        """
        Evolve the linear advection equation through one timestep.  We only
        consider the "density" variable in the CellCenterData2d object that
        is part of the Simulation.

        Parameters
        ----------
        dt : float
            The timestep to evolve through

        """

        dtdx = dt/self.cc_data.grid.dx
        dtdy = dt/self.cc_data.grid.dy

        flux_x, flux_y =  unsplitFluxes(self.cc_data, self.rp, dt, "density")

        """
        do the differencing for the fluxes now.  Here, we use slices so we
        avoid slow loops in python.  This is equivalent to:

        myPatch.data[i,j] = myPatch.data[i,j] + \
                               dtdx*(flux_x[i,j] - flux_x[i+1,j]) + \
                               dtdy*(flux_y[i,j] - flux_y[i,j+1])
        """

        qx = self.cc_data.grid.qx
        qy = self.cc_data.grid.qy

        dens = self.cc_data.get_var("density")

        dens[0:qx-1,0:qy-1] = dens[0:qx-1,0:qy-1] + \
                   dtdx*(flux_x[0:qx-1,0:qy-1] - flux_x[1:qx,0:qy-1]) + \
                   dtdy*(flux_y[0:qx-1,0:qy-1] - flux_y[0:qx-1,1:qy])

    def dovis(self):
        """
        Do runtime visualization.
        """
        plt.clf()

        dens = self.cc_data.get_var("density")

        myg = self.cc_data.grid

        plt.imshow(np.transpose(dens[myg.ilo:myg.ihi+1,myg.jlo:myg.jhi+1]),
                     interpolation="nearest", origin="lower",
                     extent=[myg.xmin, myg.xmax, myg.ymin, myg.ymax])

        plt.xlabel("x")
        plt.ylabel("y")
        plt.title("density")

        plt.colorbar()

        plt.figtext(0.05,0.0125, "t = %10.5f" % self.cc_data.t)

        plt.draw()

    def finalize(self):
        """
        Do any final clean-ups for the simulation and call the problem's
        finalize() method.
        """
        exec(self.problem_name + '.finalize()')


#Terrorism analysis with insights

#importing libraries
import pandas as pd
import dash
import dash_html_components as html
from dash.dependencies import Input, State, Output
import webbrowser
import dash_core_components as dcc
import plotly.graph_objects as go
import plotly.express as px
from dash.exceptions import PreventUpdate

app = dash.Dash()

def load_data():
  dataset_name = "global_terror.csv"

  pd.options.mode.chained_assignment = None

  #reading the csv file
  global df
  df = pd.read_csv(dataset_name)
  print(df.head(5))

  #global variables
  global month_list
  month = {"January":1,"February": 2,"March": 3,"April":4,"May":5,"June":6,
         "July": 7,"August":8,"September":9,"October":10,"November":11,
          "December":12}
  month_list= [{"label":key, "value":values} for key,values in month.items()]
  global date_list
  date_list = [x for x in range(1, 32)]
  global region_list
  region_list = [{"label": str(i), "value": str(i)}  for i in sorted( df['region_txt'].unique().tolist())]
  global country_list
  country_list = df.groupby("region_txt")["country_txt"].unique().apply(list).to_dict()
  global state_list
  state_list = df.groupby("country_txt")["provstate"].unique().apply(list).to_dict()
  global city_list
  city_list  = df.groupby("provstate")["city"].unique().apply(list).to_dict()
  global attack_type_list
  attack_type_list = [{"label": str(i), "value": str(i)}  for i in df['attacktype1_txt'].unique().tolist()]
  global year_list
  year_list = sorted ( df['iyear'].unique().tolist()  )
  global year_dict
  year_dict = {str(year): str(year) for year in year_list}

  #chart dropdown options
  global chart_dropdown_values
  chart_dropdown_values = {"Terrorist Organisation":"gname","Target Nationality":"natlty1_txt",
                            "Target Type":"targtype1_txt","Attack Type":"attacktype1_txt",
                            "Weapon Type":"weaptype1_txt","Region":'region_txt',
                            "Country Attacked":"country_txt"}

  chart_dropdown_values = [{"label":keys,"value":value} for keys, value in chart_dropdown_values.items()]

def open_browser():
  #opening default browser
  webbrowser.open_new('http://127.0.0.1:8050/')

#layout of the page
def create_app_ui():
  main_layout = html.Div([
  html.H1(id='Main_title',children = "TERRORISM ANALYSIS AND FINDING INSIGHTS"),
  dcc.Tabs(id="maintabs", value="tab1",parent_className="customtabs",children=[
      dcc.Tab(label="Map Tool" ,id="maptool",value="tab1",className='first-tab',selected_className='first-sel',
        children=[
          dcc.Tabs(id = "mapsubtabs", value="tab2",children = [
              dcc.Tab(label="World Map Tool", id="worldmap", value="tab2",className="second-tab",selected_className="second-sel"),
              dcc.Tab(label="India Map Tool", id="indiamap", value="tab3",className="second-tab",selected_className="second-tab")
              ],colors={"border":"white","primary":"black","background":"cornsilk"}),
          html.Br(),
          dcc.Dropdown(id='month-dropdown',options=month_list,placeholder='Select Month',multi = True),
          dcc.Dropdown(id='date-dropdown',placeholder='Select Day',multi = True),
          dcc.Dropdown(id='region-dropdown',options=region_list,placeholder='Select Region',multi = True),
          dcc.Dropdown(id='country-dropdown',options=[{'label': 'All', 'value': 'All'}],placeholder='Select Country',multi = True),
          dcc.Dropdown(id='state-dropdown',options=[{'label': 'All', 'value': 'All'}],placeholder='Select State',multi = True),
          dcc.Dropdown(id='city-dropdown',options=[{'label': 'All', 'value': 'All'}],placeholder='Select City',multi = True),
          dcc.Dropdown(id='attacktype-dropdown',options=attack_type_list,placeholder='Select Attack Type',multi = True),

          html.H4('Select the Year', id='year_title'),
          dcc.RangeSlider(id='year-slider',
                          min=min(year_list),
                          max=max(year_list),
                          value=[min(year_list),max(year_list)],
                          marks=year_dict,step=None),
          html.Br()
          ]),
          dcc.Tab(label="Chart Tool",id="charttool", value="chart",className="first-sel",selected_className="first-sel",
            children=[
          dcc.Tabs(id="chartsubtabs",value="tab4",children = [
              dcc.Tab(label="World Chart Tool", id="worldchart",value="tab4",className="second-tab",selected_className="second-sel"),
              dcc.Tab(label="India Chart Tool", id="indiachart",value="tab5",className="second-tab",selected_className="second-sel")],
              colors={"border":"white","primary":"black","background":"#e6ffff"}),
              html.Br(),
              dcc.Dropdown(id="chart_dropdown", options = chart_dropdown_values, placeholder="Select option", value = "region_txt"),
              html.Br(),
              html.Br(),
              html.Hr(),
              dcc.Input(id="search", placeholder="Search Filter"),
              html.Hr(),
              html.Br(),
              dcc.RangeSlider(
                      id='chart_year_slider',
                      min=min(year_list),
                      max=max(year_list),
                      value=[min(year_list),max(year_list)],
                      marks=year_dict,step=None),
              html.Br()
              ]),
         ]),
  html.Div(id='graph-object',children ='Graph is loading')
  ])
  return main_layout

#callbacks for the application
@app.callback(dash.dependencies.Output('graph-object','children'),
    [
    dash.dependencies.Input('maintabs','value'),
    dash.dependencies.Input('month-dropdown','value'),
    dash.dependencies.Input('date-dropdown','value'),
    dash.dependencies.Input('region-dropdown','value'),
    dash.dependencies.Input('country-dropdown','value'),
    dash.dependencies.Input('state-dropdown','value'),
    dash.dependencies.Input('city-dropdown','value'),
    dash.dependencies.Input('attacktype-dropdown','value'),
    dash.dependencies.Input('year-slider','value'),
    dash.dependencies.Input('chart_year_slider','value'),
    dash.dependencies.Input("chart_dropdown", "value"),
    dash.dependencies.Input("search", "value"),
    dash.dependencies.Input("chartsubtabs", "value")
    ])

def update_app_ui(Tabs,month_value,date_value,region_value,country_value,state_value,city_value,attack_value,year_value,
                  chart_year_selector, chart_dp_value, search,chartsubtabs):
    fig = None
    if Tabs == "tab1":
        print('Data Type of month value:',str(type(month_value)))
        print('Data Type of date value:',str(type(date_value)))
        print('Data Type of region value:',str(type(region_value)))
        print('Data Type of country value:',str(type(country_value)))
        print('Data Type of state value:',str(type(state_value)))
        print('Data Type of city value:',str(type(city_value)))
        print('Data Type of Attack value:',str(type(attack_value)))
        print('Data Type of year value:',str(type(year_value)))

        # year filter
        year_range = range(year_value[0], year_value[1]+1)
        df1 = df[df["iyear"].isin(year_range)]

        # month filter
        if month_value==[] or month_value is None:
            pass
        else:
            if date_value==[] or date_value is None:
                df1 = df1[df1["imonth"].isin(month_value)]
            else:
                df1 = df1[df1["imonth"].isin(month_value)
                                & (df1["iday"].isin(date_value))]
        # region, country, state, city filter
        if region_value==[] or region_value is None:
            pass
        else:
            if country_value==[] or country_value is None :
                df1 = df1[df1["region_txt"].isin(region_value)]
            else:
                if state_value == [] or state_value is None:
                    df1 = df1[(df1["region_txt"].isin(region_value))&
                             (df1["country_txt"].isin(country_value))]
                else:
                    if city_value == [] or city_value is None:
                        df1 = df1[(df1["region_txt"].isin(region_value))&
                        (df1["country_txt"].isin(country_value)) &
                        (df1["provstate"].isin(state_value))]
                    else:
                        df1 = df1[(df1["region_txt"].isin(region_value))&
                        (df1["country_txt"].isin(country_value)) &
                        (df1["provstate"].isin(state_value))&
                        (df1["city"].isin(city_value))]
        #attack type filter
        if attack_value == [] or attack_value is None:
            pass
        else:
            df1 = df1[df1["attacktype1_txt"].isin(attack_value)]

        mapfigure = go.Figure()
        if df1.shape[0]:
            pass
        else:
            df1 = pd.DataFrame(columns = ['iyear','imonth','iday','country_txt','region_txt','provstate',
                                          'city','latitude','longitude','attacktype1_txt','nkill'])
            df1.loc[0] = [0, 0 ,0, None, None, None, None, None, None, None, None]

        mapFigure = px.scatter_mapbox(df1,lat="latitude",
                                      lon="longitude",
                                      color="attacktype1_txt",
                                      hover_name="city",
                                      hover_data=["region_txt","country_txt","provstate","city","attacktype1_txt","nkill","iyear","imonth","iday"],
                                      zoom=1)
        mapFigure.update_layout(mapbox_style="open-street-map",
                                autosize=True,
                                margin=dict(l=0,r=0,t=25,b=20),
                                )
        fig = mapFigure

    elif Tabs=="chart":
        fig = None
        year_range_c = range(chart_year_selector[0], chart_year_selector[1]+1)
        chart_df = df[df["iyear"].isin(year_range_c)]

        if chartsubtabs == "tab4":
            pass
        elif chartsubtabs == "tab5":
            chart_df = chart_df[(chart_df["region_txt"]=="South Asia")
            &(chart_df["country_txt"]=="India")]
        if chart_dp_value is not None and chart_df.shape[0]:
            if search is not None:
                chart_df = chart_df.groupby("iyear")[chart_dp_value].value_counts().reset_index(name = "count")
                chart_df  = chart_df[chart_df[chart_dp_value].str.contains(search, case=False)]
            else:
                chart_df = chart_df.groupby("iyear")[chart_dp_value].value_counts().reset_index(name="count")

        if chart_df.shape[0]:
            pass
        else:
            chart_df = pd.DataFrame(columns = ['iyear', 'count', chart_dp_value])
            chart_df.loc[0] = [0, 0,"No data"]
        chartfigure = px.area(chart_df, x="iyear", y ="count", color = chart_dp_value)
        fig = chartfigure
    return dcc.Graph(figure = fig)

@app.callback(
  Output("date-dropdown","options"),
  [
  Input("month-dropdown","value")
  ])
def update_date(month):
    option = []
    if month:
        option= [{"label":m, "value":m} for m in date_list]
    return option

@app.callback(
  [
  Output('region-dropdown','value'),
  Output('region-dropdown','disabled'),
  Output('country-dropdown','value'),
  Output('country-dropdown','disabled')],
  [Input('mapsubtabs','value')])

def update_indiamap(tab):
    region=None
    disabled_r=False
    country=None
    disabled_c=False
    if tab == "tab2":
        pass
    elif tab == "tab3":
        region = ["South Asia"]
        disabled_r = True
        country = ["India"]
        disabled_c = True
    return region, disabled_r, country, disabled_c

@app.callback(
    Output('country-dropdown','options'),
    [Input('region-dropdown','value')])

def set_country_options(region_value):
    option = []
    if region_value is  None:
        raise PreventUpdate
    else:
        for var in region_value:
            if var in country_list.keys():
                option.extend(country_list[var])
    return [{'label':m , 'value':m} for m in option]

@app.callback(
    Output('state-dropdown','options'),
    [Input('country-dropdown','value')])

def set_state_options(country_value):
    option = []
    if country_value is None :
        raise PreventUpdate
    else:
        for var in country_value:
            if var in state_list.keys():
                option.extend(state_list[var])
    return [{'label':m , 'value':m} for m in option]

@app.callback(
    Output('city-dropdown', 'options'),
    [Input('state-dropdown', 'value')])

def set_city_options(state_value):
    option = []
    if state_value is None:
        raise PreventUpdate
    else:
        for var in state_value:
            if var in city_list.keys():
                option.extend(city_list[var])
    return [{'label':m , 'value':m} for m in option]

#flow of the project
def main():
    print("Welcome to the Application")

    load_data()
    open_browser()

    global app
    app.layout = create_app_ui()
    app.title = "Terrorism Analysis"

    app.run_server()

    print("Thank You")
    df = None
    app = None

if __name__ == '__main__':
    main()


import os

def instruction_parser(i, dt):
    return list(map(int, '0'*(5-len(str(dt[i])))+str(dt[i])))

def computing(dt):
    i = 0
    inputt = 5
    while i <= len(dt):

        def mode(parameter, number): return (
            dt[i+number], i+number)[parameter]

        p3, p2, p1, *opcode = instruction_parser(i, dt)
        opcode = int(''.join(map(str, opcode)))

        if opcode == 99:
            break
        elif opcode == 1:
            dt[mode(p3, 3)] = dt[mode(p2, 2)] + dt[mode(p1, 1)]
            i += 4
        elif opcode == 2:
            dt[mode(p3, 3)] = dt[mode(p2, 2)] * dt[mode(p1, 1)]
            i += 4
        elif opcode == 3:
            dt[mode(p1, 1)] = inputt
            i += 2
        elif opcode == 4:
            print(dt[mode(p1, 1)])
            break
        elif opcode == 5:
            i = dt[mode(p2, 2)] if dt[mode(p1, 1)] != 0 else i+3
        elif opcode == 6:
            i = dt[mode(p2, 2)] if dt[mode(p1, 1)] == 0 else i+3
        elif opcode == 7:
            dt[mode(p3, 3)] = 1 if dt[mode(p1, 1)] < dt[mode(p2, 2)] else 0
            i += 4
        elif opcode == 8:
            dt[mode(p3, 3)] = 1 if dt[mode(p1, 1)] == dt[mode(p2, 2)] else 0
            i += 4

def main():
    with open(os.path.dirname(os.path.realpath(__file__))+os.path.sep+'input_5.txt') as file:
        dt = list(map(int, file.read().split(',')))
        computing(dt)

if __name__ == "__main__":
    main()


from golem import actions

from projects.golem_gui.pages import common
from projects.golem_gui.pages import api
from projects.golem_gui.pages import suite_list

def setup(data):
    common.access_golem(data.env.url, data.env.admin)
    api.project.create_access_random_project()
    common.navigate_menu('Suites')
    actions.store('folder', actions.random_str())
    suite_list.add_folder(data.folder)

def test(data):
    suite_list.delete_folder(data.folder)
    assert not suite_list.folder_exists(data.folder)
    actions.refresh_page()
    assert not suite_list.folder_exists(data.folder)

def teardown(data):
    api.project.delete_project(data.project)


# -*- coding: utf-8 -*-
"""Django 工程中的变量
"""

# 判断这个模块是否跑在Django环境下，如果在Django环境下，默认从settings中读取
# 配置信息，否则使用默认配置
# 客户端可采用import后再修改的方式来改变配置
try:
    from django.conf import settings

    APP_CODE = settings.APP_CODE
    SECRET_KEY = settings.SECRET_KEY
    RUN_MODE = settings.RUN_MODE
except:
    APP_CODE = None
    SECRET_KEY = None
    RUN_MODE = 'DEVELOP'


import numpy as np
import os
import sys
from dask import delayed, compute
import pandas as pd

sys.path.append(r'v:/code/python/code')
import apCode.behavior.FreeSwimBehavior as fsb # noqa E402

def add_suffix_to_paths(paths, suffix='proc'):
    """
    Go through list of paths, adding the specified suffix ending if not
    already present
    Parameters
    ----------
    paths: str or list-like
        Path or list of paths
    """
    if (isinstance(paths, list) | isinstance(paths, np.ndarray)):
        paths_new = []
        for path_ in paths:
            paths_new.append(add_suffix_to_paths(path_))
    else:
        pre, post = os.path.split(paths)
        if post != suffix:
            paths_new = os.path.join(paths, suffix)
        else:
            paths_new = paths
    return np.array(paths_new)

def append_fishPos_to_xls(xls):
    """Given the xls dataframe, reads the HDF files at the end of the paths in
    the datrame, extracts fish position and other relevant information and
    appends this to the dataframe
    Parameters
    ----------
    xls: pandas dataframe
        The dataframe which contains paths (xls.Path) to the HDF files with
        fish position information
    Returns
    -------
    df: pandas dataframe
        Dataframe that still contains the paths to hdf files, but that also has
        additional information about fish position, etc
    """
    from apCode.FileTools import findAndSortFilesInDir

    def vel_from_fp(fp, dt):
        ds = (np.diff(fp, axis=1)**2).sum(axis=0)**0.5
        return ds/dt

    def cum_dist_from_fp(fp):
        dfp = np.sum(np.diff(fp, axis = 1)**2, axis=0)**0.5
        cfp = np.insert(np.cumsum(dfp), 0, 0)
        return cfp

    def cum_disp_from_fp(fp):
        dfp = (fp-fp[:, 0][:, np.newaxis])**2
        dfp = np.sqrt(dfp.sum(axis=0))
        return np.insert(dfp, 0, 0)

    def total_dist_from_cumDist(cfp):
        return np.round(cfp[-1]*10)/10

    def total_disp_from_cumDisp(cfp):
        return np.round(cfp[-1]*10)/10

    if 'Path_proc' not in xls.columns:
        paths = np.array(xls.Path)
        paths_deproc = remove_suffix_from_paths(paths, suffix='proc')
        paths_proc = add_suffix_to_paths(paths_deproc, suffix='proc')
        xls = xls.assign(Path_proc=paths_proc)
    df = []
    for iRow in range(xls.shape[0]):
        row = xls.iloc[iRow]
        fName = findAndSortFilesInDir(row.Path_proc, ext='mat')
        if len(fName)>0:
            with fsb.openMatFile(row.Path_proc, mode='r') as hFile:
                if 'fishPos' in hFile:
                    fp = np.array(hFile['fishPos'])
                else:
                    print('"fishpos" key not found')
                    fp = None
                if 'fps' in hFile:
                    fps = int(np.array(hFile['fps'])[0, 0])
                else:
                    print('"fps" key not found, setting to 500')
                    fps = 500
                if 'nFramesInTrl' in hFile:
                    nFramesInTrl = int(np.array(hFile['nFramesInTrl'])[0, 0])
                else:
                    print('"nFramesIntrl" key not found, setting to 750')
                    nFramesInTrl = 750
                if fp is not None:
                    pxlSize = row.pxlSize
                    dt = 1/fps
                    if np.mod(fp.shape[1], nFramesInTrl) != 0:
                        N = int((fp.shape[1]//nFramesInTrl)*nFramesInTrl)
                        fp = fp[:, N]
                    fp_trl = fp.reshape(fp.shape[0], -1, nFramesInTrl)
                    fp_trl = np.transpose(fp_trl, (1, 0, 2))
                    # --- Set first points of fish trajectory to zero
                    fp_start = fp_trl[...,0]
                    fp_trl = fp_trl - fp_start[:, :, np.newaxis]
                    # --- Change from pixel to physical units
                    fp_trl = fp_trl*pxlSize
                    cumDist_trl = [delayed(cum_dist_from_fp)(_) for _ in
                                   fp_trl]
                    cumDist_trl = compute(*cumDist_trl)
                    cumDisp_trl = [delayed(cum_disp_from_fp)(_) for _ in
                                   fp_trl]
                    cumDisp_trl = compute(*cumDisp_trl)
                    totDist_trl = [delayed(total_dist_from_cumDist)(_)
                                   for _ in cumDist_trl]
                    totDist_trl = compute(*totDist_trl)
                    totDisp_trl = [delayed(total_disp_from_cumDisp)(_)
                                   for _ in cumDisp_trl]
                    totDisp_trl = compute(*totDisp_trl)
                    swimVel_trl = [delayed(vel_from_fp)(_, dt) for _ in fp_trl]
                    swimVel_trl = compute(*swimVel_trl)
                    swimVel_trl_max = np.array(swimVel_trl).max(axis=1)
                    trlNums = np.arange(len(fp_trl))
                    fp_trl = list(fp_trl)
                    fp_start = list(fp_start)
                    dic = dict(fishPos_mm=fp_trl, fishPos_start=fp_start,
                               swimDist=cumDist_trl, swimDisp=cumDisp_trl,
                               swimDist_total=totDist_trl,
                               swimDisp_total=totDisp_trl, trlNum=trlNums,
                               fps=fps, nFramesInTrl=nFramesInTrl,
                               swimVel=swimVel_trl,
                               swimVel_max=swimVel_trl_max)
                    df_fish = pd.DataFrame(dic, columns=dic.keys())
                    df_fish = df_fish.assign(FishIdx=row.FishIdx)
                    df_fish = pd.merge(row.to_frame().T, df_fish, how='outer')
                    df.append(df_fish)
        else:
            pass
    return pd.concat(df)

def append_latency_to_df(df, var='swimVel', nKer=100, ind_start=50, zThr=1,
                         dt=1/500):
    """
    Parameters
    ----------
    df: pandas dataframe
        The number of rows of df must correspond to the total number of trials
        because the program assumes this when computing onset latencies
    """
    from apCode.machineLearning.preprocessing import Scaler
    from apCode.SignalProcessingTools import causalConvWithSemiGauss1d
    from apCode.SignalProcessingTools import levelCrossings

    swimVel = np.array([np.array(_) for _ in df[var]])
    nTrls = len(swimVel)
    swimVel_ser = causalConvWithSemiGauss1d(swimVel.flatten(), n=nKer)
    scaler = Scaler(with_mean=False)
    swimVel_trl = scaler.fit_transform(swimVel_ser[:, None]).reshape(nTrls, -1)
    lats = np.ones((nTrls, ))*np.nan
    latsToFirstPk = lats.copy()
    for iTrl, trl in enumerate(swimVel_trl):
        onInds = levelCrossings(trl, thr=zThr)[0]
        if len(onInds)>0:
            # inds_keep = np.where(onInds >= ind_start)[0]
            # if len(inds_keep) > 0:
            #     onInd = onInds[inds_keep][0]
            #     lats[iTrl] = onInd-ind_start
            lats[iTrl] = 1000*(onInds[0]-ind_start)*dt # In milliseconds
    df = df.assign(onsetLatency=lats)
    return df

def detect_noisy_trials(df, nKer : int = 100,
                        var : str = 'swimVel',
                        convolve : bool = True) -> 'dataframe':
    """Use a Gaussian Mixture Model to identify noisy points in trials
    and eliminate noisy trials from the dataframe
    Parameters
    ----------
    df: pandas dataframe
        Each row of the dataframe must correspond to a single trial
    nKer: int
        Kernel length for smoothing the relevant timeseries before fitting a
        GM model
    Returns
    -------
    df_new: pandas dataframe
        New dataframe with additional column f"noisyTrlInds_{var}" which is
        a boolean with values of 1 corresponding to trials identified as noisy.
    """
    from apCode.machineLearning.ml import GMM
    from apCode.SignalProcessingTools import causalConvWithSemiGauss1d
    swimVel = np.array([np.array(_) for _ in df[var]])
    nTrls = len(swimVel)
    swimVel_ser = swimVel.flatten()
    if convolve:
        swimVel_ser = causalConvWithSemiGauss1d(swimVel_ser, n=nKer)
    swimVel_trl = swimVel_ser.reshape(nTrls, -1)
    print('Fitting GMM model for detection of noisy trials...')
    gmm = GMM(n_components=3).fit(swimVel_ser[:, None])
    noiseLbl = np.argmax(gmm.means_)
    noisyTrlInds = np.zeros((nTrls, ))
    for iTrl, trl in enumerate(swimVel_trl):
        lbls = gmm.predict(trl[:, None])
        if len(np.intersect1d([noiseLbl], lbls)) > 0:
            noisyTrlInds[iTrl] = 1
    kwargs = {f'noisyTrlInds_{var}' : noisyTrlInds}
    df = df.assign(**kwargs)
    return df

def get_img_props(paths_to_imgs, diam=50, plotBool: bool = False,
                  verbose: bool = True, inParallel: bool = True,
                  override_and_save: bool = True, **kwargs):
    """
    Estimate pixel sizes from background images computed from the images in
    the given paths.
    Parameters
    ----------
    paths_to_imgs: str or list of string
        Path or List of paths to images
    diam: scalar
        Actual diameter of arena edge within the images in any units of
        interest
    plotBool: bool
        If True, then plots points on the detected arena edge
    verbose: bool
        If True, then displays a progress bar
    override_and_save: bool
        If True, recomputes background image regardless of whether or
        not one already exists in the path (slower)
    **kwargs: dict
        Keyword args for FreeSwimBehavior.getArenaEdge
        nIter, tol
    Returns
    -------
    pxlSizes: (nPaths_to_images,)
        Pixel sizes in given units.
    imgDims: tuple, (2,)
        Image dimensions
    """
    if isinstance(paths_to_imgs, str):
        bgd = fsb.track.computeBackground(paths_to_imgs,
                                          override_and_save=override_and_save)
        pxlSizes = fsb.getPxlSize(bgd, diam=50, plotBool=plotBool, **kwargs)[0]
        img_props = (pxlSizes, bgd.shape)
    else:
        if inParallel:
            del_func = delayed(get_img_props)
            img_props = [del_func(path_, diam=50, plotBool=plotBool, **kwargs)
                         for path_ in paths_to_imgs]
            if verbose:
                from dask.diagnostics import ProgressBar
                with ProgressBar():
                    img_props = compute(*img_props, scheduler='processes')
            else:
                img_props = np.array(compute(*img_props,
                                             scheduler='processes'))
        else:
            img_props = []
            for iPath, path_ in enumerate(paths_to_imgs):
                print(f'Path # {iPath+1}/{len(paths_to_imgs)}')
                try:
                    img_props.append(get_img_props(path_, diam=50,
                                                   plotBool=plotBool,
                                                   **kwargs))
                except RuntimeError:
                    print(f'Failed for path:\n {path_}')
    return img_props

def remove_suffix_from_paths(paths, suffix='proc'):
    """
    Filter a list of paths, removing the specified suffix ending if present
    Parameters
    ----------
    paths: str or list-like
        Path or list of paths
    """
    if (isinstance(paths, list) | isinstance(paths, np.ndarray)):
        paths_new = []
        for path_ in paths:
            paths_new.append(remove_suffix_from_paths(path_))
    else:
        pre, post = os.path.split(paths)
        if post == suffix:
            paths_new = pre
        else:
            paths_new = paths
    return np.array(paths_new)


# -*- coding: UTF-8 -*-

import json, View, Object
from collections import OrderedDict
from os.path import dirname, abspath
import ModuleLocation

class ScenarioData(object):
	def __init__(self, scenarioName):
		self.texts = OrderedDict()
		self.roomList = []
		self.sequenceList = []
		self.customObjectList = []
		self.miscObjects = []
		self.startView = None
		self.endViewList = []
		self.menuList = []

		self.dataDir = "%s/%s/%s/" %(dirname(abspath(ModuleLocation.getLocation())), "gamedata", scenarioName)

	# Load and parse game data files
	def loadScenario(self):
		self.parseTexts()
		self.parseImages()
		#self.createInteractions()

	def parseTexts(self):
		with open(self.dataDir + "texts.json", encoding='utf-8') as f:
			self.texts = json.load(f)
			f.close()

	def parseImages(self):
		with open(self.dataDir + "images.json", encoding='utf-8') as f:
			images = json.load(f)
			f.close()

		with open(self.dataDir + "objects.json", encoding='utf-8') as f:
			objects = json.load(f)
			f.close()

		# Parse objects from images.json and objects.json into categorized dict
		objectsByCat = OrderedDict()
		for child in images["children"]:
			layerObject = {}
			if ("object_name" in child["attrs"]):
				layerObject = objects[child["attrs"]["object_name"]]

			objectCategory = child["attrs"]["category"]
			objectId = child["attrs"]["id"]

			if not (objectCategory in objectsByCat):
				objectsByCat[objectCategory] = OrderedDict()

			# Leave misc objects as they are
			if (objectCategory == "misc"):
				self.miscObjects.append(child)
				continue

			layerChildren = None
			for layer in child:
				if (layer == "children"):
					layerChildren = child[layer]
				elif (layer == "attrs"):
					#layerAttrs = child[layer]
					continue

			if not (layerChildren):
				continue

			# Go through the objects in the layer
			# And check for relation with objects.json objects
			createdObjects = OrderedDict()
			for item in layerChildren:
				itemId = item["attrs"]["id"]
				jsonImage = item["attrs"]

				# Get possible attributes from objects.json
				if ("object_name" in item["attrs"]):
					itemId = item["attrs"]["object_name"]

					try:
						jsonObject = objects[itemId]
					except KeyError:
						print("Warning: Could not find object.json object for '%s' (object_name -> '%s')" %(item["attrs"]["id"], itemId))

				elif (itemId in objects):
					jsonObject = objects[itemId]

				elif not (itemId in objects):
					jsonObject = {}

				# Create dict key for the item
				if not (itemId in createdObjects):
					createdObjects[itemId] = {}

				createdObjects[itemId]["object"] = jsonObject
				createdObjects[itemId]["className"] = item["className"]

				if not ("image" in createdObjects[itemId]):
					createdObjects[itemId]["image"] = []

				createdObjects[itemId]["image"].append(jsonImage)

			# Compose the final dict key and values
			objectsByCat[objectCategory][objectId] = {}
			objectsByCat[objectCategory][objectId]["image"] = createdObjects
			objectsByCat[objectCategory][objectId]["object"] = layerObject
			objectsByCat[objectCategory][objectId]["attrs"] = child["attrs"]
			objectsByCat[objectCategory][objectId]["className"] = child["className"]

		#import pprint
		#pp = pprint.PrettyPrinter(indent=4)
		#pp.pprint(objectsByCat)

		# Create objects from the categorized dict
		for layer in objectsByCat:
			if (layer == "misc"):
				continue

			for child in objectsByCat[layer]:
				viewImages = objectsByCat[layer][child].pop("image")
				viewAttributes = objectsByCat[layer][child]

				if (layer == "room"):
					self.addRoom(child, viewAttributes, viewImages)
				elif (layer == "sequence"):
					self.addSequence(child, viewAttributes, viewImages)
				elif (layer == "start"):
					self.addStart(viewAttributes, viewImages)
				elif (layer == "end"):
					self.addEnd(child, viewAttributes, viewImages)
				elif (layer == "menu"):
					self.addMenu(child, viewAttributes, viewImages)
				elif (layer == "custom"):
					self.addCustomView(child, viewAttributes, viewImages)

		# Post-init sets triggers, outcomes etc.
		for obj in self.roomList + self.endViewList:
			obj.postInit(self.getGameObject)

		self.startView.postInit(self.getGameObject)

	# Save scenario to JSON files
	def saveScenario(self):
		#scenarioTexts = {}
		scenarioObjects = {}
		scenarioImages = []

		# Go through views
		for view in self.roomList + self.sequenceList + [self.startView] + self.endViewList + self.menuList + self.customObjectList:
			viewChildren = []

			# Contents for objects.json from view
			if ("object_name" in view.attrs):
				scenarioObjects[view.attrs["object_name"]] = view.object

			# Go through objects inside views
			for viewChild in view.getChildren():
				#childJSON = viewChild.objectAttributes

				# Go through images inside objects
				for childImage in viewChild.images:
					viewChildren.append(self.__createLayerChildJSON__(childImage.imageAttributes, childImage.getClassname()))

					# Contents for objects.json from image
					if ("object_name" in childImage.imageAttributes):
						scenarioObjects[childImage.imageAttributes["object_name"]] = childImage.objectAttributes["object"]

				if (type(viewChild) == Object.JSONImage):
					viewChildren.append(self.__createLayerChildJSON__(viewChild.imageAttributes, viewChild.getClassname()))

			layerJSON = self.__createLayerJSON__(view.attrs, viewChildren, view.classname)
			scenarioImages.append(layerJSON)

		# Go through self.texts and add modified texts from objects
		objects = self.getAllObjects()[0]
		for obj in objects:
			for objectImage in obj.getImages():
				if (objectImage.id in self.texts):
					self.texts[objectImage.id] = objectImage.texts

		# Miscellaneous objects
		for misc in self.miscObjects:
			scenarioImages.append(misc)

		# Bundle everything together
		scenarioAttrs = {"id": "Stage", "width": 981, "height": 643}
		scenarioChildren = self.__createLayerJSON__(scenarioAttrs, scenarioImages, "Stage")

		#These were unused
		textsJSON = json.dumps(self.texts, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ': '))
		imagesJSON = json.dumps(scenarioChildren, sort_keys=True, indent=4, separators=(',', ': '))
		objectsJSON = json.dumps(scenarioObjects, sort_keys=True, indent=4, separators=(',', ': '))

		#print(textsJSON)
		#print(imagesJSON)
		#print(objectsJSON)

		# Save into file
		f = open(self.dataDir + "texts.json", "w", encoding='utf-8')
		f.write(textsJSON)
		f.close()

		f = open(self.dataDir + "images.json", "w")
		f.write(imagesJSON)
		f.close()

		f = open(self.dataDir + "objects.json", "w")
		f.write(objectsJSON)
		f.close()

	# Game object layers
	def __createLayerJSON__(self, attrs, children, className="Layer"):
		return {"attrs": attrs, "className": className, "children": children}

	# Single items for game layers
	def __createLayerChildJSON__(self, attrs, className="Image"):
		return {"attrs": attrs, "className": className}

	def getRoom(self, roomId):
		for room in self.roomList:
			if (room.id == roomId):
				return room

	def getRooms(self):
		return self.roomList

	def getRoomBackLoc(self, i):
		room = self.roomList[i]
		loc = self.dataDir + room.background.imageAttributes['src']
		return loc

	def getSequence(self, sequenceId):
		for sequence in self.sequenceList:
			if (sequence.id == sequenceId):
				return sequence

	def getObject(self, objectId):
		for room in self.roomList:
			for obj in room.objectList:
				if (obj.id == objectId):
					return obj

	def getJSONObject(self, imageId):
		objects = self.getAllObjects()[0]
		for obj in objects:
			images = obj.getImages()
			for img in images:
				if (img.id == imageId):
					return img

	def getCustomObject(self, objectId):
		for obj in self.customObjectList:
			if (obj.id == objectId):
				return obj

	# Get given types of objects found in rooms
	def getObjectsByType(self, objectTypes):
		retObjects = []
		if (type(objectTypes) != list or type(objectTypes) != tuple):
			objectTypes = (objectTypes)

		for room in self.roomList:
			roomObjects = {"room": room, "objects": []}
			for item in room.getItems():
				if (item.__class__.__name__.lower() in objectTypes):
					roomObjects["objects"].append(item)
			if (len(roomObjects["objects"]) != 0):
				retObjects.append(roomObjects)
		return retObjects

	def getGeneralName(self, objectType):
		objectType = objectType.lower()
		if (objectType == "object"):
			return Object.Object.generalName
		elif (objectType == "item"):
			return Object.Item.generalName
		elif (objectType == "door"):
			return Object.Door.generalName
		elif (objectType == "container"):
			return Object.Container.generalName
		elif (objectType == "obstacle"):
			return Object.Obstacle.generalName

	# Get room, sequence or other object
	def getGameObject(self, objectType, objectId):
		objectType = objectType.lower()

		if (objectType == "room"):
			return self.getRoom(objectId)
		elif (objectType == "sequence"):
			return self.getSequence(objectId)
		elif (objectType == "object"):
			return self.getObject(objectId)
		elif (objectType == "menu"):
			return self.getMenu(objectId)
		elif (objectType == "custom"):
			return self.getCustomObject(objectId)

	# Get all right type of objects, amount of images and secrets,
	def getAllObjects(self):
		retObjects = []
		imgCount = 0
		secretCount = 0
		rightTypes = ["Object", "Item", "Container", "Door", "Obstacle"]

		for room in self.roomList:
			for obj in room.getItems():
				if (obj.__class__.__name__ in rightTypes and obj.getClassname() != "Text"):
						retObjects.append(obj)
						imgCount += len(obj.getImages())
						if (obj.getRepresentingImage().imageAttributes["category"] == "secret"):
							secretCount += 1

		return [retObjects, imgCount, secretCount]

	def getMenu(self, menuId):
		for menu in self.menuList:
			if (menu.id == menuId):
				return menu

	def deleteObject(self, objectId):
		for obj in self.objectList:
			if obj.id == objectId:
				self.objectList.remove(obj)

		for room in self.roomList:
			roomObject = room.getObject(objectId)
			if (roomObject):
				room.deleteObject(objectId)

	def addEnd(self, endId, endAttributes, endImages):
		newView = View.End(self, endId, endAttributes, endImages)
		self.endViewList.append(newView)
		return newView

	def addStart(self, startAttributes, startImages):
		newView = View.Start(self, startAttributes, startImages)
		self.startView = newView
		return newView

	def addSequence(self, sequenceId, sequenceAttributes, sequenceImages):
		newView = View.Sequence(self, sequenceId, sequenceAttributes, sequenceImages)
		self.sequenceList.append(newView)
		return newView

	def addRoom(self, roomId, roomAttributes, roomImages):
		newView = View.Room(self, roomId, roomAttributes, roomImages)
		self.roomList.append(newView)
		return newView

	def addCustomView(self, viewId, viewAttributes, viewImages):
		newView = View.Custom(self, viewId, viewAttributes, viewImages)
		self.customObjectList.append(newView)
		return newView

	def addMenu(self, menuId, menuAttributes, menuImages):
		newView = View.Menu(self, menuId, menuAttributes, menuImages)
		self.menuList.append(newView)
		return newView

	def removeObject(self, gameObject):
		# Remove object text references in other objects
		# Objects include secrets
		if (gameObject.__class__.__name__ in ("Item", "Object")):
			for room in self.getObjectsByType(("item", "object")):
				for obj in room["objects"]:
					#l = len(obj.texts)
					obj.removeText(gameObject.id)

	# TODO: Remove menu, ends, starts, custom objects
	def removeView(self, viewObject):
		viewType = viewObject.__class__.__name__

		if (viewType == "Room"):
			# Remove text references
			self.roomList.remove(viewObject)

			for roomObject in viewObject.getItems():
				self.removeObject(roomObject)

		elif (viewType == "Sequence"):
			self.sequenceList.remove(viewObject)

if (__name__ == "__main__"):
	sc = ScenarioData()
	sc.loadScenario()
	sc.saveScenario()


while True:
    m, n = list(map(int, input().split()))

    if m <= 0 or n <= 0:
        break
    if m > n:
        m, n = n, m

    soma = 0
    for i in range(m, n + 1):
        soma += i
        print('{}'.format(i), end=' ')
    print('Sum={}'.format(soma))


import numpy as np
import matplotlib as mpl
import matplotlib.animation as mpl_ani
import boltzpy.helpers.TimeTracker as h_tt

# Todo merge plots und animationen (tmax=0 -> plot)
# Todo methode plot(t=None) und save(t=None) unterscheiden
# Todo set up legend
class AnimatedFigure:
    def __init__(self,
                 tmax=1,
                 backend="TkAgg",
                 figsize=(16, 9),
                 dpi=300,
                 writer='ffmpeg',
                 **kwargs):
        mpl.use(backend)
        import matplotlib.pyplot as plt
        self.figure = plt.figure(figsize=figsize, dpi=dpi, **kwargs)
        assert type(tmax) == int
        self.tmax = int(tmax)
        self.writer = mpl.animation.writers[writer](fps=15, bitrate=1800)
        self.animated_axes = []
        self._time_tracker = h_tt.TimeTracker()
        return

    def add_subplot(self, position, **kwargs):
        assert isinstance(position, tuple)
        assert 0 < position[2] <= position[0] * position[1]
        ax = self.figure.add_subplot(*position, **kwargs)
        animated_ax = AnimatedAxes(ax, tmax=self.tmax)
        self.animated_axes.append(animated_ax)
        return animated_ax

    def update(self, i):
        for ax in self.animated_axes:
            ax.update(i)
        # print time estimate
        self._time_tracker.print(i + 1, self.tmax)
        return

    def plot(self):
        self.figure.tight_layout()
        print('Animating:')
        self._time_tracker = h_tt.TimeTracker()
        animation = mpl.animation.FuncAnimation(
            self.figure,
            self.update,
            frames=self.tmax,
            interval=1,
            blit=False
        )
        return animation

    # Todo check no file exists already
    # Todo check mp4 ending in file address
    def save(self, file_address):
        animation = self.plot()
        animation.save(file_address,
                       self.writer,
                       dpi=self.figure.dpi)
        return

# Todo add different modes:
#  Allow 0d plots
#   moving_dot: plot thin line, with thick dot at t=i
#   snake (plot t = [0,..,i])

class AnimatedAxes:
    def __init__(self, axes, tmax=1):
        assert isinstance(axes, mpl.axes.Axes)
        self.axes = axes
        assert type(tmax) == int
        self.tmax = int(tmax)
        self.animated_objects = []

    def plot(self, xdata, ydata, **kwargs):
        # assert constistency of time
        assert ydata.shape[0] == self.tmax
        # create animated line
        (mpl_line,) = self.axes.plot([], [], **kwargs)
        animated_line = AnimatedLine(mpl_line, xdata, ydata)
        self.animated_objects.append(animated_line)
        # update x limits
        self.axes.set_xlim(self.xmin, self.xmax)
        # update y limits, stretch range a bit, for nice look
        stretch = 1.25
        factors = np.array([(1 + stretch)/2, (1 - stretch)/2])
        ymin = np.sum(factors * [self.ymin, self.ymax])
        ymax = np.sum(factors * [self.ymax, self.ymin])
        self.axes.set_ylim(ymin, ymax)

    def update(self, i):
        for element in self.animated_objects:
            element.update(i)
        return

    @property
    def xmin(self):
        return np.min([e.xmin for e in self.animated_objects])

    @property
    def xmax(self):
        return np.max([e.xmax for e in self.animated_objects])

    @property
    def ymin(self):
        return np.min([e.ymin for e in self.animated_objects])

    @property
    def ymax(self):
        return np.max([e.ymax for e in self.animated_objects])

class AnimatedLine:
    def __init__(self, line, xdata, ydata):
        assert isinstance(line, mpl.lines.Line2D)
        self.line = line
        assert isinstance(xdata, np.ndarray)
        assert(xdata.ndim == 1)
        self.xdata = xdata
        assert isinstance(ydata, np.ndarray)
        assert (ydata.ndim == 2)
        assert (ydata.shape[1] == xdata.size)
        self.ydata = ydata
        self.update(0)

    def update(self, i):
        self.line.set_data(self.xdata, self.ydata[i, :])

    @property
    def tmax(self):
        return self.ydata.shape[0]

    @property
    def xmin(self):
        return np.min(self.xdata)

    @property
    def xmax(self):
        return np.max(self.xdata)

    @property
    def ymin(self):
        return np.min(self.ydata)

    @property
    def ymax(self):
        return np.max(self.ydata)


from scipy.stats import truncnorm
import numpy as np
import random
import copy

class PreData:
    def __init__(self, Data, index, label):
        self.index = index
        self.label = label
        self.Data = Data[self.index]
        self.mean = []
        self.diff = []
        return

    def mean(self, mean):
        self.mean = mean

    def diff(self, diff):
        self.diff = diff

def genPreDataDict(Data, indices, labels):
    out = dict()
    for count in range(len(indices)):
        out[labels[count]] = (PreData(Data, indices[count], labels[count]))
    return out

class TrainBatch:
    # data          :consecutive samples listed along dim0 i.e data.shape[0] = number of samples
    # bsize_mean    :batch size (used as mean for random batch size generation )
    # bsize_sigma   :sigma for random batch size generation (if None, all batches are of same size bsize_mean)
    # bsize_low     :lowest batch size allowed (used in truncated normal distribution)
    # bsize_high    :highest batch size allowed (if None, equal to data.shape[0])
    # rangelist     :rangelist to be used for batch generation (if None, rangelist is generated)
    def __init__(self, data, bsize_mean=10, bsize_sigma=None, bsize_low=1, bsize_high=None, rangelist=None):
        self.data = data
        self.rangelist=[]
        if rangelist is None:

            self.datasize = data.shape[0]

            self.bsize_mean = bsize_mean
            self.bsize_sigma = bsize_sigma
            self.bsize_low = bsize_low
            self.bsize_high = bsize_high

            if self.bsize_high is None:
                self.bsize_high = self.datasize

            if self.bsize_sigma is None:
                self.randomsized = False
            else:
                self.randomsized = True
                self.batchsizegenerator = truncnorm((self.bsize_low - self.bsize_mean) / self.bsize_sigma,
                                                    (self.bsize_high - self.bsize_mean) / self.bsize_sigma,
                                                    loc=self.bsize_mean,
                                                    scale=self.bsize_sigma)
            self.rangelist = self.generateRandRanges()
        else:
            self.rangelist = rangelist

        self.remainingrangelist = copy.deepcopy(self.rangelist)

    def getRangelist(self):
        return self.rangelist

    def nextbatch(self):
        return self.data[self.remainingrangelist.pop(), :]

    def generateRandRanges(self):
        temp_size = self.datasize
        Idx = 0
        size = self.bsize_mean
        while temp_size > 0:
            if self.randomsized:
                size = int(self.batchsizegenerator.rvs(1))
            step = min(size, temp_size)
            self.rangelist.append(range(Idx, Idx + step))
            temp_size = temp_size - size
            Idx = Idx + size
        random.shuffle(self.rangelist)
        return self.rangelist

def getTruncRandVal():
    return


# -*- coding: utf-8 -*-
# author: xiaoming
# date: 2020/8/10
import os
import configparser
import time
import logging
import shutil
from Dingtalk import DiffTask
from Dingtalk import UpdateNowTaskList
from Dingtalk import WriteEmail
from Dingtalk import GetEmail
from Dingtalk import GetDingTalkFormInfo
from Dingtalk import GetOriginName
from DownloadDcm import GetFilmno
from DownloadDcm import DownloadPatientDcm
from DownloadDcm import GetBodyItem
from DownloadDcm import IsAgentDoctor
from Oss import UploadPatientData
from Oss import GetTempAddress
from SendMail import SendEmail

#配置文件初始化
conf = configparser.ConfigParser()
conf_path = "DcmConfig.ini"
conf.read(conf_path)

#日志初始化
#级别：info、debug、warning、error

DingTalkLog = conf.get("Global","DingTalkLog")
logger = logging.getLogger(__name__)
logger.setLevel(level = logging.DEBUG)
handler = logging.FileHandler(DingTalkLog)
#handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
DicomDir = conf.get("Global","DicomDir")
Today = time.strftime("%Y/%m/%d", time.localtime())
OssAddress = conf.get('oss','Address')
EmailCache = conf.get('Global','EmailCache')

# 拿出新提交的审批
DiffTask = DiffTask()
if len(DiffTask) == 0:
    logger.info('没有新的审批')
    exit(200)
else:
    logger.info('发现新的审批。开始处理')
    # 遍历新发现的审批
    for TaskId in DiffTask:
        # 先拿出发起人
        origin_name = GetOriginName(TaskId)
        # 获取导出方式
        export_method = GetDingTalkFormInfo(TaskId)['导出方式']
        # 判断导出方式如果不是"后台统导出"就退出
        if export_method != '后台系统导出':
            logger.info('该审批不线上审批，无需后台处理。 '
                        '审批ID:%s 导出方式:%s 发起人:%s' %(TaskId,export_method,origin_name))
            # 这个函数是把当前TaskID给存到文件里，下次比对就不管了
            UpdateNowTaskList(TaskId)
            exit(200)
        # 获取钉钉审批状态和结果
        task_status = GetDingTalkFormInfo(TaskId)['task_status']
        task_result = GetDingTalkFormInfo(TaskId)['task_result']
        if task_status == 'TERMINATED':
            logger.info('审批终止了 TaskID:%s' %TaskId)
            UpdateNowTaskList(TaskId)
            exit(30)
        if task_status == 'COMPLETED':
            if task_result != 'agree':
                logger.debug('审批被拒绝 TaskId:%s 发起人:%s' %(TaskId,origin_name))
                UpdateNowTaskList(TaskId)
                exit(21)
            else:
                logger.info('审批同意，开始执行脚本 TaskId:%s 发起人:%s' %(TaskId,origin_name))
            try:
                # 获取审批人的邮箱
                OriginEmail = GetEmail(TaskId)
            except:
                logger.error('获取审批发起人Email地址失败 TaskId:%s 发起人%s' %(TaskId,origin_name))
                UpdateNowTaskList(TaskId)
                exit(11)
            # 先判断医生是否和销售关联
            doctor_name = GetDingTalkFormInfo(TaskId)['出单医生姓名']
            agent_name = GetOriginName(TaskId)
            if len(IsAgentDoctor(doctor_name)) == 0:
                logger.error('该医生不属于任何销售。任务ID:%s 医生:%s 销售:%s 发起人:%s'
                             % (TaskId, doctor_name, agent_name,origin_name))
                SendEmail(OriginEmail, '该医生不属于任何销售 销售:%s 医生:%s'
                          %(doctor_name,agent_name), '获取数据异常' )
                UpdateNowTaskList(TaskId)
                exit(16)
            if IsAgentDoctor(doctor_name)[0][0] != agent_name:
                logger.error('医生销售无关 任务ID:%s 医生:%s 销售:%s 发起人:%s'
                             %(TaskId, doctor_name, agent_name,origin_name))
                SendEmail(OriginEmail, '医生:%s 不是销售:%s 的代理医生' % (doctor_name, agent_name),
                          '获取数据异常')
                UpdateNowTaskList(TaskId)
                exit(17)
            logger.info('开始处理审批:%s 发起人:%s' %(TaskId,origin_name))
            # 先清邮件缓存
            if os.path.isfile(EmailCache):
                os.remove(EmailCache)
            # 写标题
            WriteEmail("以下是患者数据，有效期是72小时。
")
            try:
                # 获取患者姓名
                GtPatientsName = GetDingTalkFormInfo(TaskId)['患者姓名']
            except:
                logger.error('获取任务ID:%s患者姓名异常 发起人:%s' %(TaskId,origin_name))
                UpdateNowTaskList(TaskId)
                exit(12)
            # 患者姓名是空格分开的，可能有多个患者
            PatientsName = ' '.join(GtPatientsName.split())
            PatientsNameList = PatientsName.split(' ')
            # 遍历取出的患者姓名
            for PatientName in PatientsNameList:
                # 获取影像号，返回的元组，多个可能
                Filmno = GetFilmno(TaskId,PatientName)
                if len(Filmno) == 0:
                    logger.error('影像号获取异常 TaskId:%s 患者:%s '
                                 ' 发起人:%s ' %(TaskId,PatientName,origin_name))
                    WriteEmail('患者:%s 没有查到影像号' %PatientName)
                    continue
                PatientDir = '%s/%s'%(DicomDir,PatientName)
                if os.path.isdir(PatientDir) == False:
                    os.mkdir(PatientDir)
                # 遍历患者影像号
                for F in Filmno:
                    # 返回的每个影像号都是一个元组，这里给他解开
                    Fo = F[0]
                    # 获取检查项目
                    body_item = GetBodyItem(Fo)
                    patient_dir_filmno = '%s/%s/%s_%s/' % (DicomDir, PatientName,
                                                          Fo ,body_item)
                    if os.path.isdir(patient_dir_filmno) == False:
                        os.mkdir(patient_dir_filmno)
                    try:
                        logger.info('患者姓名：%s 影像号：%s 开始下载。' % (PatientName, Fo))
                        DownloadPatientDcm(Fo,patient_dir_filmno)
                        logger.info('患者姓名：%s 影像号：%s 下载完成。' %(PatientName, Fo))
                    except:
                        logger.error('患者姓名：%s 影像号：%s 下载失败。 发起人:%s' %(PatientName,Fo,origin_name))
                        exit(14)

                # 切换到dicom工作目录
                os.chdir(DicomDir)
                dir_name = DicomDir+'/'+PatientName
                try:
                    shutil.make_archive(dir_name, 'zip', dir_name)
                    logger.info('%s 压缩成功,开始上传' % PatientName)
                    LocalFile = '%s.zip' % PatientDir
                    OssDir = '%s/%s.zip' % (Today, PatientName)
                except:
                    logger.error('压缩失败 患者:%s 发起人:%s' %(PatientName,origin_name))
                    exit(14)

                try:
                    # 上传患者数据到oss
                    UploadPatientData(OssDir,LocalFile)
                    logger.info('%s 上传完成' %PatientName)
                except:
                    logger.error('上传失败 (UploadPatientData函数报错)'
                                 ' 患者:%s 发起人:%s' %(PatientName,origin_name))
                    # 写邮件
                #email_url = OssAddress+OssDir
                email_url = GetTempAddress(OssDir)
                email_content = '
点击下载患者 %s 的影像数据
' \
                                %(email_url,PatientName)
                WriteEmail(email_content)
                # 删除临时文件
                logger.info('删除患者临时文件 患者:%s' %PatientName)
                os.system('rm -rf %s*' %PatientName)
        else:
            # 审批还在进行就退出脚本不发邮件
            logger.info('审批还在进行中 TaskId:%s 发起人:%s' %(TaskId,origin_name))
            #print('审批还在进行中')
            exit(200)

        # 发邮件
        WriteEmail("
----
author by：xiaoming")
        f = open(EmailCache, "r", encoding='UTF-8')
        str = f.read()
        f.close()
        try:
            title = Today+'患者数据'
            SendEmail(OriginEmail, str,title)
            logger.info('%s 发送邮件成功.' %PatientName)
        except:
            logger.error('邮件发送失败 患者:%s 任务ID:%s 发起人:%s' %(TaskId,PatientName,origin_name))
        # 把完成的ID加到文件里去
        UpdateNowTaskList(TaskId)


import sqlite3

connection = None
cursor = None

testemail = "bob@123.ca"

def connect(path):
    global connection, cursor

    connection = sqlite3.connect(path)
    cursor = connection.cursor()
    cursor.execute(' PRAGMA foreign_keys=ON; ')
    connection.commit()
    return

def login():
    print('Menu Options:\n')
    print('l: Login\n')
    print()
    input()
    return 0

def add_carnumber():
    return 0

def add_enroute():
    return 0

def tuplestuff(keyword):
    global connection, cursor
    cursor.execute('SELECT lcode, city, prov, address FROM locations;')
    beforechanges = cursor.fetchall()
    afterchanges = []
    for i in beforechanges:
        if keyword.lower() in i[0].lower():
            afterchanges.append(i)
        elif keyword.lower() in i[1].lower():
            afterchanges.append(i)
        elif keyword.lower() in i[2].lower():
            afterchanges.append(i)
        elif keyword.lower() in i[3].lower():
            afterchanges.append(i)
    return afterchanges

def location_list(keyword):
    global connection, cursor
    selected = 0
    v = 0
    loc_list = []
    loc_list = tuplestuff(keyword)
    if (len(loc_list) == 0):
        return None
    #Locations into a list and call it loc_list
    print("LIST OF LOCATIONS:\n")
    while (selected == 0):
        for x in range(v, v+5):
            if (x < len(loc_list)):
                print(str(x+1) + ".) " + str(loc_list[x]))
        print("Input number to select location;")
        if (v+5 < len(loc_list)):
            print("f: Forward")
        if (v != 0):
            print("p: Previous")
        selection = input()
        if (selection == 'f') and (v+5 < len(loc_list)):
            v = v + 5
        elif (selection == 'p') and (v != 0):
            v = v - 5
        else:
            try:
                val = int(selection)
                if (val < len(loc_list)):
                    selected = val
                    return loc_list[selected]
            except ValueError:
                print("Syntax error\n")
    return None

def offer_ride(driver):
    global connection, cursor

    cursor.execute('SELECT * FROM rides;')
    rides = cursor.fetchall()
    rno = len(rides)

    print("Input date: ")
    date = input()
    print("Input number of seats offered: ")
    seats = input()
    print("Input price per seat: ")
    price = input()
    print("Input luggage description: ")
    lugdesc = input()
    print("Input source location: ")
    src = input()
    src_loc = location_list(src)
    if (src_loc is None):
        print("No location found; return to previous.")
        return 0
    print("Input destination location: ")
    dst = input()
    dst_loc = location_list(dst)
    if (dst_loc is None):
        print("No location found; return to previous.")
        return 0

    print("Input car number (Blank for null): ")
    cno = input()
    #Checks if car number is valid (if it belongs to the member) if not terminates

    rno = rno + 1
    data = (rno,price,date,seats,lugdesc,src_loc[0],dst_loc[0],driver,cno)
    cursor.execute('INSERT INTO rides VALUES (?,?,?,?,?,?,?,?,?)',data)

    while (enstop == False):
        print("Enroutes: \n")
        print("e: Add Enroute\n")
        print("q: Stop Adding Enroute\n")
        s = input()
        if (s == 'e'):
            print("Input destination location: ")
            enloc = input()
            ret_enloc = location_list(enloc)
            if (ret_enloc is not None):
                #Adds the rno and lcode to the enroute
                lcode = ret_enloc[0]
                values = (rno, lcode)
                cursor.execute('INSERT INTO enroute VALUES (?,?)',values)
                print("Added enroute\n")
        elif (s == 'q'):
            enstop = True
        else:
            print("Syntax error\n")
    connection.commit()
    return 0

def ride_menu():
    stop = False
    while (stop == False):
        print('Ride Menu:\ns: Stop\no: Offer rides\n')
        value = input()
        if (value == 's'):
            stop = True
        elif (value == 'o'):
            offer_ride(email)
        else:
            print("Syntax error\n")
    print('Ride menu stopped\n')
    return 0

def main():
    connect('./test.db')
    ride_menu()
    #login()

main()


#!/usr/bin/env python

# Copyright 2017, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in witing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import multiprocessing
import os

import yaml

BASE_PATH = '/etc/rackspace-monitoring-agent.conf.d'
RUN_VENV = '/usr/lib/rackspace-monitoring-agent/plugins/run_plugin_in_venv.sh'

RETURN_QUEUE = multiprocessing.Queue()

class Runner(object):
    def __init__(self):
        self.queue = multiprocessing.JoinableQueue()
        self.processes = [
            multiprocessing.Process(target=self.run_details)
            for _ in range(2)
        ]
        for p in self.processes:
            p.start()

    def put(self, item):
        self.queue.put(item)

    def run_details(self):
        while True:
            detail_args = self.queue.get()
            if not detail_args:
                break

            detail_args.insert(0, RUN_VENV)
            detail_args.insert(2, '--telegraf-output')
            RETURN_QUEUE.put(' '.join(detail_args).strip())

            self.queue.task_done()

    def terminate(self):
        self.queue.join()
        for p in self.processes:
            p.terminate()

def str2bool(boolean):
    if boolean.lower() in ("yes", "true", "1"):
        return True
    elif boolean.lower() in ("no", "false", "0"):
        return False
    else:
        raise BaseException('Not a Boolean')

def load_yaml(check_file):
    with open(check_file) as f:
        return yaml.load(f.read())

def main():
    r = Runner()
    try:
        for _, _, items in os.walk(BASE_PATH):
            for item in items:
                check_load = load_yaml(os.path.join(BASE_PATH, item))
                if not str2bool(boolean=check_load.get('disabled')):
                    details = check_load.get('details')
                    if isinstance(details, dict) and 'args' in details:
                        r.put(item=details['args'])
    finally:
        r.terminate()

    q_items = list()
    while True:
        try:
            q_item = RETURN_QUEUE.get(timeout=1)
        except Exception:
            break
        else:
            q_items.append(q_item)

    # Print a sorted list of commands
    print('\n'.join(sorted(q_items)))

if __name__ == '__main__':
    main()


def solve():
    n, k = map(int, input().split())
    arr = list(input().split())
    print(' '.join(arr[k:] + arr[0:k]))

def main():
    test = 1
    test = int(input())
    while test >= 1:
        solve()
        test -= 1

if __name__ == "__main__":
    main()


import os
from tkinter import ttk
import tkinter as tk
import tkinter.font as tkFont
from tkinter import filedialog
import tkinter.messagebox as tkMessage

import DES

class Menu(ttk.Frame):
    def __init__(self, main_window):

        #variables
        v = tk.IntVar(value = 1)
        v_1 = tk.IntVar(value = 0)
        v_2 = tk.IntVar(value = 0)
        v_3 = tk.IntVar(value = 0)
        v_4 = tk.IntVar(value = 0)
        file_route = tk.StringVar()
        operation_text = tk.StringVar(value="Encrypt")
        key_String = tk.StringVar()

        #functions
        def get_file_route():
            file_name = filedialog.askopenfilename(initialdir ="C:", title="Open File")
            file_route.set(file_name)

        def check_radios():
            if v.get() == 1:
                operation_text.set("Encrypt")
            elif v.get() == 2:
                operation_text.set("Decrypt")

        def operate():
            ok = True

            if len(key_String.get()) == 0:
                tkMessage.showinfo(title=None, message="Insert a key")
                ok = False
            if len(file_route.get()) == 0:
                tkMessage.showinfo(title=None, message="Select a file")
                ok = False
            if v.get() != 1 and v.get() != 2:
                tkMessage.showinfo(title=None, message="Select an operation")
                ok = False
            if len(key_String.get()) != 8 :
                tkMessage.showinfo(title=None, message="Invalid Key. Must be 8 characters long")
                ok = False
            if v_1.get() == 0 and v_2.get() == 0 and v_3.get() == 0 and v_4.get() == 0:
                tkMessage.showinfo(title=None, message="Select a Mode of Operation")
                ok = False

            if ok:
                if v.get() == 1:
                    if v_1.get() == 1:
                        DES.des(bytes(key_String.get(), 'utf-8'), file_route.get(), DES.ENCRYPT, DES.ECB)
                    if v_2.get() == 1:
                        DES.des(bytes(key_String.get(), 'utf-8'), file_route.get(), DES.ENCRYPT, DES.CBC)
                    if v_3.get() == 1:
                        DES.des(bytes(key_String.get(), 'utf-8'), file_route.get(), DES.ENCRYPT, DES.CFB)
                    if v_4.get() == 1:
                        DES.des(bytes(key_String.get(), 'utf-8'), file_route.get(), DES.ENCRYPT, DES.OFB)
                if v.get() == 2:
                    if v_1.get() == 1:
                        DES.des(bytes(key_String.get(), 'utf-8'), file_route.get(), DES.DECRYPT, DES.ECB)
                    if v_2.get() == 1:
                        DES.des(bytes(key_String.get(), 'utf-8'), file_route.get(), DES.DECRYPT, DES.CBC)
                    if v_3.get() == 1:
                        DES.des(bytes(key_String.get(), 'utf-8'), file_route.get(), DES.DECRYPT, DES.CFB)
                    if v_4.get() == 1:
                        DES.des(bytes(key_String.get(), 'utf-8'), file_route.get(), DES.DECRYPT, DES.OFB)

                tkMessage.showinfo(title=None, message="Process completed!")

        #window config
        super().__init__(main_window)
        main_window.title("DES")
        main_window.resizable(False, False)
        main_window.configure(background='#FFFFFF')

         # get screen width and height
        screen_width = main_window.winfo_screenwidth()
        screen_height = main_window.winfo_screenheight()
        width = 420
        height = 450

        # calculate position x and y coordinates
        x = (screen_width/2) - (width/2)
        y = (screen_height/2) - (height/2)
        main_window.geometry('%dx%d+%d+%d' % (width, height, x, y))

        #font style
        fontStyleTitle = tkFont.Font(family="Roboto", size=20)
        fontStyleSub = tkFont.Font(family="Roboto", size=12)
        xAlignment = 30

        ##components
        labelMain = ttk.Label(
            text="DES",
            background = '#FFFFFF',
            font=fontStyleTitle
        )
        labelMain.place(x=xAlignment+140, y=10)

        ##FORM
        labelKey = ttk.Label(
            text = "Key: ",
            background = '#FFFFFF',
            font=fontStyleSub
        )
        labelKey.place(x=xAlignment, y=150)

        keyEntry = tk.Entry(
            width=34,
            font=fontStyleSub,
            background="#00863d",
            foreground="#ffffff",
            textvariable = key_String,
        )
        keyEntry.place(x=xAlignment+50, y=150)

        fileEntry = tk.Entry(
            width=26,
            font=fontStyleSub,
            background="#00863d",
            state='disabled',
            textvariable=file_route
        )
        fileEntry.place(x=xAlignment+120, y=64)

        labelOper = ttk.Label(
            text="Select operation: ",
            background = '#FFFFFF',
            font=fontStyleSub
        )
        labelOper.place(x=xAlignment, y=104)

        labelMode = ttk.Label(
            text="Modes of operation:",
            background = '#FFFFFF',
            font=fontStyleSub
        )
        labelMode.place(x=xAlignment, y=208)

        rad1 = tk.Radiobutton(
            text="Encrypt",
            background = '#FFFFFF',
            padx = 20,
            variable=v,
            value=1,
            font=fontStyleSub,
            command = check_radios
        ).place(x=xAlignment+124, y=104)

        rad2 = tk.Radiobutton(
            text="Decrypt",
            background = '#FFFFFF',
            padx = 20,
            variable=v,
            value=2,
            font=fontStyleSub,
            command = check_radios
        ).place(x=xAlignment+224, y=104)

        #check buttons
        ecb= tk.Checkbutton(
            text='Electronic Codebook (ECB)',
            background = '#FFFFFF',
            font=fontStyleSub,
            variable=v_1,
            onvalue=1,
            offvalue=0
        ).place(x= xAlignment, y = 244)

        cbc= tk.Checkbutton(
            text='Cipher Block Chaining (CBC)',
            background = '#FFFFFF',
            font=fontStyleSub,
            variable=v_2,
            onvalue=1,
            offvalue=0
        ).place(x= xAlignment, y = 270)

        cfb= tk.Checkbutton(
            text='Cipher Feedback (CFB)',
            background = '#FFFFFF',
            font=fontStyleSub,
            variable=v_3,
            onvalue=1,
            offvalue=0
        ).place(x= xAlignment, y = 296)

        ofb= tk.Checkbutton(
            text='Output Feedback (OFB)',
            background = '#FFFFFF',
            font=fontStyleSub,
            variable=v_4,
            onvalue=1,
            offvalue=0
        ).place(x= xAlignment, y = 322)

        ##buttons
        operation = tk.Button(
            textvariable=operation_text,
            width=10,
            foreground="#ffffff",
            background="#00863d",
            font=fontStyleSub,
            command = operate
        )
        operation.place(x=xAlignment, y=380)

        fileSel = tk.Button(
            text="Select file",
            width=10,
            foreground="#ffffff",
            background="#00863d",
            font=fontStyleSub,
            command = get_file_route
        )
        fileSel.place(x=xAlignment, y=60)

main_window = tk.Tk()
app = Menu(main_window)
app.mainloop()


# 7. 10001st Prime

"""
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?
"""

# Brainstorm
"""
The brute force method is to simply iterate through numbers, divide by numbers from 2 to square root of itself, then
if none of the potential factors can divide the number evenly, deem it as a prime. Repeat the process until 10001st
prime is found.

However, since primes are numbers indivisible by anything but by it self and one, for all subsequent numbers bigger
than prime number P, we are assured that 2P, 3P... nP are not going to be primes.
Perhaps we can keep track of primes that we have encountered and count up to N, finding and keeping track of
primes by checking that they are not multiples of previous primes by incrementing a list of primes.

We speed up the process by incrementing by 2, since even numbers after 2 cannot be prime.
"""
from _timeit import timeit

# Solution A
@timeit
def FindNthPrimeA(N):
    track_prime = [[3, 3]]
    primenum = 1
    number = 3
    tracking = 0
    trackmul = 9
    if N == 1:
        return 2
    while primenum != N - 1:
        number += 2
        is_prime = True
        for primes in range(tracking):
            track_prime[primes][0] -= 2
            if track_prime[primes][0] == 0:
                track_prime[primes][0] = track_prime[primes][1]
                is_prime = False
            elif track_prime[primes][0] == -1:
                track_prime[primes][0] = track_prime[primes][1]-1
        if trackmul == number:
            tracking += 1
            trackmul = track_prime[tracking][0]**2
        elif is_prime:
            track_prime.append([number, number])
            primenum += 1
    return number

# Solution B
@timeit
def FindNthPrimeB(N):
    track_prime = []
    primenum = 1
    number = 1
    if N == 1:
        return 2
    while primenum != N:
        number += 2
        is_prime = True
        sqrt = number**(1/2)
        for primes in range(len(track_prime)):
            if number % track_prime[primes] == 0:
                is_prime = False
                break
            if track_prime[primes] > sqrt:
                is_prime = True
                break
        if is_prime:
            track_prime.append(number)
            primenum += 1
    return number

# Optimal Solution
@timeit
def FindNthPrimeC(N):
    primenum = 2
    count = 0
    track_prime = []
    if N < 1:
        return False
    elif N <= 2:
        return N + 1
    while primenum < N:
        count += 1
        if (IsPrime(6 * count - 1, track_prime)):
            primenum += 1
            track_prime.append(6*count-1)
        if (IsPrime(6 * count + 1, track_prime)):
            primenum += 1
            track_prime.append(6*count+1)
    if primenum == N:
        return track_prime[-1]
    else:
        return track_prime[-2]

def IsPrime(candidate, primes):
    sqrt = candidate**(1/2)
    for item in primes:
        if candidate % item == 0:
            return False
        elif item > sqrt:
            return True
    return True

# Optimal Solution
"""
Prime candidates above 2 and 3 can actually be narrowed down to the formula 6k -/+ 1.
For this reason, instead of incrementing 2 to find the potential candidate, one can simply increment k
and see if the two possible outcomes end up as primes.
As testing shows, solution A is approx. 5 times slower than B, and B is almost twice as slow as C.
"""

#print(FindNthPrimeA(10001))
#print(FindNthPrimeB(10001))
print(FindNthPrimeC(10001))

Coding Challenges/Exercise Problems/Z. Project Euler/7. 10001st Prime.py


#!/usr/bin/env python3
"""
A class representing a C structure
"""

class CStructMember():
    PADDING_SPACES = 4  # number of spaces to insert before member

    def __init__(self,
                 member_name,
                 member_type,
                 member_array_size):
        """
        A member of a C struct
        :param member_name: A valid C identifier
        :param member_type; a valid C type
        :param member_array_size: The size of the array, if type is an array
        """
        self.member_name = member_name
        self.member_type = member_type
        self.member_array_size = member_array_size

    def render_array_size_spec(self):
        """
        Render the array size specifier for this member, if there is one.
        :return: string
        """
        if self.member_array_size is not None:
            return "[{size}]".format(size=self.member_array_size)
        return ""

    def __str__(self):
        string = ""
        string += "{padding}{member_type} {member_name}{array_size_spec};".format(
            member_type=self.member_type,
            member_name=self.member_name,
            padding=" "*CStructMember.PADDING_SPACES,
            array_size_spec=self.render_array_size_spec()

        )
        return string

class CStruct():
    def __init__(self, structs_json):
        """
        :param the Struct's JSON: the name of the struct
        """
        self.structs_json = structs_json
        self.struct_name = None
        self.members = []
        self.parse_struct()

    def parse_struct(self):
        """
        Parse the structs_json
        """
        self.struct_name = self.structs_json["name"]
        self.is_packed = self.structs_json["is_packed"] == "True"
        self.parse_struct_members(members=self.structs_json["members"])

    def parse_struct_members(self, members):
        """
        Parse struct members
        :param members: the list of members in the struct. Each member has a type, value and name.
        """
        for member in members:
            array_size = None
            if "array_size" in member.keys():
                array_size = member["array_size"]
            self.members.append(CStructMember(member_name=member["name"],
                                              member_type=member["type"],
                                              member_array_size=array_size))

    def render_members(self):
        """
        Render each member's str method
        """
        string = ""
        for member in self.members:
            string += str(member) + "\n"
        return string

    def render_modifiers(self):
        """
        If any modifiers for this struct type (e.g. packed, aligned? etc.)
        render them appropriately for the GCC toolchain
        """
        string = ""
        if self.is_packed:
            string = "__attribute__((__packed__)"
        return string

    def __str__(self):
        """
        Emit a C like structure suitable for insertion in a proper .h file.
        :return string representing a header file worthy C structure definition
        """

        string = ""
        string += "typedef struct {modifiers} _{struct_name}_t\n{brace_open}\n{members}{brace_close} "\
            "{struct_name}_t;\n".format(
                struct_name=self.struct_name,
                brace_open="{",
                brace_close="}",
                members=self.render_members(),
                modifiers=self.render_modifiers())
        return string


from urllib.request import urlopen as ureq
from bs4 import BeautifulSoup as soup

my_url='https://www.newegg.com/global/in/Product/ProductList.aspx?Submit=ENE&DEPA=0&Order=BESTMATCH&Description=graphics+card&ignorear=0&N=-1&isNodeId=1'

client=ureq(my_url)
page_html=client.read()
client.close()

page_soup=soup(page_html,"html.parser")

containers = page_soup.findAll("div",{"class":"item-container"})

file_name="graphic_cards.csv"

f=open(file_name,"w",encoding="utf-8")

headers="Brand,Product_name,price\n"

f.write(headers)

for container in containers:
	brand_name=container.div.div.a.img["title"]

	title_container=container.findAll("a",{"class":"item-title"})
	product_name=title_container[0].text

	price_container = container.findAll("li",{"class":"price-current"})
	price=price_container[0].text.strip().replace("\xa0(2 Offers)\n\n","")

	f.write(brand_name+","+product_name.replace(",","|")+","+price+"\n")

f.close()


#!/usr/bin/env python3

# Read, process, and convert Antarctica data.

import numpy as np
import numpy.ma as ma

import xml.etree.ElementTree as ET
import argparse
import os
import sys

from netCDF4 import Dataset
from mpl_toolkits.basemap import Basemap
import gdal

import matplotlib.pyplot as plt

import pickle
import os

import pyproj

class Ant_data():

    def get_veloData_v0(self):

        # Velocity data.
        print("Getting velocity data...")

        velo_dir = '/net/jokull/nobak/mzzhong/Ant_Data/velocity_models'

        use_nc = 1

        if os.path.isfile(velo_dir + '/'+'AntVelo.npz') and use_nc == 0:

            npzfile = np.load(velo_dir + '/AntVelo.npz')

            v = npzfile['v']
            vel_lon = npzfile['vel_lon']
            vel_lat = npzfile['vel_lat']

        else:
            vel_file = velo_dir + '/antarctica_ice_velocity_900m.nc'
            fh=Dataset(vel_file,mode='r')
            vx = fh.variables['vx'][:]
            vy = fh.variables['vy'][:]
            v = np.sqrt(np.multiply(vx,vx)+np.multiply(vy,vy))

            x0 = np.arange(-2800000,2800000,step=900)+2800000
            y0 = np.arange(-2800000,2800000,step=900)+200+2800000
            x,y = np.meshgrid(x0,y0)

            AntVeloDataMap = Basemap(width=5600000,height=5600000,\
                                resolution='l',projection='stere',\
                                lat_ts=-71,lat_0=-90,lon_0=0)

            vel_lon, vel_lat= AntVeloDataMap(x,y,inverse="true")
            v = np.flipud(v)

            v = v/365

            #np.savez(velo_dir + '/AntVelo.npz',v=v,vel_lon=vel_lon,vel_lat=vel_lat)

        return v, vel_lon,vel_lat

    def get_veloData_v1(self):

        # Velocity data.
        print("Getting velocity data...")

        velo_dir = '/net/jokull/nobak/mzzhong/Ant_Data/velocity_models'
        file_basename = 'antarctica_ice_velocity_900m.nc'
        vel_file = os.path.join(velo_dir, file_basename)

        npz_filebasename = 'AntVelo_v1.npz'
        npz_filebasename_error = 'AntVelo_v1_error.npz'

        redo = 0
        if os.path.isfile(velo_dir + '/' + npz_filebasename) and redo == 0:

            npzfile = np.load(velo_dir + '/' + npz_filebasename)

            self.vel_lon = npzfile['vel_lon']
            self.vel_lat = npzfile['vel_lat']
            self.ve = npzfile['ve']
            self.vn = npzfile['vn']
            self.v_comb = npzfile['v_comb']

            self.vy = npzfile['vx']
            self.vx = npzfile['vy']
            self.v = npzfile['v']

            #####################
            npzfile = np.load(velo_dir + '/' + npz_filebasename_error)

            self.ve_err = npzfile['ve_err']
            self.vn_err = npzfile['vn_err']
            self.err = npzfile['err']
            self.err_comb = npzfile['err_comb']

        else:
            fh=Dataset(vel_file,mode='r')

            for key in fh.variables.keys():
                print(fh.variables[key])

            x0 = np.arange(-2800000,2800000,step=900)
            y0 = np.arange(-2800000,2800000,step=900)+200
            x,y = np.meshgrid(x0,y0)
            vProj = pyproj.Proj('+init=EPSG:3031')
            llhProj = pyproj.Proj('+init=EPSG:4326')
            vel_lon, vel_lat = pyproj.transform(vProj, llhProj, x,y)

            # Read in vx, vy.
            vx = fh.variables['vx'][:]
            vy = fh.variables['vy'][:]
            err = fh.variables['err'][:]

            vx = np.flipud(vx)
            vy = np.flipud(vy)
            err = np.flipud(err)

            # Velocity meter per day.
            vx = vx/365
            vy = vy/365
            err = err/365

            vx_err = np.multiply(err, np.divide(vx, np.sqrt(vx**2 + vy**2)+0.0001))
            vy_err = np.multiply(err, np.divide(vy, np.sqrt(vx**2 + vy**2)+0.0001))

            # Convert from XY to EN
            # refPt.
            refPt = vProj(0.0, 0.0, inverse=True)
            # refPt: lon 0, lat -90

            print('Coverting XY to EN...')
            lonr = np.radians(vel_lon - refPt[0])

            ve = np.multiply(vx, np.cos(lonr)) - np.multiply(vy, np.sin(lonr))
            vn = np.multiply(vy, np.cos(lonr)) + np.multiply(vx, np.sin(lonr))

            ve_err = np.abs(np.multiply(vx_err, np.cos(lonr)) - np.multiply(vy_err, np.sin(lonr)))
            vn_err = np.abs(np.multiply(vy_err, np.cos(lonr)) + np.multiply(vx_err, np.sin(lonr)))

            # Speed.
            v = np.sqrt(np.multiply(vx,vx)+np.multiply(vy,vy))

            # Speed for checking.
            v_comb = np.sqrt(np.multiply(ve,ve)+np.multiply(vn,vn))

            # err for checking.
            err_comb = np.sqrt(np.multiply(ve_err,ve_err)+np.multiply(vn_err,vn_err))

            # Save to the class.
            self.vel_lon = vel_lon
            self.vel_lat = vel_lat

            self.vx = vx
            self.vy = vy

            self.ve = ve
            self.vn = vn

            self.v = v
            self.v_comb = v_comb

            self.vx_err = vx_err
            self.vy_err = vy_err
            self.ve_err = ve_err
            self.vn_err = vn_err
            self.err = err
            self.err_comb = err_comb

            # Save the results.
            np.savez(velo_dir + '/'+ npz_filebasename, v_comb=v_comb, ve=ve, vn=vn, vel_lon=vel_lon, vel_lat=vel_lat, v=v, vx=vx, vy=vy)
            np.savez(velo_dir + '/'+ npz_filebasename_error, err_comb=err_comb, ve_err=ve_err, vn_err = vn_err, err=err)

        write_to_xyz = True
        if write_to_xyz:
            print('Writing...')

            vel_lon = np.round(self.vel_lon * 50)/50
            vel_lat = np.round(self.vel_lat * 200)/200
            vx = self.vx
            vy = self.vy
            v = self.v

            f = open(os.path.join(velo_dir,'FRIS_v1.xyz'),'w')
            keys = []

            for row in range(vel_lon.shape[0]):
                for col in range(vel_lon.shape[1]):

                    lon = vel_lon[row, col]
                    if lon > 180:
                        lon = lon-360

                    lat = vel_lat[row, col]

                    if lon>-95 and lon<-30 and lat>-85 and lat<-72:
                        #vxx = vx[row, col]
                        #vyy = vy[row, col]
                        #z = np.sqrt(vxx**2 + vyy**2)

                        z = v[row, col]

                        if z > 0:
                            #print(str(lon)+' '+str(lat)+' '+str(z)+'\n')
                            f.write(str(lon)+' '+str(lat)+' '+str(z)+'\n')
                            #keys.append((lon,lat))

            f.close()

    def get_veloData_v2(self):

        # Available keys:
        # 'coord_system', 'x', 'y', 'lat', 'lon', 'VX', 'VY',
        # 'STDX', 'STDY', 'ERRX', 'ERRY', 'CNT'

        # Velocity data.
        print("Getting velocity data...")

        velo_dir = '/net/jokull/nobak/mzzhong/Ant_Data/velocity_models'
        file_basename = 'another/antarctica_ice_velocity_450m_v2.nc'
        vel_file = os.path.join(velo_dir, file_basename)

        npz_filebasename = 'AntVelo_v2.npz'
        #npz_filebasename_error = 'AntVelo_v2_error.npz'

        redo = 0
        if os.path.isfile(velo_dir + '/' + npz_filebasename) and redo == 0:

            print('Loading v2...')
            npzfile = np.load(velo_dir + '/' + npz_filebasename)

            self.vel_lon = npzfile['vel_lon']
            self.vel_lat = npzfile['vel_lat']
            self.ve = npzfile['ve']
            self.vn = npzfile['vn']
            self.v_comb = npzfile['v_comb']

            self.vy = npzfile['vx']
            self.vx = npzfile['vy']
            self.v = npzfile['v']

            #####################
            #npzfile = np.load(velo_dir + '/' + npz_filebasename_error)

            #self.ve_err = npzfile['ve_err']
            #self.vn_err = npzfile['vn_err']
            #self.err = npzfile['err']
            #self.err_comb = npzfile['err_comb']

        else:
            fh=Dataset(vel_file,mode='r')

            for key in fh.variables.keys():
                print(fh.variables[key])

            # Coordinates transformation.
            vel_lon = fh.variables['lon'][:]
            vel_lat = fh.variables['lat'][:]

            vx = fh.variables['VX'][:].data
            vy = fh.variables['VY'][:].data

            print(vel_lon)

            plt.figure(1, figsize=(10,10))
            plt.imshow(np.abs(vel_lon), vmin=0, vmax=1000, cmap='coolwarm')
            plt.savefig('1.png')

            vx = vx/365
            vy = vy/365

            # Downsample:
            #vel_lon = vel_lon[0::2,0::2]
            #vel_lat = vel_lat[0::2,0::2]
            #vx = vx[0::2,0::2]
            #vy = vy[0::2,0::2]

            # Convert from XY to EN
            # refPt.
            vProj = pyproj.Proj('+init=EPSG:3031')
            refPt = vProj(0.0, 0.0, inverse=True)
            # refPt: lon 0, lat -90

            print('Converting XY to EN...')
            lonr = np.radians(vel_lon - refPt[0])

            ve = np.multiply(vx, np.cos(lonr)) - np.multiply(vy, np.sin(lonr))
            vn = np.multiply(vy, np.cos(lonr)) + np.multiply(vx, np.sin(lonr))

            # Speed.
            v = np.sqrt(vx**2 + vy**2)

            # Speed for checking.
            v_comb = np.sqrt(np.multiply(ve,ve)+np.multiply(vn,vn))

            # Save to the class.
            self.vel_lon = vel_lon
            self.vel_lat = vel_lat

            self.vx = vx
            self.vy = vy

            self.ve = ve
            self.vn = vn

            self.v = v
            self.v_comb = v_comb

            print('Saving...')

            # Save the results.
            np.savez(velo_dir + '/'+ npz_filebasename, v_comb=v_comb, ve=ve, vn=vn, vel_lon=vel_lon, vel_lat=vel_lat, v=v, vx=vx, vy=vy)

            #np.savez(velo_dir + '/'+ npz_filebasename_error, err_comb=err_comb, ve_err=ve_err, vn_err = vn_err, err=err)

        write_to_xyz = True

        if write_to_xyz:
            print('Writing...')

            vel_lon = np.round(self.vel_lon * 50)/50
            vel_lat = np.round(self.vel_lat * 200)/200
            vx = self.vx
            vy = self.vy
            v = self.v

            f = open(os.path.join(velo_dir,'Evans_v2.xyz'),'w')
            keys = []

            for row in range(vel_lon.shape[0]):
                for col in range(vel_lon.shape[1]):

                    lon = vel_lon[row, col]
                    if lon > 180:
                        lon = lon-360

                    lat = vel_lat[row, col]

                    if lon>-95 and lon<-60 and lat>-82 and lat<-72:
                        #vxx = vx[row, col]
                        #vyy = vy[row, col]
                        #z = np.sqrt(vxx**2 + vyy**2)

                        z = v[row, col]

                        # Only non-zero values are output.
                        if z > 0:
                            #print(str(lon)+' '+str(lat)+' '+str(z)+'\n')
                            f.write(str(lon)+' '+str(lat)+' '+str(z)+'\n')
                        #keys.append((lon,lat))

            f.close()

        return 0

    def get_glData(self):

        # Groundline data.
        print("Getting grounding line data...")

        #if os.path.isfile('/net/jokull/nobak/mzzhong/Ant_Data/GL_Points.npz'):
        #    npzfile = np.load('/net/jokull/nobak/mzzhong/Ant_Data/GL_Points.npz')
        #    gldata = npzfile['gldata']
        #else:
        #    gl_file = '/net/jokull/nobak/mzzhong/Ant_Data/GL_Points.txt'
        #    gldata = np.genfromtxt(gl_file,dtype=None)
        #    gldata[gldata[:,0]>180,0] = gldata[gldata[:,0]>180,0]-360

        #    np.savez('/net/jokull/nobak/mzzhong/Ant_Data/GL_Points.npz',gldata=gldata)

        gl_file = '/net/jokull/nobak/mzzhong/Ant_Data/GL/GL_Points_Evans_min.txt'
        gldata = np.genfromtxt(gl_file,dtype=None)
        gldata[gldata[:,0]>180,0] = gldata[gldata[:,0]>180,0]-360

        return gldata

    def get_CryoSat_DEM(self):

        # Velocity data.
        print("Getting cryosat dem data...")

        dem_dir = '/net/jokull/nobak/mzzhong/Ant_Data/DEM-CryoSat'
        file_basename = 'Antarctica_Cryosat2_1km_DEMv1.0.nc'
        dem_file = os.path.join(dem_dir, file_basename)

        fh=Dataset(dem_file,mode='r')

        #for key in fh.variables.keys():
        #    print(fh.variables[key])

        #x0 = np.arange(-2819500,2820500,step=1000)
        #y0 = np.arange(-2419500,2420500,step=1000)

        print(fh.variables)
        x0 = fh.variables['x'][:]
        y0 = fh.variables['y'][:]

        dem = fh.variables['z'][:]

        x,y = np.meshgrid(x0,y0)
        vProj = pyproj.Proj('+init=EPSG:3031')
        llhProj = pyproj.Proj('+init=EPSG:4326')
        dem_lon, dem_lat = pyproj.transform(vProj, llhProj, x,y)

        self.dem_lon = dem_lon
        self.dem_lat = dem_lat
        self.dem = dem

        write_to_xyz = True

        if write_to_xyz:
            r_dem_lon = np.round(dem_lon * 50)/50
            r_dem_lat = np.round(dem_lat * 200)/200
            f = open(os.path.join(dem_dir,'FRIS_DEM.xyz'),'w')
            keys = []

            for row in range(dem_lon.shape[0]):
                for col in range(dem_lon.shape[1]):
                    lon = r_dem_lon[row, col]

                    lat = r_dem_lat[row, col]
                    z = dem[row,col]

                    if lon>-95 and lon<-30 and lat>-85 and lat<-72 and not np.isnan(z):
                        f.write(str(lon)+' '+str(lat)+' '+str(z)+'\n')
                        keys.append((lon,lat))

            f.close()

        return 0

if __name__=='__main__':
    ant = Ant_data()
    #ant.get_veloData_v1()
    ant.get_veloData_v2()
    #ant.get_CryoSat_DEM()