#!/usr/bin/python
# -*- coding: utf-8 -*-
#
"""

https://towardsdatascience.com/scraping-a-website-with-4-lines-using-python-200d5c858bb1
https://newspaper.readthedocs.io/en/latest/

cd /Users/brunoflaven/Documents/02_copy/_000_IA_bruno_light/_my_article_python-explorations/git_repo_python_explorations_nlp/article_7_chatbot_with_tensorflow/

python 03_build_chatbot_python.py

"""

#Imports
import string
import random
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.tokenize import PunktSentenceTokenizer
import sklearn
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import pandas as pd
from termcolor import colored

"""
Download it one and then disable

"""

#NLTK Downloads (Need to do only once)
# nltk.download('punkt')
# nltk.download('stopwords')
# nltk.download('averaged_perceptron_tagger')
# nltk.download('wordnet')
# nltk.download('nps_chat')

#Global Constants
GREETING_INPUTS    = ("hello", "hi")
GREETING_RESPONSES = ["hi", "hey", "*nods*", "hi there", "Talkin' to me?"]
FILENAME           = "amazon_english_faq.txt"
#Global Variables
lem = nltk.stem.WordNetLemmatizer()
remove_punctuation = dict((ord(punct), None) for punct in string.punctuation)

#Functions
'''
fetch_features transforms a chat into a classifier friendly format
'''
def fetch_features(chat):
    features = {}
    for word in nltk.word_tokenize(chat):
        features['contains({})'.format(word.lower())] = True
    return features
'''
lemmatise performs lemmatization on words
'''
def lemmatise(tokens):
    return [lem.lemmatize(token) for token in tokens]

'''
tokenise tokenizes the words
'''
def tokenize(text):
    return lemmatise(nltk.word_tokenize(text.lower().translate(remove_punctuation)))

'''
Standard greeting responses that the bot can recognize and respond with
'''
def greet(sentence):
    for word in sentence.split():
        if word.lower() in GREETING_INPUTS:
            return random.choice(GREETING_RESPONSES)
'''
match matches a user input to the existing set of questions
'''
def match(user_response):
    resp      =''
    q_list.append(user_response)

    # TfidfVec  = TfidfVectorizer(tokenizer=tokenise, stop_words='english')

    # TfidfVec = TfidfVectorizer(tokenizer=tokenize, binary=True, stop_words='english', use_idf=True, max_features=feature_cnt)

    # TfidfVec = TfidfVectorizer(tokenizer=tokenize, binary=True, stop_words='english', use_idf=True)

    # remove stop_words='english' and add use_idf=True
    TfidfVec = TfidfVectorizer(tokenizer=tokenize, binary=True, use_idf=True)

    tfidf     = TfidfVec.fit_transform(q_list)
    vals      = cosine_similarity(tfidf[-1], tfidf)
    idx       = vals.argsort()[0][-2]
    flat      = vals.flatten()
    flat.sort()
    req_tfidf = flat[-2]
    if(req_tfidf==0):
        resp = resp+"Sorry! I don't know the answer to this. Would you like to try again? Type Ciao to exit"
        return resp
    else:
        resp_ids = qa_dict[idx]
        resp_str = ''
        s_id = resp_ids[0]
        end = resp_ids[1]
        while s_id<end :
            resp_str = resp_str + " " + sent_tokens[s_id]
            s_id+=1
        resp = resp+resp_str
        return resp

#Training the classifier
chats = nltk.corpus.nps_chat.xml_posts()[:10000]
featuresets = [(fetch_features(chat.text), chat.get('class')) for chat in chats]
size = int(len(featuresets) * 0.1)
train_set, test_set = featuresets[size:], featuresets[:size]
classifier = nltk.MaxentClassifier.train(train_set)
#classifier = nltk.NaiveBayesClassifier.train(train_set) #If you need to test Naive Bayes as well
print(nltk.classify.accuracy(classifier, test_set))

#Question Bank Creation
ques_bank   = open(FILENAME,'r',errors = 'ignore')
qb_text     = ques_bank.read()
qb_text     = qb_text.lower()
sent_tokens = nltk.sent_tokenize(qb_text)# converts to list of sentences
word_tokens = nltk.word_tokenize(qb_text)# converts to list of words
qa_dict     = {} #The Dictionary to store questions and corresponding answers
q_list      = [] #List of all questions
s_count     = 0  #Sentence counter

#Extract questions and answers
#Answer is all the content between 2 questions [assumption]
while s_count < len(sent_tokens):
    result = classifier.classify(fetch_features(sent_tokens[s_count]))
    if("question" in result.lower()):
        next_question_id = s_count+1
        next_question = classifier.classify(fetch_features(sent_tokens[next_question_id]))
        while(not("question" in next_question.lower()) and next_question_id < len(sent_tokens)-1):
            next_question_id+=1
            next_question = classifier.classify(fetch_features(sent_tokens[next_question_id]))
        q_list.append(sent_tokens[s_count])
        end = next_question_id
        if(next_question_id-s_count > 5):
            end = s_count+5
        qa_dict.update({len(q_list)-1:[s_count+1,end]})
        s_count = next_question_id
    else:
        s_count+=1

#Response Fetching
flag=True
print(colored("NONO:\nI am Nono, I have all the answers If you want to exit, type Ciao",'blue',attrs=['bold']))
while(flag==True):
    print(colored("\nYOU:",'red',attrs=['bold']))
    u_input = input()
    u_input = u_input.lower()
    if(u_input!='ciao'):
        if(greet(u_input)!=None):
            print(colored("\nNONO:",'blue',attrs=['bold']))
            print(greet(u_input))
        else:
            print(colored("\nNONO:",'blue',attrs=['bold']))
            print(colored(match(u_input).strip().capitalize(),'blue'))
            q_list.remove(u_input)
    else:
        flag=False
        print(colored("\nNono: Bye! take care..",'blue', attrs=['bold']))


from django.conf.urls import url
from . import views

urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^busqueda/', views.busqueda, name="busqueda"),
url(r'^episodio/(?P<epi_id>[0-9]+)', views.episodio, name='episodio'),
url(r'^personaje/(?P<char_id>[0-9]+)', views.personaje, name='personaje'),
url(r'^lugar/(?P<loc_id>[0-9]+)', views.lugar, name='lugar'),
]


#pylint: disable=C0103
from __future__ import (nested_scopes, generators, division, absolute_import,
                        print_function, unicode_literals)
import os
import sys
#import resource
#import time

from pyNastran.bdf.test.test_bdf import run_lots_of_files
from pyNastran.op2.test.test_op2 import get_failed_files
from pyNastran.op2.test.op2_test import get_all_files
from pyNastran.utils.dev import get_files_of_type

def remove_marc_files(files):
    files2 = []
    for f in files:
        if 'marc' not in f:
            files2.append(f)
    return files2

def get_open_fds():
    fds = []
    soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE)
    for fd in range(0, soft):
        try:
            flags = fcntl.fcntl(fd, fcntl.F_GETFD)
        except IOError:
            continue
        fds.append(fd)
    return fds

def get_file_names_from_file_number(fds):
    names = []
    for fd in fds:
        names.append(os.readlink('/proc/self/fd/%d' % fd))
    return names

if __name__ == '__main__':  # pragma: no cover
    files = get_files_of_type('tests', '.bdf')
    files += get_files_of_type('tests', '.dat')
    foldersFile = 'tests/foldersRead.txt'

    iSubcases = []
    debug = False

    saveCases = True
    regenerate = True
    stopOnFailure = False
    nastran = r'C:\MSC.Software\MSC.Nastran\bin\nastran.exe scr=yes bat=no old=no '
    nastran = ''

    if regenerate:
        files2 = get_all_files(foldersFile, '.bdf')
        files2 += get_all_files(foldersFile, '.nas')
        files2 += get_all_files(foldersFile, '.dat')
        files2 += files
        files2.sort()
    else:
        files2 = get_failed_files('failedCases.in')

    files = remove_marc_files(files2)

    skipFiles = []  # giant

    nStart = 0
    nStop = 10000
    try:
        os.remove('skippedCards.out')
    except:
        pass

    print("nFiles = %s" % len(files))
    cid = None
    check = True
    xref = True
    debug = False
    failed_files = run_lots_of_files(files, debug=debug, xref=xref,
                                     check=check, cid=cid, nastran=nastran)
    ntotal = len(files)
    nfailed = len(failed_files)
    npassed = ntotal - nfailed
    sys.stderr.write('%i/%i passed\n' % (npassed, ntotal))
    try:
        f = open('failedCases.in', 'wb')
    except IOError:
        #fds = get_open_fds()
        #print(get_file_names_from_file_number(fds))
        raise
    for fname in failed_files:
        f.write('%s\n' % fname)
    f.close()
    sys.exit('finished...')


# -*- coding: utf-8 -*-
"""
Created on Mai 17 15:02 2018
@author(s): Florian U. Jehn
"""
import os
import matplotlib.pyplot as plt
import pandas as pd
from pandas.api.types import is_numeric_dtype
import seaborn as sns
import sys

def plot_pca(plotting_df: pd.DataFrame, describers: list):
    """
    Plots the results of the PCA, colored by
    :return:
    """
    # Change the directory, so the figures are all saved in one dir
    os.chdir(os.path.dirname(sys.argv[0]) + os.sep + "pca_plots")

    # Basic set up
    alpha = 0.6
    fig = plt.Figure()

    # Plotting
    for describer in describers:
        sns.lmplot(x="PC 1",
                   y="PC 2",
                   data=plotting_df,
                   hue=describer,
                   fit_reg=False,
                   legend=False,
                   palette="inferno",
                   scatter_kws={"s": 10})
        ax = plt.gca()
        # Put the legend out of the figure
        legend = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.,
                   frameon=True, fancybox=True)
        legend.get_frame().set_edgecolor("grey")
        legend.get_frame().set_facecolor("white")

        # Make plot nicer by removing the borders
        ax.set_facecolor("white")
        for spine in ax.spines.values():
            spine.set_visible(False)

        # Add correct descriptions
        ax.set_title(describer, alpha=alpha)
        ax.set_ylabel("PC 2", alpha=alpha)
        ax.set_xlabel("PC 1", alpha=alpha)
        ax.grid(color="grey", alpha=alpha)
        plt.setp(ax.get_yticklabels(), alpha=alpha)
        plt.setp(ax.get_xticklabels(), alpha=alpha)

        # Save the plot
        fig.tight_layout()
        plt.savefig(describer + ".png",  bbox_inches="tight", dpi=300)
        plt.cla()

def plot_all_meta_data(meta_df: pd.DataFrame, describers=None):
    """
    Plot the meta data of all catchments combined.

    :param meta_df:
    :return:
    """
    meta_df = meta_df.copy()
    if describers is not None:
        meta_df = meta_df[describers]
    sns.violinplot(data=meta_df)
    plt.show()
    plt.cla()

def plot_cross_correlations_heatmap(sig_df: pd.DataFrame):
    """
    Plots a heatmap for the cross correlations

    :param sig_df:
    :return:
    """
    sns.set(font_scale=0.75)
    corr = sig_df.corr()
    ax = sns.heatmap(corr, cmap="inferno", square=True)#, annot=True)
    plt.yticks(rotation=0)
    plt.xticks(rotation=90)
    plt.show()
    plt.cla()

