import math

#ruutvorrandi klass
class Quadratic_Equation:
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c
        #x1 on definisatsiooni give_x1() vastus
        self.x1 = self.give_x1()
        #x2 on definisatsiooni give_x2() vastus
        self.x2 = self.give_x2()

    #defineerib x1 leidimise
    def give_x1(self):
        #proovib leida x1
        try:
            under_square_root = (self.b**2) - (4*self.a*self.c)
            square_root = math.sqrt(under_square_root)
            numerator = -1*self.b + square_root
            denominator = 2*self.a
            #tagastab x1
            return numerator/denominator
        #errori korral tagastab "x1 lahend puudub"
        except (ValueError):
            return "x1 lahend puudub"

    #defineerib x2 leidimise
    def give_x2(self):
        #proovib leida x2
        try:
            under_square_root = (self.b**2) - (4*self.a*self.c)
            square_root = math.sqrt(under_square_root)
            numerator = -1*self.b - square_root
            denominator = 2*self.a
            #tagastab x2
            return numerator/denominator
        #errori korral tagastab "x2 lahend puudub"
        except (ValueError):
            return "x2 lahend puudub"

#kui see fail on pohiprogramm
if __name__ == "__main__":
    #algab while tsukkel, kusib a,b,c vaartusi ja arvutab nendega x1, x2
    while True:
        try:
            #kysib a vaartust
            a_value = int(input("Sisestage a vaartus: "))
            #kysib b vaartust
            b_value = int(input("Sisestage b vaartus: "))
            #kysib c vaartust
            c_value = int(input("Sisestage c vaartus: "))
            #muutuja mille vaartuseks on sisestatud numbritega x1 ja x2
            solve = Quadratic_Equation(a_value, b_value, c_value)
        #errori korral palub programm sisestada uued numbrid
        except(ValueError):
            print("Palun sisestage ta1isarv numbrites.")
            continue
        #kui numbrid on taisarvud, siis lopetab "break" while tsukkli
        break
    try:
        #x1 ja x2 vastused, umardab 2 kohta parast koma
        x1_value = round(solve.x1, 2)
        x2_value = round(solve.x2, 2)
        #naitab vastused
        print("x1 =",x1_value)
        print("x2 =",x2_value)
    #errori korral teatab, et x1 ja x2 ei saanud lahendada, eri ridadel
    except (TypeError):
            print(solve.x1, solve.x2, sep ="\n")


import sqlite3
from sqlite3 import Error

def create_connection(db):
    """ Connect to a SQLite database
    :param db: filename of database
    :return connection if no error, otherwise None"""
    try:
        conn = sqlite3.connect(db)
        return conn
    except Error as err:
        print(err)
    return None

def select_all_persons(conn):
    """Query all rows of person table
    :param conn: the connection object
    :return:
    """
    cur = conn.cursor()
    cur.execute("SELECT * FROM person")

    rows = cur.fetchall()

    return rows # return the rows

if __name__ == '__main__':
    conn = create_connection("pythonsqlite.db")
    with conn:
        rows = select_all_persons(conn)
        for row in rows:
            print(row)


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

from notes.views.base import BaseView
from notes.models import Note

class Index(BaseView):
    def get(self, request):
        self.request = request

        self.common_context(**{
            'base_title': 'Входящие',
            'page_title': 'Входящие',
            'active_menu': ''
        })

        self.context['notes'] = self.paginate(Note.objects.get_inbox_notes())

        return self.base_render('notes/notes/index.html')


import sys
import string
import random
import numpy as np

from scipy.stats import ttest_ind

def header_to_common(header, length = 8):
    random.seed(42)
    uppercase = string.ascii_uppercase
    numbers = list(map(lambda x: str(x), [i for i in range(10)]))
    common_header = ['label']
    for _ in range(len(header)):
        common_name = ''
        for i in range(length):
            if i < 2:
                common_name += random.choice(uppercase)
            else:
                common_name += random.choice(numbers)
        common_header.append(common_name)

    return common_header

if __name__ == '__main__':
    luminal_path = 'dataset.csv'
    reduced_path = 'reduced_dataset.csv'
    ALPHA = 0.05
    bonferroni = 1

    luminalA = []
    luminalB = []
    with open(luminal_path, 'r') as f:
        header = f.readline().split(',')[1:]
        bonferroni = len(header)
        for line in f:
            sample = list(map(lambda x: float(x), line.split(',')[1:]))  # each position in the sample correspond to a gene
            if line.startswith('Luminal A'):
                luminalA.append(sample)
            else:
                luminalB.append(sample)

    # Reformat dataset to put genes in the rows. lumnialA and luminalB will share same number of rows
    luminalA = np.transpose(luminalA)
    luminalB = np.transpose(luminalB)

    print(f'Luminal A shape {luminalA.shape}')
    print(f'Luminal B shape {luminalA.shape}')
    print(f'Header length {len(header)}')

    # Create a reduced Datasets
    reducedA = []
    reducedB = []
    reduced_header = []
    for i in range(len(luminalA)):
        _, p_val = ttest_ind(luminalA[i], luminalB[i])
        if p_val > ALPHA/bonferroni:
            reducedA.append(luminalA[i])
            reducedB.append(luminalB[i])
            reduced_header.append(header[i])

    reducedA = np.transpose(reducedA)
    reducedB = np.transpose(reducedB)
    with open(reduced_path, 'w+') as f:
        common_header = header_to_common(header)
        f.write(','.join(common_header) + '\n')
        for sample in reducedA:
            sample = list(map(lambda el: str(el), sample))
            line = 'Luminal A,' + ','.join(sample) + '\n'
            f.write(line)
        for sample in reducedB:
            sample = list(map(lambda el: str(el), sample))
            line = 'Luminal B,' + ','.join(sample) + '\n'
            f.write(line)


import numpy as np

def getPerformance(res_x,res_y,ori_x,ori_y):

    wc = 0
    for i in range(len(res_x)):
        r = 0
        if res_x[i] in ori_x:
            index = ori_x.index(res_x[i])
            r = res_y[i] * (ori_y[index]+1)/(sum(ori_y)+17)
        else:
            r = res_y[i] * 1/(sum(ori_y)+17)
        wc += r

    common = np.intersect1d(res_x,ori_x)
    total = np.unique(np.append(res_x,ori_x))
    cover = len(common)/len(total)
    cc = wc*cover
    return wc,cover,cc

if __name__ == "__main__":
    # 改进前r
    # res_x = ['Action', 'Drama', 'War', "Children's", 'Comedy', 'Romance']
    # res_y = [0.1, 1,  0.1, 0.1, 0.3, 0.2]

    # 加入r+p
    # res_x = ['Comedy', 'War', 'Animation', "Children's", 'Adventure', 'Fantasy', 'Sci-Fi', 'Drama', 'Thriller', 'Action', 'Horror', 'Musical', 'Romance']
    # res_y = [1,0.16666667, 0.16666667, 0.33333333, 0.33333333, 0.16666667,0.33333333, 0.16666667, 0.33333333, 0.16666667, 0.33333333, 0.16666667,0.33333333]

    # 加入r+d
    res_x = ['Drama', 'War', 'Action', 'Adventure', 'Comedy', 'Crime', 'Thriller', 'Documentary', 'Musical']
    res_y = [1, 0.25, 0.375, 0.125, 0.25, 0.25, 0.125, 0.125, 0.125]

    # 加入r+p+d
    # res_x = ['Adventure', "Children's", 'Comedy', 'Fantasy', 'Sci-Fi', 'Musical', 'Romance', 'Animation', 'Drama', 'Action', 'Thriller', 'Horror']
    # res_y = [0.75, 0.75, 1, 0.25, 0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5,  0.5 ]

    ori_x = ['Drama', 'Animation', "Children's", 'Comedy', 'Action', 'Adventure', 'Musical', 'Fantasy', 'Sci-Fi', 'War',
             'Romance', 'Thriller', 'Crime']
    ori_y = [93, 62, 79, 49, 18, 17, 54, 12, 13, 10, 13, 8, 8]

    print(getPerformance(res_x,res_y,ori_x,ori_y))


'''
   @Author Ci Xin
   @E-mail sssrrr879@126.com
   @Phone 15849311404
   @Desc 主要做电影天堂数据的准备
   @Date Create on 2018/11/4/004 20:24
'''

import datetime

import mysqldat.save_to_mysql as ms
from mysqldat.pre_sql import sql as psql

class dytt:
    # 初始化变量
    def __init__(self, classic, home, m_time, title, desc, hot, downloadurl):
        self.__classic = classic
        self.__home = home
        self.__movie_time = m_time
        self.__title = title
        self.__desc = desc
        self.__hot = hot
        self.__download = downloadurl

    def save(self):
        '''
        保存数据到数据库
        :return:
        '''
        d = datetime.datetime.now().strftime('%Y-%m-%d')  # '%Y-%m-%d %H:%M:%S'
        t = datetime.datetime.now().strftime('%H:%M:%S')
        savesql = psql['c_dytt_movies'].format(self.__home, self.__classic, self.__title, self.__desc,
                                               self.__movie_time, self.__hot, d, t, d, t)

        downsql = psql['c_dytt_download'].format('lastid', '{durl}', d, t, d, t)

        ## 这里的参数 'dytt ' 是获取连接数据库需要的参数
        mo = ms.Mysqlopertion('dytt')

        ## 这里做存在性校验
        if mo.check_exists(self.__title) == False:

            ##  开始保存视频
            mo.save_movie(sql=savesql, download=self.__download, dsql=downsql)
            # print('视频：{0}，插入成功'.format(self.__title))
        else:
            print('【爬虫抓取日志:{0}】：视频：{1}已存在，不需插入！'.format(datetime.datetime.now(), self.__title))


# Copyright (c) 2019 Red Hat, Inc.
#
# All Rights Reserved.
#
#    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.
from __future__ import absolute_import

import typing

import netaddr
from oslo_log import log

import tobiko
from tobiko.shell import sh

LOG = log.getLogger(__name__)

class IpError(tobiko.TobikoException):
    message = ("Unable to get IP addresses from host "
               "(exit_status={exit_status!r}): {error!s}")

INETS = {
    4: ['inet'],
    6: ['inet6'],
    None: ['inet', 'inet6']
}

