import pygame, sys
from settings import *
from player import Player
from objects import Laser, Meteor
from animations import AnimatedStar, Explosion
from random import randint
from support import import_folder, importImgs
from overlay import Overlay
from group import AllSprites
from random import choice
from os import path

img_dir = path.join(path.dirname(__file__), 'img')

# # The main function
# def main():
#     introduction = intro()
#     board = initBoard()
#     pretty = printPretty(board)
#     symbol_1, symbol_2 = sym()
#     full = isFull(board, symbol_1, symbol_2) # The function that starts the game is also in here.

# # This function introduces the rules of the game Tic Tac Toe
# def intro():
#     print("Hello! Welcome to Pam's Tic Tac Toe game!")
#     print("\n")
#     print("Rules: Player 1 and player 2, represented by X and O, take turns "
#           "marking the spaces in a 3*3 grid. The player who succeeds in placing "
#           "three of their marks in a horizontal, vertical, or diagonal row wins.")
#     print("\n")
#     input("Press enter to continue.")
#     print("\n")

# def initBoard():
# # This function creates a blank playboard
#     print("Here is the playboard: ")
#     board = [[" ", " ", " "],
#              [" ", " ", " "],
#              [" ", " ", " "]]
#     return board

# def sym():
# # This function decides the players' symbols
#     symbol_1 = input("Player 1, do you want to be X or O? ")
#     if symbol_1 == "X":
#         symbol_2 = "O"
#         print("Player 2, you are O. ")
#     else:
#         symbol_2 = "X"
#         print("Player 2, you are X. ")
#     input("Press enter to continue.")
#     print("\n")
#     return (symbol_1, symbol_2)

# # This function starts the game.
# def startGamming(board, symbol_1, symbol_2, count):
#     # Decides the turn
#     if count % 2 == 0:
#         player = symbol_1
#     elif count % 2 == 1:
#         player = symbol_2
#     print("Player "+ player + ", it is your turn. ")
#     row = int(input("Pick a row:"
#                     "[upper row: enter 0, middle row: enter 1, bottom row: enter 2]:"))
#     column = int(input("Pick a column:"
#                        "[left column: enter 0, middle column: enter 1, right column enter 2]"))

#     # Check if players' selection is out of range, if so, repick.
#     while (row > 2 or row < 0) or (column > 2 or column < 0):
#         outOfBoard(row, column)
#         row = int(input("Pick a row[upper row:"
#                         "[enter 0, middle row: enter 1, bottom row: enter 2]:"))
#         column = int(input("Pick a column:"
#                            "[left column: enter 0, middle column: enter 1, right column enter 2]"))

#         # Check if the square is already filled
#     while (board[row][column] == symbol_1)or (board[row][column] == symbol_2):
#         filled = illegal(board, symbol_1, symbol_2, row, column)
#         row = int(input("Pick a row[upper row:"
#                         "[enter 0, middle row: enter 1, bottom row: enter 2]:"))
#         column = int(input("Pick a column:"
#                             "[left column: enter 0, middle column: enter 1, right column enter 2]"))

#     # Locates player's symbol on the board
#     if player == symbol_1:
#         board[row][column] = symbol_1

#     else:
#         board[row][column] = symbol_2

#     return (board)

# # This function check if the board is full
# def isFull(board, symbol_1, symbol_2):
#     count = 1
#     winner = True
#     while count < 10 and winner == True:
#         gaming = startGamming(board, symbol_1, symbol_2, count)
#         pretty = printPretty(board)

#         if count == 9:
#             print("The board is full. Game over.")
#             if winner == True:
#                 print("There is a tie. ")

#         # Check if here is a winner
#         winner = isWinner(board, symbol_1, symbol_2, count)
#         count += 1
#     if winner == False:
#         print("Game over.")

#     # This is function gives a report
#     report(count, winner, symbol_1, symbol_2)

# # This function tells the players that their selection is out of range
# def outOfBoard(row, column):
#     print("Out of boarder. Pick another one. ")

# # This function prints the board nice!
# def printPretty(board):
#     rows = len(board)
#     cols = len(board)
#     print("---+---+---")
#     for r in range(rows):
#         print(board[r][0], " |", board[r][1], "|", board[r][2])
#         print("---+---+---")
#     return board

# # This function checks if any winner is winning
# def isWinner(board, symbol_1, symbol_2, count):
#     winner = True
#     # Check the rows
#     for row in range (0, 3):
#         if (board[row][0] == board[row][1] == board[row][2] == symbol_1):
#             winner = False
#             print("Player " + symbol_1 + ", you won!")

#         elif (board[row][0] == board[row][1] == board[row][2] == symbol_2):
#             winner = False
#             print("Player " + symbol_2 + ", you won!")

#     # Check the columns
#     for col in range (0, 3):
#         if (board[0][col] == board[1][col] == board[2][col] == symbol_1):
#             winner = False
#             print("Player " + symbol_1 + ", you won!")
#         elif (board[0][col] == board[1][col] == board[2][col] == symbol_2):
#             winner = False
#             print("Player " + symbol_2 + ", you won!")

#     # Check the diagnoals
#     if board[0][0] == board[1][1] == board[2][2] == symbol_1:
#         winner = False
#         print("Player " + symbol_1 + ", you won!")

#     elif board[0][0] == board[1][1] == board[2][2] == symbol_2:
#         winner = False
#         print("Player " + symbol_2 + ", you won!")

#     elif board[0][2] == board[1][1] == board[2][0] == symbol_1:
#         winner = False
#         print("Player " + symbol_1 + ", you won!")

#     elif board[0][2] == board[1][1] == board[2][0] == symbol_2:
#         winner = False
#         print("Player " + symbol_2 + ", you won!")
#     return winner

# def illegal(board, symbol_1, symbol_2, row, column):
#     print("The square you picked is already filled. Pick another one.")

# def report(count, winner, symbol_1, symbol_2):
#     print("\n")
#     input("Press enter to see the game summary. ")
#     if (winner == False) and (count % 2 == 1 ):
#         print("Winner : Player " + symbol_1 + ".")
#     elif (winner == False) and (count % 2 == 0 ):
#         print("Winner : Player " + symbol_2 + ".")
#     else:
#         print("There is a tie. ")

# # Call Main
# if __name__ == "__main__":
#     main()

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class Game:
    def __init__(self):
        pygame.init()
        self.display_surface = pygame.display.set_mode(SCREEN_SIZE)
        pygame.display.set_caption('Ultimate tactoe')
        self.clock = pygame.time.Clock()

        self.all_sprites = AllSprites()
        self.lasers = pygame.sprite.Group()
        self.meteors = pygame.sprite.Group()

        # imports
        self.star_frames = import_folder('../img/star')
        self.explosion = import_folder('../img/explosion')
        self.meteor_surfaces = import_folder('../img/meteors')
        self.spareImgs = importImgs(img_dir)
        print(self.spareImgs)
        # self.playerimage = pygame.image.load(os.path.join(imgPath, '/img/player.png')).convert_alpha()
        # self.playerimage = pygame.image.load('../img/player.png').convert_alpha()
        # self.broken_surf = pygame.image.load('../img/broken.png').convert_alpha()
        self.broken_rect = self.broken_surf.get_rect(center = (SCREEN_SIZE[0]/2, SCREEN_SIZE[1]/2 - 100))

        self.player = Player(self.all_sprites, (SCREEN_SIZE[0] / 2, SCREEN_SIZE[1] - 100), self.create_laser, self.playerimage)

        # timer
        self.meteor_timer = pygame.USEREVENT + 1
        pygame.time.set_timer(self.meteor_timer, 150)

        # bg stars
        for i in range(randint(50,70)):
            AnimatedStar(self.all_sprites, self.star_frames)

        # overlay
        self.overlay = Overlay()

        # score
        self.score = 0
        self.lifes = 3
        self.start_time = 0
        self.game_over = False

        # font
        self.font = pygame.font.Font(None, 40)

        # restart button
        self.restart_surf = self.font.render('Restart', True, TEXT_COLOR)
        self.restart_rect = self.restart_surf.get_rect(center = (SCREEN_SIZE[0] / 2, SCREEN_SIZE[1] - 100))
        self.restart_surf_dark = self.font.render('Restart', True, BG_COLOR)

        # music
        self.game_music = pygame.mixer.Sound('../audio/game_music.wav')
        self.title_music = pygame.mixer.Sound('../audio/title_music.wav')
        self.laser_sound = pygame.mixer.Sound('../audio/laser.wav')
        self.explosion_sound = pygame.mixer.Sound('../audio/explosion.wav')
        self.damage_sound = pygame.mixer.Sound('../audio/damage.ogg')

    def create_laser(self, pos, direction):

        Laser((self.all_sprites, self.lasers), pos, direction)
        self.laser_sound.play()

    def collisions(self):

        # laser -> meteor
        for laser in self.lasers:
            if pygame.sprite.spritecollide(laser, self.meteors, True, pygame.sprite.collide_mask):
                Explosion(self.all_sprites, self.explosion, laser.rect.midtop)
                laser.kill()
                self.explosion_sound.play()

        # meteor -> player
        if pygame.sprite.spritecollide(self.player, self.meteors, True, pygame.sprite.collide_mask):
            self.lifes -= 1
            self.damage_sound.play()

        if self.lifes <= 0:
            self.score = pygame.time.get_ticks() - self.start_time
            self.game_over = True
            for meteor in self.meteors:
                meteor.kill()
            self.player.rect.center = (SCREEN_SIZE[0] / 2, SCREEN_SIZE[1] - 100)

            self.game_music.stop()
            self.title_music.play()

    def run(self):
        while True:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
                if event.type == self.meteor_timer and not self.game_over:
                    Meteor((self.all_sprites, self.meteors), choice(self.meteor_surfaces))

            self.display_surface.fill(BG_COLOR)

            if self.game_over:

                self.display_surface.blit(self.broken_surf,self.broken_rect)
            # text
            text_surf = self.font.render(f'Your score: {self.score}', True, TEXT_COLOR)
            text_rect = text_surf.get_rect(center = (SCREEN_SIZE[0] / 2, SCREEN_SIZE[1] / 2 + 50))
            self.display_surface.blit(text_surf, text_rect)

            # button
            if self.restart_rect.collidepoint(pygame.mouse.get_pos()):
                pygame.draw.rect(self.display_surface, TEXT_COLOR, self.restart_rect.inflate(30,30),0,3)
                self.display_surface.blit(self.restart_surf_dark, self.restart_rect)

                if pygame.mouse.get_pressed()[0]:
                    self.game_over = False
                    self.lifes = 3
                    self.start_time = pygame.time.get_ticks()

                    self.title_music.stop()
                    self.game_music.play()
                else:
                    self.display_surface.blit(self.restart_surf, self.restart_rect)

                pygame.draw.rect(self.display_surface, TEXT_COLOR, self.restart_rect.inflate(30,30), 5,3)
            else:
                dt = self.clock.tick() / 1000

                self.score = pygame.time.get_ticks() - self.start_time
                self.overlay.display_score(self.score)

                self.all_sprites.update(dt)
                self.all_sprites.custom_draw()

                self.collisions()

                self.overlay.display_lifes(self.lifes)

                pygame.display.update()

game = Game()
game.run()


import scrapy

