from typing import List

def get_names_of_methods_in_object(obj):
    return [method_name for method_name in dir(obj) if callable(getattr(obj, method_name))]

def call_object_method_without_parameters(obj, method_name: str):
    method = getattr(obj, method_name)
    if not callable(method):
        raise Exception(f'{method} is not callable.')
    else:
        method()

def call_all_class_initialization_methods_of_object(obj):
    initialization_method_names = [method_name for method_name in get_names_of_methods_in_object(obj) if
                                   method_name.startswith('initialize_class_')]
    for each_initialization_method_name in initialization_method_names:
        call_object_method_without_parameters(obj, each_initialization_method_name)

def get_str_string(object_name, property_dictionary, omit_if_zero=None, omit_if_is_none=None):
    omit_if_zero = omit_if_zero if omit_if_zero else []
    omit_if_is_none = omit_if_is_none if omit_if_is_none else []
    result_string = object_name + '('
    the_list = []
    for property_name in property_dictionary:
        property_value = property_dictionary[property_name]
        should_omit = ((property_value == 0) and (property_name in omit_if_zero)) or \
                      ((property_value is None) and (property_name in omit_if_is_none))
        if not should_omit:
            the_list.append(f'{property_name}={repr(property_value)}')
    result_string += ', '.join(the_list)
    result_string += ')'
    return result_string

def get_strict_eq_boolean(this_object_class, this_object, other_object, property_names: List[str]):
    if not isinstance(other_object, this_object_class):
        return False
    for each_property_name in property_names:
        if getattr(this_object, each_property_name) != getattr(other_object, each_property_name):
            return False
    return True

def get_requirements_union(this_object_class, this_object, other_object, requirements_property_name):
    if not isinstance(other_object, this_object_class):
        raise TypeError(f'{other_object} does not inherit from the {this_object_class.__name__} class.')
    return dict(getattr(this_object, requirements_property_name), **getattr(other_object, requirements_property_name))

def operator_method(this_object, other_object, operator_symbol):
    if hasattr(other_object, 'latex_code'):
        return f'{this_object.latex_code} {operator_symbol} {other_object.latex_code}'
    else:
        return f'{this_object.latex_code} {operator_symbol} {other_object}'


import turtle

window = turtle.Screen()
turtle = turtle.Turtle()
turtle.hideturtle()
window.tracer(1000)

def triangle(a, order):
    if order > 0:
        for i in range(6):
            turtle.forward(a)
            turtle.right(60)
            triangle(a / 3, order - 1)
            turtle.left(120)
    else:
        for i in range(6):
            turtle.forward(a)
            turtle.left(60)

triangle(100, 5)
window.update()
window.update()
turtle.getscreen().getcanvas().postscript(file='ps_files/triangle_recursion_2.ps')
window.exitonclick()