def list_ip_addresses(ip_version: int = None,
                      device: str = None,
                      scope: str = None,
                      **execute_params) -> \
        tobiko.Selection[netaddr.IPAddress]:
    inets = INETS.get(ip_version)
    if inets is None:
        error = "invalid IP version: {!r}".format(ip_version)
        raise IpError(error=error)

    command = ['-o', 'address', 'list']
    if device is not None:
        tobiko.check_valid_type(device, str)
        command.append(device)
    output = execute_ip(command, **execute_params)

    ips: tobiko.Selection[netaddr.IPAddress] = tobiko.Selection()
    if output:
        for line in output.splitlines():
            fields = line.strip().split()
            inet = fields[2]
            if inet not in inets:
                continue  # List only address of selected IP version

            if scope:
                try:
                    scope_index = fields.index('scope')
                    if fields[scope_index + 1] != scope:
                        continue
                except (IndexError, ValueError):
                    continue

            address, _ = parse_ip_address(fields[3])
            ips.append(address)
    return ips

def find_ip_address(ip_version: int = None,
                    device: str = None,
                    scope: str = None,
                    unique: bool = False,
                    **execute_params) -> \
        netaddr.IPAddress:
    ips = list_ip_addresses(ip_version=ip_version,
                            device=device,
                            scope=scope,
                            **execute_params)
    if unique:
        return ips.unique
    else:
        return ips.first

def parse_ip_address(text: str) -> typing.Tuple[netaddr.IPAddress, int]:
    if '/' in text:
        # Remove netmask prefix length
        address, prefix_len_text = text.split('/', 1)
        prefix_len = int(prefix_len_text)
    else:
        prefix_len = 0
    return netaddr.IPAddress(address), prefix_len

def list_network_namespaces(**execute_params) -> tobiko.Selection[str]:
    namespaces = tobiko.Selection[str]()
    output = execute_ip(['-o', 'netns', 'list'], **execute_params)
    if output:
        for line in output.splitlines():
            fields = line.strip().split()
            namespace = fields[0]
            namespaces.append(namespace)
    return namespaces

def list_network_interfaces(**execute_params) -> tobiko.Selection[str]:
    interfaces = tobiko.Selection[str]()
    output = execute_ip(['--brief', 'address', 'list'], **execute_params)
    if output:
        for line in output.splitlines():
            fields = line.strip().split()
            interface = fields[0]
            interfaces.append(interface)
    return interfaces

def get_network_main_route_device(dest_ip, **execute_params):
    output = execute_ip(['route', 'get', dest_ip], **execute_params)
    if output:
        for line in output.splitlines():
            fields = line.strip().split()
            device_index = fields.index('dev') + 1
            return fields[device_index]

IP_COMMAND = sh.shell_command(['/sbin/ip'])

def execute_ip(ifconfig_args, ip_command=None, ignore_errors=False,
               **execute_params) -> str:
    if ip_command:
        ip_command = sh.shell_command(ip_command)
    else:
        ip_command = IP_COMMAND
    command = ip_command + ifconfig_args
    result = sh.execute(command, stdin=False, stdout=True, stderr=True,
                        expect_exit_status=None, **execute_params)
    if not ignore_errors and result.exit_status:
        raise IpError(error=result.stderr, exit_status=result.exit_status)
    return result.stdout


# coding: utf-8

# # Notebook 2: CNN
#
# ## Overview:
#
# 1. Begin by importing and getting the embeddings and word to index mappings we created in [Notebook 1: Embed Words](Notebook_1_Embed_Words.ipynb)
#

# # Word Embedding Parameters

# In[34]:

import numpy as np
import random

embedding_num_steps = 100001

unknown_word_token = "<UNK/>"
embedding_batch_size = 20
embedding_size = 20 # Dimension of the embedding vector.
skip_window = 10       # How many words to consider left and right.
num_skips = 20         # How many times to reuse an input to generate a label.
# We pick a random validation set to sample nearest neighbors. Here we limit the
# validation samples to the words that have a low numeric ID, which by
# construction are also the most frequent.
valid_size = 16     # Random set of words to evaluate similarity on.
valid_window = 100  # Only pick dev samples in the head of the distribution.
valid_examples = np.array(random.sample(np.arange(valid_window), valid_size))
num_sampled = 64    # Number of negative examples to sample.

# # TextCNN Parameters

# In[35]:

# Model Hyperparameters
sentence_padding_token = "<PAD/>"
sentence_padding = 80

filter_sizes =  "3,4,5" #"Comma-separated filter sizes (default: '3,4,5')")
num_filters = 50  #"Number of filters per filter size (default: 128)")
dropout_keep_prob = 0.5 #"Dropout keep probability (default: 0.5)")
l2_reg_lambda = 3.0 #"L2 regularizaion lambda (default: 0.0)")

# Training parameters
text_cnn_batch_size = 64 # "Batch Size (default: 64)")
num_epochs = 500 #"Number of training epochs (default: 200)")
evaluate_every = 1000  #"Evaluate model on dev set after this many steps (default: 100)")
checkpoint_every = 100000 # "Save model after this many steps (default: 100)")

# Evaluation Parameters
num_folds = 10 # number of cross validation folds

# Misc Parameters
allow_soft_placement = True # "Allow device soft device placement")
log_device_placement = False  #"Log placement of ops on devices")
display_train_steps = False # toggles output of training step results

run_name = "10f-cv-unique-name-replacement"

# In[20]:

import cPickle as pickle
import tensorflow as tf
from tensorflow.models.rnn.rnn_cell import BasicLSTMCell, LSTMCell
import itertools
from collections import Counter
import csv
import re

import string
import pyprind
import collections
import math

# # Text Pre-processing Functions

# In[37]:

import re

urlFinder = re.compile('\w+:\/{2}[\d\w-]+(\.[\d\w-]+)*(?:(?:\/[^\s/]*))*')
atNameFinder = re.compile(r'@([A-Za-z0-9_]+)')

exclude_punc = set([
        "!",
        "?",
        ".",
        ",",
        ":",
        ";",
        "'",
        "\"",
        "'",
        "-",
        "(",
        ")"
])

def clean(string):
    global atNameFinder
    global urlFinder

    words = []

    for word in string         .strip()         .replace("&", "")         .replace(">","")         .replace("<", "")         .lower().split():

        word = word.replace(" ", "")
        if urlFinder.match(word):
            words.append("<URL/>")
        elif atNameFinder.search(word):
            words.append("<AT_NAME/>")
        else:
            word = ''.join([i if ord(i) < 128 else '' for i in word])
            word = ''.join(ch for ch in word if ch not in exclude_punc)
            word.strip()
            if word != "":
                words.append(word)
    return words

def pad(sentence):
    global sentence_padding
    global sentence_padding_token
    if(sentence_padding < len(sentence)):
        raise Exception("Increase sentence_padding,             found sentence that is %s words long. sentence_padding must be             greater than or equal to the number of words in the longest sentence" % len(sentence))
    else:
        for x in range(sentence_padding - len(sentence)):
            sentence.append(sentence_padding_token)
    return sentence

# # Training Word Embeddings

# ## 1. Loading Words

# In[11]:

wordSet = set()
vocabGrowth = 0
vocabulary = {}
vocabulary_inv = []

# Build Vocab
with open('vocab.csv', 'rb') as f:

    reader = csv.reader(f, delimiter=',')
    numline = len([row for row in reader])
    bar = pyprind.ProgBar(numline, monitor=True)
    f.seek(0)

    for row in reader:
        if len(row) > 0:
            words = clean(row[0])
            for word in words:
                word = word.encode('ascii', 'replace')
                if(word not in wordSet):
                    vocabulary_inv.append(word)
                    vocabulary[word] = vocabulary_inv.index(word)
                    wordSet.add(word)
        bar.update()

vocabulary_inv.append(sentence_padding_token)
vocabulary[sentence_padding_token] = vocabulary_inv.index(word)
wordSet.add(sentence_padding_token)

vocabulary_inv.append(unknown_word_token)
vocabulary[unknown_word_token] = vocabulary_inv.index(word)
wordSet.add(unknown_word_token)

vocabulary_size = len(wordSet)
print("Vocabulary Size: %s" % vocabulary_size)

embeddings = None
data_index = 0
data = []

# ## 2. Training Embeddings

# In[28]:

data = [ idx for word, idx in vocabulary.iteritems() ]

print('Sample data %s' % data[:10])