def plot_cluster_metrics(cluster_metrics: pd.DataFrame):
    """
    Plots the cluster metrics as bar plots

    :return: None
    """
    alpha = 0.4
    # Sort the dataframe
    cluster_metrics = cluster_metrics.sort_values(["Calinski-Harabaz Score"],
                                                  ascending=False)
    sns.set_palette("inferno")
    # Only use Calinskite Harabaz as it better pictures the clusters
    ax = sns.barplot(x=cluster_metrics.index,
                     y=cluster_metrics["Calinski-Harabaz Score"],
                     color="silver",
                     edgecolor="black")
    ax.grid(False)
    ax.set_facecolor("white")
    ax.yaxis.grid(color="grey", alpha=alpha-0.2)
    plt.setp(ax.get_yticklabels(), alpha=alpha)
    plt.xticks(rotation=90)
    ax.set_ylabel("Calinski-Harabaz Score [-]", alpha=alpha)
    ax.set_xlabel("")

    plt.savefig("ch_score.png", dpi=300, bbox_inches="tight")
    plt.cla()

def plot_principal_cluster_metrics(scores):
    """

    :param scores:
    :return:
    """
    alpha = 0.6

    ax = plt.subplot(111)
    # Get the right amount of groups
    x = []
    for i, score in enumerate(scores):
        x.append(i + 2)

    # Plot it
    ax.plot(x, scores)
    ax.grid(color="lightgrey")
    ax.set_facecolor("white")
    ax.set_ylabel("Calinski-Harabaz Score [-]", alpha=alpha)
    ax.set_xlabel("Number of Clusters", alpha=alpha)
    plt.setp(ax.get_yticklabels(), alpha=alpha)
    plt.setp(ax.get_xticklabels(), alpha=alpha)
    fig = plt.gcf()
    fig.tight_layout()
    plt.savefig("ch_scores_clusters.png", dpi=300, bbox_inches="tight")
    plt.cla()

def plot_cluster_algo_groups(principal_df: pd.DataFrame, groups,
                         num_groups=None):
    """
    Plots the pca results and colors them by the clusterings groups.

    :param principal_df:
    :param kmeans_groups:
    :return:
    """

    alpha = 0.6
    fig = plt.Figure()
    plotting_df = principal_df.copy()
    plotting_df["groups"] = groups
    plotting_df["groups"] += 1

    # Plotting
    sns.lmplot(x="PC 1",
               y="PC 2",
               data=plotting_df,
               hue="groups",
               fit_reg=False,
               legend=False,
               palette="terrain",
               scatter_kws={"s": 40, "alpha": 0.85})
    ax = plt.gca()
    # Put the legend out of the figure
    legend = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.,
               frameon=True, fancybox=True)
    legend.get_frame().set_edgecolor("grey")
    legend.get_frame().set_facecolor("white")

    # Make plot nicer by removing the borders
    ax.set_facecolor("white")
    for spine in ax.spines.values():
        spine.set_visible(False)

    # Add correct descriptions
    ax.set_ylabel("PC 2", alpha=alpha)
    ax.set_xlabel("PC 1", alpha=alpha)
    ax.grid(color="grey", alpha=alpha)
    plt.setp(ax.get_yticklabels(), alpha=alpha)
    plt.setp(ax.get_xticklabels(), alpha=alpha)

    # Save the plot
    fig.tight_layout()
    plt.savefig(str(num_groups)+"_groups.png",  bbox_inches="tight",
                dpi=300)
    plt.cla()

def plot_scatter_matrix(plotting_df: pd.DataFrame):
    """
    Plots a scatter matrix.

    :param plotting_df:
    :return: None
    """
    signatures = list(plotting_df.columns.unique())
    describers = []
    describers.append(signatures.pop())
    describers.append(signatures.pop())
    describers.append(signatures.pop())
    ax = sns.pairplot(plotting_df, y_vars=signatures, x_vars=describers,
                      diag_kind="kde", kind="reg")
    fig = plt.gcf()
    fig.tight_layout()
    plt.savefig("scatter_matrix.png",  bbox_inches="tight",
                dpi=300)
    plt.cla()

def plot_groups_violin(plot_df: pd.DataFrame, groups, describers: list = None):
    """
    Plot the meta data of all different groups based on the clustering as
    violin plots.

    :param meta_df: dataframe with all the meta information
    :param labels: labels produced by the clustering algorithm
    :return:
    """
    alpha = 0.6
    plot_df = plot_df.copy()
    if describers is not None:
        plot_df = plot_df[describers]
    properties = plot_df.columns

    plot_df["groups"] = groups
    # Add one, so they are easier to read

    for property in properties:
        # Always create a new axis to plot in
        ax = plt.subplot(111)
        if is_numeric_dtype(plot_df[property]):
            ax = sns.violinplot(x="groups", y=property, data=plot_df, ax=ax,
                                 cut=0, palette="terrain")
            ax.grid(False)
            ax.yaxis.grid(color="lightgrey")
            plt.setp(ax.get_yticklabels(), alpha=alpha)
            ax.set_xticklabels(sorted(set(groups)), alpha=alpha)
            ax.set_xlabel("Catchment Groups", alpha=alpha)
            ax.set_ylabel(property, alpha=alpha)
            ax.set_facecolor("white")
            fig = plt.gcf()
            fig.tight_layout()
            plt.savefig(property+"_violin.png", dpi=300, bbox_inches="tight")
        else:
            # Sort dataframe to get the right values for the groups
            bar_df = plot_df[[property, "groups"]].groupby("groups")[
                property].value_counts(normalize=True).unstack()
            # Plot it
            ax = bar_df.plot(kind="bar", stacked=True, ax=ax, cmap="terrain")
            ax.grid(color="grey", alpha=alpha)
            plt.setp(ax.get_yticklabels(), alpha=alpha)
            ax.set_xticklabels(sorted(set(groups)), alpha=alpha)
            ax.set_xlabel("Catchment Groups", alpha=alpha)
            ax.set_ylabel(property, alpha=alpha)
            ax.set_facecolor("white")

            fig = plt.gcf()
            fig.tight_layout()
            plt.savefig(property + "stacked_bar.png", dpi=300,
                        bbox_inches="tight")
        plt.cla()

def plot_median_heatmap(medians, cmap, name, groups=None):
    """
    Takes the normalized medians and plots them as a heatmap.

    :param medians:
    :return: None
    """
    ax = sns.heatmap(medians.transpose(), cmap=cmap, linewidths=0.1,
                     linecolor="lightgrey", square=True)
    ax.set_xticklabels(sorted(set(groups)))
    plt.yticks(rotation=0)

    fig = plt.gcf()
    fig.tight_layout()
    plt.savefig("median_heatmap_"+name+".png", dpi=300, bbox_inches="tight")
    plt.cla()


#!/usr/bin/python3

import requests
from urllib3.exceptions import InsecureRequestWarning

# site: https://revoked.badssl.com
# state should be: bad, revoked certificate
# verification: disabled
# comment: suppress warning about disabled verification via package configuration

badssl_startsite = "https://revoked.badssl.com"
https_verification = False

def verification_on():
    if https_verification:
        print("Verification is enabled")
    else:
        print("Verification is disabled")

def test_disabled_verification_with_warning():
    requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
    # alternative: requests.packages.urllib3.disable_warnings(requests.packages.urllib3.exceptions.InsecureRequestWarning)
    try:
        print("Starting a request to " + badssl_startsite)
        requests.post(badssl_startsite, verify = https_verification)
    except Exception as e:
        print("There was an error requesting " + badssl_startsite)
        print("Error:"+str(e))
        return
    print("Request returned successfully")

if __name__ == "__main__":
    verification_on()
    test_disabled_verification_with_warning()


from discord import Message

def thank(message, database, *args):
    """Thanks a particular user for his help.

    Args:
        recipient: Mention of the user to be thanked (e.g @Username#1234).

    Returns:
        A discord message acknowledging the thanks have been recorded.
    """
    thanks_giver_id = message.author_id
    if not database.member_exists(thanks_giver_id):
        database.add_member(thanks_giver_id, message.username)

    for recipient in message.mentions:
        if not database.member_exists(recipient.id):
            database.add_member(recipient.id, recipient.composite_username)

        database.thank_member(thanks_recipient_id=recipient.id, thanks_giver_id=thanks_giver_id)

    if len(message.mentions) == 0:
        response_content = "No thanks were given as no members were mentioned."
    else:
        response_content = "%s thanked %s!" % (message.username, recipient.composite_username)
    response = Message(response_content, message.channel_id, "Charlotte", "Charlotte")
    return response


# Import libraries for using pydotplus and handling png images
import cv2
import pydotplus
from cycleGraph import Graph
from inputUtility import get_num_of_transactions
from makeGraph import make_graph
# Set path for files used
INPUT_PATH = "input.txt"
OUTPUT_PATH = "output.txt"
#________________________________________________________________________________________#

# Parsing the input into a directed graph and finding cycle to check conflict serializability
with open(INPUT_PATH, "r") as infile:
	file_counter = 1
	for schedule in infile.readlines():
		# Print the id of schedule
		print ("Schedule " + str(file_counter) + ":", end = " ")

		# Find the number of transaction in the schedule
		num_of_transactions = get_num_of_transactions(schedule)

		# Make a graph to detect cycle and topological sorting
		cgraph = Graph(num_of_transactions)

		# Create a pydotplus graph
		graph = pydotplus.Dot(graph_type='digraph', ordering='out')

		make_graph(graph, cgraph, schedule)

		# Check for cycle in the graph and find serializability order using topological sorting
		if cgraph.isCyclic(graph) == 1:
		    print ("Precedence Graph is cyclic. Hence it is not conflict serializable.")
		else:
		    print ("Precedence Graph is not cyclic. Hence it is conflict serializable.")
		    cgraph.topologicalSort(graph)

	    # Print the graph on a png image using pydotplus graph
		graph.write_png('precedence' + str(file_counter) + '.png')

		# Show that image on the screen
		img = cv2.imread('precedence' + str(file_counter) + '.png')
		cv2.imshow("Precedence Graph " + str(file_counter), img)
		cv2.waitKey(0)

		# Increment the file counter for next handling schedule
		file_counter += 1


import numpy as np
import glob
import time
import os
import json
import time

all_path = []
def getallfile(path):
    allfilelist = os.listdir(path)
    for file in allfilelist:
        filepath=os.path.join(path,file)
        if 'invalid' in filepath:continue
        if os.path.isdir(filepath):
            getallfile(filepath)
        elif os.path.isfile(filepath):
            if 'json' in filepath:
                all_path.append(filepath)

label_dict = {'ebike2_front': 0,
              'ebike2_back': 0,
              'ebike2_other': 0,
              'ebike2_sunshade_front': 0,
              'ebike2_sunshade_back': 0,
              'ebike2_sunshade_other': 0,
              'ebike3_front': 0,
              'ebike3_back': 0,
              'ebike3_other': 0,
              'takeout_ebike2_front': 0,
              'takeout_ebike2_back': 0,
              'takeout_ebike2_other': 0,
              'takeout_ebike2_sunshade_front': 0,
              'takeout_ebike2_sunshade_back': 0,
              'takeout_ebike2_sunshade_other': 0,
              'takeout_ebike3_front': 0,
              'takeout_ebike3_back': 0,
              'takeout_ebike3_other': 0,
              'blank': 0,
              'head_with_helmet': 0,
              'head_without_helmet': 0,
              'license_ebike': 0,
              'license_moto': 0,
              'license_other': 0,
              'moto_front': 0,
              'moto_back': 0,
              'moto_other': 0
              }

