from selenium import webdriver
import time

class TextValue():
    def test_text_value(self):
        url = 'https://letskodeit.teachable.com/p/practice'
        driver = webdriver.Firefox()
        driver.maximize_window()
        driver.get(url)
        driver.implicitly_wait(5)
        ele_text = driver.find_element_by_xpath("//a[@id='opentab']")
        ele_name_text = ele_text.text
        print('The text in the element is ' + ele_name_text)
        ele_value  = driver.find_element_by_xpath("//input[@id='name']")
        print('The value of class attribute of ele_value is ' + ele_value.get_attribute("class"))
        driver.quit()

tv = TextValue()
tv.test_text_value()


import utils
import predict
import time as tm
import numpy as np

# This file is intended to demonstrate how we would evaluate your code
# The data loader needs to know how many feature dimensions are there
dictSize = 225
(X, y) = utils.loadData( "train", dictSize = dictSize )

# Get error class predictions from predict.py and time the thing
tic = tm.perf_counter()
yPred = predict.findErrorClass( X, 5 )
toc = tm.perf_counter()

print( "Total time taken is %.6f seconds " % (toc - tic) )

preck = utils.getPrecAtK( y, yPred, 5 )
# The macro precision code takes a bit longer to execute due to the for loop over labels
mpreck = utils.getMPrecAtK( y, yPred, 5 )

# According to our definitions, both prec@k and mprec@k should go up as k goes up i.e. for your
# method, prec@i > prec@j if i > j and mprec@i > mprec@j if i > j. See the assignment description
# to convince yourself why this must be the case.

print( "prec@1: %0.3f" % preck[0], "prec@3: %0.3f" % preck[2], "prec@5: %0.3f" % preck[4] )
# Dont be surprised if mprec is small -- it is hard to do well on rare error classes
print( "mprec@1: %0.3e" % mpreck[0], "mprec@3: %0.3e" % mpreck[2], "mprec@5: %0.3e" % mpreck[4] )


from mtgo_daily import get_alldecks, daterange
import datetime
import numpy as np
import pandas
import pylab as pl
import scipy.cluster

from deck_guess_AKH import deck_guess

bad_cards = ('Collected Company', "Nissa's Pilgrimage",
             "Knight of the White Orchid", "Smuggler's Copter",
             "Emrakul, the Promised End",
             "Reflector Mage",
             "Felidar Guardian",
             "Ojutai's Command", "Tragic Arrogance")

start_date = datetime.date(year=2017,month=1,day=25)

alldecks = get_alldecks(start_date=start_date)

alldecks = {date: results for date, results in alldecks.items()
            if not any(bad in deck['mainboard'] for deck in results.values() for bad in bad_cards)}

card_namespace = set(card
                     for dailies in alldecks.values()
                     for deck in dailies.values() if not any(x in {**deck['mainboard'], **deck['sideboard']} for x in bad_cards)
                     for card in {**deck['mainboard'], **deck['sideboard']})

deckcount = sum(len(x) for x in alldecks.values())

array = np.recarray(deckcount, dtype=[('ID','S20'),('Date','S10'),("Archetype","S25")]+[(name, np.int16) for name in card_namespace])

ii = 0
for date,daily in alldecks.items():
    for deckname,deck in daily.items():
        array['ID'][ii] = deckname.encode('ascii')
        array['Date'][ii] = date

        for card in card_namespace:
            if card in deck['mainboard']:
                array[card][ii] = int(deck['mainboard'][card])+10
            else:
                array[card][ii] = 0

        ii = ii+1

pd = pandas.DataFrame(array)

# get rid of the id column and date column
justdata = pd.T[3:]

distortions = []

# +1 for the "others"
guess_array = np.zeros([len(deck_guess)+1, justdata.shape[0]])
for ii,(deckname, deck) in enumerate(deck_guess.items()):
    for card in deck:
        if card not in pd.columns:
            print("Deck {0} is not represented in the meta".format(deckname))
            #raise ValueError("Card {0} is not real".format(card))
        guess_array[ii,justdata.T.keys() == card] = deck[card] + 10

codebook, distortion = scipy.cluster.vq.kmeans(np.array(justdata.T, 'float'), guess_array)
code, dist = scipy.cluster.vq.vq(np.array(justdata.T, 'int'), codebook)
#cntr, u, u0, d, jm, p, fpc = fuzz.cluster.cmeans(
#    data=justdata, c=ncenters, m=2, error=0.005, maxiter=1000, init=None)

pd['Distortion'] = dist

# Store fpc values for later
distortions.append(distortion)

cluster_membership = code

deck_50_pct = {}
deck_top20s = {}
deck_ids = {}

def top20_match(x):
    x = list(x)
    if len(x) == 0:
        return 0
    return np.sum(x)/len(x)

for ii in range(len(deck_guess)+1):
    mask = cluster_membership==ii
    deck = (justdata.T[mask] > 0).sum(axis=0)
    deck.sort_values(inplace=True)

    deck_top20s[ii] = deck

    name = None
    card_match_fraction = {dk:top20_match(k in deck.keys()[-20:] for k in ks)
                           for dk,ks in deck_guess.items()}
    bestfrac = 0
    for dk, frac in card_match_fraction.items():
        if frac > bestfrac:
            bestfrac = frac
            name = dk
    if bestfrac < 0.8:
        print("Deck {0}  has bad matches {1}".format(ii, bestfrac))
        name = None
    if name is None:
        print("Deck {0}: {1} matches, {2}%".format(ii, mask.sum(), mask.sum()/len(mask)))
        deck_ids[ii]=ii
        deck_50_pct[ii] = mask.sum()/len(mask)
        pd.ix[mask, 'Archetype'] = "Other "+str(ii)
    else:
        if name in deck_50_pct:
            print("**********DUPLICATE**********")
            pd.ix[mask, 'Archetype'] = name + str(ii)
        else:
            pd.ix[mask, 'Archetype'] = name
        deck_50_pct[name] = mask.sum()/len(mask)
        print("Deck {0}={2}: {1} matches, {3}%".format(ii, mask.sum(), name, mask.sum()/len(mask)))
        deck_ids[name]=ii
        #pd['Archetype'][mask] = name
    #print(deck[-20:])

print(len(deck_50_pct), deck_50_pct)

final = pandas.DataFrame.from_dict(list(deck_50_pct.items()))
final.sort_values(by=1, inplace=True)
print(final.sort_values(by=1))

timestep=3
week_starts = [day for day in daterange(start_date, datetime.date.today(), step=timestep)]

weekly_summary = pandas.DataFrame(index=week_starts, columns=deck_guess.keys())

dates = pandas.to_datetime(pd.Date.str.decode('ascii'))

for week_start in week_starts:
    week_end = week_start + datetime.timedelta(timestep)
    date_matches = (dates>=week_start) & (dates < week_end)
    for deck in deck_guess:
        deck_matches = (pd.Archetype == deck) & date_matches
        weekly_summary[deck][week_start] = deck_matches.sum() / date_matches.sum()

weekly_summary.plot(style=[x+'o'+y for x,y in zip('rgbcmykrgbcmykrgbcmyk', ['-']*7 + ['--']*7 + [':']*7)],
                    figsize=[24,20])
pl.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
pl.xlabel("First date in week")
pl.ylabel("Fraction of 5-0 decks in that week")
pl.savefig("MTGO_5-0_meta.png", bbox_inches='tight')

def get_deck(num):
    deck = pd.loc[num]
    return deck[deck!=0]


from copy import deepcopy, copy
import numpy as np
import random
from time import time