class ComputersSpider(scrapy.Spider):
    name = "computers"
    start_urls = [
        'https://ikman.lk/en/ads/colombo/computers-tablets?categoryType=ads&categoryName=Computers+%26+Tablets&type'
        '=for_sale',
    ]

    def parse(self, response):
        # follow links to computers pages
        for href in response.css('div.item-content a::attr(href)'):
            yield response.follow(href, self.parse_computers)

        # follow pagination links
        href = response.css('a.pag-next::attr(href)')[0]
        yield response.follow(href, self.parse)

    def parse_computers(self, response):
        def extract_with_css(query):
            return response.css(query).extract_first()

        # define properties()
        attribute_type = response.css('div.item-properties dt::text').extract()
        attribute_value = response.css('div.item-properties dd::text').extract()

        properties = {}
        for i in range(0, len(attribute_type)):
            properties[attribute_type[i]] = attribute_value[i]

        def extract_attribute(property):
            value = ''
            if property in properties.keys():
                value = properties[property]
            return value

        yield{
            'item_name': extract_with_css('div.item-top h1::text'),
            'sale_by': extract_with_css('span.poster a::text'),
            'date': extract_with_css('span.date::text'),
            'location': extract_with_css('span.location::text'),
            'price': extract_with_css('span.amount::text'),
            'condition': extract_attribute('Condition:'),
            'item_type': extract_attribute('Device type:'),
            'brand': extract_attribute('Brand:'),
            'model': extract_attribute('Model:'),
            'description': response.css('div.item-description p::text').extract(),

        }


import pickle
import random
import joblib
import numpy as np
from sklearn import metrics
from sklearn.svm import SVC
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split

pick_in = open('./data/picked.pickle', 'rb')
data = pickle.load(pick_in)
pick_in.close()
random.shuffle(data)

features_list = []
lables_list = []

for feature, label in data:
    features_list.append(feature)
    lables_list.append(label)

kernels = ['Polynomial', 'RBF', 'Sigmoid','Linear']

def getClassifier(ktype):
    if ktype == 0:
        # Polynomial kernal
        return SVC(kernel='poly', degree=8, gamma="auto")
    elif ktype == 1:
        # Radial Basis Function kernal
        return SVC(kernel='rbf', gamma="auto")
    elif ktype == 2:
        # Sigmoid kernal
        return SVC(kernel='sigmoid', gamma="auto")
    elif ktype == 3:
        # Linear kernal
        return SVC(kernel='linear', gamma="auto")

for i in range(4):
    X_train, X_test, y_train, y_test = train_test_split(features_list, lables_list, test_size = 0.20)
    svclassifier = getClassifier(i)
    svclassifier.fit(X_train, y_train)
    joblib.dump(svclassifier, './models/{}_model.npy'.format(i))
    y_pred = svclassifier.predict(X_test)
    print("Evaluation:", kernels[i], "kernel")
    print(classification_report(y_test,y_pred, labels=np.unique(y_pred)))
    # print(metrics.f1_score(y_test,y_pred, average=None,labels=np.unique(y_pred)))


#!/usr/bin/env python3

import os
import re
import sys
import copy
import math
import numpy
import string
import random
import crcmod.predefined

debug = True

#====================================
phenotype_dict = {
	'a': 'amber',
	'b': 'bald',
	'c': 'conehead',
	'd': 'dumpy',
	'e': 'eyeless',
	'f': 'forked',
	'g': 'garnet',
	'h': 'hook',
	'i': 'indy',
	'j': 'jagged',
	'k': 'kidney',
	'l': 'lyra',
	'm': 'marula',
	'n': 'notch',
	'o': 'okra',
	'p': 'prickly',
	'q': 'quick',
	'r': 'rosy',
	's': 'scute',
	't': 'taxi',
	'u': 'upturned',
	'v': 'vestigial',
	'w': 'white',
	'x': 'xray',
	'y': 'yellow',
	'z': 'zipper',
}

#====================================
def invertType(genotype, basetype):
	newtype = ''
	for i in range(2):
		if genotype[i] == '+':
			newtype += basetype[i]
		else:
			newtype += '+'
	return newtype

#====================================
def flipGene(genotype, gene, basetype):
	newlist = list(genotype)
	for i in range(2):
		if basetype[i] == gene:
			if genotype[i] == '+':
				newlist[i] = basetype[i]
			else:
				newlist[i] = '+'
	newtype = ""
	for i in newlist:
		newtype += i
	return newtype

#====================================
def getDistance():
	#integers
	return random.randint(2,45)

#====================================
def getProgenySize(distance):
	if debug is True: print("determine progeny size")
	gcdfinal = math.gcd(distance, 100)
	if debug is True: print("Final GCD", gcdfinal)
	progenybase = 100/gcdfinal
	minprogeny =  900/progenybase
	maxprogeny = 6000/progenybase
	progs = numpy.arange(minprogeny, maxprogeny+1, 1, dtype=numpy.float64)*progenybase
	#print(progs)
	numpy.random.shuffle(progs)
	#print(progs)
	bases = progs * distance * distance / 1e4
	#print(bases)
	devs = (bases - numpy.around(bases, 0))**2
	#print(devs)
	argmin = numpy.argmin(devs)
	progeny_size = int(progs[argmin])
	if debug is True: print(("total progeny: %d\n"%(progeny_size)))
	return progeny_size

#====================================
def getPhenotype(genotype):
	if genotype == "++":
		return 'wildtype'
	phenotype_list = []
	for allele in genotype:
		if allele == '+':
			continue
		phenotype = phenotype_dict.get(allele)
		phenotype_list.append(phenotype)
	#print(phenotype_list)
	phenotype_string = ', '.join(phenotype_list)
	return phenotype_string

#====================================
def makeProgenyHtmlTable(typemap, progeny_size):
	alltypes = list(typemap.keys())
	alltypes.sort()
	td_extra = 'align="center" style="border: 1px solid black;"'
	span = ''
	table = '
'
	table += '
'
	table += '  {1}Phenotype	'.format(td_extra, span)
	table += '  {1}Genotypes	'.format(td_extra, span)
	table += '  {1}Progeny
Count	'.format(td_extra, span)
	table += '
'
	for type in alltypes:
		phenotype_string = getPhenotype(type)
		table += '
'
		table += '  {1}{2}	'.format(td_extra.replace('center', 'left'), span, phenotype_string)
		table += ' {1}{2}	'.format(td_extra, span, type[0])
		table += ' {1}{2}	'.format(td_extra, span, type[1])
		table += ' {1}{2:d}	'.format(td_extra.replace('center', 'right'), span, typemap[type])
		table += '
'
	table += '
'
	table += '  {1}TOTAL =	'.format(td_extra.replace('center', 'right'), span)
	table += '  {1}{2:d}	'.format(td_extra.replace('center', 'right'), span, progeny_size)
	table += '
'
	table += '
'
	return table

#====================================
def makeProgenyAsciiTable(typemap, progeny_size):
	alltypes = list(typemap.keys())
	alltypes.sort()
	table = ''
	for genotype in alltypes:
		phenotype_string = getPhenotype(genotype)
		table += ("{0}\t".format(genotype[0]))
		table += ("{0}\t".format(genotype[1]))
		table += ("{0:d}\t".format(typemap[genotype]))
		table += ("{0}\t".format(phenotype_string))
		table += "\n"
	table +=  "\t\t\t-----\n"
	table +=  "\t\tTOTAL\t%d\n\n"%(progeny_size)
	return table

#====================================
def questionText(basetype):
	question_string = '
Two-Point Test-Cross Gene Mapping
'
	question_string += '
A test-cross with a heterozygote fruit fly for two genes is conducted. '
	question_string += 'The resulting phenotypes are summarized in the table above.
 '
	question_string += '
Using the table above, determine the distance between the two genes.
 '
	question_string += '

Hint 1: The gene distance will be a whole number, '
	question_string += 'do NOT enter a decimal; if you have a decimal your calculations are likely wrong.
'
	question_string += '
Hint 2: enter your answer in the blank using only numbers '
	question_string += ' with no spaces or commas. Also, do NOT add units, e.g. cM or m.u.

 '
	return question_string

#====================================
def generateTypeCounts(parental_type, basetype, progeny_size, distance):
	type_counts = {}
	recombinant_type_1 = flipGene(parental_type, geneorder[0], basetype)
	if debug is True: print("recombinant type 1=", recombinant_type_1)
	recombinant_type_2 = invertType(recombinant_type_1, basetype)
	if debug is True: print("recombinant type 2=", recombinant_type_2)

	if debug is True: print("determine recombinant type counts")
	total_recombinant_count = int(round(distance*progeny_size/100.))
	recombinant_count_1 = 0
	recombinant_count_2 = 0
	for i in range(total_recombinant_count):
		if random.random() < 0.5:
			recombinant_count_1 += 1
		else:
			recombinant_count_2 += 1
	if recombinant_count_1 == recombinant_count_2:
		shift = random.randint(1,4)
		recombinant_count_1 += shift
		recombinant_count_2 -= shift

	type_counts[recombinant_type_1] = recombinant_count_1
	if debug is True: print("recombinant count_1=", recombinant_count_1)
	type_counts[recombinant_type_2] = recombinant_count_2
	if debug is True: print("recombinant count_2=", recombinant_count_2)

	if debug is True: print("determine parental type count")
	total_parent_count = progeny_size - total_recombinant_count
	if debug is True: print("  ", parental_type, invertType(parental_type, basetype), total_parent_count)
	parent_count_1 = 0
	parent_count_2 = 0
	for i in range(total_parent_count):
		if random.random() < 0.5:
			parent_count_1 += 1
		else:
			parent_count_2 += 1
	if parent_count_1 == parent_count_2:
		shift = random.randint(1,4)
		parent_count_1 += shift
		parent_count_2 -= shift

	type_counts[parental_type] = parent_count_1
	if debug is True: print("parental count_1=", parent_count_1)
	type_counts[invertType(parental_type, basetype)] = parent_count_2
	if debug is True: print("parental count_2=", parent_count_2)

	return type_counts

#====================================
def makeQuestion(basetype, distance, progeny_size):
	if debug is True: print("------------")
	answerString = ("%s - %d - %s"
		%(basetype[0], distance, basetype[1]))
	print(answerString)
	if debug is True: print("------------")

	if debug is True: print("determine parental type")
	types = ['++', '+'+basetype[1], basetype[0]+'+', basetype[0]+basetype[1]]
	if debug is True: print("types=", types)
	parental = random.choice(types)
	if debug is True: print("parental=", parental, invertType(parental, basetype))
	type_counts = generateTypeCounts(parental, basetype, progeny_size, distance)
	if debug is True: print("type_counts=", type_counts)
	return type_counts

#=======================
def getCrc16_FromString(mystr):
	crc16 = crcmod.predefined.Crc('xmodem')
	crc16.update(mystr.encode('ascii'))
	return crc16.hexdigest().lower()

#=====================
def makeQuestionPretty(question):
	pretty_question = copy.copy(question)
	#print(len(pretty_question))
	pretty_question = re.sub('\<table.+\<\/table\>', '[]\n', pretty_question)
	#print(len(pretty_question))
	pretty_question = re.sub('\<\/p\>\s*\
', '\n', pretty_question)
	#print(len(pretty_question))
	return pretty_question

#=====================
def formatBB_NUM_Question(N, question, answer, tolerance):
	bb_question = ''

	number = "{0}. ".format(N)
	crc16 = getCrc16_FromString(question)
	bb_question += 'NUM\t
{0}. {1}
 {2}'.format(N, crc16, question)
	pretty_question = makeQuestionPretty(question)
	print('{0}. {1} -- {2}'.format(N, crc16, pretty_question))

	bb_question += '\t{0:d}\t{1:.1f}'.format(answer, tolerance)
	print("ANSWER {0:.4f} +/- {1:.1f}".format(answer, tolerance))
	print("")
	return bb_question + '\n'

#====================================
#====================================
if __name__ == "__main__":
	lowercase = "abcdefghijklmnpqrsuvwxyz"

	outfile = 'bbq-' + os.path.splitext(os.path.basename(__file__))[0] + '-questions.txt'
	print('writing to file: '+outfile)
	f = open(outfile, 'w')
	duplicates = 197
	j = -1
	N = 0
	for i in range(duplicates):
		N += 1
		j += 1
		if j + 1 == len(lowercase):
			j = 0
		basetype = lowercase[j:j+2]
		geneorder = basetype
		distance = getDistance()
		print(basetype, distance)
		progeny_size = getProgenySize(distance)
		typemap = makeQuestion(basetype, distance, progeny_size)
		ascii_table = makeProgenyAsciiTable(typemap, progeny_size)
		print(ascii_table)
		html_table = makeProgenyHtmlTable(typemap, progeny_size)
		#print(html_table)
		question_string = questionText(basetype)
		#variable_list = getVariables(geneorder)
		final_question = formatBB_NUM_Question(N, html_table+question_string, distance, 0.1)
		#final_question = blackboardFormat(N, question_string, html_table, distance)
		#print(final_question)

		f.write(final_question)
	f.close()