label_home = os.getcwd()
getallfile(label_home)
time_now = time.strftime("%Y%m%d-%H%M", time.localtime())

haitu_path = []
ours_path = []

txtpath = os.path.join(label_home, time_now+".txt")

total_num = len(all_path)
for path in all_path:
    ftt1 = open(path, 'r')
    load_dict1 = json.load(ftt1)
    for target in load_dict1['shapes']:
        if target['label'] not in label_dict:
            print ("wrong label!~ ", path)
            print(target['label'])
            continue
        label_dict[target['label']]+=1
total_box_num = 0

for key,v in label_dict.items():
    total_box_num += v

# for home, dirs, files in os.walk(label_home):
#     for dir in dirs:
#         paths = glob.glob(os.path.join(os.path.join(home,dir), '*.json'))
#         total_num += len(paths)
#         for path in paths:
#             ftt1 = open(path, 'r')
#             load_dict1 = json.load(ftt1)
#             for target in load_dict1['shapes']:
#                 if target['label'] not in label_dict:
#                     print ("wrong label!~ ", path)
#                     continue
#                 label_dict[target['label']]+=1

with open(txtpath, 'w') as fw:
    print("total num = ",total_num)
    print("total_box_num = ",total_box_num)
    print("="*64)
    fw.write("total box num = " + str(total_box_num) + "\n")
    fw.write("total num = " + str(total_num) + "\n")
    fw.write("="*64)
    fw.write('\n')
    for key,v in label_dict.items():
        print('%-30s : %d'%(key,v))
        #fw.write(key+" : "+ str(v) +'\n')
        fw.write('%-30s : %d \n'%(key,v))


# coding=utf-8
from django.conf.urls import patterns, url

__author__ = 'alexy'

urlpatterns = patterns(
    'apps.sign.views',
    # url(r'^moderator/$', 'moderator_sign', name='moderator'),
    # url(r'^manager/$', 'manager_sign', name='manager'),
    # url(r'^client/$', 'client_sign', name='client'),
    # url(r'^adjuster/$', 'adjuster_sign', name='adjuster'),
    url(r'^moderator/$', 'sign_in', {'usertype': 2}, name='moderator'),
    url(r'^client/$', 'sign_in', {'usertype': 3},  name='client'),
    url(r'^adjuster/$', 'sign_in', {'usertype': 4}, name='adjuster'),
    url(r'^manager/$', 'sign_in', {'usertype': 5},  name='manager'),
)


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

urlpatterns = [
  # NOTE: the root url (r'^') HAS TO BE LAST in this list or else there is an error where the root url overrides other urls
    url(r'^contact/', views.contact, name='contact'),
    url(r'^about/', views.about, name='about'),
    url(r'^compare/',views.compare,name='compare'),
    url(r'^', views.select_boards, name='select_boards')
]


# coding=utf8
from django.shortcuts import get_object_or_404, render_to_response
from django.template import RequestContext

from locations.models import Location
from navigation.models import Page
from users.models import Role

def main(request):
    voter_count = Role.objects.filter(type='voter').count()
    if voter_count%10 == 1:
        ending = u'ь'
    elif voter_count%10 in (2, 3, 4):
        ending = u'я'
    else:
        ending = u'ей'

    context = {
        'voter_count': voter_count,
        'ending': ending,
        'locations': list(Location.objects.filter(region=None).order_by('name')),
    }
    return render_to_response('main.html', context_instance=RequestContext(request, context))

def static_page(request, name, template):
    #page = get_object_or_404(Page, name=name)
    context = {
        #'content': page.content,
    }
    return render_to_response(template, context_instance=RequestContext(request, context))

def map_search(request):
    context = {
        'locations': Location.objects.all(),
    }
    return render_to_response('map.html', context_instance=RequestContext(request, context))

def development(request):
    return render_to_response('development.html', context_instance=RequestContext(request, {}))


#算法：把所有的数字补成最大的值是基础次数，如果小于这个次数，就是当前的最大值。如果大于这个次数，就是添加后的最大值
import sys
l = list(map(int,sys.stdin.readline().strip().split()))
n,k= l[0],l[1]
#print(l)
a = list(map(int,sys.stdin.readline().strip().split()))
#print(a)
max_a = max(a)
count = 0
for i in a:
    count += max_a - i
#print(count)
if k <= count:
    print(max_a)
else:
    plus = ((k - count-1)//n)+1
    print(max_a+ plus)

import sys
l = list(map(int,sys.stdin.readline().strip().split()))
n,m= l[0],l[1]
# 算法：如果两边除了边界上，存在一条直线是都是1的，必不能聚集

zhaozhe= []
for i in range(n):
    tmp_l = []
    a= sys.stdin.readline()
    for i in a[:-1]:
        tmp_l.append(int(i))
    zhaozhe.append(tmp_l)
#print(zhaozhe)
k = int(sys.stdin.readline().strip())
arm= []
for i in range(k):
    arm.append(list(map(int,sys.stdin.readline().strip().split())))
#print(arm)
for i in range(1,n-1):
    count = 0
    for j in range(m):
        if zhaozhe[i][j] == 1:
            count += 1
    if count == m:
        print(-1)
        break
for i in range(1,m-1):
    count = 0
    for j in range(n):
        if zhaozhe[j][i] == 1:
            count += 1
    if count == n:
        print(-1)
        break


import cv2
import numpy as np
from PIL import Image

def main():
    img_bgr = cv2.imread("inh.jpg")
    height, width, channel = img_bgr.shape
    img = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2GRAY)

    k=img.shape[0]
    l=img.shape[1]

    randomnumber={}
    encrypted = np.zeros((height, width,3), np.uint8)
    random = np.zeros((height, width,3), np.uint8)
    for i in range (k):#traverses through height of the image
        for j in range (l): #traverses through width of the image
#                original[i,j]=img[i,j]
            randomnumber[i,j]=np.random.randint(0,255)
            random[i,j]=randomnumber[i,j]
#                img_lbp[i, j] = lbp_calculated_pixel(img, i, j)
            # print(randomnumber[i,j])

            # Encryption

            if(((randomnumber[i,j]>=128) and (img[i,j]>=128))):
                encrypted[i,j]=img[i,j]-128
                temp=img[i,j]-128
            elif(((randomnumber[i,j]<128) and (img[i,j]<128))):
                encrypted[i,j]=img[i,j]
                temp=img[i,j]
            elif(((randomnumber[i,j]>=128) and (img[i,j]<128))):
                encrypted[i,j]=img[i,j]+128
                temp=img[i,j]+128
            else:
                encrypted[i,j]=img[i,j]
                temp=img[i,j]

#                enc_img_lbp[i, j] = lbp_calculated_pixel(img, i, j)
            print(encrypted[i,j])
        encrypt_image=Image.fromarray(encrypted)
        encrypt_image.save("encrypt.jpg")
if __name__ == '__main__':
    main()


import pandas as pd
from pylab import *

class EventFeaturesReader():
  def __init__(self, dataDir, run, backend):
    self.dataDir = dataDir
    self.run = run
    self.backend = backend

    fileName = dataDir+"/{}_run{}".format(backend,run)
    self.data = pd.read_csv(fileName+"_eventFeatures.csv")
    self.data["pulseAmp_sigmaNoise"] = self.data.pulseAmp/self.data.baseline_std

  def getChannel(self,channel):
    ch = self.data.query("ch=={}".format(channel))

    if self.backend == "VME": #Added cut on pulseAmp_simaNoise
      ch = ch.query(" pulsePos<{} & pulsePos>{} & pulseAmp_sigmaNoise>{}".format(1415+10,1415-10,0))
    if self.backend == "ubcm":
      ch = ch.query(" pulsePos<{} & pulsePos>{} & pulseAmp_sigmaNoise>{}".format(295+25,295-20,0))

    ch.reset_index(drop=True, inplace=True)
    return ch


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

# Author: James Diprose

import distutils.dir_util
import os
import shutil
import subprocess
from subprocess import Popen
from typing import Tuple

from observatory.api.server.openapi_renderer import OpenApiRenderer
from observatory.platform.cli.click_utils import indent, INDENT1
from observatory.platform.observatory_config import TerraformConfig, BackendType
from observatory.platform.platform_builder import PlatformBuilder
from observatory.platform.utils.config_utils import module_file_path
from observatory.platform.utils.jinja2_utils import render_template
from observatory.platform.utils.proc_utils import stream_process

def copy_dir(source_path: str, destination_path: str, ignore):
    distutils.dir_util.copy_tree(source_path, destination_path)