class GoBoard:
    def __init__(self,size,verbose=False):
        self.n_move = 0
        self.side = None
        self.board = None
        self.previous_board = None
        self.max_move = 24
        self.size = size
        self.rival_pass = False
        self.double_pass =False
        self.verbose = verbose
        self.komi = 2.5

    def set_side(self,side):
        self.side = side

    def load_from_file(self, host_file, move_file):
        board_info = host_file.readlines()
        side = int(board_info[0].strip())
        self.set_side(side)
        temp_previous_board = [[0 for x in range(self.size)] for y in range(self.size)]
        temp_board = [[0 for x in range(self.size)] for y in range(self.size)]
        for i in range(6,6+self.size):
            a,b,c,d,e = int(board_info[i][0]),int(board_info[i][1]),int(board_info[i][2]),int(board_info[i][3]),int(board_info[i][4])
            temp_board[i-6][0] = a
            temp_board[i-6][1] = b
            temp_board[i-6][2] = c
            temp_board[i-6][3] = d
            temp_board[i-6][4] = e

        cnt = self.count_pieces(temp_board)
        if cnt == 0 and self.side == 1:
            self.n_move = 0
        elif cnt == 1 and self.side == 2:
            self.n_move = 1
        else:
            for i in range(1,self.size+1):
                a, b, c, d, e = int(board_info[i][0]),int(board_info[i][1]),int(board_info[i][2]),int(board_info[i][3]),int(board_info[i][4])
                temp_previous_board[i-1][0] = a
                temp_previous_board[i-1][1] = b
                temp_previous_board[i-1][2] = c
                temp_previous_board[i-1][3] = d
                temp_previous_board[i-1][4] = e
            self.n_move = int(move_file.readline().strip())
            if temp_board != temp_previous_board:
                self.n_move += 1
            else:
                self.rival_pass = True
                self.n_move +=1

        self.board = temp_board
        self.previous_board = temp_previous_board
        host_file.close()
        move_file.close()
        return

    def count_pieces(self,board):
        cnt = 0
        for i in range(self.size):
            for j in range(self.size):
                if board[i][j] != 0:
                    cnt += 1
        return cnt

    def at_edge(self, i, j):
        if i == 0 or i == self.size-1 or j == 0 or j == self.size-1:
            return True
        return False

    def at_corner(self,i,j):
        if i == 0 or i == self.size-1:
            if j == 0 or j == self.size-1:
                return True
        return False

    def detect_neighbor(self, i, j):
        '''
        Detect all the neighbors of a given stone.

        :param i: row number of the board.
        :param j: column number of the board.
        :return: a list containing the neighbors row and column (row, column) of position (i, j).
        '''
        board = self.board
        neighbors = []
        # Detect borders and add neighbor coordinates
        if i > 0: neighbors.append((i-1, j))
        if i < len(board) - 1: neighbors.append((i+1, j))
        if j > 0: neighbors.append((i, j-1))
        if j < len(board) - 1: neighbors.append((i, j+1))
        return neighbors

    def detect_diagonal(self,i,j):
        diagonals = []
        if i>0 and j>0:
            diagonals.append((i-1,j-1))
        if i>0 and j<self.size-1:
            diagonals.append((i-1,j+1))
        if i<self.size-1 and j>0:
            diagonals.append((i+1,j-1))
        if i<self.size-1 and j<self.size-1:
            diagonals.append((i+1,j+1))
        return diagonals

    def is_eyeish(self,i,j,piece_type):
        neighbor = self.detect_neighbor(i, j)
        for pos in neighbor:
            if self.board[pos[0]][pos[1]] != piece_type:
                return False
        return True

    def is_eye(self,i, j, piece_type):
        if not self.is_eyeish(i,j,piece_type):
            return False
        diagonal = self.detect_diagonal(i,j)
        if self.at_edge(i,j):
            for pos in diagonal:
                if self.board[pos[0]][pos[1]] == 3 - piece_type:
                    return False
                elif self.board[pos[0]][pos[1]] and not self.is_eyeish(pos[0],pos[1],piece_type):
                    return False
                return True
        else:
            cnt = 0
            for pos in diagonal:
                if self.board[pos[0]][pos[1]] == piece_type:
                    cnt += 1
                elif self.board[pos[0]][pos[1]] == piece_type and self.is_eyeish([pos[0]][pos[1]],piece_type):
                    cnt +=1
                if cnt > 2:
                    return True
            return False

    def detect_neighbor_ally(self, i, j):
        '''
        Detect the neighbor allies of a given stone.

        :param i: row number of the board.
        :param j: column number of the board.
        :return: a list containing the neighbored allies row and column (row, column) of position (i, j).
        '''
        board = self.board
        neighbors = self.detect_neighbor(i, j)  # Detect neighbors
        group_allies = []
        # Iterate through neighbors
        for piece in neighbors:
            # Add to allies list if having the same color
            if board[piece[0]][piece[1]] == board[i][j]:
                group_allies.append(piece)
        return group_allies

    def ally_dfs(self, i, j):
        '''
        Using DFS to search for all allies of a given stone.

        :param i: row number of the board.
        :param j: column number of the board.
        :return: a list containing the all allies row and column (row, column) of position (i, j).
        '''
        stack = [(i, j)]  # stack for DFS serach
        ally_members = []  # record allies positions during the search
        while stack:
            piece = stack.pop()
            ally_members.append(piece)
            neighbor_allies = self.detect_neighbor_ally(piece[0], piece[1])
            for ally in neighbor_allies:
                if ally not in stack and ally not in ally_members:
                    stack.append(ally)
        return ally_members

    def find_liberty(self, i, j):
        '''
        Find liberty of a given stone. If a group of allied stones has no liberty, they all die.

        :param i: row number of the board.
        :param j: column number of the board.
        :return: boolean indicating whether the given stone still has liberty.
        '''
        board = self.board
        ally_members = self.ally_dfs(i, j)
        for member in ally_members:
            neighbors = self.detect_neighbor(member[0], member[1])
            for piece in neighbors:
                # If there is empty space around a piece, it has liberty
                if board[piece[0]][piece[1]] == 0:
                    return True
        # If none of the pieces in a allied group has an empty space, it has no liberty
        return False

    def find_liberty_for_all(self):
        '''
        Find exact liberty number for every stone on the board
        Return to a liberty board
        -1 means not applicable. Means this is a empty place
        '''
        board = self.board
        liberty_board = [[0 for x in range(self.size)] for y in range(self.size)]
        visited = set()
        for i in range(self.size):
            for j in range(self.size):
                if (i,j) in visited:
                    continue
                if board[i][j] == 0:
                    liberty_board[i][j]=-1
                else:
                    ally_members = self.ally_dfs(i,j)
                    qi = set()
                    for member in ally_members:
                        neighbors = self.detect_neighbor(member[0],member[1])
                        for piece in neighbors:
                            if board[piece[0]][piece[1]] == 0:
                                qi.add(piece)
                    total_qi = len(qi)
                    for member in ally_members:
                        liberty_board[member[0]][member[1]]=total_qi
                        visited.add(member)
        return liberty_board

    def find_died_pieces(self, piece_type):
        board = self.board
        died_pieces = []

        for i in range(len(board)):
            for j in range(len(board)):
                # Check if there is a piece at this position:
                if board[i][j] == piece_type:
                    # The piece die if it has no liberty
                    if not self.find_liberty(i, j):
                        died_pieces.append((i, j))
        return died_pieces

    def remove_died_pieces(self, piece_type):
        '''
        Remove the dead stones in the board.

        :param piece_type: 1('X') or 2('O').
        :return: locations of dead pieces.
        '''
        died_pieces = self.find_died_pieces(piece_type)
        if not died_pieces: return []
        self.remove_certain_pieces(died_pieces)
        return died_pieces

    def remove_certain_pieces(self, positions):
        '''
        Remove the stones of certain locations.

        :param positions: a list containing the pieces to be removed row and column(row, column)
        :return: None.
        '''
        board = self.board
        for piece in positions:
            board[piece[0]][piece[1]] = 0
        self.board = board

    def visualize_board(self,board):
        '''
        Visualize the board.

        :return: None
        '''
        print('-' * len(board) * 2)
        for i in range(len(board)):
            for j in range(len(board)):
                if board[i][j] == 0:
                    print(' ', end=' ')
                elif board[i][j] == 1:
                    print('X', end=' ')
                else:
                    print('O', end=' ')
            print()
        print('-' * len(board) * 2)

    def valid_place_check(self, i, j, piece_type):
        board = self.board
        verbose = False

        # Check if the place is in the board range
        if not (i >= 0 and i < len(board)):
            if verbose:
                print(('Invalid placement. row should be in the range 1 to {}.').format(len(board) - 1))
            return False
        if not (j >= 0 and j < len(board)):
            if verbose:
                print(('Invalid placement. column should be in the range 1 to {}.').format(len(board) - 1))
            return False

        # Check if the place already has a piece
        if board[i][j] != 0:
            if verbose:
                print('Invalid placement. There is already a chess in this position.')
            return False

        # Copy the board for testing
        test_go = deepcopy(self)
        test_board = test_go.board

        # Check if the place has liberty
        test_board[i][j] = piece_type
        test_go.board = test_board
        if test_go.find_liberty(i, j):
            return True

        # If not, remove the died pieces of opponent and check again
        test_go.remove_died_pieces(3 - piece_type)
        if not test_go.find_liberty(i, j):
            if verbose:
                print('Invalid placement. No liberty found in this position.')
            return False

        # Check special case: repeat placement causing the repeat board state (KO rule)
        else:
            # print("Check for KO")
            # print("potential board")
            # self.visualize_board(test_go.board)
            # print("previous board")
            # self.visualize_board(self.previous_board)
            if self.rival_pass:
                # print("Rival Pass")
                return True
            if test_go.board == self.previous_board:
                if verbose:
                    print('Invalid placement. A repeat move not permitted by the KO rule.')
                return False
        return True

    def place_chess(self, i, j, piece_type, check=True):
        board = self.board
        if i == 'P':
            if self.rival_pass:
                self.double_pass = True
            self.previous_board = deepcopy(self).board
            self.rival_pass = True
            self.n_move += 1
            return True
        if check:
            valid_place = self.valid_place_check(i, j, piece_type)
            if not valid_place:
                raise RuntimeError("Invalid Move!!!!")
                return False
        temp = deepcopy(self)
        self.previous_board = temp.board
        # print("Before")
        # self.visualize_board(self.previous_board)
        board[i][j] = piece_type
        self.board = board
        self.remove_died_pieces(3 - piece_type)
        self.n_move += 1
        self.rival_pass = False
        # print("After")
        # self.visualize_board(self.previous_board)
        return True

    def game_end(self,double_pass=False):
        if self.n_move >= self.max_move or double_pass:
            return True
        return False

    def score(self, piece_type):
        '''
        Get score of a player by counting the number of stones.

        :param piece_type: 1('X') or 2('O').
        :return: boolean indicating whether the game should end.
        '''

        board = self.board
        cnt = 0
        for i in range(self.size):
            for j in range(self.size):
                if board[i][j] == piece_type:
                    cnt += 1
        return cnt

    def advanced_evaluation(self,piece_type):
        visited = set()
        eyes_info = {}
        board = self.board
        cnt = 0
        factor = 1
        if piece_type == 2:
            factor = 1.2

        for i in range(self.size):
            for j in range(self.size):
                if board[i][j] == 0 and self.is_eye(i, j, piece_type):
                    stone = self.detect_neighbor(i, j)[0]
                    allies = self.ally_dfs(stone[0], stone[1])
                    for single in allies:
                        if single not in eyes_info:
                            eyes_info[single] = 1
                        else:
                            eyes_info[single] += 1
        for element in eyes_info:
            if eyes_info[element] == 1:
                cnt += factor * 1.0 + 1
            if eyes_info[element] > 1:
                cnt += factor * 1.5 + 1

        weight = [-0.5,0.2,0.4,0.6,0.9,1.1,1.2] # more liberty more value
        liberty_map = self.find_liberty_for_all()
        for i in range(self.size):
            for j in range(self.size):
                if board[i][j] == piece_type and (i,j) not in eyes_info:
                    qi = liberty_map[i][j]
                    qi = min(7,qi)
                    cnt += factor * weight[qi-1] + 1
                    if self.at_corner(i,j):
                        cnt -= 0.2
                    elif self.at_edge(i,j):
                        cnt -= 0.1
        return cnt

def find_possible(board, piece_type, max_branching, clean_up=True):
    '''find the possible move of current board'''
    possible_placements = []
    for i in range(board.size):
        for j in range(board.size):
            if board.valid_place_check(i, j, piece_type):
                possible_placements.append((i, j))
    possible_placements.append(('P', 'A'))
    if clean_up:
        # def distance(pos):
        #     return ((pos[0] - 2) ** 2 + (pos[1] - 2) ** 2)
        possible_placements.remove(('P','A'))
        # sorted(possible_placements, key = distance)
        # possible_placements.append(('P', 'A'))
        # return possible_placements

        normal = []
        edge = []
        corner = []
        for element in possible_placements:
            if not board.at_edge(element[0],element[1]):
                normal.append(element)
            elif not board.at_corner(element[0],element[1]):
                edge.append(element)
            else:
                corner.append(element)
        # random.shuffle(normal)
        # random.shuffle(edge)
        # random.shuffle(corner)
        final_possible = normal+edge+corner+[('P','A')]
        return final_possible[:max_branching]

    # print("Possible placement:", possible_placements)
    return possible_placements

class ABGo:
    def __init__(self, max_depth=4, max_branching=25, side=None, load=False, size=5):
        self.size = size
        self.max_depth = max_depth
        self.max_branching = max_branching
        self.side = side
        self.cur_depth = 0
        self.cur_step = 0

    def set_side(self, side):
        self.side = side

    def AB_search(self, board):
        self.cur_step = board.n_move
        if self.cur_step < 10:
            self.max_depth = 3
        elif self.cur_step < 18:
            self.max_depth = 3
        else:
            self.max_depth = 5
        self.cur_depth = 0
        temp_board = deepcopy(board)
        v, place = self.max_value(temp_board, -np.inf, np.inf)
        return place

    def max_value(self, board, alpha, beta):
        # print("Max_value called: ")
        # print("Current depth: ", self.cur_depth)
        # print("Current step: ",self.cur_step)
        komi = -2.5 if self.side == 1 else 2.5

        if board.game_end():
            return 50*(board.score(self.side) - board.score(3 - self.side)+komi), (-1, -1)

        if self.cur_depth >= self.max_depth:
            return board.advanced_evaluation(self.side) - board.advanced_evaluation(3 - self.side), (-1, -1)
            # return 50*(board.score(self.side) - board.score(3 - self.side)+komi), (-1, -1)

        possible_pos = find_possible(board,self.side,self.max_branching,clean_up=True)
        # print("Possible Pos: ",possible_pos)

        v = -np.inf
        max_pos = (-1,-1)
        for pos in possible_pos:
            temp_board = deepcopy(board)
            self.cur_depth += 1
            temp_board.place_chess(pos[0],pos[1],self.side,check=False)
            v = max(v, self.min_value(temp_board, alpha, beta))
            self.cur_depth -= 1
            if v >= beta:
                del temp_board
                return v, max_pos
            if v > alpha:
                max_pos = pos
                alpha = v

        del temp_board
        return v, max_pos

    def min_value(self,board,alpha,beta):
        # print("Min_value called")
        # print("Current depth: ", self.cur_depth)
        # print("Current step: ",self.cur_step)
        komi = -2.5 if self.side ==1 else 2.5
        if board.game_end():
            return 50 * (board.score(self.side) - board.score(3 - self.side) +komi)

        if self.cur_depth >= self.max_depth:
            # return board.weighted_score(self.side) - board.weighted_score(3 - self.side)
            # return 50 * (board.score(self.side) - board.score(3 - self.side) +komi)
            return board.advanced_evaluation(self.side) - board.advanced_evaluation(3 - self.side)

        possible_pos = find_possible(board, 3-self.side, self.max_branching, clean_up=True)
        # print("Possible Pos: ", possible_pos)

        v = np.inf
        for pos in possible_pos:
            temp_board = deepcopy(board)
            self.cur_depth += 1
            temp_board.place_chess(pos[0],pos[1],3-self.side,check=False)
            v_2, dummy = self.max_value(temp_board, alpha, beta)
            self.cur_depth -= 1
            v = min(v, v_2)
            if v <= alpha:
                del temp_board
                return v
            beta = min(beta,v)

        return v

    def move(self,board):
        if board.n_move < 2:
            if board.valid_place_check(2,2,self.side):
                return (2,2)
            if board.valid_place_check(1, 1, self.side):
                return (1,1)
            elif board.valid_place_check(3, 1, self.side):
                return (3,1)
            elif board.valid_place_check(1, 3, self.side):
                return (1,3)
            elif board.valid_place_check(3, 3, self.side):
                return (3,3)
        if board.n_move == 2 and self.side==1:
            if board.board[3][1]==2 or board.board[3][3]==2:
                return (3,2)
            elif board.board[1][1] == 2 or board.board[1][3] ==2:
                return (1,2)

        if board.game_end():
            return
        place = self.AB_search(board)
        return place