"""BlogSite URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include, re_path

from django.views.generic.base import RedirectView

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('^favicon.ico$', RedirectView.as_view(url='/static/images/favicon.ico')),
    re_path(r'^tinymce/', include('tinymce.urls')), #引用富文本编辑器
    re_path(r'ueditor/', include('DjangoUeditor.urls')), #使用百度编辑器

    path('', include(('app.blog.urls', 'app.blog'), namespace='index')),
    path('user/', include(('app.user.urls', 'app.user'), namespace='user')),  # 用户模块
    path('blog/', include(('app.blog.urls', 'app.blog'), namespace='blog')),  # blogs
    # re_path(r'^captcha/', include('captcha.urls')),
    re_path(r'^search/', include('haystack.urls')), #全文检索
]


# dash imports
from dash import dcc, html
import plotly.express as px
import plotly.graph_objects as go
# from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc

# data wrangling imports
import pandas as pd
from numpy import nan, linspace
from datetime import datetime
from math import floor

# local imports
# used for callbacks
from app import app

# function definitions
def get_date_duration(df: pd.DataFrame, pre: str, post: str):
    """
    calculate total duration from earliest date to latest date

    :param df:
    :param pre:
    :param post:
    :return:
    """

    return (df[post] - df[pre]).dt.days

def add_today_line(fig: go.Figure, orientation: str) -> go.Figure:
    """
    add line denoting today's date

    :param fig:
    :param orientation:
    :return:
    """

    # todo: change 'today' to datetime.date.today()
    today = '2009-04-20'
    fig.update_layout(shapes=[
        dict(
          type='line',
          yref='paper', y0=0, y1=1,
          xref='x', x0=today, x1=today
        )
    ])
    return fig

def visit_count_cumsum(df: pd.DataFrame, visit: str) -> pd.DataFrame:
    """
    Calculate the cumulative visits that occurred by a given date

    :param df: dataframe with visit dates
    :type df: pd.Dataframe
    :param visit: visit name corresponding to column name in df
    :type visit: str
    :return: dataframe with column of cumulative summation for visit occurrences
    :rtype: pd.Dataframe
    """

    # sort the df based on the target visit column
    df = df.sort_values(by=visit)

    # assign a value of '1' for any start visit that had occurred
    # todo: do this for every visit
    mask = pd.isnull(df[visit])

    # inverting the mask as it flags 'NaN' as 'True'
    mask = mask.replace({True: 0, False: 1})
    df[f'{visit}_Count_Bool'] = mask

    # do a cumulative summation of events
    df[f'{visit}_Count'] = df[f'{visit}_Count_Bool'].cumsum()

    return df

def create_gantt(df: pd.DataFrame, x_start: str, x_end: str, y: str) -> go.Figure:
    """

    :param df:
    :param x_start:
    :param x_end:
    :param y:
    :return:
    """
    # make base gantt chart
    gantt = px.timeline(df,
                        x_start=x_start,
                        x_end=x_end,
                        y=y,
                        color_discrete_sequence=['black'],
                        hover_data={'Start': False, 'Finish': False, 'Task': False, 'Duration': True})

    # change bar width
    # change tooltip for bar
    gantt.update_traces(
        width=0.025,
        hovertemplate="Duration: %{text} days",
        text=df['Duration']
    )

    # put gantt chart into figure object which will be used for adding traces
    fig = go.Figure(data=gantt.data, layout=gantt.layout)

    return fig

def add_visit_trace_gantt(fig: go.Figure, df: pd.DataFrame, x_name: str, y_name: str, trace_name: str = None,
                          symbol_icon: str = 'circle', symbol_color: str = 'black', symbol_size: int = 10) -> go.Figure:
    # marker documentation
    # https://plotly.com/python-api-reference/generated/plotly.graph_objects.scatter.html#plotly.graph_objects.scatter.Marker

    # note: <extra></extra> tag removes the part of the hover where the trace name is usually displayed in a contrasting
    # color
    # source: https://plotly.com/python/hover-text-and-formatting/

    if trace_name is None:
        trace_name = x_name

    fig.add_trace(
        go.Scatter(
            x=df[x_name],
            y=df[y_name],
            marker={
                'symbol': symbol_icon,
                'size': symbol_size,
                'color': symbol_color
            },
            mode='markers',
            hovertemplate="{trace_name}: %{{x}}<extra></extra>".format(trace_name=trace_name, x=''),
            name=trace_name
        )
    )
    return fig

def add_visit_trace_identity(fig: go.Figure, df: pd.DataFrame, visit: str, trace_name: str = None,
                             color: str = None) -> tuple:
    # calculate cumulative sum of visit as a fcn of date
    df = visit_count_cumsum(df, visit)

    # add plot of visit occurrences
    identity_plot.add_trace(
        go.Scatter(
            x=df[visit],
            y=df[f'{visit}_Count'],
            mode='lines',
            line={
                'shape': 'hv'
            },
            name=visit
        )
    )

    return df, identity_plot

def create_visit_symbol_map() -> tuple:
    # map visits to symbol icons and colors
    visits = ['Start', 'Finish', 'Midpoint', 'Early']
    symbol_icons = ['circle', 'star-square', 'triangle-up', 'hexagram']
    symbol_colors = ['black', 'darkviolet', 'pink', 'lightslategrey']

    return visits, symbol_icons, symbol_colors

# import data into df
df = pd.DataFrame([
    dict(Task="Job A", Start='2009-01-01', Finish='2009-02-28', Midpoint="2009-01-31", Early="2009-01-15"),
    dict(Task="Job B", Start='2009-03-05', Finish='2009-04-15', Midpoint="2009-03-31", Early="2009-03-06"),
    dict(Task="Job C", Start='2009-02-20', Finish='2009-05-30', Midpoint="2009-04-20", Early=nan)
])

# convert date columns to datetime
for col in df.columns:
    if col == "Task":
        continue
    df[col] = pd.to_datetime(df[col])

df['Duration'] = get_date_duration(df, 'Start', 'Finish')

gantt = create_gantt(df, 'Start', 'Finish', 'Task')

visits, symbol_icons, symbol_colors = create_visit_symbol_map()

# add traces to gantt plot
for idx, visit in enumerate(visits):
    gantt = add_visit_trace_gantt(gantt, df, visit, 'Task', symbol_icon=symbol_icons[idx],
                                  symbol_color=symbol_colors[idx])

# list tasks from top --> bottom
# show y grid lines
gantt.update_yaxes(
    autorange="reversed",
    showgrid=True
)

# hide x grid line
gantt.update_xaxes(
    showgrid=False
)

# add title, axis titles, legend title
gantt.update_layout(
    title={
        'text': 'Suject Enrollment Timeline',
        'x': 0.5,
        'y': 0.95,
        'xanchor': 'center',
        'yanchor': 'top'
      },
    xaxis_title={
        'text': 'Date',
        # 'font': '',
        # 'standoff': ''
            },
    yaxis_title={
        'text': 'Task',
        # 'font': '',
        # 'standoff': ''
            },
    legend={
        'title': {
            'text': 'Visits'
        }
    },
    showlegend=True
)

# gantt.update_layout(hovermode='x unified')

# make predicted vs. real enrollment graph

# calculate the duration of the trial and determine the days per new subject enrollment
# todo: set trial start and end dates
start = pd.Timestamp('2009-01-01')
end = pd.Timestamp('2009-12-31')
delta = (end - start).days
sample_size = 55
days_between_new_subject_enrollment = floor(delta / sample_size)
day_array = linspace(start.value, end.value, floor(delta/days_between_new_subject_enrollment) + 1)
day_array = pd.to_datetime(day_array).date

# make identity plot
identity_plot = go.Figure(
    data=go.Scatter(
        x=day_array,
        y=[n for n in range(1, len(day_array) + 1)],
        mode='lines+markers',
        line={
            'color': 'black',
            'shape': 'hv'
        },
        marker={
            'color': 'black',
            'size': 1.0
        }
        ,
        name='Predicted Progress'
    )
)

identity_plot.update_layout(
    title={
        'text': 'Visit Progress Timeline',
        'x': 0.5,
        'y': 0.95,
        'xanchor': 'center',
        'yanchor': 'top'
    },
    xaxis_title={
        'text': 'Date',
        # 'font': '',
        # 'standoff': ''
    },
    yaxis_title={
        'text': 'Number of Subjects',
        # 'font': '',
        # 'standoff': ''
    },
    legend={
        'title': {
            'text': 'Visits'
        }
    },
    showlegend=True
)

identity_plot.update_xaxes(
    showgrid=False,
    showspikes=True
    )

identity_plot.update_yaxes(
    showgrid=False,
    showspikes=True
    )

# add traces for each visit
for visit in visits:
    df, identity_plot = add_visit_trace_identity(identity_plot, df, visit)

# add a line to denote today's date
identity_plot = add_today_line(identity_plot, 'vertical')

# layout dash page
layout = html.Div(
    [
        dbc.Row(
            [
                dbc.Col(
                    dcc.Graph(
                        id='visit-progress-timeline',
                        figure=identity_plot
                    )
                )
            ]
        ),
        dbc.Row(
            [
                dbc.Col(
                    dcc.Graph(
                        id='subject-enrollment-timeline',
                        figure=gantt
                    )
                )
            ]
        )
    ]
)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pyflann
import numpy as np

def rand_vecs(num, dim, rng=np.random, dtype=np.uint8):
    return (rng.rand(num, dim) * 255).astype(dtype)

def run_benchmark():
    import ubelt as ub

    data_dim = 128
    num_dpts = 1000000
    num_qpts = 25000
    num_neighbs = 5
    random_seed = 42
    rng = np.random.RandomState(0)

    dataset = rand_vecs(num_dpts, data_dim, rng)
    testset = rand_vecs(num_qpts, data_dim, rng)
    # Build determenistic flann object
    flann = pyflann.FLANN()
    print('building datset for %d vecs' % (len(dataset)))

    with ub.Timer(label='building kdtrees', verbose=True) as t:
        params = flann.build_index(
            dataset, algorithm='kdtree', trees=6, random_seed=random_seed, cores=6,
        )

    print(params)

    qvec_chunks = list(ub.chunks(testset, 1000))
    times = []
    for qvecs in ub.ProgIter(qvec_chunks, label='find nn'):
        with ub.Timer(verbose=0) as t:
            _ = flann.nn_index(testset, num_neighbs)  # NOQA
        times.append(t.ellapsed)
    print(np.mean(times))

if __name__ == '__main__':
    run_benchmark()


import random
import pygame
from entity import Entity
from constants import LEFT_TO_RIGHT, WIN_WIDTH
from screen import get_r_in_pixels

class Tree(Entity):
    def __init__(self, fish_image_path, viewable_min_r, viewable_max_r):
        Entity.__init__(self)
        self.fish_image_path = fish_image_path
        self.regenerate(viewable_min_r, viewable_max_r)
        self.fish_direction = LEFT_TO_RIGHT
        self.fish_num = 1
        self.fish_image_filename = ""
        self.y = 0

    def update(self, viewable_min_r, viewable_max_r):
        #if it's onscreen then update screen position
        if Entity.is_onscreen(self, viewable_min_r, viewable_max_r):
            temp = viewable_max_r - self.r_y
            self.y = get_r_in_pixels(temp)
            if self.fish_direction == LEFT_TO_RIGHT:
                self.x += self.fish_speed
            else:
                self.x -= self.fish_speed
        else:
            self.regenerate(viewable_min_r, viewable_max_r)

        return self.r_y

    def regenerate(self, viewable_min_r, viewable_max_r):
        self.fish_num = random.randint(1, 5)
        self.fish_image_filename = self.fish_image_path + "/fish" + str(self.fish_num) + "_small.png"
        self.image = pygame.image.load(self.fish_image_filename).convert_alpha()

        #the scaled images don't look very good
        #old_width = self.image.get_rect().width
        #new_width = int(old_width * 0.8)
        #old_height = self.image.get_rect().width
        #new_height = int(old_height * 0.8)
        #self.image = pygame.transform.scale(self.image, (new_width, new_height))

        self.r_y = random.randint(viewable_min_r, viewable_max_r)
        self.fish_direction = random.randint(1, 2)
        self.fish_speed = random.randint(5, 15)

        if self.fish_direction == LEFT_TO_RIGHT:
            self.x = 0 - self.image.get_width()
        else:
            self.x = WIN_WIDTH + self.image.get_width()
            #flip the image if fish is swimming right to left
            self.image = pygame.transform.flip(self.image, True, False)

    def blit(self, screen, viewable_max_r):
        temp = viewable_max_r - self.r_y
        self.y = get_r_in_pixels(temp)
        screen.blit(self.image, (self.x - self.image.get_width(), self.y))


from __future__ import division
import pandas as pd
import numpy as np
import template
import pdb
from sklearn.linear_model import LogisticRegression
from sklearn.feature_selection import RFE
import sklearn.metrics as metrics
from sklearn.cross_validation import KFold
import scipy as sp
import matplotlib.pyplot as plt

def RFE_CrossValidate(X, y, folds):
    ranks = None

    kfold = KFold(len(y), n_folds = folds)
    logreg = LogisticRegression()

    for tr, te in kfold:
        rfe = RFE(estimator=logreg, step=1, n_features_to_select=10)
        rfe.fit(X.iloc[tr], y.iloc[tr])
        if (ranks == None):
            ranks = np.array(rfe.ranking_)
        else:
            ranks = np.row_stack((ranks,rfe.ranking_))
    plt.matshow(ranks)
    return kfold,ranks

def proficiency_debug(x, y):
    x = np.array(x[x.columns[0]])
    y = np.ravel(y)
    sum_mi = 0.0
    ent = 0.0
    x_value_list = np.unique(x)
    y_value_list = np.unique(y)
    Px = np.array([ len(x[x==xval])/float(len(x)) for xval in x_value_list ]) #P(x)
    Py = np.array([ len(y[y==yval])/float(len(y)) for yval in x_value_list ]) #P(y)
    for i in xrange(len(x_value_list)):
        if Px[i] ==0.:
            continue
        sy = y[x == x_value_list[i]]
        if len(sy)== 0:
            continue
        pxy = np.array([len(sy[sy==yval])/float(len(y))  for yval in y_value_list]) #p(x,y)
        t = pxy[Py>0.]/Py[Py>0.] /Px[i] # log(P(x,y)/( P(x)*P(y))
        sum_mi += sum(pxy[t>0]*np.log2( t[t>0]) ) # sum ( P(x,y)* log(P(x,y)/( P(x)*P(y)) )
        ent -= Px[i]*np.log2(Px[i])
    return sum_mi/ent

def proficiency(x, y):
    r = 0.0
    x_value_list = np.unique(x)
    x_oaa = np.zeros(x.shape[0])
    y_oaa = np.zeros(x.shape[0])
    for i in xrange(len(x_value_list)):
        x_oaa[x == x_value_list[i]] = 0
        x_oaa[x != x_value_list[i]] = 1
        y_oaa[y == x_value_list[i]] = 0
        y_oaa[y != x_value_list[i]] = 1
        r += prof_12(x_oaa,y_oaa)
    return r

def prof_12(x, y):
    x = np.ravel(x)
    I = metrics.mutual_info_score(x,y)
    H = sp.stats.entropy([ len(x[x==xval])/float(len(x)) for xval in np.unique(x) ])
    return I/H

def BRFSS_filename(yr):
    return "/home/charles/dev/BRFSS/data/unzipped/BRFSS{}.ASC".format(yr)

def BRFSS_data(yrs, features, label):
    for yr in yrs:
        filename = BRFSS_filename(yr)

        colspecs = template.colspec_map[yr]
        header = template.header_map[yr]
        badvals = template.badvals_map[yr]

        df = pd.read_fwf(filename, colspecs=colspecs, names=header)
        y,X = get_model_variables(df,features,label,badvals)
    return y,X

def get_model_variables(df,features,label,badvals):

    MIN_HEIGHT = 36  # inches
    MAX_HEIGHT = 100 # inches

    # Select only relevant features, labels
    allVars = list(set(features) | set(label))
    df = pd.DataFrame(df, columns=allVars)

    # Drop NAs
    for c in df.columns:
        if (sum(np.isnan(df[c]))/len(df) > .4):
            df.drop(c, axis=1, inplace=True)
            features.remove(c)
    if (len(df.dropna(how='any')) == 0):
        df = df.dropna(thresh=int(.75*len(df)))
    else:
        df = df.dropna(how='any')

    # Process special columns
    df = df[~df['HEIGHT3'].isin([7777])]
    convert = lambda x: (int(x/100) * 12) + (x - 100*int(x/100))
    ff = lambda x: ((x-9000)/2.54) if x > 9000 else convert(x)
    df['HEIGHT3'] = [ff(x)if ~np.isnan(x) else x for x in df['HEIGHT3']]
    df = df[df['HEIGHT3'] > MIN_HEIGHT]
    df = df[df['HEIGHT3'] < MAX_HEIGHT]

    # Delete bad values
    for el in badvals:
        if (el in df.keys()):
            df = df[~df[el].isin(badvals[el])]

    # Add derived columns
    df['OBESITY'] = df['WEIGHT2']/df['HEIGHT3']
    features.append('OBESITY')

    y = df[label]
    X = df[features]

    return y,X


#
# Deployed user-defined function with the trigger_next framework function
#

import sys, json
import requests
import time
import http.client

# Invoke next Lambda in the orchestration
def trigger_next(payload, next_comput):
    encoded_apikey = "ENCODED_API_KEY"
    url = "/api/v1/namespaces/NAMESPACE/actions/"
    headers = {
        'Accept': "application/json",
        'Content-type': "application/json",
        'Authorization': 'Basic %s' % encoded_apikey
    }
    conn = http.client.HTTPSConnection("openwhisk.eu-gb.bluemix.net")
    conn.request("POST", url + next_comput, json.dumps(payload), headers=headers)
    res = conn.getresponse()

def main(dict):
    time.sleep(1)

    payload = {'id': dict["id"], 'stage': dict["stage"], 'input': dict["input"]}
    trigger_next(payload, dict["next"])

    return payload


import peewee
from peewee import *
from models import *
# Setup and connect to the database
db = peewee.MySQLDatabase("the_giving_tree", host="localhost", user="root")
db.connect()

# Testing Queries
# dmw = User.get(User.email == 'dmwesterhoff@gmail.com')
# print dmw.first_name

# for account in dmw.account:
#    print account.user_id

# account = Account.get(Account.user == 'dmwesterhoff@gmail.com')
# print account.alias

# Renew tables
db.drop_tables([User,Organization,Address,Account,Event,Transaction])
db.create_tables([User,Organization,Address,Account,Event,Transaction])

# Sample Create Statements
dmwesterhoff = User.create(phone_number='15103665680',first_name='David',middle_name='Michael',last_name='Westerhoff',date_of_birth='03061992')

tco = User.create(phone_number='16504307593',first_name='Tim', middle_name='C', last_name='Co', date_of_birth='04161992')

cancer_awareness = Organization.create(name='Cancer Awareness Organization')

david_address = Address.create(line1='1663 Bellville Way',city='Sunnyvale',postal_code='94566',state='CA',country='USA')

tim_address = Address.create(line1='365 Sunfish Ct',city='Foster City',postal_code='94404',state='CA',country='USA')

test_account_1 = Account.create(user=1,debit_card='0000000000000000',expiry_date='201710',alias='Debit Card',address=1,cvc='222')

test_account_2 = Account.create(user=2,debit_card='9999999999999999',expiry_date='201801',alias='Credit Crd',address=2,cvc='111')

test_event = Event.create(name='Hooka Session', owner=1, time_begins='08-21-2015 06:10:00', time_ends='08-21-2015 09:15:00', latitude=00.00, longitude=00.00, address=1)

test_transaction = Transaction.create(to_user=1, from_user=2, event=1, amount=10000, timestamp='06-21-2015 06:25:00')


(lambda x: print(x))(2)

f = lambda x:lambda y: x & y

x = 1 << 5
t = f(x)
print(t(0))
print(t(32))

def f(x):
    def s(y):
        return x & y
    return s


import spacy

nlp = spacy.load("en_core_web_sm")

text = "Upcoming iPhone X release date leaked as Apple reveals pre-orders"

# テキストを処理
doc = ____

# 固有表現をイテレート
for ____ in ____.____:
    # 固有表現の文字列とラベルをプリント
    print(____.____, ____.____)

# iPhone Xのスパンを取得
iphone_x = ____

# スパンの文字列をプリント
print("Missing entity:", iphone_x.text)


## Numerical Analysis Assignment 3
## Date: 24 November 2019
## This file checks whether a selected step is acceptable or not.
## Authors: Parikshit Bajpai, Marcos Machado

import numpy as np
from copy import copy
from Functions import Function

def Test_Step(x_n,direction_vector,residual,Lambda,N_Function):
    x_test = copy(x_n)
    x_test = x_test + direction_vector                      # New approximation

    function_test = Function(x_test,Lambda,N_Function)

    residual_test = np.linalg.norm(function_test,2)         # Residual at new approximation.

    # If the residual is less than at previous iteration accept the new step else reject.
    if residual_test <= residual:
        accept = 1
    else:
        accept = 0

    return accept


""" This file contains tools for building an xml representation of a test
procedure, compliant with the sample scema contained at the root of this project.
An example xml scema that  output file can be found in the root directory of this
project.