class TerraformBuilder:
    def __init__(self, config_path: str, debug: bool = False):
        """Create a TerraformBuilder instance, which is used to build, start and stop an Observatory Platform instance.

        :param config_path: the path to the config.yaml configuration file.
        :param debug: whether to print debug statements.
        """

        self.api_package_path = module_file_path("observatory.api", nav_back_steps=-3)
        self.terraform_path = module_file_path("observatory.platform.terraform")
        self.api_path = module_file_path("observatory.api.server")
        self.debug = debug

        # Load config
        config_exists = os.path.exists(config_path)
        if not config_exists:
            raise FileExistsError(f"Terraform config file does not exist: {config_path}")
        else:
            self.config: TerraformConfig = TerraformConfig.load(config_path)
            self.config_is_valid = self.config.is_valid
            if self.config_is_valid:
                self.build_path = os.path.join(self.config.observatory.observatory_home, "build", "terraform")
                self.platform_builder = PlatformBuilder(
                    config_path=config_path,
                    docker_build_path=os.path.join(self.build_path, "docker"),
                    backend_type=BackendType.terraform,
                )
                self.packages_build_path = os.path.join(self.build_path, "packages")
                self.terraform_build_path = os.path.join(self.build_path, "terraform")
                os.makedirs(self.packages_build_path, exist_ok=True)
                os.makedirs(self.terraform_build_path, exist_ok=True)

    @property
    def is_environment_valid(self) -> bool:
        """Return whether the environment for building the Packer image is valid.

        :return: whether the environment for building the Packer image is valid.
        """

        return all([self.packer_exe_path is not None, self.config_is_valid, self.config is not None])

    @property
    def packer_exe_path(self) -> str:
        """The path to the Packer executable.

        :return: the path or None.
        """

        return shutil.which("packer")

    @property
    def gcloud_exe_path(self) -> str:
        """The path to the Google Cloud SDK executable.

        :return: the path or None.
        """

        return shutil.which("gcloud")

    def make_files(self):
        # Clear terraform/packages path
        if os.path.exists(self.packages_build_path):
            shutil.rmtree(self.packages_build_path)
        os.makedirs(self.packages_build_path)

        ignore = shutil.ignore_patterns("__pycache__", "*.eggs", "*.egg-info")

        # Copy local packages
        for package in self.config.python_packages:
            if package.type == "editable":
                destination_path = os.path.join(self.packages_build_path, package.name)
                copy_dir(package.host_package, destination_path, ignore)

        # Copy terraform files into build/terraform: ignore jinja2 templates
        copy_dir(self.terraform_path, self.terraform_build_path, shutil.ignore_patterns("*.jinja2", "__pycache__"))

        # Make startup scripts
        self.make_startup_script(True, "startup-main.tpl")
        self.make_startup_script(False, "startup-worker.tpl")

        # Make OpenAPI specification
        self.make_open_api_template()

    def make_startup_script(self, is_airflow_main_vm: bool, file_name: str):
        # Load and render template
        template_path = os.path.join(self.terraform_path, "startup.tpl.jinja2")
        render = render_template(template_path, is_airflow_main_vm=is_airflow_main_vm)

        # Save file
        output_path = os.path.join(self.terraform_build_path, file_name)
        with open(output_path, "w") as f:
            f.write(render)

    def make_open_api_template(self):
        # Load and render template
        specification_path = os.path.join(self.api_path, "openapi.yaml.jinja2")
        renderer = OpenApiRenderer(specification_path, cloud_endpoints=True)
        render = renderer.render()

        # Save file
        output_path = os.path.join(self.terraform_build_path, "openapi.yaml.tpl")
        with open(output_path, "w") as f:
            f.write(render)

    def build_terraform(self):
        """Build the Observatory Platform Terraform files.

        :return: None.
        """

        self.make_files()
        self.platform_builder.make_files()

    def build_image(self) -> Tuple[str, str, int]:
        """Build the Observatory Platform Google Compute image with Packer.

        :return: output and error stream results and proc return code.
        """

        # Make Terraform files
        self.build_terraform()

        # Load template
        template_vars = {
            "credentials_file": self.config.google_cloud.credentials,
            "project_id": self.config.google_cloud.project_id,
            "zone": self.config.google_cloud.zone,
            "environment": self.config.backend.environment.value,
        }
        variables = []
        for key, val in template_vars.items():
            variables.append("-var")
            variables.append(f"{key}={val}")

        # Build the containers first
        args = ["packer", "build"] + variables + ["-force", "observatory-image.json"]

        if self.debug:
            print("Executing subprocess:")
            print(indent(f"Command: {subprocess.list2cmdline(args)}", INDENT1))
            print(indent(f"Cwd: {self.terraform_build_path}", INDENT1))

        proc: Popen = subprocess.Popen(
            args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.terraform_build_path
        )

        # Wait for results
        # Debug always true here because otherwise nothing gets printed and you don't know what the state of the
        # image building is
        output, error = stream_process(proc, True)
        return output, error, proc.returncode

    def gcloud_activate_service_account(self) -> Tuple[str, str, int]:
        args = ["gcloud", "auth", "activate-service-account", "--key-file", self.config.google_cloud.credentials]

        if self.debug:
            print("Executing subprocess:")
            print(indent(f"Command: {subprocess.list2cmdline(args)}", INDENT1))
            print(indent(f"Cwd: {self.api_package_path}", INDENT1))

        proc: Popen = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.api_package_path)

        # Wait for results
        # Debug always true here because otherwise nothing gets printed and you don't know what the state of the
        # image building is
        output, error = stream_process(proc, True)
        return output, error, proc.returncode

    def gcloud_builds_submit(self) -> Tuple[str, str, int]:
        # Build the google container image
        project_id = self.config.google_cloud.project_id
        # --gcs-logs-dir is specified to avoid storage.objects.get access error, see:
        # https://github.com/google-github-actions/setup-gcloud/issues/105
        # the _cloudbuild bucket is created already to store the build image
        args = [
            "gcloud",
            "builds",
            "submit",
            "--tag",
            f"gcr.io/{project_id}/observatory-api",
            "--project",
            project_id,
            "--gcs-log-dir",
            f"gs://{project_id}_cloudbuild/logs",
        ]
        if self.debug:
            print("Executing subprocess:")
            print(indent(f"Command: {subprocess.list2cmdline(args)}", INDENT1))
            print(indent(f"Cwd: {self.api_package_path}", INDENT1))

        proc: Popen = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.api_package_path)

        # Wait for results
        # Debug always true here because otherwise nothing gets printed and you don't know what the state of the
        # image building is
        output, error = stream_process(proc, True)

        info_filepath = os.path.join(self.terraform_build_path, "api_image_build.txt")
        with open(info_filepath, "w") as f:
            f.writelines(line + "\n" for line in output.splitlines()[-2:])
        return output, error, proc.returncode


import math

class date(object):
    def __init__(self, month, day, year):
        self.day = day
        self.month = month
        self.year = year

def determine_smaller_date(date1, date2):
    """Given two dates, returns a tuple representing
    <smaller date>, <larger date>."""
    if (date1.year <= date2.year):
        if (date1.year == date2.year):
            if (date1.month <= date2.month):
                if (date1.month == date2.month):
                    if (date1.day <= date2.day):
                        # dates could be equiv here as well
                        return date1, date2
                    else:
                        return date2, date1
                else:
                    return date1, date2
            else:
                return date2, date1
        else:
            return date1, date2
    else:
        return date2, date1

def month_diff_bw_dates(date1, date2):
    """If two dates are more than a month apart, returns 1.
    Exactly a month apart (based on day value for each date), returns 0.
    Less than a month apart, returns -1."""
    # more than a month
    # years differ too much
    if (math.fabs(date1.year - date2.year) > 1):
        return 1

    # detect year boundary scenario
    if (math.fabs(date1.year - date2.year) == 1):
        # requires knowing which date is smaller
        smaller_date, larger_date = determine_smaller_date(date1, date2)

        # years differ, but we've got the boundary scenario of
        # 12/x and 1/(x+1);
        if ((smaller_date.month == 12) and (larger_date.month == 1)):
            # more than a month if days are too far apart
            if (larger_date.day > smaller_date.day):
                return 1

            # exactly a month
            if (larger_date.day == smaller_date.day):
                return 0

            # less than a month
            return -1
    else:
        # years are the same
        # difference b/w month values is > 1
        if (math.fabs(date1.month - date2.month) > 1):
            return 1

        # years are the same, month 1 apart
        if (math.fabs(date1.month - date2.month) == 1):
            # requires knowing which date is smaller
            smaller_date, larger_date = determine_smaller_date(date1, date2)

            # more than a month if days are too far apart
            if (larger_date.day > smaller_date.day):
                return 1

            # exactly a month
            if (larger_date.day == smaller_date.day):
                return 0

            # less than a month
            return -1

        return -1


import tensorflow as tf
import time
from cnn_train_clustering import MNISTtrain

# tf.train.ClusterSpec 정의를 위한 설정
tf.app.flags.DEFINE_string("ps_hosts", "",
                           "Comma-separated list of hostname:port pairs")
tf.app.flags.DEFINE_string("worker_hosts", "",
                           "Comma-separated list of hostname:port pairs")

# tf.train.Server 정의를 위한 설정
tf.app.flags.DEFINE_string("job_name", "", "One of 'ps', 'worker'")
tf.app.flags.DEFINE_integer("task_index", 0, "Index of task within the job")

FLAGS = tf.app.flags.FLAGS

def main(_):

    ps_hosts = FLAGS.ps_hosts.split(",")
    worker_hosts = FLAGS.worker_hosts.split(",")

    # 변수 서버와 작업자 클러스터를 생성한다.
    cluster = tf.train.ClusterSpec({"ps": ps_hosts, "worker": worker_hosts})

    # 로컬 작업 수행을 위해 서버를 생성하고 구동한다.
    server = tf.train.Server(cluster,
                           job_name=FLAGS.job_name,
                           task_index=FLAGS.task_index)

    mnistTrain = MNISTtrain()

    loop_count = mnistTrain.get_batch_size()

    if FLAGS.job_name == "ps":
        server.join()

    elif FLAGS.job_name == "worker":

        # 작업자에 연산을 분배한다.
        with tf.device(tf.train.replica_device_setter(
            worker_device="/job:worker/task:%d" % FLAGS.task_index, cluster=cluster)):

            # 모델 구축...
            #loss = mnistTrain.get_loss()

            global_step = tf.get_variable("global_step", 0, dtype=tf.float32)

            train_op = mnistTrain.get_train(global_step)

            saver = tf.train.Saver()

            summary_op = tf.merge_all_summaries()

            #init_op = tf.initialize_all_variables()
            init_op = tf.global_variables_initializer()

            # 훈련 과정을 살펴보기 위해 "supervisor"를 생성한다.
            sv = tf.train.Supervisor(is_chief=(FLAGS.task_index == 0),
                                     logdir="/tmp/train_logs",
                                     init_op=init_op,
                                     summary_op=summary_op,
                                     saver=saver,
                                     global_step=global_step,
                                     save_model_secs=600)

        # supervisor는 세션 초기화를 관리하고, checkpoint로부터 모델을 복원하고
        # 에러가 발생하거나 연산이 완료되면 프로그램을 종료한다.

        start_time = time.time()

        with sv.managed_session(server.target) as sess:

            for step in range(loop_count):

                if sv.should_stop() : break

                print("step : " , step)

                # offset = (step * MNISTtrain.BATCH_SIZE) % (MNISTtrain.train_size - MNISTtrain.BATCH_SIZE)
                # batch_data = MNISTtrain.train_data[offset: (offset + MNISTtrain.BATCH_SIZE)]
                # batch_labels = MNISTtrain.train_labels[offset:(offset + MNISTtrain.BATCH_SIZE)]
                #
                # feed_dict = {MNISTtrain.train_data_node: batch_data, MNISTtrain.train_labels_node: batch_labels}
                #
                # _, step = sess.run([train_op, global_step] , feed_dict=feed_dict)

                # if step % MNISTtrain.EVAL_FREQUENCY == 0:
                #     l, lr, predictions = sess.run([MNISTtrain.cost, MNISTtrain.learning_rate, MNISTtrain.train_prediction], feed_dict=feed_dict)
                #     elapsed_time = time.time() - start_time
                #     start_time = time.time()
                #
                #     print('--------------------------------------------------------------------')
                #     print('Step %d (epoch %.2f), %.1f ms' % (
                #       step, float(step) * MNISTtrain.BATCH_SIZE / MNISTtrain.train_size, 1000 * elapsed_time / MNISTtrain.EVAL_FREQUENCY))
                #     print('Minibatch loss: %.3f, learning rate: %.6f' % (l, lr))
                #
                #     print('Step %d (epoch %.2f), %.1f ms' % (
                #     step, float(step) * MNISTtrain.BATCH_SIZE / MNISTtrain.train_size, 1000 * elapsed_time / MNISTtrain.EVAL_FREQUENCY))

        sv.stop()

if __name__ == "__main__":
    tf.app.run()


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

AUTHOR = u'wutali'
SITENAME = u'blog.wuta.li'
SITEURL = 'http://blog.wuta.li.dev'
FEED_DOMAIN = SITEURL
TIMEZONE = 'Asia/Tokyo'
DEFAULT_LANG = u'en'
THEME = u'wutali_theme'


from os.path import splitext, join
import shutil
import tempfile

import click

from rv.detection.commands.make_label_map import _make_label_map
from rv.detection.commands.make_train_chips import _make_train_chips
from rv.detection.commands.make_tf_record import _make_tf_record
from rv.detection.commands.transform_geojson import _transform_geojson
from rv.detection.commands.settings import (
    default_channel_order, temp_root_dir)