def writefile(placement):
    file=open('output.txt','w')
    if placement[0]=='P':
        file.write("PASS")
        file.close()
        return
    file.write("{},{}".format(placement[0],placement[1]))
    file.close()

#
# infile=open('input.txt','r')
# board = GoBoard(5)
# writefile(ABGo.move(board))

## For test
# board = GoBoard(5)
# board.board = [[0 for x in range(5)] for y in range(5)]
# board.place_chess(2,2,1)
# board.place_chess(2,1,2)
# board.place_chess(3,3,1)
# board.place_chess(3,0,2)
# board.place_chess(4,2,1)
# board.place_chess(4,1,2)
# board.place_chess(3,1,1)
# board.place_chess(3,2,2)
# board.place_chess(3,1,1)
# board.place_chess(3,2,2)

# player = ABGo()
# player.set_side(1)
# board.visualize_board(board.board)
# print(board.game_end())
# move = player.move(board)
# print(move)
# writefile(move)
# movefile = open('move.txt','w')
# movefile.write(str(board.n_move))

## For Home work

time1=time()
board = GoBoard(5)
infile = open('input.txt','r')
movefile = open('move.txt','r')
board.load_from_file(infile,movefile)
print(board.n_move)
player = ABGo()
player.set_side(board.side)
writefile(player.move(board))
movefile = open('move.txt','w')
movefile.write(str(board.n_move+1))
time2=time()
print(time2-time1)


import torch
import torch.nn as nn
from config import args