#THE END


from collections import Counter
class Solution:
    def commonChars(self, A):
        counts = 0

        for i in range(len(A)):
            A[i] = Counter(A[i])

            if i == 0:
                prevk = set(A[i].keys())

                prevDict = dict()
                for k, v in A[i].items():
                    prevDict[k] = [v, float('Inf')]

            key = set(A[i].keys())

            _unionDict = dict()
            _unionKeys = prevk & key
            prevk = _unionKeys

            for k in _unionKeys:

                if A[i][k] >= min(prevDict[k]):

                    _unionDict[k] = min(prevDict[k])

                prevDict[k].append(A[i][k])

        res = []
        for key, value in _unionDict.items():
            current = [key]*value
            res += current

        return res

sol = Solution()
input = ["cool","lock","cook"]

print(sol.commonChars(input))


import json
import networkx as nx
from tqdm import tqdm
import numpy as np
import scipy.sparse as sp
import pickle

kw_max_keep_num = 5
feats_size = 200
class_num = 8
train_p = 0.7
val_p = 0.2
test_p = 0.1

def preprocess():

    with open('../data/subgraph_v11_1.json', 'r') as f:
        nodes = f.readlines()

    g = nx.DiGraph()

    print('node num: ', len(nodes))
    id2idx = {}
    all_node = []
    all_kw = {}
    cnt_bad_nodes = 0
    edge_num = 0
    all_class = {}
    for idx in tqdm(range(len(nodes))):
        cur_node = {}
        mes = json.loads(nodes[idx])
        id2idx[mes['id']] = idx
        bad_node = False
        try:
            feat_list = mes['fos']
            kw_mes = []
            for _ in feat_list:
                obj = (_['name'], _['w'])
                kw_mes.append(obj)
            # sort by weight and get top K keywords
            kw_mes.sort(key=lambda x: x[1], reverse=True)
            kw_mes = kw_mes[: min(len(kw_mes), kw_max_keep_num)]
            for _ in kw_mes:
                _ = _[0]
                if _ not in all_kw:
                    all_kw[_] = 1
                else:
                    all_kw[_] += 1
            cur_node['kw'] = [_[0] for _ in kw_mes]
        except:
            cur_node['kw'] = []
            bad_node = True

        try:
            label = mes['venue']
            # conference with id
            assert len(label) > 1
            label = label['raw']
            cur_node['label'] = label
            if label in all_class:
                all_class[label] += 1
            else:
                all_class[label] = 1

        except:
            bad_node = True
            cur_node['label'] = None

        # we currently overlook weight here
        if bad_node:
            cnt_bad_nodes += 1
        cur_node['id'] = mes['id']
        cur_node['references'] = mes['references']
        edge_num += len(cur_node['references'])
        all_node.append(cur_node)
    print('edge num:         ', edge_num)
    print('class num:        ', len(all_class))
    print('all keywords num: ', len(all_kw))
    print('bad nodes num:    ', cnt_bad_nodes)
    print('bad nodes percent:', round(cnt_bad_nodes / len(nodes) * 100, 3), '%')
    print('average keyword mentioned num:', (len(nodes) - cnt_bad_nodes) * kw_max_keep_num / len(all_kw))
    cnttt = 0
    for _ in all_kw:
        if all_kw[_] < 10:
            cnttt += 1
    print('kw percent < 10:  ', cnttt / len(all_kw) * 100, '%')
    print('start generating graph nodes', '***' * 10)
    # filter out bad nodes, so we need to construct a dict to record the idxes of the filtered-out nodes
    convert_back = {}
    convert = {}
    cnt = 0
    feats = []
    class_map = {}
    for idx in tqdm(range(len(all_node))):
        # filter out bad nodes
        if len(all_node[idx]['kw']) == 0 or all_node[idx]['label'] is None:
            continue
        convert_back[cnt] = idx
        convert[idx] = cnt
        g.add_node(cnt)
        feats.append(np.zeros(shape=[feats_size]))
        class_map[str(cnt)] = np.random.randint(class_num)
        cnt += 1
    feats = np.asarray(feats)
    print('feats shape: ', feats.shape)
    print('len classmap:', len(class_map))
    print('start generating graph edges', '***' * 10)
    cnt_edge = 0
    cnt_node = 0
    for idx in tqdm(range(len(all_node))):
        if idx in convert:
            cnt_node += 1
            for neighbor_idx in all_node[idx]['references']:
                if neighbor_idx not in id2idx:
                    continue
                neighbor_idx = id2idx[neighbor_idx]
                if neighbor_idx in convert:
                    # undirected graph
                    g.add_edge(convert[idx], convert[neighbor_idx])
                    g.add_edge(convert[neighbor_idx], convert[idx])
                    cnt_edge += 1
    print('final node num: ', len(g.nodes()))
    print('final edge num: ', len(g.edges()))
    print('start saving dataset')
    sp.save_npz('../data/adj_full.npz', nx.adjacency_matrix(g))
    print('adj_full.npz saved')
    sp.save_npz('../data/adj_train.npz', nx.adjacency_matrix(g))
    print('adj_train.npz saved')
    np.save('../data/feats.npy', feats)
    print('feats.npy saved')
    with open('../data/class_map.json', 'w') as f:
        json.dump(class_map, f)
    print('class_map.json saved')
    # role
    role = {'tr': [], 'va': [], 'te': []}
    rand_idx = np.random.permutation(feats.shape[0]).tolist()
    role['tr'] = rand_idx[:int(feats.shape[0] * train_p)]
    role['va'] = rand_idx[int(feats.shape[0] * train_p): int(feats.shape[0] * (train_p + val_p))]
    role['te'] = rand_idx[int(feats.shape[0] * (train_p + val_p)):]
    with open('../data/role.json', 'w') as f:
        json.dump(role, f)
    print('role.json saved')
    pickle.dump(convert, open('../data/idx_convert.pkl', 'wb'))
    print('idx_convert.pkl saved')
    print('finished')
    return

if __name__ == '__main__':
    preprocess()


#!/usr/bin/python

import argparse, shutil, os
from multiprocessing import cpu_count
from subprocess import call
#from glob import glob

VERSION = 3.2
CORES = cpu_count()

DOC_SOURCE_DIR = './resources_source/doc'
DOC_DIR = './resources/help'
LOC_SOURCE_DIR = './resources_source/lang'
LOC_DIR = './resources/lang'
PLUGINS_DIR = './plugins'
TEMP_DIR = './tmp'

LOC_SOURCE_FILES = ['agros2d-binary/', 'agros2d-library/',
                    'pythonlab-binary/', 'pythonlab-library/',
                    'agros2d-solver/', 'util/']
LOC_TARGET_FILES = ['resources_source/lang/en_US.ts', 'resources_source/lang/cs_CZ.ts',
                    'resources_source/lang/pl_PL.ts', 'resources_source/lang/ru_RU.ts',
                    'resources_source/lang/fr_FR.ts']

LOC_PLUGINS_SOURCE_FILES = ['plugins/']
LOC_PLUGINS_TARGET_FILES = ['resources_source/lang/plugin_en_US.ts',
                            'resources_source/lang/plugin_cs_CZ.ts',
                            'resources_source/lang/plugin_pl_PL.ts',
                            'resources_source/lang/plugin_ru_RU.ts',
                            'resources_source/lang/plugin_fr_FR.ts']

def documentation(format):
    call(['make', format, '-C', DOC_SOURCE_DIR])

    if (os.path.exists(DOC_DIR)):
        shutil.rmtree(DOC_DIR)

    ignored = ['doctrees']
    shutil.copytree('{0}/build/'.format(DOC_SOURCE_DIR), DOC_DIR, ignore=shutil.ignore_patterns(*ignored))

def equations():
    for root, dirs, files in os.walk(PLUGINS_DIR):
        for file in files:
            if (os.path.splitext(file)[1] != '.py'):
                continue

            call(['python', '{0}/{1}'.format(root, file)])

def release_localization():
    for file in os.listdir(LOC_SOURCE_DIR):
        if (os.path.splitext(file)[1] != '.ts'):
            continue

        call(['lrelease', '{0}/{1}'.format(LOC_SOURCE_DIR, file)])

    for file in os.listdir(LOC_SOURCE_DIR):
        if (os.path.splitext(file)[1] != '.qm'):
            continue

        if (not os.path.exists(LOC_DIR)):
            os.mkdir(LOC_DIR)

        shutil.copy2('{0}/{1}'.format(LOC_SOURCE_DIR, file), LOC_DIR)

def files_for_localization(source):
    sources = list()
    for root, dirs, files in os.walk(source):
        for file in files:
            if (file.endswith('.cpp') or file.endswith('.h')) and (not file.startswith('moc_')):
                sources.append(os.path.join(root, file))

    return sources

def update_localization():
    sources = list()
    for source in LOC_SOURCE_FILES:
      sources += files_for_localization(source)

    call(['lupdate'] + sources + ['-ts'] + LOC_TARGET_FILES)

    sources = list()
    for source in LOC_PLUGINS_SOURCE_FILES:
      sources += files_for_localization(source)

    call(['lupdate'] + sources + ['-ts'] + LOC_PLUGINS_TARGET_FILES)

def build_project(cores):
    call(['cmake', '.'])
    call(['make', '-j', str(cores)])

    call(['./agros2d_generator'])
    call(['cmake', PLUGINS_DIR + '/CMakeLists.txt'])
    call(['make', '-C', PLUGINS_DIR, '-j', str(cores)])

def run_project(project, file, run_script, server):
    env = dict(os.environ)
    env['LD_LIBRARY_PATH'] = 'libs'

    args = list()
    if (server):
        args.append('xvfb-run')
        args.append('--auto-servernum')

    args.append('./{0}'.format(project))

    if (run_script):
        args.append('-r')

    if (file != ''):
        args.append(file)

    call(args, env=env)

def source_package(version):
    call(['git', 'clean', '-fdx'])

    temp = '{0}/agros2d-{1}'.format(TEMP_DIR, version)

    ignored = ['tmp', '.git*', '*.mph']
    shutil.copytree('./', temp, ignore=shutil.ignore_patterns(*ignored))

    os.chdir(temp)
    call(['debuild', '-S', '-sa'])

def binary_package():
    call(['dpkg-buildpackage', '-sgpg', '-rfakeroot'])