These procedure files are intended to be delivered to a front-end user interface
so that the interface can generate the correct components for display to the user
and make the correct requests to step through the test procedure.
"""

from datetime import datetime
from uuid import uuid4
import xml.etree.ElementTree as ET
from xml.dom import minidom

from lxml import etree

from procedure_utils import verify_with_schema

class SubStep():
    """ A substep contains actions and checks to perform in the system """

    def __init__(self, etree_parent, etree_substep, substep_id):
        self._id = substep_id
        self._substep_xml = etree_substep
        self._step_id_xml = etree.SubElement(self._substep_xml, 'id')
        self._step_id_xml.text = substep_id

    def add_action(self, fn_name, *params):
        """ Add an action to the substep.

        Args:
            fn_name: a function name for the server to execute when performing the step
            params: the parameters that should be passed to the server function
        """
        action_xml = etree.SubElement(self._substep_xml, 'action')
        action_xml.text = f"perform:{fn_name}:{params}"

    def add_check(self, ref, *params, label):
        """ Add a check to the substep.

        Args:
            ref: the label of what is being checked (sensor, user, etc.)
            params: parameters to determine a successful check
            label: the label that is used to store the result of this check
        """
        action_xml = etree.SubElement(self._substep_xml, 'action')
        action_xml.text = f"check:{label}:{ref}:{params}"

class Step():
    """ A step contains a selection of pressure readings to display, validations
    to confirm that the step has passed and controls that should be accessible
    to the user while the step is being displayed.
    """

    def __init__(self, etree_parent, etree_step, step_id):
        self._id = step_id
        self._step_xml = etree_step
        self._step_id_xml = etree.SubElement(self._step_xml, 'id')
        self._step_id_xml.text = step_id
        self._step_readings = etree.SubElement(self._step_xml, 'readings')
        self._step_controls = etree.SubElement(self._step_xml, 'controls')
        self._step_procedure = etree.SubElement(self._step_xml, 'procedure')
        self._step_validations = etree.SubElement(self._step_xml, 'validations')
        self._setup = SubStep(self._step_xml, etree.SubElement(self._step_procedure, 'setup'), 'setup')

        self._substeps = {}

    def add_reading(self, label, sensor_id):
        """ Add an xml representation of a pressure reading that should be displayed
        during this test step. Readings consist of a label for display to the user
        and sensor_id of the corresponding sensor that should be used to reference
        the appropriate data stored by the server.

        Args:
            label: the label that should be displayed to the user
            sensor_id: the sensor id who's value should be displayed

        Ex.
        <reading>
          <label>Brake Line</label>
          <sensor_id>brake_line_pressure_sensor</sensor_id>
        </reading>
        """
        reading = etree.SubElement(self._step_readings, 'reading')

        label_xml = etree.SubElement(reading, 'label')
        label_xml.text = label

        sensor_id_xml = etree.SubElement(reading, 'sensor_id')
        sensor_id_xml.text = sensor_id

    def add_validation(self, validation_type, *params):
        """ Add a validation metric to determine whether the step has succeeded.
        These validations are intended to be performed by the server and correlated
        by label with a 'check' that is performed during this step.

        Logic has not yet been implemented for validation, meaning that the structure
        may be changed. This first-pass implementation requires a type of validation
        so that the server knows what kind of check it is performing, plus any
        parameters that can be used to perform the validation check.

        validation_type: the type of validation that is being performed - used
            by the server to correctly identify how the validation should be
            performed
        params: any parameters needed for this type of validation

        Ex.
        <validation>
          <type>value</type>
          <parameters>('pressure_1', 'number', 'greater_than', 110)</parameters>
        </validation>
        """
        validate_xml = etree.SubElement(self._step_validations, 'validation')

        type_xml = etree.SubElement(validate_xml, 'type')
        type_xml.text = validation_type

        parameters_xml = etree.SubElement(validate_xml, 'parameters')
        parameters_xml.text = f"{params}"

    def add_substep(self, substep_id):
        """ Substeps are intended to provide a procedural order of actions within
        a test step. Substeps do not contain validations, but provide a useful way
        to organize barriers to order the sequence of events during a step of testing.

        Args:
            substep_id: an id label applied to this label for reference and retrieval

        Ex.
        <sub_step>
          <id>0</id>
          <action>perform:set_flow:(2,)</action>
          <action>check:changed_to_lap:user_validate:('Change to Lap?', True)</action>
        </sub_step>
        <sub_step>
          <id>1</id>
          <action>perform:set_flow:(3,)</action>
          <action>perform:wait:(5,)</action>
          <action>check:soap_passed:user_validate:('Soap check passed?', True)</action>
        </sub_step>
        """
        substep = SubStep(self._step_xml, etree.SubElement(self._step_procedure, 'sub_step'), str(substep_id))
        self._substeps[substep_id] = substep

    def setup(self):
        """ The setup step is a special step that is designed to allow prompting
        the user or setup of initial conditions before the current test is enacted.
        """
        return self._setup

    def substep(self, substep_id):
        """ Get a substep with a specific label

        Args:
            substep_id: the id of the step to be retrieved
        """
        return self._substeps[substep_id]

class Procedure():
    """ The hghest level of the test procedure, containing metadata about the
    procedure, scema and author. Also contains the steps of the prcedure. """

    def __init__(self, schema, author, test_id=None):
        self._schema = schema

        self._xml_tree = etree.Element('test_procedure')
        etree.register_namespace('xs', 'Via_FYDP')

        created_date = etree.SubElement(self._xml_tree, 'created_date')
        today = datetime.now()
        created_date.text = today.strftime('%Y-%m-%d')

        author_xml = etree.SubElement(self._xml_tree, 'author')
        author_xml.text = author

        test_name = test_id or str(uuid4())
        test_id_xml = etree.SubElement(self._xml_tree, 'test_id')
        test_id_xml.text =test_name

        self._name = test_name
        self._test_steps_xml = etree.SubElement(self._xml_tree, 'steps')
        self._test_steps = {}

    @property
    def name(self):
        return self._name

    def add_step(self, step_num):
        """ Add a step to the Procedure

        Args:
            step_num: an integer step number to add
        """
        step = etree.SubElement(self._test_steps_xml, 'step')
        self._test_steps[step_num] = Step(self._test_steps_xml, step, str(step_num))

        self._test_steps_xml.insert(step_num, step)

    def get_step(self, step_num):
        """ Return a step with the provided label. """
        return self._test_steps[step_num]

    def verify(self):
        """ Verify that the xml structure stored in the procedure adheres to the xml schema """
        verify_with_schema(formatted_xml, self._schema)

    def write_and_verify(self, out_file):
        """ Verify that the xml structure stored in the procedure adheres to the xml schema
        and print to a file if it does

        Args:
            out_file: the output file that the xml document should be written to
        """

        # verify the programmatic representation of the xml
        formatted_xml = minidom.parseString(etree.tostring(self._xml_tree)).toprettyxml(indent="  ")
        print(formatted_xml)
        verify_with_schema(formatted_xml, self._schema)

        # output the xml to a file
        with open(out_file, 'w') as output_file:
            output_file.write(formatted_xml)

        # read in the xml file to ensure that no errors occur during printing or parsing
        with open(out_file, 'r') as read_file:
            read_xml = etree.parse(read_file)
            verify_with_schema(etree.tostring(read_xml), self._schema)


#!C:\Users\Lenovo\AppData\Local\Programs\Python\Python37\python.exe
print("Content-type:text/html\n")

#print("Content-type:image/png\n")
import pymysql as pms
conn = pms.connect("localhost","root","mysroot","finaln")
a=conn.cursor()
b=conn.cursor()
c=conn.cursor()
d=conn.cursor()

q1="select count(ayear1) from profile where ayear1=1";
a.execute(q1)
data1=a.fetchone()

q2="select count(ayear1) from profile where ayear1=2";
b.execute(q2)
data2=b.fetchone()

q3="select count(ayear1) from profile where ayear1=3";
c.execute(q3)
data3=c.fetchone()

q4="select count(ayear1) from profile where ayear1=4";
d.execute(q4)
data4=d.fetchone()

import os,sys
import cgi
import cgitb; cgitb.enable();
os.environ['HOME'] = '/tmp/'
import matplotlib as plt
import numpy as np
l1=list(data1)
l2=list(data2)
l3=list(data3)
l4=list(data4)

lf = l1+l2+l3+l4
perform = ['First Year','Second Year','Third Year','Fourth Year']

form = cgi.FieldStorage()
#x = np.array(data1)
#y = np.array(data2)

from matplotlib.pyplot import figure
import mpld3
fig = figure()
ax = fig.gca()
#ax.plot(ab,perform,type='bar')
ax.bar(perform,lf,tick_label =perform)

mpld3.show(fig)


import gym
import numpy as np
import tensorflow as tf
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--render", dest="render", type=int, default=1)
parser.add_argument("--mujoco_game", dest="mujoco_game", type=str, default="Humanoid-v2")
args = parser.parse_args()

env = gym.make(args.mujoco_game)  # environment info
xs, rs, ys = [], [], []  # State, Reward, Action History
average_reward = None  # Reward for measuring average performance
G = 0
n_episode = 0

print("GameName: ", args.mujoco_game,
      "Obs Space: ", env.observation_space, env.observation_space.shape,
      # env.observation_space.n,
      "Action space: ", env.action_space, env.observation_space.shape,
      # env.action_space.n,
      )

n_input = env.observation_space.shape[0]            # size of input
n_hidden = 256                                      # number of hidden layer neurons
n2_hidden = 128
n_actions = env.action_space.shape[0]               # ?? -1 number of available actions i.e. fire (do nothing)
learning_rate = 1e-3
gamma = .99                                         # discount factor for reward
decay1, decay2 = 0.9, 0.999                         # decay rates for Adam optimizer

game_name = str(args.mujoco_game.split("-")[0]).lower()

save_path = 'models/{game_name}/{game_name}.ckpt'.format(game_name=game_name)

tf_model = {}
# Initialize random weights using xavier initialization
with tf.variable_scope('weights_one', reuse=False):
    xavier_l1 = tf.truncated_normal_initializer(mean=0, stddev=1./np.sqrt(n_input), dtype=tf.float32)
    tf_model['W1'] = tf.get_variable("W1", [n_input, n_hidden], initializer=xavier_l1)
with tf.variable_scope('weights_two', reuse=False):
    xavier_l2 = tf.truncated_normal_initializer(mean=0, stddev=1./np.sqrt(n_hidden), dtype=tf.float32)
    tf_model['W2'] = tf.get_variable("W2", [n_hidden, n2_hidden], initializer=xavier_l2)
with tf.variable_scope('weights_three', reuse=False):
    xavier_l3 = tf.truncated_normal_initializer(mean=0, stddev=1./np.sqrt(n2_hidden), dtype=tf.float32)
    tf_model['W3'] = tf.get_variable("W3", [n2_hidden, n_actions], initializer=xavier_l3)

print("Model is: ", tf_model)

# Functions
# Apply reward discounting to a series of rewards
def tf_discount(tf_r):  # tf_r ~ [game_steps,1]
    discount_f = lambda a, v: a * gamma + v  # Function for calculating discounted reward
    tf_r_reverse = tf.scan(discount_f, tf.reverse(tf_r, [True, False]))  # Reverses and applies discounts
    tf_discounted_r = tf.reverse(tf_r_reverse, [True, False])  # Reverse back so in ascending time steps
    return tf_discounted_r

# Feed input and calculate action using weights
def tf_policy_forward(x):  # x ~ [1,D]
    h = tf.nn.relu(tf.matmul(x, tf_model['W1']))  # Calculates hidden layer
    h2 = tf.nn.relu(tf.matmul(h, tf_model['W2'])) # compute 2nd hidden layer
    logp = tf.matmul(h2, tf_model['W3'])  # Calculates output value
    p = tf.nn.softmax(logp)  # Converts out values into a probability i.e. sum of outputs = 1
    return p

# TF Placeholders
X = tf.placeholder(dtype=tf.float32, shape=[None, n_input], name="X")
Y = tf.placeholder(dtype=tf.float32, shape=[None, n_actions], name="Y")
tf_epr = tf.placeholder(dtype=tf.float32, shape=[None, 1], name="tf_epr")

# Discount rewards
tf_discounted_g = tf_discount(tf_epr)
# Normalize rewards
tf_mean, tf_variance = tf.nn.moments(tf_discounted_g, [0], shift=None, name="reward_moments")
tf_discounted_g -= tf_mean
tf_discounted_g /= tf.sqrt(tf_variance + 1e-6)

# TF optimizer op
tf_aprob = tf_policy_forward(X)
loss = tf.nn.l2_loss(Y - tf_aprob)
optimizer = tf.train.AdamOptimizer(learning_rate, beta1=decay1, beta2=decay2)
#optimizer = tf.train.RMSPropOptimizer(learning_rate, decay=decay1)
tf_grads = optimizer.compute_gradients(loss, var_list=tf.trainable_variables(), grad_loss=tf_discounted_g)
train_op = optimizer.apply_gradients(tf_grads)

# TF Graph initialization
sess = tf.Session()
sess.run(tf.global_variables_initializer())

# Load model if exists
saver = tf.train.Saver(tf.global_variables())
load_was_success = True
try:
    save_dir = '/'.join(save_path.split('/')[:-1])
    ckpt = tf.train.get_checkpoint_state(save_dir)
    load_path = ckpt.model_checkpoint_path
    saver.restore(sess, load_path)
except:
    print("no saved model to load. starting new session")
    load_was_success = False
else:
    print("loaded model: {}".format(load_path))
    saver = tf.train.Saver(tf.global_variables())
    episode_number = int(load_path.split('-')[-1])

state1 = np.zeros([1, n_input])
state2 = env.reset()
render = bool(args.render)

while True:
    # Decay learning rate
    if n_episode > 4000:
        learning_rate = 1e-4
    elif n_episode > 5000:
        learning_rate = 1e-5
    elif n_episode > 6000:
        learning_rate = 1e-6

    # Subtract current state from previous state to visualize motion
    state = state2 - state1
    state1 = state2

    # Sample a action based from the network weights
    feed = {X: np.reshape(state, (1, n_input))}
    # Feed state forward through the network, returns an action between 0 and 2
    aprob = sess.run(tf_aprob, feed)
    aprob = aprob[0, :]
    # Choose random action based upon action probabilities
    action = np.random.choice(n_actions, p=aprob)
    # Convert action into one hot vector
    label = np.zeros_like(aprob)
    label[action] = 1

    # Take an action and get new variables, we add 1 to action based upon env.env.get_action_meanings()
    state2, reward, done, info = env.step(action + 1)

    if render:
        env.render()
    G += reward

    # Record state, action & reward history
    xs.append(state)
    ys.append(label)
    rs.append(reward)

    if done:
        # parameter update
        feed = {X: np.vstack(xs), tf_epr: np.vstack(rs), Y: np.vstack(ys)}
        _ = sess.run(train_op, feed)

        # update average reward
        average_reward = G if average_reward is None else average_reward * 0.99 + G * 0.01

        # print progress console
        if n_episode % 10 == 0:
            print('Episode {}: Reward: {}    Average Reward: {:4.3f}'.format(n_episode, G, average_reward))
        else:
            print('Episode {}: Reward: {}'.format(n_episode, G))

        # bookkeeping

        G = 0  # Reset episode reward
        n_episode += 1  # the Next Episode
        state1 = np.zeros([1, n_input])
        state2 = env.reset()
        xs, rs, ys = [], [], []  # reset game history

        if n_episode % 50 == 0:
            saver.save(sess, save_path, global_step=n_episode)
            print("SAVED MODEL #{}".format(n_episode))

        if n_episode == 6000:
            break

while True:
    # Decay learning rate
    if n_episode > 4000:
        learning_rate = 1e-4
    elif n_episode > 5000:
        learning_rate = 1e-5
    elif n_episode > 6000:
        learning_rate = 1e-6

    # Subtract current state from previous state to visualize motion
    state = state2 - state1
    state1 = state2

    # Sample a action based from the network weights
    feed = {X: np.reshape(state, (1, n_input))}
    # Feed state forward through the network, returns an action between 0 and 2
    aprob = sess.run(tf_aprob, feed)
    aprob = aprob[0, :]
    # Choose random action based upon action probabilities
    action = np.random.choice(n_actions, p=aprob)
    # Convert action into one hot vector
    label = np.zeros_like(aprob)
    label[action] = 1

    # Take an action and get new variables, we add 1 to action based upon env.env.get_action_meanings()
    state2, reward, done, info = env.step(action + 1)

    if render:
        env.render()
    G += reward

    # Record state, action & reward history
    xs.append(state)
    ys.append(label)
    rs.append(reward)

    if done:
        # parameter update
        feed = {X: np.vstack(xs), tf_epr: np.vstack(rs), Y: np.vstack(ys)}
        _ = sess.run(train_op, feed)

        # update average reward
        average_reward = G if average_reward is None else average_reward * 0.99 + G * 0.01

        # print progress console
        if n_episode % 10 == 0:
            print('Episode {}: Reward: {}    Average Reward: {:4.3f}'.format(n_episode, G, average_reward))
        else:
            print('Episode {}: Reward: {}'.format(n_episode, G))

        # bookkeeping

        G = 0  # Reset episode reward
        n_episode += 1  # the Next Episode
        state1 = np.zeros([1, n_input])
        state2 = env.reset()
        xs, rs, ys = [], [], []  # reset game history

        if n_episode % 50 == 0:
            saver.save(sess, save_path, global_step=n_episode)
            print("SAVED MODEL #{}".format(n_episode))

        if n_episode == 6000:
            break


import fileinput

digits = ['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR', 'FIVE', 'SIX', 'SEVEN', 'EIGHT', 'NINE']

letters = set()
for d in digits:
    for c in d:
        letters.add(c)

def mult(s):
    m = {}
    for l in letters:
        m[l] = 0
    for c in s:
        m[c] += 1
    return m

dmult = {d: mult(digits[d]) for d in range(10)}

def has(m1, m2):
    return all([m1[l] >= m2[l] for l in letters])

def n(m, min=0):
    for i in range(min, 10):
        if has(m, dmult[i]):
            newm = {l: m[l] - dmult[i][l] for l in letters}
            if all([newm[l] == 0 for l in letters]):
                return str(i)
            rest = n(newm, i)
            if rest:
                return str(i) + rest
    return None

f = fileinput.input()
T = int(f.readline())
for t in range(1, T+1):
    s = f.readline().strip()
    m = mult(s)
    number = n(m)
    print('Case #{0}: {1}'.format(t, number))


"""Scripts module for SimpleMDMpy"""

from SimpleMDMpy.SimpleMDM import Connection, ApiError

class Scripts(Connection):
    """scripts module for SimpleMDMpy"""
    def __init__(self, api_key):
        Connection.__init__(self, api_key)
        self.url = self._url("/scripts")

    def get_script(self, script_id="all"):
        """
        Returns a listing of all scripts in the account, or the script
        specified by id.

        Args:
            script_id (int, optional):  Returns a dictionary of the specified
                script id. By default, it returns a list of all scripts.

        Returns:
            dict: A single dictionary object with script information.
            array: An array of dictionary objects with script information.
        """
        url = self.url
        if script_id != 'all':
            url = url + "/" + str(script_id)
        return self._get_data(url)

    def create_script(self, name, variable_support, content):
        """
        You can use this method to upload a new script to your account.

        Args:
            name (str): The name for the script. This is how it will appear
                in the Admin UI.
            variable_support (bool): Whether or not to enable variable support
                in this script.
            content (str): The script content. All scripts must begin with a
                valid shebang such as #!/bin/sh to be processed.
        """
        params = {
            'name': name,
            'variable_support': "1" if variable_support else "0",
        }
        files = {
            'file': ('script.sh', content)
        }
        resp = self._post_data(self.url, params, files)
        if not 200 <= resp.status_code <= 207:
            raise ApiError(f"Script creation failed with status code {resp.status_code}: {resp.content}")
        return resp.json()['data']

    def update_script(self, script_id, name=None, variable_support=None, content=None):
        """
        You can use this method to update an existing script in your account.
        Any existing Script Jobs will not be changed.
        """
        url = self.url + "/" + str(script_id)
        params = {}
        files = None
        if name is not None:
            params['name'] = name
        if variable_support is not None:
            params['variable_support'] = "1" if variable_support else "0"
        if content is not None:
            files = {
                'file': ('script.sh', content)
            }
        if not params and not files:
            raise ApiError(f"Missing updated variables.")
        resp = self._patch_data(url, params, files)
        if not 200 <= resp.status_code <= 207:
            raise ApiError(f"Script update failed with status code {resp.status_code}: {resp.content}")
        return resp.json()['data']

    def delete_script(self, script_id):
        """You can use this method to delete a script from your account. Any
        existing Script Jobs will not be changed."""
        url = self.url + "/" + str(script_id)
        return self._delete_data(url)


import pandas as pd
import sqlalchemy as sa
import urllib.parse

#Connect to SQL SERVER
params = urllib.parse.quote_plus("DRIVER={SQL Server Native Client 11.0};"
                                 "SERVER=DESKTOP-MAK81E6\SQLEXPRESS;"
                                 "DATABASE=NSEDATA;"
                                 "Trusted_Connection=yes")
dbEngine = sa.create_engine("mssql+pyodbc:///?odbc_connect={}".format(params))
conn = dbEngine.connect()

#stock_list = ['TATAMOTORS','MARUTI','ZEEL','BRITANNIA','SBIN','BAJAJFINSV','BAJFINANCE','HDFCBANK','ASIANPAINT','ICICIBANK','KOTAKBANK','RELIANCE','TITAN','HINDUNILVR','INDUSINDBK','ITC','NESTLEIND','INFY','TCS']
#stock_list = ['TATAMOTORS']

query = "SELECT NAME FROM dbo.STOCKS UNION SELECT NAME FROM dbo.STOCK_INDICES UNION SELECT NAME FROM dbo.STOCK_SECTORS"
stocks_data = conn.execute(query)
stocks = stocks_data.fetchall()

for stock in stocks:
    stock = stock[0]
    print(stock)
    # query = "SELECT * FROM dbo." + stock + " WHERE DATE >='2020-10-20 00:00:00.000' AND DATE <'2021-01-05 00:00:00.000' ORDER BY DATE ASC "
    # query = "SELECT * FROM dbo." + stock + " WHERE DATE >='2020-01-25 00:00:00.000' ORDER BY DATE ASC "
    query = "SELECT * FROM dbo." + stock + " ORDER BY DATE ASC "
    data = pd.read_sql_query(query, con=conn, parse_dates=True)
    #data['Date'] = pd.to_datetime(data['Date']).dt.strftime('%m/%d/%Y')
    data['Date'] = pd.to_datetime(data['Date'])
    data.set_index('Date',inplace=True)
    data.sort_index(inplace=True)

    logic = {"Open":'first',
             "High":'max',
             "Low":'min',
             'Close':'last',
             'Volume':'sum'
             }

    wk_data = data.resample('W').agg(logic)
    wk_data.index = wk_data.index - pd.DateOffset(days=6)
    wk_data.reset_index(inplace=True)
    wk_data['Wk_Cls_Chg'] = round(wk_data['Close'].pct_change()*100,2)
    wk_data['Bi_Wk_Cls_Chg'] = round(wk_data['Close'].pct_change(periods=2)*100,2)
    wk_data['High_52_Wk'] = wk_data['High'].rolling(52).max()
    wk_data['Low_52_Wk'] = wk_data['Low'].rolling(52).min()
    wk_data['Wk_EMA_20'] = round(wk_data['Close'].ewm(span=20).mean(),2)
    wk_data['ATH'] = wk_data['High'].expanding().max()
    wk_data['ATL'] = wk_data['Low'].expanding().min()
    wk_data['Max_Wk_Chg'] = wk_data['Wk_Cls_Chg'].expanding().max()
    wk_data['Max_Wk_Vol'] = wk_data['Volume'].expanding().max()
    wk_data['Max_Wk_Range'] = (wk_data['High'] - wk_data['Low']).expanding().max()
    print("Start Data Load for the stock : {}".format(stock))
    # Write data read from .csv to SQL Server table
    wk_data.to_sql(name=stock+'_W', con=conn, if_exists='replace', index=False)
    # stock.to_sql(name=stock + '_W', con=conn, if_exists='append', index=False)
    print("Data Load done for the stock : {}".format(stock))

print('Weekly Data Load successful for all the stocks')


from recipebook import Recipebook

def print_menu():
    print('1. 레시피 검색')
    print('2. 레시피 추가')
    print('3. 재료 검색')
    print('4. 레시피 모음')
    print('5. 종료')
    menu = input('>> 메뉴를 선택하세요: ')
    return menu

def main():
    recipebook_203 = Recipebook()
    while True:
        menu = print_menu()
        if menu == '1':
            # 레시피 검색
            recipebook_203.search_recipe()
        elif menu == '2':
            # 레시피 추가
            recipebook_203.add_recipe()
        elif menu == '3':
            # 재료 검색
            recipebook_203.search_whatin()
        elif menu == '4':
            # 레시피 모음
            recipebook_203.show_all_recipe()
        elif menu == '5':
            break
        else:
            print('다시 입력하세요.')

if __name__ == '__main__':
    main()


class Solution(object):
    def hammingDistance(self, x, y):
        """
        :type x: int
        :type y: int
        :rtype: int
        """
        hd=0
        xbin='{0:0032b}'.format(x)
        ybin='{0:0032b}'.format(y)
        for i in range(len(xbin)):
            if xbin[i]!=ybin[i]:
                hd+=1
        return hd


def cross_val_function(function,x,y,folds,*args,**kwargs):
	"""
	This is an implementation of K-folds cross validation. Which is used to gain an accurate performance measure
	for a certain model or estimator. This estimator or model should be in form of a function which returns a
	score. The cross_val_function creates the given number of folds and then saves the score in an array for each
	of these folds. For each of the folds the function creates a separate thread to make optimal use of the
	processor.

    Parameters
    ----------
    function : function
        Function that returns a score with type float or integer.
        NOTE: Parameters of function should be added to the cross_val_function's parameters
    x        : numpy array
        Numpy array that contains the predictor variables.
    y        : numpy array
        Numpy array that contains the target variable.
    folds    : integer
        Number of folds used in cross validation.
	*args    :
        Parameters of function
	*kwargs  :
        Parameters of function

    Returns
    -------
    cross_val_scores : array
        Array containing the cross validation scores of all folds.
    """
	import numpy as np
	from threading import Thread

	num_rows=x.shape[0]
	fold_size=round(num_rows/folds)
	cross_val_scores=[]
	threads=[]

	def calculate_and_append_score(train_x,train_y,test_x,test_y):
		score=function(train_x,train_y,test_x,test_y,*args,**kwargs)
		cross_val_scores.append(score)

	for i in range(0,num_rows,fold_size):
		endIndex=i+fold_size if i+fold_size<=num_rows else num_rows
		indices=range(i,endIndex)
		mask = np.ones(num_rows,dtype=bool)
		mask[indices] = False
		train_x=x[mask]
		train_y=y[mask]
		test_x=x[~mask]
		test_y=y[~mask]
		thread=Thread(target=calculate_and_append_score, args=(train_x,train_y,test_x,test_y))
		thread.start()
		threads.append(thread)

	for thread in threads:
		thread.join()

	return cross_val_scores


"""hug/documentation.py