class BiLSTM(nn.Module):
    def __init__(self, input_size, hidden_size, num_layers, num_dirs):
        super().__init__()
        self.input_size = input_size
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        self.num_dirs = num_dirs
        self.rnn = nn.LSTM(
            input_size=input_size,
            hidden_size=hidden_size // num_dirs,
            num_layers=num_layers,
            bias=True,
            batch_first=True,
            bidirectional=(num_dirs == 2)
        )

    def init_states(self, x, device):
        h0 = torch.zeros(self.num_layers*self.num_dirs, x.size(0),
                         self.hidden_size//self.num_dirs).to(device)
        c0 = torch.zeros(self.num_layers*self.num_dirs, x.size(0),
                         self.hidden_size//self.num_dirs).to(device)
        return (h0, c0)

    def forward(self, x, mask, pack=True):
        s = self.init_states(x, x.device)

        if pack:
            lens = mask.sum(1)
            sorted_lens, sorted_idx = torch.sort(lens, descending=True)
            _, unsorted_idx = torch.sort(sorted_idx)

            x = x.index_select(0, sorted_idx)
            packed_x = nn.utils.rnn.pack_padded_sequence(
                x, sorted_lens.cpu().numpy(), batch_first=True)

            self.rnn.flatten_parameters()
            out, _ = self.rnn(packed_x, s)
            padded_out, _ = nn.utils.rnn.pad_packed_sequence(
                out, batch_first=True)
            out = padded_out.index_select(0, unsorted_idx)
        else:
            self.rnn.flatten_parameters()
            out, _ = self.rnn(x, s)
        return out


import urllib.request, urllib.parse, urllib.error
import xml.etree.ElementTree as ET
import ssl

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

url = input('Enter url: ')
print('Retrieving', url)
uh = urllib.request.urlopen(url, context=ctx)

data = uh.read()
print('Retrieved', len(data), 'characters')
tree = ET.fromstring(data)

comments = tree.findall('comments/comment/count')
total = 0
for count in comments:
    total = total+int(count.text)
print(total)

Coursera/Python for Everybody/Using Python to Access Web Data/w5-assignment.py


#!/usr/bin/python
# -* - coding: UTF-8 -* -
from selenium import webdriver
import time

def jinjianluru(self):
    driver=self.driver
    time.sleep(2)
    driver.switch_to_frame("mainFrame")
    #print "switch mainframe"
    #定位交易渠道银行
    #jinjianqudao=driver.find_element_by_css_selector("div.controls select#borrowSource.input-medium").find_element_by_css_selector("select#borrowSource.input-medium option[value='%s']")
    #公司名称
    #gongsiname=driver.find_element_by_css_selector("div.controls input#compName.input-medium")
    #融资人信息 从公司名称 到项目联系人手机号 7组元素
    rongzirenxinxi=driver.find_elements_by_css_selector("div#companyDiv div.control-group div.controls input.input-medium")
    #借款金额
    jiekuanjine=driver.find_element_by_css_selector("div.controls input#borrowAmount.input-medium")
    #借款总成本
    #jiekuanzongchengben=driver.find_element_by_css_selector("div.controls input#borrowDegression.input-medium")
    #筹款利率
    #choukuanlilv=driver.find_element_by_css_selector("div.controls input#borrowRate.input-medium")
    #借款期限
    jiekuanqixian=driver.find_element_by_css_selector("div.controls input#borrowLimit.input-medium")
    #筹款期限
    choukuanqixian=driver.find_element_by_css_selector("div.controls input#bidDeadline.input-medium")

    #臭婊期限
    choubiaoqixian=driver.find_element_by_css_selector("div.controls input#bidDeadline.input-medium")
    lists=[rongzirenxinxi,jiekuanjine,jiekuanqixian,choukuanqixian,choubiaoqixian]
    return lists


class Node:
    def __init__(self, value, next=None):
        self.value = value
        self.next = next

class LinkedList:
    def __init__(self, node=None):
        self.length = 1 if node else 0
        self.head = node
        self.tail = node

    def add_to_tail(self, value):
        new_tail = Node(value)
        if self.length == 0:
            self.head = new_tail
        else:
            self.tail.next = new_tail
        self.tail = new_tail
        self.length += 1

    def contains(self, value):
        cur_node = self.head
        while cur_node:
            if cur_node.value == value:
                return True
            cur_node = cur_node.next
        return False

    def remove_head(self):
        if self.length == 0:
            return None
        value = self.head.value
        if self.length == 1:
            self.tail = None
        self.head = self.head.next
        self.length -= 1
        return value

    def get_max(self):
        if self.length == 0:
            return None
        max_val = float('-inf')
        cur_node = self.head
        while cur_node:
            if cur_node.value > max_val:
                max_val = cur_node.value
            cur_node = cur_node.next
        return max_val


import numpy as np
import sympy as sp
import matplotlib.pyplot as plt
from scipy import integrate

KSI = 1/14
A = 1/14
B = 1/15
X_0 = 0
Y_0 = np.array([B * np.pi, A * np.pi])
X_k = np.pi
EPS = 10 ** -4

def f(x, y, symbol=False):
    if symbol:
        return [A * sp.Symbol('y1'), -B * sp.Symbol('y0')]
    return np.array([A * y[1], -B * y[0]])

def two_stage_schema(k, x_0=X_0, y_0=Y_0, x_k=X_k, c_2=KSI):
    x, y = [x_0], [y_0]
    b_2 = 1 / (2 * c_2)
    b_1 = 1 - b_2
    h = (x_k - x_0) / k
    for i in range(k):
        k_1 = h * f(x[i], y[i])
        k_2 = h * f(x[i] + c_2 * h, y[i] + c_2 * k_1)
        x.append(x[i] + h)
        y.append(y[i] + b_1 * k_1 + b_2 * k_2)
    return y

def opponent(k):
    return two_stage_schema(k, c_2=1)

def runge_error(y_dash, y_wave, s=2):
    error = np.linalg.norm((y_wave - y_dash) / (1 - 2 ** -s))
    return error

def solving(method=two_stage_schema, epsilon=EPS):
    k = 2
    y_dash = method(k)[-1]
    k *= 2
    y_wave = method(k)[-1]
    error = runge_error(y_dash, y_wave)
    while error > epsilon:
        k *= 2
        y_dash = y_wave
        y_wave = method(k)[-1]
        error = runge_error(y_dash, y_wave)
    return k, y_dash, error

def step_opt(s=2, epsilon=EPS, method=two_stage_schema):
    k = 2
    y_dash = method(k)[-1]
    y_wave = method(2 * k)[-1]
    h = (X_k - X_0) / k
    h_opt = (h / 2) * ((2 ** s - 1) * epsilon / np.linalg.norm(y_dash - y_wave)) ** (1 / s)
    print(h_opt)
    k_opt = int(np.ceil((X_k - X_0) / h_opt)) - 1
    h_opt = (X_k - X_0) / k_opt
    return k_opt, h_opt

def integrate_auto_step(method=two_stage_schema, delta=10**-5):
    x_list = [X_0]
    y_list = [Y_0]
    k = 2
    len_segmenth = X_k - X_0
    steps_list = [k]
    while X_k > x_list[-1]:
        pass
    return 0

def main():
    true_result = integrate.solve_ivp(
        f, (X_0, X_k), Y_0, dense_output=True, atol=1e-13, rtol=1e-13).sol.__call__(X_k)
    my_result, error = solving()[1:3]
    k_opt, h_opt = step_opt()
    y_nodes = two_stage_schema(k_opt)
    y_nodes_er = two_stage_schema(k_opt*2)[::2]
    my_result_opt = y_nodes[-1]
    x_nodes = [X_0 + h_opt * i for i in range(k_opt + 1)]
    errors = [runge_error(y_nodes[i], y_nodes_er[i]) for i in range(len(x_nodes))]
    plt.plot(x_nodes, errors)
    plt.title('runge error by x')
    plt.grid(True)
    plt.show()
    print('True result:', true_result, '\nMy result:', my_result, '\nAbsolute error:', errors[-1],
          '\nEstimated error:', error, '\nOptimal step:', h_opt, '\nMy result with optimal step:', my_result_opt)
    return 0

main()


import numpy
import torch
from torch import nn
from ...jastrows.distance.electron_electron_distance import ElectronElectronDistance
from .orbital_dependent_backflow_kernel import OrbitalDependentBackFlowKernel

class OrbitalDependentBackFlowTransformation(nn.Module):

    def __init__(self, mol, backflow_kernel, backflow_kernel_kwargs={}, cuda=False):
        """Transform the electorn coordinates into backflow coordinates.
        see : Orbital-dependent backflow wave functions for real-space quantum Monte Carlo
        https://arxiv.org/abs/1910.07167

        .. math:
            \\bold{q}_i = \\bold{r}_i + \\sum_{j\neq i} \\eta(r_{ij})(\\bold{r}_i - \\bold{r}_j)
        """
        super().__init__()

        self.edist = ElectronElectronDistance(mol.nelec)
        self.nelec = mol.nelec
        self.nao = mol.basis.nao
        self.backflow_kernel = OrbitalDependentBackFlowKernel(
            backflow_kernel, backflow_kernel_kwargs, mol, cuda)
        self.ndim = 3

        self.cuda = cuda
        self.device = torch.device('cpu')
        if self.cuda:
            self.device = torch.device('cuda')

    def forward(self, pos, derivative=0):

        if derivative == 0:
            return self._backflow(pos)

        elif derivative == 1:
            return self._backflow_derivative(pos)

        elif derivative == 2:
            return self._backflow_second_derivative(pos)

        else:
            raise ValueError(
                'Derivative of the backflow transformation must be 0, 1 or 2')

    def _backflow(self, pos):
        """Computes the backflow transformation

        .. math:
            \\bold{q}_i = \\bold{r}_i + \\sum_{j\neq i} \\eta(r_{ij})(\\bold{r}_i - \\bold{r}_j)

        Args:
            pos(torch.tensor): original positions Nbatch x[Nelec*Ndim]

        Returns:
            torch.tensor: transformed positions Nbatch x[Nelec*Ndim]
        """

        nbatch = pos.shape[0]

        # compute the difference
        # Nbatch x 1 x Nelec x Nelec x 3
        delta_ee = self.edist.get_difference(
            pos.reshape(nbatch, self.nelec, self.ndim)).unsqueeze(1)

        # compute the backflow function
        # Nbatch x Nao x Nelec x Nelec x 1
        bf_kernel = self.backflow_kernel(
            self.edist(pos)).unsqueeze(-1)
        nao = bf_kernel.shape[self.backflow_kernel.stack_axis]

        # update pos
        pos = pos.reshape(nbatch, 1, self.nelec, self.ndim) + \
            (bf_kernel * delta_ee).sum(3)

        # retrurn Nbatch x Nao x Nelec*Ndim
        return pos.reshape(nbatch, nao, self.nelec*self.ndim)

    def _backflow_derivative(self, pos):
        r"""Computes the derivative of the backflow transformation
           wrt the original positions of the electrons

        .. math::
            \\bold{q}_i = \\bold{r}_i + \\sum_{j\\neq i} \\eta(r_{ij})(\\bold{r}_i - \\bold{r}_j)

        .. math::
            \\frac{d q_i}{d x_k} = \\delta_{ik}(1 + \\sum_{j\\neq i} \\frac{d \\eta(r_ij)}{d x_i}(x_i-x_j) + \\eta(r_ij)) +
                                   \\delta_{i\\neq k}(-\\frac{d \\eta(r_ik)}{d x_k}(x_i-x_k) - \\eta(r_ik))

        Args:
            pos(torch.tensor): orginal positions of the electrons Nbatch x[Nelec*Ndim]

        Returns:
            torch.tensor: d q_{i}/d x_k with:
                          q_{i} bf position of elec i
                          x_k original coordinate of the kth elec
                          Nelec x  Nbatch x Nelec x Norb x Ndim
        """

        # ee dist matrix : Nbatch x  Nelec x Nelec
        ree = self.edist(pos)
        nbatch, nelec, _ = ree.shape

        # derivative ee dist matrix : Nbatch x 1 x 3 x Nelec x Nelec
        # dr_ij / dx_i = - dr_ij / dx_j
        dree = self.edist(pos, derivative=1).unsqueeze(1)

        # difference between elec pos
        # Nbatch, 1, 3, Nelec, Nelec
        delta_ee = self.edist.get_difference(
            pos.reshape(nbatch, nelec, 3)).permute(0, 3, 1, 2).unsqueeze(1)

        # backflow kernel : Nbatch x Nao x Nelec x Nelec
        bf = self.backflow_kernel(ree)
        nao = bf.shape[self.backflow_kernel.stack_axis]

        # (d eta(r_ij) / d r_ij) (d r_ij/d beta_i)
        # derivative of the back flow kernel : Nbatch x Nao x 3 x Nelec x Nelec
        dbf = self.backflow_kernel(ree, derivative=1).unsqueeze(2)
        dbf = dbf * dree

        # (d eta(r_ij) / d beta_i) (alpha_i - alpha_j)
        # Nbatch x Nao x 3 x 3 x Nelec x Nelec
        dbf_delta_ee = dbf.unsqueeze(2) * delta_ee.unsqueeze(3)

        # compute the delta_ij * (1 + sum k \neq i eta(rik))
        # Nbatch x Nao x Nelec x Nelec (diagonal matrix)
        delta_ij_bf = torch.diag_embed(
            1 + bf.sum(-1), dim1=-1, dim2=-2)

        # eye 3x3 in 1x3x3x1x1
        eye_mat = torch.eye(3, 3).view(
            1, 1, 3, 3, 1, 1).to(self.device)

        # compute the delta_ab * delta_ij * (1 + sum k \neq i eta(rik))
        # Nbatch x Ndim x Ndim x Nelec x Nelec (diagonal matrix)
        delta_ab_delta_ij_bf = eye_mat * \
            delta_ij_bf.view(nbatch, nao, 1, 1, nelec, nelec)

        # compute sum_k df(r_ik)/dbeta_i (alpha_i - alpha_k)
        # Nbatch x Nao x Ndim x Ndim x Nelec x Nelec
        delta_ij_sum = torch.diag_embed(
            dbf_delta_ee.sum(-1), dim1=-1, dim2=-2)

        # compute delta_ab * f(rij)
        delta_ab_bf = eye_mat * \
            bf.view(nbatch, nao, 1, 1, nelec, nelec)

        # return Nbatch x Ndim(alpha) x Ndim(beta) x Nelec(i) x Nelec(j)
        # nbatch d alpha_i / d beta_j
        return delta_ab_delta_ij_bf + delta_ij_sum - dbf_delta_ee - delta_ab_bf

    def _backflow_second_derivative(self, pos):
        r"""Computes the second derivative of the backflow transformation
           wrt the original positions of the electrons

        .. math::
            \\bold{q}_i = \\bold{r}_i + \\sum_{j\\neq i} \\eta(r_{ij})(\\bold{r}_i - \\bold{r}_j)

        .. math::
            \\frac{d q_i}{d x_k} = \\delta_{ik}(1 + \\sum_{j\\neqi} \\frac{d \\eta(r_ij)}{d x_i} + \\eta(r_ij)) +
                                   \\delta_{i\\neq k}(-\\frac{d \\eta(r_ik)}{d x_k} - \\eta(r_ik))

        .. math::
            \\frac{d ^ 2 q_i}{d x_k ^ 2} = \\delta_{ik}(\\sum_{j\\neqi} \\frac{d ^ 2 \\eta(r_ij)}{d x_i ^ 2} + 2 \\frac{d \\eta(r_ij)}{d x_i}) +
                                       - \\delta_{i\\neq k}(\\frac{d ^ 2 \\eta(r_ik)}{d x_k ^ 2} + \\frac{d \\eta(r_ik)}{d x_k})

        Args:
            pos(torch.tensor): orginal positions of the electrons Nbatch x[Nelec*Ndim]

        Returns:
            torch.tensor: d q_{i}/d x_k with:
                          q_{i} bf position of elec i
                          x_k original coordinate of the kth elec
                          Nelec x  Nbatch x Nelec x Norb x Ndim
        """

        # ee dist matrix :
        # Nbatch x  Nelec x Nelec
        ree = self.edist(pos)
        nbatch, nelec, _ = ree.shape

        # difference between elec pos
        # Nbatch, 1, 3, Nelec, Nelec
        delta_ee = self.edist.get_difference(
            pos.reshape(nbatch, nelec, 3)).permute(0, 3, 1, 2).unsqueeze(1)

        # derivative ee dist matrix  d r_{ij} / d x_i
        # Nbatch x 1 x 3 x Nelec x Nelec
        dree = self.edist(pos, derivative=1).unsqueeze(1)

        # derivative ee dist matrix :  d2 r_{ij} / d2 x_i
        # Nbatch x 1 x 3 x Nelec x Nelec
        d2ree = self.edist(pos, derivative=2).unsqueeze(1)

        # derivative of the back flow kernel : d eta(r_ij)/d r_ij
        # Nbatch x Nao x 1 x Nelec x Nelec
        dbf = self.backflow_kernel(ree, derivative=1).unsqueeze(2)
        nao = dbf.shape[self.backflow_kernel.stack_axis]

        # second derivative of the back flow kernel : d2 eta(r_ij)/d2 r_ij
        # Nbatch x Nao x 1 x Nelec x Nelec
        d2bf = self.backflow_kernel(ree, derivative=2).unsqueeze(2)

        # (d^2 eta(r_ij) / d r_ij^2) (d r_ij/d x_i)^2
        # + (d eta(r_ij) / d r_ij) (d^2 r_ij/d x_i^2)
        # Nbatch x Nao x 3 x Nelec x Nelec
        d2bf = (d2bf * dree * dree) + (dbf * d2ree)

        # (d eta(r_ij) / d r_ij) (d r_ij/d x_i)
        # Nbatch x Nao x 3 x Nelec x Nelec
        dbf = dbf * dree

        # eye matrix in dim x dim
        eye_mat = torch.eye(3, 3).reshape(
            1, 1, 3, 3, 1, 1).to(self.device)

        # compute delta_ij delta_ab 2 sum_k dbf(ik) / dbeta_i
        term1 = 2 * eye_mat * \
            torch.diag_embed(
                dbf.sum(-1), dim1=-1, dim2=-2).reshape(nbatch, nao, 1, 3, nelec, nelec)

        # (d2 eta(r_ij) / d2 beta_i) (alpha_i - alpha_j)
        # Nbatch x Nao x 3 x 3 x Nelec x Nelec
        d2bf_delta_ee = d2bf.unsqueeze(2) * delta_ee.unsqueeze(3)

        # compute sum_k d2f(r_ik)/d2beta_i (alpha_i - alpha_k)
        # Nbatch x Nao x Ndim x Ndim x Nelec x Nelec
        term2 = torch.diag_embed(
            d2bf_delta_ee.sum(-1), dim1=-1, dim2=-2)

        # compute delta_ab * df(rij)/dbeta_j
        term3 = 2 * eye_mat * \
            dbf.reshape(nbatch, nao, 1, 3, nelec, nelec)

        # return Nbatch x Ndim(alpha) x Ndim(beta) x Nelec(i) x Nelec(j)
        # nbatch d2 alpha_i / d2 beta_j
        return term1 + term2 + d2bf_delta_ee + term3


#!/usr/bin/env python
"""
pruned version of medley/player.py
"""

import sys, os
import platform

import PySide
from PySide import QtGui, QtCore

__version__ = '0.0.1'

#main window global:
main_window = None
main_player = None

#class MainWidget(QtGui.QWidget):
class MainWidget(QtGui.QSplitter):
    def __init__(self, parent):
        super(MainWidget, self).__init__(parent)

        self.setChildrenCollapsible(False)
        self.setHandleWidth(1)

        #self.layout = QtGui.QHBoxLayout()
        #self.layout = QtGui.QGridLayout()
        #self.layout = QtGui.QSplitter()

        self.left_nav = LeftNavWidget()
        self.addWidget(self.left_nav)

        #self.table = QtGui.QTableView()
        #self.table = PlaylistView()
        self.table = PlaylistWidget()
        self.addWidget(self.table)

        #http://srinikom.github.io/pyside-docs/PySide/QtGui/QSplitter.html#PySide.QtGui.PySide.QtGui.QSplitter.setSizes
        self.setStretchFactor(0, 25)
        self.setStretchFactor(1, 75)

        #pass self.table reference into self.left_nav.tree
        #for update after selection
        #self.left_nav.table = self.table
        self.left_nav.set_table_view(self.table.playlist_view)
        #could also pass that in

        #self.layout.setColumnStretch(1, 1)

        #self.setLayout(self.layout)
        #self.setWidget(self.layout)

class AppWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(AppWindow, self).__init__(parent)

        self.widget = MainWidget(self)

        self.setCentralWidget(self.widget)

        fileMenu = self.menuBar().addMenu("&File")

        openAction = QtGui.QAction('Open State', self)
        openAction.setShortcut('Ctrl+O')
        openAction.setStatusTip('Open playlist structure')
        openAction.triggered.connect(self.widget.left_nav.tree_view.open_lists)
        fileMenu.addAction(openAction)

        saveAction = QtGui.QAction('Save State', self)
        saveAction.setShortcut('Ctrl+S')
        saveAction.setStatusTip('Save playlist structure')
        saveAction.triggered.connect(self.widget.left_nav.tree_view.save_lists)
        fileMenu.addAction(saveAction)

        #metaMenu.addSeparator()

        aboutMenu = self.menuBar().addMenu("&About")

        infoAction = QtGui.QAction('Info', self)
        infoAction.setStatusTip('Information about Medley')
        infoAction.triggered.connect(self.about)
        aboutMenu.addAction(infoAction)

    def about(self):
        """
        Popup a box with about message.

        via:
        http://qt-project.org/wiki/PySideSimplicissimus_Module_3_AboutBox
        """
        QtGui.QMessageBox.about(self, "About PySide, Platform, etc",
                                """Medley v %s

Python %s

PySide version %s

Qt version %s on %s
                                """ % (__version__,
                                       platform.python_version(),
                                       PySide.__version__,
                                       PySide.QtCore.__version__,
                                       platform.system()))

if __name__ == '__main__':
    global main_window
    app = QtGui.QApplication(sys.argv)
    #this is needed for Phonon on Linux (DBUS):
    app.setApplicationName("Medley")
    main_window = AppWindow()
    #main_window.resize(900, 600)
    main_window.resize(640, 350)
    main_window.show()
    app.exec_()


#!/usr/bin/env python3

import numpy as np
from py4j.java_gateway import JavaGateway

# connect to the JVM
gateway = JavaGateway()

# get the HeadlessModel instance
headlessModel = gateway.entry_point

# call the static method exec(), which runs the simulation
result = gateway.jvm.housing.Model.exec(1.5)

# result is a py4j.java_collections.JavaArray, whose elements are floats
print(type(result))

for x in result:
    print(x)


# Given a grid where each entry is only 0 or 1, find the number of corner rectangles.

# A corner rectangle is 4 distinct 1s on the grid that form an axis-aligned rectangle.
# Note that only the corners need to have the value 1. Also, all four 1s used must be distinct.

# 起始点不同的矩形个数

# class Solution:
#     """
#     @param grid: the grid
#     @return: the number of corner rectangles
#     """
#     def countCornerRectangles(self, grid):
#         # Write your code here
#         # Naive, time O(m^2 * n^2), space O(1)， TLE
#         m, n = len(grid), len(grid[0])
#         res = 0
#         for row_i in range(m):
#             for row_j in range(row_i + 1, m):
#                 for col_k in range(n):
#                     if grid[row_i][col_k] == 0 or grid[row_j][col_k] == 0:
#                         continue
#                     for col_l in range(col_k + 1, n):
#                         if grid[row_i][col_l] == 1 and grid[row_j][col_l] == 1:
#                             res += 1

#         return res

# 两行同时遍历，如果两行中相同列位置的值都为1，则计数器count加1.
# 但是两列都是1才是个长方形，所以共有count*(count-1)/2个.

class Solution:
    """
    @param grid: the grid
    @return: the number of corner rectangles
    """
    def countCornerRectangles(self, grid):
        # Write your code here
        # linear scan, time O(m^2 * n), space O(1), 慢， 反复遍历同一行
        m, n = len(grid), len(grid[0])
        res = 0
        for row_i in range(m):
            for row_j in range(row_i + 1, m):
                count = 0 # count the number of 1s pairs in the same column position of row_i and row_j
                for col in range(n):
                    if grid[row_i][col] == 1 and grid[row_j][col] == 1:
                        count +=1
                res += count * (count - 1) // 2 # can consist so many rectangles with <count> pairs

        return res

class Solution:
    """
    @param grid: the grid
    @return: the number of corner rectangles
    """
    def countCornerRectangles(self, grid):
        # Write your code here
        # Naive, time O(m * n^2), space O(n^2), 快， 一行一行添加，dp记录了之前有多少行，其j, k列为1，然后以当前行为底，构造矩阵
        m, n = len(grid), len(grid[0])
        res = 0
        dp = [[0] * n for __ in range(n)] # dp[i][j][k]: the number of rows where for each row, its row index is no greater than i and its values at both indices j and k are 1.
        for row_i in range(m): # traverse each row, count the number of corner rectangles as i-th row are added to the matrix.
            for col_j in range(n):
                if grid[row_i][col_j] == 0:
                    continue
                for col_k in range(col_j + 1, n):
                    if grid[row_i][col_k] == 0:
                        continue
                    res += dp[col_j][col_k]  # the number of new rectangules using i-th row as bottom
                    dp[col_j][col_k] += 1  # grid[i][j] and grid[i][k] both are 1, add one (i-th row)

        return res


# coding: utf-8

# In[13]:

import itertools
import random

# In[37]:

def divisors(n):
    original = n
    div = 2
    while div**2 <= n:
        while n % div == 0:
            yield div
            n //= div
        div += 1
    if n > 1 and n < original:
        yield n

# In[38]:

def some_divisor(n):
    for divisor in divisors(n):
        return divisor
    return False

# In[39]:

def check_coin(coin):
    divs = []
    for base in range(2, 11):
        n = int(coin, base)
        div = some_divisor(n)
        if not div:
            return False
        divs.append(div)
    return divs

# In[69]:

def solve(j, n):
    high = 2 ** (j-2)
    seen = set()
    for i in range(n):
        divisors = False
        while not divisors:
            coin = bin(high + random.randrange(high))[2:] + "1"
            if coin in seen:
                continue
            seen.add(coin)
            divisors = check_coin(coin)
            if not divisors:
                continue
            print("{} {}".format(coin, " ".join(map(str, divisors))))

# In[71]:

cases = int(input())
for case in range(1, cases+1):
    j, n = map(int, input().split(" "))
    print("Case #{}:".format(case))
    solve(j, n)


import turtle

def drawSquare(myTurt, sideLen):
    for i in range(4):
        myTurt.forward(sideLen)
        myTurt.left(90)

def fence(myTurt, sideLen):
    for i in range(4):
        newXcor = myTurt.xcor()
        newYcor = myTurt.ycor()
        newPos = drawSquare(myTurt, sideLen)
        myTurt.penup()
        myTurt.goto(newXcor-10, newYcor-10)
        myTurt.pendown()
        sideLen+=20

wn = turtle.Screen()
wn.bgcolor("lightgreen")

fabio = turtle.Turtle()
fabio.pensize(3)
fabio.color("hotpink")
sideLen=20
fence(fabio, sideLen)


from trains import Task
import csv

import numpy as np
from datetime import datetime
from sklearn import preprocessing

task = Task.init(project_name="diabetes prediction", task_name="ingesting dataset", task_type=Task.TaskTypes.data_processing)

args = {
    'dataset_task_id': 'c9943dc9aba047cea250364277061dbc',
    'test_size': 20
}

task.connect(args)

dataset_upload_task = Task.get_task(task_id=args['dataset_task_id'])
print('Input task id={} artifacts {}'.format(args['dataset_task_id'], list(dataset_upload_task.artifacts.keys())))
# download the csv file
source_ds = dataset_upload_task.artifacts['dataset'].get_local_copy()

x_vals = []
y_vals = []

# Converting CSV file to lists
with open(source_ds) as input_csv:
    csv_reader = csv.reader(input_csv, delimiter=',')
    next(csv_reader)
    for row in csv_reader:
        x_vals.append(float(row[2]))
        y_vals.append(int(row[-1]))

scaler = preprocessing.StandardScaler()
x_vals = np.array(x_vals).reshape(-1, 1)
x_scaled = scaler.fit_transform(x_vals)
y_vals = np.array(y_vals)

x_train = x_scaled[:-args['test_size']]
x_test = x_scaled[-args['test_size']:]

y_train = y_vals[:-args['test_size']]
y_test = y_vals[-args['test_size']:]

date= {'date': str(datetime.date(datetime.now()))}

task.upload_artifact('x_train', artifact_object=x_train, metadata=date)
task.upload_artifact('x_test', artifact_object=x_test, metadata=date)
task.upload_artifact('y_train', artifact_object=y_train, metadata=date)
task.upload_artifact('y_test', artifact_object=y_test, metadata=date)

print('all done!')


import os
from Utilities import folder_processor
from Utilities.Database import database_loader
from Utilities import configure_functions

# Path to crawled data
CRAWLED_DATA_PATH = "CrawledData/"
# Path to processed data
PROCESSED_DATA_PATH = "ProcessedData/"
# Path to crawler logs
CRAWLER_LOGS_PATH = "CrawlerLogs/"
# Path to dataset configuration files
CONFIG_FILES_PATH = "DatasetConfigs"

def hard_reset_dataset(dataset_name: str) -> None:
    """
    Resets all saved data in dataset except config and implementation
    Args:
        dataset_name: name of dataset that has existing configuration file
    """

    path = CRAWLED_DATA_PATH + dataset_name + "/"
    folder_processor.clean_folder(path)

    path = PROCESSED_DATA_PATH + dataset_name + "/"
    folder_processor.clean_folder(path)

    database_loader.remove_dataset_database(dataset_name)

def soft_reset_dataset(dataset_name: str) -> None:
    """
    Resets all saved data in dataset except config and implementation
    Args:
        dataset_name: name of dataset that has existing configuration file
    """
    path = PROCESSED_DATA_PATH + dataset_name + "/"
    folder_processor.clean_folder(path)

    database_loader.remove_dataset_database(dataset_name)

def soft_reset_all_datasets() -> None:
    """
    Resets all saved data in all datasets with config file except configs and implementation
    """
    datasets = os.listdir(CONFIG_FILES_PATH)

    for dataset in datasets:
        soft_reset_dataset(dataset.split('.')[0])

def hard_reset_all_datasets() -> None:
    """
    Resets all saved data in all datasets with config file except configs and implementation
    """
    datasets = os.listdir(CONFIG_FILES_PATH)

    for dataset in datasets:
        hard_reset_dataset(dataset.split('.')[0])

def main() -> None:
    print(
        "Zadejte jméno Datasetu který chcete resetovat (pokud všechny zadejte '-ALL'):\n"
    )

    dataset_name = input().upper()

    print("Chcete smazat i stažená data ? (ANO/NE) \n")

    input_decision = input().upper()

    if dataset_name == '-ALL':
        if input_decision == 'ANO':
            hard_reset_all_datasets()
        elif input_decision == 'NE':
            soft_reset_all_datasets()
        else:
            print('Neplatný vstup (ANO/NE)')
    else:
        test = configure_functions.check_if_there_is_a_config_file(
            dataset_name)
        if test == True:
            if input_decision == 'ANO':
                hard_reset_dataset(dataset_name)
            elif input_decision == 'NE':
                soft_reset_dataset(dataset_name)
            else:
                print('Neplatný vstup (ANO/NE)')
        else:
            print("Tento dataset v architektuře neexistuje")

if __name__ == "__main__":
    main()


from keras.models import load_model
import we_helpers as wh
import numpy as np

m_model = load_model('./models/model1490017738-54-71')
test_case = [
    "I don't like it, i hate it",
    "Amazing! It's a masterpiece",
    "What a boring film, i'll never watch it again",
    "illuminating if overly talky documentary",
    "interesting , but not compelling",
    "offers a breath of the fresh air of true sophistication"
]

x_test = []
for tc in test_case:
    x_test.append(wh.sentence2matrix(tc))

x_test = np.array(x_test)
x_test = x_test.reshape((x_test.shape[0], x_test.shape[1], x_test.shape[2], 1))

result = m_model.predict(x_test)
print(result)


#!/usr/bin/env python
# coding: utf-8

# In[1]:

fruits=["apple","banana","pinapple","watermelon","orange"]
print(fruits)
while True:
    x=input("請輸入想刪除的水果或按enter結束:")
    if x in fruits:
        fruits.remove(x)
        print(fruits)
    elif x not in fruits and x !="":
        print("本超市沒有賣這種水果。")
    elif x =="":
        for i in fruits:
            print(i)
        break

# In[ ]:


def myLen(data):
    count=0
    for i in range(0,len(data)):
        count += 1
    print("The Length of string is:",count)

def myMax(data):
    max=data[0]
    for i in range(0,len(data)):
        if max < i :
            max=i
    print("The max is:",data[max])

def myMin(data):
    min=data[0]
    for i in range(0,len(data)):
        if min > i :
            min=i
    print("The min is:",data[min])
data=[1,2,3,4,5]
print(myLen(data))
print(myMax(data))
print(myMin(data))

#HW Code your own functions

#def myMin(data)

#List Comprehension
print([x**2 for x in data])

productPrices=[1123,1342,3341,4421,5456]
#List Comprehension and expressions give error
#Explore the alternate

numbers = list(range(1,101))
print(numbers)

names=("John","James","Jennie","Jim","John")
names1=list(names)
names2=set(names)

print(names,type(names))
print(names1,type(names1))
print(names2,type(names2))


from talon.voice import Context

from talon import applescript

def kmaestro(id):

    def _kmaestro(*_):
        print("Kmaestro {}".format(id))

        applescript.run(
            """
        tell application "Keyboard Maestro Engine"
            do script "{id}"
        end tell
        """.format(
                id=id
            )
        )

    return _kmaestro

ctx = Context("window")
ctx.keymap(
    {
        "switcher": kmaestro("Switcher"),
        "window left": kmaestro("Windy Left"),
        "window right": kmaestro("Windy Right"),
        "window max": kmaestro("Windy Max"),
    }
)


from django.shortcuts import render, redirect, get_object_or_404, reverse
from django.template import loader
from django.http import HttpResponse, JsonResponse
from django.contrib.auth.decorators import login_required
from ..contract.models import Contract
from ..worker.models import Worker
from ..employer.models import Employer, Address, Position
from ..account.models import Company
from ..work.models import Work
from ..document.models import Document
from django.contrib import messages
from django.utils.translation import gettext_lazy as _
from datetime import date
from dateutil.relativedelta import relativedelta

@login_required()
def work_workers(request):
    month_end = date.today() + relativedelta(months=1)
    addresess = Address.objects.all().order_by('employer__name')
    works = Work.objects.filter(dismiss_date__lte=date.today()).order_by('worker__lname')
    documents = Document.objects.filter(document_name_id = 5)
    upc = Company.objects.get(id=1)
    pi = Company.objects.get(id=2)
    count_all_workers = 0
    worker_address = []
    for address in addresess:
        address_dict = {}
        address_dict['address'] = "{} {}".format(address.employer.long_name, address.address)
        count_worker_address = 0
        address_dict['works'] = []
        temp_worker = []
        for work in works:
            row = {}
            if work.position.address == address:
                if not work.worker in temp_worker:
                    row['worker'] = work.worker
                    temp_worker.append(work.worker)
                    count_worker_address+=1
                    count_all_workers+=1
                    for document in documents:
                        # print(document)
                        if document.worker == work.worker and document.company == upc:
                            row['upc_start'] = document.start
                            row['upc_end'] = document.end
                            if document.end < month_end:
                                row['color_upc'] = 'red'
                        elif document.worker == work.worker and document.company == pi:
                            row['pi_start'] = document.start
                            row['pi_end'] = document.end
                            if document.end < month_end:
                                row['color_pi'] = 'red'
                    address_dict['works'].append(row)
                else:
                    continue
        if count_worker_address != 0:
            address_dict['count_workers'] = count_worker_address
            worker_address.append(address_dict)
        else:
            continue
    context = {
        'breadcrumb': [
            {'url': reverse('main:index'), 'title': _("Home")},
            {'url': reverse('tables:work_workers'), 'title': _('Work workers')},
            ],
        'works':works,
        'addresess':addresess,
        'worker_address':worker_address,
        'count_all_workers':count_all_workers,
        'upc':upc,
        'pi':pi,
        }
    template = loader.get_template('tables/work_workers.html')
    return HttpResponse(template.render(context, request))

@login_required()
def contract_workers(request):
    addresess = Address.objects.all()
    contracts = Contract.objects.filter(end__gte=date.today())
    count_all_workers = 0
    worker_address = []
    for address in addresess:
        address_dict = {}
        address_dict['address'] = "{} {}".format(address.employer, address.address)
        count_worker_address = 0
        address_dict['contracts'] = []
        for contract in contracts:
            if contract.position.address == address:
                count_worker_address+=1
                count_all_workers+=1
                address_dict['contracts'].append(contract)
        if count_worker_address != 0:
            address_dict['count_workers'] = count_worker_address
            worker_address.append(address_dict)
        else:
            continue
    context = {
        'breadcrumb': [
            {'url': reverse('main:index'), 'title': _("Home")},
            {'url': reverse('tables:contract_workers'), 'title': _('Contract workers')},
            ],
        'contracts':contracts,
        'addresess':addresess,
        'worker_address':worker_address,
        'count_all_workers':count_all_workers,
        }
    template = loader.get_template('tables/contract_workers.html')
    return HttpResponse(template.render(context, request))

@login_required()
def dismissed_workers_all(request):
    works = Work.objects.filter(is_dismiss=True)
    context = {
        'breadcrumb': [
            {'url': reverse('main:index'), 'title': _("Home")},
            {'url': reverse('tables:dismissed_workers_all'), 'title': _('All dismissed workers')},
            ],
        'works':works,
        }
    template = loader.get_template('tables/dismissed_workers_all.html')
    return HttpResponse(template.render(context, request))

@login_required()
def dismissed_last_month(request):
    month_end = date.today() - relativedelta(months=1)
    works = Work.objects.filter(is_dismiss=True, dismiss_date__gte=month_end).order_by('dismiss_date')
    context = {
        'breadcrumb': [
            {'url': reverse('main:index'), 'title': _("Home")},
            {'url': reverse('tables:dismissed_last_month'), 'title': _('Last month dismissed workers')},
            ],
        'works':works,
        }
    template = loader.get_template('tables/dismissed_workers_all.html')
    return HttpResponse(template.render(context, request))

@login_required()
def none_registered_work(request):
    works = Work.objects.filter(is_dismiss=False)
    contracts = Contract.objects.filter(end__gte=date.today()).values_list('worker', 'position').distinct()
    un_works = []
    for work in works:
        if (work.worker.id, work.position.id) not in contracts:
            un_works.append(work)
    context = {
        'breadcrumb': [
            {'url': reverse('main:index'), 'title': _("Home")},
            {'url': reverse('tables:none_registered_work'), 'title': _('None registered work')},
            ],
        'works':un_works,
        }
    template = loader.get_template('tables/none_registered_work.html')
    return HttpResponse(template.render(context, request))

@login_required()
def pre_dismissed(request):
    works = Work.objects.filter(is_dismiss=False).exclude(dismiss_date__isnull=True)
    context = {
        'breadcrumb': [
            {'url': reverse('main:index'), 'title': _("Home")},
            {'url': reverse('tables:pre_dismissed'), 'title': _('Pre dismissed')},
            ],
        'works':works,
        }
    template = loader.get_template('tables/pre_dismissed.html')
    return HttpResponse(template.render(context, request))


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

#  source: https://leetcode.com/problems/running-sum-of-1d-array/

# Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

# Return the running sum of nums.

# Example 1:

# Input: nums = [1,2,3,4]
# Output: [1,3,6,10]
# Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].

nums = [1, 2, 3, 4]

def running_sum(nums):
    result = []

    # solution 1

    for i in range(len(nums)):
        if i == 0:
            result.append(nums[i])
        else:
            result.append(result[i - 1] + nums[i])
    return result

    # solution 2

    # result.append(nums[0])

    # for i in range(1, len(nums)):
    #     result.append(sum(nums[:i]) + nums[i])
    # return result

print(running_sum(nums))


import tensorflow as tf
import numpy as np
from alexnet import AlexNet
from datagenerator import ImageDataGenerator
from tensorflow.data import Iterator
from datetime import datetime
import os
import pickle

current_dir = os.getcwd()
train_file = os.path.join(current_dir,'train_v2.csv')
val_file = os.path.join(current_dir,'validation_v2.csv')
weights_path = os.path.join(current_dir,'weights','bvlc_alexnet.npy')
learning_rate = 0.001
num_epochs = 100
batch_size = 128
dropout_rate = 0.5
num_classes = 3
train_layers = [['fc8'],['fc7', 'fc8'],['fc6','fc7','fc8'],
                ['conv5','fc6','fc7','fc8']]

#choose amongst which of the train_layers we want to pick
i = 1

# filewriter_path = os.path.join(current_dir,'tensorboard')
checkpoint_path = os.path.join(current_dir,'checkpoints_alexnet')

if not os.path.isdir(checkpoint_path):
    os.mkdir(checkpoint_path)

with tf.device('/cpu:0'):
    tr_data = ImageDataGenerator(train_file,
                                     mode='training',
                                     batch_size=batch_size,
                                     num_classes=num_classes,
                                     shuffle=True)
    val_data = ImageDataGenerator(val_file,
                                      mode='inference',
                                      batch_size=batch_size,
                                      num_classes=num_classes,
                                      shuffle=False)

    iterator = Iterator.from_structure(tr_data.data.output_types,
                                           tr_data.data.output_shapes)
    next_batch = iterator.get_next()

training_init_op = iterator.make_initializer(tr_data.data)
validation_init_op = iterator.make_initializer(val_data.data)

# iterator = tr_data.make_initializable_iterator()
# next_batch = iterator.get_next()

x = tf.placeholder(tf.float32, [batch_size,227,227,3])
y = tf.placeholder(tf.float32,[batch_size, num_classes])
keep_prob = tf.placeholder(tf.float32)

#display_step = 20

model = AlexNet(x,keep_prob,num_classes,train_layers[i],weights_path=weights_path)
score = model.fc8

#list of tf.trainable variables of the layers we want to train
var_list = [v for v in tf.trainable_variables() if v.name.split('/')[0] in train_layers[i]]

with tf.name_scope('cross_ent'):
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=score, labels=y))