def generate_batch(batch_size, num_skips, skip_window):
    global data_index
    global data
    assert batch_size % num_skips == 0
    assert num_skips <= 2 * skip_window
    batch = np.ndarray(shape=(batch_size), dtype=np.int32)
    labels = np.ndarray(shape=(batch_size, 1), dtype=np.int32)
    span = 2 * skip_window + 1 # [ skip_window target skip_window ]
    buffer = collections.deque(maxlen=span)
    for _ in range(span):
        buffer.append(data[data_index])
        data_index = (data_index + 1) % len(data)
    for i in range(batch_size // num_skips):
        target = skip_window  # target label at the center of the buffer
        targets_to_avoid = [ skip_window ]
    for j in range(num_skips):
        while target in targets_to_avoid:
            target = random.randint(0, span - 1)
        targets_to_avoid.append(target)
        batch[i * num_skips + j] = buffer[skip_window]
        labels[i * num_skips + j, 0] = buffer[target]
    buffer.append(data[data_index])
    data_index = (data_index + 1) % len(data)
    return batch, labels

batch, labels = generate_batch(batch_size=10, num_skips=10, skip_window=5)

for i in range(10):
    print('%s -> %s' % (batch[i], labels[i, 0]))
    print('%s -> %s' % (vocabulary_inv[batch[i]], vocabulary_inv[labels[i, 0]]))

graph = tf.Graph()
with graph.as_default():
    # Input da 4ta.
    train_inputs = tf.placeholder(tf.int32, shape=[embedding_batch_size])
    train_labels = tf.placeholder(tf.int32, shape=[embedding_batch_size, 1])
    valid_dataset = tf.constant(valid_examples, dtype=tf.int32)
    # Ops and variables pinned to the CPU because of missing GPU implementation
    with tf.device('/cpu:0'):
        # Look up embeddings for inputs.
        embeddings = tf.Variable(
            tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
        embed = tf.nn.embedding_lookup(embeddings, train_inputs)
        # Construct the variables for the NCE loss
        with tf.name_scope("nce_weights") as scope:
            nce_weights = tf.Variable(
                tf.truncated_normal([vocabulary_size, embedding_size],
                                    stddev=1.0 / math.sqrt(embedding_size)))
        nce_biases = tf.Variable(tf.zeros([vocabulary_size]))
        nce_biases_hist = tf.histogram_summary("nce_biases", nce_biases)

    # Compute the average NCE loss for the batch.
    # tf.nce_loss automatically draws a new sample of the negative labels each
    # time we evaluate the loss.
    with tf.name_scope("loss") as scope:
        loss = tf.reduce_mean(
            tf.nn.nce_loss(nce_weights, nce_biases, embed, train_labels,
                         num_sampled, vocabulary_size))
    # Construct the SGD optimizer using a learning rate of 1.0.
    with tf.name_scope("train") as scope:
        optimizer = tf.train.GradientDescentOptimizer(0.25).minimize(loss)

    # Compute the cosine similarity between minibatch examples and all embeddings.
    norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True))
    normalized_embeddings = embeddings / norm
    valid_embeddings = tf.nn.embedding_lookup(
        normalized_embeddings, valid_dataset)
    similarity = tf.matmul(
    valid_embeddings, normalized_embeddings, transpose_b=True)

    # Step 5: Begin training.

    with tf.Session(graph=graph) as session:
        # We must initialize all variables before we use them.
        merged = tf.merge_all_summaries()
        writer = tf.train.SummaryWriter("/tmp/tensor_logs/expiriment_1", session.graph_def)

        #Adds an op to initialize all variables
        init_op = tf.initialize_all_variables()

        # Begins running the init opp
        init_op.run()

        print("Initialized")
        average_loss = 0
        bar = pyprind.ProgBar(embedding_num_steps, monitor=True)
        for step in xrange(embedding_num_steps):
            batch_inputs, batch_labels = generate_batch(
                embedding_batch_size, num_skips, skip_window)
            feed_dict = {train_inputs : batch_inputs, train_labels : batch_labels}
            # We perform one update step by evaluating the optimizer op (including it
            # in the list of returned values for session.run()
            summary_str, _, loss_val = session.run([merged, optimizer, loss], feed_dict=feed_dict)
            writer.add_summary(summary_str, step)
            average_loss += loss_val
            if step % 2000 == 0:
                if step > 0:
                    average_loss /= 2000
                # The average loss is an estimate of the loss over the last 2000 batches.
                print("Average loss at step %s: %s" % (step, average_loss))
                average_loss = 0
            # Note that this is expensive (~20% slowdown if computed every 500 steps)
            if step % 5000 == 0:
                sim = similarity.eval()
                for i in xrange(valid_size):
                    valid_word = vocabulary_inv[valid_examples[i]]
                    top_k = 8 # number of nearest neighbors
                    nearest = (-sim[i, :]).argsort()[1:top_k+1]
                    log_str = "Nearest to %s:" % valid_word
                for k in xrange(top_k):
                    close_word = vocabulary_inv[nearest[k]]
                    log_str = "%s %s" % (log_str, close_word)
                print(log_str)
            bar.update()

        # eval embedding tensor
        embeddings = normalized_embeddings.eval()

# # Training TextCNN Model

# In[57]:

sentences = []
labels = []
_y = []
with open('data.csv', 'rb') as f:
    reader = csv.reader(f, delimiter=',')

    for row in reader:
        sentences.append(clean(row[1]))
        labels.append(([0, 1] if row[0] == "example" else [1, 0]))
        _y.append(1 if row[0] == "example" else 0)

sequence_length = max(len(i) for i in sentences)
padded_sentences = [ pad(sentence) for sentence in sentences]

word_counts = Counter(itertools.chain(*padded_sentences))

# Mapping from index to word
vocabulary_inv = [x[0] for x in word_counts.most_common()]
# Mapping from word to index
vocabulary = {x: i for i, x in enumerate(vocabulary_inv)}

x = np.array([[vocabulary[word] for word in sentence] for sentence in padded_sentences])
y = np.array(labels)
_y = np.array(_y)

# In[58]:

import tensorflow as tf
import numpy as np

class TextCNN(object):
    """
    A CNN for text classification.
    Uses an embedding layer, followed by a convolutional, max-pooling and softmax layer.
    """
    def __init__(
      self, sequence_length, num_classes, vocab_size,
      embedding_size, embedding_tensor, filter_sizes, num_filters, l2_reg_lambda=0.0):

        # Placeholders for input, output and dropout
        self.input_x = tf.placeholder(tf.int32, [None, sequence_length], name="input_x")
        self.input_y = tf.placeholder(tf.float32, [None, num_classes], name="input_y")
        self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")

        # Keeping track of l2 regularization loss (optional)
        l2_loss = tf.constant(0.0)

        # Embedding layer
        with tf.device('/cpu:0'), tf.name_scope("embedding"):
            W = tf.Variable(embedding_tensor,
                name="W")
            self.embedded_chars = tf.nn.embedding_lookup(W, self.input_x)
            self.embedded_chars_expanded = tf.expand_dims(self.embedded_chars, -1)

        # Create a convolution + maxpool layer for each filter size
        pooled_outputs = []
        for i, filter_size in enumerate(filter_sizes):
            with tf.name_scope("conv-maxpool-%s" % filter_size):
                # Convolution Layer
                filter_shape = [filter_size, embedding_size, 1, num_filters]
                W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W")
                b = tf.Variable(tf.constant(0.1, shape=[num_filters]), name="b")
                conv = tf.nn.conv2d(
                    self.embedded_chars_expanded,
                    W,
                    strides=[1, 1, 1, 1],
                    padding="VALID",
                    name="conv")
                # Apply nonlinearity
                h = tf.nn.relu(tf.nn.bias_add(conv, b), name="relu")
                # Maxpooling over the outputs
                pooled = tf.nn.max_pool(
                    h,
                    ksize=[1, sequence_length - filter_size + 1, 1, 1],
                    strides=[1, 1, 1, 1],
                    padding='VALID',
                    name="pool")
                pooled_outputs.append(pooled)

        # Combine all the pooled features
        num_filters_total = num_filters * len(filter_sizes)
        self.h_pool = tf.concat(3, pooled_outputs)
        self.h_pool_flat = tf.reshape(self.h_pool, [-1, num_filters_total])

        # Add dropout
        with tf.name_scope("dropout"):
            self.h_drop = tf.nn.dropout(self.h_pool_flat, self.dropout_keep_prob)

        # Final (unnormalized) scores and predictions
        with tf.name_scope("output"):
            W = tf.Variable(tf.truncated_normal([num_filters_total, num_classes], stddev=0.1), name="W")
            b = tf.Variable(tf.constant(0.1, shape=[num_classes]), name="b")
            l2_loss += tf.nn.l2_loss(W)
            l2_loss += tf.nn.l2_loss(b)
            self.scores = tf.nn.xw_plus_b(self.h_drop, W, b, name="scores")
            self.predictions = tf.argmax(self.scores, 1, name="predictions")

        # CalculateMean cross-entropy loss
        with tf.name_scope("loss"):
            losses = tf.nn.softmax_cross_entropy_with_logits(self.scores, self.input_y)
            self.loss = tf.reduce_mean(losses) + l2_reg_lambda * l2_loss

        # Accuracy
        with tf.name_scope("accuracy"):
            correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1))
            self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")

# In[60]:

def batch_iter(data, batch_size, num_epochs):
    """
    Generates a batch iterator for a dataset.
    """
    data = np.array(data)
    data_size = len(data)
    num_batches_per_epoch = int(len(data)/batch_size) + 1
    for epoch in range(num_epochs):
        # Shuffle the data at each epoch
        shuffle_indices = np.random.permutation(np.arange(data_size))
        shuffled_data = data[shuffle_indices]
        for batch_num in range(num_batches_per_epoch):
            start_index = batch_num * batch_size
            end_index = min((batch_num + 1) * batch_size, data_size)
            yield shuffled_data[start_index:end_index]

# In[62]:

import tensorflow as tf
import numpy as np
import os
import time
import datetime
from sklearn.cross_validation import StratifiedKFold
import sys

# Data Preparatopn
# ==================================================

# Load data
print("Loading data...")
sys.stdout.flush()
np.random.seed(10)
shuffle_indices = np.random.permutation(np.arange(len(y)))
x_shuffled = x[shuffle_indices]
y_shuffled = y[shuffle_indices]
_y_shuffled = _y[shuffle_indices]

skf = StratifiedKFold(_y_shuffled, n_folds=num_folds)

fold_accuracies = []
fold_specificities = []
fold_sensitivities = []

# Split train/test set
# TODO: This is very crude, should use cross-validation
#x_train, x_dev = x_shuffled[:len(x_shuffled)-1], x_shuffled[-len(x_shuffled)-1:]
#y_train, y_dev = y_shuffled[:len(y_shuffled)-1], y_shuffled[-len(y_shuffled)-1:]
#print("Vocabulary Size: {:d}".format(len(vocabulary)))
#print("Train/Dev split: {:d}/{:d}".format(len(y_train), len(y_dev)))
#print("Train Pos/Dev Pos Split {:d}/{:d}"
#      .format(
#        len(
#            [y for y in y_train if y[0] == 0 and y[1] == 1]
#        ), len(
#            [y for y in y_dev if y[0] == 0 and y[1] == 1]
#        )))
#print("Train Neg/Dev Neg Split {:d}/{:d}"
#      .format(
#        len(
#            [y for y in y_train if y[0] == 1 and y[1] == 0]
#        ), len(
#            [y for y in y_dev if y[0] == 1 and y[1] == 0]
#        )))

# Training
# ==================================================

