from collections import OrderedDict
import sys

#OrderedDict for game data because we care about the order players are added
game_stats = OrderedDict([])

#constant variables for game rules provided
max_players = 6
max_frames = 10
max_pins = 10
balls_per_frame = 2
strike = [max_pins, 0]

#for development, we might use an pre-set OrderedDict as below
##with expected outcome:
##Jones: 300
##Smith: 20
##Foo: 190
##Bar 300
"""
game_stats = OrderedDict(
    [('Jones', [10, 0, 10, 0, 10, 0, 10, 0, 10, 0,
                10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 10]),
    ('Smith', [1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
               1, 1, 1, 1, 1, 1, 1, 1, 1, 1]),
     ('Foo', [9, 1, 9, 1, 9, 1, 9, 1, 9, 1,
              9, 1, 9, 1, 9, 1, 9, 1, 9, 1, 9]),
     ('Bar', [10, 0, 10, 0, 10, 0, 10, 0, 10, 0,
                10, 0, 10, 0, 10, 0, 10, 0, 10, 0, 10, 10])])
"""

def present_menu():
    """
    present_menu() presents options and directs to chosen function
    """
    option_selected = input('\nPlease select an option:\n'+
                             '1. Add a player\n'+
                            '2. Play game or rematch\n' +
                            '3. Print scoreboard\n' +
                            '4. New game (reset players and score)\n> ').strip()

    #check valid input and direct to option
    if option_selected in options:
        options[option_selected]()
    else:
        print_error('Sorry, please input a number from the list of options')

def add_player():
    """
    add_player() asks for player's name
    and then adds player to game_stats dict
    """
    #a game can only have up to 6 players
    if len(game_stats) >= max_players:
        print_error('Sorry, there is already the maximum number of players ' +
              'in this game')
        return

    new_player = input('Please enter the new player\'s name:\n'+
                        '(Type \'esc\' to return to the menu)\n>').strip()

    #check for escape pattern
    if new_player == 'esc':
        return

    #store the player's name if it's unique
    elif new_player not in game_stats:
        game_stats[new_player] = []
        print('Welcome to the game, ' + new_player + '!')
    else:
        print_error('Sorry, ' + new_player +
              ' could not be added to the game. '
              + new_player + ' is already playing!')

def play_game():
    """
    play_game() cycles through frames of a bowling game and ask for the score of
    each player assuming the game is being played in real-time or that the user
    wants to input the scores in the order at which it was played
    Uses get_ball_score to get user input or escape pattern 'esc'
    """
    if len(game_stats) is 0:
        print_error('You must add players to play a game')
        return

    #assumes a new game is started
    rematch()

    for frame_num in range(1, max_frames+1):

        for player_name in game_stats:

            frame_total = 0

            #2 balls per frame (unless bonus are awarded)
            max_balls = balls_per_frame
            ball_num = 1
            while ball_num <= max_balls:
                ball_score = get_ball_score(player_name, ball_num, frame_num)

                #If user entered 'esc' get_ball_score returns -1
                ##so stop playing game
                if ball_score is None:
                    return

                frame_total += ball_score

                #The user should not claim a score of over 10 pins per frame
                if frame_num != max_frames and frame_total > max_pins:
                    print_error('The value you entered is too high')

                    #mark ball as unplayed
                    frame_total -= ball_score
                    continue

                game_stats[player_name].append(ball_score)

                #When all pins are knocked down, either a strike or spare
                if frame_total is max_pins:

                    if ball_num is 1:
                        print('Strike!')

                        #mark strike
                        game_stats[player_name].append(0)

                        #if strike in normal frame then
                        ##skip second ball
                        if frame_num < max_frames:
                            ball_num += 1
                            break

                        #total of 3 balls in final frame if strike
                        ##1 'normal', 2 'bonus'
                        max_balls += 1

                    elif ball_num is 2:
                        print('Spare!')

                        #In final frame,
                        ##you get 1 bonus ball if a spare is thrown
                        if frame_num is max_frames:
                            max_balls += 1
                ball_num += 1

def get_ball_score(player, ball, frame):
    """
    Asks player to enter their score for a specific frame,
    checks validity and only returns once
    returns None when user types 'esc'
    """
    while True:
        ball_score = input('Enter '
                           + str(player) + '\'s score for bowl '
                           + str(ball) + ' of frame '
                           + str(frame) + ':\n'+
                           '(Type \'esc\' to return to the menu)\n>').strip()

        #check for escape pattern
        if ball_score == 'esc':
            return None

        #only accept if number is integer, else loop
        try:
            ball_score = int(ball_score)
            if ball_score not in range(max_pins+1):
                raise ValueError
            return ball_score
        except ValueError:
            print_error('You must enter the number of pins knocked down.')

def print_score():
    """
    Prints table for each player's score and overall ranking
    Uses find_bonus to calculate the bonus of strikes and spares
    """
    if len(game_stats) is 0:
        print_error('You must add players to print score')
        return

    #to make a general ranking, store name and score in OderedDict
    ##will need to be sorted later
    ranking = OrderedDict()

    for player_name in game_stats:
        balls_line = '{h: <8}'.format(h = 'Ball:')
        bonus_line = '{h: <8}'.format(h = 'Bonus:')
        total_line = '{h: <8}'.format(h = 'Total:')

        player_stats = game_stats[player_name]

        player_total = 0
        ball_num = 0

        #two normal throws per frame
        while ball_num < len(player_stats) and ball_num < max_frames*balls_per_frame:

            #find the ball scores for this frame
            ##(could be one ball if game is incomplete)
            if ball_num < len(player_stats)-1:
                frame_stats = player_stats[ball_num:ball_num+balls_per_frame]
            else:
                frame_stats = [player_stats[ball_num]]

            frame_total = sum(frame_stats)
            bonus = 0

            #For a Strike, print 'X' and score two ball as bonus
            if frame_stats == strike:
                bonus = find_bonus(player_stats, (ball_num+2) , 2)
                frame_stats = ['X',' ']

            #For a Spare, print '/' and award one ball as bonus
            elif frame_total is max_pins:
                bonus = find_bonus(player_stats, (ball_num+2) , 1)
                frame_stats[1] = '/'

            #Calculate frame score and accumulate players game total
            frame_total += bonus
            player_total += frame_total

            #Append each string ready to print
            balls_line += '{s: <7}'.format(s = ' '.join(str(stat) for stat in frame_stats))
            bonus_line += '{b: <7}'.format(b = bonus)
            total_line += '{t: <7}'.format(t = player_total)

            ball_num += balls_per_frame

        #append bonus balls too (after max_frames)
        while ball_num < len(player_stats):

            if player_stats[ball_num] in range(11):
                balls_line += '{stat: <3}'.format(stat = str(player_stats[ball_num]))
            ball_num += 1

        #print as table
        print_bold(player_name)
        print(balls_line)
        print(bonus_line)
        print(total_line)

        ranking[player_name] = player_total

    #order players by descending score
    ranking = OrderedDict(sorted(ranking.items(), key=lambda t: t[1], reverse = True))

    #print a ranking table
    print_bold('Overall Ranking:')
    rank = 1
    prev_score = 0
    for name in ranking:
        #compare to last score to see if rank is shared
        score = ranking[name]
        if score < prev_score:
            rank += 1
        prev_score = score

        print('{r: <3}{n: <20}{s: <5}'.format(r = rank, n = name, s = score))

def find_bonus(player_stats, next_ball, bonuses_allowed):
    """
    find_bonus looks ahead to see
    what the bonus of strike or spare is
    """
    bonuses_used = 0
    bonus = 0

    #while there are bonus balls to be awarded
    ##find total bonus
    while (
        bonuses_used < bonuses_allowed and next_ball < len(player_stats)
        ):

        if player_stats[next_ball] < max_pins+1:
            bonus += player_stats[next_ball]
            bonuses_used += 1

        #if first ball of frame & is a strike, then
        ## skip the second ball
        if (
            next_ball%2 is 0 and player_stats[next_ball:next_ball+2] == strike
            ):
            next_ball += 2

        else:
            next_ball += 1

    return bonus

def rematch():
    """
    rematch() sets all player's stats to an empty list
    """
    for player_name in game_stats:
        game_stats[player_name] = []

def clear_game():
    """
    clear_game() empties all entries in the game_stats ordered dict
    """
    game_stats.clear()

def print_error(message):
    """
    print_error(message) attempts to print an error message in red
    if running in idle, ----message---- is printed
    expects user to supply a string to print
    """
    if "idlelib" not in sys.modules:
        attr = []
        attr.append('31')
        sys.stdout.write('\n\033[91m' + message + '\033[0m\n')
    else:
        print('\n----' + message + '----\n')

def print_bold(message):
    """
    print_bold(message) attempts to print a message in bold
    if running in idle, _message_ is printed
    expects user to supply a string to print
    """
    if "idlelib" not in sys.modules:
        attr = []
        attr.append('31')
        sys.stdout.write('\n\033[1m' + message + '\033[0m\n')
    else:
        print('\n_' + message + '_')

#options maps the option chosen in present_menu to the relevent def
options = {
    '1': add_player,
    '2': play_game,
    '3': print_score,
    '4': clear_game
}

#continuously present menu and do function chosen
while True:
    present_menu()


import pandas as pd
import matplotlib.pyplot as plt
import datetime
plt.rcParams["font.sans-serif"] = ["SimHei"]

def month_and_day(x):
    '''
    横轴刻度标签精确到月日
    '''
    temp = []
    for one in x:
        temp.append(one.strftime("%m-%d"))
    return temp

def ytick(y):
    temp = []
    maxinum = y.max()

    if maxinum < 1000:
        increment = 100
        extra = 50
        i = 100
    elif maxinum < 2000:
        increment = 200
        i = 200
        extra = 100
    elif maxinum < 3000:
        increment = 300
        i = 300
        extra = 200
    elif maxinum < 4000:
        increment = 500
        i = 500
        extra = 400
    else:
        increment = 1000
        i = 1000
        extra = 800

    while i < maxinum:
        temp.append(i)
        i += increment

    temp.append(i)
    temp.append(int(y.min()))
    temp.append(int(maxinum)+50)
    return temp

def generate_color(y):
    '''
    每一个条形区域取定一个颜色
    '''
    teal = "#45a0a2" # 中间价格
    red = "#e87a59" # 最低价格
    blue_gray = "#4f6268" # 最高价格
    mininum = y.min()
    maxinum = y.max()
    temp = []
    for one in y:
        if one == mininum:
            temp.append(red)
        elif one == maxinum:
            temp.append(blue_gray)
        else:
            temp.append(teal)
    return temp

def top_price_list(x,y,date_range):
    '''
    在条形区域底部显示出价格
    空缺的价格标记为空缺，灰蓝色显示
    '''
    red = "#dc2624"
    grey = "#4f6268"
    color = []
    text = []
    price = []
    def get_price(someday):
        for i in range(x.shape[0]):
            if x[i].strftime("%Y-%m-%d") == someday.strftime("%Y-%m-%d"):
                return y[i]

    for d in date_range:
        if d in x:
            p = get_price(d)
            text.append("%.0d"%p)
            color.append(red)
            price.append(p)
        else:
            text.append("空缺")
            color.append(grey)
            price.append(0)
    return text,color,price