with tf.name_scope('train'):
    optimizer = tf.train.AdamOptimizer(learning_rate)
    train_op = optimizer.minimize(loss)

with tf.name_scope('accuracy'):
    correct_pred = tf.equal(tf.argmax(score,1), tf.argmax(y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# for var in var_list:
#     tf.summary.histogram(var.name, var)
#
# tf.summary.scalar('cross_entropy', loss)

# tf.summary.scalar('accuracy', accuracy)

# merged_summary = tf.summary.merge_all()

# writer = tf.summary.FileWriter(filewriter_path)

saver = tf.train.Saver()

train_batches_per_epoch = int(np.floor(tr_data.data_size/batch_size))
val_batches_per_epoch = int(np.floor(val_data.data_size / batch_size))

with tf.Session(config=tf.ConfigProto(log_device_placement=False)) as sess:

    sess.run(tf.global_variables_initializer())

    # writer.add_graph(sess.graph)

    model.load_initial_weights(sess)
    # saver.restore(sess, "checkpoints/model_epoch8.ckpt")

    print("{} Start training...".format(datetime.now()))
    # print("{} Open Tensorboard at --logdir {}".format(datetime.now(),
                                                      # filewriter_path))
    validation_accuracy = []

    for epoch in range(num_epochs):

        print("{} Epoch number: {}".format(datetime.now(), epoch+1))

        sess.run(training_init_op)

        for step in range(train_batches_per_epoch):

            img_batch, label_batch = sess.run(next_batch)

            sess.run(train_op, feed_dict={x: img_batch,
                                          y: label_batch,
                                          keep_prob: dropout_rate})

#            if step % display_step == 0:
#                s = sess.run(merged_summary, feed_dict={x: img_batch,
#                                                        y: label_batch,
#                                                        keep_prob: 1.})

                # writer.add_summary(s, epoch*train_batches_per_epoch + step)

        # Validate the model on the entire validation set
        print("{} Start validation".format(datetime.now()))
        sess.run(validation_init_op)
        val_acc = 0.
        val_count = 0
        for _ in range(val_batches_per_epoch):

            img_batch, label_batch = sess.run(next_batch)
            acc = sess.run(accuracy, feed_dict={x: img_batch,
                                                y: label_batch,
                                                keep_prob: 1.})
            val_acc += acc
            val_count += 1
        val_acc /= val_count
        validation_accuracy.append(val_acc)
        print("{} Validation Accuracy = {:.4f}".format(datetime.now(),
                                                       val_acc))
        if val_acc >=  max(validation_accuracy) and val_acc >= 0.94:
#            for filename in os.listdir(checkpoint_path):
#                os.unlink(filename)
            print("{} Saving checkpoint of model...".format(datetime.now()))

            checkpoint_name = os.path.join(checkpoint_path,
                                           'model_epoch'+str(epoch+1)+'.ckpt')
            save_path = saver.save(sess, checkpoint_name)

            print("{} Model checkpoint saved at {}".format(datetime.now(),
                                                           checkpoint_name))
    with open("validation_accuracy.txt", "wb") as fp:   #Pickling
        pickle.dump(validation_accuracy, fp)


#!/usr/bin/python3

import logging
import os
import sys
import multiprocessing
import configparser

from gensim.models import Word2Vec
from gensim.models.word2vec import LineSentence

def train():
    program = os.path.basename(sys.argv[0])
    logger = logging.getLogger(program)

    logging.basicConfig(format="%(asctime)s: %(levelname)s: %(message)s")
    logging.root.setLevel(level=logging.INFO)

    logger.info("Running %s" % " ".join(sys.argv))

    inp = "./output/wiki.data.utf8.txt"
    out1 = "./models/data.model"
    out2 = "./models/data.vector"

    config = configparser.ConfigParser()
    config.read("./config/w2v.conf", encoding="utf-8")

    train_size = int(config.get("train", "size"))
    train_window = int(config.get("train", "window"))
    train_min_count = int(config.get("train", "min_count"))

    model = Word2Vec(LineSentence(inp), size=train_size, window=train_window,
                       min_count=train_min_count,workers=multiprocessing.cpu_count())

    model.save(out1)
    model.wv.save_word2vec_format(out2, binary=False)

if __name__ == "__main__":
    train()


"""Build FUSE client"""
import daos_build

def scons():
    """Execute build"""
    Import('env', 'prereqs')

    denv = env.Clone()

    denv.AppendUnique(CPPDEFINES=['-DFUSE_USE_VERSION=32'])

    libraries = ['$LIBS', 'daos_common', 'daos', 'uuid']

    dfs_src = ['dfs.c']
    dfs = daos_build.library(denv, 'dfs', dfs_src, LIBS=libraries)
    denv.Install('$PREFIX/lib/', dfs)

    libraries = libraries + ['dfs']

    denv.AppendUnique(LIBPATH=[Dir('.')])

    fenv = denv.Clone()
    prereqs.require(fenv, 'fuse')
    dfuse_hl_src = ['dfuse_hl.c']
    dfuse_hl = daos_build.program(fenv, 'dfuse_hl', dfuse_hl_src,
                                  LIBS=libraries)
    fenv.Install('$PREFIX/bin/', dfuse_hl)

    duns = daos_build.library(denv, 'duns', 'duns.c', LIBS=libraries)
    denv.Install('$PREFIX/lib/', duns)

if __name__ == "SCons.Script":
    scons()


# Copyright (C) 2012 by Alex Brandt <alunduil@alunduil.com>
#
# singularity is freely distributable under the terms of an MIT-style license.
# See COPYING or http://www.opensource.org/licenses/mit-license.php.

import logging
import os

from singularity.configurators import SingularityConfigurator

logger = logging.getLogger(__name__) # pylint: disable=C0103

class GentooNetworkConfigurator(SingularityConfigurator):
    @property
    def function(self):
        return "network"

    @property
    def confd_net_path(self): # pylint: disable=R0201,C0111
        return os.path.join(os.path.sep, "etc", "conf.d", "net")

    def runnable(self, configuration):
        """True if configurator can run on this system and in this context.

        ### Arguments

        Argument      | Description
        --------      | -----------
        configuration | Configuration items to be applied (dict)

        ### Description

        We should be able to run if the following conditions are true:
        * On a Gentoo system
        * Has a writable /etc/conf.d/net file

        """

        if "ips" not in configuration:
            logger.info("Must be passed ips in the message")
            return False

        if not os.access(self.confd_net_path, os.W_OK):
            logger.info("Can't write to %s", self.confd_net_path)
            return False

        logger.info("GentooNetworkConfigurator is runnable!")
        return True

    def content(self, configuration):
        """Generated content of this configurator as a dictionary.

        ### Arguments

        Argumnet      | Description
        --------      | -----------
        configuration | Configuration settings to be applied by this configurator (dict)

        ### Description

        Provides the contents of /etc/conf.d/net.

        """

        lines = []

        logger.debug("ips: %s", configuration["ips"])

        for interface, ips in configuration["ips"].iteritems():
            lines.append("config_{0}=\"".format(interface))
            for ip in ips: # pylint: disable=C0103
                lines.append(ip[0])
            lines.append("\"")

        logger.debug("routes: %s", configuration["routes"])

        for interface, routes in configuration["routes"].iteritems():
            lines.append("routes_{0}=\"".format(interface))
            for route in routes:
                lines.append("{0} via {1}".format(route[0], route[1]))
            lines.append("\"")

        return { self.confd_net_path: lines }


#!/usr/bin/python3
"""
lists all states and there cities table
"""
from sqlalchemy.orm import sessionmaker
from relationship_state import State
from relationship_city import City
from sqlalchemy import create_engine
import sys

if __name__ == "__main__":
    engine = create_engine('mysql+mysqldb://{}:{}@localhost/{}'
                           .format(sys.argv[1], sys.argv[2],
                                   sys.argv[3]), pool_pre_ping=True)
    Session = sessionmaker(bind=engine)
    session = Session()
    states = session.query(State).order_by(State.id)
    for item in states:
        print("{}: {}".format(item.id, item.name))
        for n in range(len(item.cities)):
            print("\t{}: {}".format(item.cities[n].id, item.cities[n].name))
    session.commit()


"""
This file is parsing i2b2 training data and annotating it with
the CoNLL BIO scheme, which has this form:
[word] [POS tag] [chunk tag] [NER tag]
"""

import os
from nltk import pos_tag, RegexpParser
import pandas as pd
import numpy as np

a_ids = []
e_ids = []

for filename in os.listdir("./n2c2 Data/data/annotations"):
    if filename[0] != ".":  # ignore hidden files
        a_ids.append(int(filename))
for filename in os.listdir("./n2c2 Data/data/entries"):
    if filename[0] != ".":
        e_ids.append(int(filename))

a_ids = sorted(a_ids)      #Se ordenan las annotations
e_ids = sorted(e_ids)      #Se ordenan las entries

intersection = list(set(a_ids) & set(e_ids))
if len(intersection) == len(a_ids):
    print("Success: all anotations have a corresponding entry.", len(intersection))

# ------------------- BUILD CORPORA --------------------
# build annotation and entry corpora

a_corpus = []
e_corpus = []

# only annotations and corresponding files
for file in a_ids:
    path = "./n2c2 Data/data/annotations/" + str(file)
    with open(path) as f:
        content = f.read().splitlines()
        a_corpus.append(content)

    path = "./n2c2 Data/data/entries/" + str(file)
    with open(path) as f:
        content = f.read().splitlines()
        e_corpus.append(content)

# ------------------ SET UP DATAFRAME --------------------- (!) UNDERSTAND

#  ["id", "row", "offset", "word", "POS", "chunk", "NER"]
entries_cols = ["id", "row", "offset", "word"]
entries_df = pd.DataFrame(columns=entries_cols)

#print(entries_df.head())

annotations_cols = ["id", "NER_tag", "row", "offset", "length"]
annotations_df = pd.DataFrame(columns=annotations_cols)

#print(annotations_df.head())

# ------------------ NUMBER OF ANNOTATIONS -------------------
med_count = 0
dosage_count = 0
mode_count = 0
freq_count = 0
dur_count = 0
reason_count = 0

for document in a_corpus:
    for line in document:
        if "m=\"nm\"" not in line:
            med_count += 1
        if "do=\"nm\"" not in line:
            dosage_count += 1
        if "mo=\"nm\"" not in line:
            mode_count += 1
        if "f=\"nm\"" not in line:
            freq_count += 1
        if "du=\"nm\"" not in line:
            dur_count += 1
        if "r=\"nm\"" not in line:
            reason_count += 1
"""
print("Medication annotations: ", med_count)
print("Dosage annotations: ", dosage_count)
print("Mode annotations: ", mode_count)
print("Frequency annotations: ", freq_count)
print("Duration annotations: ", dur_count)
print("Reason annotations: ", reason_count)
"""

# ----------- BUILD ANNOTATIONS DATA FRAME ---------------

annotations_df = pd.DataFrame(columns=annotations_cols)  # reset df
tmp_list = []

for i, document in enumerate(a_corpus):

    for row in document:
        row = row.split("||")

        for tag in row:
            tag = tag.split("=") #ex: tag = ['m', '"acetylsalicylic acid" 16:0 16:1']
            if ":" in tag[1]:
                tag_label = tag[0].lstrip(" ") #Remove white spaces before the tag
                tag_row_a = tag[1].split(" ")[-2:][0].split(":")[0]
                tag_row_b = tag[1].split(" ")[-2:][1].split(":")[0]

                # some annotations have non-standard formatting (losing 64 instances)
                try:
                    tag_offset_a = int(tag[1].split(" ")[-2:][0].split(":")[1])
                    tag_offset_b = int(tag[1].split(" ")[-2:][1].split(":")[1])
                    length = tag_offset_b - tag_offset_a + 1

                    # 1 row = 1 token with a tag
                    first = True
                    BIO_tag = "B-"
                    if length > 1 and tag_row_a == tag_row_b:
                        for offset in range(tag_offset_a, tag_offset_b + 1):
                            if first:
                                tag_label = BIO_tag + tag_label
                                first = False
                            else:
                                tag_label = tag_label.replace("B-", "I-")
                            tmp_list.append([a_ids[i], tag_label, tag_row_a, offset, 1])
                    # TODO: tags over line breaks
                    else:
                        tmp_list.append([a_ids[i], BIO_tag + tag_label, tag_row_a, tag_offset_a, length])
                except:
                    pass

annotations_df = pd.DataFrame(tmp_list, columns=annotations_cols)
annotations_df.reset_index(inplace=True)

annotations_df = annotations_df.drop(columns=["index", "length"]) #Remove columns
#print(annotations_df.shape)

# -------------------------- BUILD ENTRIES DATAFRAME -----------------------------------
#List of token modifications: - "|": ignored - "." removed from end of token
entries_df = pd.DataFrame(columns=entries_cols)  # reset df
tmp_list = []

for doc_i, document in enumerate(e_corpus):
    tmp_list.append([0, 0, 0, "-DOCSTART-"])
    tmp_list.append([0, 0, 0, "-EMPTYLINE-"])

    for row_i, row in enumerate(document):
        row_split = row.split(" ")
        for word_i, word in enumerate(row_split):
            word = word.rstrip(".")  # strip "." from end of word
            word = word.replace("\t", "")
            word_id = a_ids[doc_i]
            word_row = row_i + 1  # 1-based indexing
            word_offset = word_i  # 0-based indexing

            if len(word) > 0 and "|" not in word:
                tmp_list.append([word_id, word_row, word_offset, word])

    tmp_list.append([0, 0, 0, "-EMPTYLINE-"])

entries_df = pd.DataFrame(tmp_list, columns=entries_cols)

#print(annotations_df.head())
print(entries_df.head())

ner_counter = [1 for i in annotations_df["NER_tag"] if "B-" in i]
print(len(ner_counter), "named entities")

# --------------- JOING ENTRIES AND ANNOTATIONS ----------------------- (!)

# ensure correct dtypes
annotations_df[['id', 'row', 'offset']] = annotations_df[['id', 'row', 'offset']].apply(pd.to_numeric)
annotations_df['NER_tag'] = annotations_df["NER_tag"].astype(str)
entries_df[['id', 'row', 'offset']] = entries_df[['id', 'row', 'offset']].apply(pd.to_numeric)
entries_df["word"] = entries_df["word"].astype(str)

result_df = pd.merge(entries_df, annotations_df, how="left", on=['id', 'row', 'offset'])

# replace NaNs with "O"
#print("columns with missing data:\n", result_df.isna().any())
result_df = result_df.fillna("O")
#print("columns with missing data:\n", result_df.isna().any())

result_df = result_df.drop(columns=["id", "offset"]) #result_df = result_df.drop(columns=["id", "row", "offset"])
print("Result: ", result_df.head())

# ------------------- POS tagger ------------------------------------ (!)
from nltk.chunk.regexp import RegexpChunkParser, ChunkRule, RegexpParser
from nltk.tree import Tree

text = result_df["word"].tolist()
text_pos = pos_tag(text)
text_pos_list = [i[1] for i in text_pos]

result_df["POS_tag"] = text_pos_list

# ----------------- ConLL chunk tagger --------------------------------
""""
text_test = "EU rejects German call to boycott British lamb.".split(" ")
text_pos_test = pos_tag(text_test)
print(text_pos_test)
"""

#NOUN PHRASES
rule_0 = ChunkRule("<DT>?<JJ.*>*<NN.*>+", "More complete chunk NP sequences")
chunk_parser_np = RegexpChunkParser([rule_0],chunk_label='NP')
chunk_result_tree_np = chunk_parser_np.parse(text_pos)

chunk_tag_np = []

for i in chunk_result_tree_np:
    if isinstance(i, Tree):
        for j in range(0, len(i)):
            if j == 0:
                # print("B-" + i.label())
                chunk_tag_np.append("B-" + i.label())
            else:
                chunk_tag_np.append("I-" + i.label())
                # print("I-" + i.label())
    else:
        # print("O")
        chunk_tag_np.append("O")

#print(len(chunk_tag_np) == result_df.shape[0])  # check that chunk col has same length

#VERB PHRASES

rule_1 = ChunkRule("<VBD|IN|\.>", "Verb phrases")
chunk_parser_vp = RegexpChunkParser([rule_1],chunk_label='VP')
chunk_result_tree_vp = chunk_parser_vp.parse(text_pos)

chunk_tag_vp = []

for i in chunk_result_tree_vp:
    if isinstance(i, Tree):
        for j in range(0, len(i)):
            if j == 0:
                # print("B-" + i.label())
                chunk_tag_vp.append("B-" + i.label())
            else:
                chunk_tag_vp.append("I-" + i.label())
                # print("I-" + i.label())
    else:
        # print("O")
        chunk_tag_vp.append("O")

#len(chunk_tag_np) == result_df.shape[0] == len(chunk_tag_vp)

# augment chunk tags with verb phrase tags
for i, entry in enumerate(chunk_tag_np):
    if entry == "O":
        chunk_tag_np[i] = chunk_tag_vp[i]

# There are not prepositional phrases
result_df["chunk_tag"] = chunk_tag_np
result_df = result_df[['row', 'word', 'POS_tag', 'chunk_tag', 'NER_tag']]  # order columns
result_df[['row', 'word', 'POS_tag', 'chunk_tag', 'NER_tag']] = result_df[['row', 'word', 'POS_tag', 'chunk_tag', 'NER_tag']].astype(str)

# -------------------- DATA SPLIT -----------------------------
print(result_df.shape)
result_df = result_df.reindex()
# find indices of new documents
result_df[result_df["word"] == "-DOCSTART-"].index.values.tolist()

train = 202062
dev = 247618
result_train_df = result_df.iloc[:train]
result_dev_df = result_df.iloc[train:dev]
result_test_df = result_df.iloc[dev:]

print(result_test_df.tail())

print("train shape ", result_train_df.shape)
print("dev shape ", result_dev_df.shape)
print("test shape ", result_test_df.shape)

result_df.to_csv("result_df_NER_POS_chunk.csv")

# ------------------------- WRITE TO TXT --------------------------

np.savetxt("train.txt", result_train_df.values, fmt="%s")
np.savetxt("valid.txt", result_dev_df.values, fmt="%s")
np.savetxt("test.txt", result_test_df.values, fmt="%s")


"""globals.py

This module provides the global variable definitions and various utility
functions for use throughout the mesonet software.

For the time being, the data access password will be stored here.

Simply add "import globals" to a file to access this module.
"""

import logging
import logging.handlers
import time

# root directories
ROOT    = '/home/jddeal/mesonet/'

CONFIG  = '%s/config/'  % ROOT
DATA    = '%s/data/'    % ROOT
LOGS    = '%s/logs/'    % ROOT
SCRIPTS = '%s/scripts/' % ROOT

# plotting directories
PLOTTING = '%s/plotting/' % SCRIPTS
GIS     = '%s/GIS/'     % CONFIG
PICKLES = '%s/pickles/' % PLOTTING

# data directories
OBSERV = '%s/observations/' % DATA
METARS = '%s/metars/'       % OBSERV

# model directories
MODELS = '%s/models/' % DATA
GFS    = '%s/GFS/'    % MODELS
NAM    = '%s/NAM/'    % MODELS
RAP    = '%s/RAP/'    % MODELS

# iamge directory
IMAGES = '%s/images/' % DATA

# Database definitions
HOST      = 'localhost'
DB        = 'mesonet'
USER      = 'meso_user'
PASS      = ''
DATA_USER = 'meso_data'
DATA_PASS = 'not_secure'

# Mesonet boundaries
LL_LAT = 39.0
LL_LON = -106.0
UR_LAT = 52.0
UR_LON = -86.0

# Mesonet states
states = ['North Dakota', 'South Dakota', 'Nebraska', 'Minnesota', 'Iowa', 'Wisconsin']

def create_logger(name, filename):
    """
    Creates and returns a logger object.

    Parameters:
        name:     name of the logger
        filename: full filepath to output file for the log

    Returns:
        logger object
    """

    # formatter
    format = logging.Formatter('%(asctime)s - %(filename)s - %(funcName)s - %(levelname)s - %(message)s')

    # handlers
    console = logging.StreamHandler()
    console.setLevel(logging.INFO)
    console.setFormatter(format)

    filename = filename + time.strftime('_%j-%H.log')
    file = logging.FileHandler(filename = filename)
    file.setLevel(logging.DEBUG)
    file.setFormatter(format)

    # logger
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    logger.addHandler(console)
    logger.addHandler(file)

    return logger


import subprocess
from moviepy.editor import *
import math
import os
from moviepy.config import get_setting

def modify_filename(filename):
     newfilename=''
     for i in filename:
          if i=='\\':
               newfilename+='\\'
          newfilename+=i
     return newfilename

def putText(original_video,new_name,text_list):
    input_file=modify_filename(original_video)
    for i in text_list:
        start=i[0]#/1000
        end=i[1]#/1000
        cmd="ffmpeg -y -i "+input_file+" -vf drawtext=enable=\'between(t,"+str(start)+","+str(end)+")\':fontfile=C\:\\\\\\\\ffmpeg\\\\\\\\arial.ttf\:text="+i[2]+"\:x="+str(i[3][0])+"\:y="+str(i[3][1])+" "+modify_filename(new_name)
        #cmd=[get_setting("FFMPEG_BINARY"),"-y","-i",input_file,"-vf","drawtext=enable=\'between(t,"+str(start)+","+str(end)+")\'","\:fontfile=C\://ffmpeg//arial.ttf","\:text="+i[2],"\:x="+str(i[3][0]),"\:y="+str(i[3][1]),modify_filename(new_name)]
        '''cmd = [get_setting("FFMPEG_BINARY"),"-y",
        "-i", filename,
        "-ss", "%0.2f"%t1,
        "-t", "%0.2f"%(t2-t1),
        "-vcodec", "copy", "-acodec", "copy", targetname]

    p = subprocess.Popen(cmd, shell=True,
                     stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output = p.communicate()[0]'''
        print(cmd)
        p = subprocess.Popen(cmd.split(), shell=True,
                     stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output = p.communicate()[0]
        print(output)

putText("C:\moviepy-master\moviepy-master\lecture7.mp4","C:\moviepy-master\moviepy-master\image_videotext.mp4",[[5,50,"helloworld",(100,100)]])


import unittest
import torch
from pytorch_metric_learning.losses import MarginLoss
from pytorch_metric_learning.utils import common_functions as c_f

class TestMarginLoss(unittest.TestCase):
    def test_margin_loss(self):
        margin, nu, beta = 0.1, 0, 1
        loss_func = MarginLoss(margin=margin, nu=nu, beta=beta)

        embedding_angles = [0, 20, 40, 60, 80]
        embeddings = torch.FloatTensor([c_f.angle_to_coord(a) for a in embedding_angles]) #2D embeddings
        labels = torch.LongTensor([0, 0, 1, 1, 2])

        loss = loss_func(embeddings, labels)

        triplets = [(0,1,2), (0,1,3), (0,1,4), (1,0,2), (1,0,3), (1,0,4), (2,3,0), (2,3,1), (2,3,4), (3,2,0), (3,2,1), (3,2,4)]

        correct_total_loss = 0
        num_non_zero = 0
        for a, p, n in triplets:
            anchor, positive, negative = embeddings[a], embeddings[p], embeddings[n]
            pos_loss = torch.relu(torch.sqrt(torch.sum((anchor-positive)**2)) - beta + margin)
            neg_loss = torch.relu(beta - torch.sqrt(torch.sum((anchor-negative)**2)) + margin)
            correct_total_loss += pos_loss + neg_loss
            if pos_loss > 0:
                num_non_zero += 1
            if neg_loss > 0:
                num_non_zero += 1

        if num_non_zero > 0:
            correct_total_loss /= num_non_zero

        self.assertTrue(torch.isclose(loss, correct_total_loss))

    def test_with_no_valid_triplets(self):
        margin, nu, beta = 0.1, 0, 1
        loss_func = MarginLoss(margin=margin, nu=nu, beta=beta)
        embedding_angles = [0, 20, 40, 60, 80]
        embeddings = torch.FloatTensor([c_f.angle_to_coord(a) for a in embedding_angles]) #2D embeddings
        labels = torch.LongTensor([0, 1, 2, 3, 4])
        self.assertEqual(loss_func(embeddings, labels), 0)


from cloudshell.cli.session.session_exceptions import CommandExecutionException
from cloudshell.devices.flows.cli_action_flows import EnableSnmpFlow
from cloudshell.networking.juniper.cli.juniper_cli_handler import JuniperCliHandler
from cloudshell.networking.juniper.command_actions.commit_rollback_actions import CommitRollbackActions
from cloudshell.networking.juniper.command_actions.enable_disable_snmp_actions import EnableDisableSnmpActions
from cloudshell.snmp.snmp_parameters import SNMPV2Parameters

class JuniperEnableSnmpFlow(EnableSnmpFlow):
    def __init__(self, cli_handler, logger):
        """
        Enable snmp flow
        :param cli_handler:
        :type cli_handler: JuniperCliHandler
        :param logger:
        :return:
        """
        super(JuniperEnableSnmpFlow, self).__init__(cli_handler, logger)
        self._cli_handler = cli_handler

    def execute_flow(self, snmp_parameters):
        if not isinstance(snmp_parameters, SNMPV2Parameters):
            message = 'Unsupported SNMP version'
            self._logger.error(message)
            raise Exception(self.__class__.__name__, message)

        if not snmp_parameters.snmp_community:
            message = 'SNMP community cannot be empty'
            self._logger.error(message)
            raise Exception(self.__class__.__name__, message)

        snmp_community = snmp_parameters.snmp_community
        with self._cli_handler.config_mode_service() as cli_service:
            snmp_actions = EnableDisableSnmpActions(cli_service, self._logger)
            commit_rollback = CommitRollbackActions(cli_service, self._logger)
            if not snmp_actions.configured(snmp_community):
                self._logger.debug('Configuring SNMP with community {}'.format(snmp_community))
                try:
                    output = snmp_actions.enable_snmp(snmp_community)
                    output += commit_rollback.commit()
                    return output
                except CommandExecutionException as exception:
                    commit_rollback.rollback()
                    self._logger.error(exception)
                    raise exception


import json
from django.http import HttpResponse
from django.views import View
from movie_theatre.models.room import Room
from movie_theatre.models.movie import Movie

class RoomsView(View):

    def get(self, request):
        rooms = self.get_all_rooms()
        return HttpResponse(json.dumps({
            'status': 'success',
            'rooms': rooms
        }))

    def get_all_rooms(self):
        rooms = Room.objects.all()
        rooms_dict = dict((room.id, room.to_api_dict()) for room in rooms)
        for room in rooms:
            room_item = rooms_dict.get(room.id, {})
            if room_item:
                movies_playing = list(movie.to_api_dict() for movie in Movie.objects.filter(room=room))
                room_item['movies_playing'] = movies_playing

        return rooms_dict

    def post(self, request):
        new_room = self.create_new_room(request.POST)
        return HttpResponse(json.dumps({
            'status': 'success',
            'message': '{room_name} has been setup'.format(room_name=new_room.room_name),
            'room_id': new_room.id
        }))

    def create_new_room(self, data):
        room_name = data.get('room_name')
        capacity = data.get('capacity')

        new_room = Room(room_name=room_name, capacity=capacity)
        new_room.save()
        return new_room


import parse
import sys

def run(file_):
    with open(file_, 'rb') as file:
        for line in file.readlines():
            parse.parse(line)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        sys.exit("Usage: ./httplang <file.http>")
    inputFile = sys.argv[1]
    run(inputFile)


# Gallegos, Isaac 9-13-2018
# Homewrok 2

from geopy.geocoders import Nominatim
from geopy.distance import geodesic
from geopy.distance import great_circle

# Create a geolocator object to access geo data using Nominatum
geolocator = Nominatim(user_agent="comp_geo_dist.py")

# A query address
query1 = "1314 chavez st, las vegas, nm"
query2 = "1701 bryant st, denver, co"

# Build/retrieve a geopy Location object with query addresses
location1 = geolocator.geocode(query1)
location2 = geolocator.geocode(query2)

# Access the latitude and longitude from location objects
coordinates1 = (location1.latitude, location1.longitude)
coordinates2 = (location2.latitude, location2.longitude)
print(query1, coordinates1)
print(query2, coordinates2)

# Compute and print the distance between locations
distance = great_circle(coordinates1, coordinates2).miles
print("Distance in miles", distance)


#calss header
class _BACKPACK():
	def __init__(self,):
		self.name = "BACKPACK"
		self.definitions = [u'a large bag used to carry things on your back, used especially by people who go camping or walking: ']

		self.parents = []
		self.childen = []
		self.properties = []
		self.jsondata = {}

		self.specie = 'nouns'

	def run(self, obj1 = [], obj2 = []):
		return self.jsondata