from rv.utils.misc import load_projects
from rv.utils.files import (
    download_if_needed, make_dir, get_local_path, upload_if_needed,
    MyTemporaryDirectory)

def filter_annotations(annotations_paths, temp_dir, min_area, single_label):
    filtered_annotations_dir = join(temp_dir, 'filtered_annotations')
    make_dir(filtered_annotations_dir)
    filtered_annotations_paths = []
    for annotation_ind, annotations_path in enumerate(annotations_paths):
        filtered_annotations_path = join(
            filtered_annotations_dir, '{}.json'.format(annotation_ind))
        _transform_geojson(
            annotations_path, filtered_annotations_path, min_area=min_area,
            single_label=single_label)
        filtered_annotations_paths.append(filtered_annotations_path)
    return filtered_annotations_paths

@click.command()
@click.argument('projects_uri')
@click.argument('output_zip_uri')
@click.argument('label_map_uri')
@click.option('--chip-size', default=300, help='Height and width of each chip')
@click.option('--num-neg-chips', default=None, type=float,
              help='Number of chips without objects to generate per image')
@click.option('--max-attempts', default=None, type=float,
              help='Maximum num of random windows to try per image when ' +
                   'generating negative chips.')
@click.option('--channel-order', nargs=3, type=int, default=default_channel_order,
              help='Indices of the RGB channels')
@click.option('--debug', is_flag=True,
              help='Generate debug plots that contain bounding boxes')
@click.option('--min-area', default=0.0,
              help='Minimum allowed area of objects in meters^2')
@click.option('--single-label', help='The label to convert all labels to')
@click.option('--no-partial', is_flag=True, help='Whether to include boxes for ' +
              'partially visible objects')
@click.option('--redact-partial', is_flag=True, help='Whether to black out ' +
              'partially visible objects')
@click.option('--save-temp', is_flag=True)
def prep_train_data(projects_uri, output_zip_uri, label_map_uri, chip_size,
                    num_neg_chips, max_attempts, channel_order, debug,
                    min_area, single_label, no_partial, redact_partial,
                    save_temp):
    """Generate training chips and TFRecord for set of projects.

    Given a set of projects (each a set of images and a GeoJSON file with
    labels), this generates training chips, a TFRecord, and zips them.

    Args:
        projects_uri: JSON file listing projects
            (each a list of images and an annotation file)
        output_zip_uri: zip file that will contain the training data
    """
    prefix = temp_root_dir
    temp_dir = join(prefix, 'prep-train-data') if save_temp else None
    with MyTemporaryDirectory(temp_dir, prefix) as temp_dir:
        if num_neg_chips is not None and max_attempts is None:
            max_attempts = 10 * num_neg_chips

        projects_path = download_if_needed(projects_uri, temp_dir)
        project_ids, image_paths_list, annotations_paths = \
            load_projects(projects_path, temp_dir)
        annotations_paths = filter_annotations(
            annotations_paths, temp_dir, min_area, single_label)

        output_zip_path = get_local_path(output_zip_uri, temp_dir)
        output_zip_dir = splitext(output_zip_path)[0]
        make_dir(output_zip_dir, check_empty=True)

        label_map_path = get_local_path(label_map_uri, temp_dir)
        make_dir(label_map_path, use_dirname=True)
        _make_label_map(annotations_paths, label_map_path)

        train_chip_dir = join(temp_dir, 'train-chips')
        chip_dirs = []
        chip_label_paths = []
        for project_id, image_paths, annotations_path in \
                zip(project_ids, image_paths_list, annotations_paths):
            chip_dir = join(train_chip_dir, str(project_id))
            chip_dirs.append(chip_dir)
            chip_label_path = join(train_chip_dir, '{}.csv'.format(project_id))
            chip_label_paths.append(chip_label_path)

            _make_train_chips(image_paths, annotations_path, chip_dir,
                              chip_label_path, label_map_path, chip_size,
                              num_neg_chips, max_attempts, no_partial,
                              redact_partial, channel_order, save_temp)

        _make_tf_record(label_map_path, chip_dirs, chip_label_paths,
                        output_zip_dir, debug)

        # Copy label map so it's included in the zip file for convenience.
        label_map_copy_path = join(output_zip_dir, 'label-map.pbtxt')
        shutil.copyfile(label_map_path, label_map_copy_path)
        shutil.make_archive(output_zip_dir, 'zip', output_zip_dir)

        upload_if_needed(output_zip_path, output_zip_uri)
        upload_if_needed(label_map_path, label_map_uri)

if __name__ == '__main__':
    prep_train_data()


from tkinter import *
from tk import *
import time
import random
import sys

class S:
	pass

def paths(path):
	if path == 1:
		return [0,25,50,75,100,125,150,175,200,200],[250,225,200,175,150,125,100,75,50,50],[0,0,1,0,0,1,0,0,0,1]
	elif path == 2:
		return [500-0,500-25,500-50,500-75,500-100,500-125,500-150,500-175,500-200,500-200],[250,225,200,175,150,125,100,75,50,50],[0,0,1,0,0,1,0,0,0,1]
	elif path==3:
		return [0,25,50,75,100,125,150,175,200,225],[250,250,250,250,250,250,250,250,250,250],[1,0,0,1,0,0,0,1,0,0]
	elif path==4:
		return [500-0,500-25,500-50,500-75,500-100,500-125,500-150,500-175,500-200,500-225],[250,250,250,250,250,250,250,250,250,250],[1,0,0,1,0,0,0,1,0,0]
	elif path==5:
		return [250+0,25+250,50+250,75+250,100+250,125+250,150+250,400,400,400],[275,250,250,250,250,250,225,200,175,125],[0,1,0,1,1,0,0,0,0,0]
	elif path==6:
		return [500-(250+0),500-(25+250),500-(50+250),500-(75+250),500-(100+250),500-(125+250),500-(150+250),100,100,100],[225,250,250,250,250,250,225,200,175,125],[0,1,0,1,1,0,0,0,0,0]
	elif path==7:
		return [200,200,200,200,200,150,125,100,75,75]
	elif path==8:
		return [200,200,200,200,200,200,200,200,175,175],[50,75,100,125,150,175,200,225,250,275],[0,1,0,1,0,0,1,0,1,0]
	elif path==9:
		return [500-200,500-200,500-200,500-200,500-200,500-200,500-200,500-200,500-175,500-175],[50,75,100,125,150,175,200,225,250,275],[0,1,0,1,0,0,1,0,1,0]
	elif path==10:
		return [150,150,150,150,150,150,150,150,150,150],[275,300,325,350,375,400,425,450,475,525],[1,0,1,0,0,0,0,1,0,0]
	elif path==11:
		return [350,350,350,350,350,350,350,350,350,350],[275,300,325,350,375,400,425,450,475,525],[1,0,1,0,0,0,0,1,0,0]
	elif path==12:
		return [375,350,325,350,375,400,425,450,475,500],[150,175,200,225,250,275,300,325,350,375],[0,0,0,1,0,1,0,0,1,0]
	elif path==13:
		return [500-375,500-350,500-325,500-350,500-375,500-400,500-425,500-450,500-475,500-525],[150,175,200,225,250,275,300,325,350,375],[0,0,0,1,0,1,0,0,1,0]
	else :
		return None

def move_player(event):
	if p.dead==False:
		if event.keysym == 'Left':
			if p.pos > 0:
				canvas.move(p.player_ship1,-25,0)
				canvas.move(p.player_ship2,-25,0)
				canvas.move(p.player_ship3,-25,0)
				canvas.move(p.player_ship4,-25,0)
				p.pos-=25
				p.pos_side=list(range(p.pos+5,p.pos+55))
		else:
			if p.pos < 450:
				canvas.move(p.player_ship1,25,0)
				canvas.move(p.player_ship2,25,0)
				canvas.move(p.player_ship3,25,0)
				canvas.move(p.player_ship4,25,0)
				p.pos+=25
				p.pos_side=list(range(p.pos+5,p.pos+55))

def fireshot(event):
	p.xshots.append(p.pos)
	p.yshots.append(500)

def spriteshot(x,y):
	Sprite.s_shots_x.append(x)
	Sprite.s_shots_y.append(y)

class player:
	def __init__(self):
		canvas.bind_all('<KeyPress-Left>',move_player)
		canvas.bind_all('<KeyPress-Right>',move_player)
		canvas.bind_all('<KeyPress-Up>',fireshot)
		self.dead=False
		self.pos=200
		self.lives=1
		global player_ship
		self.player_ship1=canvas.create_rectangle(5+self.pos,450,5+self.pos+12,500,outline='blue',fill='blue')
		self.player_ship2=canvas.create_rectangle(5+self.pos+36,450,5+self.pos+50,500,outline='blue',fill='blue')
		self.player_ship3=canvas.create_rectangle(5+self.pos,485,5+self.pos+50,500,outline='blue',fill='blue')
		self.player_ship4=canvas.create_rectangle(5+self.pos+18,465,5+self.pos+30,500,outline='blue',fill='blue')
		self.pos_side=list(range(self.pos+5,self.pos+55))

	def drawplayer(self):
		global player_ship,round
		pos=self.pos
		if p.dead==False:
			p.player_ship1=canvas.create_rectangle(5+self.pos,450,5+self.pos+12,500,outline='blue',fill='blue')
			p.player_ship2=canvas.create_rectangle(5+self.pos+36,450,5+self.pos+50,500,outline='blue',fill='blue')
			p.player_ship3=canvas.create_rectangle(5+self.pos,485,5+self.pos+50,500,outline='blue',fill='blue')
			p.player_ship4=canvas.create_rectangle(5+self.pos+18,465,5+self.pos+30,500,outline='blue',fill='blue')
	def draw_explosion(self):
		global player_ship,canvas,round
		player_ship1=None
		player_ship2=None
		player_ship3=None
		player_ship4=None
		canvas.create_rectangle(5+self.pos,450,55+self.pos,500,outline='orange',fill='orange')
		canvas.create_polygon(5+self.pos,450,30+self.pos,450,5+self.pos+12,425,outline='orange',fill='orange')
		canvas.create_polygon(5+self.pos+25,450,25+self.pos+30,450,5+self.pos+25+12,425,outline='orange',fill='orange')
		canvas.create_polygon(5+self.pos,450,5+self.pos,480,5+self.pos-25,465,outline='orange',fill='orange')
		canvas.create_polygon(5+self.pos,450+25,5+self.pos,480+25,5+self.pos-25,465+25,outline='orange',fill='orange')
		canvas.create_polygon(5+self.pos+50,450,5+self.pos+50,480,5+self.pos+25+50,465,outline='orange',fill='orange')
		canvas.create_polygon(5+self.pos+50,450+25,5+self.pos+50,480+25,5+self.pos+25+50,465+25,outline='orange',fill='orange')
		canvas.create_polygon(5+self.pos,450+50,30+self.pos,450+50,5+self.pos+12,425+50,outline='orange',fill='orange')
		canvas.create_polygon(5+self.pos+25,450+50,30+self.pos+25,450+50,5+self.pos+12+25,425+50,outline='orange',fill='orange')