foldBar = pyprind.ProgBar(num_folds, title='CV_Progress')
for idx, fold in zip(skf, range(num_folds)):
    x_train = x[idx[0]]
    y_train = y[idx[0]]

    x_dev = x[idx[1]]
    y_dev = y[idx[1]]

    with tf.Graph().as_default():
        session_conf = tf.ConfigProto(
          allow_soft_placement=allow_soft_placement,
          log_device_placement=log_device_placement)
        sess = tf.Session(config=session_conf)
        with sess.as_default():
            cnn = TextCNN(
                sequence_length=x_train.shape[1],
                num_classes=2,
                vocab_size=len(vocabulary),
                embedding_size=embedding_size,
                embedding_tensor=embeddings,
                filter_sizes=map(int, filter_sizes.split(",")),
                num_filters=num_filters,
                l2_reg_lambda=l2_reg_lambda)

            # Define Training procedure
            global_step = tf.Variable(0, name="global_step", trainable=False)
            optimizer = tf.train.AdamOptimizer(1e-4)
            grads_and_vars = optimizer.compute_gradients(cnn.loss)
            train_op = optimizer.apply_gradients(grads_and_vars, global_step=global_step)

            # Keep track of gradient values and sparsity (optional)
            grad_summaries = []
            for g, v in grads_and_vars:
                if g is not None:
                    grad_hist_summary = tf.histogram_summary("{}/grad/hist".format(v.name), g)
                    sparsity_summary = tf.scalar_summary("{}/grad/sparsity".format(v.name), tf.nn.zero_fraction(g))
                    grad_summaries.append(grad_hist_summary)
                    grad_summaries.append(sparsity_summary)
            grad_summaries_merged = tf.merge_summary(grad_summaries)

            # Output directory for models and summaries
            timestamp = str(int(time.time()))
            out_dir = os.path.abspath(os.path.join(os.path.curdir, "runs", run_name, timestamp))
            print("Writing to {}\n".format(out_dir))

            # Summaries for loss and accuracy
            loss_summary = tf.scalar_summary("loss", cnn.loss)
            acc_summary = tf.scalar_summary("accuracy", cnn.accuracy)

            # Train Summaries
            train_summary_op = tf.merge_summary([loss_summary, acc_summary, grad_summaries_merged])
            train_summary_dir = os.path.join(out_dir, "summaries", "train", str(fold))
            train_summary_writer = tf.train.SummaryWriter(train_summary_dir, sess.graph_def)

            # Dev summaries
            dev_summary_op = tf.merge_summary([loss_summary, acc_summary])
            dev_summary_dir = os.path.join(out_dir, "summaries", "dev", str(fold))
            dev_summary_writer = tf.train.SummaryWriter(dev_summary_dir, sess.graph_def)

            # Checkpoint directory. Tensorflow assumes this directory already exists so we need to create it
            checkpoint_dir = os.path.abspath(os.path.join(out_dir, "checkpoints"))
            checkpoint_prefix = os.path.join(checkpoint_dir, "model")
            if not os.path.exists(checkpoint_dir):
                os.makedirs(checkpoint_dir)
            saver = tf.train.Saver(tf.all_variables())

            # Initialize all variables
            sess.run(tf.initialize_all_variables())

            def train_step(x_batch, y_batch):
                """
                A single training step
                """
                feed_dict = {
                  cnn.input_x: x_batch,
                  cnn.input_y: y_batch,
                  cnn.dropout_keep_prob: dropout_keep_prob
                }
                _, step, summaries, loss, accuracy = sess.run(
                    [train_op, global_step, train_summary_op, cnn.loss, cnn.accuracy],
                    feed_dict)
                time_str = datetime.datetime.now().isoformat()
                if(display_train_steps):
                    print("{}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, accuracy))
                train_summary_writer.add_summary(summaries, step)

            def dev_step(x_batch, y_batch, writer=None):
                """
                Evaluates model on a dev set
                """
                feed_dict = {
                  cnn.input_x: x_batch,
                  cnn.input_y: y_batch,
                  cnn.dropout_keep_prob: 1.0
                }
                step, summaries, loss, accuracy = sess.run(
                    [global_step, dev_summary_op, cnn.loss, cnn.accuracy],
                    feed_dict)
                time_str = datetime.datetime.now().isoformat()
                print("{}: step {}, loss {:g}, acc {:g}".format(time_str, step, loss, accuracy))

                if writer:
                    writer.add_summary(summaries, step)
                return accuracy

            # Generate batches
            batches = batch_iter(
                zip(x_train, y_train), text_cnn_batch_size, num_epochs)
            # Training loop. For each batch...
            it = ((int(len(data)/text_cnn_batch_size)+1)*num_epochs)
            print("Steps: {0}".format(it))
            bar = pyprind.ProgBar(it, title='fold_{0}'.format(fold), monitor=True)
            for batch in batches:
                x_batch, y_batch = zip(*batch)
                train_step(x_batch, y_batch)
                current_step = tf.train.global_step(sess, global_step)
                if current_step % checkpoint_every == 0:
                    path = saver.save(sess, checkpoint_prefix, global_step=current_step)
                    print("Saved model checkpoint to {}\n".format(path))
                if current_step % evaluate_every == 0:
                    print("\nEvaluation:")
                    dev_step(x_dev, y_dev, writer=dev_summary_writer)
                    print("")

                bar.update()
                sys.stderr.flush()

            acc = dev_step(x_dev, y_dev, writer=dev_summary_writer)
            fold_accuracies.append(acc)

            tp = 0
            fp = 0
            tn = 0
            fn = 0

            for _y, _x in zip(y_dev, x_dev):
                a= dev_step([_x], [_y])
                expected = "example" if _y[0] == 0 and _y[1] == 1  else "nonexample"
                actual = None
                if(_y[0] == 0 and _y[1] == 1):
                    # correct label is example
                    if(a == 1.0):
                        actual = "example"
                    else:
                        actual = "nonexample"
                elif(_y[0] == 1 and _y[1] == 0):
                    if(a == 1.0):
                        actual = "nonexample"
                    else:
                        actual = "example"

                if(expected == "example" and actual == "example"):
                    tp += 1
                elif(expected == "example" and actual == "nonexample"):
                    fn += 1
                elif(expected == "nonexample" and actual =="exaple"):
                    fp += 1
                elif(expected == "nonexample" and actual == "nonexample"):
                    tn +=1

            sensitivity = (tp/(tp+float(fn)))
            fold_sensitivities.append(sensitivity)

            specificity = (tn/(tn+float(fp)))
            fold_specificities.append(specificity)
            foldBar.update()

            sys.stderr.flush()

        print("")

# In[7]:

final_accuracy = sum(fold_accuracies) / float(len(fold_accuracies))
print("10-fold final accuracy: %s" % final_accuracy)
final_specificity = sum(fold_specificities) / float(len(fold_specificities))
print("10-fold final specificity: %s" % final_specificity)
final_sensitivities = sum(fold_sensitivities) / float(len(fold_sensitivities))
print("10-fold final sensitivity: %s" % final_sensitivities)

# In[ ]:

# In[ ]:


import pandas as pd
from paul_resources import HealthcareSymbols, tprint, PriceTable
from beta_class_7 import ScrubParams, Beta
from decorators import my_time_decorator
import pickle

lookback = 400
stock_cutoff = .0875
index_cutoff = .0125
percentile_cutoff = .85
stocks = HealthcareSymbols[0:1000]

@my_time_decorator
def get_best_betas():
    best_indices = []
    best_betas = []
    best_corrs = []

    for stock in stocks:
        indices = ['SPY', 'XLV', 'IBB', 'XBI']
        index_cutoffs = {'SPY': .015, 'XLV': .015, 'IBB': .015, 'XBI': .0225}
        outcomes = []
        for index in indices:
            index_cutoff = index_cutoffs[index]
            scrub_params = ScrubParams(stock_cutoff, index_cutoff, percentile_cutoff)
            beta = Beta(stock, index, lookback, scrub_params)
            outcomes.append((index, beta.beta_value, beta.corr))
        max_corr = max([i[2] for i in outcomes])
        best_fit = [i for i in outcomes if i[2] == max_corr][0]
        best_indices.append(best_fit[0])
        best_betas.append(best_fit[1])
        best_corrs.append(best_fit[2])

    info = {'Stock': stocks,
            'Index': best_indices,
            'Beta': best_betas,
            'Corr': best_corrs}

    return pd.DataFrame(info).set_index('Stock').loc[:, ['Index', 'Beta', 'Corr']]

best_betas = get_best_betas().sort_values(['Index', 'Beta'], ascending=[True, False])

print(best_betas.round(2).to_string())

best_betas.to_csv('best_betas.csv')

pickle_file = open('best_betas.pkl', 'wb')
pickle.dump(best_betas, pickle_file, pickle.HIGHEST_PROTOCOL)
pickle_file.close()


import pytest
from slackbot.get_testers import balance_testing, find_next
from slackbot.db_setup.sql_declarations import User

from sqlalchemy import desc

def test_balance_tester(session):

    highest_user = session.query(User).order_by(desc(User.assigned_tests)).first()
    highest_user.assigned_tests = 0
    session.commit()
    absent_set = {highest_user.slack_id}

    balance_testing(absent_set=absent_set, session=session)

    session.commit()
    user = session.query(User).order_by(User.assigned_tests).first()

    assert user.slack_id in absent_set
    assert user.assigned_tests > 0

@pytest.mark.filterwarnings('ignore::sqlalchemy.exc.SAWarning')
def test_find_next(session):
    # Test with an empty exception set
    exception_set = set()
    tester_one, tester_two = find_next(session=session, exception_set=exception_set)

    T1, T2 = session.query(User).filter(User.slack_id.in_({tester_one, tester_two})).all()

    assert not (T1.is_intern and T2.is_intern)
    assert not T1.slack_id in exception_set or T2.slack_id in exception_set

    # Test with non empty absent set
    exception_set.add(tester_one)

    tester_one, tester_two = find_next(session=session, exception_set=exception_set)

    T1, T2 = session.query(User).filter(User.slack_id.in_({tester_one, tester_two})).all()

    assert not (T1.is_intern and T2.is_intern)
    assert not T1.slack_id in exception_set or T2.slack_id in exception_set


# Phenotype genes and methods for classification etc. -- its me and its good
import random
from models.instances import Instances
from utils import calcScore

class Phenotype(object):

    def __init__(self, phenotype_id, committee, gen_length):
        # phenotype attributes
        self.__id = phenotype_id
        self.__committee = committee
        self.__initialCommittee = committee
        self.__genLength = gen_length
        self.__fitness = 0.0
        self.__normalizedFitness = 0.0
        self.__genes = [False for _ in range(self.gen_length)]
        # classifier attribute
        self.__predictions = []
        self.create_random_genes()
        # testing
        self.__counter = 0

    @property
    def counter(self):
        return self.__counter

    @counter.setter
    def counter(self, value):
        self.__counter = value

    @property
    def phenotype_id(self):
        return self.__id

    @property
    def committee(self):
        return self.__committee

    @committee.setter
    def committee(self, value):
        if value > 0:
            self.__committee = value

    @property
    def gen_length(self):
        return self.__genLength

    @property
    def genes(self):
        return self.__genes

    @genes.setter
    def genes(self, value):
        if len(value) == self.gen_length:
            self.__genes = value

    @property
    def fitness(self):
        return self.__fitness

    @fitness.setter
    def fitness(self, value):
        if value >= 0.0:
            self.__fitness = value

    @property
    def normalizedFitness(self):
        return self.__normalizedFitness

    @normalizedFitness.setter
    def normalizedFitness(self, value):
        if 0.0 <= value <= 1.0:
            self.__normalizedFitness = value

    def copy(self, donor):
        self.__committee = donor.committee
        self.__genLength = donor.gen_length
        self.__fitness = donor.fitness
        self.__normalizedFitness = donor.normalizedFitness
        self.__genes = donor.genes

    # generate <gen_length> positive genes (classifiers)
    def create_random_genes(self):
        it = 0
        while it < self.committee:
            index = random.randint(0, self.gen_length - 1)
            if not self.genes[index]:
                self.genes[index] = True
                it += 1

    # Take results, vote for answer and calc score, then penalize bigger committees
    def calc_fitness(self):
        def punish_length(x):
            dist = x - self.__initialCommittee
            y = -(dist / (self.__initialCommittee / 2)) ** 4 + 0.5
            if y < 0:
                y = 0
            return y

        committee_answers = []
        for i in range(len(self.__predictions[0])):
            tmp = {}
            for predicts in self.__predictions:
                if predicts[i] in tmp.keys():
                    tmp[predicts[i]] += 1
                else:
                    tmp[predicts[i]] = 1
            inverse = [(value, key) for key, value in tmp.items()]
            val = max(inverse)[1]
            committee_answers.append(val)
        self.__fitness = 0.8 * pow(calcScore(predictions=committee_answers, verify=False) + 1, 2) + \
                         0.2 * punish_length(self.committee)
        return self.__fitness

    # choose classifiers from list
    # then vote and calculate fitness
    def run(self):
        self.__predictions.clear()
        for i, gen in enumerate(self.genes):
            if gen:
                self.__predictions.append(Instances.predictions_arr[i])
        return self.calc_fitness()


# -- coding: utf-8 -- //打印中文
import os;
import sys;

reload(sys)
sys.setdefaultencoding('utf8')

def indexInString(string, spliteChar):
	index = string.find(spliteChar, 0, len(file))
	return index;

def inVaildFile(string, spliteChar):
	index = indexInString(string, spliteChar)
	return index >= 0;

# curPath = sys.argv[0];
# print("curPath:\n", curPath);
# upath = unicode(curPath,'utf-8')
print("\r\n======打印路径=====\r\n");

fileName = os.path.basename(__file__);
pathName = os.path.dirname(__file__);

upath = sys.argv[0];
upath = upath[0: len(upath) - len(fileName)];
print("ppath:", fileName, __file__, upath, pathName);

result = ""

# 从文件名中，取出“贷款额度”，并以此进行排序
spliteChar = "=";
file_temp = []
file_comp = []
dirs = os.listdir(upath);
for file in dirs:
	if (inVaildFile(file, spliteChar)):
		file_temp.append(file) #[file]

		value = file.split("万")[0].split("款")[1]
		int_value = int(value)
		file_temp.append(int_value) #[file, 贷款额度]

		file_comp.append(file_temp)
		file_temp = []

file_comp.sort(key = lambda x: (x[1]))
sorted_files = file_comp;

result += "======排序后的文件信息=====\r\n";
print("\r\n======排序后的文件信息=====\r\n");

# 从文件名中，取出“利息额度”
list = [];
listA = [];
for element in sorted_files:
	file = element[0]
	value = element[1]
	index = indexInString(file, spliteChar)
	if (index >= 0):
		profit = file[index + 1: len(file) - 3];
		list.append(value)
		list.append(float(profit))
		list.append(file) #[贷款额度，利息，文件]
		listA.append(list)

		res = unicode(file);
		result += "\n" + res;
		print("current file:", listA);

		list = []

result += "\r\n"
result += "\r\n=====计算利息额度=====\r\n";
print("\r\n======计算利息额度=====\r\n");

# 从文件名中，取出“利息额度”
listB = []
list_with_diff = []
for n in range(1, len(listA)):
	temp_diff = listA[n][1] - listA[n-1][1];
	listB.append(listA[n][0]);
	listB.append(listA[n-1][0]);
	listB.append(temp_diff);
	list_with_diff.append(listB)

	res = ', '.join(str(i) for i in listB)
	result += "\n" + res;
	print("current diff:", listB);

	listB = []

# 输出到文件
resultPath = pathName + "/" + "result.txt";
f = open(resultPath, "w")
f.write(result);


import sqlite3

def connect():
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    # id is the serial number for books, it uniquely identifies each book
    cur.execute(
        "CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY, title TEXT,author TEXT,year INTEGER,isbn INTEGER)")
    conn.commit()
    conn.close()

def insert(title, author, year, isbn):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    # NULL is for id Python automatically creates id
    cur.execute("INSERT INTO books VALUES(NULL,?,?,?,?)",
                (title, author, year, isbn))
    conn.commit()
    conn.close()

def view():
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM books")
    rows = cur.fetchall()
    conn.close()
    return rows

# We pass empty strings bc, if the user only enters one parameter we get an error
def search(title="", author="", year="", isbn=""):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    cur.execute("SELECT * FROM books WHERE title=? OR author=? OR year=? OR isbn=?",
                (title, author, year, isbn))
    rows = cur.fetchall()
    conn.close()
    return rows

# Deleting the entire tuple

def delete(id):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    # NULL is for id Python automatically creates id
    cur.execute("DELETE FROM books WHERE id=?", (id,))
    conn.commit()
    conn.close()

def update(id, title, author, year, isbn):
    conn = sqlite3.connect("books.db")
    cur = conn.cursor()
    # NULL is for id Python automatically creates id
    cur.execute("UPDATE books SET title=?,author=?,year=?,isbn=? WHERE id=?",
                (title, author, year, isbn, id))
    conn.commit()
    conn.close()

# Function Calls
connect()
# # insert("Godfather", "Mario Puzo", 1950, 9471)
# # insert("Da Vinci Code", "Dan Brown", 2005, 9898)

# # delete(3)
# # print(search(isbn="Dan Brown"))
# update(2, "The Shining", "Stephen King", 1972, 7845)
# print(view())


##
## Genere una lista de tuplas, donde cada tupla contiene en la primera
## posicion, el valor de la segunda columna; la segunda parte de la
## tupla es una lista con las letras de la primera columna que aparecen
## asociadas a dicho valor de la segunda columna. Esto es:
##
##    ('0', ['C'])
##    ('1', ['E', 'B', 'D', 'A', 'A', 'E'])
##    ('2', ['A', 'E', 'D'])
##    ('3', ['A', 'B', 'D', 'E', 'E'])
##    ('4', ['E', 'B'])
##    ('5', ['B', 'C', 'D', 'D', 'E', 'E', 'E'])
##    ('6', ['C', 'E', 'A', 'B'])
##    ('7', ['A', 'C', 'E', 'D'])
##    ('8', ['E', 'E', 'A', 'B'])
##    ('9', ['A', 'B', 'E', 'C'])
##
##

#Leer el archivo
import glob #importar archivos
filenames=glob.glob('data.csv')

text =[] #Grabar archivo en una variable
for filename in filenames:
    with open(filename, 'rt') as f:
        text += f.readlines()

archivo2 = [row[0:-1] for row in text] #Eliminar retornos de carros
archivo2 = [row.split('\t') for row in archivo2] #Separar archivo en tab

#Respuesta
count = {}

for e in archivo2:
	count[e[1]] = []

for e in archivo2:
	count[e[1]].append(e[0])

for i in sorted(count.items()):
	print (i)


from multiprocessing import Process
import time

# 创建子进程
class MyProcess(Process):
    def __init__(self,name,*args,**kwargs):
        # 调用父类的init()方法
        super().__init__(*args,**kwargs)
        self.id = id

    # 重写run方法
    def run(self):
        print('子进程%d开始执行--%s'%(self.id,self.pid))
        time.sleep(2)
        print('子进程%d执行结束--%s'%(self.id,self.pid))

if __name__ == '__main__':
    #　主进程
    print('主进程开始执行')
    p = MyProcess(name='子进程',id = 1)
    p.start()
    time.sleep(3)

    print('主进程结束')


import urllib
import requests
from bs4 import BeautifulSoup
import gzip
from io import StringIO,BytesIO
import zlib
import database as DB
import datetime
from changeUtils import changeleixing, changeTeamName, dealPankou, gzipData, deflate, checkContain

requests.adapters.DEFAULT_RETRIES = 5
s = requests.session()
s.keep_alive = False

def parseData(bisaiDate,leixings):
    tempUrl = 'https://live.leisu.com/wanchang?date='+str(bisaiDate)
    req = urllib.request.Request(tempUrl)
    req.add_header('Accept',
                   'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3')
    req.add_header('Accept-Language', 'zh-CN,zh;q=0.9')
    req.add_header('Accept-Encoding', 'gzip,deflate')
    req.add_header('Cache-Control', 'max-age=0')
    req.add_header('Connection', 'keep-alive')
    req.add_header('Cookie','acw_tc=2f61f27c15662043202395048e4426defba93f49a45f4cf8db50cc5b36835a; lang=; PHPSESSID=rv690j4lh7gqsuh29qrrhs3qb3; Hm_lvt_63b82ac6d9948bad5e14b1398610939a=1566204298,1566204368,1566204377; SERVERID=4ab2f7c19b72630dd03ede01228e3e61|1566974507|1566974422; Hm_lpvt_63b82ac6d9948bad5e14b1398610939a=1566974507')

    req.add_header('Host', 'live.leisu.com')
    req.add_header('If-None-Match','W/"fe010-hQk3q4B/k1m4zDOwdbMVMB9r2hI"')
    req.add_header('Sec-Fetch-Mode','navigate')
    req.add_header('Sec-Fetch-Site','none')
    req.add_header('Sec-Fetch-User','?1')

    req.add_header('Upgrade-Insecure-Requests', '1')
    req.add_header('User-Agent',
                   "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3809.100 Safari/537.36")

    res = urllib.request.urlopen(tempUrl)
    content = res.read()
    encoding = res.info().get('Content-Encoding')
    if encoding == 'gzip':
        content = gzipData(content)
    elif encoding == 'deflate':
        content = deflate(content)
    soup = BeautifulSoup(content, 'html.parser')
    trs = soup.select('li .clearfix-row')
    map = {}

    for tr in trs:
        leixing, leixing2 = changeleixing(tr.select('.event-name span')[0].get_text().strip())
        if leixing not in leixings and (leixing2 == '' or (leixing2 != '' and leixing2 not in leixings)):
            continue
        zhudui = changeTeamName(tr.select('.lab-team-home .name')[0].get_text().strip())
        kedui = changeTeamName(tr.select('.lab-team-away .name')[0].get_text().strip())

        zhubifen = tr.select('.lab-score .color-red')[0].get_text().strip().split('-')[0]
        kebifen = tr.select('.lab-score .color-red')[0].get_text().strip().split('-')[1]
        linchangpankou = dealPankou(tr.select('.lab-ratel')[0].get_text().strip())
        if linchangpankou is None:
            continue
        element = {'zhudui': zhudui, 'kedui': kedui,'linchangpankou': linchangpankou,'zhubifen':zhubifen,'kebifen':kebifen}
        elements = []
        if map.__contains__(leixing):
            elements = map.get(leixing)
        elements.append(element)
        map[leixing] = elements
        if leixing2 != '':
            elements = []
            if map.__contains__(leixing2):
                elements = map.get(leixing2)
            elements.append(element)
            map[leixing2] = elements
    return map

def updatePankou(bisaiDate,leixings,matchs):
    print(str(bisaiDate[0]))
    curDay = (bisaiDate[0]).strftime("%Y%m%d")
    map = parseData(curDay, leixings)

    afterDay = (bisaiDate[0] + datetime.timedelta(days=1)).strftime("%Y%m%d")

    if datetime.datetime.now().strftime("%Y%m%d") != afterDay:
        map2 = parseData(afterDay, leixings)

        for leixing in map2:
            arr = map2[leixing]
            if leixing in map:
                map[leixing] = map[leixing] + arr
            else:
                map[leixing] = arr

    updateDatas = []

    for match in matchs:
        bisaileixing = match[0]
        zhudui = match[1]
        kedui = match[2]
        zhubifen = match[3]
        kebifen = match[4]
        if not map.__contains__(bisaileixing):
            continue
        results = map[bisaileixing]

        for result in results:
            if checkContain(zhudui, result['zhudui']) and checkContain(kedui, result['kedui']):
                if zhubifen == int(result['zhubifen']) and kebifen == int(result['kebifen']):
                    updateData = [result['linchangpankou'], bisaiDate, bisaileixing,
                                  zhudui, kedui]
                    updateDatas.append(tuple(updateData))
                    continue

    DB.updatePankou(database, tuple(updateDatas))

def updateTask(database):
    dates = DB.queryNoPankouDates(database)
    if dates is None or len(dates) == 0:
        return
    for bisaiDate in dates:
        try:
            leixings = DB.queryNoPankouLeixingByDate(database,bisaiDate)
            teams = DB.queryNoPankouByDate(database,bisaiDate)
            lxs = []
            for lx in leixings:
                lxs.append(lx[0])

            updatePankou(bisaiDate,lxs,teams)
        except Exception as e:
            print(e)

if __name__ == '__main__':
    database = DB.Database('localhost', 'root', 'root', 'sports')
    updateTask(database)
    #lexings = ['韩足杯','芬甲']
    #parseData('2019-07-02',lexings)


# coding=utf-8
from searchWinner import searchWinner
from buildBoard import buildBoard
from drawBoard import drawBoard
from sizeBoard import sizeBoar
from instructions import Inst
from setMove import setMove
from colors import Colors
from player import Player
from help import Help
import random

# Modo de Juego Humano versus Humano
def PlayerVsPlayer():
    print('\n*-------------------------*')
    print('** MODO PLAYER vs PLAYER **')
    print('*-------------------------*\n')
    print(
        'Antes de empezar a jugar si tiene duda de las instrucciones del juego ingrese el  siguiente comando -i o -I y si necesita ayuda ingrese el siguiente comando -h o -H')
    player1 = input("Ingrese nombre para PLAYER 1: ")
    while True:
        if player1 == '-i' or player1 == '-I':
            Inst()
            player1 = input("Ingrese nombre para PLAYER 1: ")
        elif player1 == '-h' or player1 == '-H':
            Help()
            player1 = input("Ingrese nombre para PLAYER 1: ")
        else:
            break
    player1 = Player(player1, 0, 0, 0, Colors.CRED)
    player2 = input("Ingrese nombre para PLAYER 2: ")
    while True:
        if player2 == '-i' or player2 == '-I':
            Inst()
            player2 = input("Ingrese nombre para PLAYER 2: ")
        elif player2 == '-h' or player2 == '-H':
            Help()
            player2 = input("Ingrese nombre para PLAYER 2: ")
        else:
            break
    player2 = Player(player2, 1, 1, 0, Colors.CGREEN)
    sizeT = input('Ingrese el tamaño del tablero: ')
    print()
    while True:
        if sizeT == '-i' or sizeT == '-I':
            Inst()
            sizeT = input('Ingrese el tamaño del tablero: ')
        elif sizeT == '-h' or sizeT == '-H':
            Help()
            sizeT = input('Ingrese el tamaño del tablero: ')
        else:
            break
    sizeT = sizeBoar(sizeT)
    player = player1 if (random.randint(0, 100) % 2 == 0) else player2
    aux = player.getTurn()
    stop = (sizeT[0] * sizeT[1]) + player.getTurn()
    while True:
        board = buildBoard(sizeT[0], sizeT[1])
        drawBoard(board)
        while True:
            player = player1 if (aux % 2 == 0) else player2
            try:
                print()
                move = input("Turno de -> " + player.getNameColor() + " elija una columna: ")
                if move == '-i' or move == '-I':
                    Inst()
                    continue
                elif move == '-h' or move == '-H':
                    Help()
                    continue
                move = int(move)
                if 0 < move <= len(board[0]):
                    move -= 1
                    if setMove(board, move, player.getSymbol()):
                        if searchWinner(board, player.getSymbol()):
                            drawBoard(board)
                            print("JUEGO TERMINADO", player.getNameColor(), "GANÓ")
                            if player.getTurn() == 0:
                                player1.setWon(player1.getWon() + 1)
                            else:
                                player2.setWon(player2.getWon() + 1)
                            # player.setWon(player.getWon() + 1)
                            break
                        drawBoard(board)
                        aux += 1
                    else:
                        print("Columna llena")
                    if aux == stop:
                        print("JUEGO EMPATADO....")
                        break
                else:
                    print("Columna fuera de rango")
            except ValueError:
                print("Columna no válida")
        playAgain = input("¿Otra partida (s)? ").lower()
        if playAgain == 's':
            aux = player.getTurn()
            stop = (sizeT[0] * sizeT[1]) + player.getTurn()
        else:
            print(player1.getNameColor(), player1.getWon(), player2.getNameColor(), player2.getWon())
            print()
            break


friends = ['john', 'pat', 'gary', 'michael']
for i, name in enumerate(friends):
    print ("iteration {iteration} is {name}".format(iteration=i, name=name))
    import re

    for test_string in ['555-1212', 'ILL-EGAL']:
        if re.match(r'^\d{3}-\d{4}$', test_string):
            print(test_string, 'is a valid US local phone number')
        else:
            print(test_string, 'rejected')


import csv

# downloaded from http://www.nber.org/patents/apat63_99.zip
patentFile = "apat63_99.txt"
# downloaded from http://www.nber.org/patents/acite75_99.zip
citationFile = "cite75_99.txt"

graphFile = "modified_cite75_99.txt"

patents = {}
nodes = set()
edges = set()

print("reading patent file ...")
with open(patentFile, "r") as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    next(reader) # file has a header
    for row in reader:
        patent = row[0]
        gyear = row[1]
        patents[patent] = gyear

print("reading citation file ...")
with open(citationFile, "r") as csvfile:
    reader = csv.reader(csvfile, delimiter=',')
    next(reader) # file has a header
    for row in reader:
        a = row[0]
        b = row[1]
        if a in patents and b in patents:
            nodes.add(a)
            nodes.add(b)
            edges.add((a, b))

print("writing graph file ...")
with open(graphFile, "w") as csvfile:
    writer = csv.writer(csvfile, delimiter=" ")
    for (a, b) in edges:
        writer.writerow(["+", a, b, patents[a]])

print("finished!")
print("\tcount of nodes: ", len(nodes))
print("\tcount of edges: ", len(edges))


# Copyright (C) 2019 New York University.
#
# This file is part of REANA Templates. REANA Templates is free software; you
# can redistribute it and/or modify it under the terms of the MIT License; see
# LICENSE file for more details.

"""Export the template parameter schema specification. Either prints the schema
to standard output or write to file if the optional file parameter is given.
"""

import getopt
import json
import sys
import yaml

from reanatempl.parameter.declaration import PARAMETER_SCHEMA

# Command line usage string
COMMAND = 'Usage: [-f <JSON|YAML> | --format=<JSON|YAML>] {output-file}'

def main(args, format):
    """Write template parameter declaration schema to file or standard output.

    Raise value error if invalid format is given.

    Parameters
    ----------
    args: list
        List of command line arguments. If the list is not empty only the first
        argument will be considered as the output file. If the argument list is
        empty the schema will be printed to standard output
    format: string
        Expects either YAML or JSON as the output format specification
    """
    if not format.upper() in ['YAML', 'JSON']:
        raise ValueError('invalid output format \'' + str(format) + '\'')
    if format.upper() == 'YAML':
        schema = yaml.dump(PARAMETER_SCHEMA, indent=4)
    else:
        schema = json.dumps(PARAMETER_SCHEMA, indent=4)
    # Output source depends on whether the output file argument is given or not
    if len(args) > 0:
        with open(args[0], 'w') as f:
            f.write(schema + '\n')
    else:
        print(schema)

if __name__ == '__main__':
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'f:', ['format='])
        if len(args) > 1 or len(opts) > 1:
            print(COMMAND)
            sys.exit(-1)
        # If an option is given it is expected to contain a valid format (either
        # JSON or YAML). The default format is YAML.
        format = 'YAML'
        if len(opts) == 1:
            _, format = opts[0]
        if not format.upper() in ['YAML', 'JSON']:
            print('Invalid output format \'' + str(format) + '\'')
            print(COMMAND)
            sys.exit(-1)
        main(args=args, format=format)
    except getopt.GetoptError as ex:
        print(ex)
        sys.exit(-1)