def callgrind():
	call(['valgrind --tool=callgrind --smc-check=all-non-file --fn-skip=QMetaObject::activate* --fn-skip=QMetaObject::metacall* --fn-skip=*::qt_metacall* --fn-skip=*::qt_static_metacall* ./agros2d'])

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(dest='command')

    # documentation
    doc = subparsers.add_parser('doc', help='generate documentation')
    doc.add_argument('-f', '--format', nargs='?', default='html', type=str, required=False,
                     help='output format (default parameter is html)')

    # localization
    loc = subparsers.add_parser('loc', help='release or update localization')
    loc.add_argument('-r', '--release', default=False, action='store_true', required=False)
    loc.add_argument('-u', '--update', default=False, action='store_true', required=False)

    # build
    build = subparsers.add_parser('build', help='build project')
    build.add_argument('-c', '--cores', nargs='?', default=CORES, type=int, required=False,
                      help='number of used cores')

    # run
    run = subparsers.add_parser('run', help='run project')
    run.add_argument('-p', '--project', nargs='?', default='agros2d', type=str, required=False,
                     help='project (valid parameters are agros2d, agros2d_pythonalb, agros2d_generator, agros2d_solver)')
    run.add_argument('-f', '--file', nargs='?', default=str(), type=str, required=False,
                     help='open Agros2D data file or Python script')
    run.add_argument('-r', '--run', action='store_true', required=False,
                     help='run Python script defined as file')
    run.add_argument('-s', '--server', action='store_true', required=False,
                     help='run project with X virtual framebuffer')

    # package
    pack = subparsers.add_parser('pack', help='make source or binary package')
    pack.add_argument('-s', '--source', default=True, action='store_true')
    pack.add_argument('-b', '--binary', default=False, action='store_true')
    pack.add_argument('-v', '--version', nargs='?', default=VERSION, type=float, required=False,
                      help='version of package')

    # equations
    eqs = subparsers.add_parser('eqs', help='generate equations from modules')

	  # callgrind
    cgrind = subparsers.add_parser('callgrind', help='call callgrind')

    args = parser.parse_args()

    if (args.command == 'doc'):
        documentation(args.format)

    if (args.command == 'loc'):
        if (args.release):
            release_localization()
        if (args.update):
            update_localization()

    if (args.command == 'build'):
        build_project(args.cores)

    if (args.command == 'run'):
        run_project(args.project, args.file, args.run, args.server)

    if (args.command == 'pack'):
        if (args.source):
            source_package(args.version)
        if (args.binary):
            binary_package()

    if (args.command == 'eqs'):
        equations()

    if (args.command == 'callgrind'):
        callgrind()


import matplotlib.pyplot as plt
import numpy as np

def PillarDesign(sigma_z, pillar_width, room_width, height, design_fos):
    """
    The Purpose of this program is to successfully design pillar for room and
    pillar underground mining method based on example Measurements.
    Copyright Joaquin Roibal 2016
    New Mexico Institute of Mining and Technology
    """

    pillar_stress = PillarStress(sigma_z, pillar_width, room_width)
    ps = PillarStrength(height,pillar_width)
    factor_of_safety = FactorOfSafety(ps, pillar_stress)

    print("Original Pillar Stress: {}".format(pillar_stress))
    print("Calculated Pillar Strength: {}".format(ps))
    print("Factor of Safety of Original Design Requirements: {}".format(factor_of_safety))

    while factor_of_safety > design_fos:
        room_width += .1
        pillar_width -= .1
        ps = PillarStrength(height, pillar_width)
        pillar_stress = PillarStress(sigma_z, pillar_width, room_width)
        factor_of_safety = FactorOfSafety(ps, pillar_stress)
    print("Final Design Requirements for FOS = {}\nRoom Width: {} \nPillar Width: {}".format(factor_of_safety,
                                                                                room_width, pillar_width))
    print("Pillar Strength: {}".format(ps))
    print("Extraction Ratio: {}".format(ExtractionRatio(pillar_width,room_width)))
    print("Extraction Ratio: {}".format(ExtractionRatio(pillar_width,room_width)))

    while factor_of_safety < design_fos:
        room_width -= .1
        pillar_width +=.1
        ps = PillarStrength(height,pillar_width)
        pillar_stress = PillarStress(sigma_z, pillar_width, room_width)
        factor_of_safety = FactorOfSafety(ps, pillar_stress)
    print("Final Design Requirements for FOS = {}\nRoom Width: {} \nPillar Width: {}".format(factor_of_safety,
                                                                                room_width, pillar_width))
    print("Pillar Stress: {}".format(pillar_stress))
    print("Pillar Strength: {}".format(ps))
    print("Extraction Ratio: {}".format(ExtractionRatio(pillar_width,room_width)))

def PillarStrength(height, width):
    return 10.44*height**(-.7)*width**(0.5)

def FactorOfSafety(p_strength, p_stress):
    return p_strength/p_stress

def ExtractionRatio(pillar_width,room_width):
    return ((pillar_width+room_width)**2-(pillar_width**2))/((pillar_width+room_width)**2)

def PillarStress(sigma_z, pillar_width, room_width):
    return sigma_z*((pillar_width+room_width)/pillar_width)**2

def PlotExample():
    """
    Example Plot for varying Pillar, Room Widths which displays the
    Varying Extraction Ratios.
    """
    er = []
    wp = [x for x in range(1,15)]
    wo = [y for y in range(1,15)]
    for w_p in wp:
        w_open = []
        for w_o in wo:
            w_open.append(ExtractionRatio(w_p,w_o))
        er.append(w_open)
        plt.plot(wo, w_open, label = 'Pillar Length: {}'.format(w_p))
    plt.xlabel('Width Opening of Tunnel')
    plt.ylabel('Extraction Ratio')
    plt.title('Extraction Ratio, Varying Room and Pillar Sizing')
    print(er)
    plt.legend(loc=4)

    plt.figure(2)
    er2 = []
    sigma_z = 3.375
    height = 3
    for w_p in wp:
        w_open2 = []
        for w_o in wo:
            pstress = PillarStress(sigma_z, w_p, w_o)
            pstrength = PillarStrength(height, w_p)
            FOS = FactorOfSafety(pstrength, pstress)
            w_open2.append(FOS)
        er2.append(w_open2)
        plt.plot(wo, w_open2, label = 'Pillar Length: {}'.format(w_p))
    plt.xlabel('Width Opening of Tunnel')
    plt.ylabel('Factor of Safety')
    plt.title('Factor of Safety, Varying Room and Pillar Sizing')
    plt.legend(loc=1)
    plt.show()

def main():
    depth = 150                             #Meters
    height = 3                              #Pillar Height in Meters
    gamma_rock = 22.5                       #kN/m^3
    pillar_width = 7                        #Meters
    room_width = 6                          #Meters
    sigma_z = depth * gamma_rock/1000
    design_fos = [1.3, 1.6]
    for fos in design_fos:
        PillarDesign(sigma_z, pillar_width, room_width, height, fos)

    PlotExample()

if __name__ == "__main__":
    main()


import numpy as np
import json
import itertools
from io import StringIO

# For image manipulation
from PIL import Image
from mmorph import dilate, sedisk

def interpolate(x_in, y_in):
    """
    For line interpretations.
    Connect the dots of each interpretation.
    """

    # Check difference in x and in y, so we can
    # interpolate in the direction with the most range

    x_range = np.arange(np.amin(x_in), np.amax(x_in)+1)
    y_range = np.arange(np.amin(y_in), np.amax(y_in)+1)

    if x_range.size >= y_range.size:
        x_out = x_range
        y_out = np.interp(x_out, x_in, y_in)
    else:
        y_out = y_range
        x_out = np.interp(y_out, y_in, x_in)

    return x_out.astype(int), y_out.astype(int)

def normalize(a, newmax):
    """
    Normalize the values of an
    array a to some new max.

    """
    normed = np.zeros_like(a)
    normed = (float(newmax) * a) / np.amax(a)
    return normed

def draw_pick_to_user_layer(user_layer, picks, img_obj):
    """
    This is where the magic happens.
    """
    w = img_obj.width
    h = img_obj.height
    pickstyle = img_obj.pickstyle

    if pickstyle == 'polygons':
        agg = np.append(picks, picks[0])
        picks = agg.reshape(picks.shape[0]+1, picks.shape[1])

    # Deal with the points
    if pickstyle != 'points':
        for i, _ in enumerate(picks[:-1]):

            xpair = picks[i:i+2, 0]

            if xpair[0] > xpair[1]:
                xpair = xpair[xpair[:].argsort()]
                xrev = True
            else:
                xrev = False

            ypair = picks[i:i+2, 1]

            if ypair[0] > ypair[1]:
                ypair = ypair[ypair[:].argsort()]
                yrev = True
            else:
                yrev = False

            # Do the interpolation
            x, y = interpolate(xpair, ypair)

            if xrev:  # then need to unreverse...
                x = x[::-1]
            if yrev:  # then need to unreverse...
                y = y[::-1]

            # Build up the image, accounting for pixels at
            # the edge, which have the wrong indices.
            x[x >= w] = w - 1
            y[y >= h] = h - 1
            user_layer[(y, x)] = 1.

    else:
        x, y = picks[:, 0], picks[:, 1]
        user_layer[(y, x)] = 1.

    return user_layer

def draw_all_picks_to_user_layer(user_layer, all_picks, img_obj):
    if all_picks[0].size == 3:
        # picks are tagged with their group
        # group indicates the feature, eg the line segment
        for group in itertools.groupby(all_picks, lambda x: x[2]):
            picks = np.array([p for p in group[1]])
            return draw_pick_to_user_layer(user_layer, picks, img_obj)
    else:
        return draw_pick_to_user_layer(user_layer, all_picks, img_obj)

def calculate_disk_radius(img_obj):
    w = img_obj.width
    h = img_obj.height
    avg = (w + h) / 2.
    if (img_obj.pickstyle == 'points'):
        n = np.ceil(avg / 150.).astype(int)
    else:
        n = np.ceil(avg / 300.).astype(int)
    return n

def create_user_heatmap_layer(img_obj, picks, cohort):
    w = img_obj.width
    h = img_obj.height

    # Make a new image for this interpretation.
    user_layer = np.zeros((h, w), dtype=np.float32)

    # Get the points.
    all_picks = np.array(json.loads(picks))

    if all_picks.size == 0:
        raise Exception

    user_layer = draw_all_picks_to_user_layer(user_layer, all_picks, img_obj)
    n = calculate_disk_radius(img_obj)

    # Dilate this image.
    return dilate(user_layer.astype(int), B=sedisk(r=n)), cohort

def convert_array_to_image(the_array):
    """
    Normalize the heatmap from 0-255 for making an image.
    More muted version: Subtract 1 first to normalize to
    the non-zero data only.
    """
    heatmap_norm = normalize(the_array, 255)
    alpha_norm = normalize(np.ones_like(heatmap_norm), 255)

    # Make the RGB channels.
    r = np.clip((2 * heatmap_norm), 0, 255)
    g = np.clip(((3 * heatmap_norm) - 255), 0, 255)
    b = np.clip(((3 * heatmap_norm) - 510), 0, 255)
    a = alpha_norm

    # Set everything corresponding to zero data to transparent.
    a[the_array == 0] = 0

    # Make the 4-channel image from an array.
    im = np.dstack([r, g, b, a])
    im_out = Image.fromarray(im.astype('uint8'), 'RGBA')
    return im_out

def array_to_png(the_array):
    im_out = convert_array_to_image(the_array)
    output = StringIO()
    im_out.save(output, 'png')
    return output.getvalue()

def generate_user_heatmap_layer(img_obj, user_data):
    try:
        user_layer, cohort = create_user_heatmap_layer(img_obj, user_data)
        the_png = array_to_png(user_layer)
        user_heatmap = Heatmap.all().ancestor(img_obj).filter("user_id =", user_data.user_id).get()
        if not user_heatmap:
            user_heatmap = Heatmap(stale=False, png=the_png,parent=img_obj, user_id=user_data.user_id, cohort=cohort)
        else:
            user_heatmap.stale = False
            user_heatmap.png = the_png
        user_heatmap.put()

    except HeatMapException:
        pass

def cache_composite_heatmap(img_obj, png, cohort):
    cached_heatmap = Heatmap.all().ancestor(img_obj).filter('user_id =', None)
    cached_heatmap = cached_heatmap.filter('cohort =', cohort)
    cached_heatmap = cached_heatmap.get()
    if not cached_heatmap:
        cached_heatmap = Heatmap(stale=False,
                                 png=png,
                                 parent=img_obj,
                                 user_id=None,
                                 cohort=cohort)
    else:
        cached_heatmap.stale = False
        cached_heatmap.png = png
    cached_heatmap.put()

def create_a_composite_png_from_layers(size, layers):
    """
    Creates a composite from layers, producing a blank image if
    there are no layers.
    """
    heatmap = np.zeros(size, dtype=np.float32)
    for layer in layers:
        try:
            im = np.asarray(Image.open(StringIO(layer.png)))[:, :, 0]
        except AttributeError:
            # Then it's an array if we're in the library
            im = layer
        heatmap += im  # layers are greyscale so just use one channel
    return array_to_png(heatmap)