class sprite:
	def __init__(self):
		self.path=None
		self.x_coords=None
		self.y_coords=None
		self.begin_time=None
		self.dead=False
		self.died=False
		self.marker=False
	def draw(self):
		global canvas,round
		if self.path!=None:
			if self.path==0:
#				canvas.create_rectangle(self.x_pos,self.y_pos,self.x_pos+5,self.y_pos+25,outline='red',fill='red')
#				canvas.create_rectangle(self.x_pos+20,self.y_pos,self.x_pos+25,self.y_pos+25,outline='red',fill='red')
#				canvas.create_rectangle(self.x_pos+10,self.y_pos+5,self.x_pos+15,self.y_pos+20,outline='red',fill='red')
#				canvas.create_rectangle(self.x_pos+5,self.y_pos+10,self.x_pos+10,self.y_pos+15,outline='red',fill='red')
#				canvas.create_rectangle(self.x_pos+15,self.y_pos+10,self.x_pos+20,self.y_pos+15,outline='red',fill='red')
#				canvas.create_rectangle(self.x_pos+15,self.y_pos+10,self.x_pos+20,self.y_pos+15,outline='red',fill='red')
				canvas.create_rectangle(self.x_pos,self.y_pos,self.x_pos+5,self.y_pos+25,outline='red',fill='red')
				canvas.create_rectangle(self.x_pos+20,self.y_pos,self.x_pos+25,self.y_pos+25,outline='red',fill='red')
				canvas.create_rectangle(self.x_pos+10,self.y_pos+5,self.x_pos+15,self.y_pos+20,outline='red',fill='red')
				canvas.create_rectangle(self.x_pos+5,self.y_pos+10,self.x_pos+10,self.y_pos+15,outline='red',fill='red')
				canvas.create_rectangle(self.x_pos+15,self.y_pos+10,self.x_pos+20,self.y_pos+15,outline='red',fill='red')
			else:
				inc=(round-self.begin_time)
#				print(inc)
#				print(inc>=len(self.shot_pos)-1)
				if inc > len(self.shot_pos)-1:
					self.path=0
					canvas.create_rectangle(self.x_pos,self.y_pos,self.x_pos+5,self.y_pos+25,outline='red',fill='red')
					canvas.create_rectangle(self.x_pos+20,self.y_pos,self.x_pos+25,self.y_pos+25,outline='red',fill='red')
					canvas.create_rectangle(self.x_pos+10,self.y_pos+5,self.x_pos+15,self.y_pos+20,outline='red',fill='red')
					canvas.create_rectangle(self.x_pos+5,self.y_pos+10,self.x_pos+10,self.y_pos+15,outline='red',fill='red')
					canvas.create_rectangle(self.x_pos+15,self.y_pos+10,self.x_pos+20,self.y_pos+15,outline='red',fill='red')
#					canvas.create_rectangle(self.x_pos,self.y_pos,self.x_pos+5,self.y_pos+25,outline='red',fill='red')
#					canvas.create_rectangle(self.x_pos+20,self.y_pos,self.x_pos+25,self.y_pos+25,outline='red',fill='red')
#					canvas.create_rectangle(self.x_pos+5,self.y_pos+10,self.x_pos+10,self.y_pos+15,outline='red',fill='red')
#					canvas.create_rectangle(self.x_pos+5+10,self.y_pos+10,self.x_pos+10+10,self.y_pos+15,outline='red',fill='red')
#					canvas.create_rectangle(self.x_pos+15,self.y_pos+10,self.x_pos+20,self.y_pos+15,outline='red',fill='red')
				else:
					self.x_pos=self.x_coords[inc]
					self.y_pos=self.y_coords[inc]
					canvas.create_rectangle(self.x_pos,self.y_pos,self.x_pos+5,self.y_pos+25,outline='red',fill='red')
					canvas.create_rectangle(self.x_pos+20,self.y_pos,self.x_pos+25,self.y_pos+25,outline='red',fill='red')
					canvas.create_rectangle(self.x_pos+10,self.y_pos+5,self.x_pos+15,self.y_pos+20,outline='red',fill='red')
					canvas.create_rectangle(self.x_pos+5,self.y_pos+10,self.x_pos+10,self.y_pos+15,outline='red',fill='red')
					canvas.create_rectangle(self.x_pos+15,self.y_pos+10,self.x_pos+20,self.y_pos+15,outline='red',fill='red')
					self.pos_side=list(range(self.x_pos-25,self.x_pos))
					if self.shot_pos[inc] == 1:
						spriteshot(self.x_pos,self.y_pos)
						canvas.create_rectangle(self.x_pos+10,self.y_pos+20,self.x_pos+15,self.y_pos+25,outline='orange',fill='orange')
	def explode(self):
		canvas.create_rectangle(self.x_pos,self.y_pos,self.x_pos+25,self.y_pos+25,fill='orange',outline='orange')
	def enter(self,path):
		global canvas,round
		self.path=path
		self.x_coords,self.y_coords,self.shot_pos=paths(self.path)
		if len(self.x_coords)!=len(self.y_coords):
			raise TypeError
		if len(self.shot_pos)!=len(self.y_coords):
			raise TypeError
		self.begin_time=round
		self.x_pos=self.x_coords[0]
		self.y_pos=self.x_coords[0]
		self.pos_side=list(range(self.x_coords[0]-25,self.x_coords[0]))

def check_collisions():
	global Sprites
	for s in Sprites:
		if s.dead==False:
			for shot in range(len(p.xshots)):
				if p.xshots[shot] in s.pos_side:
					shotrange=list(range(p.yshots[shot],p.yshots[shot]+100))
					if s.y_pos+25 in shotrange or s.y_pos in shotrange:
						s.path=None
						s.dead=True
	for shot in range(len(Sprite.s_shots_x)):
		if Sprite.s_shots_x[shot] in p.pos_side:
			shotrange=list(range(Sprite.s_shots_y[shot],Sprite.s_shots_y[shot]+100))
			if 450 in shotrange or 500 in shotrange:
				p.lives-=1
				if p.lives==0:
#					p.dead=True
					pass

def printshots():
	global canvas,p
	if len(p.xshots) > 0 and len(p.yshots) > 0:
		for i in range(len(p.yshots)):
			if p.yshots[i] > 0:
				p.yshots[i]-=100
				canvas.create_line(p.xshots[i]+30,p.yshots[i]+10,p.xshots[i]+30,p.yshots[i]+20,fill='orange')
	ct=0
	if len(Sprite.s_shots_x) > 0 and len(Sprite.s_shots_y) > 0:
		r=len(Sprite.s_shots_x)
		for i in range(r):
			if len(Sprite.s_shots_x) > 0 and len(Sprite.s_shots_y) > 0:
				j=i-ct
				if Sprite.s_shots_y[j] < 500:
					Sprite.s_shots_y[j]+=100
					canvas.create_line(Sprite.s_shots_x[j]-30,Sprite.s_shots_y[j]-10,Sprite.s_shots_x[j]-30,Sprite.s_shots_y[j]-20,fill='orange')
				else:
					ct+=1
					del Sprite.s_shots_x[j]
					del Sprite.s_shots_y[j]

def update_sprites():
	global round,Sprites
	if round==0:
		if Sprites[0].dead==False:
			Sprites[0].enter(1)
		if Sprites[1].dead==False:
			Sprites[1].enter(2)
	elif round==5:
		if Sprites[2].dead==False:
			Sprites[2].enter(3)
		if Sprites[3].dead==False:
			Sprites[3].enter(4)
	elif round==10:
		if Sprites[2].dead==False:
			Sprites[2].enter(5)
		if Sprites[3].dead==False:
			Sprites[3].enter(6)
		if Sprites[4].dead==False:
			Sprites[4].enter(1)
		if Sprites[5].dead==False:
			Sprites[5].enter(2)
	elif round==15:
		if Sprites[0].dead==False:
			Sprites[0].enter(8)
		if Sprites[1].dead==False:
			Sprites[1].enter(9)
	elif round==20:
		if Sprites[0].dead==False:
			Sprites[0].enter(10)
		if Sprites[1].dead==False:
			Sprites[1].enter(11)
	elif round==25:
		if Sprites[2].dead==False:
			Sprites[2].enter(12)
		if Sprites[3].dead==False:
			Sprites[3].enter(13)
		if Sprites[0].dead==False:
			Sprites[0].enter(1)
		if Sprites[1].dead==False:
			Sprites[1].enter(2)
	for s in Sprites:
		if s.dead==False:
			s.draw()
		else:
			if s.marker==False:
				s.explode()
				s.marker=True

def update_vals():
	global tk,canvas,p,ctr,round,end,Sprites,died,s_count,mark
	round+=1
	val=False
	for s in Sprites:
		if s.marker==False:
			val=True
	if mark==False:
		canvas.delete('all')
		bkrnd()
		canvas.create_text(250,250,text='Game Over',font=('Helvetica',30),fill='blue')
		canvas.create_text(250,300,text='You Win!',font=('Helvetica',30),fill='blue')
		tk.update_idletasks()
		tk.update()
		time.sleep(1)
		tk.destroy()
		s_count=[]
		try:
			sys.exit()
		except SystemExit:
			pass
		return True
	if died==True:
		canvas.delete('all')
		bkrnd()
		canvas.create_text(250,250,text='Game Over',font=('Helvetica',30),fill='red')
		canvas.create_text(250,300,text='You Lose',font=('Helvetica',30),fill='red')
		tk.update_idletasks()
		tk.update()
		time.sleep(1)
		tk.destroy()
		try:
			sys.exit()
		except SystemExit:
			pass
		return True
	if val==False:
		mark=False
	if p.dead==True:
		died=True

def bkrnd():
	global stars_x,stars_y,pts
	canvas.delete('all')
	canvas.create_rectangle(0,0,600,600,outline='black',fill='black')
	i=0
	for j in range(len(stars_x)):
		canvas.create_rectangle(stars_x[i],stars_y[i],stars_x[i]+2,stars_y[i]+2,fill='white')
		i+=1
	canvas.create_text(100,25,text='	GalactaPi		score : '+str(pts),font=('Helvetica',15),fill='blue')
	canvas.create_oval(236,369,246-5,379-5,fill='#ffd800')
	canvas.create_line(232,369,246,374,fill='#ffd800')
	canvas.create_oval(236-150,369+45,246-5-150,379-5+45,fill='#ffd800')
	canvas.create_line(232-150,369+45,246-150,374+45,fill='#ffd800')
	canvas.create_oval(200,150,215,165,fill='blue')

def update():
	global tk,canvas,p,ctr,round,end,Sprites,died,s_count,pts
	for s in Sprites:
		if s.dead==True:
			if s.died==False:
				s.died=True
				pts+=1
	bkrnd()
	p.drawplayer()
	update_sprites()
	check_collisions()
	if p.dead==True:
		p.draw_explosion()
	printshots()
	var=update_vals()
	if var==True:
		return
	if ctr < 100:
		try:
			ctr+=1
			tk.update_idletasks()
			tk.update()
			tk.after(250,update)
			tk.mainloop()
		except KeyboardInterrupt:
			end=True