#!/usr/bin/python

import os

if __name__ == "__main__":

	# Input File Format: track_id \t item_id
	inputFiles = []
	for filename in os.listdir(os.getcwd()):
		root, ext = os.path.splitext(filename)
		if root.endswith('hypergraph') and ext == '.txt':
			inputFiles.append(filename)

	inputFiles.remove("Playlist_hypergraph.txt")

	# Need to keep track of all the hyper-edge membership
	all_edges = []
	hyper_edge_count = 0
	for filename in inputFiles:
		current_edge_mapping = {}
		filename = os.getcwd() + '/' + filename
		with open(filename, 'r') as f:
			for line in f:
				track_id, item_id = line.split()
				# Need to transform from local mapping to global mapping
				if item_id not in current_edge_mapping:
					current_edge_mapping[item_id] = hyper_edge_count
					hyper_edge_count += 1
				edge_pair = (current_edge_mapping[item_id], track_id)
				all_edges.append(edge_pair)
		f.close()

	playlist_file = os.getcwd() + '/Playlist_hypergraph.txt'
	current_edge_mapping = {}
	with open(playlist_file, 'r') as f:
		for line in f:
			track, playlist = line.split()
			if int(playlist) < 700:
				if playlist not in current_edge_mapping:
					current_edge_mapping[playlist] = hyper_edge_count
					hyper_edge_count += 1
				edge_pair = (current_edge_mapping[playlist], track_id)
				all_edges.append(edge_pair)

	outputFile = os.getcwd() + '/track_hypergraph_all_features.csv'
	with open(outputFile, 'w+') as f:
		for ep in all_edges:
			line = "{0},{1}\n".format(ep[0], ep[1])
			f.write(line)
	f.close()
	print("Finished making track hypergraph")