# 做完特徵工程前的所有準備 (與前範例相同)
import pandas as pd
import numpy as np
import copy
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
from sklearn.model_selection import cross_val_score
from sklearn.ensemble import GradientBoostingClassifier

import warnings
warnings.filterwarnings('ignore')

data_path ='E:\\python\\ML-day100\\data\\'
df = pd.read_csv(data_path + 'titanic_train.csv')

train_Y = df['Survived']
df = df.drop(['PassengerId', 'Survived'] , axis=1)
df.head()

# 因為需要把類別型與數值型特徵都加入, 故使用最簡版的特徵工程
LEncoder = LabelEncoder()
MMEncoder = MinMaxScaler()
for c in df.columns:
    df[c] = df[c].fillna(-1)
    if df[c].dtype == 'object':
        df[c] = LEncoder.fit_transform(list(df[c].values))
    df[c] = MMEncoder.fit_transform(df[c].values.reshape(-1, 1))
df.head()

# 梯度提升樹擬合後, 將結果依照重要性由高到低排序 (note : D27作業中'Ticket'是第一名特徵, 'Age'是數值特徵中排名最高者)
estimator = GradientBoostingClassifier()
estimator.fit(df.values, train_Y)
feats = pd.Series(data=estimator.feature_importances_, index=df.columns)
feats = feats.sort_values(ascending=False)
feats

# 原始特徵 + 梯度提升樹
train_X = MMEncoder.fit_transform(df)
cross_val_score(estimator, train_X, train_Y, cv=5).mean()

# 高重要性特徵 + 梯度提升樹

high_feature = list(feats[:4].index)

train_X = MMEncoder.fit_transform(df[high_feature])
cross_val_score(estimator, train_X, train_Y, cv=5).mean()

# 觀察重要特徵與目標的分布
# 第一名 : Ticket
import seaborn as sns
import matplotlib.pyplot as plt
sns.regplot(x=df['Ticket'], y=train_Y, fit_reg=False)
plt.show()

# 第二名 : Name
sns.regplot(x=df['Name'], y=train_Y, fit_reg=False)
plt.show()

# 製作新特徵看效果

df['Add_char'] = (df['Ticket'] + df['Name']) / 2
df['Multi_char'] = df['Ticket'] * df['Name']
df['GO_div1p'] = df['Ticket'] / (df['Name']+1) * 2
df['OG_div1p'] = df['Name'] / (df['Ticket']+1) * 2

train_X = MMEncoder.fit_transform(df)
cross_val_score(estimator, train_X, train_Y, cv=5).mean()


from django.db import migrations, models

class Migration(migrations.Migration):

    dependencies = [
        ('sms', '0010_update_sqlmobilebackend_couch_id'),
    ]

    operations = [
        migrations.AlterField(
            model_name='sqlmobilebackend',
            name='inbound_api_key',
            field=models.CharField(unique=True, max_length=126, db_index=True),
            preserve_default=True,
        ),
    ]


import os
import json
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    logger.info("#### I am logging Lambda versioning")

    request_body = json.loads(event.get('body'))

    name = request_body.get('name')

    if name == 'Joker':
        raise ValueError('Bad guys should not be greeted')

    return {
        "statusCode": 200,
        "body": json.dumps({
            "greeting": "Hello and wellcome again " + name,
        }),
    }


import logging
from functools import wraps

from flask import request

from GeoPredictor.errors import GeostoreNotFound, error
from GeoPredictor.services import GeostoreService

def remove_keys(keys, dictionary):
    for key in keys:
        try:
            del dictionary[key]
        except KeyError:
            pass
        except Exception as err:
            return error(status=502, detail=f'{err}')
    return dictionary

def sanitize_parameters(func):
    """Sets any queryparams in the kwargs"""

    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            logging.debug(f'[middleware] [sanitizer] args: {args}')
            logging.debug(f'[middleware] [sanitizer] kargs: {kwargs}')

            myargs = {**kwargs, **request.args, **request.args.get('payload', {})}
            logging.debug(f'[middleware] [sanitizer] User_args: {myargs}')
            # Exclude params like loggedUser here

            sanitized_args = remove_keys(['loggedUser'], myargs)
            kwargs['params'] = sanitized_args
        except GeostoreNotFound:
            return error(status=404, detail='body params not found')
        except Exception as err:
            return error(status=502, detail=f'{err}')

        return func(*args, **kwargs)

    return wrapper

def parse_payload(func):
    """Get payload data"""

    @wraps(func)
    def wrapper(*args, **kwargs):
        kwargs["payload"] = request.args.get('payload', {'payload': None})
        return func(*args, **kwargs)

    return wrapper

def get_geo_by_hash(func):
    """Get geostore data"""

    @wraps(func)
    def wrapper(*args, **kwargs):

        try:
            if 'geostore' in kwargs["sanitized_params"].keys():
                geostore = kwargs["sanitized_params"]["geostore"]
                logging.info(f'[middleware]: {geostore}')
                geojson = GeostoreService.get(geostore)
                kwargs["sanitized_params"]["geojson"] = geojson
        except GeostoreNotFound:
            return error(status=404, detail='Geostore not found')
        except Exception as err:
            return error(status=502, detail=f'{err}')

        return func(*args, **kwargs)

    return wrapper


import websocket, json
from typing import Optional
import threading
import time

class Coinbase(threading.Thread):

    def __init__(
        self,
        url: Optional[str] = 'wss://ws-feed.exchange.coinbase.com',
        exchange: Optional[str] = 'Coinbase',
        orderBook: Optional[dict] = dict(),
        lock = None
    ):
        super().__init__()
        self.url = url
        self.exchange = exchange
        self.orderBook = orderBook[exchange]
        self.lock = lock
        self.ws = websocket.WebSocketApp(self.url, on_open = self.on_open, on_message=self.on_message)

    def run(self):
        while True:
            self.ws.run_forever()

    def on_open(self,ws):
        """
            Definition: sets subscribe data
        """

        # print("Connection establish with Coinbase")

        subscribe = {
            "type":"subscribe",
            "channels": [
                {
                    "name":"ticker",
                    "product_ids": [
                        "BTC-USD",
                        "ETH-USD"
                    ]
                }
            ]
        }

        self.ws.send(json.dumps(subscribe))

    def on_message(self,ws,message):
        json_message = json.loads(message)
        # print(json_message)
        with self.lock:

            if json_message['product_id'] == 'BTC-USD':

                self.orderBook['BTC-USDT']['bid'] = json_message["best_bid"]
                self.orderBook['BTC-USDT']['ask'] = json_message["best_ask"]
            else:
                self.orderBook['ETH-USDT']['bid'] = json_message["best_bid"]
                self.orderBook['ETH-USDT']['ask'] = json_message["best_ask"]

class Binance(threading.Thread):

    def __init__(
        self,
        url: Optional[str] = 'wss://stream.binance.com:9443/ws',
        exchange: Optional[str] = 'Binance',
        orderBook: Optional[dict] = dict(),
        lock = None
    ):
        super().__init__()
        self.url = url
        self.exchange = exchange
        self.orderBook = orderBook[exchange]
        self.lock = lock
        self.ws =  websocket.WebSocketApp(self.url, on_open = self.on_open, on_message=self.on_message)

    def run(self):

        while True:
            self.ws.run_forever()

    def on_open(self,ws):

        # print("Connection established with Binance")

        subscribe = {
                "method":"SUBSCRIBE",
                "params": [
                        "btcusdt@bookTicker",
                        "ethusdt@bookTicker"
                    ],
                "id": 1
            }

        self.ws.send(json.dumps(subscribe))

    def on_message(self, ws, message):
        json_message = json.loads(message)

        with self.lock:

            if json_message['s'] == "BTCUSDT":

                self.orderBook["BTC-USDT"]['bid'] = json_message["b"]
                self.orderBook["BTC-USDT"]['ask'] = json_message["a"]

            elif json_message['s'] == "ETHUSDT":
                self.orderBook["ETH-USDT"]['bid'] = json_message["b"]
                self.orderBook["ETH-USDT"]['ask'] = json_message["a"]


from celery.result import AsyncResult

from fastapi import FastAPI
from fastapi.responses import JSONResponse
from worker import start_task
from pydantic import BaseModel
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")