def begin():
	global tk,canvas,p,ctr,round,Sprites,died,s_count,stars_x,stars_y,end,pts,mark,Sprite
	mark=True
	died=False
	ctr=0
	tk,canvas=setup(500,500)
	p=player()
	s1=sprite()
	s2=sprite()
	s3=sprite()
	s4=sprite()
	s5=sprite()
	s6=sprite()
	Sprites=[s1,s2,s3,s4,s5,s6]
	pts=0
	s_count=len(Sprites)
	round=0
	tk.title("GalactaPi")
	tk.resizable(0,0)
	tk.wm_attributes("-topmost",1)
	p.xshots=[]
	p.yshots=[]
	Sprite=S()
	Sprite.s_shots_x=[]
	Sprite.s_shots_y=[]
	stars_x=[]
	stars_y=[]
	random.seed(26)
	for s in Sprites:
		s.pos_side=[0,1,2,3,4,5]
	for i in range(30):
		stars_x.append(random.randint(5,495))
		stars_y.append(random.randint(5,495))
	end=False
	canvas.bind_all('<KeyPress-Return>',end_menu)
	menu()
	update()
	end=False
	try:
		while end==False:
			if p.dead==True or s_count==[]:
				return
			if ctr==50:
				ctr=0
				tk.after(250,update)
	except KeyboardInterrupt:
		end=True

def end_menu(event):
	global end
	end=True

def menu():
	global end,tk
	bkrnd()
	canvas.create_text(250,250,text='GalactaPi v1.3',font=('Helvetica',30),fill='blue')
	canvas.create_text(250,300,text='Press Enter To Play',font=('Helvetica',30),fill='blue')
	if end==False:
		tk.update_idletasks()
		tk.update()
		time.sleep(0.25)
		var=menu()
		if var==True:
			return True
	else:
		return True

if __name__ == '__main__':
	global canvas,end
	end=False
	begin()


# -*- coding: utf-8 -*-
from pirates import *

def asteroid_behavior(game, pirate, next_location):
    """
    the function controls the asteroid behavior of the pirate.
    strategy: checks if thepirate is goiing to collide with an asteroid. if so,
    checks if the pirate should and can push the asteroid and to wat direction or let it pass.

    :param game: the current game state, pirate: a pirate, next_location: the pirates next location
    :type game: PirateGame, pirate: Pirate, next_location: Location
    """
    acted = False  #
    if len(game.get_living_asteroids()) >= 1:
        for asteroid in game.get_living_asteroids():
            a_is_close = (pirate.distance(asteroid) <= pirate.push_range)
            p_can_push = pirate.can_push(asteroid)
            #a_is_in_direction =
            if a_is_close and p_can_push:
                pirate.push(asteroid, (asteroid.direction * (-1)))  # for now assume there are no friendly pirates there
            elif a_is_close and pirate.push_reload_turns > 0:
            # if (asteroid.location + asteroid.direction) == pirate_loc_next_turn(game, pirate)
            # #<<func not yet written>>, maybe not needed
                # check if asteroid will hit pirate in 5 turns
                will_hit = False
                for t in asteroid_linear(game, asteroid, 5):
                    if t is pirate.location:
                        will_hit = True
                if will_hit: # if it will not hit in 5:
                    if pirate.location.row > 3200:
                        y1 = 400
                    else:
                            y1 = 6000
                            if pirate.location.col > 3200:
                                x1 = 400
                            else:
                                x1 = 6000
                    pirate.sail(asteroid.location.add(x1, y1))
            else:
                pass  # not sure if other situation is needed

def asteroid_linear(game, asteroid, n, turns=[]):
    """
    The function creates a list of next n locations the asteroid will be at.
    It assumes it won't be pushed, and won't be destroyed during n turns.

    :param game: the current game state, asteroid: the asteroid, n: number of turns to check, turns: empty list
    :type game: PirateGame, asteroid: Asteroid, n: int
    """
    if n > 1:
        return turns.append(asteroid_linear(game, asteroid, (n-1)))
    return turns.append(asteroid.location + asteroid.direction)

def open_capsule(game, capsules_list):
    """
    the function finds whether there is an unused capsule. if so , it returns the capsule, else return None.

    :param game: the current game state, capsules_list: a lsit with the current state of each capsule
    :type game: PirateGame, capsules_list: list of Bool
    """
    for capsule in game.get_my_capsules():
        if not capsules_list[game.get_my_capsules().index(capsule)]:
            return capsule
    return None

def can_enemys_push(game, pirate):
    """
    the function gets a pirate and return true if there are 2 enemy ship that can push the pirate.

    :param game: a game,  pirate: a pirate
    :type game: PirateGame, pirate: Pirate
    """
    count = 0
    for enemy in game.get_enemy_living_pirates():
        if enemy.can_push(pirate) or enemy.distance(pirate) < 350:
            count += 1
        if count == 2:
            return True
    return False

def get_next_location(pirate, destination):
    """
    the function gets a pirate and its sailing destination and return its upcoming location

    :param pirate: a pirate,  destination: a location
    :type pirate: Pirate, destination: Location
    """
    pass

def capsule_holder(pirate1, pirate2):
    """
    the function gets a pair of pirates and return the following list:
    [holding capsule, not holding capsule]

    :param pirate1: a pirate,  pirate2: a pirate
    :type pirate1: Pirate, pirate2: Pirate
    :return list: the desirable mine
    :rtype list: list of Pirate
    """
    if pirate1.has_capsule():
        return [pirate1, pirate2]
    else:
        return [pirate2, pirate1]

def desirable_capsule(game):
    """
    the function returns the mine which is located closest to a mothership, and has a capsule

    :param game: the current game state
    :type game: PirateGame
    :return mine: the desirable mine
    :rtype mine: Location
    """

    minimal_distance = 999999
    minimal_capsule = None
    for capsule in game.get_my_capsules():
        if capsule.holder is None:
            for mothership in game.get_my_motherships():
                if mothership.distance(capsule.initial_location) < minimal_distance:
                    minimal_capsule = capsule
                    minimal_distance = mothership.distance(capsule.initial_location)
    return minimal_capsule

def optimal_pushing_direction(pirate, game):
    """
    the function finds the closest wall to the pirate, thus the best direction to push

    :param pirate: a pirate, game: the current game state
    :type pirate: Pirate, game: PirateGame
    """
    pirate_location = pirate.get_location()
    walls = [Location(0, pirate_location.col), Location(game.rows, pirate_location.col),
             Location(pirate_location.row, game.cols),
             Location(pirate_location.row, 0)]
    minimal_wall = None
    minimal_distance = 999999
    for wall in walls:
        if pirate_location.distance(wall) < minimal_distance:
            minimal_wall = walls.index(wall)
            minimal_distance = pirate_location.distance(wall)
    # in order to push out of the board
    if minimal_wall == 0:
        walls[minimal_wall].row -= 1000
    elif minimal_wall == 1:
        walls[minimal_wall].row += 1000
    elif minimal_wall == 2:
        walls[minimal_wall].col += 1000
    elif minimal_wall == 3:
        walls[minimal_wall].col -= 1000

    return walls[minimal_wall]

def desirable_enemy(game, mothership):
    """
    the function return the enemy's capsule which is in the closest range to the specified mothership

    :param game: the current game state, mothership: the current mothership
    :type game: PirateGame, mothership: Mothership
    """
    minimal_distance = 999999
    minimal_capsule = None
    for capsule in game.get_enemy_capsules():
        if capsule.distance(mothership.get_location()) < minimal_distance:
            minimal_capsule = capsule
            minimal_distance = capsule.distance(mothership.get_location())
    return minimal_capsule

def desirable_enemy_mothership(game):
    """
    the function returns the enemy mothership closest to a mine, best to defend on

    :param game: the current game state
    :type game: PirateGame
    :return mothership: the desirable mothership
    :rtype mothership: Mothership
    """

    minimal_distance = 999999
    minimal_mothership = None
    for mothership in game.get_enemy_motherships():
        for capsule in game.get_enemy_capsules():
            if mothership.distance(capsule.initial_location) < minimal_distance:
                minimal_mothership = mothership
                minimal_distance = mothership.distance(capsule.initial_location)
    return minimal_mothership

def desirable_mothership(game, pirate):
    """
    the function returns the  mothership closest to the pirate, best to sail to

    :param game: the current game state, pirate: a pirate
    :type game: PirateGame, pirate: Pirate
    :return mothership: the desirable mothership
    :rtype mothership: Mothership
    """
    minimal_distance = 999999
    minimal_mothership = None
    for mothership in game.get_my_motherships():
        if mothership.distance(pirate) < minimal_distance:
            minimal_mothership = mothership
            minimal_distance = mothership.distance(pirate)
    return minimal_mothership


"""
Provides functionality for starting process on the host operating system.
    Easily done without pymake3, but we want pymake3 to be dead simple to use,
so we include this functionality here.
"""

#---------------------------------------
# IMPORTS
#---------------------------------------

import subprocess

from pymake3.cli import main

#---------------------------------------
# FUNCTIONS
#---------------------------------------

def run_program(s, args=None):
    """
    Runs the specified program with the specified arguments.

    :param s:    Name of the program to run.
    :param args: Program arguments.

    :return: Exit code returned by the executed program.
    """
    if not args:
        args = []

    result = subprocess.call([s] + args)
    main.exit_code = result
    return result

def run(spec, mode="auto"):
    originalSpec = spec
    assert mode in ["auto", "exec", "split", "shell"]
    kwargs = {}
    if mode == "auto":
        if isinstance(spec, str):
            if '|' in spec or '>' in spec:
                mode = "shell"
            elif ' ' in spec:
                mode = "split"
            else:
                mode = "exec"
                spec = [spec]
        elif isinstance(spec, list):
            mode = "exec"
        else:
            raise Exception("Unable to auto determine mode for spec '%s'" % spec)

    if mode == "shell":
        kwargs['shell'] = True
    if mode == "split":
        spec = spec.split(' ')
    result = subprocess.call(spec, close_fds=True, **kwargs)
    if result != 0:
        raise Exception("Failed: run(%s)" % originalSpec)
    return True


from collections import defaultdict # defaultdict makes incrementing way nicer
import re

def word_count(phrase):
    word_dict = defaultdict(int)
    words_raw = re.split("(\\n)|[,]| |_|\t", phrase)
    words = [w.strip().strip("@^':.!%$&§").lower() for w in words_raw if w != '' and w != ' ' and w != '\n' and w!= None]
    print(words)
    for w in words:
        if w != '' and w != ' ' and w != '\n' and w!= None:
            word_dict[w] += 1

    return dict(word_dict)


import urllib.request
import os
import time
import requests
import re
from bs4 import BeautifulSoup
import  pymysql
import  pymysql.cursors

# 打开数据库连接
db = pymysql.connect("localhost","root","root","exe" )

# 使用 cursor() 方法创建一个游标对象 cursor
cursor = db.cursor()

# 使用 execute() 方法执行 SQL，如果表存在则删除
cursor.execute("DROP TABLE IF EXISTS exe")

# 使用预处理语句创建表
sql_creat = """CREATE TABLE EMPLOYEE (
         FIRST_NAME  CHAR(20) NOT NULL,
         LAST_NAME  CHAR(20),
         AGE INT,
         SEX CHAR(1),
         INCOME FLOAT )"""
cursor.execute(sql_creat)

# 关闭数据库连接