import unittest

class Queue:

    def __init__(self, size=10):
        self.size = size
        self.items = self.size * [None]
        self.head = 0
        self.actualSize = 0

    def is_empty(self):
        return self.actualSize == 0

    def is_full(self):
        return self.actualSize == self.size

    def put(self, item):
        if self.is_full():
            raise ValueError("Queue is full!")
        self.items[(self.head + self.actualSize) % self.size] = item
        self.actualSize += 1

    def get(self):
        if self.is_empty():
            raise ValueError("Queue is empty!")
        item = self.items[self.head]
        self.items[self.head] = None
        self.head = (self.head + 1) % self.size
        self.actualSize -= 1
        return item

class TestsQueue(unittest.TestCase):

    def testIsEmpty(self):
        queue = Queue()
        self.assertEqual(queue.is_empty(), True)
        queue.put(1)
        self.assertEqual(queue.is_empty(), False)
        queue.get()
        self.assertEqual(queue.is_empty(), True)

    def testIsFull(self):
        queue = Queue(3)
        self.assertEqual(queue.is_full(), False)
        queue.put(1)
        queue.put(2)
        self.assertEqual(queue.is_full(), False)
        queue.put(3)
        self.assertEqual(queue.is_full(), True)
        with self.assertRaises(ValueError) as context: queue.put(4)
        self.assertTrue("Queue is full!" in context.exception)

    def testPut(self):
        queue = Queue(3)
        queue.put(1)
        self.assertEqual(queue.is_empty(), False)
        self.assertEqual(queue.is_full(), False)
        queue.put(2)
        queue.put(3)
        with self.assertRaises(ValueError) as context: queue.put(4)
        self.assertTrue("Queue is full!" in context.exception)

    def testGet(self):
        queue = Queue(2)
        with self.assertRaises(ValueError) as context: queue.get()
        self.assertTrue("Queue is empty!" in context.exception)
        queue.put(1)
        queue.put(2)
        self.assertEqual(queue.get(), 1)
        self.assertEqual(queue.get(), 2)
        with self.assertRaises(ValueError) as context: queue.get()
        self.assertTrue("Queue is empty!" in context.exception)

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