app.add_middleware(
    CORSMiddleware,
    allow_origins=['*'],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

class RequestBody(BaseModel):
    prompt: str
    speaker_index: int
    output_file_name: str

@app.post("/api/generate", status_code=201)
def run_task(request_body: RequestBody):
    task = start_task.delay(
        request_body.prompt, request_body.speaker_index, request_body.output_file_name)
    return JSONResponse({"task_id": task.id})

@app.get("/api/tasks/{task_id}")
def get_status(task_id):
    task_result = AsyncResult(task_id)
    if task_result.failed():
        return JSONResponse({
            "task_id": task_id,
            "task_status": task_result.state,
            "exception": task_result.result.args[0]
        })
    result = {
        "task_id": task_id,
        "task_status": task_result.state,
        "task_result": task_result.result
    }
    return JSONResponse(result)


#!/usr/bin/env python
# -*- encoding: utf-8 -*-
from deal import Deal
from tuangou import Tuangou

class Tongcheng(Tuangou):

    def __init__(self):
        #cityID=2 means beijing
        api = "http://open.t.58.com/api/products?city=bj"
        n = "58tuan"
        chname = '58同城团'
        Tuangou.__init__(self, api=api, n=n, chname=chname)

    def parseDeals(self):

        assert self.json

        items = self.json['urlset']['url']

        for item in items:

            url = item['loc']
            item = item['data']['display']

            d = {
                'title': item['title'],
                'price': item['price'],
                'value': item['value'],
                'cate': item['category'],
                'subcate': item['tag'],
                'url': url,
                'time': item["startTime"],
            }

            if self.timestamp > int(d['time']):
                continue

            deal = Deal().create_item(d)
            if len(deal.title) * len(deal.cate) * len(deal.subcate) == 0:
                continue

            self.deals.append(deal)

if __name__ == '__main__':
    Tongcheng().start()


from django.shortcuts import render
from django.views.generic import View
from infoapp.utils import is_json
from infoapp.mixins import HttpResponseMixin,SerializeMixin,GetObjectById
import json
from infoapp.models import Student
from django.views.decorators.csrf import csrf_exempt
from django.utils.decorators import method_decorator
from infoapp.forms import StudentForm

@method_decorator(csrf_exempt,name='dispatch')
class StudentCRUD(HttpResponseMixin,SerializeMixin,GetObjectById,View):
    def get(self,request,*args,**kwargs):
        data=request.body
        valid_json=is_json(data)
        if not valid_json:
            return self.render_http_response(json.dumps({'msg':'please provide valid json data'}),status=400)
        p_data=json.loads(data)
        id=p_data.get('id',None)
        if id is not None:
            std=self.get_object_by_id(id)
            if std is None:
                return self.render_http_response(json.dumps({'msg':'not matched record found with given id'}),status=400)
            json_data=self.serialize([std,])
            return self.render_http_response(json_data)
        qs=Student.objects.all()
        json_data=self.serialize(qs)
        return self.render_http_response(json_data)
    def post(self,request,*args,**kwargs):
        data=request.body
        valid_json=is_json(data)
        if not valid_json:
            return self.render_http_response(json.dumps({'msg':'please provide valid json data'}),status=400)
        std_data=json.loads(data)
        form=StudentForm(std_data)
        if form.is_valid():
            form.save(commit=True)
            return self.render_http_response(json.dumps({'msg':'Resource created successfully'}))
        if form.errors:
            json_data=json.dumps(form.errors)
            return self.render_http_response(json_data,status=400)
    def put(self,request,*args,**kwargs):
        data=request.body
        valid_json=is_json(data)
        if not valid_json:
            return self.render_http_response(json.dumps({'msg':'please provide valid json data'}),status=400)
        provided_data=json.loads(data)
        id=provided_data.get('id',None)
        if id is None:
            return self.render_http_response(json.dumps({'msg':'To perfrom updation id is mandatory'}),status=400)
        std=self.get_object_by_id(id)
        if std is None:
                return self.render_http_response(json.dumps({'msg':'No matched record found with the given id'}),status=400)
        original_date={
        'name':std.name,
        'rollno':std.rollno,
        'marks':std.marks,
        'location':std.location,
        }
        original_date.update(provided_data)
        form=StudentForm(original_date,instance=std)
        if form.is_valid():
            form.save(commit=True)
            return self.render_http_response(json.dumps({'msg':'Resources updated successfully'}))
        if form.errors:
            json_data=json.dumps(form.errors)
            return self.render_http_response(json_data,status=400)
    def delete(self,request,*args,**kwargs):
        data=request.body
        valid_json=is_json(data)
        if not valid_json:
            return self.render_http_response(json.dumps({'msg':'Please provide valid json data'}),status=400)
        p_data=json.loads(data)
        id=p_data.get('id',None)
        if id is None:
            return self.render_http_response(json.dumps({'msg':'To deletion id is mandatory please provide'}),status=400)
        std=self.get_object_by_id(id)
        if std is None:
            return self.render_http_response(json.dumps({'msg':'No matched record found with the given id'}),status=400)
        status,deleted_item=std.delete()
        if status==1:
            json_data=json.dumps({'msg':'Resource Deleted successfully'})
            return self.render_http_response(json_data)
        json_data=json.dumps({'msg':'unable to delte.. plzz try again'})
        return self.render_http_response(json_data,status=500)


from requests import Request, post, put, get
from VinylShop.spotify_api.playlist import Playlist
from  VinylShop.spotify_api.song import Song
from pickle import *
from VinylShop.spotify_api.credentials import *
from flask import redirect
from copy import deepcopy
from flask import session

BASE_URL = "https://api.spotify.com/v1/"

null = None
false = False
true = True

availableVinyls = {'ab67616d0000b273692d9189b2bd75525893f0c1':"The Beatles",
                       'ab67616d0000b273370c12f82872c9cfaee80193':"Tame Impala",
                       'ab67616d0000b2734ada80e2e0d11d8f6a99d42e':"Pink Floyd"}

EXAMPLE_PLAYLISTS_RESPONSE = {
  "href": "https://api.spotify.com/v1/users/9zjte07qm6qxb3ynn9bm6atiu/playlists?offset=0&limit=3",
  "items": [
    {
      "collaborative": false,
      "description": "",
      "external_urls": {
        "spotify": "https://open.spotify.com/playlist/5p4U0hdrqumFpgpfIN30Eq"
      },
      "href": "https://api.spotify.com/v1/playlists/5p4U0hdrqumFpgpfIN30Eq",
      "id": "5p4U0hdrqumFpgpfIN30Eq",
      "images": [
        {
          "height": 640,
          "url": "https://mosaic.scdn.co/640/ab67616d0000b273176e82d09ac75d62810f0056ab67616d0000b273692d9189b2bd75525893f0c1ab67616d0000b273bfa99afb5ef0d26d5064b23bab67616d0000b273ca69f52416a728ebd0b9103c",
          "width": 640
        },
        {
          "height": 300,
          "url": "https://mosaic.scdn.co/300/ab67616d0000b273176e82d09ac75d62810f0056ab67616d0000b273692d9189b2bd75525893f0c1ab67616d0000b273bfa99afb5ef0d26d5064b23bab67616d0000b273ca69f52416a728ebd0b9103c",
          "width": 300
        },
        {
          "height": 60,
          "url": "https://mosaic.scdn.co/60/ab67616d0000b273176e82d09ac75d62810f0056ab67616d0000b273692d9189b2bd75525893f0c1ab67616d0000b273bfa99afb5ef0d26d5064b23bab67616d0000b273ca69f52416a728ebd0b9103c",
          "width": 60
        }
      ],
      "name": "T6 - Big Chungus Waz Ere",
      "owner": {
        "display_name": "johncaffrey",
        "external_urls": {
          "spotify": "https://open.spotify.com/user/creedbrattonheymah"
        },
        "href": "https://api.spotify.com/v1/users/creedbrattonheymah",
        "id": "creedbrattonheymah",
        "type": "user",
        "uri": "spotify:user:creedbrattonheymah"
      },
      "primary_color": null,
      "public": false,
      "snapshot_id": "NDcsN2FhYTU1MGUwMGZkODVkYjY5MjNjZWExMTY3MDI4YWZjY2FkYjI3OQ==",
      "tracks": {
        "href": "https://api.spotify.com/v1/playlists/5p4U0hdrqumFpgpfIN30Eq/tracks",
        "total": 69
      },
      "type": "playlist",
      "uri": "spotify:playlist:5p4U0hdrqumFpgpfIN30Eq"
    },
    {
      "collaborative": false,
      "description": "",
      "external_urls": {
        "spotify": "https://open.spotify.com/playlist/2LhU2BKMxiPDHjdM4QWzrq"
      },
      "href": "https://api.spotify.com/v1/playlists/2LhU2BKMxiPDHjdM4QWzrq",
      "id": "2LhU2BKMxiPDHjdM4QWzrq",
      "images": [
        {
          "height": 640,
          "url": "https://mosaic.scdn.co/640/ab67616d0000b273176e82d09ac75d62810f0056ab67616d0000b2732557f5ed1ca07d5111d49b93ab67616d0000b27334ef8f7d06cf2fc2146f420aab67616d0000b27358267bd34420a00d5cf83a49",
          "width": 640
        },
        {
          "height": 300,
          "url": "https://mosaic.scdn.co/300/ab67616d0000b273176e82d09ac75d62810f0056ab67616d0000b2732557f5ed1ca07d5111d49b93ab67616d0000b27334ef8f7d06cf2fc2146f420aab67616d0000b27358267bd34420a00d5cf83a49",
          "width": 300
        },
        {
          "height": 60,
          "url": "https://mosaic.scdn.co/60/ab67616d0000b273176e82d09ac75d62810f0056ab67616d0000b2732557f5ed1ca07d5111d49b93ab67616d0000b27334ef8f7d06cf2fc2146f420aab67616d0000b27358267bd34420a00d5cf83a49",
          "width": 60
        }
      ],
      "name": "Stephen’s Opti 24th Sep 2021",
      "owner": {
        "display_name": "Paulcaff123",
        "external_urls": {
          "spotify": "https://open.spotify.com/user/9zjte07qm6qxb3ynn9bm6atiu"
        },
        "href": "https://api.spotify.com/v1/users/9zjte07qm6qxb3ynn9bm6atiu",
        "id": "9zjte07qm6qxb3ynn9bm6atiu",
        "type": "user",
        "uri": "spotify:user:9zjte07qm6qxb3ynn9bm6atiu"
      },
      "primary_color": null,
      "public": false,
      "snapshot_id": "MjM1LDM0MzM4M2UxMWVkYjQwZDc2OTk5NDM2NzgxM2IyNjE5NzY3OTA3NWE=",
      "tracks": {
        "href": "https://api.spotify.com/v1/playlists/2LhU2BKMxiPDHjdM4QWzrq/tracks",
        "total": 70
      },
      "type": "playlist",
      "uri": "spotify:playlist:2LhU2BKMxiPDHjdM4QWzrq"
    },
    {
      "collaborative": false,
      "description": "The songs that influenced everything that has happened since. Cover: Jimi Hendrix",
      "external_urls": {
        "spotify": "https://open.spotify.com/playlist/37i9dQZF1DX5qNE4zrflL7"
      },
      "href": "https://api.spotify.com/v1/playlists/37i9dQZF1DX5qNE4zrflL7",
      "id": "37i9dQZF1DX5qNE4zrflL7",
      "images": [
        {
          "height": null,
          "url": "https://i.scdn.co/image/ab67706f00000003a37256f94f873658dfd43bb6",
          "width": null
        }
      ],
      "name": "Alternative 60s",
      "owner": {
        "display_name": "Spotify",
        "external_urls": {
          "spotify": "https://open.spotify.com/user/spotify"
        },
        "href": "https://api.spotify.com/v1/users/spotify",
        "id": "spotify",
        "type": "user",
        "uri": "spotify:user:spotify"
      },
      "primary_color": null,
      "public": false,
      "snapshot_id": "MTYzMzQ5MTc5MiwwMDAwMDAwNzAwMDAwMTdjNTNiMmIzZGEwMDAwMDE3YzUzOWVjNDgw",
      "tracks": {
        "href": "https://api.spotify.com/v1/playlists/37i9dQZF1DX5qNE4zrflL7/tracks",
        "total": 80
      },
      "type": "playlist",
      "uri": "spotify:playlist:37i9dQZF1DX5qNE4zrflL7"
    }
  ],
  "limit": 3,
  "next": "https://api.spotify.com/v1/users/9zjte07qm6qxb3ynn9bm6atiu/playlists?offset=3&limit=3",
  "offset": 0,
  "previous": null,
  "total": 71
}

EXAMPLE_RESPONSE = {
  "items": [
    {
      "track": {
        "album": {
          "artists": [
            {
              "external_urls": {
                "spotify": "https://open.spotify.com/artist/3WrFJ7ztbogyGnTHbHJFl2"
              },
              "href": "https://api.spotify.com/v1/artists/3WrFJ7ztbogyGnTHbHJFl2",
              "id": "3WrFJ7ztbogyGnTHbHJFl2",
              "name": "The Beatles",
              "type": "artist",
              "uri": "spotify:artist:3WrFJ7ztbogyGnTHbHJFl2"
            }
          ],
          "href": "https://api.spotify.com/v1/albums/2BtE7qm1qzM80p9vLSiXkj",
          "id": "2BtE7qm1qzM80p9vLSiXkj",
          "name": "Magical Mystery Tour (Remastered)"
        },
        "href": "https://api.spotify.com/v1/tracks/0qHMhBZqYb99yhX9BHcIkV",
        "id": "0qHMhBZqYb99yhX9BHcIkV",
        "name": "Magical Mystery Tour - Remastered 2009"
      }
    },
      {
          "track": {
              "album": {
                  "artists": [
                      {
                          "external_urls": {
                              "spotify": "https://open.spotify.com/artist/16eRpMNXSQ15wuJoeqguaB"
                          },
                          "href": "https://api.spotify.com/v1/artists/16eRpMNXSQ15wuJoeqguaB",
                          "id": "16eRpMNXSQ15wuJoeqguaB",
                          "name": "The Flaming Lips",
                          "type": "artist",
                          "uri": "spotify:artist:16eRpMNXSQ15wuJoeqguaB"
                      }
                  ],
                  "href": "https://api.spotify.com/v1/albums/49LA20VMk65fQyEaIzYdvf",
                  "id": "49LA20VMk65fQyEaIzYdvf",
                  "name": "Yoshimi Battles the Pink Robots"
              },
              "href": "https://api.spotify.com/v1/tracks/0ScgmigVOJr2mFsAtwFQmz",
              "id": "0ScgmigVOJr2mFsAtwFQmz",
              "name": "Fight Test"
          }
      },
{
      "track": {
        "album": {
          "artists": [
            {
              "external_urls": {
                "spotify": "https://open.spotify.com/artist/16eRpMNXSQ15wuJoeqguaB"
              },
              "href": "https://api.spotify.com/v1/artists/16eRpMNXSQ15wuJoeqguaB",
              "id": "16eRpMNXSQ15wuJoeqguaB",
              "name": "The Flaming Lips",
              "type": "artist",
              "uri": "spotify:artist:16eRpMNXSQ15wuJoeqguaB"
            }
          ],
          "href": "https://api.spotify.com/v1/albums/49LA20VMk65fQyEaIzYdvf",
          "id": "49LA20VMk65fQyEaIzYdvf",
          "name": "Yoshimi Battles the Pink Robots"
        },
        "href": "https://api.spotify.com/v1/tracks/0ScgmigVOJr2mFsAtwFQmz",
        "id": "0ScgmigVOJr2mFsAtwFQmz",
        "name": "Fight Test"
      }
    },
    {
      "track": {
        "album": {
          "artists": [
            {
              "external_urls": {
                "spotify": "https://open.spotify.com/artist/0epOFNiUfyON9EYx7Tpr6V"
              },
              "href": "https://api.spotify.com/v1/artists/0epOFNiUfyON9EYx7Tpr6V",
              "id": "0epOFNiUfyON9EYx7Tpr6V",
              "name": "The Strokes",
              "type": "artist",
              "uri": "spotify:artist:0epOFNiUfyON9EYx7Tpr6V"
            }
          ],
          "href": "https://api.spotify.com/v1/albums/2xkZV2Hl1Omi8rk2D7t5lN",
          "id": "2xkZV2Hl1Omi8rk2D7t5lN",
          "name": "The New Abnormal"
        },
        "href": "https://api.spotify.com/v1/tracks/5ruzrDWcT0vuJIOMW7gMnW",
        "id": "5ruzrDWcT0vuJIOMW7gMnW",
        "name": "The Adults Are Talking"
      }
    }
  ]
}

at = None

def get_auth_code():
    scopes = 'playlist-read-private playlist-read-collaborative user-library-read user-top-read user-read-private user-read-email'

    url = Request('GET', 'https://accounts.spotify.com/authorize',
                  params={
                      'scope': scopes,
                      'response_type': 'code',
                      'redirect_uri': REDIRECT_URI,
                      'client_id': CLIENT_ID
                  }).prepare().url

    return url

def spotify_callback(request):
    code = request.args['code']

    response = post('https://accounts.spotify.com/api/token', data={
        'grant_type': 'authorization_code',
        'code': code,
        'redirect_uri': REDIRECT_URI,
        'client_id': CLIENT_ID,
        'client_secret': CLIENT_SECRET
    }).json()

    access_token = response.get('access_token')
    token_type = response.get('token_type')
    refresh_token = response.get('refresh_token')
    expires_in = response.get('expires_in')
    error = response.get('error')

    global at

    at = access_token

    return redirect(SERVER_URL+"/displayPlaylists/" + str(access_token))

def spotify_api_request(endpoint, at, post_=False, put_=False):
    #access_token = at  # FIX - User token
    access_token = session.get('access_token')
    headers = {'Content-Type': 'application/json',
               'Authorization': "Bearer " + access_token}

    if post_:
        post(BASE_URL + endpoint, headers=headers)
    if put_:
        put(BASE_URL + endpoint, headers=headers)

    response = get(BASE_URL + endpoint, {}, headers=headers)
    try:
        return response.json()
    except:
        return {'Error': 'Issue with request'}

def get_playlists(at):
    endpoint = "me/playlists"  # "users/user_id/playlists"
    response = spotify_api_request(endpoint, at)

    if 'error' in response or 'items' not in response:
        print("ERROR IN RESPONSE FOR PLAYLISTS")
        return response, None  # FIX: RETURN AN ERROR

    playlists_dictionary = response.get('items')

    playlists = list()

    for playlist in playlists_dictionary:
        playlists.append({'name': playlist['name'], 'id': playlist['id'],
                          'playlist_img': playlist['images'][0]['url'][27:]})

    return playlists

def get_songs_in_playlist(playlist_id, at, playlist_obj=None):
    playlist = playlist_obj
    if playlist is None:
        print("Printing")
        playlist = uploadPlaylist(playlist_id, at, save_playlist=False)

    songs = list()

    for song in playlist.songs:
        songs.append({'name': song.name, 'artist': song.artist,
                      'album': song.album, 'album_img': song.album_img, 'artist_img': song.artist_img})

    return songs

def make_playlist_objects():
    playlist_ids, playlist_names = get_playlists()
    playlist_objects = list()
    for i in range(len(playlist_ids)):
        playlistID = playlist_ids[i]
        playlistName = playlist_names[i]
        newPlaylist = Playlist(playlistName, playlistID)
        playlist_objects.append(newPlaylist)

def find_most_popular_album(playlist):
    albums = dict()
    highest_album_amount = 0
    highest_album_name = ""
    for song in playlist.songs:
        album = song.album
        if album in albums:
            albums[album] += 1
        else:
            albums[album] = 1
        if albums[album] > highest_album_amount:
            highest_album_name = album
            highest_album_amount = albums[album]
    print(highest_album_amount, highest_album_name)

def getTopArtists(playlist):
    artists = dict()
    playlist_songs = deepcopy(playlist.songs)
    for song in playlist_songs:
        artist = song.artist
        if artist in artists:
            artists[artist]['count'] += 1
        else:
            artists[artist] = {'count':1, 'song':song}
    sortedArtists = sorted(artists, key=lambda i: artists[i]['count'], reverse=True)
    orderedArtists = list()
    position = 0
    for artist in sortedArtists:
        song_of_artist = artists[artist]['song']
        artist_img = song_of_artist.artist_img
        count = artists[artist]['count']
        artist_info = {'name': artist, 'artist_img': artist_img, 'count': count, 'position': str(position)}
        position += 1
        orderedArtists.append(artist_info)
    return orderedArtists

def getTopAlbums(playlist):
    albums = dict()
    playlist_songs = deepcopy(playlist.songs)
    for song in playlist_songs:
        album = song.album
        if album in albums:
            albums[album]['count'] += 1
        else:
            albums[album] = {'count': 1, 'song': song}
    sortedAlbums = sorted(albums, key=lambda i: albums[i]['count'], reverse=True)
    orderedAlbums = list()
    for album in sortedAlbums:
        song_of_album = albums[album]['song']
        album_img = song_of_album.album_img
        artist = song_of_album.artist
        count = albums[album]['count']
        album_info = {'name': album, 'album_img': album_img, 'artist': artist, 'count': count}
        orderedAlbums.append(album_info)
    return orderedAlbums

def find_most_popular_artist(playlist):
    artists = dict()
    highest_artist_amount = 0
    highest_artist_name = ""
    for song in playlist.songs:
        artist = song.artist
        if artist in artists:
            artists[artist] += 1
        else:
            artists[artist] = 1
        if artists[artist] > highest_artist_amount:
            highest_artist_name = artist
            highest_artist_amount = artists[artist]
    print(highest_artist_name, highest_artist_amount)

def getArtistImages(artistIDs, accessToken=None):
    global at
    if accessToken is None:
        accessToken = at

    artist_id_string = ""

    count = 0
    for artist in artistIDs:
        if count == 0:
            artist_id_string += artist
        else:
            artist_id_string += "%2C" + artist
        count += 1

    endpoint = "artists?ids=" + artist_id_string
    response = spotify_api_request(endpoint, accessToken)

    if 'error' in response or 'artists' not in response:
        print(response)
        return  # FIX: RETURN AN ERROR

    artists_list = response.get('artists')
    artistImgs = list()

    for artist in artists_list:
        artistImgs.append(artist['images'][0]['url'][24:])

    print("Artist imgs", artistImgs)

    return artistImgs

def updateArtistImages(playlist):
    artistIDs = list()

    songs = playlist.songs

    for song in songs:
        artistIDs.append(song.artistID)

    artistImgs = getArtistImages(artistIDs)

    count = 0
    for song in songs:
        song.artist_img = artistImgs[count]
        count += 1

def uploadPlaylist(playlist_id, at, playlist_name='Playlist', playlist_img='None', save_playlist=False):

    endpoint = "playlists/" + playlist_id + "/tracks?fields=items(track(name%2Chref%2Calbum(name%2Chref%2Cimages%2C%20artists)))&limit=50"
    response = spotify_api_request(endpoint, at)

    if 'error' in response or 'items' not in response:
        print(response)
        return  # FIX: RETURN AN ERROR

    songs_dictionary = response.get('items')

    songs = list()

    for song in songs_dictionary:
        track = song['track']
        name = track['name']
        song_id = 2
        album = track['album']['name']
        artist = track['album']['artists'][0]['name']
        artistID = track['album']['artists'][0]['id']
        artist_img = track['album']['images'][0]['url'][24:]
        album_img = track['album']['images'][0]['url'][24:]

        newSong = Song(name, song_id, album, artist, artistID, artist_img=artist_img, album_img=album_img)

        songs.append(newSong)

    playlist_obj = Playlist(playlist_name, playlist_id, img=playlist_img)
    playlist_obj.songs = songs

    if save_playlist:
        print("Saved playlist to database") # FIX DATABASE STUFF

    updateArtistImages(playlist_obj)

    return playlist_obj

def getAvailableVinylsByArtist(artist_name):
    global availableVinyls
    vinyls = list()
    for vinyl in availableVinyls:
        if availableVinyls[vinyl] == artist_name:
            vinyls.append(vinyl)

    return vinyls

def checkAlbumAvailability(album):
    global availableVinyls
    if album in availableVinyls:
        return True
    return False


"""A setuptools based setup module.
See:
https://packaging.python.org/en/latest/distributing.html
https://github.com/pypa/sampleproject
"""

# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

# Get the long description from the README file
with open(path.join(here, 'README.md'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='alix',
    version='0.5',
    description='Alix the tiny micro services for Python',
    long_description=long_description,
    url='https://github.com/boudartjj/alix',
    author='Jean-Jacques Boudart',
    author_email='jean-jacques@boudart.eu',
    license='MIT',
    classifiers=[
        'Development Status :: 3 - Alpha',
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Build Tools',
        'License :: OSI Approved :: MIT License',
        'Programming Language :: Python :: 3.6',
    ],
    keywords='microservices framework',
    packages=['alix'],
    install_requires=['redis'],
)


from __future__ import division

import os
import sys
import math

ROOT = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(ROOT, '..'))

from pygooglechart import Chart
from pygooglechart import SimpleLineChart
from pygooglechart import XYLineChart
from pygooglechart import SparkLineChart
from pygooglechart import Axis

import settings
import helper

def simple_random():
    chart = SimpleLineChart(settings.width, settings.height, y_range=(0, 100))
    chart.add_data(helper.random_data())
    chart.download('line-simple-random.png')

def xy_random():
    chart = XYLineChart(settings.width, settings.height,
                        x_range=(0, 100), y_range=(0, 100))
    chart.add_data(helper.random_data())
    chart.add_data(helper.random_data())
    chart.download('line-xy-random.png')

def xy_rect():
    chart = XYLineChart(settings.width, settings.height,
                        x_range=(0, 100), y_range=(0, 100))
    chart.add_data([10, 90, 90, 10, 10])
    chart.add_data([10, 10, 90, 90, 10])
    chart.download('line-xy-rect.png')

def xy_circle():
    chart = XYLineChart(settings.width, settings.height,
                        x_range=(0, 100), y_range=(0, 100))
    steps = 40
    xradius = 25
    yradius = 45
    xmid = 50
    ymid = 50
    xlist = []
    ylist = []
    for angle in range(0, steps + 1):
        angle = float(angle) / steps * math.pi * 2
        xlist.append(math.cos(angle) * xradius + xmid)
        ylist.append(math.sin(angle) * yradius + ymid)
    chart.add_data(xlist)
    chart.add_data(ylist)
    chart.download('line-xy-circle.png')

def sparklines():
    chart = SparkLineChart(settings.width, settings.height)
    chart.add_data(helper.random_data())
    chart.download('line-sparkline.png')

def fill():

    # Set the vertical range from 0 to 50
    max_y = 50
    chart = SimpleLineChart(200, 125, y_range=[0, max_y])

    # First value is the highest Y value. Two of them are needed to be
    # plottable.
    chart.add_data([max_y] * 2)

    # 3 sets of real data
    chart.add_data([28, 30, 31, 33, 35, 36, 42, 48, 43, 37, 32, 24, 28])
    chart.add_data([16, 18, 18, 21, 23, 23, 29, 36, 31, 25, 20, 12, 17])
    chart.add_data([7, 9, 9, 12, 14, 14, 20, 27, 21, 15, 10, 3, 7])

    # Last value is the lowest in the Y axis.
    chart.add_data([0] * 2)

    # Black lines
    chart.set_colours(['000000'] * 5)

    # Filled colours
    # from the top to the first real data
    chart.add_fill_range('76A4FB', 0, 1)

    # Between the 3 data values
    chart.add_fill_range('224499', 1, 2)
    chart.add_fill_range('FF0000', 2, 3)

    # from the last real data to the
    chart.add_fill_range('80C65A', 3, 4)

    # Some axis data
    chart.set_axis_labels(Axis.LEFT, ['', max_y / 2, max_y])
    chart.set_axis_labels(Axis.BOTTOM, ['Sep', 'Oct', 'Nov', 'Dec'])

    chart.download('line-fill.png')

def stripes():

    # Set the vertical range from 0 to 100
    max_y = 100

    # Chart size of 200x125 pixels and specifying the range for the Y axis
    chart = SimpleLineChart(200, 125, y_range=[0, max_y])

    # Add the chart data
    data = [
        32, 34, 34, 32, 34, 34, 32, 32, 32, 34, 34, 32, 29, 29, 34, 34, 34, 37,
        37, 39, 42, 47, 50, 54, 57, 60, 60, 60, 60, 60, 60, 60, 62, 62, 60, 55,
        55, 52, 47, 44, 44, 40, 40, 37, 34, 34, 32, 32, 32, 31, 32
    ]
    chart.add_data(data)

    # Set the line colour to blue
    chart.set_colours(['0000FF'])

    # Set the vertical stripes
    chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.2, 'FFFFFF', 0.2)

    # Set the horizontal dotted lines
    chart.set_grid(0, 25, 5, 5)

    # The Y axis labels contains 0 to 100 skipping every 25, but remove the
    # first number because it's obvious and gets in the way of the first X
    # label.
    left_axis = list(range(0, max_y + 1, 25))
    left_axis[0] = ''
    chart.set_axis_labels(Axis.LEFT, left_axis)

    # X axis labels
    chart.set_axis_labels(Axis.BOTTOM, \
        ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'])

    chart.download('line-stripes.png')

def main():
    simple_random()
    xy_random()
    xy_rect()
    xy_circle()
    sparklines()
    fill()
    stripes()

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 10 22:02:23 2020

@author: arti
"""

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

titanic = pd.read_csv('titanic.csv')

sns.set_style('darkgrid')

fig = plt.figure(figsize=(15, 5))
ax1 = fig.add_subplot(1, 3, 1)
ax2 = fig.add_subplot(1, 3, 2)
ax3 = fig.add_subplot(1, 3, 3)

sns.distplot(titanic['fare'], ax=ax1)
sns.distplot(titanic['fare'], hist=False, ax=ax2)
sns.distplot(titanic['fare'], kde=False, ax=ax3)

ax1.set_title('titanic fare - hist/ked')
ax2.set_title('titanic fare - ked')
ax3.set_title('titanic fare - hist')

plt.show()


import os
import sys

import pygame

from level_builder.level_builder import LevelBuilder
from routes.route import Route
from routes.singleplayer import Singleplayer
from utils.button import Button

class LevelBuilderMenu(Route):

    def __init__(self):
        self.color = (200, 200, 200)
        self.menu_button = Button("MENU", 15, (10, 10, 60, 30), (0, 200, 0), (0, 150, 0))
        self.add_button = Button("ADD NEW", 30, (300, 10, 400, 80), (0, 200, 0), (0, 150, 0))
        self.is_add_active = True
        self.level_buttons = []
        self.del_buttons = []
        filenames = sorted(os.listdir("boards/builder_boards"))
        for i, name in enumerate(filenames):
            self.level_buttons.append(
                Button(name.upper()[:-4], 30, (300, 130 + i * 100, 400, 80), (0, 0, 200), (0, 0, 150))
            )
            self.del_buttons.append(
                Button("X", 30, (750, 150 + i * 100, 50, 40), (200, 0, 0), (150, 0, 0))
            )

    def draw(self, win: pygame.Surface) -> None:
        win.fill(self.color)
        self.menu_button.draw(win)
        self.add_button.draw(win)
        for button in self.level_buttons:
            button.draw(win)
        for del_button in self.del_buttons:
            del_button.draw(win)

    def update_state(self) -> 'Route':
        if len(self.level_buttons) >= 5:
            self.is_add_active = False
            self.add_button.color = (100, 100, 100)
            self.add_button.hover_color = (70, 70, 70)
        else:
            self.is_add_active = True
            self.add_button.color = (0, 200, 0)
            self.add_button.hover_color = (0, 150, 0)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit(0)
            if self.menu_button.is_mouse(event):
                from routes.menu import Menu
                return Menu((1000, 700))
            if self.add_button.is_mouse(event) and self.is_add_active:
                maxi = 0
                for button in self.level_buttons:
                    s = button.text.split()
                    maxi = max(int(s[1]), maxi)
                return LevelBuilder(maxi + 1)
            for button in self.level_buttons:
                if button.is_mouse(event):
                    return Singleplayer(button.text.lower())
            for i, del_button in enumerate(self.del_buttons):
                if del_button.is_mouse(event):
                    self.del_level(i, del_button)
        return self

    def del_level(self, i: int, del_butt: Button):
        self.del_level_file(self.level_buttons[i].text.lower())
        self.del_buttons.remove(del_butt)
        del self.level_buttons[i]
        for i, button in enumerate(self.level_buttons):
            if button.rect.y > del_butt.rect.y:
                button.move_y(-100)
                self.del_buttons[i].move_y(-100)

    @staticmethod
    def del_level_file(text: str):
        os.remove(f"boards/builder_boards/{text}.txt")


import os
import json
import time
import copy
import importlib
import argparse
import scipy.misc as misc
import numpy as np
import skimage.measure as measure
from collections import OrderedDict
from PIL import Image

import torch
import torch.nn as nn
from torch.autograd import Variable
from dataset import TestDataset

def psnr(im1, im2):
    def im2double(im):
        min_val, max_val = 0, 255
        out = (im.astype(np.float64)-min_val) / (max_val-min_val)
        return out

    im1 = im2double(im1)
    im2 = im2double(im2)
    psnr = measure.compare_psnr(im1, im2, data_range=1)
    return psnr

def generate_single(net, lr, hr, chunk, stage, cfg):
    scale_diff = cfg.scale_diff
    h, w = lr.size()[1:]
    h_chunk, w_chunk = int(h/chunk), int(w/chunk)
    h_chop, w_chop   = h_chunk + cfg.shave, w_chunk + cfg.shave

    lr_patch = torch.FloatTensor(chunk**2, 3, h_chop, w_chop)
    for i in range(chunk):
        for j in range(chunk):
            h_from, h_to = i*h_chunk, (i+1)*h_chunk+cfg.shave
            w_from, w_to = j*w_chunk, (j+1)*w_chunk+cfg.shave
            if (i+1) == chunk: h_from, h_to = h-h_chop, h
            if (j+1) == chunk: w_from, w_to = w-w_chop, w
            lr_patch[i+j*chunk].copy_(lr[:, h_from:h_to, w_from:w_to])
    lr_patch = Variable(lr_patch, volatile=True).cuda()

    h, h_chunk, h_chop = h*scale_diff, h_chunk*scale_diff, h_chop*scale_diff
    w, w_chunk, w_chop = w*scale_diff, w_chunk*scale_diff, w_chop*scale_diff

    sr = np.empty((chunk**2, h_chop, w_chop, 3), dtype=np.uint8)
    for i, patch in enumerate(lr_patch):
        out = net(patch.unsqueeze(0), stage).data[0]
        out = out.cpu().mul(255).clamp(0, 255).byte().permute(1, 2, 0).numpy()
        sr[i] = out

    result = np.empty((h, w, 3), dtype=np.uint8)
    for i in range(chunk):
        for j in range(chunk):
            h_from, h_to = 0, h_chunk
            w_from, w_to = 0, w_chunk
            hh_from, hh_to = i*h_chunk, (i+1)*h_chunk
            ww_from, ww_to = j*w_chunk, (j+1)*w_chunk

            if (i+1) == chunk:
                h_from, h_to = -h_chunk-(h-(i+1)*h_chunk), None
                hh_from, hh_to = i*h_chunk, None
            if (j+1) == chunk:
                w_from, w_to = -w_chunk-(w-(j+1)*w_chunk), None
                ww_from, ww_to = j*w_chunk, None

            result[hh_from:hh_to, ww_from:ww_to, :] = \
                copy.deepcopy(sr[i+j*chunk, h_from:h_to, w_from:w_to, :])
    return result

def recover_origin(image, index):
    angle = [0, 3, 2, 1]
    image = np.rot90(image, angle[index%4]).copy()
    if index > 3:
        image = np.fliplr(image).copy()

    return image

def evaluate(net, dataset, chunk, stage, cfg):
    net.eval()
    scale_diff = cfg.scale_diff
    mean_psnr, mean_runtime = 0, 0
    for step, (lr_ensemble, hr, name) in enumerate(dataset):
        hr = hr.cpu().mul(255).clamp(0, 255).byte().permute(1, 2, 0).numpy()

        h, w = lr_ensemble[0].size()[1:]
        sr_ensemble = np.zeros((len(lr_ensemble), h*scale_diff, w*scale_diff, 3))

        t1 = time.time()
        for i, lr in enumerate(lr_ensemble):
            tmp_image = generate_single(net, lr, hr, chunk, stage, cfg)
            sr_ensemble[i] = recover_origin(tmp_image, i)

        sr = np.mean(sr_ensemble, axis=0)
        sr = np.clip(sr, 0, 255)

        t2 = time.time()

        # match resolution when stage is less then two
        if int(cfg.scales[0]/scale_diff) > 1:
            sr = misc.imresize(sr, cfg.scales[0]/scale_diff)

        # crop HR to match SR
        hr = hr[:sr.shape[0], :sr.shape[1]]
        bnd = int(cfg.scales[0]/cfg.scales[-1]) + 6
        hr = hr[bnd:-bnd, bnd:-bnd]
        sr = sr[bnd:-bnd, bnd:-bnd]

        mean_psnr += psnr(hr, sr) / len(dataset)
        mean_runtime += (t2-t1) / len(dataset)

    return mean_runtime, mean_psnr

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", type=str)

    parser.add_argument("--dirname", type=str)
    parser.add_argument("--data_from", type=str)
    parser.add_argument("--data_to", type=str)

    parser.add_argument("--ckpt_path", type=str)
    parser.add_argument("--shave", type=int, default=20)

    parser.add_argument("--chunk", type=int)
    parser.add_argument("--stage", type=int)

    return parser.parse_args()

def main(cfg):
    cfg.scales = list()
    cfg.scales.append([int(s) for s in cfg.data_from if s.isdigit()][-1])
    if "HR" in cfg.data_to:
        cfg.scales.append(1)
    else:
        cfg.scales.append([int(s) for s in cfg.data_to if s.isdigit()][-1])
    cfg.scale_diff = 8

    module = importlib.import_module("model.{}".format(cfg.model))
    net = module.Net()
    print(json.dumps(vars(cfg), indent=4, sort_keys=True))

    state_dict = torch.load(cfg.ckpt_path)
    new_state_dict = OrderedDict()
    for k, v in state_dict.items():
        name = k
        # name = k[7:] # remove "module."
        new_state_dict[name] = v

    net.load_state_dict(new_state_dict)
    net.cuda()

    dataset = TestDataset(cfg.dirname,
                          cfg.scale_diff,
                          cfg.data_from,
                          cfg.data_to, self_ensemble=True)

    mean_runtime, mean_psnr = evaluate(net, dataset, cfg.chunk, cfg.stage, cfg)
    print("Mean runtime: {:.3f}s mean PSNR: {:.3f}".format(mean_runtime, mean_psnr))

if __name__ == "__main__":
    cfg = parse_args()
    main(cfg)