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

 Stat 202A 2019 Fall - Homework 01
 Author:
 Date :

 INSTRUCTIONS: Please fill in the corresponding function. Do not change function names,
 function inputs or outputs. Do not write anything outside the function.
 Do not use any of Python's built in functions for matrix inversion or for linear modeling
 (except for debugging or in the optional examples section).

"""

import numpy as np

###############################################
## Function 1: (2a) Uniform 				 ##
###############################################

def sample_uniform(low=0, high=1):
	random = np.zeros(10000)
	randomy = np.zeros(10000)

	m = 2**31 - 1
	a = 7**5
	c = 12345

	  # Set the seed using the current system time in microseconds
	import time
	d = int(time.time())

	for i in range (0,10000):
		d = (a * d + c) % m
		random[i] = d / m * (high - low) + low

	for i in range (0,9999):
		randomy[i] = random[i+1]

	import matplotlib.pyplot as plt
	fig1 = plt.figure('Figure1',figsize = (6,4)).add_subplot(111)
	fig1.hist(random)
	fig2 = plt.figure('Figure2',figsize = (6,4)).add_subplot(111)
	fig2.scatter(random,randomy)
	plt.show()

###############################################
## Function 2: (2b) Exponential				 ##
###############################################

def sample_exponential(k=1):
	import math
	randomu = np.zeros(10000)
	randomx = np.zeros(10000)
	r = np.zeros(10000)

	m = 2 ** 31 - 1
	a = 7 ** 5
	c = 12345

	  # Set the seed using the current system time in microseconds
	import time
	d = int(time.time())

	for i in range(0,10000):
	    d = (a * d + c) % m
	    randomu[i] = d / m
	    randomx[i] = - (math.log(randomu[i])) / k

	import matplotlib.pyplot as plt
	plt.hist(randomx)
	plt.show()

###############################################
## Function 3: (2c) Normal	 				 ##
###############################################

def sample_normal(mean=0, var=1):
	import math
	import time
	d1 = int(time.time() * 500 * math.exp(2))
	d2 = int(time.time() * 500 * math.exp(1))
	randomu1 = np.zeros(10000)
	randomu2 = np.zeros(10000)
	theta = np.zeros(10000)
	t = np.zeros(10000)
	r = np.zeros(10000)
	x = np.zeros(10000)
	y = np.zeros(10000)

	m = 2 ** 31 - 1
	a = 7 ** 5
	c = 12345

	for i in range (0,10000):
	    d1 = (a * d1 + c) % m
	    d2 = (a * d2 + c) % m
	    randomu1[i] = d1 / m
	    randomu2[i] = d2 / m
	    theta[i] = 2 * math.pi * randomu1[i]
	    t[i] = - math.log(randomu2[i])
	    r[i] = (2 * var * t[i]) ** (1/2)
	    x[i] = r[i]*math.cos(theta[i]) + mean
	    y[i] = r[i]*math.sin(theta[i]) + mean

	import matplotlib.pyplot as plt
	fig1 = plt.figure('Figure4',figsize = (6,6)).add_subplot(111)
	fig1.scatter(x,y)
	fig2 = plt.figure('Figure5',figsize = (6,4)).add_subplot(111)
	fig2.hist(t)
	plt.show()

###############################################
## Function 4: (3) Monte Carlo 				 ##
###############################################

def monte_carlo(d=2):
	ds = []
	random = []
	n = 0

	m = 2 ** 31 - 1
	a = 7 ** 5
	c = 12345

	for i in range (1, d+1):
		ds.append(int(time.time() * 500 * math.exp(i)))
		random.append(np.zeros(1000000))
	for i in range (0,1000000):
		squaresum = 0
		for j in range (0, d):
			ds[j] = (a * ds[j] + c) % m
			random[j][i] = ds[j]/m   #j is dimension, i is iteration
			squaresum += random[j][i]**2 # sum of squares
		if squaresum <= 1:
			n = n + 1
	volume = n * (2**d) / 1000000
	print ("volume="+str(volume))
	if d == 2:
		pi1 = 4 * n / 1000000
		print ("pi="+str(pi1))

########################################################
## Optional examples (comment out before submitting!) ##
########################################################

## test()


"""200803_main.py"""
"""네이버 클라우드 API에서 제공하는 CSR 모듈을 활용한 음성을 텍스트로 변환하는 프로그램"""

"""녹음한 음성을 글자로 변경하는 모듈"""

"""
네이버 클라우드 API중 CSR API를 활용함
"""

"""1. 필요한 모듈"""
# 음성을 글로
import requests

"""2. 음성 파일을 글자로 변경"""

def csrFunc(cloudApi_clientId, cloudApi_clientSecret, wantToText):
    """음성을 글자로"""
    """2-1. 어떤 언어의 음성 파일을 변경할 것인지 입력 - 현재 가능한 것은 Eng와 Kor"""
    # inputLang = input('언어를 입력하세요: ')  # lang = "Eng" # 언어 코드 ( Kor, Jpn, Eng, Chn )

    """2-2. 네이버 CSR API를 활용하기 위해서 필요한 정보"""
    url = "https://naveropenapi.apigw.ntruss.com/recog/v1/stt?lang=Kor" # + inputLang
    data = open(wantToText, 'rb')
    headers = {
        "X-NCP-APIGW-API-KEY-ID": cloudApi_clientId,
        "X-NCP-APIGW-API-KEY": cloudApi_clientSecret,
        "Content-Type": "application/octet-stream"
    }
    response = requests.post(url, data=data, headers=headers)
    rescode = response.status_code
    if (rescode == 200):  # 응답이 성공했으면
        response_data = response.json()
        sttResult = response_data['text']
        return sttResult # 글자로 나온 결과와 설정해준 언어를 반환
    else:  # 200(성공)으로 떨어지지 않으면 이유를 출력. 음성 파일에 말이 없어도 에러가 떨어짐
        print("Error : " + response.text)


import unittest
import test_rest_resource
import requests
from datetime import date
from model.project import Project, get_project_from_json

class TestProjects(test_rest_resource.TestRestResource):

    def setUp(self):
        super(TestProjects, self).setUp()
        self.resource_name = 'project'
        self.resources_uri = self.root_uri + 'projects'
        self.single_resource_uri = self.resources_uri + '/1'
        self.example_resource = Project(
            name="Test Project",
            description="some description",
            start_date=date(2013, 1, 1),
            end_date=date(2014, 12, 31),
            status=1,
            owner=1,
            invited_devs=[],
            sprints=[],
            id_=2
        )

        self.update_resource = Project(
            name="Updated Test Project",
            description="some updated description",
            start_date=date(2013, 1, 2),
            end_date=date(2014, 12, 2),
            status=2,
            owner=2,
            invited_devs=[1],
            sprints=[],
            id_=2
        )

        self.invalid_create_new_json = [
            # Not a json object
            'Test',

            # Missing status
            '{"description": "some description", "end_date": "2014-12-31", "start_date": "2013-01-01", "owner": 1, "name": "Test Project"}',

            # Missing description
            '{"status": 1, "end_date": "2014-12-31", "start_date": "2013-01-01", "owner": 1, "name": "Test Project"}',

            # Missing end_date
            '{"status": 1, "description": "some description", "start_date": "2013-01-01", "owner": 1, "name": "Test Project"}',

            # Missing start_date
            '{"status": 1, "description": "some description", "end_date": "2014-12-31", "owner": 1, "name": "Test Project"}',

            # Missing owner
            '{"status": 1, "description": "some description", "end_date": "2014-12-31", "start_date": "2013-01-01", "name": "Test Project"}',

            # Missing name
            '{"status": 1, "description": "some description", "end_date": "2014-12-31", "start_date": "2013-01-01", "owner": 1}',

            # Additional non-existing field (a)
            '{"status": 1, "description": "some description", "end_date": "2014-12-31", "start_date": "2013-01-01", "owner": 1, "name": "Test Project", "a": "a"}',

            # Additional existing field (id)
            '{"status": 1, "description": "some description", "end_date": "2014-12-31", "start_date": "2013-01-01", "owner": 1, "name": "Test Project", "id":1}'
        ]

        self.invalid_update_json = [
            # Non-existing field
            '{"a": "a"}',
        ]

    def test_invite_developers(self):
        method = requests.put
        uri = self.single_resource_uri + '/invite_dev'
        data = '{"email":"vimal@chellakudam"}'
        self.ensure_unauthorized_fails_authorized_passes(method, uri, 204, data=data)

    def test_remove_developers(self):
        method = requests.put
        uri = self.single_resource_uri + '/remove_devs'
        data = '{"devs":[3, 4]}'
        self.ensure_unauthorized_fails_authorized_passes(method, uri, 204, data=data)

    def get_resource_from_json(self, json_str):
        return get_project_from_json(json_str)

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


from flask import Flask, request, jsonify

app = Flask (__name__)

@app.route('/')
def index():
    # stuurt inteface naar browser.
    return app.send_static_file('index.html')

@app.route('/light', methods=['GET'])
def light():
    data = {}
    #zet licht aan. Voeg commando's toe!
    #bevestig licht aan.
    data['light'] = 'ON'
    # converteer dict naat json en print naar interface.
    return jsonify(data)

@app.route('/light', methods=['POST'])
def lightPOST():
    data = request.get_json()
    data['light'] = not data['light']
    return jsonify(data)

if __name__ == "__main__":
    app.run(debug=True)


# DEPENDENCIES
import keras
from keras.models import Model
from keras.preprocessing import image
from keras.layers import Lambda, Input, Conv2D, MaxPooling2D, UpSampling2D
from keras.layers.core import Layer, Dense
from keras.utils import np_utils
import keras.backend as K
import pandas as pd
import numpy as np
import random
import matplotlib.pyplot as plt
from scipy.ndimage.filters import laplace
import re
import os
from sklearn.model_selection import train_test_split
from keras.losses import binary_crossentropy
#------------------

# HELPER FUNCTIONS -----------

# DATA PROCESSING FUNCTIONS

def create_priors_posts(file):

    # Reads csv with rows as pixels and separates even/odd rows into prior/post datasets

    print('reading image CSV - %s' %(str(file)) )
    df = pd.read_csv(file, header=None)
    df['frame_num'] = pd.to_numeric(df[0].apply(lambda x: re.search(r'\d+', x).group()))
    df = df.sort_values(by='frame_num')
    df = df.drop(['frame_num', 0], axis=1)

    print('splitting images into prior/post sets')
    priors = df.iloc[::2].reset_index(drop=True)
    posts = df.iloc[1::2].reset_index(drop=True)

    if len(priors) > len(posts):
        priors = priors[:-1]

    return priors, posts

def get_train_test_indices(X, y, test_size):

    # Split prior/post dataset based on user-defined split and return frame indices so they can be re-used
    # Only run once!

    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=42, shuffle=True)

    train_indices = list(X_train.index)
    test_indices = list(X_test.index)

    return train_indices, test_indices

def split_training_data(X, y, train_indices, test_indices):

    # Delete train/test index if out of bounds (happens for bidirectional data)

    train_indices = [i for i in train_indices if i <= max(list(X.index))]
    test_indices = [i for i in test_indices if i <= max(list(X.index)) ]

    # Split dataset based on consistent training/test indices

    X_train, y_train = X.iloc[train_indices], y.iloc[train_indices]
    X_test, y_test = X.iloc[test_indices], y.iloc[test_indices]

    return X_train, X_test, y_train, y_test

def create_bidirectional_data(priors, posts):

    # Shuffle data to match t-1, t+1 frames as training data to label image at time t

    bidirectional_train  = priors.copy().shift(-1).reset_index(drop=True)[:-1]
    bidirectional_train = priors.join(bidirectional_train, lsuffix='_t-1', rsuffix='_t+1', how='inner')
    bidirectional_response = posts[:-1]

    return bidirectional_train, bidirectional_response

def prep_conv_ae_training_data(video_dir, image_dimensions):

    # Create priors/posts with numpy dimensions needed for convolutional AE
    # This is a bit hacky but otherwise having difficulty getting shape to be correct

    print('Converting images to arrays...')
    image_files = []

    for i in os.listdir(video_dir):
        frame_number = re.search('frame(\d+)', i).group(1)
        frame_image = image.load_img(video_dir + '/' + i, color_mode='grayscale', target_size=image_dimensions)
        frame_array = image.img_to_array(frame_image) / 255
        image_files.append([frame_number, frame_array])

    print('Aggregating & sorting image arrays...')
    df = pd.DataFrame(image_files, columns=['frame_num', 'image'])
    df['frame_num'] = pd.to_numeric(df['frame_num'])

    # need to make sure images are in order
    df = df.sort_values(by='frame_num').reset_index(drop=True)

    print('Splitting priors and posts...')

    conv_priors = []
    for prior in df.iloc[::2]['image']:
      conv_priors.append(prior)

    conv_posts = []
    for post in df.iloc[1::2]['image']:
      conv_posts.append(post)

    return np.array(conv_priors), np.array(conv_posts)

# AUTOENCODER FUNCTIONS

def create_mlae(input_size, hidden_size1, hidden_size2, hidden_size3, code_size, output_size):

    # Standard NN autoencoder (used for forward, backward, and bi-directional)

    input_layer = Input(shape=(input_size,)) #input
    hidden_1 = Dense(hidden_size1, activation='relu')(input_layer) #encoder
    hidden_2 = Dense(hidden_size2, activation='relu')(hidden_1) #encoder
    hidden_3 = Dense(hidden_size3, activation='relu')(hidden_2) #encoder
    code = Dense(code_size, activation='relu')(hidden_3) #code
    hidden_4 = Dense(hidden_size3, activation='relu')(code) #decoder
    hidden_5 = Dense(hidden_size2, activation='relu')(hidden_4) #decoder
    hidden_6 = Dense(hidden_size1, activation='relu')(hidden_5) #decoder
    output_layer = Dense(output_size, activation='sigmoid')(hidden_6) #output

    mlae = Model(inputs=input_layer, outputs=output_layer)
    mlae.compile(optimizer='adam', loss='mse')

    return mlae

def create_conv_ae(input_shape, filters):

    # Convolutional autoencoder

    input_img = Input(input_shape)

    x = Conv2D(filters, (3, 3), activation='relu', padding='same')(input_img)
    x = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(filters, (3, 3), activation='relu', padding='same')(x)
    encoded = MaxPooling2D((2, 2), padding='same')(x)
    x = Conv2D(filters, (3, 3), activation='relu', padding='same')(encoded)
    x = UpSampling2D((2, 2))(x)
    x = Conv2D(filters, (3, 3), activation='relu', padding='same')(x)
    x = UpSampling2D((2, 2))(x)
    decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

    conv_autoencoder = Model(input_img, decoded)
    conv_autoencoder.compile(optimizer='adadelta', loss='mse')
    return conv_autoencoder

# Variational autoencoder (VAE) sampling helper function

def sampling(args):
    z_mean, z_log_var = args
    batch = K.shape(z_mean)[0]
    dim = K.int_shape(z_mean)[1]
    # by default, random_normal has mean=0 and std=1.0
    epsilon = K.random_normal(shape=(batch, dim))
    return z_mean + K.exp(0.5 * z_log_var) * epsilon

def create_vae(original_dim, intermediate_dim, latent_dim):

    # Variational autoencoder

    input_shape = (original_dim, )

    # build encoder model
    inputs = Input(shape=input_shape, name='encoder_input')
    x = Dense(intermediate_dim, activation='relu')(inputs)
    z_mean = Dense(latent_dim, name='z_mean')(x)
    z_log_var = Dense(latent_dim, name='z_log_var')(x)

    # use reparameterization trick to push the sampling out as input
    # note that "output_shape" isn't necessary with the TensorFlow backend
    z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var])

    # instantiate encoder model
    encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')

    # build decoder model
    latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
    x = Dense(intermediate_dim, activation='relu')(latent_inputs)
    outputs = Dense(original_dim, activation='sigmoid')(x)

    # instantiate decoder model
    decoder = Model(latent_inputs, outputs, name='decoder')

    # instantiate VAE model
    outputs = decoder(encoder(inputs)[2])
    vae = Model(inputs, outputs, name='vae_mlp')

    # VAE custom loss function (variables not found if outside this function)
    def vae_loss(inputs, outputs):
          reconstruction_loss = binary_crossentropy(inputs, outputs)
          reconstruction_loss *= original_dim
          kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
          kl_loss = K.sum(kl_loss, axis=-1)
          kl_loss *= -0.5
          vae_loss = K.mean(reconstruction_loss + kl_loss)
          return vae_loss

    models = (encoder, decoder)
    vae.compile(optimizer='adam', loss=vae_loss)
    return models, vae

# Helper function to kick off training for any model type

def train_mlae(model, X_train, Y_train, X_test, Y_test, epochs, batch_size):
    hist = model.fit(X_train, Y_train,
                        batch_size=batch_size, epochs=epochs, verbose=1,
                        validation_data=(X_test, Y_test))
    return hist

# RUN ACTUAL MODELS

#FORWARD MLAE MODEL--------------
#basically: given frame t-1, can we predict frame t
print('\n\nGATHERING DATA FOR FORWARD MLAE MODEL')

# Use subset for testing

#priors, posts = create_priors_posts('small_data.csv')

priors, posts = create_priors_posts('data.csv')

train_indices, test_indices = get_train_test_indices(priors, posts, test_size=0.3)

priors_train, priors_test, posts_train, posts_test = split_training_data(priors, posts, train_indices, test_indices)

#create and train model
print('\nINITIALIZING FORWARD MLAE MODEL')
forward_mlae = create_mlae(20000, 1024, 512, 256, 128, 20000) #params: input_size, hidden_size1, hidden_size2, hidden_size3, code_size, output_size
print('\nTRAINING FORWARD MLAE MODEL')
hist_forward_mlae = train_mlae(forward_mlae, priors_train, posts_train, priors_test, posts_test, 1, 64) #params: model, X_train, Y_train, X_test, Y_test, epochs, batch_size
print('\tDone.')
#--------------

#BACKWARD MLAE MODEL--------------
#basically: given frame t+1, can we predict frame t
print('\n\nGATHERING DATA FOR BACKWARD MLAE MODEL')

#we want to swap priors and posts for this so we can keep the rest the same as the Forward MLAE model
temp = priors #standard switcheroo
priors = posts
posts = temp
del temp

priors_train, priors_test, posts_train, posts_test = split_training_data(priors, posts, train_indices, test_indices)

#create and train model
print('\nINITIALIZING BACKWARD MLAE MODEL')
backward_mlae = create_mlae(20000, 1024, 512, 256, 128, 20000) #params: input_size, hidden_size1, hidden_size2, hidden_size3, code_size, output_size
print('\nTRAINING BACKWARD MLAE MODEL')
hist_backward_mlae = train_mlae(backward_mlae, priors_train, posts_train, priors_test, posts_test, 1, 64) #params: model, X_train, Y_train, X_test, Y_test, epochs, batch_size
print('\tDone.')
#--------------

#BIDIRECTIONAL MLAE MODEL--------------
#basically: given frames t-1, t+1, can we predict frame t
print('\n\nGATHERING DATA FOR BIDIRECTIONAL MLAE MODEL')

# Swap priors/posts back to original
temp = priors #standard switcheroo
priors = posts
posts = temp
del temp

bidirectional_train, bidirectional_response = create_bidirectional_data(priors, posts)
bi_priors_train, bi_priors_test, bi_posts_train, bi_posts_test =  split_training_data(bidirectional_train,
                                                                                bidirectional_response,
                                                                                train_indices, test_indices)
#create and train model
print('\nINITIALIZING BIDIRECTIONAL MLAE MODEL')
bidirectional_mlae = create_mlae(20000*2, 1024, 512, 256, 128, 20000) #params: input_size, hidden_size1, hidden_size2, hidden_size3, code_size, output_size
print('\nTRAINING BIDIRECTIONAL MLAE MODEL')
hist_bidirectional_mlae = train_mlae(bidirectional_mlae, bi_priors_train, bi_posts_train, bi_priors_test, bi_posts_test, 1, 64) #params: model, X_train, Y_train, X_test, Y_test, epochs, batch_size
print('\tDone.')
#--------------

#CONVOLUTIONAL AUTOENCODER--------------

print('\n\nGATHERING DATA FOR CONVOLUTIONAL AE MODEL')

# Run custom conv_ae data pipeline
conv_priors, conv_posts = prep_conv_ae_training_data(video_dir='Frames_bambi', image_dimensions=(200, 100))

conv_priors_train, conv_posts_train = conv_priors[(train_indices)], conv_posts[(train_indices)]
conv_priors_test, conv_posts_test = conv_priors[(test_indices)], conv_posts[(test_indices)]

# Free up some memory
del conv_priors
del conv_posts

#create and train model
print('\nINITIALIZING CONVOLUTIONAL AE MODEL')
conv_autoencoder = create_conv_ae(input_shape=(200, 100, 1), filters=32)
print('\nTRAINING CONVOLUTIONAL AE MODEL')
hist_convolutional_ae = train_mlae(conv_autoencoder, conv_priors_train, conv_posts_train, conv_priors_test, conv_posts_test, epochs=1, batch_size=1)
print('\tDone.')
#--------------

#VARIATIONAL AUTOENCODER--------------
print('\n\nGATHERING DATA FOR VARIATIONAL AE MODEL')
priors_train, priors_test, posts_train, posts_test = split_training_data(priors, posts, train_indices, test_indices)

print('\nINITIALIZING VARIATIONAL AE MODEL')
models, vae = create_vae(original_dim=20000, intermediate_dim=512, latent_dim=3)
print('\nTRAINING VARIATIONAL AE MODEL')
hist_vae = train_mlae(vae, priors_train, posts_train, priors_test, posts_test, epochs=1, batch_size=1)
print('\tDone.')

#--------------


from bs4 import BeautifulSoup

import requests

def get_proxies():
    proxies = []
    response = requests.get('https://free-proxy-list.net/')
    soup = BeautifulSoup(response.text, "html.parser")

    for tr in soup.find_all('tr'):
        td_list = tr.find_all('td')
        proxy_dict = {}
        if not td_list is None and len(td_list) == 8:
            for n, key in enumerate(['IP', 'Port', 'code', 'country',
                                     'anonymity', 'google', 'https', 'refresh']):
                text = td_list[n].get_text()
                if text == 'yes': text = True
                elif text == 'no': text = False
                proxy_dict[key] = text
            proxies.append(proxy_dict)
    return proxies


from OmniBot import OmniBot
from discord import Embed
from discord.ext import commands

class Eval:
    def __init__(self, bot: OmniBot):
        self.bot = bot
        return

    @commands.command(aliases=["evaluate", "calculate", "math"])
    async def eval(self, context: commands.context.Context, *equation):
        result = eval(''.join(equation))
        embed = Embed(colour=0xd44e52)
        embed.set_author(name=context.author.name, icon_url=context.author.avatar_url)
        embed.add_field(name="Result", value=result, inline=False)
        embed.set_footer(text="Does your maths for you.")
        await context.send(embed=embed)
        return

def setup(bot):
    bot.add_cog(Eval(bot))


from  collections import defaultdict
a_group = defaultdict(list)
n_val, m_val =(int(x) for x in input().split(' '))
for i in range(n_val):
    a_group[input()].append(i + 1)
for _ in range(m_val):
    lst = a_group[input()]
    if len(lst) > 0:
        for el in lst:
            print(el, end=' ')
        print()
    else:
        print(-1)


from JumpScale import j
import yaml

CATEGORY = "ays:bp"

class Blueprint:
    """
    """

    def __init__(self, aysrepo, path="", content=""):
        self.logger = j.logger.get('j.atyourservice.blueprint')
        self.aysrepo = aysrepo
        self.path = path
        self.active = True
        self.models = []  # can be actions or services or recurring
        self.actions = []
        self.eventFilters = []
        self._contentblocks = []

        if path != "":
            self.name = j.sal.fs.getBaseName(path)
            if self.name[0] == "_":
                self.active = False
            self.name = self.name.lstrip('_')
            self.content = j.sal.fs.fileGetContents(path)
        else:
            self.name = 'unknown'
            self.content = content

        self.is_valid = self._validate_yaml(self.content)

        if self.is_valid:
            content = ""
            nr = 0
            # we need to do all this work because the yaml parsing does not
            # maintain order because its a dict
            for line in self.content.split("\n"):
                if len(line) > 0 and line[0] == "#":
                    continue
                if content == "" and line.strip() == "":
                    continue

                line = line.replace("\t", "    ")
                nr += 1
                if len(content) > 0 and (len(line) > 0 and line[0] != " "):
                    self._add2models(content, nr)
                    content = ""

                content += "%s\n" % line

            # to process the last one
            self._add2models(content, nr)
            self._contentblocks = []

            self.hash = j.data.hash.md5_string(self.content)

            self.is_valid = self._validate_format(self.models)

    def load(self, role="", instance=""):
        self.actions = []
        self.eventFilters = []

        for model in self.models:
            if model is not None:

                if "actions" in model:
                    # found action need to add them to blueprint
                    for actionModel in model["actions"]:
                        if 'actor' not in actionModel:
                            actor0 = ""
                        else:
                            actor0 = actionModel["actor"]
                        if 'service' not in actionModel:
                            service0 = ""
                        else:
                            service0 = actionModel["service"]
                        if 'recurring' not in actionModel:
                            recurring0 = ""
                        else:
                            recurring0 = actionModel["recurring"]
                        servicesFound = self.aysrepo.servicesFind(name=service0, actor=actor0)

                        if len(servicesFound) == 0:
                            raise j.exceptions.Input(message="found action to execute but could not find required service:%s!%s" % (
                                actor0, service0), level=1, source="", tags="", msgpub="")

                        if "action" not in actionModel:
                            raise j.exceptions.Input(message="need to specify action.",
                                                     level=1, source="", tags="", msgpub="")

                        actions = [item.strip() for item in actionModel["action"].split(",") if item.strip() != ""]

                        for serviceObj in servicesFound:
                            for actionName in actions:
                                self.actions.append({
                                    'service_key': serviceObj.model.key,
                                    'action_name': actionName,
                                    'recurring_period': recurring0,
                                })

                elif "eventfilters" in model:
                        # found action need to add them to blueprint
                    for obj in model["eventfilters"]:
                        if 'actor' not in obj:
                            actor0 = ""
                        else:
                            actor0 = obj["actor"]
                        if 'service' not in obj:
                            service0 = ""
                        else:
                            service0 = obj["service"]
                        if 'channel' not in obj:
                            channel0 = ""
                        else:
                            channel0 = obj["channel"]
                        if 'cmd' not in obj:
                            cmd0 = ""
                        else:
                            cmd0 = obj["cmd"]
                        if 'secret' not in obj:
                            secret0 = ""
                        else:
                            secret0 = obj["secret"]
                        if 'action' not in obj:
                            action0 = ""
                        else:
                            action0 = obj["action"]

                        servicesFound = self.aysrepo.servicesFind(name=service0, actor=actor0)

                        if len(servicesFound) == 0:
                            raise j.exceptions.Input(message="found action to execute but could not find required service:%s!%s" % (
                                actor0, service0), level=1, source="", tags="", msgpub="")

                        for serviceObj in servicesFound:
                            self.eventFilters.append({
                                'service_key': serviceObj.model.key,
                                'channel': channel0,
                                'command': cmd0,
                                'secret': secret0,
                                'action_name': action0,
                            })

                else:
                    for key, item in model.items():
                        if key.find("__") == -1:
                            raise j.exceptions.Input(
                                "Key in blueprint is not right format, needs to be $aysname__$instance, found:'%s'" % key)
                        actorname, bpinstance = key.split("__", 1)

                        if instance != "" and bpinstance != instance:
                            self.logger.info(
                                "ignore load from blueprint for: %s:%s" % (actorname, bpinstance))
                            continue

                        if actorname.find(".") != -1:
                            rolefound, _ = actorname.split(".", 1)
                        else:
                            rolefound = actorname

                        if role != "" and role != rolefound:
                            self.logger.info(
                                "ignore load from blueprint based on role for: %s:%s" % (actorname, bpinstance))
                            continue

                        # check if we can find actorname and if not then check if there is a blueprint.  name...
                        if not self.aysrepo.templateExists(actorname) and not actorname.startswith('blueprint.'):
                            blueaysname = 'blueprint.%s' % actorname
                            if self.aysrepo.templateExists(blueaysname):
                                actorname = blueaysname

                        if not self.aysrepo.templateExists(actorname):
                            raise j.exceptions.Input(message="Cannot find actor:%s" %
                                                     actorname, level=1, source="", tags="", msgpub="")

                        actor = self.aysrepo.actorGet(actorname, reload=False)
                        args = {} if item is None else item
                        actor.serviceCreate(instance=bpinstance, args=args)

        # first we had to make sure all services do exist, then we can add these properties
        for action_info in self.actions:
            service = self.aysrepo.serviceGetByKey(action_info['service_key'])
            service.scheduleAction(action_info['action_name'], period=action_info['recurring_period'])
            service.saveAll()

        for event_filter in self.eventFilters:
            service = self.aysrepo.serviceGetByKey(event_filter['service_key'])
            service.model.eventFilterSet(
                channel=event_filter['channel'], action=event_filter['action_name'],
                command=event_filter['command'], secrets=event_filter['secret'])
            service.saveAll()

    def _add2models(self, content, nr):
        # make sure we don't process double
        if content in self._contentblocks:
            return
        self._contentblocks.append(content)
        try:
            model = j.data.serializer.yaml.loads(content)
        except Exception as e:
            msg = "Could not process blueprint (load from yaml):\npath:'%s',\nline: '%s', content:\n######\n\n%s\n######\nerror:%s" % (
                self.path, nr, content, e)
            raise j.exceptions.Input(msg)

        self.models.append(model)

    @property
    def services(self):
        services = []
        for model in self.models:

            if model is not None:
                for key in model.keys():

                    aysname, aysinstance = key.split("__", 1)
                    if aysname.find(".") != -1:
                        rolefound, _ = aysname.split(".", 1)
                    else:
                        rolefound = aysname

                    service = self.aysrepo.serviceGet(role=rolefound, instance=aysinstance, die=False)
                    if service:
                        services.append(service)

        return services

    def disable(self):
        if self.active:
            base = j.sal.fs.getBaseName(self.path)
            dirpath = j.sal.fs.getDirName(self.path)
            newpath = j.sal.fs.joinPaths(dirpath, "_%s" % base)
            j.sal.fs.moveFile(self.path, newpath)
            self.path = newpath
            self.active = False

    def enable(self):
        if self.active is False:
            base = j.sal.fs.getBaseName(self.path)
            if base.startswith("_"):
                base = base[1:]
            dirpath = j.sal.fs.getDirName(self.path)
            newpath = j.sal.fs.joinPaths(dirpath, base)
            j.sal.fs.moveFile(self.path, newpath)
            self.path = newpath
            self.active = True

    def _validate_yaml(self, content):
        try:
            j.data.serializer.yaml.loads(content)
            return True
        except yaml.YAMLError:
            raise j.exceptions.Input('Yaml format is not valid for %s please fix this to continue' % self.name)

    def _validate_format(self, models):
        for model in models:
            if not j.data.types.dict.check(model):
                self.logger.error("Bad formatted blueprint: %s" % self.path)
                return False

            if model is not None:
                for key in model.keys():

                    # this two blocks doesn't have the same format as classic service declaration
                    if key in ['actions', 'eventfilters']:
                        if not j.data.types.list.check(model[key]):
                            self.logger.error("%s should be a list of dictionary: found %s" % (key, type(model[key])))
                            return False
                    else:
                        if key.find("__") == -1:
                            self.logger.error("Key in blueprint is not right format, needs to be $aysname__$instance, found:'%s'" % key)
                            return False

                        aysname, _ = key.split("__", 1)
                        if aysname not in self.aysrepo.templates:
                            self.logger.error("Service template %s not found. Can't execute this blueprint" % aysname)
                            return False

        return True

    def validate(self):
        if not self._validate_yaml(self.content):
            return False

        if not self._validate_format(self.models):
            return False

        return True

    def __str__(self):
        return "%s:%s" % (self.name, self.hash)

    __repr__ = __str__


#!/usr/bin/env python3
# coding: utf-8
###################################################################
# Copyright (c) 2016-2020 European Synchrotron Radiation Facility #
#                                                                 #
# This work is licensed under the terms of the MIT license.       #
# For further information, see https://github.com/mretegan/crispy #
###################################################################
"""This module provides a command line interface for generating the parameters of atomic configurations."""

__authors__ = ["Marius Retegan"]
__license__ = "MIT"
__date__ = "11/05/2019"

import argparse
import logging
import os

import h5py

from crispy.modules.quanty.utils import Calculations, Cowan, Element

def generate_parameters(elements):
    """Generate the atomic parameters of the elements and store them in an HDF5 container."""

    config = h5py.get_config()
    config.track_order = True

    root = os.path.dirname(os.path.abspath(__file__))

    calculations = Calculations(os.path.join(root, "calculations.json"))

    if elements == "all":
        elements = calculations.get_all_elements()

    for element in elements:
        element = Element(element)
        confs = calculations.unique_configurations(element)

        filename = "{:s}.h5".format(element.symbol)
        path = os.path.join(root, "atomic_parameters", filename)

        with h5py.File(path, "w") as h5:
            for conf in confs:
                cowan = Cowan(element, conf)
                conf.energy, conf.atomic_parameters = cowan.get_parameters()
                cowan.remove_calculation_files()

                # Write the parameters to the HDF5 file.
                root = "/{:s}".format(conf.name)
                h5[root + "/energy"] = conf.energy
                for parameter, value in conf.atomic_parameters.items():
                    path = root + "/parameters/{:s}".format(parameter)
                    h5[path] = value

                logging.info("%-2s %-8s", element.symbol, conf)
                logging.info("E = %-.4f eV", conf.energy)
                for parameter, value in conf.atomic_parameters.items():
                    logging.debug("%-s = %-.4f eV", parameter, value)
                logging.info("")

def main():
    parser = argparse.ArgumentParser(
        description="Generate the data needed to run the Quanty calculations.",
    )
    parser.add_argument("-l", "--loglevel", default="info")

    subparsers = parser.add_subparsers(dest="command")

    parameters = subparsers.add_parser("parameters")
    parameters.add_argument(
        "-e",
        "--elements",
        default="all",
        nargs="+",
        help="list of elements for which to generate the parameters",
    )

    args = parser.parse_args()

    logging.basicConfig(format="%(message)s", level=args.loglevel.upper())

    if args.command == "parameters":
        generate_parameters(args.elements)

if __name__ == "__main__":
    main()


from selenium import webdriver
import time
import csv

ETFS = []
#open the csv file and record column[0] in a dictionary
with open('Materials Equity ETF List (62).csv', newline='') as csvfile:
	rows = csv.reader(csvfile)
	for row in rows:
		ETFS.append(row[0])

#timestamp now
Now = int(time.time())

for i in range(1, len(ETFS)):
	print(ETFS[i])
	#目標網頁的網址
	url1 = "https://finance.yahoo.com/quote/"
	etf = ETFS[i]
	print(etf)
	url2 = "/history?period1=1451491200&period2="
	now = str(Now)
	url3 = "&interval=1d&filter=history&frequency=1d"
	url = url1+etf+url2+now+url3

	#用chromedriver開啟Chrome
	driver = webdriver.Chrome()
	driver.get(url)

	#在網頁中找尋"Download Data並點擊"
	driver.find_element_by_link_text("Download Data").click()

driver.close()


from http.server import BaseHTTPRequestHandler, HTTPServer

# HTTPRequestHandler sınıfı
class HTTPServer_RequestHandler(BaseHTTPRequestHandler):

    # GET fonksiyonu
    def do_GET(self):
        # status kodunu gönder
        self.send_response(200)

        # header'ları gönder
        self.send_header('Content-type','text/html')
        self.end_headers()

        # istemciye gönderilecek mesajı belirle
        if self.path == "/":
            message = "Hello, world!"
        else:
            name = self.path[1:]
            message = "Hello, {}!".format(name)

        # mesajı yaz
        self.wfile.write(bytes(message, "utf8"))
        return

def run():
  print('starting server...')

  # set up server
  port = 8080
  server_address = ('0.0.0.0', port)
  httpd = HTTPServer(server_address, HTTPServer_RequestHandler)

  # run server
  print('running server on port {}...'.format(port))
  httpd.serve_forever()

run()