from Accept import apply_globally_ordered_update_to_ds
from ClientFuntions import enqueue_unbound_pending_updates, remove_bound_updates_from_queue
from Proposal import send_proposals, apply_proposal_to_ds

from ProcessVariables import LEADER_ELECTION, REG_LEADER, REG_NON_LEADER
from MessageFormats import Prepare_Message, Prepare_OK
from NetworkFunctions import send_message, send_to_all_servers
from FileOps import write_to_file

# Flag only used for checking if the leader has installed the view, this is to prevent the leader to go into
# shift_to_reg_leader() every time a prepare ok comes after majority is reached.
leader_view_installed = False

# Print the current leader of this view, this also sets the current leader in my_info
def print_leader(my_info, total_hosts):
    current_view_leader = my_info.last_attempted % total_hosts
    print("\n<{0}> :: Server {1} is the new leader of view {2}.\n".format(my_info.pid, current_view_leader,
                                                                          my_info.last_attempted))
    my_info.current_leader_hostname = get_hostname_of_current_leader(current_view_leader, my_info)
    print_global_history(my_info)

# Function for checking the global history in each view:
def print_global_history(my_info):
    if len(my_info.global_history) > 0:
        print("\n**************************************************************")
        print("Global History so far:")
        for gh in my_info.global_history:
            print("\nGH Index Number:{0}".format(gh))
            if my_info.global_history[gh]['Proposal'] is not None:
                print("Proposal Details:")
                prop = my_info.global_history[gh]['Proposal']
                print("server_id:{0} view:{1} seq:{2}  update:{3}".format(prop.server_id, prop.view, prop.seq,
                                                                          prop.update.update))
            print("Accepts:")
            for a in my_info.global_history[gh]['Accepts']:
                print("server_id:{0} view:{1} seq:{2}".format(a.server_id, a.view, a.seq))

            if my_info.global_history[gh]['Globally_Ordered_Update'] is not None:
                print("Globally_Ordered_Update:")
                gbu = my_info.global_history[gh]['Globally_Ordered_Update']
                print("server_id:{0} seq:{1} update:{2}".format(gbu.server_id, gbu.seq, gbu.update.update))
        print("\n Local ARU: {0}".format(my_info.local_aru))
        print("**************************************************************\n")

# Check for conflicts in the received prepare message
def check_conflict_for_prepare_message(prepare_msg, my_info):
    if prepare_msg.server_id == my_info.pid:
        return True
    if prepare_msg.view != my_info.last_attempted:
        return True
    return False

# Update data structures for a prepare message:
def update_prepare_to_data_structures(prepare_msg, my_info):
    my_info.prepare = prepare_msg

# Update data structures on receiving a prepare ok:
def update_prepare_ok_to_data_structures(prepare_ok_msg, my_info):
    if prepare_ok_msg.server_id not in my_info.prepare_oks:
        my_info.prepare_oks[prepare_ok_msg.server_id] = prepare_ok_msg
    if prepare_ok_msg.data_list:
        for d in prepare_ok_msg.data_list:
            # If this is Proposal:
            if d is not None and d.type == 9:
                apply_proposal_to_ds(my_info, d)

            # If this is a Globally Ordered Update:
            if d is not None and d.type == 10:
                apply_globally_ordered_update_to_ds(my_info, d)

# Handle prepare_ok messages here:
def handle_prepare_ok(prepare_ok_msg, my_info, all_hosts):
    update_prepare_ok_to_data_structures(prepare_ok_msg, my_info)
    global leader_view_installed
    if not leader_view_installed:
        if view_prepared_ready(prepare_ok_msg.view, my_info, len(all_hosts)):
            print("\nPrepare phase over, let's shift to leader....\n")
            print_leader(my_info, len(all_hosts))
            shift_to_reg_leader(my_info)

# Check if you have received enough prepare_oks
def view_prepared_ready(view, my_info, total_hosts_number):
    if len(my_info.prepare_oks) >= (int(total_hosts_number / 2) + 1):
        for po in my_info.prepare_oks.values():
            if po.view == view:
                continue
            else:
                return False
        global leader_view_installed
        leader_view_installed = True
        return True

def handle_prepare_message(prepare_msg, my_info, address, total_hosts):
    # print("Handling prepare message")
    if my_info.state == LEADER_ELECTION:
        update_prepare_to_data_structures(prepare_msg, my_info)
        data_list = construct_data_list(my_info, prepare_msg.aru)
        prepare_ok = Prepare_OK(8, my_info.pid, prepare_msg.view, data_list)
        my_info.prepare_oks[my_info.pid] = prepare_ok
        shift_to_reg_non_leader(my_info)
        print("Sending Prepare OK to leader...")
        send_message(prepare_ok, address)
        print_leader(my_info, total_hosts)
    else:
        # Already installed the view
        if my_info.pid in my_info.prepare_oks:
            prepare_ok = my_info.prepare_oks.get(my_info.pid)
            send_message(prepare_ok, address)
        print_leader(my_info, total_hosts)

# Shift to prepare phase for leader:
def shift_to_prepare_phase(my_info, all_hosts):
    # print("In prepare phase, getting ready to send prepare to all servers...")
    my_info.last_installed = my_info.last_attempted
    prepare_msg = Prepare_Message(7, my_info.pid, my_info.last_installed, my_info.local_aru)
    update_prepare_to_data_structures(prepare_msg, my_info)
    data_list = construct_data_list(my_info, my_info.local_aru)
    prepare_ok = Prepare_OK(8, my_info.pid, my_info.last_installed, data_list)
    my_info.prepare_oks[my_info.pid] = prepare_ok
    my_info.last_enqueued.clear()
    write_to_file(my_info)
    send_to_all_servers(prepare_msg, all_hosts)

# Construct data_list to send to all leader
def construct_data_list(my_info, aru):
    # print("Making a data list")
    data_list = []
    for i in my_info.global_history:
        if i > aru and my_info.global_history[i]:
            if my_info.global_history[i]['Globally_Ordered_Update'] is not None:
                data_list.append(my_info.global_history[i].get('Globally_Ordered_Update'))
            else:
                data_list.append(my_info.global_history[i].get("Proposal"))
    return data_list

# Get the hostname of the current leader given the id of the server
def get_hostname_of_current_leader(server_id, my_info):
    for host in my_info.all_hosts:
        if host.split('r')[1] == str(server_id):
            return host
    return None

# Function to shift to regular non leader
def shift_to_reg_non_leader(my_info):
    my_info.state = REG_NON_LEADER
    my_info.last_installed = my_info.last_attempted
    my_info.update_queue.clear()
    write_to_file(my_info)

# Function to shift to regular leader
def shift_to_reg_leader(my_info):
    my_info.state = REG_LEADER
    enqueue_unbound_pending_updates(my_info)
    remove_bound_updates_from_queue(my_info)
    my_info.last_proposed = my_info.local_aru
    send_proposals(my_info)