Defines tools that automate the creation of documentation for an API build using the Hug Framework

Copyright (C) 2015  Timothy Edmund Crosley

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

"""
from collections import OrderedDict

import hug.types

def for_handler(handler, version=None, doc=None, base_url="", url=""):
    """Generates documentation for a single API handler function"""
    if doc is None:
        doc = OrderedDict()

    usage = handler.function.__doc__
    if usage:
        doc['usage'] = usage
    for example in handler.examples:
        example_text =  "{0}{1}{2}".format(base_url, '/v{0}'.format(version) if version else '', url)
        if isinstance(example, str):
            example_text += "?{0}".format(example)
        doc_examples = doc.setdefault('examples', [])
        if not example_text in doc_examples:
            doc_examples.append(example_text)
    doc['outputs'] = OrderedDict(format=handler.outputs.__doc__,
                                 content_type=handler.outputs.content_type)
    if getattr(handler, 'output_doc', ''):
        doc['outputs']['type'] = handler.output_doc

    parameters = [param for param in handler.parameters if not param in ('request', 'response', 'self')
                                                                    and not param.startswith('hug_')
                                                                    and not hasattr(param, 'directive')]

    if parameters:
        inputs = doc.setdefault('inputs', OrderedDict())
        types = handler.function.__annotations__
        for argument in parameters:
            kind = types.get(argument, hug.types.text)
            input_definition = inputs.setdefault(argument, OrderedDict())
            input_definition['type'] = kind if isinstance(kind, str) else kind.__doc__
            default = handler.defaults.get(argument, None)
            if default is not None:
                input_definition['default'] = default

    return doc

def for_module(module, base_url="", api_version=None, handler_documentation=for_handler):
    """Generates documentation based on a Hug API module, base_url, and api_version (if applicable)"""
    documentation = OrderedDict()
    overview = module.__doc__
    if overview:
        documentation['overview'] = overview

    documentation['versions'] = OrderedDict()
    versions = module.__hug__.versions
    for version in (api_version, ) if api_version else versions:
        documentation['versions'][version] = OrderedDict()

    for url, methods in module.__hug__.routes.items():
        for method, method_versions in methods.items():
            for version, handler in method_versions.items():
                if version == None:
                    applies_to = versions
                else:
                    applies_to = (version, )
                for version in applies_to:
                    if api_version and version != api_version:
                        continue

                    doc = documentation['versions'][version].setdefault(url, OrderedDict())
                    doc[method] = handler_documentation(handler, version, doc=doc.get(method, None), base_url=base_url,
                                                        url=url)

    if len(documentation['versions']) == 1:
        documentation.update(tuple(documentation['versions'].values())[0])
        documentation.pop('versions')
    else:
        documentation['versions'].pop(None, '')

    return documentation


from ase.lattice.spacegroup import crystal
from util import swap_axis

def blackP(a=3.3136, b=10.478, c=4.3763, v=8.0):

    bulk = crystal('P', [0, 0.10168, 0.08056],
                   spacegroup=64,
                   cellpar=[a,b,c,90,90,90])

    bulk = swap_axis(bulk, 1,2)

    del bulk[[0,3,5,6]]
    bulk.center(vacuum=v, axis=2)
    return bulk

def blackP_bilayer(a=3.3136, b=10.478, c=4.3763, v=8.0):
    ### BROKEN AT THE MOMENT
    ### NEED TO TRANSLATE through z so when vacuum is added layers are together
    bulk = crystal('P', [0, 0.10168, 0.08056],
                   spacegroup=64,
                   cellpar=[a,b,c,90,90,90])

    bulk = swap_axis(bulk, 1,2)
    #del bulk[[0,3,5,6]]
    bulk.center(vacuum=v, axis=2)
    return bulk

if __name__ == "__main__":
    from ase.visualize import view
    mono = blackP()
    bilayer = blackP_bilayer()
    view(mono)
    view(bilayer)


class Solution:
    def minimumTotal(self, triangle):
        """
        :type triangle: List[List[int]]
        :rtype: int
        """
        if not triangle: return
        i = len(triangle)-2
        prev = triangle[i+1]
        while i >= 0:
            curr = triangle[i]
            for j in range(len(prev)-1):
                curr[j] += min(prev[j], prev[j+1])
            prev = curr
            i -= 1
        return curr[0]

print(Solution().minimumTotal([[2],[3,4],[6,5,7],[4,1,8,3]]))


import datetime
import threading
from dataclasses import dataclass
from typing import Optional

from prowler.lib.logger import logger
from prowler.lib.scan_filters.scan_filters import is_resource_filtered
from prowler.providers.aws.aws_provider import generate_regional_clients

################### Codebuild
class Codebuild:
    def __init__(self, audit_info):
        self.service = "codebuild"
        self.session = audit_info.audit_session
        self.audited_account = audit_info.audited_account
        self.audit_resources = audit_info.audit_resources
        self.regional_clients = generate_regional_clients(self.service, audit_info)
        self.projects = []
        self.__threading_call__(self.__list_projects__)
        self.__list_builds_for_project__()

    def __get_session__(self):
        return self.session

    def __threading_call__(self, call):
        threads = []
        for regional_client in self.regional_clients.values():
            threads.append(threading.Thread(target=call, args=(regional_client,)))
        for t in threads:
            t.start()
        for t in threads:
            t.join()

    def __list_projects__(self, regional_client):
        logger.info("Codebuild - listing projects")
        try:
            list_projects_paginator = regional_client.get_paginator("list_projects")
            for page in list_projects_paginator.paginate():
                for project in page["projects"]:
                    if not self.audit_resources or (
                        is_resource_filtered(project, self.audit_resources)
                    ):
                        self.projects.append(
                            CodebuildProject(
                                name=project,
                                region=regional_client.region,
                                last_invoked_time=None,
                                buildspec=None,
                            )
                        )

        except Exception as error:
            logger.error(
                f"{regional_client.region} -- {error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}"
            )

    def __list_builds_for_project__(self):
        logger.info("Codebuild - listing builds from projects")
        try:
            for project in self.projects:
                for region, client in self.regional_clients.items():
                    if project.region == region:
                        ids = client.list_builds_for_project(projectName=project.name)
                        if "ids" in ids:
                            if len(ids["ids"]) > 0:
                                builds = client.batch_get_builds(ids=[ids["ids"][0]])
                                if "builds" in builds:
                                    if "endTime" in builds["builds"][0]:
                                        project.last_invoked_time = builds["builds"][0][
                                            "endTime"
                                        ]

                        projects = client.batch_get_projects(names=[project.name])[
                            "projects"
                        ][0]["source"]
                        if "buildspec" in projects:
                            project.buildspec = projects["buildspec"]

        except Exception as error:
            logger.error(
                f"{error.__class__.__name__}[{error.__traceback__.tb_lineno}]: {error}"
            )

@dataclass
class CodebuildProject:
    name: str
    region: str
    last_invoked_time: Optional[datetime.datetime]
    buildspec: Optional[str]


import unittest
import datetime as dt
import random
import pandas as pd
import os
from pathlib import Path
from library.data_storage.stored_reading import StoredReadings
import sqlite3

class TestStoredReadings(unittest.TestCase):

    def setUp(self):
        pass

    # this also tests get number of readings
    def test_add_readings(self):
        print('Starting test add reading')
        aSR = StoredReadings()

        # Create data to send

        for i in range(0, 3, 1):
            x = random.randint(0, 359)
            y = random.randint(0, 359)
            z = random.randint(0, 359)

            aSR.add_readings("46406064", "faketime", x, y, z )

        n = aSR.get_number_of_readings()
        print('n = {}'.format(n))
        self.assertTrue(n == 2)

    def test_add_readings_to_db(self):
        print("Starting ADD readings to DB test")
        os.chdir('C:\\users\\shane\\documents\\code\\flask-pi-iot')
        print('This is the current working directory {}'.format(os.getcwd()))
        aSR = StoredReadings()
        initial_number = aSR.get_number_of_readings_from_db()
        print('Initial_number {}'.format(initial_number))
        for i in range(0, 3):
            x = random.randint(0, 358)
            y = random.randint(0, 358)
            z = random.randint(0, 358)
            d = dt.datetime.now()
            sn = "46406064"
            aSR.add_readings_to_db(sn, d, x, y, z)

        ending_number = aSR.get_number_of_readings_from_db()
        print('Ending_number {}'.format(ending_number))
        self.assertTrue(ending_number - initial_number == 3)

    def test_get_first_reading(self):
        print('Starting  getfirst reading test')
        aSR = StoredReadings()
        serialNumber = [100, 200, 300]

        for i in range(0, 3):
            x = random.randint(0, 359)
            y = random.randint(0, 359)
            z = random.randint(0, 359)

            aSR.add_readings(serialNumber[i], "faketime", x, y, z)

        fr = aSR.get_first_reading()

        self.assertTrue(type(fr) == dict)
        self.assertTrue(len(fr) == 6)
        self.assertTrue(fr['serial_no'] == 100)

    def test_get_next_reading(self):
        print('Strarting next reading test')
        aSR = StoredReadings()
        serialNumber = [100, 200, 300]

        for i in range(0, 3):
            x = random.randint(0, 359)
            y = random.randint(0, 359)
            z = random.randint(0, 359)

            aSR.add_readings(serialNumber[i], "faketime", x, y, z)

        fr = aSR.get_next_reading()

        self.assertTrue(type(fr) == dict)
        self.assertTrue(len(fr) == 6)
        self.assertTrue(fr['serial_no'] == 200)

    def test_get_previous_reading(self):
        print('Strarting previous reading test')
        aSR = StoredReadings()
        serialNumber = [100, 200, 300]

        for i in range(0, 3):
            x = random.randint(0, 359)
            y = random.randint(0, 359)
            z = random.randint(0, 359)

            aSR.add_readings(serialNumber[i], "faketime", x, y, z)

        first_reading = aSR.get_first_reading()
        get_next_reading = aSR.get_next_reading()
        fr = aSR.get_previous_reading()

        self.assertTrue(type(fr) == dict)
        self.assertTrue(len(fr) == 6)
        self.assertTrue(fr['serial_no'] == 100)
        self.assertTrue(fr == first_reading)

    # Pull a selection of entries between certian time periods
    def test_time_selection(self,):
        aSR = StoredReadings()
        serialNumber = [100, 200, 300, 400, 500]
        time = [1, 2, 3, 4, 5]

        for i in range(0, 3):
            x = random.randint(0, 359)
            y = random.randint(0, 359)
            z = random.randint(0, 359)

            aSR.add_readings(serialNumber[i], time[i], x, y, z)

        fr = aSR.time_selection(2, 4)

        self.assertTrue(len(fr) == 12)
        self.assertTrue()

    def test_push_to_s3(self,):
        # setup a dataframe
        column_title = ['people', 'places', 'numbers']
        data = [('david', 'italy', 4), ('megan', 'germany', 5), ('Katie', 'spain', 9), ('shane', 'norway', 3)]
        df = pd.DataFrame(data, columns=column_title)
        aSR = StoredReadings()

        aSR.push_to_s3(df)

    def test_get_df_from_db_by_serial_no(self):

        print("Starting get DF from DB test")
        os.chdir('C:\\users\\shane\\documents\\code\\flask-pi-iot')
        print('This is the current working directory {}'.format(os.getcwd()))
        aSR = StoredReadings()

        for i in range(0, 3):
            x = random.randint(0, 358)
            y = random.randint(0, 358)
            z = random.randint(0, 358)
            d = dt.datetime.now()
            sn = "DFTEST00"
            aSR.add_readings_to_db(sn, d, x, y, z)

        df = aSR.get_df_from_db_by_serial_no("DFTEST00")
        print('This is the Dataframe returned from the database {}'.format(df))

        conn = sqlite3.connect('data\\readings.db')
        cur = conn.cursor()
        sql_string = "DELETE FROM readings WHERE serial_no = 'DFTEST00'"
        cur.execute(sql_string)
        conn.commit()
        conn.close()

        self.assertTrue(df.shape[0] == 3)

if __name__ == '__main__':
    print('Starting the unittest')
    unittest.main()


from tarjeta import *

def test_Saldo():
	#Probamos la función Saldo().
	tarj = TarjetaComun(1)
	tarjM = TarjetaMedioBoleto(2)
	assert tarj.Saldo() == tarj.saldo
	assert tarjM.Saldo() == tarjM.saldo
	assert tarj.saldo == 0
	assert tarjM.saldo == 0

def test_RecargaTarjeta1():
	#Probamos el return de la función RecargaTarjeta().
	tarj = TarjetaComun(1)
	tarjM = TarjetaMedioBoleto(2)
	assert tarj.RecargaTarjeta(70) == 70
	assert tarj.RecargaTarjeta (196) == 230
	assert tarj.RecargaTarjeta (368) == 460

def test_RecargaTarjeta2():
	#Probamos la función Saldo() luego de implementar la RecargaTarjeta().
	tarj = TarjetaComun(1)
	tarjM = TarjetaMedioBoleto(2)
	tarj.RecargaTarjeta(70)
	tarjM.RecargaTarjeta(35)
	assert tarj.saldo == 70
	assert tarjM.saldo == 35
	tarj.RecargaTarjeta(196)
	tarjM.RecargaTarjeta(368)
	assert tarj.saldo == 300
	assert tarjM.saldo == 495

def test_PagarBoletoSinSaldo():
	#Probamos la función  PagarBoleto() con tarjetas sin saldo.
	K1 = Colectivo (1,"Semtur","K")
	v122= Colectivo(2,"Semtur","122v")
	tarj = TarjetaComun(1)
	tarjM = TarjetaMedioBoleto(2)
	assert tarjM.PagarBoleto(v122,"06/07/2008 04:20") == False
	assert tarj.PagarBoleto(v122,"06/07/2008 04:20") == False

def test_PagarBoleto():
	#Probamos la función PagarBoleto() para tarjetas cargadas.
	K1 = Colectivo (1,"Semtur","K")
	v122= Colectivo(2,"Semtur","122v")
	tarj = TarjetaComun(1)
	tarjM = TarjetaMedioBoleto(2)
	tarj.RecargaTarjeta(5.75)
	tarjM.RecargaTarjeta(2.90)
	assert tarjM.PagarBoleto(v122,"06/07/2008 04:20") == False #El medio boleto no funciona de 0 a 6 hs.
	assert tarj.PagarBoleto(v122,"06/07/2008 04:20") == True
	assert tarjM.PagarBoleto(K1,"06/07/2008 06:20") == True
	assert tarjM.Saldo() == 0
	assert tarj.Saldo() == 0

def test_Tranbordo():
	#Probamos la implementación de los transbordos.
	K1 = Colectivo (1,"Semtur","K")
	v122= Colectivo(2,"Semtur","122v")
	tarj = TarjetaComun(1)
	tarjM = TarjetaMedioBoleto(2)
	#Cargamos la tarjeta normal con lo justo para un boleto normal y dos transbordos.
	tarj.RecargaTarjeta(5.75+1.90+1.90)
	#Cargamos la tarjeta de medio boleto con lo justo para un medio boleto y un medio transbordo.
	tarjM.RecargaTarjeta(2.90+0.96)
	#Pagamos 3 viajes con la tarjeta normal, con 20 mins de diferencia entre c/u, en distintos colectivos.
	assert tarj.PagarBoleto(v122,"06/07/2008 06:20") == True
	assert tarj.PagarBoleto(K1,"06/07/2008 06:40") == True
	assert tarj.PagarBoleto(v122,"06/07/2008 07:00") == False
	#Pagamos 2 viajes con la tarjeta de medio boleto, en las mismas condiciones que antes.
	assert tarjM.PagarBoleto(K1,"06/07/2008 06:20") == True
	assert tarjM.PagarBoleto(v122,"06/07/2008 06:40") == True
	assert tarjM.Saldo() == 0
	assert round(tarj.Saldo()) == round(1.90)

def test_ViajesRealizados():
	#Probamos si la función pagar boleto guarda correctamente los viajes realizados.
	K1 = Colectivo (1,"Semtur","K")
	v122= Colectivo(2,"Semtur","122v")
	tarj = TarjetaComun(1)
	tarjM = TarjetaMedioBoleto(2)
	#Cargamos las 2 tarjetas con lo justo para 4 viajes
	tarj.RecargaTarjeta(5.75*2+1.90*2)
	tarjM.RecargaTarjeta(2.90*2+0.96*2)
	tarj.PagarBoleto(v122,"06/07/2008 06:20")
	tarj.PagarBoleto(K1,"06/07/2008 06:40")
	tarj.PagarBoleto(v122,"06/07/2008 07:00")
	tarj.PagarBoleto(K1,"06/07/2008 7:20")
	tarjM.PagarBoleto(v122,"06/07/2008 06:20")
	tarjM.PagarBoleto(K1,"06/07/2008 06:40")
	tarjM.PagarBoleto(v122,"06/07/2008 07:00")
	tarjM.PagarBoleto(K1,"06/07/2008 7:20")
	assert round(tarj.Saldo()) == 0
	assert round(tarjM.Saldo()) == 0
	#Creamos listas independientes que tengan los mismos viajes.
	Lista=[x for x in range(0,6)]
	Lista[0]=Viaje(K1,"06/07/2008 7:20", 1.90)
	Lista[1]=Viaje(v122,"06/07/2008 7:00", 5.75)
	Lista[2]=Viaje(K1,"06/07/2008 6:40", 1.90)
	Lista[3]=Viaje(v122,"06/07/2008 6:20", 5.75)
	Lista[4]=Viaje()
	Lista[5]=Viaje()
	ListaM=[y for y in range(0,6)]
	ListaM[0]=Viaje(K1,"06/07/2008 7:20", 0.96)
	ListaM[1]=Viaje(v122,"06/07/2008 7:00", 2.90)
	ListaM[2]=Viaje(K1,"06/07/2008 6:40", 0.96)
	ListaM[3]=Viaje(v122,"06/07/2008 6:20", 2.90)
	ListaM[4]=Viaje()
	ListaM[5]=Viaje()
	#Comparamos con el retorno de las funciones
	prueba = tarj.ViajesRealizados()
	pruebaM = tarjM.ViajesRealizados()
	for i in range(0,6):
		assert prueba[i].monto == Lista[i].monto
		assert prueba[i].colectivo == Lista[i].colectivo
		assert prueba[i].horario == Lista[i].horario
		assert pruebaM[i].monto == ListaM[i].monto
		assert pruebaM[i].colectivo == ListaM[i].colectivo
		assert pruebaM[i].horario == ListaM[i].horario


import numpy as np
from prediction import predict_

def simple_gradient(x, y, theta):
    """Computes a gradient vector from three non-empty numpy.ndarray,
    without any for-loop. The three arrays must have compatible dimensions.
    Args:
    x: has to be an numpy.ndarray, a vector of dimension m * 1.
    y: has to be an numpy.ndarray, a vector of dimension m * 1.
    theta: has to be an numpy.ndarray, a 2 * 1 vector.
    Returns:
    The gradient as a numpy.ndarray, a vector of dimension 2 * 1.
    None if x, y, or theta are empty numpy.ndarray.
    None if x, y and theta do not have compatible dimensions.
    Raises:
    This function should not raise any Exception.
    """
    if (not isinstance(x, np.ndarray) or not isinstance(y, np.ndarray)
            or not isinstance(theta, np.ndarray) or theta.size != 2
            or x.size != y.size or x.ndim != 1 or y.ndim != 1
            or theta.ndim != 1):
        return None
    else:
        h = predict_(x, theta)
        j0 = (1 / y.size) * (h - y)
        j0 = abs(j0.sum())
        j1 = (1 / y.size) * ((h - y) * x)
        j1 = abs(j1.sum())
        return np.array((j0, j1))

if __name__ == '__main__':
    x = np.array([12.4956442, 21.5007972, 31.5527382, 48.9145838, 57.5088733])
    y = np.array([37.4013816, 36.1473236, 45.7655287, 46.6793434, 59.5585554])
    # Example 0:
    theta1 = np.array([2, 0.7])
    print(simple_gradient(x, y, theta1))
    # Example 1:
    theta2 = np.array([1, -0.4])
    print(simple_gradient(x, y, theta2))


# -*- coding: utf-8 -*-
# 项目: wensfood
# 文件: import_main_data
# 模块名称
# Copyright 2017 WENS <www.wens.com.cn>
# Created by HHL at 2017/4/30 08:14
import logging

from openerp.osv import osv, fields
import requests
import simplejson

_logger = logging.getLogger(__name__)

class ws_import_main_data(osv.osv_memory):
    _name = "ws_init.import_data_wizard"
    _description = u"导入主数据"

    _columns = {
        'url': fields.char(u'主数据地址')
    }

    _defaults = {
        'url': 'http://wfd-master.wens.com.cn'
    }

    def import_main_data(self, cr, uid, ids=None, context=None):
        for data in self.browse(cr, uid, ids, context):
            url = data.url
        self._import_product_category(cr, uid, 'ws.report.category', url)
        self._reset_product_report_category(cr, uid, url)

        if True:
            return

        self._clean_data(cr, uid, ids, context)
        url = None

        self._import_bank(cr, uid, url)
        self._import_product_category(cr, uid, 'ws.product.category', url)
        self._import_product_category(cr, uid, 'ws.pos.category', url)
        self._import_product_category(cr, uid, 'ws.report.category', url)
        self._import_product_category(cr, uid, 'ws.promotion.category', url)
        self._import_uom_category(cr, uid, url)
        self._import_uom(cr, uid, url)
        self._import_product(cr, uid, url)
        self._import_service_product_mapping(cr, uid, url)
        return True

    def _clean_data(self, cr, uid, model_ids, context=None):
        '''
        清除数据
        :param cr:
        :param uid:
        :param ids:
        :param context:
        :return:
        '''
        _logger.info('start clean data....')
        bank_model = self.pool.get('res.bank')
        ids = bank_model.search(cr, uid, [])
        bank_model.unline(cr, uid, ids)

        model = self.pool.get('ws.product.service.config')
        ids = model.search(cr, uid, [])
        model.unlink(cr, uid, ids)

        lot = self.pool.get('stock.production.lot')
        ids = lot.search(cr, uid, [])
        lot.unlink(cr, uid, ids)
        product_model = self.pool.get('product.product')
        ids = product_model.search(cr, uid, [])
        product_model.unlink(cr, uid, ids)

        product_template = self.pool.get('product.template')
        ids = product_template.search(cr, uid, [])
        product_template.unlink(cr, uid, ids)

        uom_model = self.pool.get('product.uom')
        categ_model = self.pool.get('product.uom.categ')
        ids = uom_model.search(cr, uid, [])
        uom_model.unlink(cr, uid, ids)

        ids = categ_model.search(cr, uid, [])
        categ_model.unlink(cr, uid, ids)

        product_categ_model = self.pool.get('product.category')
        pos_category_model = self.pool.get('pos.category')

        ids = product_categ_model.search(cr, uid, [])
        product_categ_model.unlink(cr, uid, ids)

        ids = pos_category_model.search(cr, uid, [])
        pos_category_model.unlink(cr, uid, ids)

        _logger.info('clean data end....')

    def _import_bank(self, cr, uid, url):
        _logger.info("start import bank...")
        result = requests.get('%s/api/master/bank/list' % url).content
        bank_list = simplejson.loads(result)
        bank_model = self.pool.get('res.bank')
        for b in bank_list:
            bank_model.create(cr, uid, {
                'bic': b['bic'],
                'name': b['name'],
                'outer_id': str(b['id'])
            })

    def _import_service_product_mapping(self, cr, uid, url):
        _logger.info('start import product_mapping')
        result = requests.get('%s/api/master/service/mapping' % url).content
        mapping_list = simplejson.loads(result)
        product_model = self.pool.get('product.product')
        model = self.pool.get('ws.product.service.config')
        for m in mapping_list:
            product_id = product_model.search(cr, uid, [('outer_id', '=', str(m['product_id']))])[0]
            service_id = product_model.search(cr, uid, [('outer_id', '=', str(m['service_id']))])[0]
            old_ids = model.search(cr, uid, [('service_id', '=', service_id), ('is_split', '=', False)])
            if old_ids:
                continue
            val = {
                'product_id': product_id,
                'service_id': service_id,
                'rate': m['rate'],
                'is_split': m['is_split'],
                'outer_id': str(m['id'])
            }
            model.create(cr, uid, val)

    def _reset_product_report_category(self, cr, uid, url):
        _logger.info('start import product')
        result = requests.get('%s/api/master/product/list' % url).content
        product_list = simplejson.loads(result)
        report_category_model = self.pool.get('ws.report.category')
        product_model = self.pool.get('product.product')
        for p in product_list:
            if p['report_categ_id']:
                _logger.info('report categ id from master:%d' % p['report_categ_id'])
                report_categ_id = report_category_model.search(cr, uid, [('outer_id', '=', str(p['report_categ_id']))])[
                    0]
                product_ids = product_model.search(cr, uid, [('outer_id', '=', str(p['id']))])
                if report_categ_id and product_ids:
                    ids_str = [str(_id) for _id in product_ids]
                    product_model.write(cr, uid, product_ids, {'report_categ_id': report_categ_id})
                    _logger.debug('update product %s report category id to %d' % (ids_str, report_categ_id))
                else:
                    _logger.error('not found report_categ_id and product_ids')
            else:
                _logger.error('report categ id is null from master data')

    def _import_product(self, cr, uid, url):
        _logger.info('start import product')
        result = requests.get('%s/api/master/product/list' % url).content
        product_list = simplejson.loads(result)
        uom_model = self.pool.get('product.uom')
        product_categ_model = self.pool.get('product.category')
        pos_category_model = self.pool.get('pos.category')
        report_category_model = self.pool.get('ws.report.category')
        promotion_category_model = self.pool.get('ws.promotion.category')
        product_model = self.pool.get('product.product')
        for p in product_list:
            uom_id = 0
            categ_id = 0
            report_categ_id = 0
            pos_categ_id = 0
            promotion_categ_id = 0
            if p['uom_id']:
                uom_id = uom_model.search(cr, uid, [('outer_id', '=', str(p['uom_id']))])[0]
            if p['categ_id']:
                categ_id = product_categ_model.search(cr, uid, [('outer_id', '=', str(p['categ_id']))])[0]
            if p['report_categ_id']:
                report_categ_id = report_category_model.search(cr, uid, [('outer_id', '=', str(p['report_categ_id']))])[
                    0]
            if p['pos_categ_id']:
                pos_categ_id = pos_category_model.search(cr, uid, [('outer_id', '=', str(p['pos_categ_id']))])[0]
            if p['promotion_categ_id']:
                promotion_categ_id = \
                    promotion_category_model.search(cr, uid, [('outer_id', '=', str(p['promotion_categ_id']))])[0]
            val = {
                'company_id': None,
                'outer_id': str(p['id']),
                'default_code': p['code'] or '000',
                'ean13': p['ean13'],
                'name': p['name'],
                'full_name': p['full_name'],
                'image': p['image'],
                'uom_id': uom_id,
                'uom_po_id': uom_id,
                'cost_method': 'standard',
                'categ_id': categ_id,
                'report_categ_id': report_categ_id,
                'pos_categ_id': pos_categ_id,
                'promotion_categ_id': promotion_categ_id,
                'available_in_pos': p['is_pos'],
                'purchase_ok': p['is_purchase'],
                'sale_ok': p['is_sale'],
                'is_object_voucher': p['is_voucher'],
                'is_prepaid_card': p['is_prepaid_card'],
                'coupon': p['is_coupon'],
                'nature_sequence': p['nature_sequence'],
                'report_sequence': p['report_sequence'],
                'cost_method': p['cost_method'],
                'list_price': p['list_price'],
                'to_weight': p['to_weight'],
                'type': p['type'],
                # 'seller_ids': [(6, False, [1])]
            }
            _logger.info('create product:%s, id:%d' % (p['code'], p['id']))
            p_id = product_model.create(cr, uid, val)
            if p['type'] == 'product':
                tpl_id = product_model.browse(cr, uid, p_id).product_tmpl_id.id
                supplier_info_model = self.pool.get('product.supplierinfo')
                info_id = supplier_info_model.create(cr, uid, {
                    'name': 1,
                    'product_tmpl_id': tpl_id,
                    'company_id': None
                })

                partner_price_model = self.pool.get('pricelist.partnerinfo')
                partner_price_model.create(cr, uid, {
                    'name': '',
                    'suppinfo_id': info_id,
                    'min_quantity': 0,
                    'price': 0
                })

    def _import_uom_category(self, cr, uid, url):
        _logger.info('start import uom category')
        result = requests.get('%s/api/master/uom_categ/list' % url).content
        categ_list = simplejson.loads(result)
        categ_model = self.pool.get('product.uom.categ')
        for c in categ_list:
            val = {
                'outer_id': str(c['id']),
                'name': c['name']
            }
            categ_model.create(cr, uid, val)

    def _import_uom(self, cr, uid, url):
        _logger.info('start import uom')
        result = requests.get('%s/api/master/uom/list' % url).content
        uom_model = self.pool.get('product.uom')
        categ_model = self.pool.get('product.uom.categ')
        uom_list = simplejson.loads(result)
        for uom in uom_list:
            categ_id = categ_model.search(cr, uid, [('outer_id', '=', str(uom['categ_id']))])[0]
            val = {
                'name': uom['name'],
                'outer_id': str(uom['id']),
                'category_id': categ_id
            }
            uom_model.create(cr, uid, val)

    def _import_product_category(self, cr, uid, model, url):
        _logger.info('start import uom category')
        result = requests.get("%s/api/master/category/list?mode=%s" % (url, model)).content
        categ_list = simplejson.loads(result)
        product_categ_model = self.pool.get('product.category')
        pos_category_model = self.pool.get('pos.category')
        report_category_model = self.pool.get('ws.report.category')
        promotion_category_model = self.pool.get('ws.promotion.category')
        categ_model = None
        if model == 'ws.product.category':
            categ_model = product_categ_model
        elif model == 'ws.pos.category':
            categ_model = pos_category_model
        elif model == 'ws.report.category':
            categ_model = report_category_model
        elif model == 'ws.promotion.category':
            categ_model = promotion_category_model
        for item in categ_list:  # 创建数据
            val = {
                'name': item['name'],
                'parent_id': 0,
                'sequence': item['sequence'],
                'outer_id': str(item['id'])
            }
            old_ids = categ_model.search(cr, uid, [('outer_id', '=', str(item['id']))])
            if old_ids:
                continue

            categ_model.create(cr, uid, val)

        for item in categ_list:  # 确定层级关系
            if item['parent_id']:
                p_id = categ_model.search(cr, uid, [('outer_id', '=', str(item['parent_id']))])[0]
                child_id = categ_model.search(cr, uid, [('outer_id', '=', str(item['id']))])[0]
                categ_model.write(cr, uid, child_id, {'parent_id': p_id})


from django.contrib.admin import RelatedOnlyFieldListFilter
from django.contrib.gis import admin

from dora.core.admin import EnumAdmin

from .models import (
    AccessCondition,
    BeneficiaryAccessMode,
    CoachOrientationMode,
    ConcernedPublic,
    Credential,
    LocationKind,
    Requirement,
    Service,
    ServiceCategory,
    ServiceKind,
    ServiceModel,
    ServiceModificationHistoryItem,
    ServiceStatusHistoryItem,
    ServiceSubCategory,
)

class ServiceModificationHistoryItemInline(admin.TabularInline):
    model = ServiceModificationHistoryItem
    readonly_fields = ["user", "date", "fields", "service", "status"]
    extra = 0

    def has_add_permission(self, request, obj):
        return False

    def has_change_permission(self, request, obj):
        return False

class ServiceModificationHistoryItemAdmin(admin.ModelAdmin):
    list_display = ("service", "date", "user", "status")
    date_hierarchy = "date"
    list_filter = ("status",)
    readonly_fields = ["user", "date", "fields", "service", "status"]

    def has_add_permission(self, request, obj=None):
        return False

    def has_change_permission(self, request, obj=None):
        return False

class ServiceStatusHistoryItemInline(admin.TabularInline):
    model = ServiceStatusHistoryItem
    readonly_fields = ["user", "date", "new_status", "previous_status", "service"]
    extra = 0

    def has_add_permission(self, request, obj):
        return False

    def has_change_permission(self, request, obj):
        return False

class ServiceStatusHistoryItemAdmin(admin.ModelAdmin):
    list_display = ("service", "date", "user", "new_status", "previous_status")
    date_hierarchy = "date"
    readonly_fields = ["user", "date", "new_status", "previous_status", "service"]

    def has_add_permission(self, request, obj=None):
        return False

    def has_change_permission(self, request, obj=None):
        return False

class ServiceAdmin(admin.GISModelAdmin):
    search_fields = ("name", "structure__name", "slug", "data_inclusion_id")
    list_display = [
        "name",
        "slug",
        "structure",
        "creation_date",
        "modification_date",
        "publication_date",
        "last_editor",
        "status",
    ]
    list_filter = [
        "status",
        "moderation_status",
        "source",
        "use_inclusion_numerique_scheme",
    ]
    filter_horizontal = [
        "categories",
        "subcategories",
        "access_conditions",
        "concerned_public",
        "requirements",
        "credentials",
    ]
    inlines = [ServiceStatusHistoryItemInline, ServiceModificationHistoryItemInline]
    ordering = ["-modification_date"]
    save_as = True
    readonly_fields = (
        "creation_date",
        "status",
        "data_inclusion_id",
        "data_inclusion_source",
    )
    raw_id_fields = ["structure", "model", "creator", "last_editor"]

class ServiceModelAdmin(admin.ModelAdmin):
    search_fields = (
        "name",
        "structure__name",
        "slug",
    )
    list_display = [
        "name",
        "slug",
        "structure",
        "creation_date",
        "modification_date",
        "publication_date",
        "last_editor",
    ]
    list_filter = [
        ("structure", RelatedOnlyFieldListFilter),
    ]
    filter_horizontal = [
        "categories",
        "subcategories",
        "access_conditions",
        "concerned_public",
        "requirements",
        "credentials",
    ]
    inlines = [ServiceModificationHistoryItemInline]
    ordering = ["-modification_date"]
    save_as = True
    readonly_fields = ("creation_date", "modification_date", "status")
    raw_id_fields = ["structure", "model", "creator", "last_editor"]

class CustomizableChoiceAdmin(admin.ModelAdmin):
    list_display = ("name", "structure")
    list_filter = [
        ("structure", RelatedOnlyFieldListFilter),
    ]
    search_fields = (
        "name",
        "structure__name",
    )
    list_per_page = 1000
    raw_id_fields = ["structure"]

class ServiceModelInline(admin.TabularInline):
    model = ServiceModel
    show_change_link = True
    fields = [
        "slug",
        "name",
        "structure",
    ]
    readonly_fields = [
        "slug",
        "name",
        "structure",
    ]
    extra = 0

admin.site.register(Service, ServiceAdmin)
admin.site.register(ServiceModel, ServiceModelAdmin)
admin.site.register(AccessCondition, CustomizableChoiceAdmin)
admin.site.register(ConcernedPublic, CustomizableChoiceAdmin)
admin.site.register(Requirement, CustomizableChoiceAdmin)
admin.site.register(Credential, CustomizableChoiceAdmin)
admin.site.register(ServiceModificationHistoryItem, ServiceModificationHistoryItemAdmin)
admin.site.register(ServiceStatusHistoryItem, ServiceStatusHistoryItemAdmin)

admin.site.register(BeneficiaryAccessMode, EnumAdmin)
admin.site.register(CoachOrientationMode, EnumAdmin)
admin.site.register(LocationKind, EnumAdmin)
admin.site.register(ServiceCategory, EnumAdmin)
admin.site.register(ServiceKind, EnumAdmin)
admin.site.register(ServiceSubCategory, EnumAdmin)


import ray
import ray.rllib.agents.dqn as dqn
import ray.rllib.agents.pg as pg
import ray.rllib.agents.ppo as ppo
import numpy as np
#from Data_Generator.env_toy import env_toy
from Data_Generator.env_test import env_test
from ray.tune.logger import pretty_print
from ray.rllib.models import ModelCatalog
from ray.rllib.models.preprocessors import Preprocessor
from tf_model import Dense
import os
import sys

env_config = {}
env_config["preprocess"] = "DNN"
env_config["config_path"] = "Data_Generator/Config/config.random.dat"

GPU_ID = sys.argv[1]
max_step  = int(sys.argv[2])
episode_task_num = int(sys.argv[3])
#max_task_size   5   #int
participant_num = int(sys.argv[4])
#max_par_size    10  #int

env_config["max_step"] = max_step
env_config["episode_task_num"] = episode_task_num
env_config["max_task_size"] = episode_task_num
env_config["participant_num"] = participant_num
env_config["max_par_size"] = participant_num

ray.init()
DEFAULT_CONFIG = {
    # If true, use the Generalized Advantage Estimator (GAE)
    # with a value function, see https://arxiv.org/pdf/1506.02438.pdf.
    "use_gae": True,
    # The GAE(lambda) parameter.
    "lambda": 1.0,
    # Initial coefficient for KL divergence.
    "kl_coeff": 0.2,
    # Size of batches collected from each worker.
    "sample_batch_size": 200,
    # Number of timesteps collected for each SGD round. This defines the size
    # of each SGD epoch.
    "train_batch_size": 4000,
    # Total SGD batch size across all devices for SGD. This defines the
    # minibatch size within each epoch.
    "sgd_minibatch_size": 128,
    # Whether to shuffle sequences in the batch when training (recommended).
    "shuffle_sequences": True,
    # Number of SGD iterations in each outer loop (i.e., number of epochs to
    # execute per train batch).

    "num_sgd_iter": 30,

    # Stepsize of SGD.
    "lr": 5e-5,
    # Learning rate schedule.
    "lr_schedule": None,
    # Share layers for value function. If you set this to True, it's important
    # to tune vf_loss_coeff.
    "vf_share_layers": False,
    # Coefficient of the value function loss. IMPORTANT: you must tune this if
    # you set vf_share_layers: True.
    "vf_loss_coeff": 1.0,
    # Coefficient of the entropy regularizer.
    "entropy_coeff": 0.0,
    # Decay schedule for the entropy regularizer.
    "entropy_coeff_schedule": None,
    # PPO clip parameter.
    "clip_param": 0.3,
    # Clip param for the value function. Note that this is sensitive to the
    # scale of the rewards. If your expected V is large, increase this.
    "vf_clip_param": 10.0,
    # If specified, clip the global norm of gradients by this amount.
    "grad_clip": None,
    # Target value for KL divergence.
    "kl_target": 0.01,
    # Whether to rollout "complete_episodes" or "truncate_episodes".
    "batch_mode": "truncate_episodes",
    # Which observation filter to apply to the observation.
    "observation_filter": "NoFilter",
    # Uses the sync samples optimizer instead of the multi-gpu one. This is
    # usually slower, but you might want to try it if you run into issues with
    # the default optimizer.
    "simple_optimizer": False,
}
config = pg.DEFAULT_CONFIG.copy()
#config = dqn.DEFAULT_CONFIG.copy()
#config = ppo.DEFAULT_CONFIG.copy()
os.environ["CUDA_VISIBLE_DEVICES"] = GPU_ID #'3'
config["ignore_worker_failures"] = True
#config["sgd_minibatch_size"] = episode_task_num
config["train_batch_size"] = 4000
config["lr"] = 5e-4
config["num_gpus"] = 1
config["num_workers"] = 1
config["eager"] = False
config["env_config"] = env_config

# class OneHotPreprocessor(Preprocessor):
#     def _init_shape(self, obs_space, options):
#         return (self._obs_space.n, )

#     def transform(self, observation):
#         self.check_shape(observation)
#         arr = np.zeros(self._obs_space.n)
#         arr[observation] = 1
#         return arr

#     def write(self, observation, array, offset):
#         array[offset + observation] = 1

# # Can optionally call trainer.restore(path) to load a checkpoint.
# class MyPreprocessor(Preprocessor):
#     def _init_shape(self, obs_space, options):
#         return (4, 4, 1)

#     def transform(self, observation):
#         arr = np.zeros(16, )
#         arr[observation] = 1
#         return arr.reshape(4, 4, 1)

#ModelCatalog.register_custom_preprocessor("my_prep", OneHotPreprocessor)
#config["model"]["custom_preprocessor"] = "my_prep"

#ModelCatalog.register_custom_model("my_model", Dense)
#config["model"]["custom_model"] = "my_model"

#trainer = ppo.PPOTrainer(config=config, env=env_test)
#trainer = dqn.DQNTrainer(config=config, env=env_test)
trainer = pg.PGTrainer(config=config, env=env_test)

for i in range(200):
   # Perform one iteration of training the policy with PPO
   result = trainer.train()
   print(pretty_print(result))

   if i % 100 == 0:
       checkpoint = trainer.save()
       print("checkpoint saved at", checkpoint)