def open_url(url):
    # 根据当前URL创建请求包
    req = urllib.request.Request(url)
    # 添加头信息，伪装成浏览器访问
    req.add_header('User-Agent',
                   'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36')
    # 发起请求
    response = urllib.request.urlopen(req)
    # 返回请求到的HTML信息
    return response.read()

url = 'http://202.119.210.3:8080/opac/item.php?marc_no=0000435712'
html = open_url(url).decode('utf-8')
soup = BeautifulSoup(html)
tt = soup.find_all('dl')
result = {}
data = {}
data['type'] = re.findall('文献类型：(.*)浏览', html)
for ii in tt:
    result[ii.find('dt').text] = ii.find('dd').text

data['title'] = result.get('题名/责任者:')
data['publish'] = result.get('出版发行项:')
data['title1'] = result.get('其它题名:')
data['title2'] = result.get('并列正题名:')

data['flh'] = result.get('中图法分类号:')
data['page'] = result.get('载体形态项:')
data['add'] = result.get('责任者附注:')
data['subject'] = result.get('学科主题:')
data['abstract'] = result.get('豆瓣简介:')

#
# print(data)
# print(result)
# exit()

sql = 'insert into `urls`(`title`,`title1`,`title2`,`flh`,`publish`,`subject`,`page`,`type`,`add`,`abstract`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
cursor.execute(sql, data.values())

db.close()
print(result)
exit()


#!/usr/bin/env python 3.6
# w/ openCV3, version 3.4.2

import numpy as np
import cv2
from collections import deque
from checkWinner import check

def dist(a,b):
    return ((a[0]-b[0])**2+(a[1]-b[1])**2)**0.5

def fingerCursor(device):
    cap = cv2.VideoCapture(device)
    cap_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
    cap_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
    # print(cap_height,cap_width)
    out = cv2.VideoWriter('outpy.avi', cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'), 10, (1280, 720))

    ## gesture matching initialization
    gesture2 = cv2.imread('gesture2.png')
    gesture2 = cv2.cvtColor(gesture2, cv2.COLOR_BGR2GRAY)
    _, gesture2 , _ = cv2.findContours(gesture2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

    # skin color segmentation mask

    skin_min = np.array([0, 48, 80],np.uint8)  # HSV mask
    skin_max = np.array([20, 255, 255],np.uint8) # HSV mask

    ## trajectory drawing initialization
    des_1 = (540, 260)
    des_2 = (640, 260)
    des_3 = (740, 260)
    des_4 = (540, 360)
    des_5 = (640, 360)
    des_6 = (740, 360)
    des_7 = (540, 460)
    des_8 = (640, 460)
    des_9 = (740, 460)
    ori_1 = (540, 260)
    ori_2 = (640, 260)
    ori_3 = (740, 260)
    ori_4 = (540, 360)
    ori_5 = (640, 360)
    ori_6 = (740, 360)
    ori_7 = (540, 460)
    ori_8 = (640, 460)
    ori_9 = (740, 460)
    img_ori_position = deque([ori_1, ori_2, ori_3, ori_4, ori_5, ori_6, ori_7, ori_8, ori_9])
    img_ori_position_status = [-1, -1, -1, -1, -1, -1, -1, -1, -1]
    img_des_position = deque([des_1, des_2, des_3, des_4, des_5, des_6, des_7, des_8, des_9])
    img_des_position_status = [-1, -1, -1, -1, -1, -1, -1, -1, -1]
    topmost_last = ori_9    # initial position of finger cursor

    img = cv2.imread('IMG/logo.png')
    symbol = [1, 2, 1, 2, 1, 2, 1, 2, 1]
    currentsum = 0

    ## finger cursor position low_pass filter, keep robustness of moving object
    low_filter_size = 5
    # low_filter = deque([ori_9,ori_9,ori_9,ori_9,ori_9],low_filter_size )  # filter size is 5

    ## gesture_index low_pass filter
    gesture_filter_size = 5
    gesture_matching_filter = deque([0.0,0.0,0.0,0.0,0.0], gesture_filter_size )
    gesture_index_thres = 2.0

    ## color definition
    orange = (0,97,255)
    blue = (255,0,0)
    green = (0,255,0)
    red = (0,0,255)
    flag_1 = -1 # control if is gesture 2

    ## background segmentation

    # some kernels
    kernel_size = 5
    kernel1 = np.ones((kernel_size,kernel_size),np.float32)/kernel_size/kernel_size
    # kernel2 = np.ones((10,10), np.uint8)/100
    n = 0
    while(cap.isOpened()):
        flag_2 = -1  # control img_des_position_status
        if n < 10:
            n = n + 1
        # print(n)
        ## Capture frame-by-frame
        ret, frame_raw = cap.read()
        while not ret:
            ret,frame_raw = cap.read()
        frame_raw = cv2.flip(frame_raw,1)
        frame = frame_raw[:round(cap_height),:round(cap_width)]    # ROI of the image
        # cv2.imshow('raw_frame',frame)

        ## Color seperation and noise cancellation at HSV color space
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        mask = cv2.inRange(hsv, skin_min, skin_max)
        res = cv2.bitwise_and(hsv, hsv, mask= mask)
        res = cv2.erode(res, kernel1, iterations=1)
        res = cv2.dilate(res, kernel1, iterations=1)

        rgb = cv2.cvtColor(res, cv2.COLOR_HSV2BGR)
        # cv2.imshow('rgb_2',rgb)

        gray = cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)
        # cv2.imshow('gray',gray)
        # gray = cv2.filter2D(gray,-1,kernel2)    # hacky
        gray = cv2.GaussianBlur(gray, (11, 11), 0)
        # cv2.imshow('gray',gray)

        ## Background segmentation using motion detection (Optional)

        ## main function: find finger cursor position & draw trajectory
        im2, contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)    # find all contours in the image
        print(len(contours))
        if len(contours) !=0:
            contours.sort(key = cv2.contourArea,reverse = True)
            c = contours[0]  # find biggest contour in the image

            if True:
                if cv2.contourArea(c) > 1000:
                    topmost = tuple(c[c[:, :, 1].argmin()][0])  # consider the topmost point of the biggest contour as cursor
                    gesture_index = cv2.matchShapes(c, gesture2[0], 2, 0.0)

                    # obtain gesture matching index using gesture matching low_pass filter
                    gesture_matching_filter.append(gesture_index)
                    sum_gesture = 0
                    for i in gesture_matching_filter:
                        sum_gesture += i
                    gesture_index = sum_gesture / gesture_filter_size

                    dis_bound = 50
                    flag = -1
                    # decide which img is chosen
                    for i in range(len(img_ori_position)):
                        dis = dist(topmost, img_ori_position[i])
                        if (img_ori_position_status[i] == -1) and (dis < dis_bound):
                            print("nearest: ", i)
                            flag = i
                            patch_current = i
                            flag_1 = 0
                    if flag != -1:
                        topmost_last = img_ori_position[patch_current]
                        low_filter = deque([topmost_last, topmost_last, topmost_last, topmost_last, topmost_last], low_filter_size)
                        img_ori_position_status[patch_current] = patch_current

                    dist_pts = dist(topmost,topmost_last)  # calculate the distance of last cursor position and current cursor position

                    if dist_pts < 150:  # filter big position change of cursor
                        try:

                            low_filter.append(topmost)
                            sum_x = 0
                            sum_y = 0
                            for i in low_filter:
                                sum_x += i[0]
                                sum_y += i[1]
                            topmost = (sum_x//low_filter_size, sum_y//low_filter_size)

                            if gesture_index < gesture_index_thres:
                                print(gesture_index)
                                for i in range(len(img_des_position)):
                                    dis = dist(topmost,img_des_position[i])
                                    if (dis < dis_bound) and (img_des_position_status[i] == -1):
                                        img_des_position_status[i] = symbol[currentsum]
                                        currentsum = currentsum + 1
                                        flag_1 = 1

                            else:
                                pass
                            topmost_last = topmost  # update cursor position
                            # print(topmost_last)
                        except:
                            print('error')
                            pass

        # draw img_patches already in grid
        for i in range(len(img_des_position)):
            if img_des_position_status[i] != -1:
                img_temp = cv2.imread('IMG2/' + '00' + str(img_des_position_status[i]) + '.jpg')
                frame[int(img_des_position[i][1] - img_temp.shape[0]/2):int(img_des_position[i][1] + img_temp.shape[0]/2), \
                int(img_des_position[i][0] - img_temp.shape[0]/2):int(img_des_position[i][0] + img_temp.shape[0]/2)] = img_temp
        # print(img_des_position_status)
        total = 0
        for item in img_des_position_status:
            if item != 1:
                total = total + 1
            # if total < 3:
            #     flag_2 = 0
            if item == -1 or total<3:
                flag_2 = 0

        # if flag_2 == -1:
        if True:
            print(img_des_position_status)
            result, winner = check(img_des_position_status)

            if result == 1:
                # print winner
                if winner == 1:
                    cv2.putText(img=frame, text='O win!', org=(int(cap_width / 2 - 100), int(cap_height / 2)),
                            fontFace=cv2.FONT_HERSHEY_TRIPLEX, fontScale=3,
                            color=orange, thickness=5)
                else:
                    cv2.putText(img=frame, text='X win!', org=(int(cap_width / 2 - 100), int(cap_height / 2)),
                                fontFace=cv2.FONT_HERSHEY_TRIPLEX, fontScale=3,
                                color=orange, thickness=5)
            elif result == 0:

                cv2.putText(img=frame, text='DRAW!', org=(int(cap_width / 2 - 150), int(cap_height / 2)),
                            fontFace=cv2.FONT_HERSHEY_TRIPLEX, fontScale=3,
                            color=blue, thickness=5)

        if flag_1 == 1:
            cv2.circle(frame, topmost_last, 10, green, 3)

        if flag_2 == 0:
            cv2.line(frame, (490, 210), (790, 210), red, 1)
            cv2.line(frame, (490, 310), (790, 310), red, 1)
            cv2.line(frame, (490, 410), (790, 410), red, 1)
            cv2.line(frame, (490, 510), (790, 510), red, 1)
            cv2.line(frame, (490, 210), (490, 510), red, 1)
            cv2.line(frame, (590, 210), (590, 510), red, 1)
            cv2.line(frame, (690, 210), (690, 510), red, 1)
            cv2.line(frame, (790, 210), (790, 510), red, 1)

        ## Display the resulting frame

        # frame_raw = cv2.resize(frame_raw, (1024,768))
        # cv2.imshow('frame', frame_raw)
        out.write(frame)
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    ## When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    device = 0    # if device = 0, use the built-in computer camera
    fingerCursor(device)


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

# Time complexity: O()
# Space complexity: O()

class StreamChecker:

    def __init__(self, words: List[str]):
        self.trie = dict()
        self.waitlist = []
        # create a trie
        for word in words:
            temp_dict = self.trie
            for char in word:
                temp_dict = temp_dict.setdefault(char, dict())
            temp_dict['#'] = '#'

    def query(self, letter: str) -> bool:
        temp = []
        if letter in self.trie:
            temp.append(self.trie[letter])
        for item in self.waitlist:
            if letter in item:
                temp.append(item[letter])

        self.waitlist = temp
        return any('#' in item for item in self.waitlist)

# Your StreamChecker object will be instantiated and called as such:
# obj = StreamChecker(words)
# param_1 = obj.query(letter)