import re
a='1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )'
a=a.replace(" ","")
def plus(value):
    value =value .replace(" ","")
    value =value .replace("+-","-")
    value =value  .replace("--","+")
    return value

def mul_dive(value_list):
    aa_list=[]
    for index,element in enumerate(value_list):
        aa=re.findall("[*/]",element)        #寻找
        bb=re.split("[*]",element)          #以* / 分割
        for i,j in enumerate(bb):
            #print(index, element,j)
            print(i,j)
            #print(j)
            #print(aa)
            if i == 1 :
                print('yes')

def backend(value):
    value =re.sub("[()]","",value)
    #value =plus(value )
    print(type(value))

#backend(a)
mul_dive(a)


from parent._heap import Heap

class MinHeap(Heap):
  """
  A binary tree that uses the minimum value at the root of its tree.
  """
  def __init__(self, items=None):
    super().__init__(items)

  def _siftup(self, position, data=None):
    """
    Moves an item up the heap to its correct location.
    """
    if data is not None:
      self.heap = data

    end_position = len(self.heap)
    start_position = len(self.heap)
    new_item = self.heap[position]

    # Bubble up the smaller child until reached a leaf
    child_position = self.left_child(position)

    while child_position < end_position:
      right_position = self.right_child(position)

      # Set child to index of smaller child
      if right_position < end_position and not self.heap[child_position] < self.heap[right_position]:
        child_position = right_position

      # Move the smaller child up
      self.heap[position] = self.heap[child_position]
      position = child_position
      child_position = self.right_child(position)

    # Bubble up new item to final place (shift parent down)
    self.heap[position] = new_item
    self._siftdown(start_position, position)

  def _siftdown(self, start_position, position):
    """
    Moves an item down the heap to its correct location.
    """
    new_item = self.heap[position]

    while position > start_position:
      parent_position = (position - 1) >> 1
      parent = self.heap[parent_position]

      # Compare items
      if new_item < parent:
        self.heap[position] = parent
        position = parent_position
        continue
      break

    self.heap[position] = new_item


"""ELASPIC TRAIN
"""
import os.path as op
import json
import pickle
import logging
import pandas as pd
from elaspic import DATA_DIR, CACHE_DIR, elaspic_predictor

logger = logging.getLogger(__name__)

def elaspic_train(args):
    """Train ELASPIC core and interface predictors."""
    _train_predictor('core')
    _train_predictor('interface')

def _train_predictor(prefix):
    """Train ELASPIC core (`prefix` == 'core') OR interface (`prefix` == interface) predictor."""
    logger.debug("_train_predictor(%s)", prefix)
    assert prefix in ['core', 'interface']
    # Load data
    data = pd.read_csv(
        op.join(DATA_DIR, '{}_training_set.tsv.gz'.format(prefix)),
        sep='\t', low_memory=False)
    with open(op.join(DATA_DIR, '{}_features.json'.format(prefix)), 'rt') as fh:
        features = json.load(fh)
    with open(op.join(DATA_DIR, '{}_options.json'.format(prefix)), 'rt') as fh:
        options = json.load(fh)
    # Train
    core_predictor = elaspic_predictor.get_final_predictor(data, features, options)
    # Save
    with open(op.join(CACHE_DIR, '{}_clf.pickle'.format(prefix)), 'wb') as fh:
        pickle.dump(core_predictor, fh, pickle.HIGHEST_PROTOCOL)

def configure_train_parser(sub_parsers):
    help = "Train the ELASPIC classifiers"
    description = help + """
"""
    example = """
Examples:

    elaspic train

"""
    parser = sub_parsers.add_parser(
        'train',
        help=help,
        description=description,
        epilog=example,
    )
    parser.set_defaults(func=elaspic_train)


'''
Optimal Utilization – Prime Air time
https://leetcode.com/discuss/interview-question/373202

 Given 2 lists a and b. Each element is a pair of integers where the first integer represents the unique id and the second integer represents a value. Your task is to find an element from a and an element form b such that the sum of their values is less or equal to target and as close to target as possible. Return a list of ids of selected elements. If no pair is possible, return an empty list.
Example 1:
Input:
a = [[1, 2], [2, 4], [3, 6]]
b = [[1, 2]]
target = 7
Output: [[2, 1]]
Explanation:
There are only three combinations [1, 1], [2, 1], and [3, 1], which have a
total sum of 4, 6 and 8, respectively.
Since 6 is the largest sum that does not exceed 7, [2, 1] is the optimal pair.
Example 2:
Input:
a = [[1, 3], [2, 5], [3, 7], [4, 10]] b = [[1, 2], [2, 3], [3, 4], [4, 5]] target = 10
Output: [[2, 4], [3, 2]]
Explanation:
There are two pairs possible. Element with id = 2 from the list `a` has
a value 5, and element with id = 4 from the list `b` also has a value 5. Combined, they add up to 10. Similarily, element with id = 3 from `a` has
a value 7, and element with id = 2 from `b` has a value 3.
These also add up to 10. Therefore, the optimal pairs are [2, 4] and [3, 2].
Example 3:
Input:
a = [[1, 8], [2, 7], [3, 14]]
b = [[1, 5], [2, 10], [3, 14]]
target = 20
Output: [[3, 1]]
Example 4:
Input:
a = [[1, 8], [2, 15], [3, 9]]
b = [[1, 8], [2, 11], [3, 12]]
target = 20
Output: [[1, 3], [3, 2]]

O(MlogM + NlogN) and two-pointer traversal is O(M + N), the final complexity can be
 regarded as O(KlogK) where K is the longest input array.
'''

class MaxShipping(object):
    def maxShippingDist(self, A, B, maxDist):
        if not A or not A[0]:
            return []
        if not B or not B[0]:
            return []
        res = []
        target = 0
        A.sort(key=lambda x: x[1])
        B.sort(key=lambda x: x[1], reverse=True)
        m, n = len(A), len(B)
        i, j = 0, 0
        while i < m and j < n:
            theSum = A[i][1] + b[j][1]
            if theSum > maxDist:
                j += 1
            elif theSum < maxDist:
                if theSum > target:
                    target = theSum
                i += 1
            else:
                target = maxDist
                break

        i, j = 0, 0
        res = []
        while i < m and j < n:
            theSum = A[i][1] + B[j][1]
            if theSum == targeT:
                res.append([A[i][0], B[j][0]])
                i, j = i + 1, j + 1
            elif theSum < target:
                i += 1
            else:
                j += 1
        return res

    def test(self):
        list1 = [[1, 8], [2, 15], [3, 9]]
        list2 = [[1, 8], [2, 11], [3, 12]]
        maxDist = 20
        res = self.maxShippingDist(list1, list2, maxDist)
        pause = 1


import tkinter as tk
from tkinter import *
from tkinter import ttk

window = tk.Tk()
window.title("Example")
window.geometry("300x200")

dogphoto = PhotoImage(file="dog.png")
label1 = tk.Label(window, image=dogphoto)
label2 = tk.Label(window, text="Pochacco!")
label3 = tk.Label(
    window, text=" A cuddly little puppy! This is from the same ", bg="#aaffff")
label4 = tk.Label(
    window, text="creators who bought you Keropi and Kero Kero", bg="#aaffff")

label1.place(x=70, y=50)
label2.place(x=140, y=85)
label3.place(x=15, y=150)
label4.place(x=15, y=170)

window.mainloop()


# encoding: utf-8
import ctypes
# pywin32
import win32api
import win32con
# pip install pygubu
from tkinter_test import *
from tkinter_test.filedialog import askdirectory

root = Tk()
path = StringVar()

def set_reg(full_path):
    reg_root = win32con.HKEY_CLASSES_ROOT
    # 键的路径（具体路径自行修改）
    reg_path = "Directory\\Background\\shell\\wallpaper"
    reg_path_children = reg_path + "\\command"
    # 权限和参数设置
    reg_flags = win32con.WRITE_OWNER | win32con.KEY_WOW64_64KEY | win32con.KEY_ALL_ACCESS
    try:
        # 判断注册表中键值是否存在
        key = win32api.RegOpenKeyEx(reg_root, reg_path, 0, reg_flags)
    except Exception as e:
        print(e)
        key = None
    key_flag = False
    if key is None:
        key_flag = True
    else:
        key_flag = False
    if key_flag:
        try:
            # 直接创建（若存在，则为获取）
            key, _ = win32api.RegCreateKeyEx(reg_root, reg_path, reg_flags)
            win32api.RegSetValueEx(key, '', 0, win32con.REG_SZ, '更换壁纸')
            key, _ = win32api.RegCreateKeyEx(reg_root, reg_path_children, reg_flags)
            win32api.RegSetValueEx(key, '', 0, win32con.REG_SZ, full_path)
            ctypes.windll.user32.MessageBoxW(0, '设置成功，请点击右键查看！！！', '右键快捷菜单设置（更换壁纸）', 0)
        except Exception as e:
            print(e)
            ctypes.windll.user32.MessageBoxW(0, '设置设置失败，为什么啊！！！', '右键快捷菜单设置（更换壁纸）', 0)
    else:
        value = ctypes.windll.user32.MessageBoxW(0, '更换壁纸快捷键已经存在，点击确认替换', '右键快捷菜单设置（更换壁纸）', 1)
        if value is 1:
            key, _ = win32api.RegCreateKeyEx(reg_root, reg_path_children, reg_flags)
            win32api.RegSetValueEx(key, '', 0, win32con.REG_SZ, full_path)
            ctypes.windll.user32.MessageBoxW(0, '设置成功，请点击右键查看！！！', '右键快捷菜单设置（更换壁纸）', 0)
        else:
            ctypes.windll.user32.MessageBoxW(0, '操作已取消', '右键快捷菜单设置（更换壁纸）', 0)
            pass

def select_path():
    path_ = askdirectory()
    path.set(path_)
    print(path.get())

def submit():
    full_path = path.get()
    if path.get():
        set_reg(full_path)
    else:
        ctypes.windll.user32.MessageBoxW(0, '路径不能为空，请选择.exe文件', '右键快捷菜单设置（更换壁纸）', 0)

def create_box():
    Label(root, text='目标路径:').grid(row=0, column=0)
    Entry(root, textvariable=path).grid(row=0, column=1)
    Button(root, text='路径选择', command=select_path).grid(row=0, column=2)
    Button(root, text='确定', command=submit).grid(row=0, column=3)
    root.mainloop()

if __name__ == "__main__":
    create_box()