def generate_price_picture(x,y,saving_path):

    # 生成画布
    fig = plt.figure(figsize=(12,6),dpi=100)

    # 生成坐标轴
    ax = fig.add_subplot(1,1,1)

    # 绘出条形图
    color = generate_color(y)
    ax.bar(x,y,color=color,width=0.95)
    # 隐藏坐标轴的右边和上边的轴
    ax.spines["top"].set_visible(False)
    ax.spines["right"].set_visible(False)
    # 表示最低价格的水平线
    ax.axhline(y=y.min(),ls=":",c="#dc8018")
    ax.axhline(y=y.max(),ls=":",c="#dc8018")

    # 确定日期的范围
    incr = datetime.timedelta(days=1)
    ax.set_xlim([datetime.date.today()-incr,datetime.date.today()+incr*10])
    # 日期的刻度标签
    date_range = pd.date_range(datetime.date.today(),periods=10,freq="D",closed="left")
    yticklabels = month_and_day(date_range)
    ax.set_xticks(date_range)
    ax.set_xticklabels(yticklabels,fontdict={"size":20})

    # 柱形区域顶部显示价格信息
    texts,text_colors,prices = top_price_list(x,y,date_range)
    for i in range(date_range.shape[0]):
        ax.text(date_range[i],prices[i]+10,texts[i],color=text_colors[i],fontsize=18,horizontalalignment='center')

    # 设置价格标签刻度
    y_ticks = ytick(y)
    ax.set_yticks(y_ticks[0:-1])
    ax.set_yticklabels(y_ticks[0:-1])
    ax.set_ylim([0,y_ticks[-1]+50])
    for text in ax.get_yticklabels():
        if text.get_text() == str(int(y.min())):
            text.set(fontsize=16,color="#dc8018")
        else:
            text.set(fontsize=20)
    fig.savefig(saving_path,bbox_inches = 'tight')


"""
    Module For Making Fake CCDImage
    Author : Kim Tae Bum
    e-mail : phya.ktaebum@gmail.com
    Lastly Modified : 2017-06-17
"""

import numpy as np
from astropy.modeling.functional_models import Gaussian2D, Sersic2D
from ccdproc import CCDData

class FakeCCD():
    """
        Class of Fake CCD
    """

    def __init__(self, base=100, size=800, readout=10):
        """
        Constructor For Class
        :param base: Base Value of Sky Background
        :param size: Size of Mesh Grid(square)
        :param readout: Standard Deviation of Sky Background
                Recommended: readout ~= base / 10
        Class Members:
            1. x, y = pixel grid with size; default size = 800
            2. star = fake star map
            3. galaxy = fake galaxy map
            4. cr = fake cosmic rays map
            5. sky = fake sky background
            6. std = readout noise of sky background, default = 10
            7. header = header infor for CCDData
        """
        self.size = size
        self.x, self.y = np.mgrid[0:size, 0:size]
        self.star = np.zeros(shape=(size, size), dtype=np.float64)
        self.galaxy = np.zeros(shape=(size, size), dtype=np.float64)
        self.cr = np.zeros(shape=(size, size), dtype=np.float64)
        # Set Sky Background
        self.sky = np.random.normal(base, readout, size=(size, size))
        self.std = readout
        self.header = dict(name='Taebum\'s fake data',
                           size='{:d} by {:d}'.format(size, size),
                           readout=readout,
                           unit='ADU')

    def merge(self):
        """
        :return: Merged Data
        """
        return self.sky + self.galaxy + self.cr + self.star

    def getAll(self):
        """
        :return: Merged Data in CCDData object
        """
        header = self.header.copy()
        header['Data'] = 'sky, cosmic ray, galaxy, star'
        data = self.merge()
        return CCDData(header=header, data=data, unit='adu')

    def getSky(self):
        """
        :return: Just sky background
        """
        header = self.header.copy()
        header['Data'] = 'Sky'
        data = self.merge()
        return CCDData(header=header, data=data, unit='adu')

    def getStar(self):
        """
        :return: Get Only Star Map(include sky background)
        """
        header = self.header.copy()
        header['Data'] = 'sky, star'
        return CCDData(header=header, data=self.sky + self.star, unit='adu')

    def getGalaxy(self):
        """
        :return: Get Only Galaxy Map(include sky background)
        """
        header = self.header.copy()
        header['Data'] = 'sky, galaxy'
        return CCDData(header=header, data=self.sky + self.galaxy, unit='adu')

    def getCR(self):
        """
        :return: Get Only Cosmic Ray Map(in this case, do not include sky background, only pure cr)
        """
        header = self.header.copy()
        header['Data'] = 'Cosmic ray'
        data = self.cr.copy()
        data[data > 0] = 100
        return CCDData(header=header, data=data, unit='adu')

    def set_star(self, num):
        """
        Set num stars in ccd data
        Following Gaussian 2D Distribution
        :param num: number of fake stars
        :return:
        """
        for i in range(num):
            x = np.random.uniform(0, self.size)
            y = np.random.uniform(0, self.size)
            # std = np.random.uniform(3, 5)
            std = 1.5
            amp = np.random.uniform(5 * self.std, 10 * self.std)
            self.star += Gaussian2D.evaluate(self.x, self.y, amplitude=amp,
                                             theta=0, x_mean=x, y_mean=y, x_stddev=std,
                                             y_stddev=std)
        return

    def set_galaxy(self, num):
        """
        Set num galaxies in ccd data
        following Sersic 2D Profile
        :param num: number of fake galaxies
        :return:
        """
        for i in range(num):
            amp = np.random.uniform(0.5 * self.std, 5 * self.std)
            r_eff = np.random.uniform(0, self.size / 100)
            n = np.random.uniform(0.5, 10)
            x_c = np.random.uniform(0, self.size)
            y_c = np.random.uniform(0, self.size)
            eps = np.random.uniform(0, 0.5)
            theta = np.pi * np.random.random() - np.pi - 2
            galaxy = Sersic2D(amp, r_eff, n, x_c, y_c, eps, theta)
            self.galaxy += galaxy(self.x, self.y)
        return

    def set_cr(self, num, prob=0.2):
        """
        Set num Cosmic Rays in CCD Data
        There are two Types of Cosmic Rays
        1. Line Shape
        2. Dot Shape
        :param num: number of cosmic rays
        :param prob: probability of being line cosmic ray, default = 0.2
        :return:
        """
        for i in range(num):
            x = np.random.randint(0, self.size)
            y = np.random.randint(0, self.size)
            theta = np.pi * np.random.random() - np.pi / 2
            length = np.random.randint(int(self.size / 100), int(1.2 * self.size / 100))
            dice = np.random.random()
            if dice < prob:
                self.set_line_cr(x, y, theta, length)
            else:
                self.set_dot_cr(x, y)

        return

    def set_line_cr(self, x_s, y_s, theta, length):
        """
        :param x_s: start x point
        :param y_s: start y point
        :param theta: direction
        :param length: x pixel length
        :return:
        """
        value = np.random.uniform(5 * self.std, 100 * self.std)
        x_prev = x_s
        y_prev = y_s
        for i in range(length):
            x_s = x_prev + 1
            y_s = int(y_prev + np.tan(theta))
            if y_s < 0 or y_s >= self.size or x_s < 0 or x_s >= self.size:
                return
            self.cr[x_s, y_prev:y_s] = value
            x_prev = x_s
            y_prev = y_s

        return

    def set_dot_cr(self, x, y):
        """
        :param x: x center
        :param y: y centerget update
        :return:
        """
        value = np.random.uniform(5 * self.std, 100 * self.std)
        x_g, y_g = np.mgrid[x - 1:x + 2, y - 1:y + 2]
        sli = (x_g >= 0) & (x_g < self.size)
        sli = sli & (y_g >= 0) & (y_g < self.size)
        self.cr[x_g[sli], y_g[sli]] = value

    def analysis(self, masked):
        """
        Determine How LA Cosmic Correctly Masked Fake Cosmic Rays Via Comparison
        :param masked: masked cosmic rays
        :return:
        """
        mark = np.where(masked > 0)
        correct = 0
        as_star = 0
        as_gal = 0
        for i in range(len(mark[0])):
            if self.cr[mark[0][i], mark[1][i]] > 0:
                correct += 1
            else:
                if self.star[mark[0][i], mark[1][i]] >= self.galaxy[mark[0][i], mark[1][i]]:
                    as_star += 1
                else:
                    as_gal += 1
        print('Number of Cosmic Ray Pixels : {:d}'.format(len(np.where(self.cr > 0)[0])))
        print('Correctly Masked Pixels : {:d}'.format(correct))
        print('Incorrectly Masked, as Stars : {:d}'.format(as_star))
        print('Incorrectly Masked, as Galaxies : {:d}'.format(as_gal))


import csv
import os

add_header = input('Add a header?[y]/n >> ')
if not set_header or add_header == 'y':
    c = int(input('The number of columns: '))
    header = []
    for i in range(c):
        cname = input('column:{} >> '.format(i))
        header.append(cname)

path = input('Path: ')
delimiter = input('Delimiter type: ')
for file in os.listdir(path):
    base, ext = os.path.splitext(file)

    if ext == '.txt':
        with open(path + '/' + file, 'r') as ftxt, open(path + '/' + base + '.csv', 'x', newline='') as fcsv:
            reader = csv.reader(ftxt, delimiter=delimiter, skipinitialspace=True)
            writer = csv.writer(fcsv)
            writer.writerow(header)
            writer.writerows(reader)


from bs4 import BeautifulSoup as bs
from requests import get
import pandas as pd
from selenium import webdriver
from time import sleep

def get_chromedriver():
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--disable-notifications')
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    driver = webdriver.Chrome(chrome_options=chrome_options)

    return driver

def scrape(file):
    links = pd.read_csv(file, index_col=False, encoding = "utf-8", header=None)
    browser = get_chromedriver()

    for url in links[0]:
        try:
            with open('tokopedia(toko).csv', 'r', encoding='utf-8') as fp:
                s = fp.read()

        except:
            s = '$$$$$$'

        with open('tokopedia(toko).csv', 'a+', encoding='utf-8') as f:
            print('\n')
            print('-' * (len(url) + 14))
            print('Memproses url ' + url)
            print('-' * (len(url) + 14))
            print('\n')

            page = 1
            while True:

                browser.get(url + '/page/' + str(page))
                sleep(3)
                soup = bs(browser.page_source,'lxml')

                if soup.find_all('div','product') == []:
                    break

                print('*' * (len(str(page)) + 5))
                print('\n\nPage ' + str(page) + '\n\n')
                print('*' * (len(str(page)) + 5))

                for item in soup.find_all('div','product'):
                    while True:
                        if item.find('a').get('href') not in s:
                            data = {}

                            print('Loading...')

                            try:
                                item_html = get(item.find('a').get('href')).text
                                item_soup = bs(item_html,'lxml')
                            except:
                                continue

                            print('Done ' + item_soup.find('span',{'itemprop':'name'}).get_text().strip())

                            try:
                                item_soup.find('div','rvm-button--buy')
                            except:
                                break

                            data['Url Produk'] = item.find('a').get('href')

                            data['Nama Produk'] = item_soup.find('span',{'itemprop':'name'}).get_text().strip()

                            data['Status'] = 'Ready'

                            try:
                                category = item_soup.find('ul','breadcrumb')
                                if len(category.find_all('h2')) == 4:
                                    data['Kategori'] = category.find_all('h2')[0].get_text().strip()
                                    data['Sub Kategori 1'] = category.find_all('h2')[1].get_text().strip()
                                    data['Sub Kategori 2'] = category.find_all('h2')[2].get_text().strip()
                                elif len(category.find_all('h2')) == 3:
                                    data['Kategori'] = category.find_all('h2')[0].get_text().strip()
                                    data['Sub Kategori 1'] = category.find_all('h2')[1].get_text().strip()
                                    data['Sub Kategori 2'] = None
                                elif len(category.find_all('h2')) == 2:
                                    data['Kategori'] = category.find_all('h2')[0].get_text().strip()
                                    data['Sub Kategori 1'] = None
                                    data['Sub Kategori 2'] = None
                            except AttributeError:
                                data['Kategori'] = None
                                data['Sub Kategori 1'] = None
                                data['Sub Kategori 2'] = None

                            data['Harga (Rp)'] = item_soup.find('span',{'itemprop':'price'}).get('content')

                            try:
                                data['Berat'] = item_soup.find('div','rvm-shipping-content').get_text().strip()
                            except:
                                data['Berat'] = None

                            data['Kondisi'] = item_soup.find_all('div','rvm-product-info--item_value mt-5')[0].get_text()

                            data['Asuransi'] = item_soup.find_all('div','rvm-product-info--item_value mt-5')[2].get_text()

                            data['Lokasi Toko'] = item_soup.find('div','rvm-merchat-city mt-10').get_text().strip()

                            data['Deskripsi'] = str(item_soup.find('div',{'id':'info'})).replace('
\n                                ','').replace('\n
','').replace('
', 'New Line (CHAR10)').replace('<', '<').replace('>', '>').replace('&', '&').replace('"', '"').replace('"', '"').replace(''', "'").replace(''', "'").replace('
', '')

                            try:
                                data['Url Gambar 1'] = item_soup.find('div','thumbnail-img__holder').find_all('img')[0].get('src')
                            except:
                                data['Url Gambar 1'] = None
                            try:
                                data['Url Gambar 2'] = item_soup.find('div','thumbnail-img__holder').find_all('img')[1].get('src')
                            except:
                                data['Url Gambar 2'] = None
                            try:
                                data['Url Gambar 3'] = item_soup.find('div','thumbnail-img__holder').find_all('img')[2].get('src')
                            except:
                                data['Url Gambar 3'] = None
                            try:
                                data['Url Gambar 4'] = item_soup.find('div','thumbnail-img__holder').find_all('img')[3].get('src')
                            except:
                                data['Url Gambar 4'] = None
                            try:
                                data['Url Gambar 5'] = item_soup.find('div','thumbnail-img__holder').find_all('img')[4].get('src')
                            except:
                                data['Url Gambar 5'] = None

                            pd.DataFrame([data]).to_csv(f, index=False, columns=['Url Produk','Nama Produk','Status','Kategori','Sub Kategori 1',
                            'Sub Kategori 2','Berat','Kondisi','Asuransi','Lokasi Toko','Deskripsi','Harga (Rp)',
                            'Url Gambar 1','Url Gambar 2','Url Gambar 3','Url Gambar 4','Url Gambar 5'], header=False, encoding='utf-8')

                        break

                page += 1

    browser.quit()
    print('\n\n--Proses selesai--')

scrape('links.csv')


# -*- coding: utf-8 -*-
# https://ac.nowcoder.com/acm/contest/49/G
inf = 0x3f3f3f3f
maxn = 501
dirx = [0, 1, -1, 0]
diry = [1, 0, 0, -1]
global n, m

def init():
    global flag
    flag = True
    global maze
    maze = []
    global sx, sy, ex, ey
    for i in range(n):
        maze.append(input())
        for j in range(m):
            if maze[i][j] == 'S':
                sx = i
                sy = j
            elif maze[i][j] == 'E':
                ex = i
                ey = j

def cal(sx, sy):
    global flag
    queue = []
    queue.append((sx, sy))
    while queue:
        q = queue[0]
        if q[0] == ex and q[1] == ey:
            print('Yes')
            flag = False
            break
        queue.pop(0)
        for i in range(4):
            nx = q[0] + dirx[i]
            ny = q[1] + diry[i]
            if 0 <= nx < n and 0 <= ny < m:
                if maze[nx][ny] != '#':
                    queue.append((nx, ny))
                    maze[nx] = list(maze[nx])
                    maze[nx][ny] = '#'
                    maze[nx] = ''.join(maze[nx])

    if flag:
        print('No')

if __name__ == '__main__':
    try:
        while True:
            n, m = map(int, input().split())
            init()
            cal(sx, sy)
    except EOFError:
        pass


class HtmlOutputer(object):

    def collect_data(self, movie_data):
        if movie_data is None:
            return
        fout = open('output.html', 'a+')
        for data in movie_data:
            print
            data['name'] + '|', data['rate'] + '|', data['actor'], '\n'
            fout.write('%s,' % data['name'].encode('utf-8'))
        fout.write('%s,' % data['rate'])
        fout.write('%s\n' % data['actor'].encode('utf-8'))
        fout.close()


from django.shortcuts import render
import json, urllib2
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect
from django.core.context_processors import csrf
# Create your views here.
from slider.models import *

def home(request):
	c = {}
	return render(request, 'home.html',c)
def slider(request, slider_id):
	c = {}
	slider = Slider.objects.get(id=int(slider_id))
	c['slider'] = json.dumps(slider.slides_as_json())
	return render(request, 'slider.html', c)

@login_required
def sliders(request):
	c = {}
	c['sliders'] = Slider.objects.filter(user=request.user)
	return render(request, 'sliders.html',c)
@login_required
def slider_create(request):
	if request.method == 'POST':
		d = request.POST['data']
		slides = json.loads(d)['slides']
		slider = Slider(user=request.user)
		slider.save()
		i = 0
		for slide in slides:
			s = Slide(image=slide['img'], headeing=slide['heading'], slide_id=i, slider=slider)
			i += 1
			s.save()
		return HttpResponseRedirect('/slider/'+str(slider.id))
	c = {}
	c.update(csrf(request))
	return render(request, 'create_slider.html', c)

def get_rand_image(request):
	d = urllib2.urlopen("http://www.splashbase.co/api/v1/images/random").read()
	d = json.loads(d)
	return HttpResponse(d['url'])

def error404(request):
	return render(request, '404.html', {})


# -*- coding: utf-8 -*-
from django import template

register = template.Library()

# value Exper_arg_value, arg id_expersubst
#only one point by expersubst
@register.filter
def get_points_from_arg_by_expersubst(value, arg):
    point = value.exper_func_points.filter(exper_subst__pk=arg)
    if len(point) > 0:
        return point[0].func_val
    else:
        return '0.0'

#register.filter('get_points_from_arg_by_expersubst', get_points_from_arg_by_expersubst)


"""Unit tests for the Vector3 class"""

import unittest

from Vector3 import Vector3
from Matrix import Matrix

class TestVector3(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def testCreation(self):

        testVector = Vector3(-39, 1.0, 879)

        self.assertTrue(testVector.x == -39.0)
        self.assertTrue(testVector.y == 1.0)
        self.assertTrue(testVector.z == 879)

    def testDot(self):

        testVector1 = Vector3(5, 4, 3)
        testVector2 = Vector3(.5, 2, 1)

        dot1_2 = testVector1.dot(testVector2)
        dot2_1 = testVector2.dot(testVector1)

        self.assertTrue(dot1_2 == 13.5)
        self.assertTrue(dot2_1 == 13.5)

    def testCross(self):

        testVector1 = Vector3(.12, 1.3, .97)
        testVector2 = Vector3(-1.2, -.2, 3.0)

        testVector3 = testVector1.cross(testVector2)

        self.assertTrue(testVector3.x == 4.094 and testVector3.y == -1.524 and testVector3.z == 1.536)

    def testTransform(self):

        testVector1 = Vector3(8.2, 1.524, 1.5)

        testMatrix = Matrix()
        testMatrix.setValue(3, 3, 1)
        testMatrix.setValue(2, 2, 3)
        testMatrix.setValue(1, 1, -1)
        testMatrix.setValue(0, 0, 2)

        testVector2 = testVector1.transform(testMatrix)

        self.assertTrue(testVector2.x == 16.4)
        self.assertTrue(testVector2.y == -1.524)
        self.assertTrue(testVector2.z == 4.5)

    def testAddition(self):

        testVector1 = Vector3(1, 2, 3)
        testVector2 = Vector3(-1, -2, -3)

        testVector3 = testVector1 + testVector2
        testVector4 = testVector2 + testVector1

        self.assertTrue(testVector3.x == 0 and testVector3.y == 0 and testVector3.z == 0)
        self.assertTrue(testVector4.x == 0 and testVector4.y == 0 and testVector4.z == 0)

    def testSubtraction(self):

        testVector1 = Vector3(1, 2, 3)
        testVector2 = Vector3(-1, -2, -3)

        testVector3 = testVector1 - testVector2
        testVector4 = testVector2 - testVector1

        self.assertTrue(testVector3.x == 2 and testVector3.y == 4 and testVector3.z == 6)
        self.assertTrue(testVector4.x == -2 and testVector4.y == -4 and testVector4.z == -6)

def testSuite():
    return unittest.TestLoader().loadTestsFromTestCase(TestVector3)


from django.conf.urls import url, include
from .views import *
from rest_framework.routers import DefaultRouter
from rest_framework_swagger.views import get_swagger_view

schema_view = get_swagger_view(title='Streamer API')

router = DefaultRouter()
router.register('artists', ArtistViewSet)
router.register('albums', AlbumViewSet)
router.register('tracks', TrackViewSet)
router.register('playlists', PlaylistViewSet)

urlpatterns = [
    url(r'^', include(router.urls)),
    url(r'^auth/', include('rest_auth.urls')),
    url(r'^auth/facebook/', FacebookLogin.as_view()),
    url(r'^auth/twitter/', TwitterLogin.as_view()),
    url(r'^register/', include('rest_auth.registration.urls')),
    url(r'^docs/', schema_view)
]


from pyspark.ml.wrapper import JavaEstimator, JavaModel
from pyspark.ml.param.shared import *
from pyspark.mllib.common import inherit_doc
from pyspark.ml.util import keyword_only

@inherit_doc
class KNNRegression(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol,
                    HasInputCols, HasThresholds, HasSeed, HasWeightCol):
    @keyword_only
    def __init__(self, featuresCol="features", labelCol="label", predictionCol="prediction",
                 seed=None, topTreeSize=1000, topTreeLeafSize=10, subTreeLeafSize=30, bufferSize=-1.0,
                 bufferSizeSampleSize=list(range(100, 1000 + 1, 100)), balanceThreshold=0.7,
                 k=5, neighborsCol="neighbors", maxNeighbors=float("inf")):
        super(KNNRegression, self).__init__()
        self._java_obj = self._new_java_obj(
            "org.apache.spark.ml.regression.KNNRegression", self.uid)

        self.topTreeSize = Param(self, "topTreeSize", "number of points to sample for top-level tree")
        self.topTreeLeafSize = Param(self, "topTreeLeafSize",
                                     "number of points at which to switch to brute-force for top-level tree")
        self.subTreeLeafSize = Param(self, "subTreeLeafSize",
                                     "number of points at which to switch to brute-force for distributed sub-trees")
        self.bufferSize = Param(self, "bufferSize",
                                "size of buffer used to construct spill trees and top-level tree search")
        self.bufferSizeSampleSize = Param(self, "bufferSizeSampleSize",
                                          "number of sample sizes to take when estimating buffer size")
        self.balanceThreshold = Param(self, "balanceThreshold",
                                      "fraction of total points at which spill tree reverts back to metric tree if "
                                      "either child contains more points")
        self.k = Param(self, "k", "number of neighbors to find")
        self.neighborsCol = Param(self, "neighborsCol", "column names for returned neighbors")
        self.maxNeighbors = Param(self, "maxNeighbors", "maximum distance to find neighbors")

        self._setDefault(topTreeSize=1000, topTreeLeafSize=10, subTreeLeafSize=30, bufferSize=-1.0,
                         bufferSizeSampleSize=list(range(100, 1000 + 1, 100)), balanceThreshold=0.7,
                         k=5, neighborsCol="neighbors", maxNeighbors=float("inf"))

        kwargs = self.__init__._input_kwargs
        self.setParams(**kwargs)

    @keyword_only
    def setParams(self, featuresCol="features", labelCol="label", predictionCol="prediction",
                  seed=None, topTreeSize=1000, topTreeLeafSize=10, subTreeLeafSize=30, bufferSize=-1.0,
                  bufferSizeSampleSize=list(range(100, 1000 + 1, 100)), balanceThreshold=0.7,
                  k=5, neighborsCol="neighbors", maxNeighbors=float("inf")):
        kwargs = self.setParams._input_kwargs
        return self._set(**kwargs)

    def _create_model(self, java_model):
        return KNNRegressionModel(java_model)

class KNNRegressionModel(JavaModel):
    """
    Model fitted by KNNRegression.
    """
    def __init__(self, java_model):
        super(KNNRegressionModel, self).__init__(java_model)

        # note: look at https://issues.apache.org/jira/browse/SPARK-10931 in the future
        self.bufferSize = Param(self, "bufferSize",
                                "size of buffer used to construct spill trees and top-level tree search")
        self.k = Param(self, "k", "number of neighbors to find")
        self.neighborsCol = Param(self, "neighborsCol", "column names for returned neighbors")
        self.maxNeighbors = Param(self, "maxNeighbors", "maximum distance to find neighbors")

        self._transfer_params_from_java()


from flask import Flask, request, jsonify, make_response, render_template
import gva.logging
from gva.data.readers import Reader, FileReader
from gva.data.formats import dictset
import datetime

app = Flask(__name__)

logger = gva.logging.get_logger()
logger.setLevel(10)

def stringify(pager):
    result = []
    page = next(pager, [])
    for item in page:
        result.append(item.decode())
    return ''.join(result)

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html')

@app.route('/search', methods=['GET'])
@app.route('/search/<query>/<start_date>/<end_date>/', methods=['GET'])
def search_query(query=None, start_date=None, end_date=None):

    def search_term(term):
        if term is None or len(term) == 0:
            return None
        return lambda x: term in x['tweet'].lower()

    def date_term(term):
        print(term, datetime.date.fromisoformat(term))
        if term is None or len(term) == 0:
            return None
        return datetime.date.fromisoformat(term)

    r = Reader(
        #fork_processes=True,
        #thread_count=4,
        reader=FileReader,
        from_path='data/%datefolders/',
 #       data_format='text',
        start_date=date_term(start_date),
        end_date=date_term(end_date),
        #select=['username'],
        where=search_term(query.lower())
)

    pages = dictset.page_dictset(r, 25)
    page = next(pages, [])

    return jsonify(list(page))

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=2100, debug=True)  # nosec


#!/usr/bin/env python3
'''
Author: Zhipeng Xie

Topic:

Effect:

'''

from tkinter import *
import tkinter.messagebox as messagebox
class test(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

    def createWidgets(self):
        self.helloLabel = Label(self, text='Who are you, my friend?')
        self.helloLabel.pack()
        self.nameInput = Entry(self)
        self.nameInput.pack()
        self.alertButton = Button(self, text='Confirm', command=self.hello)
        self.alertButton.pack()
        self.quitButton = Button(self, text='Quit', command=self.quit)
        self.quitButton.pack()

    def hello(self):
        name = self.nameInput.get() or 'Blank'
        messagebox.showinfo('Welcome', 'Hello, %s' % name)

app = test()
app.master.title('Hi')
app.mainloop()


lijst1 = eval(input("Geef lijst met minimaal 10 strings: "))
lijst2 = []

for word in lijst1:
    if len(word) == 4:
        lijst2.append(word)

print('De nieuw-gemaakte lijst met alle vier-letter strings is:' + str(lijst2))


import os
from glob import glob
from ConfigParser import ConfigParser

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

class Settings(object):
    def __init__(self):

        self.confs = glob("%s/config/*.conf" % PROJECT_ROOT)
        self.__parse_config()
        self.__join_config()

    def __parse_config(self):
        config_parse = ConfigParser()
        config_parse.read(self.confs)
        self.config_parse = config_parse

    def __join_config(self):
        conf = ConfigParser()
        conf.read(['%s/node.conf' % PROJECT_ROOT])
        for section_name in conf.sections():
            for name,value in conf.items(section_name):
                self.config_parse.set(section_name,name,value)

    def get_item(self,section,field):
        try :
            value = self.config_parse.get(section,field)
            if value.isdigit() :
                return int(value)
            else :
                return value
        except :
            return None


import yaml
import requests
import os
from slugify import slugify

class _DataObject(object):

	def __getattr__(self, k):
		return self.data.get(k)

class SiteCollection(object):

	def __init__(self, directory):
		self.sites = []
		for site_file in os.listdir(directory):
			with open(os.path.join(directory, site_file), 'rb') as fh:
				site = Site(yaml.load(fh))
				self.sites.append(site)

	def get(self, slug):
		for site in self.sites:
			if site.slug == slug:
				return site

	def __iter__(self):
		return self.sites.__iter__()

	def to_dict(self):
		return {'sites': self.sites}

class Filter(_DataObject):

	def __init__(self, site, data):
		self.site = site
		self.data = data
		self.default = unicode(data.get('default'))
		self._values = None

	@property
	def values(self):
		if self._values is None:
			field = self.data.get('field')
			dimension, attribute = field, None
			if '.' in dimension:
				dimension, attribute = dimension.split('.', 2)

			res = requests.get('https://openspending.org/api/2/aggregate',
				params={'dataset': self.site.dataset, 'drilldown': field})

			self._values = []
			for drilldown in res.json().get('drilldown'):
				v = drilldown.get(dimension)
				if isinstance(v, dict):
					if not attribute:
						self.data['field'] = field + '.label'
					v = v.get(attribute or 'label')
				self._values.append(v)
			self._values = list(sorted(self._values))
		return self._values

	@property
	def class_name(self):
		_ = self.values
		return self.field.replace('.', ' ')

	def to_dict(self):
		values = self.values
		data = self.data.copy()
		data['slug'] = self.slug
		data['values'] = values
		return data

class Site(_DataObject):

	def __init__(self, data):
		self.data = data
		self.slug = slugify(data.get('slug', data.get('name')))
		self.filters = [Filter(self, d) for d in data.get('filters', [])]

	def to_dict(self):
		data = self.data.copy()
		data['slug'] = self.slug
		data['filters'] = self.filters
		return data

def load_sites(app):
	return SiteCollection(app.config['SITES_FOLDER'])


from item import Item, Weapon, Shield, Armour, Treasure, Lightsource
from items import items
from player import Monster

# Initialize all the monsters
slime = Monster()
slime.name = 'Slime'
slime.description = 'Slimey slime'
slime.attack = 5
slime.exp = 10
slime.gold = 10
slime.hp = 50
slime.inventory = [items['Book']]
slime.dead = False

bat = Monster()
bat.name = 'Bat'
bat.description = 'A dark nat'
bat.attack = 5
bat.exp = 10
bat.gold = 10
bat.hp = 50
bat.inventory = []
bat.dead = False

monsters = {'Slime': slime, 'Bat': bat}


import sys
import numpy as np
import datetime
import os

stops= ["101S", "103S", "112S", "115S", "119S", "120S", "124S", "127S", "137S", "142S", "142N", "137N", "127N", "125N", "120N", "115N", "112N", "104N", "103N", "101N",
"201S", "213S", "221S", "224S", "120S", "127S", "132S", "137S", "235S", "239S", "247S", "247N","239N","235N","137N","132N","127N","120N","224N","221N","213N","201N"]
day_in_month=(31,28,22,30,31,30,31,31,30,31,30,31)
for month in range(1, 3):
	for day in range(1, day_in_month[month-1]+1):
		if(datetime.date(2018, month, day).weekday()>4):
			continue
		for stop_i in range(len(stops)):
			schedule= []
			s= stops[stop_i]
			s_str=[]
			if(stop_i< 20):
				a= open("a/1/"+ s+ ".txt").read()
			else:
				a= open("a/2/"+ s+ ".txt").read()
			for i in range(len(a)):
				if(a[i: i+2]== "['" or a[i: i+2]== " '"):
					temp= int(a[i+2:i+4])*60+int(a[i+5:i+7])
					s_str.append(a[i+2:i+7])
					schedule.append(temp)

			real_time= []
			if(stop_i< 20 and os.path.isfile("b/1/2018_"+str(month)+"_"+str(day)+"_"+s+".txt")== False):
				continue
			elif(stop_i>= 20 and os.path.isfile("b/2/2018_"+str(month)+"_"+str(day)+"_"+s+".txt")== False):
				continue
			else:
				if(stop_i< 20):
					b= open("b/1/2018_"+str(month)+"_"+str(day)+"_"+s+".txt").read()
				else:
					b= open("b/2/2018_"+str(month)+"_"+str(day)+"_"+s+".txt").read()
			for i in range(0, len(b),8):
				temp= int(b[i:i+2])*60+int(b[i+3:i+5])
				real_time.append(temp)
			real_time= np.sort(real_time)

			num=0
			delay=[]
			for i in range(len(real_time)):
				while((num+1)< len(schedule) and schedule[num+1]<= real_time[i]):
					num+=1
				if((num+1)>= len(schedule) or (real_time[i]- schedule[num])<(schedule[num+1]- real_time[i])):
					string= str(s_str[num]+": "+str(real_time[i]- schedule[num])+"\n")
				else:
					string= str(s_str[num]+": "+str(-1*(schedule[num+1]- real_time[i]))+"\n")
				if(stop_i< 20):
					with open("delay/1/2018_"+str(month)+"_"+str(day)+"_"+s+".txt","a") as f:
						f.write(string)
				else:
					with open("delay/2/2018_"+str(month)+"_"+str(day)+"_"+s+".txt","a") as f:
						f.write(string)


from __future__ import absolute_import
import json
from json.decoder import JSONDecodeError

from .base import BaseReceiver

__all__ = ["Wal2JsonReceiver"]

class Wal2JsonReceiver(BaseReceiver):
    def __init__(self, *args, **kwargs):
        super(Wal2JsonReceiver, self).__init__(*args, **kwargs)
        self.is_parsing_chunks = False
        for option_key, option_value in self.options:
            if option_key == "write-in-chunks":
                self.is_parsing_chunks = option_value == "t"
        self.current_chunk = ""

    @classmethod
    def from_config(cls, config):
        opts = []
        for k in (
            "include-xids",
            "include-timestamp",
            "include-schemas",
            "include-types",
            "include-typmod",
            "include-type-oids",
            "include-not-null",
            "pretty-print",
            "include-lsn",
            "filter-tables",
            "add-tables",
            "write-in-chunks",
            "include-unchanged-toast",
        ):
            if k in config:
                v = config.pop(k)
                opts.append((k, (v and "t" or "f")))

        return cls(options=opts)

    def process_payload(self, raw_payload):
        payload = raw_payload.decode("utf-8")
        self.logger.debug(
            "next chunk payload:\n\t%s%s",
            payload[:300],
            len(payload) > 300 and "..." or "",
        )
        if self.is_parsing_chunks:
            self.current_chunk += payload
            try:
                parsed_payload = json.loads(self.current_chunk)
                self.current_chunk = ""
            except JSONDecodeError:
                return
        else:
            parsed_payload = json.loads(payload)
        for change in parsed_payload["change"]:
            self.message_cb(change)


##
## EPITECH PROJECT, 2020
## Zappy
## File description:
## socket
##

import socket
import sys

from ai.src.requests import sendRequest
from ai.src.error import *

def initSocket(params):
    try:
        mainsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        mainsock.connect((params['machine'], params['port']))
        sendRequest("")
        mainsock.recv(1024)
        return mainsock
    except:
        if params["debug"]:
            print("Can't connect to server")
        return None


import pickle
from sklearn.manifold import TSNE

data = pickle.load(open('pickles/vectorlists.pickle', 'rb'))
data = data[1:]

for i in range(len(data)):
	data[i] = data[i][1:]

model = TSNE(learning_rate = 100, n_components = 3, n_iter=2000)
transformed = model.fit_transform(data)

#saving in pickle
with open('pickles/TSNE-trained.pickle', 'wb') as f:
	pickle.dump(transformed, f)

print ("Dataset trained for TSNE model")


#
# items/urls.py
#
# SVN/CVS Keywords
#----------------------------------
# $Author: cnobile $
# $Date: 2014-01-16 21:17:19 -0500 (Thu, 16 Jan 2014) $
# $Revision: 87 $
#----------------------------------

from django.conf.urls import patterns, include, url

urlpatterns = patterns('inventory.apps.items.views',
    url(r'^$', 'frontPage'),
    url(r'^lookup/regions/', 'processRegion'),
)


# Shira Yair 315389759
# Erez Weiss 311130215
import numpy as np
import sys

PICTURE_SIZE = 784
NUMBER_OF_LABELS = 10
HIDDEN_LAYER_SIZE = 100
EPOCHS = 10
ETA = 0.005

RelU = lambda z: np.maximum(0, z)

def predict_on_validation(params, active_function, validation_x, validation_y):
    sum_loss = 0.0
    good = 0.0
    for x, y in zip(validation_x, validation_y):
        x = np.reshape(x, (1, PICTURE_SIZE))
        # get vector of probabilities as a result , where index y is the probability that x is classified
        # as tag y
        fprop_cache = forwardPass(params, active_function, x, y)
        y_predict = fprop_cache["y_predict"]
        loss = loss_function(y, y_predict)
        sum_loss += loss
        max_index_array = y_predict.argmax(axis=0)
        if int(y) == max_index_array[0]:
            good += 1
    acc = good / len(validation_x)
    avg_loss = sum_loss / len(validation_x)
    return avg_loss, acc

def softmax(w, h, b):
    result_vec = np.zeros((NUMBER_OF_LABELS, 1))
    dominator = 0
    for j in range(len(w)):
        dominator += np.exp(np.dot(w[j], h) + b[j])
    for i in xrange(NUMBER_OF_LABELS):
        result_vec[i] = np.exp(np.dot(w[i], h) + b[i]) / dominator
    return result_vec

def forwardPass(params, relu, x, y):
    # extract initialize parameters
    W1, b1, W2, b2 = [params[key] for key in ('W1', 'b1', 'W2', 'b2')]
    # entering the *hidden* layer
    z1 = np.dot(W1, np.transpose(x)) + b1
    h1 = relu(z1)
    # entering the *final* layer
    z2 = np.dot(W2, h1) + b2
    # y_predict is 10x1 matrix.
    y_predict = softmax(W2, h1, b2)
    ret = {'x': x, 'y': y, 'z1': z1, 'h1': h1, 'z2': z2, 'y_predict': y_predict}
    # including initialize parameters in "ret" struct.
    for key in params:
        ret[key] = params[key]
    return ret

def loss_function(y, y_predict):
    return -np.log(y_predict[int(y)])

def validation_and_results(finalParams, relu, validX, validY, sumLoss):
    val_loss, acc = predict_on_validation(finalParams, relu, validX, validY)
    print ("Test avg loss: {0}".format(sumLoss / len(train_x)))
    print("Validation set results: avg loss: {0}, acc: {1}%".format(val_loss, acc * 100))

def relu_gradient(z):
    z[z <= 0] = 0
    z[z > 0] = 1
    return z

def update_weights_sgd(params, gradients, learning_rate):
    W1, b1, W2, b2 = [params[key] for key in ('W1', 'b1', 'W2', 'b2')]
    grad_w1,grad_b1,grad_w2,grad_b2 = [gradients[key] for key in ('W1', 'b1', 'W2', 'b2')]
    W1 = W1 - (learning_rate*grad_w1)
    W2 = W2 - (learning_rate * grad_w2)
    b1 = b1 - (learning_rate * grad_b1)
    b2 = b2 - (learning_rate * grad_b2)
    return {'b1': b1, 'W1': W1, 'b2': b2, 'W2': W2}

def backPropagation(fprop_cache):
    x, y, z1, h1, z2, y_predict = [fprop_cache[key] for key in ('x', 'y', 'z1', 'h1', 'z2', 'y_predict')]
    one_vec = np.zeros((NUMBER_OF_LABELS, 1))
    one_vec[int(y)] = 1
    dz2 = (y_predict - one_vec)  # dL/dz2
    dW2 = np.dot(dz2, h1.T)  # dL/dz2 * dz2/dw2
    db2 = dz2  # dL/dz2 * dz2/db2
    dz1 = np.dot(fprop_cache['W2'].T,
                 (y_predict - one_vec)) * relu_gradient(z1)  # dL/dz2 * dz2/dh1 * dh1/dz1
    dW1 = np.dot(dz1, x)  # dL/dz2 * dz2/dh1 * dh1/dz1 * dz1/dw1
    db1 = dz1  # dL/dz2 * dz2/dh1 * dh1/dz1 * dz1/db1
    return {'b1': db1, 'W1': dW1, 'b2': db2, 'W2': dW2}

def shuffle(train_x, train_y):
    s = np.arange(train_x.shape[0])
    np.random.shuffle(s)
    train_x = train_x[s]
    train_y = train_y[s]
    return [train_x, train_y]

def train(params, epochs, relu, learning_rate, train_x, train_y):
    for i in xrange(epochs):
        print ("Epoch no. {0}".format(i + 1))
        sum_loss = 0.0
        train_x, train_y = shuffle(train_x, train_y)
        for x, y in zip(train_x, train_y):
            x = np.reshape(x, (1, PICTURE_SIZE)) # from 1x784 --> 784x1
            # fprop_cache is matrix of probabilities 1x10
            fprop_cache = forwardPass(params, relu, x, y)
            y_predict = fprop_cache["y_predict"]
            loss = loss_function(y, y_predict)
            sum_loss += loss
            # gradients for each parameter
            gradients = backPropagation(fprop_cache)
            # update the parameters
            params = update_weights_sgd(params, gradients, learning_rate)
    return params, sum_loss

def test(test_x, params):
    with open("test_y", 'w') as test_pred_file:
        prediction_list = []
        for x in test_x:
            x = np.reshape(x, (1, PICTURE_SIZE))
            fprop_cache = forwardPass(params, RelU, x, None)
            y_predict = fprop_cache["y_predict"]
            index = y_predict.argmax(axis=0)
            prediction_list.append(str(index[0]))
        test_pred_file.write('\n'.join(prediction_list))

train_x = np.loadtxt("train_x")
# valid_x = np.loadtxt("valid_x_1000")
train_y = np.loadtxt("train_y")
# valid_y = np.loadtxt("valid_y_1000")
test_x = np.loadtxt("test_x")

train_x = train_x / 255
# valid_x = valid_x / 255
test_x = test_x / 255

# Defining hyper-parameters
w1 = np.random.uniform(-0.08, 0.08, [HIDDEN_LAYER_SIZE, PICTURE_SIZE])
b1 = np.random.uniform(-0.08, 0.08, [HIDDEN_LAYER_SIZE, 1])
w2 = np.random.uniform(-0.08, 0.08, [NUMBER_OF_LABELS, HIDDEN_LAYER_SIZE])
b2 = np.random.uniform(-0.08, 0.08, [NUMBER_OF_LABELS, 1])
eta = ETA
epochs = EPOCHS
params = {"W1": w1, "b1": b1, "W2": w2, "b2": b2}

final_params, sum_loss = train(params, EPOCHS, RelU, ETA, train_x, train_y)
#validation_and_results(final_params, RelU, valid_x, valid_y, sum_loss)
test(test_x, final_params)


#import itertools
from itertools import islice

def fib():
    prev, curr = 0, 1
    while True:
        yield curr
        prev, curr = curr, curr + prev

def main():
    f = fib()
    print(list(islice(f, 0, 20)))

if __name__ == '__main__':
    main()


import unittest

from Home.Guidance.FlightPathFile import FlightPath

"""
Meter2Feet = 3.2808  # one meter equals 3.28 feet
Meter2NauticalMiles = 0.000539956803  # One Meter = 0.0005 nautical miles
NauticalMiles2Meter = 1852
"""
Knots2MetersPerSecond = 0.514444444

class TestFlightPathFile(unittest.TestCase):

    def setUp(self) -> None:
        route_a320_charles_de_gaulle_lisbonne = \
            'ADEP/LFPG/26R-LAIGLE-ROLEN-PEPON-KURIS-TERPO-ERIGA-INBAB-ATLEN-' \
            'DEVAR-ASTURIAS-KUVAN-BISMU-BARKO-FATIMA-ADES/LPPT/03'
        self.str_route = route_a320_charles_de_gaulle_lisbonne

    def test_flight_path(self):
        flight_path = FlightPath(
            route=self.str_route,
            aircraftICAOcode='A320',
            RequestedFlightLevel=330,
            cruiseMach=0.82,
            takeOffMassKilograms=68000.0,
            windSpeedMetersPerSecond=25*Knots2MetersPerSecond,
            windDirectionDegrees=25
        )

        expectation = []

        self.assertEqual(flight_path, expectation)

    def test_flight_path_compute_flight(self):
        flight_path = FlightPath(
            route=self.str_route,
            aircraftICAOcode='A320',
            RequestedFlightLevel=330,
            cruiseMach=0.82,
            takeOffMassKilograms=68000.0,
            windSpeedMetersPerSecond=25*Knots2MetersPerSecond,
            windDirectionDegrees=25
        )

        # If we want to generate the whole flight using the old code, use the .computeFlight function
        flight_path.computeFlight(deltaTimeSeconds=1.0)
        flight_path.createFlightOutputFiles()

    def test_flight_path_fly(self):
        flyAndGenerateConfig = FlightPath(
            route=self.str_route,
            aircraftICAOcode='A320',
            RequestedFlightLevel=330,
            cruiseMach=0.82,
            takeOffMassKilograms=68000.0,
            windSpeedMetersPerSecond=25 * Knots2MetersPerSecond,
            windDirectionDegrees=25
        )

        flyAndGenerateConfig.fly()

    def test_flight_path_simulate_fly(self):
        updateFlightPath = FlightPath(
            route=self.str_route,
            aircraftICAOcode='A320',
            RequestedFlightLevel=330,
            cruiseMach=0.82,
            takeOffMassKilograms=68000.0,
            windSpeedMetersPerSecond=25 * Knots2MetersPerSecond,
            windDirectionDegrees=25
        )

        # fly from index 4 for the new route
        updateFlightPath.simulateFly(4, self.str_route)
        # fly from index 4 for the new route, with speed 260 and altitude 10000
        updateFlightPath.simulateFly(4, self.str_route, 260, 10000.0)

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


import os

from werkzeug import security
from flask import json

class Config:
    SECRET_KEY = security.gen_salt(32)
    DEBUG = False
    TESTING = False
    APP_DIRECTORY = os.path.abspath(os.path.dirname(__file__))

    @staticmethod
    def load_local_config():
        """
        Load Environment Variables needed for Python Predix SDK from config.json
        :return: None
        """
        config_filename = os.path.join(Config.APP_DIRECTORY, 'config.json')
        if os.path.exists(config_filename):
            with open(os.path.join(Config.APP_DIRECTORY, 'config.json')) as config_json:
                config = json.loads(config_json.read())

            for env_variable, value in config.items():
                if env_variable == 'PREDIX_DATA_TIMESERIES_ZONE_ID':
                    os.environ['PREDIX_DATA_TIMESERIES_INGEST_ZONE_ID'] = value
                    os.environ['PREDIX_DATA_TIMESERIES_QUERY_ZONE_ID'] = value
                else:
                    os.environ[env_variable] = value

class Development(Config):
    DEBUG = True

    Config.load_local_config()

class Testing(Config):
    TESTING = True

    Config.load_local_config()

class Predix(Config):
    # Set Predix Service Labels
    uaa_label = 'predix-uaa'
    ts_label = 'predix-timeseries'

    # Must check even if not running in this environment
    if 'VCAP_SERVICES' in os.environ:
        vcap_services = json.loads(os.environ['VCAP_SERVICES'])

        uaa_credentials = vcap_services[uaa_label][0]['credentials']
        ts_credentials = vcap_services[ts_label][0]['credentials']

        # Set Environment Variables needed for Python Predix SDK
        os.environ['PREDIX_SECURITY_UAA_URI'] = uaa_credentials['uri']
        os.environ['PREDIX_DATA_TIMESERIES_INGEST_ZONE_ID'] = ts_credentials['ingest']['zone-http-header-value']
        os.environ['PREDIX_DATA_TIMESERIES_INGEST_URI'] = ts_credentials['ingest']['uri']
        os.environ['PREDIX_DATA_TIMESERIES_QUERY_ZONE_ID'] = ts_credentials['query']['zone-http-header-value']
        os.environ['PREDIX_DATA_TIMESERIES_QUERY_URI'] = ts_credentials['query']['uri']


import xlrd
class ExcelUntil():
    def __init__(self,filepath,fileName="Sheet1"):
        #打开EXCEL
        self.path=xlrd.open_workbook(filepath)
        self.table=self.path.sheet_by_name(fileName)
        #读取第一行数据
        self.keys=self.table.row_values(0)
        #读取所有行数
        self.nrows=self.table.nrows
        #读取所有列
        self.cols=self.table.ncols

    def red_table(self):
        if self.nrows<=1:
            print("总行数小于或等于1")
        else:
            r=[]
            for i in range(1,self.nrows):
                s={}
                values=self.table.row_values(i)
                for x in range(0,self.cols):
                    s[self.keys[x]]=values[x]
                r.append(s)
        return r

if __name__=="__main__":
    filepath=r"F:\\pycharm 5.0.4\wb_auto\common\datas.xlsx"
    excle=ExcelUntil(filepath)
    t=excle.red_table()
    print(t)


"""
Visualization of different thresholding functions in opencv
"""
import argparse
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, RadioButtons
import cv2

parser = argparse.ArgumentParser()
parser.add_argument(
    "-i", "--image", required=True)
args = parser.parse_args()

img = cv2.imread(args.image, 0)
assert img is not None, "Failed to read image: {}".format(args.image)

# resize if the image is too big
if img.shape[0]*img.shape[1] > 1.2E6:
    aspect_ratio = img.shape[1]/img.shape[0]
    img = cv2.resize(
        img, (int(1024*aspect_ratio), 1024),
        interpolation=cv2.INTER_CUBIC)

fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
ax.imshow(img, cmap="gray")
ax.set_title("Image Thresholding")

# Define slider and axes and set initial visibility
# threshold value
th_ax = plt.axes([0.25, 0.15, 0.65, 0.03])
th_sl = Slider(th_ax, 'thresh val', 0, 255, valinit=127, valstep=1)
th_ax.set_visible(False)
# block size (adaptive)
bs_ax = plt.axes([0.25, 0.1, 0.65, 0.03])
bsize = int(max(img.shape[0], img.shape[1])/4.0)
if bsize % 2 == 0:
    bsize += 1
bs_sl = Slider(bs_ax, 'block size', 3, bsize, valinit=11, valstep=2)
bs_ax.set_visible(False)
# constant (adaptive)
c_ax = plt.axes([0.25, 0.05, 0.65, 0.03])
c_sl = Slider(c_ax, 'constant', 0, 50, valinit=2, valstep=1)
c_ax.set_visible(False)

method = "original"
def update(val=None):
    if method == "original":
        th = img
    elif method == "binary":
        _, th = cv2.threshold(
            img, th_sl.val, 255, cv2.THRESH_BINARY)
    elif method == "adaptive mean":
        th = cv2.adaptiveThreshold(
            img, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
            cv2.THRESH_BINARY, int(bs_sl.val), int(c_sl.val))
    elif method == "adaptive gaussian":
        th = cv2.adaptiveThreshold(
            img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
            cv2.THRESH_BINARY, int(bs_sl.val), int(c_sl.val))
    else:  # otsu
        _, th = cv2.threshold(
            img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    ax.imshow(th, cmap="gray")
    plt.draw()

th_sl.on_changed(update)
bs_sl.on_changed(update)
c_sl.on_changed(update)

def set_axes_visibility(label):
    global method
    method = label
    if label == "binary":
        th_ax.set_visible(True)
        bs_ax.set_visible(False)
        c_ax.set_visible(False)
    elif label in ["adaptive mean", "adaptive gaussian"]:
        th_ax.set_visible(False)
        bs_ax.set_visible(True)
        c_ax.set_visible(True)
    elif label in ["otsu", "original"]:
        th_ax.set_visible(False)
        bs_ax.set_visible(False)
        c_ax.set_visible(False)
    update()

rax = plt.axes([0.025, 0.5, 0.15, 0.15])
radio = RadioButtons(
    rax, ('original', 'binary', 'otsu', 'adaptive mean', 'adaptive gaussian'))
radio.on_clicked(set_axes_visibility)
plt.show()


import sys

from yowsup.layers import YowLayerEvent
from yowsup.layers.auth import AuthError
from yowsup.layers.axolotl.props import PROP_IDENTITY_AUTOTRUST
from yowsup.layers.network import YowNetworkLayer
from yowsup.stacks import YowStackBuilder

from external.layer import YowsupCliLayer

class YowsupServerStack(object):
    def __init__(self, credentials, encryptionEnabled=True):
        stackBuilder = YowStackBuilder()
        self.stack = stackBuilder \
            .pushDefaultLayers(encryptionEnabled) \
            .push(YowsupCliLayer) \
            .build()

        self.stack.setCredentials(credentials)
        self.stack.setProp(PROP_IDENTITY_AUTOTRUST, True)
        self.stack.getLayer(8).setCredentials(credentials[0], credentials[1])

    def start(self):
        print("Yowsup Cli client\n==================\nType /help for available commands\n")
        self.stack.broadcastEvent(YowLayerEvent(YowNetworkLayer.EVENT_STATE_CONNECT))

        try:
            self.stack.loop(timeout=0.5, discrete=0.5)
        except AuthError as e:
            print("Auth Error, reason %s" % e)
            return False
        except KeyboardInterrupt:
            print("\nYowsdown")
            sys.exit(0)


shop_prices = [30000, 2000, 1500000]
user_coupons = [20, 40]

def get_max_discounted_price(prices, coupons):
    # 이 곳을 채워보세요!
    total_price = 0
    index_difference = abs(len(prices) - len(coupons))
    prices.sort(reverse=True)
    coupons.sort(reverse=True)

    for i in range(len(coupons)):
        total_price += prices[i] * (1 - coupons[i] / 100)

    for length_diff in range(index_difference):
        if len(coupons) < len(prices):
            total_price += prices[len(coupons) + length_diff]

    return int(total_price)

print(get_max_discounted_price(shop_prices, user_coupons))  # 926000 이 나와야 합니다.


#!/usr/bin/python
# WIP Doesn't expect the commented out sections to work.
import boto3
import os

s3 = boto3.resource('s3')
bucket_name = ''
bucket_folder = ''
region = 'us-east-1'
directory = 'uploads'

def check_if_uploads_directory_exists():
	print('Running Folder Check')
	if not os.path.exists(directory):
		#os.makedirs(directory)
		print('The uploads directory doesn\'t exist. Bailing out.')
		exit()

def check_if_bucket_exists():
	print('Checking if ' + bucket_name + ' bucket exists')
	exists = True
	#try:
	#	s3.meta.client.head_bucket(Bucket=bucket_name)
	#except botocore3.exceptions.ClientError as e:
	#	# If a client error is thrown, then check that it was a 404 error.
	#	# If it was a 404 error, then the bucket does not exist.
	#	error_code = int(e.response['Error']['Code'])
	#	if error_code == 404:
	#		s3.create_bucket(Bucket=bucket_name, CreateBucketConfiguration={ 'LocationConstraint': region } )

def create_bucket():
	bucket = s3.Bucket(bucket_name)
	s3.create_bucket(Bucket=bucket,
		CreateBucketConfiguration={ 'LocationConstraint': region } )

def upload_contents_of_directory():
	upload_files = os.listdir('uploads')
	for upload_file in upload_files:
		s3.Object(bucket_name, bucket_folder + upload_file).put(Body=open(directory + '/' + upload_file, 'rb'))

print('Starting Program')
check_if_uploads_directory_exists()
#check_if_bucket_exists
#create_bucket
upload_contents_of_directory()


import sys
import argparse

def load_conll(path):
	data = []
	cur_toks = []
	cur_pos = []
	cur_chunk = []
	cur_ner = []
	cnt = 0
	max_seq_l = 0
	with open(path, 'r+') as f:
		for l in f:
			l = l.strip()
			# the end of example
			if l == '':
				ls = []
				for tok, pos, chunk, ner in zip(cur_toks, cur_pos, cur_chunk, cur_ner):
					ls.append((tok, pos, chunk, ner))
				data.append(ls)

				# clear cache
				cur_toks = []
				cur_pos = []
				cur_chunk = []
				cur_ner = []

				# stats
				cnt += 1
				max_seq_l = max(max_seq_l, len(ls))
			else:
				toks = l.split()
				if len(toks) != 4:
					print('must be 4 tokens: {0}'.format(l))
					assert(False)
				tok, pos, chunk, ner = toks
				cur_toks.append(tok)
				cur_pos.append(pos)
				cur_chunk.append(chunk)
				cur_ner.append(ner)
	print('{0} examples loaded'.format(cnt))
	print('max seq length: {0}'.format(max_seq_l))

	source = [[p[0] for p in ex] for ex in data]
	pos = [[p[1] for p in ex] for ex in data]
	chunk = [[p[2] for p in ex] for ex in data]
	label = [[p[3] for p in ex] for ex in data]
	return source, pos, chunk, label

def write_to(ls, out_file):
	print('writing to {0}'.format(out_file))
	with open(out_file, 'w+') as f:
		for l in ls:
			f.write((l + '\n'))

parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--dir', help="Path to the data dir", default="data/ner/")
parser.add_argument('--data', help="Path to CONLL2003 ner file", default="train.txt")
parser.add_argument('--output', help="Prefix to the path of output", default="train")

def main(args):
	opt = parser.parse_args(args)

	# append path
	opt.data = opt.dir + opt.data
	opt.output = opt.dir + opt.output

	source, pos, chunk, label = load_conll(opt.data)

	write_to([' '.join(ls) for ls in source], opt.output + '.source.txt')
	write_to([' '.join(ls) for ls in pos], opt.output + '.pos.txt')
	write_to([' '.join(ls) for ls in chunk], opt.output + '.chunk.txt')
	write_to([' '.join(ls) for ls in label], opt.output + '.label.txt')

if __name__ == '__main__':
	sys.exit(main(sys.argv[1:]))


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

import numpy as np

from .legion import AffineTransform
from .partition import NoPartition, Restriction, Tiling
from .shape import Shape

class NonInvertibleError(Exception):
    pass

class Transform(object):
    def __repr__(self):
        return str(self)

    def serialize(self, buf):
        code = self._runtime.get_transform_code(self.__class__.__name__)
        buf.pack_32bit_int(code)

class Shift(Transform):
    def __init__(self, runtime, dim, offset):
        self._runtime = runtime
        self._dim = dim
        self._offset = offset

    def compute_shape(self, shape):
        return shape

    def __str__(self):
        return f"Shift(dim: {self._dim}, offset: {self._offset})"

    def __eq__(self, other):
        if not isinstance(other, type(self)):
            return False
        return self._dim == other._dim and self._offset == other._offset

    def __hash__(self):
        return hash((type(self), self._dim, self._offset))

    @property
    def invertible(self):
        return True

    def invert(self, partition):
        if isinstance(partition, Tiling):
            offset = partition.offset[self._dim] - self._offset
            return Tiling(
                self._runtime,
                partition.tile_shape,
                partition.color_shape,
                partition.offset.update(self._dim, offset),
            )
        else:
            raise ValueError(
                f"Unsupported partition: {type(partition).__name__}"
            )

    def invert_dimensions(self, dims):
        return dims

    def invert_restrictions(self, restrictions):
        return restrictions

    def convert(self, partition):
        if isinstance(partition, Tiling):
            offset = partition.offset[self._dim] + self._offset
            return Tiling(
                self._runtime,
                partition.tile_shape,
                partition.color_shape,
                partition.offset.update(self._dim, offset),
            )
        elif isinstance(partition, NoPartition):
            return partition
        else:
            raise ValueError(
                f"Unsupported partition: {type(partition).__name__}"
            )

    def convert_restrictions(self, restrictions):
        return restrictions

    def get_inverse_transform(self, shape, parent_transform=None):
        result = AffineTransform(shape.ndim, shape.ndim, True)
        result.offset[self._dim] = -self._offset
        if parent_transform is not None:
            result = result.compose(parent_transform)
        return result

    def serialize(self, buf):
        super(Shift, self).serialize(buf)
        buf.pack_32bit_int(self._dim)
        buf.pack_64bit_int(self._offset)

class Promote(Transform):
    def __init__(self, runtime, extra_dim, dim_size):
        self._runtime = runtime
        self._extra_dim = extra_dim
        self._dim_size = dim_size

    def compute_shape(self, shape):
        return shape.insert(self._extra_dim, self._dim_size)

    def __str__(self):
        return f"Promote(dim: {self._extra_dim}, size: {self._dim_size})"

    def __eq__(self, other):
        if not isinstance(other, type(self)):
            return False
        return (
            self._extra_dim == other._extra_dim
            and self._dim_size == other._dim_size
        )

    def __hash__(self):
        return hash((type(self), self._extra_dim, self._dim_size))

    @property
    def invertible(self):
        return True

    def invert(self, partition):
        if isinstance(partition, Tiling):
            return Tiling(
                self._runtime,
                partition.tile_shape.drop(self._extra_dim),
                partition.color_shape.drop(self._extra_dim),
                partition.offset.drop(self._extra_dim),
            )
        else:
            raise ValueError(
                f"Unsupported partition: {type(partition).__name__}"
            )

    def invert_dimensions(self, dims):
        return dims[: self._extra_dim] + dims[self._extra_dim + 1 :]

    def invert_restrictions(self, restrictions):
        left = restrictions[: self._extra_dim]
        right = restrictions[self._extra_dim + 1 :]
        return left + right

    def convert(self, partition):
        if isinstance(partition, Tiling):
            return Tiling(
                self._runtime,
                partition.tile_shape.insert(self._extra_dim, self._dim_size),
                partition.color_shape.insert(self._extra_dim, 1),
                partition.offset.insert(self._extra_dim, 0),
            )
        elif isinstance(partition, NoPartition):
            return partition
        else:
            raise ValueError(
                f"Unsupported partition: {type(partition).__name__}"
            )

    def convert_restrictions(self, restrictions):
        left = restrictions[: self._extra_dim]
        right = restrictions[self._extra_dim :]
        new = (Restriction.AVOIDED,)
        return left + new + right

    def get_inverse_transform(self, shape, parent_transform=None):
        parent_ndim = shape.ndim - 1
        result = AffineTransform(parent_ndim, shape.ndim, False)
        parent_dim = 0
        for child_dim in range(shape.ndim):
            if child_dim != self._extra_dim:
                result.trans[parent_dim, child_dim] = 1
                parent_dim += 1
        if parent_transform is not None:
            result = result.compose(parent_transform)
        return result

    def serialize(self, buf):
        super(Promote, self).serialize(buf)
        buf.pack_32bit_int(self._extra_dim)
        buf.pack_64bit_int(self._dim_size)

class Project(Transform):
    def __init__(self, runtime, dim, index):
        self._runtime = runtime
        self._dim = dim
        self._index = index

    def compute_shape(self, shape):
        return shape.drop(self._dim)

    def __str__(self):
        return f"Project(dim: {self._dim}, index: {self._index})"

    def __eq__(self, other):
        if not isinstance(other, type(self)):
            return False
        return self._dim == other._dim and self._index == other._index

    def __hash__(self):
        return hash((type(self), self._dim, self._index))

    @property
    def invertible(self):
        return True

    def invert(self, partition):
        if isinstance(partition, Tiling):
            return Tiling(
                self._runtime,
                partition.tile_shape.insert(self._dim, 1),
                partition.color_shape.insert(self._dim, 1),
                partition.offset.insert(self._dim, self._index),
            )
        else:
            raise ValueError(
                f"Unsupported partition: {type(partition).__name__}"
            )

    def invert_dimensions(self, dims):
        return dims[: self._dim] + (-1,) + dims[self._dim :]

    def invert_restrictions(self, restrictions):
        left = restrictions[: self._dim]
        right = restrictions[self._dim :]
        new = (Restriction.UNRESTRICTED,)
        return left + new + right

    def convert(self, partition):
        if isinstance(partition, Tiling):
            return Tiling(
                self._runtime,
                partition.tile_shape.drop(self._dim),
                partition.color_shape.drop(self._dim),
                partition.offset.drop(self._dim),
            )
        elif isinstance(partition, NoPartition):
            return partition
        else:
            raise ValueError(
                f"Unsupported partition: {type(partition).__name__}"
            )

    def convert_restrictions(self, restrictions):
        return restrictions[: self._dim] + restrictions[self._dim + 1 :]

    def get_inverse_transform(self, shape, parent_transform=None):
        parent_ndim = shape.ndim + 1
        result = AffineTransform(parent_ndim, shape.ndim, False)
        result.offset[self._dim] = self._index
        child_dim = 0
        for parent_dim in range(parent_ndim):
            if parent_dim != self._dim:
                result.trans[parent_dim, child_dim] = 1
                child_dim += 1
        if parent_transform is not None:
            result = result.compose(parent_transform)
        return result

    def serialize(self, buf):
        super(Project, self).serialize(buf)
        buf.pack_32bit_int(self._dim)
        buf.pack_64bit_int(self._index)

class Transpose(Transform):
    def __init__(self, runtime, axes):
        self._runtime = runtime
        self._axes = axes
        self._inverse = tuple(np.argsort(self._axes))

    def compute_shape(self, shape):
        new_shape = Shape(tuple(shape[dim] for dim in self._axes))
        return new_shape

    def __str__(self):
        return f"Transpose(axes: {self._axes})"

    def __eq__(self, other):
        if not isinstance(other, type(self)):
            return False
        return self._axes == other._axes

    def __hash__(self):
        return hash((type(self), tuple(self._axes)))

    @property
    def invertible(self):
        return True

    def invert(self, partition):
        if isinstance(partition, Tiling):
            return Tiling(
                self._runtime,
                partition.tile_shape.map(self._inverse),
                partition.color_shape.map(self._inverse),
                partition.offset.map(self._inverse),
            )
        else:
            raise ValueError(
                f"Unsupported partition: {type(partition).__name__}"
            )

    def invert_dimensions(self, dims):
        return tuple(dims[idx] for idx in self._inverse)

    def invert_restrictions(self, restrictions):
        return tuple(restrictions[idx] for idx in self._inverse)

    def convert(self, partition):
        if isinstance(partition, Tiling):
            return Tiling(
                self._runtime,
                partition.tile_shape.map(self._axes),
                partition.color_shape.map(self._axes),
                partition.offset.map(self._axes),
            )
        elif isinstance(partition, NoPartition):
            return partition
        else:
            raise ValueError(
                f"Unsupported partition: {type(partition).__name__}"
            )

    def convert_restrictions(self, restrictions):
        return tuple(restrictions[idx] for idx in self._axes)

    def get_inverse_transform(self, shape, parent_transform=None):
        result = AffineTransform(shape.ndim, shape.ndim, False)
        for dim in range(shape.ndim):
            result.trans[self._axes[dim], dim] = 1
        if parent_transform is not None:
            result = result.compose(parent_transform)
        return result

    def serialize(self, buf):
        super(Transpose, self).serialize(buf)
        buf.pack_32bit_uint(len(self._axes))
        for axis in self._axes:
            buf.pack_32bit_int(axis)

class Delinearize(Transform):
    def __init__(self, runtime, dim, shape):
        self._runtime = runtime
        self._dim = dim
        self._shape = Shape(shape)
        self._strides = self._shape.strides()

    def compute_shape(self, shape):
        return shape.replace(self._dim, self._shape)

    def __str__(self):
        return f"Delinearize(dim: {self._dim}, shape: {self._shape})"

    def __eq__(self, other):
        if not isinstance(other, type(self)):
            return False
        return self._dim == other._dim and self._shape == other._shape

    def __hash__(self):
        return hash((type(self), self._shape, self._strides))

    @property
    def invertible(self):
        return False

    def invert(self, partition):
        if isinstance(partition, Tiling):
            if (
                partition.color_shape[
                    self._dim + 1 : self._dim + self._shape.ndim
                ].volume()
                == 1
                and partition.offset[
                    self._dim + 1 : self._dim + self._shape.ndim
                ].sum()
                == 0
            ):
                new_tile_shape = partition.tile_shape
                new_color_shape = partition.color_shape
                new_offset = partition.offset
                for _ in range(self._shape.ndim):
                    new_tile_shape = new_tile_shape.drop(self._dim)
                    new_color_shape = new_color_shape.drop(self._dim)
                    new_offset = new_offset.drop(self._dim)

                dim_tile_size = (
                    partition.tile_shape[self._dim] * self._strides[0]
                )
                dim_offset = partition.offset[self._dim] * self._strides[0]
                dim_colors = partition.color_shape[self._dim]

                new_tile_shape = new_tile_shape.insert(
                    self._dim, dim_tile_size
                )
                new_color_shape = new_color_shape.insert(self._dim, dim_colors)
                new_offset = new_offset.insert(self._dim, dim_offset)

                return Tiling(
                    self._runtime,
                    new_tile_shape,
                    new_color_shape,
                    new_offset,
                )
            else:
                raise NonInvertibleError()
        else:
            raise ValueError(
                f"Unsupported partition: {type(partition).__name__}"
            )

    def invert_dimensions(self, dims):
        left = dims[: self._dim + 1]
        right = dims[self._dim + self._shape.ndim :]
        return left + right

    def invert_restrictions(self, restrictions):
        left = restrictions[: self._dim + 1]
        right = restrictions[self._dim + self._shape.ndim :]
        return left + right

    def convert_restrictions(self, restrictions):
        left = restrictions[: self._dim]
        right = restrictions[self._dim + 1 :]
        new = (Restriction.UNRESTRICTED,) + (Restriction.RESTRICTED,) * (
            self._shape.ndim - 1
        )
        return left + new + right

    def get_inverse_transform(self, shape, parent_transform=None):
        assert shape.ndim >= self._strides.ndim
        out_ndim = shape.ndim - self._strides.ndim + 1
        result = AffineTransform(out_ndim, shape.ndim, False)

        in_dim = 0
        for out_dim in range(out_ndim):
            if out_dim == self._dim:
                for dim, stride in enumerate(self._strides):
                    result.trans[out_dim, in_dim + dim] = stride
                in_dim += self._strides.ndim
            else:
                result.trans[out_dim, in_dim] = 1
                in_dim += 1

        if parent_transform is not None:
            result = result.compose(parent_transform)
        return result

    def serialize(self, buf):
        super(Delinearize, self).serialize(buf)
        buf.pack_32bit_int(self._dim)
        buf.pack_32bit_uint(self._shape.ndim)
        for extent in self._shape:
            buf.pack_64bit_int(extent)


import fresh_tomatoes  # file to display the below code in the form of website.
# file where class Movie is defined.
import media

# Making Instances of the class Movie by writing media.Movie().
# And passing movie title,storyline,poster-image,youtube-trailer as arguments.

A_Wrinkle_In_Time = media.Movie("A Wrinkle in Time",
                                "Follows Meg Murry (Storm Reid), a young girl"
                                "traumatized by the disappearance of her"
                                "scientist father (Pine) years before."
                                "She finds herself on an interplanetary"
                                "journey with a schoolmate and her younger"
                                "brother to find her father."
                                "They are aided by a trio of"
                                "supernatural beings:"
                                "Mrs. Whatsit (Witherspoon), Mrs. Who (Kaling)"
                                "and Mrs. Which (Winfrey).",
                                "https://vignette.wikia.nocookie.net/disney/"
                                "images"
                                "/6/61/"
                                "A_Wrinkle_In_Time_Character_Poster_01.jpg/"
                                "revision/latest?cb=20171207033356",
                                "https://www.youtube.com/watch?v=UhZ56rcWwRQ")

AVENGER = media.Movie("AVENGERS: INFINITY WAR",
                      "The Avengers and their Super Hero allies must be"
                      "willing to sacrifice all in an attempt to"
                      "defeat the powerful Thanos before his blitz"
                      "of devastation and ruin puts an end to the universe.",
                      "https://i.redd.it/ip4jygus8huz.jpg",
                      "https://www.youtube.com/watch?v=6ZfuNTqbHE8")

STRANGERS = media.Movie("THE STRANGERS PREY AT NIGHT",
                        "A familys road trip takes a dangerous turn"
                        "when they arrive at a secluded mobile home park"
                        "to stay with some relatives and find it"
                        "mysteriously deserted. Under the cover of darkness,"
                        "three masked psychopaths pay them a visit to test"
                        "the familys every limit as they struggle to survive.",
                        "https://images-na.ssl-images-amazon.com/images/M"
                        "/MV5BMTY1OTIwODgzMV5BMl5BanBnXkFtZTgwMzUyMDgzNDM@."
                        "_V1_QL50_SY1000_CR0,0,674,1000_AL_.jpg",
                        "https://www.youtube.com/watch?v=4_geUW08phI")

Death_wish = media.Movie("Death Wish",
                         "Dr. Paul Kersey (Bruce Willis) is a surgeon who only"
                         "sees the aftermath of Chicago violence when it is"
                         "rushed"
                         "into his ER - until his wife (Elisabeth Shue)"
                         "and college-age daughter (Camila Morrone) are"
                         "viciously"
                         "attacked in their suburban home. With the police"
                         "overloaded with crimes, Paul, burning for revenge,"
                         "hunts his family's assailants to deliver justice."
                         "As the anonymous slayings of criminals grabs the"
                         "media's attention,"
                         "the city wonders if this deadlyvigilante is a"
                         "guardian angel or"
                         "a grim reaper. Fury and fate collide in the intense,"
                         "action-thriller Death Wish.Paul Kersey becomes a "
                         "divided person:A man who saves lives, and a man who"
                         "takes them;"
                         "a husband and father trying to take care of his"
                         "family,"
                         "and a shadowy figure fighting Chicago crime;"
                         "a surgeon extracting bullets from suspects' bodies,"
                         "and the vigilante called The Grim Reaper who"
                         "detectives are quickly closing in on.",
                         "https://images-na.ssl-images-amazon.com/images/M/MV"
                         "5BMTkzNjU3MjE0MF5BMl5BanBnXkFtZTgwNTIyNDk0NDM@."
                         "_V1_QL50_.jpg",
                         "https://www.youtube.com/watch?v=HzILu6yyA20")

jurasic = media.Movie("JURASSIC WORLD: FALLEN KINGDOM",
                      "It’s been four years since theme park and luxury resort"
                      "Jurassic World was destroyed by dinosaurs out of"
                      "containment. Isla Nublar now sits abandoned by"
                      "humans while the surviving dinosaur fend for themselves"
                      "in the jungles.When the island’s dormant volcano begins"
                      "roaring to life, Owen (Chris Pratt) and"
                      "Claire (Bryce Dallas Howard) mount a campaign to rescue"
                      "the remaining dinosaurs from this extinction-level"
                      "event. Owen is driven to find Blue, his lead raptor"
                      "who’s still missing in the wild, and Claire has"
                      "grown a respect for these creatures she now makes"
                      "her mission. Arriving on the unstable island as"
                      "lava begins raining down, their expedition uncovers"
                      "a conspiracy that could return our entire planet"
                      "to a perilous order not seen since prehistoric times."
                      "With all of the wonder,adventure and thrills synonymous"
                      "with one of the most popular and successful series in"
                      "cinema history, this all-new motion-picture event sees"
                      "the return of favorite characters and dinosaurs—along"
                      "with new breeds more awe-inspiring and terrifying"
                      "than ever before."
                      "Welcome to Jurassic World: Fallen Kingdom.",
                      "https://www.movieinsider.com/images/p"
                      "/457037_m1498144102.jpg",
                      "https://www.youtube.com/watch?v=NPv5UopHnj8")

tom = media.Movie("TOMB RAIDER",
                  "Lara Croft is the fiercely independent daughter of"
                  "an eccentric adventurer who vanished when she was scarcely"
                  "a teen. Now a young woman of 21 without any real focus"
                  "or purpose, Lara navigates the chaotic streets of"
                  "trendy East London as a bike courier, barely making the"
                  "rent, and takes college courses, rarely making it to class."
                  "Determined to forge her own path, she refuses to take the"
                  "reins of her father’s global empire just as staunchly as"
                  "she rejects the idea that he’s truly gone."
                  "Advised to face the fact and move forward after seven years"
                  "without him, even Lara can’t understand what drives"
                  "her to finally solve the puzzle of his mysterious death."
                  "Going explicitly against his final wishes,"
                  "she leaves everything she knows behind in search of her"
                  "dad’s last-known destination: a fabled tomb on a"
                  "mythical island that might be somewhere off the"
                  "coast of Japan. But her mission will not be an easy one;"
                  "just reaching the island will be extremely treacherous."
                  "Suddenly, the stakes couldn’t be higher for Lara,"
                  "who—against the odds and armed with only her sharp mind,"
                  "blind faith and inherently stubborn spirit—must learn to"
                  "push herself beyond her limits as she journeys into"
                  "the unknown. If she survives this perilous adventure,"
                  "it could be the making of her,"
                  "earning her the name tomb raider.",
                  "https://www.movieinsider.com/images"
                  "/p/484466_m1505738258.jpg",
                  "https://www.youtube.com/watch?v=8ndhidEmUbI")

# passing list of movies to a new variable move_library.
movie_library = [A_Wrinkle_In_Time,
                 AVENGER,
                 STRANGERS,
                 Death_wish,
                 jurasic,
                 tom]

# passing movie_library as an argument to the function
# of the file fresh_tomatoes to display movies in the browser.
fresh_tomatoes.open_movies_page(movie_library)


'''
Created on 07-Jun-2018

@author: Pritika
'''
import logging

#logging root, logging is thread safe by default
fmt_str =' %(asctime)s::%(levelname)s::%(name)s::%(process)s::%(filename)s::%(ipaddr)s::%(message)s'
logging.basicConfig(level =logging.DEBUG,format =fmt_str, filename ='access.log') # used to alter behaviour of logger and to be used before logging start
#filename to write logs in a file
#fmt_str format of string log will be printed

#change logger name from root to User
logging.getLogger().name ='User'

logging.debug("debug message", extra =dict(ipaddr ='127.0.0.1'))
logging.info("info message", extra =dict(ipaddr ='127.0.0.1'))
logging.warning("warning message" ,extra =dict(ipaddr ='127.0.0.1'))
#logging.error("error message")
#logging.critical("critical message")  # root denotes name of default logger